Code

Converted text toolbar to GTK toolbar.
[inkscape.git] / src / ink-comboboxentry-action.cpp
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   }
107 static void ink_comboboxentry_action_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
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   }
142 static void
143 ink_comboboxentry_action_connect_proxy (GtkAction *action,
144                                         GtkWidget *proxy)
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);
154 static void ink_comboboxentry_action_class_init (Ink_ComboBoxEntry_ActionClass *klass)
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);
243 static void ink_comboboxentry_action_init (Ink_ComboBoxEntry_Action *action)
245   action->active = -1;
246   action->text = NULL;
247   action->entry_completion = NULL;
248   action->popup = false;
249   action->warning = NULL;
252 GType ink_comboboxentry_action_get_type ()
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;
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 )
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);
301 // Create a widget for a toolbar.
302 GtkWidget* create_tool_item( GtkAction* action )
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 );
353       
354     gtk_widget_show_all( item );
356   } else {
358     item = ink_comboboxentry_action_parent_class->create_tool_item( action );
360   }
361     
362   return item;
365 // Create a drop-down menu.
366 GtkWidget* create_menu_item( GtkAction* action )
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;
376 // Setters/Getters ---------------------------------------------------
378 GtkTreeModel *ink_comboboxentry_action_get_model( Ink_ComboBoxEntry_Action* action ) {
380   return action->model;
383 GtkComboBoxEntry *ink_comboboxentry_action_get_comboboxentry( Ink_ComboBoxEntry_Action* action ) {
385   return action->combobox;
388 gchar* ink_comboboxentry_action_get_active_text( Ink_ComboBoxEntry_Action* action ) {
390   gchar* text = g_strdup( action->text );
391   return text;
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       gtk_entry_set_icon_from_icon_name( ink_comboboxentry_action->entry,
416                                          GTK_ENTRY_ICON_SECONDARY,
417                                          GTK_STOCK_DIALOG_WARNING );
418       // Can't add tooltip until icon set
419       gtk_entry_set_icon_tooltip_text( ink_comboboxentry_action->entry,
420                                        GTK_ENTRY_ICON_SECONDARY,
421                                        ink_comboboxentry_action->warning );
423     } else {
424       gtk_entry_set_icon_from_icon_name( GTK_ENTRY(ink_comboboxentry_action->entry),
425                                          GTK_ENTRY_ICON_SECONDARY,
426                                          NULL );
427     }
428   }
430   // Return if active text in list
431   gboolean found = ( ink_comboboxentry_action->active != -1 );
432   return found;
435 void ink_comboboxentry_action_set_width( Ink_ComboBoxEntry_Action* action, gint width ) {
437   action->width = width;
439   // Widget may not have been created....
440   if( action->entry ) {
441     gtk_entry_set_width_chars( GTK_ENTRY(action->entry), width );
442   }
445 void ink_comboboxentry_action_popup_enable( Ink_ComboBoxEntry_Action* action ) {
447   action->popup = true;
449   // Widget may not have been created....
450   if( action->entry ) {
452     // Check we don't already have a GtkEntryCompletion
453     if( action->entry_completion ) return;
455     action->entry_completion = gtk_entry_completion_new();
457     gtk_entry_set_completion( action->entry, action->entry_completion );
458     gtk_entry_completion_set_model( action->entry_completion, action->model );
459     gtk_entry_completion_set_text_column( action->entry_completion, 0 );
460     gtk_entry_completion_set_popup_completion( action->entry_completion, true );
461     gtk_entry_completion_set_inline_completion( action->entry_completion, false );
462     gtk_entry_completion_set_inline_selection( action->entry_completion, true );
464     g_signal_connect (G_OBJECT (action->entry_completion),  "match-selected", G_CALLBACK (match_selected_cb), action );
466   }
469 void ink_comboboxentry_action_popup_disable( Ink_ComboBoxEntry_Action* action ) {
471   action->popup = false;
473   if( action->entry_completion ) {
474     gtk_object_destroy( GTK_OBJECT( action->entry_completion ) );
475   }
478 void     ink_comboboxentry_action_set_warning( Ink_ComboBoxEntry_Action* action, gchar* warning ) {
480   g_free( action->warning );
481   action->warning = g_strdup( warning );
483   // Widget may not have been created....
484   if( action->entry ) {
485     gtk_entry_set_icon_tooltip_text( GTK_ENTRY(action->entry),
486                                      GTK_ENTRY_ICON_SECONDARY,
487                                      action->warning );
488   }
491 // Internal ---------------------------------------------------
493 // Return row of active text or -1 if not found.
494 gint get_active_row_from_text( Ink_ComboBoxEntry_Action* action, gchar* target_text ) {
496   // Check if text in list
497   gint row = 0;
498   gboolean found = false;
499   GtkTreeIter iter;
500   gboolean valid = gtk_tree_model_get_iter_first( action->model, &iter );
501   while ( valid ) {
503     // Get text from list entry
504     gchar* text = 0;
505     gtk_tree_model_get( action->model, &iter, 0, &text, -1 ); // Column 0
507     // Check for match
508     if( strcmp( target_text, text ) == 0 ){
509       found = true;
510       break;
511     }
512     ++row;
513     valid = gtk_tree_model_iter_next( action->model, &iter );
514   }
516   if( !found ) row = -1;
518   return row;
523 // Callbacks ---------------------------------------------------
525 static void combo_box_changed_cb( GtkComboBoxEntry* widget, gpointer data ) {
527   // Two things can happen to get here:
528   //   An item is selected in the drop-down menu.
529   //   Text is typed.
530   // We only react here if an item is selected.
532   // Get action
533   Ink_ComboBoxEntry_Action *act = INK_COMBOBOXENTRY_ACTION( data );
535   // Check if item selected:
536   gint newActive = gtk_combo_box_get_active( GTK_COMBO_BOX( widget ));
537   if( newActive >= 0 ) {
539     if( newActive != act->active ) {
540       act->active = newActive;
541       g_free( act->text );
542       act->text = gtk_combo_box_get_active_text( GTK_COMBO_BOX( widget ));
544       // Now let the world know
545       g_signal_emit( G_OBJECT(act), signals[CHANGED], 0 );
547     }
548   }
551 static void entry_activate_cb( GtkEntry* widget, gpointer data ) {
553   // Get text from entry box.. check if it matches a menu entry.
555   // Get action
556   Ink_ComboBoxEntry_Action *ink_comboboxentry_action = INK_COMBOBOXENTRY_ACTION( data );
558   // Get text
559   g_free( ink_comboboxentry_action->text );
560   ink_comboboxentry_action->text = g_strdup( gtk_entry_get_text( widget ) );
562   // Get row
563   ink_comboboxentry_action->active =
564     get_active_row_from_text( ink_comboboxentry_action, ink_comboboxentry_action->text );
566   // Set active row
567   gtk_combo_box_set_active( GTK_COMBO_BOX( ink_comboboxentry_action->combobox), ink_comboboxentry_action->active );
569   // Now let the world know
570   g_signal_emit( G_OBJECT(ink_comboboxentry_action), signals[CHANGED], 0 );
574 static gboolean match_selected_cb( GtkEntryCompletion* widget, GtkTreeModel* model, GtkTreeIter* iter, gpointer data )
576   // Get action
577   Ink_ComboBoxEntry_Action *ink_comboboxentry_action = INK_COMBOBOXENTRY_ACTION( data );
578   GtkEntry *entry = ink_comboboxentry_action->entry;
580   if( entry) {
581     gchar *family = 0;
582     gtk_tree_model_get(model, iter, 0, &family, -1);
584     // Set text in GtkEntry
585     gtk_entry_set_text (GTK_ENTRY (entry), family );
586     
587     // Set text in GtkAction
588     g_free( ink_comboboxentry_action->text );
589     ink_comboboxentry_action->text = family;
591     // Get row
592     ink_comboboxentry_action->active =
593       get_active_row_from_text( ink_comboboxentry_action, ink_comboboxentry_action->text );
595     // Set active row
596     gtk_combo_box_set_active( GTK_COMBO_BOX( ink_comboboxentry_action->combobox), ink_comboboxentry_action->active );
598     // Now let the world know
599     g_signal_emit( G_OBJECT(ink_comboboxentry_action), signals[CHANGED], 0 );
601     return true;
602   }
603   return false;