1

I have the following GTK+ program which uses an Glade generated XML file to construct its GUI.

#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

////////////////////////////////////////////////////
// Example can be compiled with:
// gcc gui.c `pkg-config --cflags --libs gtk+-2.0`
//
// GUI file from glade must be in same folder from
// which the compiled binary is called.
////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
    GtkBuilder* gtkBuilder = gtk_builder_new();

    gtk_init(&argc, &argv);

    gtk_builder_add_from_file(gtkBuilder, "gui.glade", NULL);

    GtkTreeStore* treestore = GTK_TREE_STORE(gtk_builder_get_object(gtkBuilder, "treestore"));

    GtkTreeIter iterChild;

    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "a", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "b", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "m", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "k", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "g", -1);

    gtk_builder_connect_signals(gtkBuilder, NULL);

    gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "main")));

    g_object_unref(G_OBJECT(gtkBuilder));

    gtk_main();

    return 0;
}

The XML Glade file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkTreeStore" id="treestore">
    <columns>
      <!-- column-name col -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <property name="default_width">690</property>
    <property name="default_height">500</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkTreeView" id="treeview">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="model">treestore</property>
        <property name="reorderable">True</property>
        <property name="level_indentation">1</property>
        <property name="enable_tree_lines">True</property>
        <child>
          <object class="GtkTreeViewColumn" id="treeviewcolumn">
            <property name="sizing">fixed</property>
            <property name="title" translatable="yes">column</property>
            <property name="clickable">True</property>
            <property name="sort_indicator">True</property>
            <property name="sort_column_id">0</property>
            <child>
              <object class="GtkCellRendererText" id="treeviewrenderer">
                <property name="height">30</property>
              </object>
              <attributes>
                <attribute name="text">0</attribute>
              </attributes>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

When the application starts the rows can be reordered by Drag 'n' Drop. After the column header is clicked the rows will be sorted. Afterwards they can not be rearranged by Drag 'n' Drop anymore.

Is there any way to reactivate manual reordering after the rows have been sorted?

I have tried, e.g., calling gtk_tree_view_set_reorderable() from within a callback function (callback was on_cursor_moved()) but this did not work.

Is it possible at all to switch between sorted and reorderable rows when using a Glade XML file to construct the GUI?

1 Answers1

1

You need to wrap the treemodel with a treesort, then every third click disables the sorting.

Glade file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <object class="GtkTreeStore" id="treestore">
    <columns>
      <!-- column-name col -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkTreeModelSort" id="treesort">
    <property name="model">treestore</property>
  </object>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <property name="default_width">690</property>
    <property name="default_height">500</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkTreeView" id="treeview">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="model">treesort</property>
        <property name="reorderable">True</property>
        <property name="level_indentation">1</property>
        <property name="enable_tree_lines">True</property>
        <child internal-child="selection">
          <object class="GtkTreeSelection" id="treeview-selection1"/>
        </child>
        <child>
          <object class="GtkTreeViewColumn" id="treeviewcolumn">
            <property name="sizing">fixed</property>
            <property name="title" translatable="yes">column</property>
            <property name="clickable">True</property>
            <property name="sort_indicator">True</property>
            <property name="sort_column_id">0</property>
            <child>
              <object class="GtkCellRendererText" id="treeviewrenderer">
                <property name="height">30</property>
              </object>
              <attributes>
                <attribute name="text">0</attribute>
              </attributes>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>
theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • This restores the previous order but I still cannot reorder it manually by drag and drop. –  Feb 14 '18 at 16:40
  • Can you reorder at all with drag and drop? Sorry, for some reason your program doesn't run for me and I am not good at C. – theGtknerd Feb 14 '18 at 17:29
  • Ok, I got it going. But your program is Gtk2 and I only know Gtk3. – theGtknerd Feb 14 '18 at 17:57
  • I can reorder with my version until I click the column and sort the treeview. If I wrap the treemodel in a treesort then I cannot reorder at all. –  Feb 14 '18 at 18:34
  • Of [interest](https://stackoverflow.com/questions/13882076/how-to-have-drag-and-drop-and-sorted-gtktreeview-in-gtk3). – theGtknerd Feb 14 '18 at 18:57