Code

Adding custom action to work with spinbuttons in toolbars
[inkscape.git] / src / ink-action.cpp
4 #include <glib/gi18n.h>
5 #include <gtk/gtktooltips.h>
6 #include <gtk/gtktoolitem.h>
7 #include <gtk/gtktoggletoolbutton.h>
8 #include <gtk/gtkcheckmenuitem.h>
10 #include "icon-size.h"
11 #include "ink-action.h"
13 #include "widgets/button.h"
14 #include "widgets/icon.h"
18 static void ink_action_class_init( InkActionClass* klass );
19 static void ink_action_init( InkAction* action );
20 static void ink_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec );
21 static void ink_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec );
23 static GtkWidget* ink_action_create_menu_item( GtkAction* action );
24 static GtkWidget* ink_action_create_tool_item( GtkAction* action );
26 static GtkActionClass* gInkActionParentClass = 0;
28 GType ink_action_get_type( void )
29 {
30     static GType myType = 0;
31     if ( !myType ) {
32         static const GTypeInfo myInfo = {
33             sizeof( InkActionClass ),
34             NULL, /* base_init */
35             NULL, /* base_finalize */
36             (GClassInitFunc)ink_action_class_init,
37             NULL, /* class_finalize */
38             NULL, /* class_data */
39             sizeof( InkAction ),
40             0, /* n_preallocs */
41             (GInstanceInitFunc)ink_action_init,
42             NULL
43         };
45         myType = g_type_register_static( GTK_TYPE_ACTION, "InkAction", &myInfo, (GTypeFlags)0 );
46     }
48     return myType;
49 }
51 enum {
52     PROP_INK_ID = 1,
53     PROP_INK_SIZE
54 };
56 static void ink_action_class_init( InkActionClass* klass )
57 {
58     if ( klass ) {
59         gInkActionParentClass = GTK_ACTION_CLASS( g_type_class_peek_parent( klass ) );
60         GObjectClass * objClass = G_OBJECT_CLASS( klass );
62         objClass->get_property = ink_action_get_property;
63         objClass->set_property = ink_action_set_property;
65         klass->parent_class.create_menu_item = ink_action_create_menu_item;
66         klass->parent_class.create_tool_item = ink_action_create_tool_item;
67         /*klass->parent_class.connect_proxy = connect_proxy;*/
68         /*klass->parent_class.disconnect_proxy = disconnect_proxy;*/
70         g_object_class_install_property( objClass,
71                                          PROP_INK_ID,
72                                          g_param_spec_string( "iconId",
73                                                               "Icon ID",
74                                                               "The id for the icon",
75                                                               "",
76                                                               (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
78         g_object_class_install_property( objClass,
79                                          PROP_INK_SIZE,
80                                          g_param_spec_int( "iconSize",
81                                                            "Icon Size",
82                                                            "The size the icon",
83                                                            (int)Inkscape::ICON_SIZE_MENU,
84                                                            (int)Inkscape::ICON_SIZE_DECORATION,
85                                                            (int)Inkscape::ICON_SIZE_SMALL_TOOLBAR,
86                                                            (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
87     }
88 }
90 static void ink_action_init( InkAction* action )
91 {
92 }
94 InkAction* ink_action_new( const gchar *name,
95                            const gchar *label,
96                            const gchar *tooltip,
97                            const gchar *inkId,
98                            Inkscape::IconSize size )
99 {
100     GObject* obj = (GObject*)g_object_new( INK_ACTION_TYPE,
101                                            "name", name,
102                                            "label", label,
103                                            "tooltip", tooltip,
104                                            "iconId", inkId,
105                                            "iconSize", size,
106                                            NULL );
108     InkAction* action = INK_ACTION( obj );
110     return action;
113 static void ink_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec )
115     InkAction* action = INK_ACTION( obj );
116     (void)action;
117     switch ( propId ) {
118         case PROP_INK_ID:
119         {
120             //g_value_set_pointer( value, action->private_data->adj );
121         }
122         break;
124         case PROP_INK_SIZE:
125         {
126             //g_value_set_pointer( value, action->private_data->adj );
127         }
128         break;
130         default:
131             G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
132     }
135 void ink_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec )
137     InkAction* action = INK_ACTION( obj );
138     (void)action;
139     switch ( propId ) {
140         case PROP_INK_ID:
141         {
142 //          gchar* tmp = action->private_data->iconId;
143 //          action->private_data->iconId = g_value_dup_string( value );
144 //          g_free( tmp );
145         }
146         break;
148         case PROP_INK_SIZE:
149         {
150 //          action->private_data->iconSize = g_value_get_( value );
151         }
152         break;
154         default:
155             G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
156     }
159 static GtkWidget* ink_action_create_menu_item( GtkAction* action )
161     GtkWidget* item = 0;
162     g_message("INK ACTION CREATE MENU ITEM");
163     item = gInkActionParentClass->create_menu_item( action );
164     return item;
167 static GtkWidget* ink_action_create_tool_item( GtkAction* action )
169     GtkWidget* item = 0;
170     g_message("INK ACTION CREATE TOOL ITEM");
173     //item = gInkActionParentClass->create_tool_item( action );
174     GtkTooltips *tt = gtk_tooltips_new();
175     GtkWidget *button = sp_button_new_from_data( Inkscape::ICON_SIZE_DECORATION,
176                                                  SP_BUTTON_TYPE_NORMAL,
177                                                  NULL,
178                                                  "use_pressure",
179                                                  _("Use the pressure of the input device to alter the width of the pen"),
180                                                  tt);
181     //g_signal_connect_after (G_OBJECT (button), "clicked", G_CALLBACK (sp_ddc_pressure_state_changed), NULL);
182     //gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), prefs_get_int_attribute ("tools.calligraphic", "usepressure", 1));
183     item = GTK_WIDGET(gtk_tool_item_new());
184     gtk_container_add( GTK_CONTAINER(item), button );
186     gtk_widget_show_all( item );
188     return item;
193 /* --------------------------------------------------------------- */
194 /* --------------------------------------------------------------- */
195 /* --------------------------------------------------------------- */
196 /* --------------------------------------------------------------- */
199 static void ink_toggle_action_class_init( InkToggleActionClass* klass );
200 static void ink_toggle_action_init( InkToggleAction* action );
201 static void ink_toggle_action_finalize( GObject* obj );
202 static void ink_toggle_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec );
203 static void ink_toggle_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec );
205 static GtkWidget* ink_toggle_action_create_menu_item( GtkAction* action );
206 static GtkWidget* ink_toggle_action_create_tool_item( GtkAction* action );
208 static GtkToggleActionClass* gInkToggleActionParentClass = 0;
210 struct _InkToggleActionPrivate
212     gchar* iconId;
213     Inkscape::IconSize iconSize;
214 };
216 #define INK_TOGGLE_ACTION_GET_PRIVATE( o ) ( G_TYPE_INSTANCE_GET_PRIVATE( (o), INK_TOGGLE_ACTION_TYPE, InkToggleActionPrivate ) )
218 GType ink_toggle_action_get_type( void )
220     static GType myType = 0;
221     if ( !myType ) {
222         static const GTypeInfo myInfo = {
223             sizeof( InkToggleActionClass ),
224             NULL, /* base_init */
225             NULL, /* base_finalize */
226             (GClassInitFunc)ink_toggle_action_class_init,
227             NULL, /* class_finalize */
228             NULL, /* class_data */
229             sizeof( InkToggleAction ),
230             0, /* n_preallocs */
231             (GInstanceInitFunc)ink_toggle_action_init,
232             NULL
233         };
235         myType = g_type_register_static( GTK_TYPE_TOGGLE_ACTION, "InkToggleAction", &myInfo, (GTypeFlags)0 );
236     }
238     return myType;
242 static void ink_toggle_action_class_init( InkToggleActionClass* klass )
244     if ( klass ) {
245         gInkToggleActionParentClass = GTK_TOGGLE_ACTION_CLASS( g_type_class_peek_parent( klass ) );
246         GObjectClass * objClass = G_OBJECT_CLASS( klass );
248         objClass->finalize = ink_toggle_action_finalize;
249         objClass->get_property = ink_toggle_action_get_property;
250         objClass->set_property = ink_toggle_action_set_property;
252         klass->parent_class.parent_class.create_menu_item = ink_toggle_action_create_menu_item;
253         klass->parent_class.parent_class.create_tool_item = ink_toggle_action_create_tool_item;
254         /*klass->parent_class.connect_proxy = connect_proxy;*/
255         /*klass->parent_class.disconnect_proxy = disconnect_proxy;*/
257         g_object_class_install_property( objClass,
258                                          PROP_INK_ID,
259                                          g_param_spec_string( "iconId",
260                                                               "Icon ID",
261                                                               "The id for the icon",
262                                                               "",
263                                                               (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
265         g_object_class_install_property( objClass,
266                                          PROP_INK_SIZE,
267                                          g_param_spec_int( "iconSize",
268                                                            "Icon Size",
269                                                            "The size the icon",
270                                                            (int)Inkscape::ICON_SIZE_MENU,
271                                                            (int)Inkscape::ICON_SIZE_DECORATION,
272                                                            (int)Inkscape::ICON_SIZE_SMALL_TOOLBAR,
273                                                            (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
275         g_type_class_add_private( klass, sizeof(InkToggleActionClass) );
276     }
279 static void ink_toggle_action_init( InkToggleAction* action )
281     action->private_data = INK_TOGGLE_ACTION_GET_PRIVATE( action );
282     action->private_data->iconId = 0;
283     action->private_data->iconSize = Inkscape::ICON_SIZE_SMALL_TOOLBAR;
286 static void ink_toggle_action_finalize( GObject* obj )
288     InkToggleAction* action = INK_TOGGLE_ACTION( obj );
290     g_free( action->private_data->iconId );
291     g_free( action->private_data );
295 InkToggleAction* ink_toggle_action_new( const gchar *name,
296                            const gchar *label,
297                            const gchar *tooltip,
298                            const gchar *inkId,
299                            Inkscape::IconSize size )
301     GObject* obj = (GObject*)g_object_new( INK_TOGGLE_ACTION_TYPE,
302                                            "name", name,
303                                            "label", label,
304                                            "tooltip", tooltip,
305                                            "iconId", inkId,
306                                            "iconSize", size,
307                                            NULL );
309     InkToggleAction* action = INK_TOGGLE_ACTION( obj );
311     return action;
314 static void ink_toggle_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec )
316     InkToggleAction* action = INK_TOGGLE_ACTION( obj );
317     (void)action;
318     switch ( propId ) {
319         case PROP_INK_ID:
320         {
321             g_value_set_string( value, action->private_data->iconId );
322         }
323         break;
325         case PROP_INK_SIZE:
326         {
327             g_value_set_int( value, action->private_data->iconSize );
328         }
329         break;
331         default:
332             G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
333     }
336 void ink_toggle_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec )
338     InkToggleAction* action = INK_TOGGLE_ACTION( obj );
339     (void)action;
340     switch ( propId ) {
341         case PROP_INK_ID:
342         {
343             gchar* tmp = action->private_data->iconId;
344             action->private_data->iconId = g_value_dup_string( value );
345             g_free( tmp );
346         }
347         break;
349         case PROP_INK_SIZE:
350         {
351             action->private_data->iconSize = (Inkscape::IconSize)g_value_get_int( value );
352         }
353         break;
355         default:
356         {
357             G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
358         }
359     }
362 static GtkWidget* ink_toggle_action_create_menu_item( GtkAction* action )
364     GtkWidget* item = gInkToggleActionParentClass->parent_class.create_menu_item(action);
366     return item;
369 static GtkWidget* ink_toggle_action_create_tool_item( GtkAction* action )
371     InkToggleAction* act = INK_TOGGLE_ACTION( action );
372     GtkWidget* item = gInkToggleActionParentClass->parent_class.create_tool_item(action);
374     if ( act->private_data->iconId ) {
375         if ( GTK_IS_TOOL_BUTTON(item) ) {
376             GtkToolButton* button = GTK_TOOL_BUTTON(item);
378             GtkWidget* child = sp_icon_new( act->private_data->iconSize, act->private_data->iconId );
379             gtk_tool_button_set_icon_widget( button, child );
380         } else {
381             // For now trigger a warning but don't do anything else
382             GtkToolButton* button = GTK_TOOL_BUTTON(item);
383             (void)button;
384         }
385     }
387     // TODO investigate if needed
388     gtk_widget_show_all( item );
390     return item;