a2ea9ff0db9842fc8d3b28c2134351c8feeea71f
3 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
4 *
5 */
6 /* ***** BEGIN LICENSE BLOCK *****
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8 *
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
13 *
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
18 *
19 * The Original Code is EGE Adjustment Action.
20 *
21 * The Initial Developer of the Original Code is
22 * Jon A. Cruz.
23 * Portions created by the Initial Developer are Copyright (C) 2006
24 * the Initial Developer. All Rights Reserved.
25 *
26 * Contributor(s):
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
39 *
40 * ***** END LICENSE BLOCK ***** */
42 /* Note: this file should be kept compliable as both .cpp and .c */
44 #include <string.h>
46 #include <gdk/gdkkeysyms.h>
47 #include <gtk/gtktoolitem.h>
48 #include <gtk/gtkspinbutton.h>
49 #include <gtk/gtkhbox.h>
50 #include <gtk/gtklabel.h>
51 #include <gtk/gtkmisc.h>
52 #include <gtk/gtktoolbar.h>
54 #include "ege-adjustment-action.h"
57 static void ege_adjustment_action_class_init( EgeAdjustmentActionClass* klass );
58 static void ege_adjustment_action_init( EgeAdjustmentAction* action );
59 static void ege_adjustment_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec );
60 static void ege_adjustment_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec );
62 static GtkWidget* create_menu_item( GtkAction* action );
63 static GtkWidget* create_tool_item( GtkAction* action );
64 static void connect_proxy( GtkAction *action, GtkWidget *proxy );
65 static void disconnect_proxy( GtkAction *action, GtkWidget *proxy );
67 static gboolean focus_in_cb( GtkWidget *widget, GdkEventKey *event, gpointer data );
68 static gboolean focus_out_cb( GtkWidget *widget, GdkEventKey *event, gpointer data );
69 static gboolean keypress_cb( GtkWidget *widget, GdkEventKey *event, gpointer data );
71 static void ege_adjustment_action_defocus( EgeAdjustmentAction* action );
74 static GtkActionClass* gParentClass = 0;
77 struct _EgeAdjustmentActionPrivate
78 {
79 GtkAdjustment* adj;
80 GtkWidget* focusWidget;
81 gdouble climbRate;
82 guint digits;
83 gchar* selfId;
84 gdouble lastVal;
85 gdouble step;
86 gdouble page;
87 gboolean transferFocus;
88 };
90 #define EGE_ADJUSTMENT_ACTION_GET_PRIVATE( o ) ( G_TYPE_INSTANCE_GET_PRIVATE( (o), EGE_ADJUSTMENT_ACTION_TYPE, EgeAdjustmentActionPrivate ) )
92 enum {
93 PROP_ADJUSTMENT = 1,
94 PROP_FOCUS_WIDGET,
95 PROP_CLIMB_RATE,
96 PROP_DIGITS,
97 PROP_SELFID
98 };
100 GType ege_adjustment_action_get_type( void )
101 {
102 static GType myType = 0;
103 if ( !myType ) {
104 static const GTypeInfo myInfo = {
105 sizeof( EgeAdjustmentActionClass ),
106 NULL, /* base_init */
107 NULL, /* base_finalize */
108 (GClassInitFunc)ege_adjustment_action_class_init,
109 NULL, /* class_finalize */
110 NULL, /* class_data */
111 sizeof( EgeAdjustmentAction ),
112 0, /* n_preallocs */
113 (GInstanceInitFunc)ege_adjustment_action_init,
114 NULL
115 };
117 myType = g_type_register_static( GTK_TYPE_ACTION, "EgeAdjustmentAction", &myInfo, (GTypeFlags)0 );
118 }
120 return myType;
121 }
124 static void ege_adjustment_action_class_init( EgeAdjustmentActionClass* klass )
125 {
126 if ( klass ) {
127 gParentClass = GTK_ACTION_CLASS( g_type_class_peek_parent( klass ) );
128 GObjectClass * objClass = G_OBJECT_CLASS( klass );
130 objClass->get_property = ege_adjustment_action_get_property;
131 objClass->set_property = ege_adjustment_action_set_property;
133 klass->parent_class.create_menu_item = create_menu_item;
134 klass->parent_class.create_tool_item = create_tool_item;
135 klass->parent_class.connect_proxy = connect_proxy;
136 klass->parent_class.disconnect_proxy = disconnect_proxy;
138 g_object_class_install_property( objClass,
139 PROP_ADJUSTMENT,
140 g_param_spec_object( "adjustment",
141 "Adjustment",
142 "The adjustment to change",
143 GTK_TYPE_ADJUSTMENT,
144 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
146 g_object_class_install_property( objClass,
147 PROP_FOCUS_WIDGET,
148 g_param_spec_pointer( "focus-widget",
149 "Focus Widget",
150 "The widget to return focus to",
151 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
153 g_object_class_install_property( objClass,
154 PROP_CLIMB_RATE,
155 g_param_spec_double( "climb-rate",
156 "Climb Rate",
157 "The acelleraton rate",
158 0.0, G_MAXDOUBLE, 0.0,
159 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
161 g_object_class_install_property( objClass,
162 PROP_DIGITS,
163 g_param_spec_uint( "digits",
164 "Digits",
165 "The number of digits to show",
166 0, 20, 0,
167 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
169 g_object_class_install_property( objClass,
170 PROP_SELFID,
171 g_param_spec_string( "self-id",
172 "Self ID",
173 "Marker for self pointer",
174 0,
175 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
177 g_type_class_add_private( klass, sizeof(EgeAdjustmentActionClass) );
178 }
179 }
181 static void ege_adjustment_action_init( EgeAdjustmentAction* action )
182 {
183 action->private_data = EGE_ADJUSTMENT_ACTION_GET_PRIVATE( action );
184 action->private_data->adj = 0;
185 action->private_data->focusWidget = 0;
186 action->private_data->climbRate = 0.0;
187 action->private_data->digits = 2;
188 action->private_data->selfId = 0;
189 action->private_data->lastVal = 0.0;
190 action->private_data->step = 0.0;
191 action->private_data->page = 0.0;
192 action->private_data->transferFocus = FALSE;
193 }
195 EgeAdjustmentAction* ege_adjustment_action_new( GtkAdjustment* adjustment,
196 const gchar *name,
197 const gchar *label,
198 const gchar *tooltip,
199 const gchar *stock_id,
200 gdouble climb_rate,
201 guint digits )
202 {
203 GObject* obj = (GObject*)g_object_new( EGE_ADJUSTMENT_ACTION_TYPE,
204 "name", name,
205 "label", label,
206 "tooltip", tooltip,
207 "stock_id", stock_id,
208 "adjustment", adjustment,
209 "climb-rate", climb_rate,
210 "digits", digits,
211 NULL );
213 EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION( obj );
215 return action;
216 }
218 static void ege_adjustment_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec )
219 {
220 EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION( obj );
221 switch ( propId ) {
222 case PROP_ADJUSTMENT:
223 g_value_set_object( value, action->private_data->adj );
224 break;
226 case PROP_FOCUS_WIDGET:
227 g_value_set_pointer( value, action->private_data->focusWidget );
228 break;
230 case PROP_CLIMB_RATE:
231 g_value_set_double( value, action->private_data->climbRate );
232 break;
234 case PROP_DIGITS:
235 g_value_set_uint( value, action->private_data->digits );
236 break;
238 case PROP_SELFID:
239 g_value_set_string( value, action->private_data->selfId );
240 break;
242 default:
243 G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
244 }
245 }
247 void ege_adjustment_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec )
248 {
249 EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION( obj );
250 switch ( propId ) {
251 case PROP_ADJUSTMENT:
252 {
253 action->private_data->adj = GTK_ADJUSTMENT( g_value_get_object( value ) );
254 g_object_get( G_OBJECT(action->private_data->adj),
255 "step-increment", &action->private_data->step,
256 "page-increment", &action->private_data->page,
257 NULL );
258 }
259 break;
261 case PROP_FOCUS_WIDGET:
262 {
263 /* TODO unhook prior */
264 action->private_data->focusWidget = (GtkWidget*)g_value_get_pointer( value );
265 }
266 break;
268 case PROP_CLIMB_RATE:
269 {
270 /* TODO pass on */
271 action->private_data->climbRate = g_value_get_double( value );
272 }
273 break;
275 case PROP_DIGITS:
276 {
277 /* TODO pass on */
278 action->private_data->digits = g_value_get_uint( value );
279 }
280 break;
282 case PROP_SELFID:
283 {
284 /* TODO pass on */
285 gchar* prior = action->private_data->selfId;
286 action->private_data->selfId = g_value_dup_string( value );
287 g_free( prior );
288 }
289 break;
291 default:
292 G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
293 }
294 }
296 GtkAdjustment* ege_adjustment_action_get_adjustment( EgeAdjustmentAction* action )
297 {
298 g_return_val_if_fail( IS_EGE_ADJUSTMENT_ACTION(action), NULL );
300 return action->private_data->adj;
301 }
303 void ege_adjustment_action_set_focuswidget( EgeAdjustmentAction* action, GtkWidget* widget )
304 {
305 g_return_if_fail( IS_EGE_ADJUSTMENT_ACTION(action) );
307 /* TODO unhook prior */
309 action->private_data->focusWidget = widget;
310 }
312 GtkWidget* ege_adjustment_action_get_focuswidget( EgeAdjustmentAction* action )
313 {
314 g_return_val_if_fail( IS_EGE_ADJUSTMENT_ACTION(action), NULL );
316 return action->private_data->focusWidget;
317 }
319 static GtkWidget* create_menu_item( GtkAction* action )
320 {
321 GtkWidget* item = 0;
323 item = gParentClass->create_menu_item( action );
325 return item;
326 }
328 void value_changed_cb( GtkSpinButton* spin, EgeAdjustmentAction* act )
329 {
330 if ( GTK_WIDGET_HAS_FOCUS( GTK_WIDGET(spin) ) ) {
331 ege_adjustment_action_defocus( act );
332 }
333 }
335 static GtkWidget* create_tool_item( GtkAction* action )
336 {
337 GtkWidget* item = 0;
339 if ( IS_EGE_ADJUSTMENT_ACTION(action) ) {
340 EgeAdjustmentAction* act = EGE_ADJUSTMENT_ACTION( action );
341 GtkWidget* spinbutton = gtk_spin_button_new( act->private_data->adj, act->private_data->climbRate, act->private_data->digits );
342 GtkWidget* hb = gtk_hbox_new( FALSE, 5 );
343 GValue value;
345 item = GTK_WIDGET( gtk_tool_item_new() );
347 memset( &value, 0, sizeof(value) );
348 g_value_init( &value, G_TYPE_STRING );
349 g_object_get_property( G_OBJECT(action), "label", &value );
350 const gchar* sss = g_value_get_string( &value );
351 GtkWidget* lbl = gtk_label_new( sss ? sss : "wwww" );
353 gtk_misc_set_alignment( GTK_MISC(lbl), 1.0, 0.5 );
355 gtk_box_pack_start( GTK_BOX(hb), lbl, FALSE, FALSE, 0 );
356 gtk_box_pack_end( GTK_BOX(hb), spinbutton, FALSE, FALSE, 0 );
357 gtk_container_add( GTK_CONTAINER(item), hb );
359 if ( act->private_data->selfId ) {
360 gtk_object_set_data( GTK_OBJECT(spinbutton), act->private_data->selfId, spinbutton );
361 }
363 g_signal_connect( G_OBJECT(spinbutton), "focus-in-event", G_CALLBACK(focus_in_cb), action );
364 g_signal_connect( G_OBJECT(spinbutton), "focus-out-event", G_CALLBACK(focus_out_cb), action );
365 g_signal_connect( G_OBJECT(spinbutton), "key-press-event", G_CALLBACK(keypress_cb), action );
367 g_signal_connect( G_OBJECT(spinbutton), "value-changed", G_CALLBACK(value_changed_cb), action );
368 /* g_signal_connect( G_OBJECT(EGE_ADJUSTMENT_ACTION(action)->private_data->adj), "value-changed", G_CALLBACK(flippy), action ); */
371 gtk_widget_show_all( item );
372 } else {
373 item = gParentClass->create_tool_item( action );
374 }
376 return item;
377 }
379 static void connect_proxy( GtkAction *action, GtkWidget *proxy )
380 {
381 gParentClass->connect_proxy( action, proxy );
382 }
384 static void disconnect_proxy( GtkAction *action, GtkWidget *proxy )
385 {
386 gParentClass->disconnect_proxy( action, proxy );
387 }
389 void ege_adjustment_action_defocus( EgeAdjustmentAction* action )
390 {
391 if ( action->private_data->transferFocus ) {
392 if ( action->private_data->focusWidget ) {
393 gtk_widget_grab_focus( action->private_data->focusWidget );
394 }
395 }
396 }
398 gboolean focus_in_cb( GtkWidget *widget, GdkEventKey *event, gpointer data )
399 {
400 (void)event;
401 if ( IS_EGE_ADJUSTMENT_ACTION(data) ) {
402 EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION( data );
403 action->private_data->lastVal = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
404 action->private_data->transferFocus = TRUE;
405 }
407 return FALSE; /* report event not consumed */
408 }
410 static gboolean focus_out_cb( GtkWidget *widget, GdkEventKey *event, gpointer data )
411 {
412 (void)widget;
413 (void)event;
414 if ( IS_EGE_ADJUSTMENT_ACTION(data) ) {
415 EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION( data );
416 action->private_data->transferFocus = FALSE;
417 }
419 return FALSE; /* report event not consumed */
420 }
423 static gboolean process_tab( GtkWidget* widget, int direction )
424 {
425 gboolean handled = FALSE;
426 GtkWidget* parent = gtk_widget_get_parent(widget);
427 GtkWidget* gp = parent ? gtk_widget_get_parent(parent) : 0;
428 GtkWidget* ggp = gp ? gtk_widget_get_parent(gp) : 0;
430 if ( ggp && GTK_IS_TOOLBAR(ggp) ) {
431 GList* kids = gtk_container_get_children( GTK_CONTAINER(ggp) );
432 if ( kids ) {
433 GtkWidget* curr = widget;
434 while ( curr && (gtk_widget_get_parent(curr) != ggp) ) {
435 curr = gtk_widget_get_parent( curr );
436 }
437 if ( curr ) {
438 GList* mid = g_list_find( kids, curr );
439 while ( mid ) {
440 mid = ( direction < 0 ) ? g_list_previous(mid) : g_list_next(mid);
441 if ( mid && GTK_IS_TOOL_ITEM(mid->data) ) {
442 /* potential target */
443 GtkWidget* child = gtk_bin_get_child( GTK_BIN(mid->data) );
444 if ( child && GTK_IS_HBOX(child) ) { /* could be ours */
445 GList* subChildren = gtk_container_get_children( GTK_CONTAINER(child) );
446 if ( subChildren ) {
447 GList* last = g_list_last(subChildren);
448 if ( last && GTK_IS_SPIN_BUTTON(last->data) && GTK_WIDGET_IS_SENSITIVE( GTK_WIDGET(last->data) ) ) {
449 gtk_widget_grab_focus( GTK_WIDGET(last->data) );
450 handled = TRUE;
451 mid = 0; /* to stop loop */
452 }
454 g_list_free(subChildren);
455 }
456 }
457 }
458 }
459 }
460 g_list_free( kids );
461 }
462 }
464 return handled;
465 }
467 gboolean keypress_cb( GtkWidget *widget, GdkEventKey *event, gpointer data )
468 {
469 gboolean wasConsumed = FALSE; /* default to report event not consumed */
470 EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION(data);
471 guint key = 0;
472 gdk_keymap_translate_keyboard_state( gdk_keymap_get_for_display( gdk_display_get_default() ),
473 event->hardware_keycode, (GdkModifierType)event->state,
474 0, &key, 0, 0, 0 );
476 switch ( key ) {
477 case GDK_Escape:
478 {
479 action->private_data->transferFocus = TRUE;
480 gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), action->private_data->lastVal );
481 ege_adjustment_action_defocus( action );
482 wasConsumed = TRUE;
483 }
484 break;
486 case GDK_Return:
487 case GDK_KP_Enter:
488 {
489 action->private_data->transferFocus = TRUE;
490 ege_adjustment_action_defocus( action );
491 wasConsumed = TRUE;
492 }
493 break;
495 case GDK_Tab:
496 {
497 action->private_data->transferFocus = FALSE;
498 wasConsumed = process_tab( widget, 1 );
499 }
500 break;
502 case GDK_ISO_Left_Tab:
503 {
504 action->private_data->transferFocus = FALSE;
505 wasConsumed = process_tab( widget, -1 );
506 }
507 break;
509 case GDK_Up:
510 case GDK_KP_Up:
511 {
512 action->private_data->transferFocus = FALSE;
513 gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
514 gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val + action->private_data->step );
515 wasConsumed = TRUE;
516 }
517 break;
519 case GDK_Down:
520 case GDK_KP_Down:
521 {
522 action->private_data->transferFocus = FALSE;
523 gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
524 gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val - action->private_data->step );
525 wasConsumed = TRUE;
526 }
527 break;
529 case GDK_Page_Up:
530 case GDK_KP_Page_Up:
531 {
532 action->private_data->transferFocus = FALSE;
533 gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
534 gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val + action->private_data->page );
535 wasConsumed = TRUE;
536 }
537 break;
539 case GDK_Page_Down:
540 case GDK_KP_Page_Down:
541 {
542 action->private_data->transferFocus = FALSE;
543 gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
544 gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val - action->private_data->page );
545 wasConsumed = TRUE;
546 }
547 break;
549 case GDK_z:
550 case GDK_Z:
551 {
552 action->private_data->transferFocus = FALSE;
553 gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), action->private_data->lastVal );
554 wasConsumed = TRUE;
555 }
556 break;
558 }
560 return wasConsumed;
561 }