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 / gimpcolornotebook.c < prev    next >
C/C++ Source or Header  |  2004-07-13  |  18KB  |  493 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpcolornotebook.c
  5.  * Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
  6.  *
  7.  * based on color_notebook module
  8.  * Copyright (C) 1998 Austin Donnelly <austin@greenend.org.uk>
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Lesser General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2 of the License, or (at your option) any later version.
  14.  *
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.  * Library General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU Lesser General Public
  21.  * License along with this library; if not, write to the
  22.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23.  * Boston, MA 02111-1307, USA.
  24.  */
  25.  
  26. #include "config.h"
  27.  
  28. #include <gtk/gtk.h>
  29.  
  30. #include "libgimpcolor/gimpcolor.h"
  31.  
  32. #include "gimpwidgetstypes.h"
  33.  
  34. #include "gimpcolornotebook.h"
  35. #include "gimpcolorscales.h"
  36. #include "gimpwidgetsmarshal.h"
  37.  
  38. #include "libgimp/libgimp-intl.h"
  39.  
  40.  
  41. #define DEFAULT_TAB_BORDER     0
  42. #define DEFAULT_TAB_ICON_SIZE  GTK_ICON_SIZE_BUTTON
  43.  
  44.  
  45. static void   gimp_color_notebook_class_init (GimpColorNotebookClass *klass);
  46. static void   gimp_color_notebook_init       (GimpColorNotebook      *notebook);
  47.  
  48. static void   gimp_color_notebook_finalize        (GObject           *object);
  49.  
  50. static void   gimp_color_notebook_style_set       (GtkWidget         *widget,
  51.                                                    GtkStyle          *prev_style);
  52. static void   gimp_color_notebook_togg_visible    (GimpColorSelector *selector,
  53.                                                    gboolean           visible);
  54. static void   gimp_color_notebook_togg_sensitive  (GimpColorSelector *selector,
  55.                                                    gboolean           sensitive);
  56. static void   gimp_color_notebook_set_show_alpha  (GimpColorSelector *selector,
  57.                                                    gboolean           show_alpha);
  58. static void   gimp_color_notebook_set_color       (GimpColorSelector *selector,
  59.                                                    const GimpRGB     *rgb,
  60.                                                    const GimpHSV     *hsv);
  61. static void   gimp_color_notebook_set_channel     (GimpColorSelector *selector,
  62.                                                    GimpColorSelectorChannel channel);
  63.  
  64. static void   gimp_color_notebook_switch_page     (GtkNotebook       *gtk_notebook,
  65.                                                    GtkNotebookPage   *page,
  66.                                                    guint              page_num,
  67.                                                    GimpColorNotebook *notebook);
  68.  
  69. static void   gimp_color_notebook_color_changed   (GimpColorSelector *page,
  70.                                                    const GimpRGB     *rgb,
  71.                                                    const GimpHSV     *hsv,
  72.                                                    GimpColorNotebook *notebook);
  73. static void   gimp_color_notebook_channel_changed (GimpColorSelector *page,
  74.                                                    GimpColorSelectorChannel channel,
  75.                                                    GimpColorNotebook *notebook);
  76.  
  77. static GtkWidget * gimp_color_notebook_add_page   (GimpColorNotebook *notebook,
  78.                                                    GType              page_type);
  79.  
  80.  
  81. static GimpColorSelectorClass *parent_class = NULL;
  82.  
  83.  
  84. GType
  85. gimp_color_notebook_get_type (void)
  86. {
  87.   static GType notebook_type = 0;
  88.  
  89.   if (! notebook_type)
  90.     {
  91.       static const GTypeInfo notebook_info =
  92.       {
  93.         sizeof (GimpColorNotebookClass),
  94.     (GBaseInitFunc) NULL,
  95.     (GBaseFinalizeFunc) NULL,
  96.     (GClassInitFunc) gimp_color_notebook_class_init,
  97.     NULL,           /* class_finalize */
  98.     NULL,           /* class_data     */
  99.     sizeof (GimpColorNotebook),
  100.     0,              /* n_preallocs    */
  101.     (GInstanceInitFunc) gimp_color_notebook_init,
  102.       };
  103.  
  104.       notebook_type = g_type_register_static (GIMP_TYPE_COLOR_SELECTOR,
  105.                                               "GimpColorNotebook",
  106.                                               ¬ebook_info, 0);
  107.     }
  108.  
  109.   return notebook_type;
  110. }
  111.  
  112. static void
  113. gimp_color_notebook_class_init (GimpColorNotebookClass *klass)
  114. {
  115.   GObjectClass           *object_class   = G_OBJECT_CLASS (klass);
  116.   GtkWidgetClass         *widget_class   = GTK_WIDGET_CLASS (klass);
  117.   GimpColorSelectorClass *selector_class = GIMP_COLOR_SELECTOR_CLASS (klass);
  118.  
  119.   parent_class = g_type_class_peek_parent (klass);
  120.  
  121.   object_class->finalize                = gimp_color_notebook_finalize;
  122.  
  123.   widget_class->style_set               = gimp_color_notebook_style_set;
  124.  
  125.   selector_class->name                  = "Notebook";
  126.   selector_class->help_id               = "gimp-colorselector-notebook";
  127.   selector_class->set_toggles_visible   = gimp_color_notebook_togg_visible;
  128.   selector_class->set_toggles_sensitive = gimp_color_notebook_togg_sensitive;
  129.   selector_class->set_show_alpha        = gimp_color_notebook_set_show_alpha;
  130.   selector_class->set_color             = gimp_color_notebook_set_color;
  131.   selector_class->set_channel           = gimp_color_notebook_set_channel;
  132.  
  133.   gtk_widget_class_install_style_property (widget_class,
  134.                                            g_param_spec_int ("tab_border",
  135.                                                              NULL,
  136.                                                              "Width of the border around the tab contents",
  137.                                                              0, G_MAXINT,
  138.                                                              DEFAULT_TAB_BORDER,
  139.                                                              G_PARAM_READABLE));
  140.   gtk_widget_class_install_style_property (widget_class,
  141.                                            g_param_spec_enum ("tab_icon_size",
  142.                                                               NULL,
  143.                                                               "Size for icons displayed in the tab",
  144.                                                               GTK_TYPE_ICON_SIZE,
  145.                                                               DEFAULT_TAB_ICON_SIZE,
  146.                                                               G_PARAM_READABLE));
  147. }
  148.  
  149. static void
  150. gimp_color_notebook_init (GimpColorNotebook *notebook)
  151. {
  152.   GType *selector_types;
  153.   gint   n_selector_types;
  154.   gint   i;
  155.  
  156.   notebook->notebook = gtk_notebook_new ();
  157.   gtk_box_pack_start (GTK_BOX (notebook), notebook->notebook, TRUE, TRUE, 0);
  158.   gtk_widget_show (notebook->notebook);
  159.  
  160.   gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook->notebook));
  161.  
  162.   g_signal_connect (notebook->notebook, "switch_page",
  163.                     G_CALLBACK (gimp_color_notebook_switch_page),
  164.                     notebook);
  165.  
  166.   selector_types = g_type_children (GIMP_TYPE_COLOR_SELECTOR,
  167.                     &n_selector_types);
  168.  
  169.   if (n_selector_types == 2)
  170.     {
  171.       gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook->notebook), FALSE);
  172.       gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook->notebook), FALSE);
  173.     }
  174.  
  175.   for (i = 0; i < n_selector_types; i++)
  176.     {
  177.       /*  skip ourselves  */
  178.       if (g_type_is_a (selector_types[i], GIMP_TYPE_COLOR_NOTEBOOK))
  179.         continue;
  180.  
  181.       /*  skip the "Scales" color selector  */
  182.       if (g_type_is_a (selector_types[i], GIMP_TYPE_COLOR_SCALES))
  183.         continue;
  184.  
  185.       gimp_color_notebook_add_page (notebook, selector_types[i]);
  186.     }
  187.  
  188.   g_free (selector_types);
  189. }
  190.  
  191. static void
  192. gimp_color_notebook_finalize (GObject *object)
  193. {
  194.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (object);
  195.  
  196.   if (notebook->selectors)
  197.     {
  198.       g_list_free (notebook->selectors);
  199.       notebook->selectors = NULL;
  200.     }
  201.  
  202.   G_OBJECT_CLASS (parent_class)->finalize (object);
  203. }
  204.  
  205. static void
  206. gimp_color_notebook_style_set (GtkWidget *widget,
  207.                                GtkStyle  *prev_style)
  208. {
  209.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (widget);
  210.   GList             *list;
  211.   gint               tab_border;
  212.   GtkIconSize        icon_size;
  213.  
  214.   if (GTK_WIDGET_CLASS (parent_class)->style_set)
  215.     GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);
  216.  
  217.   gtk_widget_style_get (widget,
  218.                         "tab_border",    &tab_border,
  219.                         "tab_icon_size", &icon_size,
  220.                         NULL);
  221.  
  222.   g_object_set (notebook->notebook,
  223.                 "tab_border", tab_border,
  224.                 NULL);
  225.  
  226.   for (list = notebook->selectors; list; list = g_list_next (list))
  227.     {
  228.       GimpColorSelectorClass *selector_class;
  229.       GtkWidget              *image;
  230.  
  231.       selector_class = GIMP_COLOR_SELECTOR_GET_CLASS (list->data);
  232.  
  233.       image = gtk_image_new_from_stock (selector_class->stock_id, icon_size);
  234.  
  235.       gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook->notebook),
  236.                                   GTK_WIDGET (list->data),
  237.                                   image);
  238.     }
  239. }
  240.  
  241. static void
  242. gimp_color_notebook_togg_visible (GimpColorSelector *selector,
  243.                                   gboolean           visible)
  244. {
  245.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (selector);
  246.   GList             *list;
  247.  
  248.   for (list = notebook->selectors; list; list = g_list_next (list))
  249.     {
  250.       GimpColorSelector *child = list->data;
  251.  
  252.       gimp_color_selector_set_toggles_visible (child, visible);
  253.     }
  254. }
  255.  
  256. static void
  257. gimp_color_notebook_togg_sensitive (GimpColorSelector *selector,
  258.                                     gboolean           sensitive)
  259. {
  260.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (selector);
  261.   GList             *list;
  262.  
  263.   for (list = notebook->selectors; list; list = g_list_next (list))
  264.     {
  265.       GimpColorSelector *child = list->data;
  266.  
  267.       gimp_color_selector_set_toggles_sensitive (child, sensitive);
  268.     }
  269. }
  270.  
  271. static void
  272. gimp_color_notebook_set_show_alpha (GimpColorSelector *selector,
  273.                                     gboolean           show_alpha)
  274. {
  275.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (selector);
  276.   GList             *list;
  277.  
  278.   for (list = notebook->selectors; list; list = g_list_next (list))
  279.     {
  280.       GimpColorSelector *child = list->data;
  281.  
  282.       gimp_color_selector_set_show_alpha (child, show_alpha);
  283.     }
  284. }
  285.  
  286. static void
  287. gimp_color_notebook_set_color (GimpColorSelector *selector,
  288.                                const GimpRGB     *rgb,
  289.                                const GimpHSV     *hsv)
  290. {
  291.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (selector);
  292.  
  293.   g_signal_handlers_block_by_func (notebook->cur_page,
  294.                                    gimp_color_notebook_color_changed,
  295.                                    notebook);
  296.  
  297.   gimp_color_selector_set_color (notebook->cur_page, rgb, hsv);
  298.  
  299.   g_signal_handlers_unblock_by_func (notebook->cur_page,
  300.                                      gimp_color_notebook_color_changed,
  301.                                      notebook);
  302. }
  303.  
  304. static void
  305. gimp_color_notebook_set_channel (GimpColorSelector        *selector,
  306.                                  GimpColorSelectorChannel  channel)
  307. {
  308.   GimpColorNotebook *notebook = GIMP_COLOR_NOTEBOOK (selector);
  309.  
  310.   g_signal_handlers_block_by_func (notebook->cur_page,
  311.                                    gimp_color_notebook_channel_changed,
  312.                                    notebook);
  313.  
  314.   gimp_color_selector_set_channel (notebook->cur_page, channel);
  315.  
  316.   g_signal_handlers_unblock_by_func (notebook->cur_page,
  317.                                      gimp_color_notebook_channel_changed,
  318.                                      notebook);
  319. }
  320.  
  321. static void
  322. gimp_color_notebook_switch_page (GtkNotebook       *gtk_notebook,
  323.                                  GtkNotebookPage   *page,
  324.                                  guint              page_num,
  325.                                  GimpColorNotebook *notebook)
  326. {
  327.   GimpColorSelector *selector = GIMP_COLOR_SELECTOR (notebook);
  328.   GtkWidget         *page_widget;
  329.  
  330.   page_widget = gtk_notebook_get_nth_page (gtk_notebook, page_num);
  331.  
  332.   notebook->cur_page = GIMP_COLOR_SELECTOR (page_widget);
  333.  
  334.   g_signal_handlers_block_by_func (notebook->cur_page,
  335.                                    gimp_color_notebook_color_changed,
  336.                                    notebook);
  337.   g_signal_handlers_block_by_func (notebook->cur_page,
  338.                                    gimp_color_notebook_channel_changed,
  339.                                    notebook);
  340.  
  341.   gimp_color_selector_set_color (notebook->cur_page,
  342.                                  &selector->rgb,
  343.                                  &selector->hsv);
  344.   gimp_color_selector_set_channel (notebook->cur_page,
  345.                                    selector->channel);
  346.  
  347.   g_signal_handlers_unblock_by_func (notebook->cur_page,
  348.                                      gimp_color_notebook_color_changed,
  349.                                      notebook);
  350.   g_signal_handlers_unblock_by_func (notebook->cur_page,
  351.                                      gimp_color_notebook_channel_changed,
  352.                                      notebook);
  353. }
  354.  
  355. static void
  356. gimp_color_notebook_color_changed (GimpColorSelector *page,
  357.                                    const GimpRGB     *rgb,
  358.                                    const GimpHSV     *hsv,
  359.                                    GimpColorNotebook *notebook)
  360. {
  361.   GimpColorSelector *selector = GIMP_COLOR_SELECTOR (notebook);
  362.  
  363.   selector->rgb = *rgb;
  364.   selector->hsv = *hsv;
  365.  
  366.   gimp_color_selector_color_changed (selector);
  367. }
  368.  
  369. static void
  370. gimp_color_notebook_channel_changed (GimpColorSelector        *page,
  371.                                      GimpColorSelectorChannel  channel,
  372.                                      GimpColorNotebook        *notebook)
  373. {
  374.   GimpColorSelector *selector = GIMP_COLOR_SELECTOR (notebook);
  375.  
  376.   selector->channel = channel;
  377.  
  378.   gimp_color_selector_channel_changed (selector);
  379. }
  380.  
  381. static GtkWidget *
  382. gimp_color_notebook_add_page (GimpColorNotebook *notebook,
  383.                               GType              page_type)
  384. {
  385.   GimpColorSelector      *selector = GIMP_COLOR_SELECTOR (notebook);
  386.   GimpColorSelectorClass *selector_class;
  387.   GtkWidget              *page;
  388.   GtkWidget              *menu_widget;
  389.   GtkWidget              *image;
  390.   GtkWidget              *label;
  391.  
  392.   page = gimp_color_selector_new (page_type,
  393.                                   &selector->rgb,
  394.                                   &selector->hsv,
  395.                                   selector->channel);
  396.  
  397.   if (! page)
  398.     return NULL;
  399.  
  400.   selector_class = GIMP_COLOR_SELECTOR_GET_CLASS (page);
  401.  
  402.   gimp_color_selector_set_show_alpha (GIMP_COLOR_SELECTOR (page),
  403.                                       GIMP_COLOR_SELECTOR (notebook)->show_alpha);
  404.  
  405.   menu_widget = gtk_hbox_new (FALSE, 4);
  406.  
  407.   image = gtk_image_new_from_stock (selector_class->stock_id,
  408.                                     GTK_ICON_SIZE_MENU);
  409.   gtk_box_pack_start (GTK_BOX (menu_widget), image, FALSE, FALSE, 0);
  410.   gtk_widget_show (image);
  411.  
  412.   label = gtk_label_new (gettext (selector_class->name));
  413.   gtk_box_pack_start (GTK_BOX (menu_widget), label, FALSE, FALSE, 0);
  414.   gtk_widget_show (label);
  415.  
  416.   image = gtk_image_new_from_stock (selector_class->stock_id,
  417.                                     DEFAULT_TAB_ICON_SIZE);
  418.  
  419.   gtk_notebook_append_page_menu (GTK_NOTEBOOK (notebook->notebook),
  420.                                  page, image, menu_widget);
  421.  
  422.   if (! notebook->cur_page)
  423.     notebook->cur_page = GIMP_COLOR_SELECTOR (page);
  424.  
  425.   notebook->selectors = g_list_append (notebook->selectors, page);
  426.  
  427.   gtk_widget_show (page);
  428.  
  429.   g_signal_connect (page, "color_changed",
  430.                     G_CALLBACK (gimp_color_notebook_color_changed),
  431.                     notebook);
  432.   g_signal_connect (page, "channel_changed",
  433.                     G_CALLBACK (gimp_color_notebook_channel_changed),
  434.                     notebook);
  435.  
  436.   return page;
  437. }
  438.  
  439.  
  440. /**
  441.  * gimp_color_notebook_set_has_page:
  442.  * @notebook:  A #GimpColorNotebook widget.
  443.  * @page_type: The #GType of the notebook page to add or remove.
  444.  * @has_page:  Whether the page should be added or removed.
  445.  *
  446.  * This function adds and removed pages to / from a #GimpColorNotebook.
  447.  * The @page_type passed must be a #GimpColorSelector subtype.
  448.  *
  449.  * Return value: The new page widget, if @has_page was #TRUE, or #NULL
  450.  *               if @has_page was #FALSE.
  451.  **/
  452. GtkWidget *
  453. gimp_color_notebook_set_has_page (GimpColorNotebook *notebook,
  454.                                   GType              page_type,
  455.                                   gboolean           has_page)
  456. {
  457.   GimpColorSelector *selector;
  458.   GList             *list;
  459.  
  460.   g_return_val_if_fail (GIMP_IS_COLOR_NOTEBOOK (notebook), NULL);
  461.   g_return_val_if_fail (g_type_is_a (page_type, GIMP_TYPE_COLOR_SELECTOR),
  462.                         NULL);
  463.   g_return_val_if_fail (! g_type_is_a (page_type, GIMP_TYPE_COLOR_NOTEBOOK),
  464.                         NULL);
  465.  
  466.   selector = GIMP_COLOR_SELECTOR (notebook);
  467.  
  468.   for (list = notebook->selectors; list; list = g_list_next (list))
  469.     {
  470.       GimpColorSelector *page = list->data;
  471.  
  472.       if (G_TYPE_FROM_INSTANCE (page) == page_type)
  473.         {
  474.           if (has_page)
  475.             return GTK_WIDGET (page);
  476.  
  477.           gtk_container_remove (GTK_CONTAINER (notebook->notebook),
  478.                                 GTK_WIDGET (page));
  479.           notebook->selectors = g_list_remove (notebook->selectors, page);
  480.  
  481.           if (! notebook->selectors)
  482.             notebook->cur_page = NULL;
  483.  
  484.           return NULL;
  485.         }
  486.     }
  487.  
  488.   if (! has_page)
  489.     return NULL;
  490.  
  491.   return gimp_color_notebook_add_page (notebook, page_type);
  492. }
  493.