Code

Adding labels to popup menu values
authorjoncruz <joncruz@users.sourceforge.net>
Wed, 21 Mar 2007 01:08:04 +0000 (01:08 +0000)
committerjoncruz <joncruz@users.sourceforge.net>
Wed, 21 Mar 2007 01:08:04 +0000 (01:08 +0000)
src/ege-adjustment-action.cpp
src/ege-adjustment-action.h
src/widgets/toolbox.cpp

index 179f1f24877fce91f415b2ab05b6f457f553b216..d140ae12cceea295a4d70d87d37cb0636b9b4782 100644 (file)
@@ -75,6 +75,14 @@ static void ege_adjustment_action_defocus( EgeAdjustmentAction* action );
 static GtkActionClass* gParentClass = 0;
 static GQuark gDataName = 0;
 
+typedef struct _EgeAdjustmentDescr EgeAdjustmentDescr;
+
+struct _EgeAdjustmentDescr
+{
+    gchar* descr;
+    gdouble value;
+};
+
 struct _EgeAdjustmentActionPrivate
 {
     GtkAdjustment* adj;
@@ -87,6 +95,7 @@ struct _EgeAdjustmentActionPrivate
     gdouble step;
     gdouble page;
     gboolean transferFocus;
+    GList* descriptions;
 };
 
 #define EGE_ADJUSTMENT_ACTION_GET_PRIVATE( o ) ( G_TYPE_INSTANCE_GET_PRIVATE( (o), EGE_ADJUSTMENT_ACTION_TYPE, EgeAdjustmentActionPrivate ) )
@@ -213,6 +222,7 @@ static void ege_adjustment_action_init( EgeAdjustmentAction* action )
     action->private_data->step = 0.0;
     action->private_data->page = 0.0;
     action->private_data->transferFocus = FALSE;
+    action->private_data->descriptions = 0;
 }
 
 EgeAdjustmentAction* ege_adjustment_action_new( GtkAdjustment* adjustment,
@@ -349,6 +359,59 @@ GtkWidget* ege_adjustment_action_get_focuswidget( EgeAdjustmentAction* action )
     return action->private_data->focusWidget;
 }
 
+static void i_free_description( gpointer data, gpointer user_data ) {
+    (void)user_data;
+    if ( data ) {
+        EgeAdjustmentDescr* descr = (EgeAdjustmentDescr*)data;
+       if ( descr->descr ) {
+           g_free( descr->descr );
+           descr->descr = 0;
+       }
+       g_free( descr );
+    }
+}
+
+static gint i_compare_descriptions( gconstpointer a, gconstpointer b )
+{
+    gint val = 0;
+
+    EgeAdjustmentDescr const * aa = (EgeAdjustmentDescr const *)a;
+    EgeAdjustmentDescr const * bb = (EgeAdjustmentDescr const *)b;
+
+    if ( aa && bb ) {
+        if ( aa->value < bb->value ) {
+           val = -1;
+       } else if ( aa->value > bb->value ) {
+           val = 1;
+       }
+    }
+
+    return val;
+}
+
+void ege_adjustment_action_set_descriptions( EgeAdjustmentAction* action, gchar const** descriptions, gdouble const* values, guint count )
+{
+    g_return_if_fail( IS_EGE_ADJUSTMENT_ACTION(action) );
+
+    if ( action->private_data->descriptions ) {
+        g_list_foreach( action->private_data->descriptions, i_free_description, 0 );
+       g_list_free( action->private_data->descriptions );
+       action->private_data->descriptions = 0;
+    }
+
+    if ( count && descriptions && values ) {
+        guint i = 0;
+        for ( i = 0; i < count; i++ ) {
+           EgeAdjustmentDescr* descr = g_new0( EgeAdjustmentDescr, 1 );
+           if ( descriptions[i] ) {
+               descr->descr = g_strdup( descriptions[i] );
+               descr->value = values[i];
+           }
+           action->private_data->descriptions = g_list_insert_sorted( action->private_data->descriptions, (gpointer)descr, i_compare_descriptions );
+       }
+    }
+}
+
 static void process_menu_action( GtkWidget* obj, gpointer data )
 {
     GtkCheckMenuItem* item = GTK_CHECK_MENU_ITEM(obj);
@@ -397,10 +460,37 @@ static void process_menu_action( GtkWidget* obj, gpointer data )
     }
 }
 
