diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-01-27 16:40:01 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-01-28 13:17:44 -0800 |
commit | 77ece3fccb1a08ef8eea10a9f548bc6a4a3c889e (patch) | |
tree | 71dd23e661b88cf690779193a8579a3c6aaa944b | |
parent | 32e497b5744ef690e91b4a9f22a63109dde4c7df (diff) | |
download | subsurface-77ece3fccb1a08ef8eea10a9f548bc6a4a3c889e.tar.gz |
Clean up gtk combo box handling
This cleans up our handling of combo boxes and all the duplicated
completion logic, and simplifies the code.
In particular, we get rid of the deprecated GtkComboBoxEntry. While it
made some things easier, it made other things harder. Just using
GtkComboBox and setting that up correctly ends up being simpler, and
also makes the logic work with gtk-3.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | display-gtk.h | 6 | ||||
-rw-r--r-- | download-dialog.c | 7 | ||||
-rw-r--r-- | equipment.c | 85 | ||||
-rw-r--r-- | gtk-gui.c | 36 | ||||
-rw-r--r-- | info.c | 34 | ||||
-rw-r--r-- | planner.c | 15 |
6 files changed, 84 insertions, 99 deletions
diff --git a/display-gtk.h b/display-gtk.h index df29ff1d8..cd8496d2a 100644 --- a/display-gtk.h +++ b/display-gtk.h @@ -72,6 +72,12 @@ extern GtkWidget *weightsystem_list_widget(int w_idx); extern GtkWidget *dive_list_create(void); extern void dive_list_destroy(void); +/* Helper functions for gtk combo boxes */ +extern GtkEntry *get_entry(GtkComboBox *); +extern const char *get_active_text(GtkComboBox *); +extern void set_active_text(GtkComboBox *, const char *); +extern GtkWidget *combo_box_with_model_and_entry(GtkListStore *); + extern gboolean icon_click_cb(GtkWidget *w, GdkEventButton *event, gpointer data); unsigned int amount_selected; diff --git a/download-dialog.c b/download-dialog.c index 73ed4bac9..6237004d9 100644 --- a/download-dialog.c +++ b/download-dialog.c @@ -330,7 +330,7 @@ static GtkComboBox *dc_device_selector(GtkWidget *vbox) frame = gtk_frame_new(_("Device or mount point")); gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 3); - combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0); + combo_box = combo_box_with_model_and_entry(model); gtk_container_add(GTK_CONTAINER(frame), combo_box); renderer = gtk_cell_renderer_text_new(); @@ -340,8 +340,7 @@ static GtkComboBox *dc_device_selector(GtkWidget *vbox) gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), default_index); else if (default_dive_computer_device) - gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box))), - default_dive_computer_device); + set_active_text(GTK_COMBO_BOX(combo_box), default_dive_computer_device); return GTK_COMBO_BOX(combo_box); } @@ -430,7 +429,7 @@ repeat: set_default_dive_computer(vendor, product); /* get the device name from the combo box entry and set as default */ - devname = strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(device))))); + devname = strdup(get_active_text(device)); set_default_dive_computer_device(devname); /* clear leading and trailing white space from the device name and also * everything after (and including) the first '(' char. */ diff --git a/equipment.c b/equipment.c index 416c2c853..98162c9b9 100644 --- a/equipment.c +++ b/equipment.c @@ -119,6 +119,12 @@ static int convert_weight(int grams, double *m) return decimals; } +static void set_cylinder_description(struct cylinder_widget *cylinder, const char *desc) +{ + set_active_text(cylinder->description, desc); +} + + static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar) { double volume, pressure; @@ -151,6 +157,11 @@ static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->end), pressure); } +static void set_weight_description(struct ws_widget *ws_widget, const char *desc) +{ + set_active_text(ws_widget->description, desc); +} + static void set_weight_weight_spinbutton(struct ws_widget *ws_widget, int grams) { double weight; @@ -185,17 +196,16 @@ static gboolean match_desc(GtkTreeModel *model, GtkTreePath *path, static int get_active_item(GtkComboBox *combo_box, GtkTreeIter *iter, GtkListStore *model) { - char *desc; + const char *desc; if (gtk_combo_box_get_active_iter(combo_box, iter)) return TRUE; - desc = gtk_combo_box_get_active_text(combo_box); + desc = get_active_text(combo_box); found_match = NULL; gtk_tree_model_foreach(GTK_TREE_MODEL(model), match_desc, (void *)desc); - g_free(desc); if (!found_match) return FALSE; @@ -232,10 +242,9 @@ static void cylinder_cb(GtkComboBox *combo_box, gpointer data) */ if (!cylinder->changed && cyl->type.description) { int same; - char *desc = gtk_combo_box_get_active_text(combo_box); + const char *desc = get_active_text(combo_box); same = !strcmp(desc, cyl->type.description); - g_free(desc); if (same) return; } @@ -276,10 +285,9 @@ static void weight_cb(GtkComboBox *combo_box, gpointer data) */ if (!ws_widget->changed && ws->description) { int same; - char *desc = gtk_combo_box_get_active_text(combo_box); + const char *desc = get_active_text(combo_box); same = !strcmp(desc, ws->description); - g_free(desc); if (same) return; } @@ -361,12 +369,10 @@ void add_cylinder_description(cylinder_type_t *type) static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar) { - GtkTreeIter iter, *match; + GtkTreeIter iter; cylinder->name = desc; - match = add_cylinder_type(desc, ml, mbar, &iter); - if (match) - gtk_combo_box_set_active_iter(cylinder->description, match); + add_cylinder_type(desc, ml, mbar, &iter); } void add_weightsystem_description(weightsystem_t *weightsystem) @@ -384,12 +390,10 @@ void add_weightsystem_description(weightsystem_t *weightsystem) static void add_weightsystem(struct ws_widget *ws_widget, const char *desc, int weight) { - GtkTreeIter iter, *match; + GtkTreeIter iter; ws_widget->name = desc; - match = add_weightsystem_type(desc, weight, &iter); - if (match) - gtk_combo_box_set_active_iter(ws_widget->description, match); + add_weightsystem_type(desc, weight, &iter); } static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder) @@ -410,6 +414,7 @@ static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder) mbar = cyl->type.workingpressure.mbar; add_cylinder(cylinder, desc, ml, mbar); + set_cylinder_description(cylinder, desc); set_cylinder_type_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar); set_cylinder_pressure_spinbuttons(cylinder, cyl); @@ -442,6 +447,7 @@ static void show_weightsystem(weightsystem_t *ws, struct ws_widget *weightsystem grams = ws->weight.grams; add_weightsystem(weightsystem_widget, desc, grams); + set_weight_description(weightsystem_widget, desc); set_weight_weight_spinbutton(weightsystem_widget, ws->weight.grams); } @@ -679,7 +685,7 @@ static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cyl if (!box) return; - desc = gtk_combo_box_get_active_text(box); + desc = strdup(get_active_text(box)); volume = gtk_spin_button_get_value(cylinder->size); pressure = gtk_spin_button_get_value(cylinder->pressure); if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) { @@ -711,7 +717,7 @@ static void record_weightsystem_changes(weightsystem_t *ws, struct ws_widget *we if (!box) return; - desc = gtk_combo_box_get_active_text(box); + desc = strdup(get_active_text(box)); value = gtk_spin_button_get_value(weightsystem_widget->weight); if (prefs.units.weight == LBS) @@ -869,16 +875,6 @@ static void pressure_cb(GtkToggleButton *button, gpointer data) gtk_widget_set_sensitive(cylinder->end, state); } -static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder) -{ - const char *desc; - unsigned int ml, mbar; - - gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1); - add_cylinder(cylinder, desc, ml, mbar); - return TRUE; -} - static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data) { struct cylinder_widget *cylinder = data; @@ -923,8 +919,6 @@ static GtkWidget *labeled_spinbutton(GtkWidget *box, const char *name, double mi static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model) { GtkWidget *frame, *hbox; - GtkEntry *entry; - GtkEntryCompletion *completion; GtkWidget *widget; /* @@ -936,21 +930,13 @@ static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, G hbox = gtk_hbox_new(FALSE, 3); gtk_container_add(GTK_CONTAINER(frame), hbox); - widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0); + widget = combo_box_with_model_and_entry(model); gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0); cylinder->description = GTK_COMBO_BOX(widget); g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder); - entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(widget))); - g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder); - - completion = gtk_entry_completion_new(); - gtk_entry_completion_set_text_column(completion, 0); - gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model)); - g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder); - gtk_entry_set_completion(entry, completion); - g_object_unref(completion); + g_signal_connect(get_entry(GTK_COMBO_BOX(widget)), "activate", G_CALLBACK(cylinder_activate_cb), cylinder); hbox = gtk_hbox_new(FALSE, 3); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); @@ -991,16 +977,6 @@ static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, G g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(gasmix_cb), cylinder); } -static gboolean weight_completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct ws_widget *ws_widget) -{ - const char *desc; - unsigned int weight; - - gtk_tree_model_get(model, iter, WS_DESC, &desc, WS_WEIGHT, &weight, -1); - add_weightsystem(ws_widget, desc, weight); - return TRUE; -} - static void weight_activate_cb(GtkComboBox *combo_box, gpointer data) { struct ws_widget *ws_widget = data; @@ -1010,7 +986,6 @@ static void weight_activate_cb(GtkComboBox *combo_box, gpointer data) static void ws_widget(GtkWidget *vbox, struct ws_widget *ws_widget, GtkListStore *model) { GtkWidget *frame, *hbox; - GtkEntryCompletion *completion; GtkWidget *widget; GtkEntry *entry; @@ -1022,21 +997,15 @@ static void ws_widget(GtkWidget *vbox, struct ws_widget *ws_widget, GtkListStore hbox = gtk_hbox_new(FALSE, 3); gtk_container_add(GTK_CONTAINER(frame), hbox); - widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0); + widget = combo_box_with_model_and_entry(model); gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0); ws_widget->description = GTK_COMBO_BOX(widget); g_signal_connect(widget, "changed", G_CALLBACK(weight_cb), ws_widget); - entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(widget))); + entry = get_entry(GTK_COMBO_BOX(widget)); g_signal_connect(entry, "activate", G_CALLBACK(weight_activate_cb), ws_widget); - completion = gtk_entry_completion_new(); - gtk_entry_completion_set_text_column(completion, 0); - gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model)); - g_signal_connect(completion, "match-selected", G_CALLBACK(weight_completion_cb), ws_widget); - gtk_entry_set_completion(entry, completion); - hbox = gtk_hbox_new(FALSE, 3); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0); @@ -415,6 +415,42 @@ GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char return col; } +/* Helper functions for gtk combo boxes */ +GtkEntry *get_entry(GtkComboBox *combo_box) +{ + return GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box))); +} + +const char *get_active_text(GtkComboBox *combo_box) +{ + return gtk_entry_get_text(get_entry(combo_box)); +} + +void set_active_text(GtkComboBox *combo_box, const char *text) +{ + gtk_entry_set_text(get_entry(combo_box), text); +} + +GtkWidget *combo_box_with_model_and_entry(GtkListStore *model) +{ + GtkWidget *widget; + GtkEntryCompletion *completion; + + widget = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(model)); + gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(widget), 0); + + completion = gtk_entry_completion_new(); + gtk_entry_completion_set_text_column(completion, 0); + gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model)); + gtk_entry_completion_set_inline_completion(completion, TRUE); + gtk_entry_completion_set_inline_selection(completion, TRUE); + gtk_entry_completion_set_popup_single_match(completion, FALSE); + gtk_entry_set_completion(get_entry(GTK_COMBO_BOX(widget)), completion); + g_object_unref(completion); + + return widget; +} + static void create_radio(GtkWidget *vbox, const char *w_name, ...) { va_list args; @@ -61,12 +61,11 @@ static const char *skip_space(const char *str) * The "master" string is the string of the current dive - we only consider it * changed if the old string is either empty, or matches that master string. */ -static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp, const char *master) +static char *get_combo_box_entry_text(GtkComboBox *combo_box, char **textp, const char *master) { char *old = *textp; const char *old_text; const gchar *new; - GtkEntry *entry; old_text = skip_space(old); master = skip_space(master); @@ -81,8 +80,7 @@ static char *get_combo_box_entry_text(GtkComboBoxEntry *combo_box, char **textp, if (strcmp(master, old_text)) return NULL; - entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box))); - new = gtk_entry_get_text(entry); + new = get_active_text(combo_box); while (isspace(*new)) new++; /* If the master string didn't change, don't change other dives either! */ @@ -202,7 +200,7 @@ static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data) edit_multi_dive_info(NULL); } -static void add_menu_item(GtkMenu *menu, const char *label, const char *icon, void (*cb)(GtkMenuItem *, gpointer)) +static void add_menu_item(GtkMenuShell *menu, const char *label, const char *icon, void (*cb)(GtkMenuItem *, gpointer)) { GtkWidget *item; if (icon) { @@ -215,10 +213,10 @@ static void add_menu_item(GtkMenu *menu, const char *label, const char *icon, vo } g_signal_connect(item, "activate", G_CALLBACK(cb), NULL); gtk_widget_show(item); /* Yes, really */ - gtk_menu_prepend(menu, item); + gtk_menu_shell_prepend(menu, item); } -static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data) +static void populate_popup_cb(GtkTextView *entry, GtkMenuShell *menu, gpointer user_data) { if (amount_selected) add_menu_item(menu, _("Edit"), GTK_STOCK_EDIT, info_menu_edit_cb); @@ -251,32 +249,20 @@ static GtkEntry *single_text_entry(GtkWidget *box, const char *label, const char return entry; } -static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text) +static GtkComboBox *text_entry(GtkWidget *box, const char *label, GtkListStore *completions, const char *text) { - GtkEntry *entry; GtkWidget *combo_box; GtkWidget *frame = gtk_frame_new(label); - GtkEntryCompletion *completion; gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0); - combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0); + combo_box = combo_box_with_model_and_entry(completions); gtk_container_add(GTK_CONTAINER(frame), combo_box); - entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo_box))); if (text && *text) - gtk_entry_set_text(entry, text); - - completion = gtk_entry_completion_new(); - gtk_entry_completion_set_text_column(completion, 0); - gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions)); - gtk_entry_completion_set_inline_completion(completion, TRUE); - gtk_entry_completion_set_inline_selection(completion, TRUE); - gtk_entry_completion_set_popup_single_match(completion, FALSE); - gtk_entry_set_completion(entry, completion); - g_object_unref(completion); + set_active_text(GTK_COMBO_BOX(combo_box), text); - return GTK_COMBO_BOX_ENTRY(combo_box); + return GTK_COMBO_BOX(combo_box); } enum writable { @@ -518,7 +504,7 @@ static gboolean gps_changed(struct dive *dive, struct dive *master, const char * } struct dive_info { - GtkComboBoxEntry *location, *divemaster, *buddy, *rating, *suit, *viz; + GtkComboBox *location, *divemaster, *buddy, *rating, *suit, *viz; GtkEntry *airtemp, *gps; GtkTextView *notes; }; @@ -931,7 +931,7 @@ static void gas_changed_cb(GtkWidget *combo, gpointer data) int o2, he; int idx = data - NULL; - gastext = gtk_combo_box_get_active_text(GTK_COMBO_BOX(combo)); + gastext = get_active_text(GTK_COMBO_BOX(combo)); /* stupidly this gets called for two reasons: * a) any keystroke into the entry field * b) mouse selection of a dropdown @@ -1045,8 +1045,6 @@ static gboolean gf_focus_out_cb(GtkWidget *entry, GdkEvent * event, gpointer dat static GtkWidget *add_gas_combobox_to_box(GtkWidget *box, const char *label, int idx) { GtkWidget *frame, *combo; - GtkEntryCompletion *completion; - GtkEntry *entry; if (!gas_model) { gas_model = gtk_list_store_new(1, G_TYPE_STRING); @@ -1054,7 +1052,7 @@ static GtkWidget *add_gas_combobox_to_box(GtkWidget *box, const char *label, int add_string_list_entry("EAN32", gas_model); add_string_list_entry("EAN36 @ 1.6", gas_model); } - combo = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(gas_model), 0); + combo = combo_box_with_model_and_entry(gas_model); gtk_widget_add_events(combo, GDK_FOCUS_CHANGE_MASK); g_signal_connect(gtk_bin_get_child(GTK_BIN(combo)), "focus-out-event", G_CALLBACK(gas_focus_out_cb), NULL + idx); g_signal_connect(combo, "changed", G_CALLBACK(gas_changed_cb), NULL + idx); @@ -1065,15 +1063,6 @@ static GtkWidget *add_gas_combobox_to_box(GtkWidget *box, const char *label, int } else { gtk_box_pack_start(GTK_BOX(box), combo, FALSE, FALSE, 2); } - entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))); - completion = gtk_entry_completion_new(); - gtk_entry_completion_set_text_column(completion, 0); - gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(gas_model)); - gtk_entry_completion_set_inline_completion(completion, TRUE); - gtk_entry_completion_set_inline_selection(completion, TRUE); - gtk_entry_completion_set_popup_single_match(completion, FALSE); - gtk_entry_set_completion(entry, completion); - g_object_unref(completion); return combo; } |