home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Linux / gimp-2.2.0.tar.gz / gimp-2.2.0.tar / gimp-2.2.0 / libgimpwidgets / gimpintcombobox.c < prev    next >
C/C++ Source or Header  |  2004-04-21  |  11KB  |  388 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpintcombobox.c
  5.  * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include "config.h"
  24.  
  25. #include <libintl.h>
  26.  
  27. #include <gtk/gtk.h>
  28.  
  29. #include "gimpwidgetstypes.h"
  30.  
  31. #include "gimpintcombobox.h"
  32. #include "gimpintstore.h"
  33.  
  34.  
  35. static void  gimp_int_combo_box_init (GimpIntComboBox *combo_box);
  36.  
  37.  
  38. GType
  39. gimp_int_combo_box_get_type (void)
  40. {
  41.   static GType box_type = 0;
  42.  
  43.   if (! box_type)
  44.     {
  45.       static const GTypeInfo box_info =
  46.       {
  47.         sizeof (GimpIntComboBoxClass),
  48.         NULL,           /* base_init      */
  49.         NULL,           /* base_finalize  */
  50.         NULL,           /* class_init     */
  51.         NULL,           /* class_finalize */
  52.         NULL,           /* class_data     */
  53.         sizeof (GimpIntComboBox),
  54.         0,              /* n_preallocs    */
  55.         (GInstanceInitFunc) gimp_int_combo_box_init
  56.       };
  57.  
  58.       box_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
  59.                                          "GimpIntComboBox",
  60.                                          &box_info, 0);
  61.     }
  62.  
  63.   return box_type;
  64. }
  65.  
  66. static void
  67. gimp_int_combo_box_init (GimpIntComboBox *combo_box)
  68. {
  69.   GtkListStore    *store;
  70.   GtkCellRenderer *cell;
  71.  
  72.   store = gimp_int_store_new ();
  73.  
  74.   gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
  75.  
  76.   g_object_unref (store);
  77.  
  78.   cell = gtk_cell_renderer_pixbuf_new ();
  79.   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, FALSE);
  80.   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
  81.                                   "stock_id", GIMP_INT_STORE_STOCK_ID,
  82.                                   "pixbuf",   GIMP_INT_STORE_PIXBUF,
  83.                                   NULL);
  84.  
  85.   cell = gtk_cell_renderer_text_new ();
  86.   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
  87.   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
  88.                                   "text", GIMP_INT_STORE_LABEL,
  89.                                   NULL);
  90. }
  91.  
  92.  
  93. /**
  94.  * gimp_int_combo_box_new:
  95.  * @first_label: the label of the first item
  96.  * @first_value: the value of the first item
  97.  * @...: a %NULL terminated list of more label, value pairs
  98.  *
  99.  * Creates a GtkComboBox that has integer values associated with each
  100.  * item. The items to fill the combo box with are specified as a %NULL
  101.  * terminated list of label/value pairs.
  102.  *
  103.  * Return value: a new #GimpIntComboBox.
  104.  *
  105.  * Since: GIMP 2.2
  106.  **/
  107. GtkWidget *
  108. gimp_int_combo_box_new (const gchar *first_label,
  109.                         gint         first_value,
  110.                         ...)
  111. {
  112.   GtkWidget *combo_box;
  113.   va_list    args;
  114.  
  115.   va_start (args, first_value);
  116.  
  117.   combo_box = gimp_int_combo_box_new_valist (first_label, first_value, args);
  118.  
  119.   va_end (args);
  120.  
  121.   return combo_box;
  122. }
  123.  
  124. /**
  125.  * gimp_int_combo_box_new_valist:
  126.  * @first_label: the label of the first item
  127.  * @first_value: the value of the first item
  128.  * @values: a va_list with more values
  129.  *
  130.  * A variant of gimp_int_combo_box_new() that takes a va_list of
  131.  * label/value pairs. Probably only useful for language bindings.
  132.  *
  133.  * Return value: a new #GimpIntComboBox.
  134.  *
  135.  * Since: GIMP 2.2
  136.  **/
  137. GtkWidget *
  138. gimp_int_combo_box_new_valist (const gchar *first_label,
  139.                                gint         first_value,
  140.                                va_list      values)
  141. {
  142.   GtkWidget    *combo_box;
  143.   GtkListStore *store;
  144.   const gchar  *label;
  145.   gint          value;
  146.  
  147.   store = gimp_int_store_new ();
  148.  
  149.   combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
  150.                             "model", store,
  151.                             NULL);
  152.   g_object_unref (store);
  153.  
  154.   for (label = first_label, value = first_value;
  155.        label;
  156.        label = va_arg (values, const gchar *), value = va_arg (values, gint))
  157.     {
  158.       GtkTreeIter  iter;
  159.  
  160.       gtk_list_store_append (store, &iter);
  161.       gtk_list_store_set (store, &iter,
  162.                           GIMP_INT_STORE_VALUE, value,
  163.                           GIMP_INT_STORE_LABEL, label,
  164.                           -1);
  165.     }
  166.  
  167.   return combo_box;
  168. }
  169.  
  170. /**
  171.  * gimp_int_combo_box_new_array:
  172.  * @n_values: the number of values
  173.  * @labels:   an array of labels (array length must be @n_values)
  174.  *
  175.  * A variant of gimp_int_combo_box_new() that takes an array of labels.
  176.  * The array indices are used as values.
  177.  *
  178.  * Return value: a new #GimpIntComboBox.
  179.  *
  180.  * Since: GIMP 2.2
  181.  **/
  182. GtkWidget *
  183. gimp_int_combo_box_new_array (gint         n_values,
  184.                               const gchar *labels[])
  185. {
  186.   GtkWidget    *combo_box;
  187.   GtkListStore *store;
  188.   gint          i;
  189.  
  190.   g_return_val_if_fail (n_values > 0, NULL);
  191.   g_return_val_if_fail (labels != NULL, NULL);
  192.  
  193.   store = gimp_int_store_new ();
  194.  
  195.   combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
  196.                             "model", store,
  197.                             NULL);
  198.   g_object_unref (store);
  199.  
  200.   for (i = 0; i < n_values; i++)
  201.     {
  202.       GtkTreeIter  iter;
  203.  
  204.       if (labels[i])
  205.         {
  206.           gtk_list_store_append (store, &iter);
  207.           gtk_list_store_set (store, &iter,
  208.                               GIMP_INT_STORE_VALUE, i,
  209.                               GIMP_INT_STORE_LABEL, gettext (labels[i]),
  210.                               -1);
  211.         }
  212.     }
  213.  
  214.   return combo_box;
  215. }
  216.  
  217. /**
  218.  * gimp_int_combo_box_prepend:
  219.  * @combo_box: a #GimpIntComboBox
  220.  * @...:       pairs of column number and value, terminated with -1
  221.  *
  222.  * This function provides a convenient way to prepend items to a
  223.  * #GimpIntComboBox. It prepends a row to the @combo_box's list store
  224.  * and calls gtk_list_store_set() for you.
  225.  *
  226.  * The column number must be taken from the enum #GimpIntStoreColumns.
  227.  *
  228.  * Since: GIMP 2.2
  229.  **/
  230. void
  231. gimp_int_combo_box_prepend (GimpIntComboBox *combo_box,
  232.                             ...)
  233. {
  234.   GtkListStore *store;
  235.   GtkTreeIter   iter;
  236.   va_list       args;
  237.  
  238.   g_return_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box));
  239.  
  240.   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
  241.  
  242.   va_start (args, combo_box);
  243.  
  244.   gtk_list_store_prepend (store, &iter);
  245.   gtk_list_store_set_valist (store, &iter, args);
  246.  
  247.   va_end (args);
  248. }
  249.  
  250. /**
  251.  * gimp_int_combo_box_append:
  252.  * @combo_box: a #GimpIntComboBox
  253.  * @...:       pairs of column number and value, terminated with -1
  254.  *
  255.  * This function provides a convenient way to append items to a
  256.  * #GimpIntComboBox. It appends a row to the @combo_box's list store
  257.  * and calls gtk_list_store_set() for you.
  258.  *
  259.  * The column number must be taken from the enum #GimpIntStoreColumns.
  260.  *
  261.  * Since: GIMP 2.2
  262.  **/
  263. void
  264. gimp_int_combo_box_append (GimpIntComboBox *combo_box,
  265.                            ...)
  266. {
  267.   GtkListStore *store;
  268.   GtkTreeIter   iter;
  269.   va_list       args;
  270.  
  271.   g_return_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box));
  272.  
  273.   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
  274.  
  275.   va_start (args, combo_box);
  276.  
  277.   gtk_list_store_append (store, &iter);
  278.   gtk_list_store_set_valist (store, &iter, args);
  279.  
  280.   va_end (args);
  281. }
  282.  
  283. /**
  284.  * gimp_int_combo_box_set_active:
  285.  * @combo_box: a #GimpIntComboBox
  286.  * @value:     an integer value
  287.  *
  288.  * Looks up the item that belongs to the given @value and makes it the
  289.  * selected item in the @combo_box.
  290.  *
  291.  * Return value: %TRUE on success or %FALSE if there was no item for
  292.  *               this value.
  293.  *
  294.  * Since: GIMP 2.2
  295.  **/
  296. gboolean
  297. gimp_int_combo_box_set_active (GimpIntComboBox *combo_box,
  298.                                gint             value)
  299. {
  300.   GtkTreeModel *model;
  301.   GtkTreeIter   iter;
  302.  
  303.   g_return_val_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box), FALSE);
  304.  
  305.   model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
  306.  
  307.   if (gimp_int_store_lookup_by_value (model, value, &iter))
  308.     {
  309.       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
  310.       return TRUE;
  311.     }
  312.  
  313.   return FALSE;
  314. }
  315.  
  316. /**
  317.  * gimp_int_combo_box_get_active:
  318.  * @combo_box: a #GimpIntComboBox
  319.  * @value:     return location for the integer value
  320.  *
  321.  * Retrieves the value of the selected (active) item in the @combo_box.
  322.  *
  323.  * Return value: %TRUE if @value has been set or %FALSE if no item was
  324.  *               active.
  325.  *
  326.  * Since: GIMP 2.2
  327.  **/
  328. gboolean
  329. gimp_int_combo_box_get_active (GimpIntComboBox *combo_box,
  330.                                gint            *value)
  331. {
  332.   GtkTreeIter  iter;
  333.  
  334.   g_return_val_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box), FALSE);
  335.   g_return_val_if_fail (value != NULL, FALSE);
  336.  
  337.   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
  338.     {
  339.       gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)),
  340.                           &iter,
  341.                           GIMP_INT_STORE_VALUE, value,
  342.                           -1);
  343.       return TRUE;
  344.     }
  345.  
  346.   return FALSE;
  347. }
  348.  
  349. /**
  350.  * gimp_int_combo_box_connect:
  351.  * @combo_box: a #GimpIntComboBox
  352.  * @value:     the value to set
  353.  * @callback:  a callback to connect to the @combo_box's "changed" signal
  354.  * @data:      a pointer passed as data to g_signal_connect()
  355.  *
  356.  * A convenience function that sets the inital @value of a
  357.  * #GimpIntComboBox and connects @callback to the "changed"
  358.  * signal.
  359.  *
  360.  * This function also calls the @callback once after setting the
  361.  * initial @value. This is often convenient when working with combo
  362.  * boxes that select a default active item (like for example
  363.  * gimp_drawable_combo_box_new). If you pass an invalid initial
  364.  * @value, the @callback will be called with the default item active.
  365.  *
  366.  * Return value: the signal handler ID as returned by g_signal_connect()
  367.  *
  368.  * Since: GIMP 2.2
  369.  **/
  370. gulong
  371. gimp_int_combo_box_connect (GimpIntComboBox *combo_box,
  372.                             gint             value,
  373.                             GCallback        callback,
  374.                             gpointer         data)
  375. {
  376.   gulong handler = 0;
  377.  
  378.   g_return_val_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box), 0);
  379.  
  380.   if (callback)
  381.     handler = g_signal_connect (combo_box, "changed", callback, data);
  382.  
  383.   if (! gimp_int_combo_box_set_active (combo_box, value))
  384.     g_signal_emit_by_name (combo_box, "changed", NULL);
  385.  
  386.   return handler;
  387. }
  388.