Re: ComboBox: selecting items with children, Gtk2 vs Gtk3



On 07-07-2018 11:00, Eduardo M KALINOWSKI wrote:
On 05-07-2018 11:02, Eduardo M KALINOWSKI wrote:
Consider the sample attached: it builds a tree model with an item that
has child items, and uses that model in a ComboBox.

When compiled with Gtk2, when the item with children is selected and
its children displayed, and extra item representing the parent is
added to the top, allowing the parent to be selected. The attached
image should make that clearer.

However, compiled with Gtk3, that item that allows selection of the
parent is missing, as the other image shows.

So how does one select "Item with children", using the mouse, under
Gtk3? The item can be selected using the keyboard, but apparently with
the mouse. Is there a way to get the Gtk2 behaviour back with Gtk3?
The attachments didn't make it, so here's the code:

----
#include <gtk/gtk.h>

GtkTreeModel *create_model()
{
  GtkTreeStore *model;
  GtkTreeIter iter, child;

  model = gtk_tree_store_new(1, G_TYPE_STRING);

  gtk_tree_store_append(model, &iter, NULL);
  gtk_tree_store_set(model, &iter, 0, "Item with no children", -1);

  gtk_tree_store_append(model, &iter, NULL);
  gtk_tree_store_set(model, &iter, 0, "Item with children", -1);

  gtk_tree_store_append(model, &child, &iter);
  gtk_tree_store_set(model, &child, 0, "Child 1", -1);

  gtk_tree_store_append(model, &child, &iter);
  gtk_tree_store_set(model, &child, 0, "Child 2", -1);

  return GTK_TREE_MODEL(model);
}


void on_changed(GtkComboBox *widget, gpointer data)
{
  GtkComboBox *combo = widget;
  GtkTreeIter iter;
  gchar *selected_text;

  if (gtk_combo_box_get_active_iter(combo, &iter)) {
    gtk_tree_model_get(gtk_combo_box_get_model(combo), &iter,
                       0, &selected_text, -1);
    printf("Selected: %s\n", selected_text);
  }
}


GtkWidget *create_combo(GtkTreeModel *model)
{
  GtkWidget *combo;
  GtkCellRenderer *column;

  combo = gtk_combo_box_new_with_model(model);
  g_object_unref(model);

  column = gtk_cell_renderer_text_new();
  gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), column, TRUE);
  gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), column,
                                 "text", 0, NULL);

  g_signal_connect(combo, "changed", G_CALLBACK(on_changed), NULL);

  return combo;
}


int main(int argc, char **argv)
{
  GtkWidget *window, *combo;
  GtkTreeModel *model;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  g_signal_connect(window, "delete_event", G_CALLBACK(gtk_main_quit), NULL);

  model = create_model();
  combo = create_combo(model);

  gtk_container_add(GTK_CONTAINER(window), combo);

  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}
---

And the screenshots:
- Gtk2: https://imgur.com/a/kcYx1Wt
- Gtk3: https://imgur.com/a/VsuSz73
In case someone finds this thread in the future, I opened a bug report
(https://gitlab.gnome.org/GNOME/gtk/issues/1229), and the change that
resulted in the new Gtk3 behavior was quickly identified, but nothing else.

Ideally the Gtk2 behavior should be restored, but judging by previous
experience, I doubt that will happen. So I made a small wrapper that
restores the previous behavior (at least for my use case):
https://github.com/ekalin/kgtk3-combo-model


-- 
Indifference will certainly be the downfall of mankind, but who cares?

Eduardo M KALINOWSKI
eduardo kalinowski com br



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]