1 /*
2 * A subclass of GtkAction that wraps a GtkComboBoxEntry.
3 * Features:
4 * Setting GtkEntryBox width in characters.
5 * Passing a function for formatting cells.
6 * Displaying a warning if text isn't in list.
7 *
8 * Author(s):
9 * Tavmjong Bah
10 *
11 * Copyright (C) 2010 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 /*
17 * We must provide for both a toolbar item and a menu item.
18 * As we don't know which widgets are used (or even constructed),
19 * we must keep track of things like active entry ourselves.
20 */
22 #include <iostream>
23 #include <string.h>
25 #include <gtk/gtk.h>
26 #include <gtk/gtktoolitem.h>
27 #include <gtk/gtkcomboboxentry.h>
28 #include <gtk/gtkentrycompletion.h>
30 #include "ink-comboboxentry-action.h"
32 // Must handle both tool and menu items!
33 static GtkWidget* create_tool_item( GtkAction* action );
34 static GtkWidget* create_menu_item( GtkAction* action );
36 // Internal
37 static gint get_active_row_from_text( Ink_ComboBoxEntry_Action* action, gchar* target_text );
39 // Callbacks
40 static void combo_box_changed_cb( GtkComboBoxEntry* widget, gpointer data );
41 static void entry_activate_cb( GtkEntry* widget, gpointer data );
42 static gboolean match_selected_cb( GtkEntryCompletion* widget, GtkTreeModel* model, GtkTreeIter* iter, gpointer data );
44 enum {
45 PROP_MODEL = 1,
46 PROP_COMBOBOX,
47 PROP_ENTRY,
48 PROP_WIDTH,
49 PROP_CELL_DATA_FUNC,
50 PROP_POPUP
51 };
53 enum {
54 CHANGED = 0,
55 ACTIVATED,
56 N_SIGNALS
57 };
58 static guint signals[N_SIGNALS] = {0};
60 static GtkActionClass *ink_comboboxentry_action_parent_class = NULL;
61 static GQuark gDataName = 0;
63 static void ink_comboboxentry_action_finalize (GObject *object)
64 {
65 // Free any allocated resources.
67 G_OBJECT_CLASS (ink_comboboxentry_action_parent_class)->finalize (object);
68 }
71 static void ink_comboboxentry_action_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
72 {
73 Ink_ComboBoxEntry_Action *action = INK_COMBOBOXENTRY_ACTION (object);
75 switch(property_id) {
77 case PROP_MODEL:
78 action->model = GTK_TREE_MODEL( g_value_get_object( value ));
79 break;
81 case PROP_COMBOBOX:
82 action->combobox = GTK_COMBO_BOX_ENTRY( g_value_get_object( value ));
83 break;
85 case PROP_ENTRY:
86 action->entry = GTK_ENTRY( g_value_get_object( value ));
87 break;
89 case PROP_WIDTH:
90 action->width = g_value_get_int( value );
91 break;
93 case PROP_CELL_DATA_FUNC:
94 action->cell_data_func = g_value_get_pointer( value );
95 break;
97 case PROP_POPUP:
98 action->popup = g_value_get_boolean( value );
99 break;
101 default:
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
103 }
104 }
107 static void ink_comboboxentry_action_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
108 {
109 Ink_ComboBoxEntry_Action *action = INK_COMBOBOXENTRY_ACTION (object);
111 switch(property_id) {
113 case PROP_MODEL:
114 g_value_set_object (value, action->model);
115 break;
117 case PROP_COMBOBOX:
118 g_value_set_object (value, action->combobox);
119 break;
121 case PROP_ENTRY:
122 g_value_set_object (value, action->entry);
123 break;
125 case PROP_WIDTH:
126 g_value_set_int (value, action->width);
127 break;
129 case PROP_CELL_DATA_FUNC:
130 g_value_set_pointer (value, action->cell_data_func);
131 break;
133 case PROP_POPUP:
134 g_value_set_boolean (value, action->popup);
135 break;
137 default:
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139 }
140 }
142 static void
143 ink_comboboxentry_action_connect_proxy (GtkAction *action,
144 GtkWidget *proxy)
145 {
146 /* Override any proxy properties. */
147 // if (GTK_IS_MENU_ITEM (proxy)) {
148 // }
150 GTK_ACTION_CLASS (ink_comboboxentry_action_parent_class)->connect_proxy (action, proxy);
151 }
154 static void ink_comboboxentry_action_class_init (Ink_ComboBoxEntry_ActionClass *klass)
155 {
157 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
158 GtkActionClass *gtkaction_class = GTK_ACTION_CLASS (klass);
160 gtkaction_class->connect_proxy = ink_comboboxentry_action_connect_proxy;
162 gobject_class->finalize = ink_comboboxentry_action_finalize;
163 gobject_class->set_property = ink_comboboxentry_action_set_property;
164 gobject_class->get_property = ink_comboboxentry_action_get_property;
166 gDataName = g_quark_from_string("ink_comboboxentry-action");
168 klass->parent_class.create_tool_item = create_tool_item;
169 klass->parent_class.create_menu_item = create_menu_item;
171 ink_comboboxentry_action_parent_class = GTK_ACTION_CLASS(g_type_class_peek_parent (klass) );
173 g_object_class_install_property (
174 gobject_class,
175 PROP_MODEL,
176 g_param_spec_object ("model",
177 "Tree Model",
178 "Tree Model",
179 GTK_TYPE_TREE_MODEL,
180 (GParamFlags)G_PARAM_READWRITE));
181 g_object_class_install_property (
182 gobject_class,
183 PROP_COMBOBOX,
184 g_param_spec_object ("combobox",
185 "GtkComboBoxEntry",
186 "GtkComboBoxEntry",
187 GTK_TYPE_WIDGET,
188 (GParamFlags)G_PARAM_READABLE));
189 g_object_class_install_property (
190 gobject_class,
191 PROP_ENTRY,
192 g_param_spec_object ("entry",
193 "GtkEntry",
194 "GtkEntry",
195 GTK_TYPE_WIDGET,
196 (GParamFlags)G_PARAM_READABLE));
197 g_object_class_install_property (
198 gobject_class,
199 PROP_WIDTH,
200 g_param_spec_int ("width",
201 "EntryBox width",
202 "EntryBox width (characters)",
203 -1.0, 100, -1.0,
204 (GParamFlags)G_PARAM_READWRITE));
206 g_object_class_install_property (
207 gobject_class,
208 PROP_CELL_DATA_FUNC,
209 g_param_spec_pointer ("cell_data_func",
210 "Cell Data Func",
211 "Cell Deta Function",
212 (GParamFlags)G_PARAM_READWRITE));
214 g_object_class_install_property (
215 gobject_class,
216 PROP_POPUP,
217 g_param_spec_boolean ("popup",
218 "Entry Popup",
219 "Entry Popup",
220 false,
221 (GParamFlags)G_PARAM_READWRITE));
223 // We need to know when GtkComboBoxEvent or Menu ready for reading
224 signals[CHANGED] = g_signal_new( "changed",
225 G_TYPE_FROM_CLASS(klass),
226 G_SIGNAL_RUN_FIRST,
227 G_STRUCT_OFFSET(Ink_ComboBoxEntry_ActionClass, changed),
228 NULL, NULL,
229 g_cclosure_marshal_VOID__VOID,
230 G_TYPE_NONE, 0);
232 // Probably not needed... originally to keep track of key-presses.
233 signals[ACTIVATED] = g_signal_new( "activated",
234 G_TYPE_FROM_CLASS(klass),
235 G_SIGNAL_RUN_FIRST,
236 G_STRUCT_OFFSET(Ink_ComboBoxEntry_ActionClass, activated),
237 NULL, NULL,
238 g_cclosure_marshal_VOID__VOID,
239 G_TYPE_NONE, 0);
241 }
243 static void ink_comboboxentry_action_init (Ink_ComboBoxEntry_Action *action)
244 {
245 action->active = -1;
246 action->text = NULL;
247 action->entry_completion = NULL;
248 action->popup = false;
249 action->warning = NULL;
250 }
252 GType ink_comboboxentry_action_get_type ()
253 {
254 static GType ink_comboboxentry_action_type = 0;
256 if (!ink_comboboxentry_action_type) {
257 static const GTypeInfo ink_comboboxentry_action_info = {
258 sizeof(Ink_ComboBoxEntry_ActionClass),
259 NULL, /* base_init */
260 NULL, /* base_finalize */
261 (GClassInitFunc) ink_comboboxentry_action_class_init,
262 NULL, /* class_finalize */
263 NULL, /* class_data */
264 sizeof(Ink_ComboBoxEntry_Action),
265 0, /* n_preallocs */
266 (GInstanceInitFunc)ink_comboboxentry_action_init, /* instance_init */
267 NULL /* value_table */
268 };
270 ink_comboboxentry_action_type = g_type_register_static (GTK_TYPE_ACTION,
271 "Ink_ComboBoxEntry_Action",
272 &ink_comboboxentry_action_info,
273 (GTypeFlags)0 );
274 }
276 return ink_comboboxentry_action_type;
277 }
280 Ink_ComboBoxEntry_Action *ink_comboboxentry_action_new (const gchar *name,
281 const gchar *label,
282 const gchar *tooltip,
283 const gchar *stock_id,
284 GtkTreeModel *model,
285 gint width,
286 void *cell_data_func )
287 {
288 g_return_val_if_fail (name != NULL, NULL);
290 return (Ink_ComboBoxEntry_Action*)g_object_new (INK_COMBOBOXENTRY_TYPE_ACTION,
291 "name", name,
292 "label", label,
293 "tooltip", tooltip,
294 "stock-id", stock_id,
295 "model", model,
296 "width", width,
297 "cell_data_func", cell_data_func,
298 NULL);
299 }
301 // Create a widget for a toolbar.
302 GtkWidget* create_tool_item( GtkAction* action )
303 {
304 GtkWidget* item = 0;
306 if ( INK_COMBOBOXENTRY_IS_ACTION( action ) && INK_COMBOBOXENTRY_ACTION(action)->model ) {
308 Ink_ComboBoxEntry_Action* ink_comboboxentry_action = INK_COMBOBOXENTRY_ACTION( action );
310 item = GTK_WIDGET( gtk_tool_item_new() );
312 GtkWidget* comboBoxEntry = gtk_combo_box_entry_new_with_model( ink_comboboxentry_action->model, 0 );
314 gtk_container_add( GTK_CONTAINER(item), comboBoxEntry );
316 ink_comboboxentry_action->combobox = GTK_COMBO_BOX_ENTRY(comboBoxEntry);
318 gtk_combo_box_set_active( GTK_COMBO_BOX( comboBoxEntry ), ink_comboboxentry_action->active );
320 g_signal_connect( G_OBJECT(comboBoxEntry), "changed", G_CALLBACK(combo_box_changed_cb), action );
322 // Optionally add formatting...
323 if( ink_comboboxentry_action->cell_data_func != NULL ) {
324 GtkCellRenderer *cell = gtk_cell_renderer_text_new();
325 gtk_cell_layout_clear( GTK_CELL_LAYOUT( comboBoxEntry ) );
326 gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( comboBoxEntry ), cell, true );
327 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT( comboBoxEntry ), cell,
328 GtkCellLayoutDataFunc (ink_comboboxentry_action->cell_data_func),
329 NULL, NULL );
330 }
332 // Get reference to GtkEntry and fiddle a bit with it.
333 GtkWidget *child = gtk_bin_get_child( GTK_BIN(comboBoxEntry) );
334 if( child && GTK_IS_ENTRY( child ) ) {
335 ink_comboboxentry_action->entry = GTK_ENTRY(child);
337 // Change width
338 if( ink_comboboxentry_action->width > 0 ) {
339 gtk_entry_set_width_chars (GTK_ENTRY (child), ink_comboboxentry_action->width );
340 }
342 // Add pop-up entry completion if required
343 if( ink_comboboxentry_action->popup ) {
344 ink_comboboxentry_action_popup_enable( ink_comboboxentry_action );
345 }
347 // Add signal for GtkEntry to check if finished typing.
348 g_signal_connect( G_OBJECT(child), "activate", G_CALLBACK(entry_activate_cb), action );
350 }
352 gtk_action_connect_proxy( GTK_ACTION( action ), item );
354 gtk_widget_show_all( item );
356 } else {
358 item = ink_comboboxentry_action_parent_class->create_tool_item( action );
360 }
362 return item;
363 }
365 // Create a drop-down menu.
366 GtkWidget* create_menu_item( GtkAction* action )
367 {
368 GtkWidget* item = 0;
370 item = ink_comboboxentry_action_parent_class->create_menu_item( action );
371 g_warning( "ink_comboboxentry_action: create_menu_item not implemented" );
372 // One can easily modify ege-select-one-action routine to implement this.
373 return item;
374 }
376 // Setters/Getters ---------------------------------------------------
378 GtkTreeModel *ink_comboboxentry_action_get_model( Ink_ComboBoxEntry_Action* action ) {
380 return action->model;
381 }
383 GtkComboBoxEntry *ink_comboboxentry_action_get_comboboxentry( Ink_ComboBoxEntry_Action* action ) {
385 return action->combobox;
386 }
388 gchar* ink_comboboxentry_action_get_active_text( Ink_ComboBoxEntry_Action* action ) {
390 gchar* text = g_strdup( action->text );
391 return text;
392 }
394 gboolean ink_comboboxentry_action_set_active_text( Ink_ComboBoxEntry_Action* ink_comboboxentry_action, gchar* text ) {
396 g_free( ink_comboboxentry_action->text );
397 ink_comboboxentry_action->text = g_strdup( text );
399 // Get active row or -1 if none
400 ink_comboboxentry_action->active = get_active_row_from_text( ink_comboboxentry_action, ink_comboboxentry_action->text );
402 // Set active row, check that combobox has been created.
403 if( ink_comboboxentry_action->combobox ) {
404 gtk_combo_box_set_active( GTK_COMBO_BOX( ink_comboboxentry_action->combobox ), ink_comboboxentry_action->active );
405 }
407 // Fiddle with entry
408 if( ink_comboboxentry_action->entry ) {
410 // Explicitly set text in GtkEntry box (won't be set if text not in list).
411 gtk_entry_set_text( ink_comboboxentry_action->entry, text );
413 // Show or hide warning
414 if( ink_comboboxentry_action->active == -1 && ink_comboboxentry_action->warning != NULL ) {
415 #if GTK_CHECK_VERSION(2,16,0)
416 gtk_entry_set_icon_from_icon_name( ink_comboboxentry_action->entry,
417 GTK_ENTRY_ICON_SECONDARY,
418 GTK_STOCK_DIALOG_WARNING );
419 // Can't add tooltip until icon set
420 gtk_entry_set_icon_tooltip_text( ink_comboboxentry_action->entry,
421 GTK_ENTRY_ICON_SECONDARY,
422 ink_comboboxentry_action->warning );
424 #endif // GTK_CHECK_VERSION(2,16,0)
425 } else {
426 #if GTK_CHECK_VERSION(2,16,0)
427 gtk_entry_set_icon_from_icon_name( GTK_ENTRY(ink_comboboxentry_action->entry),
428 GTK_ENTRY_ICON_SECONDARY,
429 NULL );
430 #endif // GTK_CHECK_VERSION(2,16,0)
431 }
432 }
434 // Return if active text in list
435 gboolean found = ( ink_comboboxentry_action->active != -1 );
436 return found;
437 }
439 void ink_comboboxentry_action_set_width( Ink_ComboBoxEntry_Action* action, gint width ) {
441 action->width = width;
443 // Widget may not have been created....
444 if( action->entry ) {
445 gtk_entry_set_width_chars( GTK_ENTRY(action->entry), width );
446 }
447 }
449 void ink_comboboxentry_action_popup_enable( Ink_ComboBoxEntry_Action* action ) {
451 action->popup = true;
453 // Widget may not have been created....
454 if( action->entry ) {
456 // Check we don't already have a GtkEntryCompletion
457 if( action->entry_completion ) return;
459 action->entry_completion = gtk_entry_completion_new();
461 gtk_entry_set_completion( action->entry, action->entry_completion );
462 gtk_entry_completion_set_model( action->entry_completion, action->model );
463 gtk_entry_completion_set_text_column( action->entry_completion, 0 );
464 gtk_entry_completion_set_popup_completion( action->entry_completion, true );
465 gtk_entry_completion_set_inline_completion( action->entry_completion, false );
466 gtk_entry_completion_set_inline_selection( action->entry_completion, true );
468 g_signal_connect (G_OBJECT (action->entry_completion), "match-selected", G_CALLBACK (match_selected_cb), action );
470 }
471 }
473 void ink_comboboxentry_action_popup_disable( Ink_ComboBoxEntry_Action* action ) {
475 action->popup = false;
477 if( action->entry_completion ) {
478 gtk_object_destroy( GTK_OBJECT( action->entry_completion ) );
479 }
480 }
482 void ink_comboboxentry_action_set_warning( Ink_ComboBoxEntry_Action* action, gchar* warning ) {
484 g_free( action->warning );
485 action->warning = g_strdup( warning );
487 // Widget may not have been created....
488 if( action->entry ) {
489 #if GTK_CHECK_VERSION(2,16,0)
490 gtk_entry_set_icon_tooltip_text( GTK_ENTRY(action->entry),
491 GTK_ENTRY_ICON_SECONDARY,
492 action->warning );
493 #endif // GTK_CHECK_VERSION(2,16,0)
494 }
495 }
497 // Internal ---------------------------------------------------
499 // Return row of active text or -1 if not found.
500 gint get_active_row_from_text( Ink_ComboBoxEntry_Action* action, gchar* target_text ) {
502 // Check if text in list
503 gint row = 0;
504 gboolean found = false;
505 GtkTreeIter iter;
506 gboolean valid = gtk_tree_model_get_iter_first( action->model, &iter );
507 while ( valid ) {
509 // Get text from list entry
510 gchar* text = 0;
511 gtk_tree_model_get( action->model, &iter, 0, &text, -1 ); // Column 0
513 // Check for match
514 if( strcmp( target_text, text ) == 0 ){
515 found = true;
516 break;
517 }
518 ++row;
519 valid = gtk_tree_model_iter_next( action->model, &iter );
520 }
522 if( !found ) row = -1;
524 return row;
526 }
529 // Callbacks ---------------------------------------------------
531 static void combo_box_changed_cb( GtkComboBoxEntry* widget, gpointer data ) {
533 // Two things can happen to get here:
534 // An item is selected in the drop-down menu.
535 // Text is typed.
536 // We only react here if an item is selected.
538 // Get action
539 Ink_ComboBoxEntry_Action *act = INK_COMBOBOXENTRY_ACTION( data );
541 // Check if item selected:
542 gint newActive = gtk_combo_box_get_active( GTK_COMBO_BOX( widget ));
543 if( newActive >= 0 ) {
545 if( newActive != act->active ) {
546 act->active = newActive;
547 g_free( act->text );
548 act->text = gtk_combo_box_get_active_text( GTK_COMBO_BOX( widget ));
550 // Now let the world know
551 g_signal_emit( G_OBJECT(act), signals[CHANGED], 0 );
553 }
554 }
555 }
557 static void entry_activate_cb( GtkEntry* widget, gpointer data ) {
559 // Get text from entry box.. check if it matches a menu entry.
561 // Get action
562 Ink_ComboBoxEntry_Action *ink_comboboxentry_action = INK_COMBOBOXENTRY_ACTION( data );
564 // Get text
565 g_free( ink_comboboxentry_action->text );
566 ink_comboboxentry_action->text = g_strdup( gtk_entry_get_text( widget ) );
568 // Get row
569 ink_comboboxentry_action->active =
570 get_active_row_from_text( ink_comboboxentry_action, ink_comboboxentry_action->text );
572 // Set active row
573 gtk_combo_box_set_active( GTK_COMBO_BOX( ink_comboboxentry_action->combobox), ink_comboboxentry_action->active );
575 // Now let the world know
576 g_signal_emit( G_OBJECT(ink_comboboxentry_action), signals[CHANGED], 0 );
578 }
580 static gboolean match_selected_cb( GtkEntryCompletion* /*widget*/, GtkTreeModel* model, GtkTreeIter* iter, gpointer data )
581 {
582 // Get action
583 Ink_ComboBoxEntry_Action *ink_comboboxentry_action = INK_COMBOBOXENTRY_ACTION( data );
584 GtkEntry *entry = ink_comboboxentry_action->entry;
586 if( entry) {
587 gchar *family = 0;
588 gtk_tree_model_get(model, iter, 0, &family, -1);
590 // Set text in GtkEntry
591 gtk_entry_set_text (GTK_ENTRY (entry), family );
593 // Set text in GtkAction
594 g_free( ink_comboboxentry_action->text );
595 ink_comboboxentry_action->text = family;
597 // Get row
598 ink_comboboxentry_action->active =
599 get_active_row_from_text( ink_comboboxentry_action, ink_comboboxentry_action->text );
601 // Set active row
602 gtk_combo_box_set_active( GTK_COMBO_BOX( ink_comboboxentry_action->combobox), ink_comboboxentry_action->active );
604 // Now let the world know
605 g_signal_emit( G_OBJECT(ink_comboboxentry_action), signals[CHANGED], 0 );
607 return true;
608 }
609 return false;
610 }