From 9650830860b7adea70f1944f80bcafc37880f142 Mon Sep 17 00:00:00 2001 From: Toshio Sekiya Date: Sat, 4 Feb 2023 21:52:42 +0900 Subject: [PATCH] Bug fixed (section 30, list editor). --- docs/sec30.html | 173 +++++++++++++++++++++-------------- gfm/sec30.md | 159 ++++++++++++++++++++------------ src/listeditor/listeditor.c | 35 +++++-- src/listeditor/listeditor.ui | 9 +- src/sec30.src.md | 70 +++++++++----- 5 files changed, 284 insertions(+), 162 deletions(-) diff --git a/docs/sec30.html b/docs/sec30.html index 89cf420..24db3ce 100644 --- a/docs/sec30.html +++ b/docs/sec30.html @@ -186,53 +186,59 @@ class="sourceCode numberSource C numberLines">setup2_cb (GtkListItemFactory *factory, GtkListItem *listitem) { GtkWidget *text = gtk_text_new (); gtk_list_item_set_child (listitem, GTK_WIDGET (text)); -} - -static void -bind2_cb (GtkListItemFactory *factory, GtkListItem *listitem) { - GtkWidget *text = gtk_list_item_get_child (listitem); - LeData *data = LE_DATA (gtk_list_item_get_item (listitem)); - GtkEntryBuffer *buffer; - GBinding *bind; - - buffer = gtk_text_get_buffer (GTK_TEXT (text)); - gtk_entry_buffer_set_text (buffer, le_data_look_string (data), -1); - bind = g_object_bind_property (buffer, "text", data, "string", G_BINDING_DEFAULT); - g_object_set_data (G_OBJECT (listitem), "bind", bind); -} - -static void -unbind2_cb (GtkListItemFactory *factory, GtkListItem *listitem) { - GBinding *bind = G_BINDING (g_object_get_data (G_OBJECT (listitem), "bind")); - - g_binding_unbind(bind); - g_object_set_data (G_OBJECT (listitem), "bind", NULL); -} + gtk_editable_set_alignment (GTK_EDITABLE (text), 0.0); +} + +static void +bind2_cb (GtkListItemFactory *factory, GtkListItem *listitem) { + GtkWidget *text = gtk_list_item_get_child (listitem); + GtkEntryBuffer *buffer = gtk_text_get_buffer (GTK_TEXT (text)); + LeData *data = LE_DATA (gtk_list_item_get_item (listitem)); + GBinding *bind; + + gtk_editable_set_text (GTK_EDITABLE (text), le_data_look_string (data)); + gtk_editable_set_position (GTK_EDITABLE (text), 0); + + bind = g_object_bind_property (buffer, "text", data, "string", G_BINDING_DEFAULT); + g_object_set_data (G_OBJECT (listitem), "bind", bind); +} + +static void +unbind2_cb (GtkListItemFactory *factory, GtkListItem *listitem) { + GBinding *bind = G_BINDING (g_object_get_data (G_OBJECT (listitem), "bind")); + + g_binding_unbind(bind); + g_object_set_data (G_OBJECT (listitem), "bind", NULL); +}

This technique is not so complicated. You can use it when you make a @@ -271,6 +277,10 @@ moves.

  • When the current line is deleted, no line will be current.
  • When a button in the first column of GtkColumnView is clicked, the line will be current.
  • +
  • It is necessary to set the line status (whether current or not) when +a GtkListItem is bound to an item in the list. It is because GtkListItem +is recycled. A GtkListItem was possibly current line before but not +current after recycled. The opposite can also be happen.
  • The button of the current line is colored with red and otherwise white.

    @@ -284,7 +294,7 @@ the item in the list. It is the opposite direction from gtk_list_item_get_item function. To accomplish this, we set a listitem element of LeData to point the corresponding GtkListItem instance. Therefore, items (LeData) in the list always know -the GtkListItem. If there’s no GtkListItem binded to the item, NULL is +the GtkListItem. If there’s no GtkListItem bound to the item, NULL is assigned.

    void
    @@ -298,42 +308,56 @@ class="sourceCode numberSource C numberLines">setup1_cb (GtkListItemFactory *factory, GtkListItem *listitem) {
       GtkWidget *button = gtk_button_new ();
       gtk_list_item_set_child (listitem, button);
    -  g_signal_connect (button, "clicked", G_CALLBACK (select_cb), listitem);
    -}
    -
    -static void
    -bind1_cb (GtkListItemFactory *factory, GtkListItem *listitem) {
    -  LeData *data = LE_DATA (gtk_list_item_get_item (listitem));
    -  if (data)
    -    le_data_set_listitem (data, listitem);
    -}
    -
    -static void
    -unbind1_cb (GtkListItemFactory *factory, GtkListItem *listitem) {
    -  LeData *data = LE_DATA (gtk_list_item_get_item (listitem));
    -  if (data)
    -    le_data_set_listitem (data, NULL);
    -}
    + gtk_widget_set_focusable (GTK_WIDGET (button), FALSE); + g_signal_connect (button, "clicked", G_CALLBACK (select_cb), listitem); +} + +static void +bind1_cb (GtkListItemFactory *factory, GtkListItem *listitem, gpointer user_data) { + LeWindow *win = LE_WINDOW (user_data); + GtkWidget *button = gtk_list_item_get_child (listitem); + const char *non_current[1] = {NULL}; + const char *current[2] = {"current", NULL}; + LeData *data = LE_DATA (gtk_list_item_get_item (listitem)); + + if (data) { + le_data_set_listitem (data, listitem); + if (win->position == gtk_list_item_get_position (listitem)) + gtk_widget_set_css_classes (GTK_WIDGET (button), current); + else + gtk_widget_set_css_classes (GTK_WIDGET (button), non_current); + } +} + +static void +unbind1_cb (GtkListItemFactory *factory, GtkListItem *listitem) { + LeData *data = LE_DATA (gtk_list_item_get_item (listitem)); + if (data) + le_data_set_listitem (data, NULL); +}