gimpcontroller.c 7.84 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpcontroller.c
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24
#include <gegl.h>
25 26 27
#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
28
#include "libgimpconfig/gimpconfig.h"
29 30 31 32 33

#include "gimpwidgetstypes.h"

#include "gimpwidgetsmarshal.h"

34 35
#define GIMP_ENABLE_CONTROLLER_UNDER_CONSTRUCTION
#include "gimpcontroller.h"
36
#include "gimpicons.h"
37

38 39 40 41 42 43 44 45 46 47

/**
 * SECTION: gimpcontroller
 * @title: GimpController
 * @short_description: Pluggable GIMP input controller modules.
 *
 * An abstract interface for implementing arbitrary input controllers.
 **/


48 49 50
enum
{
  PROP_0,
51 52
  PROP_NAME,
  PROP_STATE
53 54 55 56 57 58 59 60 61
};

enum
{
  EVENT,
  LAST_SIGNAL
};


62 63 64 65 66 67 68 69 70
static void   gimp_controller_finalize     (GObject      *object);
static void   gimp_controller_set_property (GObject      *object,
                                            guint         property_id,
                                            const GValue *value,
                                            GParamSpec   *pspec);
static void   gimp_controller_get_property (GObject      *object,
                                            guint         property_id,
                                            GValue       *value,
                                            GParamSpec   *pspec);
71 72


73
G_DEFINE_TYPE_WITH_CODE (GimpController, gimp_controller, G_TYPE_OBJECT,
74
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG, NULL))
75

76
#define parent_class gimp_controller_parent_class
77

78
static guint controller_signals[LAST_SIGNAL] = { 0 };
79 80 81 82 83 84 85


static void
gimp_controller_class_init (GimpControllerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

Sven Neumann's avatar
Sven Neumann committed
86
  object_class->finalize     = gimp_controller_finalize;
87 88 89
  object_class->set_property = gimp_controller_set_property;
  object_class->get_property = gimp_controller_get_property;

90 91 92
  klass->name                = "Unnamed";
  klass->help_domain         = NULL;
  klass->help_id             = NULL;
93
  klass->icon_name           = GIMP_ICON_CONTROLLER;
94 95 96 97 98

  klass->get_n_events        = NULL;
  klass->get_event_name      = NULL;
  klass->event               = NULL;

99
  g_object_class_install_property (object_class, PROP_NAME,
100 101 102
                                   g_param_spec_string ("name",
                                                        "Name",
                                                        "The controller's name",
103
                                                        "Unnamed Controller",
104
                                                        GIMP_PARAM_READWRITE |
105
                                                        G_PARAM_CONSTRUCT));
106

107
  g_object_class_install_property (object_class, PROP_STATE,
108 109 110
                                   g_param_spec_string ("state",
                                                        "State",
                                                        "The controller's state, as human-readable string",
111
                                                        "Unknown",
112
                                                        GIMP_PARAM_READWRITE |
113 114
                                                        G_PARAM_CONSTRUCT));

115 116 117 118 119
  controller_signals[EVENT] =
    g_signal_new ("event",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GimpControllerClass, event),
120
                  g_signal_accumulator_true_handled, NULL,
121 122 123
                  _gimp_widgets_marshal_BOOLEAN__POINTER,
                  G_TYPE_BOOLEAN, 1,
                  G_TYPE_POINTER);
124
}
125

126 127 128
static void
gimp_controller_init (GimpController *controller)
{
129 130
}

Sven Neumann's avatar
Sven Neumann committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
static void
gimp_controller_finalize (GObject *object)
{
  GimpController *controller = GIMP_CONTROLLER (object);

  if (controller->name)
    {
      g_free (controller->name);
      controller->name = NULL;
    }

  if (controller->state)
    {
      g_free (controller->state);
      controller->state = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

151 152 153 154 155 156 157 158 159 160
static void
gimp_controller_set_property (GObject      *object,
                              guint         property_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GimpController *controller = GIMP_CONTROLLER (object);

  switch (property_id)
    {
161 162 163 164 165
    case PROP_NAME:
      if (controller->name)
        g_free (controller->name);
      controller->name = g_value_dup_string (value);
      break;
166 167 168 169 170
    case PROP_STATE:
      if (controller->state)
        g_free (controller->state);
      controller->state = g_value_dup_string (value);
      break;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_controller_get_property (GObject    *object,
                              guint       property_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GimpController *controller = GIMP_CONTROLLER (object);

  switch (property_id)
    {
187 188 189
    case PROP_NAME:
      g_value_set_string (value, controller->name);
      break;
190 191 192
    case PROP_STATE:
      g_value_set_string (value, controller->state);
      break;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

GimpController *
gimp_controller_new (GType controller_type)
{
  GimpController *controller;

  g_return_val_if_fail (g_type_is_a (controller_type, GIMP_TYPE_CONTROLLER),
                        NULL);

  controller = g_object_new (controller_type, NULL);

  return controller;
}

gint
gimp_controller_get_n_events (GimpController *controller)
{
  g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), 0);

  if (GIMP_CONTROLLER_GET_CLASS (controller)->get_n_events)
    return GIMP_CONTROLLER_GET_CLASS (controller)->get_n_events (controller);

  return 0;
}

const gchar *
gimp_controller_get_event_name (GimpController *controller,
                                gint            event_id)
{
227 228
  const gchar *name = NULL;

229 230 231
  g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), NULL);

  if (GIMP_CONTROLLER_GET_CLASS (controller)->get_event_name)
232
    name = GIMP_CONTROLLER_GET_CLASS (controller)->get_event_name (controller,
233 234
                                                                   event_id);

235 236 237 238
  if (! name)
    name = "<invalid event id>";

  return name;
239 240
}

241 242 243 244
const gchar *
gimp_controller_get_event_blurb (GimpController *controller,
                                 gint            event_id)
{
245 246
  const gchar *blurb = NULL;

247 248 249
  g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), NULL);

  if (GIMP_CONTROLLER_GET_CLASS (controller)->get_event_blurb)
250 251 252 253 254
    blurb =  GIMP_CONTROLLER_GET_CLASS (controller)->get_event_blurb (controller,
                                                                      event_id);

  if (! blurb)
    blurb = "<invalid event id>";
255

256
  return blurb;
257 258
}

259 260 261 262 263 264 265 266 267
gboolean
gimp_controller_event (GimpController            *controller,
                       const GimpControllerEvent *event)
{
  gboolean retval = FALSE;

  g_return_val_if_fail (GIMP_IS_CONTROLLER (controller), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

268 269
  g_signal_emit (controller, controller_signals[EVENT], 0,
                 event, &retval);
270 271 272

  return retval;
}