-static void create_single_menu( GCallback toggleCb, int val, GtkWidget* menu, EgeAdjustmentAction* act, GtkWidget** dst, GSList** group, gdouble num, gboolean active )
+static void create_single_menu_item( GCallback toggleCb, int val, GtkWidget* menu, EgeAdjustmentAction* act, GtkWidget** dst, GSList** group, gdouble num, gboolean active )
 {
-    char* fmt = g_strdup_printf("%%0.%df", act->private_data->digits);
-    char *str = g_strdup_printf ( fmt, num );
+    gdouble epsilon = 0.1;
+    char* fmt = 0;
+    char* str = 0;
+    EgeAdjustmentDescr* marker = 0;
+    GList* cur = act->private_data->descriptions;
+
+    switch ( act->private_data->digits ) {
+        case 0: epsilon = 1.0; break;
+        case 1: epsilon = 0.1; break;
+        case 2: epsilon = 0.01; break;
+        case 3: epsilon = 0.001; break;
+        case 4: epsilon = 0.0001; break;
+    }
+
+    while ( cur ) {
+        EgeAdjustmentDescr* descr = (EgeAdjustmentDescr*)cur->data;
+       gdouble delta = num - descr->value;
+       if ( delta < 0.0 ) {
+           delta = -delta;
+       }
+       if ( delta < epsilon ) {
+           marker = descr;
+           break;
+       }
+       cur = g_list_next( cur );
+    }
+
+    fmt = g_strdup_printf("%%0.%df%%s%%s", act->private_data->digits);
+    str = g_strdup_printf ( fmt, num, (marker?" ":""), (marker?marker->descr:"") );
 
     *dst = gtk_radio_menu_item_new_with_label( *group, str );
     if ( !*group) {
@@ -438,24 +528,25 @@ static GtkWidget* create_popup_number_menu( EgeAdjustmentAction* act )
                   NULL );
 
     if ( base < upper ) {
-        create_single_menu( G_CALLBACK(process_menu_action), BUMP_TOP, menu, act, &single, &group, upper, FALSE );
+        create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_TOP, menu, act, &single, &group, upper, FALSE );
         if ( (base + page) < upper ) {
-            create_single_menu( G_CALLBACK(process_menu_action), BUMP_PAGE_UP, menu, act, &single, &group, base + page, FALSE );
+            create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_PAGE_UP, menu, act, &single, &group, base + page, FALSE );
         }
         if ( (base + step) < upper ) {
-            create_single_menu( G_CALLBACK(process_menu_action), BUMP_UP, menu, act, &single, &group, base + step, FALSE );
+            create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_UP, menu, act, &single, &group, base + step, FALSE );
         }
     }
 
-    create_single_menu( G_CALLBACK(process_menu_action), BUMP_NONE, menu, act, &single, &group, base, TRUE );
+    create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_NONE, menu, act, &single, &group, base, TRUE );
+
     if ( base > lower ) {
         if ( (base - step) > lower ) {
-            create_single_menu( G_CALLBACK(process_menu_action), BUMP_DOWN, menu, act, &single, &group, base - step, FALSE );
+            create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_DOWN, menu, act, &single, &group, base - step, FALSE );
         }
         if ( (base - page) > lower ) {
-            create_single_menu( G_CALLBACK(process_menu_action), BUMP_PAGE_DOWN, menu, act, &single, &group, base - page, FALSE );
+            create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_PAGE_DOWN, menu, act, &single, &group, base - page, FALSE );
         }
-        create_single_menu( G_CALLBACK(process_menu_action), BUMP_BOTTOM, menu, act, &single, &group, lower, FALSE );
+        create_single_menu_item( G_CALLBACK(process_menu_action), BUMP_BOTTOM, menu, act, &single, &group, lower, FALSE );
     }
 
     return menu;
