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 / gimpcontroller.c < prev    next >
C/C++ Source or Header  |  2004-11-23  |  8KB  |  273 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpcontroller.c
  5.  * Copyright (C) 2004 Michael Natterer <mitch@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 <gtk/gtk.h>
  26.  
  27. #include "libgimpcolor/gimpcolor.h"
  28.  
  29. #include "gimpwidgetstypes.h"
  30.  
  31. #include "gimpwidgetsmarshal.h"
  32.  
  33. #define GIMP_ENABLE_CONTROLLER_UNDER_CONSTRUCTION
  34. #include "gimpcontroller.h"
  35.  
  36. enum
  37. {
  38.   PROP_0,
  39.   PROP_NAME,
  40.   PROP_STATE
  41. };
  42.  
  43. enum
  44. {
  45.   EVENT,
  46.   LAST_SIGNAL
  47. };
  48.  
  49.  
  50. static void   gimp_controller_class_init   (GimpControllerClass *klass);
  51. static void   gimp_controller_set_property (GObject             *object,
  52.                                             guint                property_id,
  53.                                             const GValue        *value,
  54.                                             GParamSpec          *pspec);
  55. static void   gimp_controller_get_property (GObject             *object,
  56.                                             guint                property_id,
  57.                                             GValue              *value,
  58.                                             GParamSpec          *pspec);
  59.  
  60.  
  61. static GObjectClass *parent_class = NULL;
  62.  
  63. static guint  controller_signals[LAST_SIGNAL] = { 0 };
  64.  
  65.  
  66. GType
  67. gimp_controller_get_type (void)
  68. {
  69.   static GType controller_type = 0;
  70.  
  71.   if (! controller_type)
  72.     {
  73.       static const GTypeInfo controller_info =
  74.       {
  75.         sizeof (GimpControllerClass),
  76.         (GBaseInitFunc) NULL,
  77.         (GBaseFinalizeFunc) NULL,
  78.         (GClassInitFunc) gimp_controller_class_init,
  79.         NULL,           /* class_finalize */
  80.         NULL,           /* class_data     */
  81.         sizeof (GimpController),
  82.         0,              /* n_preallocs    */
  83.         NULL            /* instance_init  */
  84.       };
  85.  
  86.       controller_type = g_type_register_static (G_TYPE_OBJECT,
  87.                                                 "GimpController",
  88.                                                 &controller_info, 0);
  89.     }
  90.  
  91.   return controller_type;
  92. }
  93.  
  94. gboolean
  95. gimp_controller_boolean_handled_accumulator (GSignalInvocationHint *ihint,
  96.                                              GValue                *return_accu,
  97.                                              const GValue          *handler_return,
  98.                                              gpointer               dummy)
  99. {
  100.   gboolean continue_emission;
  101.   gboolean signal_handled;
  102.  
  103.   signal_handled = g_value_get_boolean (handler_return);
  104.   g_value_set_boolean (return_accu, signal_handled);
  105.   continue_emission = ! signal_handled;
  106.  
  107.   return continue_emission;
  108. }
  109.  
  110. static void
  111. gimp_controller_class_init (GimpControllerClass *klass)
  112. {
  113.   GObjectClass *object_class = G_OBJECT_CLASS (klass);
  114.  
  115.   parent_class = g_type_class_peek_parent (klass);
  116.  
  117.   object_class->set_property = gimp_controller_set_property;
  118.   object_class->get_property = gimp_controller_get_property;
  119.  
  120.   g_object_class_install_property (object_class, PROP_NAME,
  121.                                    g_param_spec_string ("name", NULL, NULL,
  122.                                                         "Unnamed Controller",
  123.                                                         G_PARAM_READWRITE |
  124.                                                         G_PARAM_CONSTRUCT));
  125.  
  126.   g_object_class_install_property (object_class, PROP_STATE,
  127.                                    g_param_spec_string ("state", NULL, NULL,
  128.                                                         "Unknown",
  129.                                                         G_PARAM_READWRITE |
  130.                                                         G_PARAM_CONSTRUCT));
  131.  
  132.   controller_signals[EVENT] =
  133.     g_signal_new ("event",
  134.                   G_TYPE_FROM_CLASS (klass),
  135.                   G_SIGNAL_RUN_LAST,
  136.                   G_STRUCT_OFFSET (GimpControllerClass, event),
  137.                   gimp_controller_boolean_handled_accumulator, NULL,
  138.                   _gimp_widgets_marshal_BOOLEAN__POINTER,
  139.                   G_TYPE_BOOLEAN, 1,
  140.                   G_TYPE_POINTER);
  141.  
  142.   klass->name           = "Unnamed";
  143.   klass->help_domain    = NULL;
  144.   klass->help_id        = NULL;
  145.  
  146.   klass->get_n_events   = NULL;
  147.   klass->get_event_name = NULL;
  148.   klass->event          = NULL;
  149. }
  150.  
  151. static void
  152. gimp_controller_set_property (GObject      *object,
  153.                               guint         property_id,
  154.                               const GValue *value,
  155.                               GParamSpec   *pspec)
  156. {
  157.   GimpController *controller = GIMP_CONTROLLER (object);
  158.  
  159.   switch (property_id)
  160.     {
  161.     case PROP_NAME:
  162.       if (controller->name)
  163.         g_free (controller->name);
  164.       controller->name = g_value_dup_string (value);
  165.       break;
  166.     case PROP_STATE:
  167.       if (controller->state)
  168.         g_free (controller->state);
  169.       controller->state = g_value_dup_string (value);
  170.       break;
  171.     default:
  172.       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  173.       break;
  174.     }
  175. }
  176.  
  177. static void
  178. gimp_controller_get_property (GObject    *object,
  179.                               guint       property_id,
  180.                               GValue     *value,
  181.                               GParamSpec *pspec)
  182. {
  183.   GimpController *controller = GIMP_CONTROLLER (object);
  184.  
  185.   switch (property_id)
  186.     {
  187.     case PROP_NAME:
  188.       g_value_set_string (value, controller->name);
  189.       break;
  190.     case PROP_STATE:
  191.       g_value_set_string (value, controller->state);
  192.       break;
  193.     default:
  194.       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  195.       break;
  196.     }
  197. }
  198.  
  199. GimpController *
  200. gimp_controller_new (GType controller_type)
  201. {
  202.   GimpController *controller;
  203.  
  204.   g_return_val_if_fail (g_type_is_a (controller_type, GIMP_TYPE_CONTROLLER),
  205.                         NULL);
  206.  
  207.   controller = g_object_new (controller_type, NULL);
  208.  
  209.   return controller;
  210. }
  211.  
  212. gint
  213. gimp_controller_get_n_events (GimpController *controller)
  214. {
  215.   g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), 0);
  216.  
  217.   if (GIMP_CONTROLLER_GET_CLASS (controller)->get_n_events)
  218.     return GIMP_CONTROLLER_GET_CLASS (controller)->get_n_events (controller);
  219.  
  220.   return 0;
  221. }
  222.  
  223. const gchar *
  224. gimp_controller_get_event_name (GimpController *controller,
  225.                                 gint            event_id)
  226. {
  227.   const gchar *name = NULL;
  228.  
  229.   g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), NULL);
  230.  
  231.   if (GIMP_CONTROLLER_GET_CLASS (controller)->get_event_name)
  232.     name = GIMP_CONTROLLER_GET_CLASS (controller)->get_event_name (controller,
  233.                                                                    event_id);
  234.  
  235.   if (! name)
  236.     name = "<invalid event id>";
  237.  
  238.   return name;
  239. }
  240.  
  241. const gchar *
  242. gimp_controller_get_event_blurb (GimpController *controller,
  243.                                  gint            event_id)
  244. {
  245.   const gchar *blurb = NULL;
  246.  
  247.   g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), NULL);
  248.  
  249.   if (GIMP_CONTROLLER_GET_CLASS (controller)->get_event_blurb)
  250.     blurb =  GIMP_CONTROLLER_GET_CLASS (controller)->get_event_blurb (controller,
  251.                                                                       event_id);
  252.  
  253.   if (! blurb)
  254.     blurb = "<invalid event id>";
  255.  
  256.   return blurb;
  257. }
  258.  
  259. gboolean
  260. gimp_controller_event (GimpController            *controller,
  261.                        const GimpControllerEvent *event)
  262. {
  263.   gboolean retval = FALSE;
  264.  
  265.   g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), FALSE);
  266.   g_return_val_if_fail (event != NULL, FALSE);
  267.  
  268.   g_signal_emit (controller, controller_signals[EVENT], 0,
  269.                  event, &retval);
  270.  
  271.   return retval;
  272. }
  273.