static void string_to_unit (const GValue *src_value, GValue *dest_value) { const gchar *str; gint num_units; gint i; str = g_value_get_string (src_value); if (!str || !*str) goto error; num_units = gimp_unit_get_number_of_units (); for (i = GIMP_UNIT_PIXEL; i < num_units; i++) if (strcmp (str, gimp_unit_get_identifier (i)) == 0) break; if (i == num_units) { if (strcmp (str, gimp_unit_get_identifier (GIMP_UNIT_PERCENT)) == 0) i = GIMP_UNIT_PERCENT; else goto error; } g_value_set_int (dest_value, i); return; error: g_warning ("Can't convert string '%s' to GimpUnit.", str); }
/** * gimp_unit_format_string: * @format: A printf-like format string which is used to create the unit * string. * @unit: A unit. * * The @format string supports the following percent expansions: * * <informaltable pgwide="1" frame="none" role="enum"> * <tgroup cols="2"><colspec colwidth="1*"/><colspec colwidth="8*"/> * <tbody> * <row> * <entry>% f</entry> * <entry>Factor (how many units make up an inch)</entry> * </row> * <row> * <entry>% y</entry> * <entry>Symbol (e.g. "''" for GIMP_UNIT_INCH)</entry> * </row> * <row> * <entry>% a</entry> * <entry>Abbreviation</entry> * </row> * <row> * <entry>% s</entry> * <entry>Singular</entry> * </row> * <row> * <entry>% p</entry> * <entry>Plural</entry> * </row> * <row> * <entry>%%</entry> * <entry>Literal percent</entry> * </row> * </tbody> * </tgroup> * </informaltable> * * Returns: A newly allocated string with above percent expressions * replaced with the resp. strings for @unit. * * Since: 2.8 **/ gchar * gimp_unit_format_string (const gchar *format, GimpUnit unit) { gchar buffer[1024]; gint i = 0; g_return_val_if_fail (format != NULL, NULL); g_return_val_if_fail (unit == GIMP_UNIT_PERCENT || (unit >= GIMP_UNIT_PIXEL && unit < gimp_unit_get_number_of_units ()), NULL); while (i < (sizeof (buffer) - 1) && *format) { switch (*format) { case '%': format++; switch (*format) { case 0: g_warning ("%s: unit-menu-format string ended within %%-sequence", G_STRFUNC); break; case '%': buffer[i++] = '%'; break; case 'f': /* factor (how many units make up an inch) */ i += print (buffer, sizeof (buffer), i, "%f", gimp_unit_get_factor (unit)); break; case 'y': /* symbol ("''" for inch) */ i += print (buffer, sizeof (buffer), i, "%s", gimp_unit_get_symbol (unit)); break; case 'a': /* abbreviation */ i += print (buffer, sizeof (buffer), i, "%s", gimp_unit_get_abbreviation (unit)); break; case 's': /* singular */ i += print (buffer, sizeof (buffer), i, "%s", gimp_unit_get_singular (unit)); break; case 'p': /* plural */ i += print (buffer, sizeof (buffer), i, "%s", gimp_unit_get_plural (unit)); break; default: g_warning ("%s: unit-menu-format contains unknown format " "sequence '%%%c'", G_STRFUNC, *format); break; } break; default: buffer[i++] = *format; break; } format++; } buffer[MIN (i, sizeof (buffer) - 1)] = 0; return g_strdup (buffer); }
/** * gimp_unit_menu_set_unit: * @menu: The unit menu you want to set the unit for. * @unit: The new unit. * * Sets a new #GimpUnit for the specified #GimpUnitMenu. **/ void gimp_unit_menu_set_unit (GimpUnitMenu *menu, GimpUnit unit) { GtkWidget *menuitem = NULL; GList *items; gint user_unit; g_return_if_fail (GIMP_IS_UNIT_MENU (menu)); g_return_if_fail (((unit >= GIMP_UNIT_PIXEL) && ((unit > GIMP_UNIT_PIXEL) || menu->show_pixels) && (unit < gimp_unit_get_number_of_units ())) || ((unit == GIMP_UNIT_PERCENT) && menu->show_percent)); if (unit == menu->unit) return; items = GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu)->children; user_unit = (GIMP_UNIT_END + (((menu->show_pixels || menu->show_percent) ? 2 : 0) + ((menu->show_pixels && menu->show_percent) ? 1 : 0))); if ((unit >= GIMP_UNIT_END) && (unit != GIMP_UNIT_PERCENT)) { gchar *string; if ((g_list_length (items) - 3) >= user_unit) { gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items, user_unit - 1))); gtk_widget_destroy (GTK_WIDGET (g_list_nth_data (items, user_unit - 1))); } menuitem = gtk_menu_item_new (); gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu), menuitem); gtk_widget_set_sensitive (menuitem, FALSE); gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu), menuitem, user_unit - 1); gtk_widget_show (menuitem); string = gimp_unit_format_string (menu->format, unit); menuitem = gtk_menu_item_new_with_label (string); g_free (string); gtk_menu_shell_append (GTK_MENU_SHELL (GTK_OPTION_MENU (menu)->menu), menuitem); g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu", GINT_TO_POINTER (unit)); gtk_menu_reorder_child (GTK_MENU (GTK_OPTION_MENU (menu)->menu), menuitem, user_unit); gtk_widget_show (menuitem); g_signal_connect (menuitem, "activate", G_CALLBACK (gimp_unit_menu_callback), menu); } menu->unit = unit; gtk_option_menu_set_history (GTK_OPTION_MENU (menu), (unit == GIMP_UNIT_PIXEL) ? 0 : ((unit == GIMP_UNIT_PERCENT) ? (menu->show_pixels ? 1 : 0) : (((menu->show_pixels || menu->show_percent) ? 2 : 0) + ((menu->show_pixels && menu->show_percent) ? 1 : 0) + ((unit < GIMP_UNIT_END) ? (unit - 1) : GIMP_UNIT_END)))); g_signal_emit (menu, gimp_unit_menu_signals[UNIT_CHANGED], 0); }
/* private function of gimp_unit_menu_callback () */ static void gimp_unit_menu_create_selection (GimpUnitMenu *menu) { GtkWidget *parent = gtk_widget_get_toplevel (GTK_WIDGET (menu)); GtkWidget *vbox; GtkWidget *scrolled_win; GtkListStore *list; GtkTreeSelection *sel; GtkTreeIter iter; GtkTreePath *path; GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT; GimpUnit unit; gint num_units; if (gtk_window_get_modal (GTK_WINDOW (parent))) flags |= GTK_DIALOG_MODAL; menu->selection = gimp_dialog_new (_("Unit Selection"), "gimp-unit-selection", parent, flags, gimp_standard_help_func, "gimp-unit-dialog", GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL); gtk_dialog_set_alternative_button_order (GTK_DIALOG (menu->selection), GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL, -1); g_object_add_weak_pointer (G_OBJECT (menu->selection), (gpointer) &menu->selection); g_signal_connect (menu->selection, "response", G_CALLBACK (gimp_unit_menu_selection_response), menu); g_signal_connect_object (menu, "unmap", G_CALLBACK (gtk_widget_destroy), menu->selection, G_CONNECT_SWAPPED); /* the main vbox */ vbox = gtk_vbox_new (FALSE, 0); gtk_container_set_border_width (GTK_CONTAINER (vbox), 2); gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (menu->selection))), vbox, TRUE, TRUE, 0); gtk_widget_show (vbox); /* the selection list */ scrolled_win = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_ETCHED_IN); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS); gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0); gtk_widget_show (scrolled_win); list = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT); menu->tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list)); g_object_unref (list); gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv), -1, _("Unit"), gtk_cell_renderer_text_new (), "text", UNIT_COLUMN, NULL); gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (menu->tv), -1, _("Factor"), gtk_cell_renderer_text_new (), "text", FACTOR_COLUMN, NULL); /* the unit lines */ num_units = gimp_unit_get_number_of_units (); for (unit = GIMP_UNIT_END; unit < num_units; unit++) { gchar *string; gtk_list_store_append (list, &iter); string = gimp_unit_format_string (menu->format, unit); gtk_list_store_set (list, &iter, UNIT_COLUMN, string, -1); g_free (string); string = gimp_unit_format_string ("(%f)", unit); gtk_list_store_set (list, &iter, FACTOR_COLUMN, string, -1); g_free (string); gtk_list_store_set (list, &iter, DATA_COLUMN, unit, -1); } gtk_widget_set_size_request (menu->tv, -1, 150); gtk_container_add (GTK_CONTAINER (scrolled_win), menu->tv); g_signal_connect (menu->tv, "row-activated", G_CALLBACK (gimp_unit_menu_selection_row_activated_callback), menu); gtk_widget_show (menu->tv); g_signal_connect (menu->tv, "destroy", G_CALLBACK (gtk_widget_destroyed), &menu->tv); gtk_widget_show (vbox); gtk_widget_show (menu->selection); if (menu->unit >= GIMP_UNIT_END) { path = gtk_tree_path_new (); gtk_tree_path_append_index (path, menu->unit - GIMP_UNIT_END); sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (menu->tv)); gtk_tree_selection_select_path (sel, path); gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (menu->tv), path, NULL, FALSE, 0.0, 0.0); } }
/** * gimp_unit_menu_new: * @format: A printf-like format string which is used to create the unit * strings. * @unit: The initially selected unit. * @show_pixels: %TRUE if the unit menu should contain an item for * GIMP_UNIT_PIXEL. * @show_percent: %TRUE in the unit menu should contain an item for * GIMP_UNIT_PERCENT. * @show_custom: %TRUE if the unit menu should contain a "More..." item for * opening the user-defined-unit selection dialog. * * Creates a new #GimpUnitMenu widget. * * For the @format string's possible expansions, see gimp_unit_format_string(). * * Returns: A pointer to the new #GimpUnitMenu widget. **/ GtkWidget * gimp_unit_menu_new (const gchar *format, GimpUnit unit, gboolean show_pixels, gboolean show_percent, gboolean show_custom) { GimpUnitMenu *unit_menu; GtkWidget *menu; GtkWidget *menuitem; gchar *string; GimpUnit u; g_return_val_if_fail (((unit >= GIMP_UNIT_PIXEL) && (unit < gimp_unit_get_number_of_units ())) || (unit == GIMP_UNIT_PERCENT), NULL); if ((unit >= gimp_unit_get_number_of_built_in_units ()) && (unit != GIMP_UNIT_PERCENT)) show_custom = TRUE; unit_menu = g_object_new (GIMP_TYPE_UNIT_MENU, NULL); unit_menu->format = g_strdup (format); unit_menu->show_pixels = show_pixels; unit_menu->show_percent = show_percent; menu = gtk_menu_new (); for (u = show_pixels ? GIMP_UNIT_PIXEL : GIMP_UNIT_INCH; u < gimp_unit_get_number_of_built_in_units (); u++) { /* special cases "pixels" and "percent" */ if (u == GIMP_UNIT_INCH) { if (show_percent) { string = gimp_unit_format_string (format, GIMP_UNIT_PERCENT); menuitem = gtk_menu_item_new_with_label (string); g_free (string); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu", GINT_TO_POINTER (GIMP_UNIT_PERCENT)); gtk_widget_show (menuitem); g_signal_connect (menuitem, "activate", G_CALLBACK (gimp_unit_menu_callback), unit_menu); } if (show_pixels || show_percent) { menuitem = gtk_menu_item_new (); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); gtk_widget_set_sensitive (menuitem, FALSE); gtk_widget_show (menuitem); } } string = gimp_unit_format_string (format, u); menuitem = gtk_menu_item_new_with_label (string); g_free (string); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu", GINT_TO_POINTER (u)); gtk_widget_show (menuitem); g_signal_connect (menuitem, "activate", G_CALLBACK (gimp_unit_menu_callback), unit_menu); } if ((unit >= gimp_unit_get_number_of_built_in_units ()) && (unit != GIMP_UNIT_PERCENT)) { menuitem = gtk_menu_item_new (); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); gtk_widget_set_sensitive (menuitem, FALSE); gtk_widget_show (menuitem); string = gimp_unit_format_string (format, unit); menuitem = gtk_menu_item_new_with_label (string); g_free (string); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu", GINT_TO_POINTER (unit)); gtk_widget_show (menuitem); g_signal_connect (menuitem, "activate", G_CALLBACK (gimp_unit_menu_callback), unit_menu); } if (show_custom) { menuitem = gtk_menu_item_new (); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); gtk_widget_set_sensitive (menuitem, FALSE); gtk_widget_show (menuitem); menuitem = gtk_menu_item_new_with_label (_("More...")); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); g_object_set_data (G_OBJECT (menuitem), "gimp_unit_menu", GINT_TO_POINTER (GIMP_UNIT_PERCENT + 1)); gtk_widget_show (menuitem); g_signal_connect (menuitem, "activate", G_CALLBACK (gimp_unit_menu_callback), unit_menu); } gtk_option_menu_set_menu (GTK_OPTION_MENU (unit_menu), menu); unit_menu->unit = unit; gtk_option_menu_set_history (GTK_OPTION_MENU (unit_menu), (unit == GIMP_UNIT_PIXEL) ? 0 : ((unit == GIMP_UNIT_PERCENT) ? (show_pixels ? 1 : 0) : (((show_pixels || show_percent) ? 2 : 0) + ((show_pixels && show_percent) ? 1 : 0) + ((unit < GIMP_UNIT_END) ? (unit - 1) : GIMP_UNIT_END)))); return GTK_WIDGET (unit_menu); }