index 45049fbf5b6a95f7160a554e688aba5619d8ff33..53cb493fe7b13955e12119ac939e3ee8113d8f1e 100644 (file)
@@ -85,6 +85,7 @@ GtkAdjustment* ege_adjustment_action_get_adjustment( EgeAdjustmentAction* action
 void ege_adjustment_action_set_focuswidget( EgeAdjustmentAction* action, GtkWidget* widget );
 GtkWidget* ege_adjustment_action_get_focuswidget( EgeAdjustmentAction* action );
 
+void ege_adjustment_action_set_descriptions( EgeAdjustmentAction* action, gchar const** descriptions, gdouble const* values, guint count );
 
 typedef void (*EgeWidgetFixup)(GtkWidget *widget);
 
index 0d92dbe8a11d76b7fc88edea7210d088adabed8f..fa2f171382181f3d7f77c6b896fb40089d3c6ae7 100644 (file)
@@ -828,6 +828,7 @@ static EgeAdjustmentAction * create_adjustment_action( gchar const *name,
                                                        GtkWidget *dataKludge,
                                                        gboolean altx, gchar const *altx_mark,
                                                        gdouble lower, gdouble upper, gdouble step, gdouble page,
+                                                       gchar const** descrLabels, gdouble const* descrValues, guint descrCount,
                                                        void (*callback)(GtkAdjustment *, GtkWidget *),
                                                        gdouble climb = 0.1, guint digits = 3, double factor = 1.0 )
 {
@@ -841,6 +842,10 @@ static EgeAdjustmentAction * create_adjustment_action( gchar const *name,
 
     EgeAdjustmentAction* act = ege_adjustment_action_new( adj, name, label, tooltip, 0, climb, digits );
 
+    if ( (descrCount > 0) && descrLabels && descrValues ) {
+        ege_adjustment_action_set_descriptions( act, descrLabels, descrValues, descrCount );
+    }
+
     if ( focusTarget ) {
         ege_adjustment_action_set_focuswidget( act, focusTarget );
     }
@@ -1265,16 +1270,22 @@ sp_star_toolbox_new(SPDesktop *desktop)
         }
 
         /* Magnitude */
+        //gchar const* labels[] = {_("tri (default)"), _("quad"), _("pent")};
+        //gdouble values[] = {3, 4, 5};
         eact = create_adjustment_action( "MagnitudeAction",
                                          _("Corners:"), _("Number of corners of a polygon or star"),
                                          "tools.shapes.star", "magnitude", 3,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
-                                         3, 1024, 1, 1,
-                                         sp_stb_magnitude_value_changed );
+                                         3, 1024, 1, 5,
+                                         0, 0, 0, // labels, values, G_N_ELEMENTS(labels), 
+                                         sp_stb_magnitude_value_changed,
+                                         1.0, 0 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Spoke ratio */
+        //gchar const* labels2[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values2[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "SpokeAction",
                                          _("Spoke ratio:"),
                                          // TRANSLATORS: Tip radius of a star is the distance from the center to the farthest handle.
@@ -1283,6 +1294,7 @@ sp_star_toolbox_new(SPDesktop *desktop)
                                          "tools.shapes.star", "proportion", 0.5,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          0.01, 1.0, 0.01, 0.1,
+                                         0, 0, 0, // labels2, values2, G_N_ELEMENTS(labels2),
                                          sp_stb_proportion_value_changed );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         g_object_set_data(G_OBJECT(holder), "prop_action", eact);
@@ -1294,21 +1306,27 @@ sp_star_toolbox_new(SPDesktop *desktop)
         }
 
         /* Roundedness */
+        //gchar const* labels3[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values3[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "RoundednessAction",
                                          _("Rounded:"), _("How much rounded are the corners (0 for sharp)"),
                                          "tools.shapes.star", "rounded", 0.0,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          -100.0, 100.0, 0.01, 0.1,
+                                         0, 0, 0, // labels3, values3, G_N_ELEMENTS(labels3),
                                          sp_stb_rounded_value_changed );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Randomization */
+        //gchar const* labels4[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values4[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "RandomizationAction",
                                          _("Randomized:"), _("Scatter randomly the corners and angles"),
                                          "tools.shapes.star", "randomized", 0.0,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          -10.0, 10.0, 0.001, 0.01,
+                                         0, 0, 0, // labels4, values4, G_N_ELEMENTS(labels4),
                                          sp_stb_randomized_value_changed, 0.1, 3 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
@@ -2213,84 +2231,108 @@ sp_calligraphy_toolbox_new(SPDesktop *desktop)
         EgeAdjustmentAction* eact = 0;
 
         /* Width */
+        //gchar const* labels1[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values1[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "WidthAction",
                                          _("Width:"), _("The width of the calligraphic pen (relative to the visible canvas area)"),
                                          "tools.calligraphic", "width", 15,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, TRUE, "altx-calligraphy",
                                          1, 100, 1.0, 10.0,
+                                         0, 0, 0, // labels1, values1, G_N_ELEMENTS(labels1),
                                          sp_ddc_width_value_changed2,  0.01, 0, 100 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Thinning */
+        //gchar const* labels2[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values2[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "ThinningAction",
                                          _("Thinning:"), _("How much velocity thins the stroke (> 0 makes fast strokes thinner, < 0 makes them broader, 0 makes width independent of velocity)"),
                                          "tools.calligraphic", "thinning", 0.1,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          -1.0, 1.0, 0.01, 0.1,
+                                         0, 0, 0, // labels2, values2, G_N_ELEMENTS(labels2),
                                          sp_ddc_velthin_value_changed2, 0.01, 2);
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Angle */
+        gchar const* labels3[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        gdouble values3[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "AngleAction",
                                          _("Angle:"), _("The angle of the pen's nib (in degrees; 0 = horizontal; has no effect if fixation = 0)"),
                                          "tools.calligraphic", "angle", 30,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, TRUE, "calligraphy-angle",
                                          -90.0, 90.0, 1.0, 10.0,
+                                         labels3, values3, G_N_ELEMENTS(labels3),
                                          sp_ddc_angle_value_changed2, 1, 0 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
         calligraphy_angle = eact;
 
         /* Fixation */
+        //gchar const* labels4[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values4[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "FixationAction",
                                          _("Fixation:"), _("Angle behavior (0 = nib always perpendicular to stroke direction, 1 = fixed angle)"),
                                          "tools.calligraphic", "flatness", 0.9,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          0.0, 1.0, 0.01, 0.1,
+                                         0, 0, 0, // labels4, values4, G_N_ELEMENTS(labels4),
                                          sp_ddc_flatness_value_changed2, 0.01, 2 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Cap Rounding */
+        //gchar const* labels5[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values5[] = {-90, 0, 30, 90};
         // TRANSLATORS: "cap" means "end" (both start and finish) here
         eact = create_adjustment_action( "CapRoundingAction",
                                          _("Caps:"), _("Increase to make caps at the ends of strokes protrude more (0 = no caps, 1 = round caps)"),
                                          "tools.calligraphic", "cap_rounding", 0.0,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          0.0, 5.0, 0.01, 0.1,
+                                         0, 0, 0, // labels5, values5, G_N_ELEMENTS(labels5),
                                          sp_ddc_cap_rounding_value_changed2, 0.01, 2 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Tremor */
+        //gchar const* labels6[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values6[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "TremorAction",
                                          _("Tremor:"), _("Increase to make strokes rugged and trembling"),
                                          "tools.calligraphic", "tremor", 0.0,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          0.0, 1.0, 0.01, 0.1,
+                                         0, 0, 0, // labels6, values6, G_N_ELEMENTS(labels6),
                                          sp_ddc_tremor_value_changed2, 0.01, 2 );
 
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Wiggle */
+        //gchar const* labels7[] = {_("(left edge up)"), _("(horizontal)"), _("(default)"), _("(right edge up)")};
+        //gdouble values7[] = {-90, 0, 30, 90};
         eact = create_adjustment_action( "WiggleAction",
                                          _("Wiggle:"), _("Increase to make the pen waver and wiggle"),
                                          "tools.calligraphic", "wiggle", 0.0,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          0.0, 1.0, 0.01, 0.1,
+                                         0, 0, 0, // labels7, values7, G_N_ELEMENTS(labels7),
                                          sp_ddc_wiggle_value_changed2, 0.01, 2 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );
 
         /* Mass */
+        //gchar const* labels8[] = {_("low"), _("(default)"), _("high")};
+        //gdouble values8[] = {0.0, 0.2, 1.0};
         eact = create_adjustment_action( "MassAction",
                                          _("Mass:"), _("Increase to make the pen drag behind, as if slowed by inertia"),
                                          "tools.calligraphic", "mass", 0.02,
                                          GTK_WIDGET(desktop->canvas), NULL, holder, FALSE, NULL,
                                          0.0, 1.0, 0.01, 0.1,
+                                         0, 0, 0, //labels8, values8, G_N_ELEMENTS(labels8),
                                          sp_ddc_mass_value_changed2, 0.01, 2 );
         gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
         gtk_action_set_sensitive( GTK_ACTION(eact), TRUE );