gimpdevicestatus.c 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
 *
 * gimpdevicestatus.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimp.h"
#include "core/gimpbrush.h"
#include "core/gimpcontainer.h"
#include "core/gimpdatafactory.h"
#include "core/gimpgradient.h"
#include "core/gimppattern.h"
#include "core/gimptoolinfo.h"

#include "gimpdnd.h"
#include "gimpdeviceinfo.h"
#include "gimpdevices.h"
#include "gimpdevicestatus.h"
#include "gimpdialogfactory.h"
#include "gimppropwidgets.h"
45
#include "gimpview.h"
46 47 48 49

#include "gimp-intl.h"


50
#define CELL_SIZE 20 /* The size of the view cells */
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116


struct _GimpDeviceStatusEntry
{
  GdkDevice *device;

  GtkWidget *separator;
  GtkWidget *label;
  GtkWidget *arrow;
  GtkWidget *tool;
  GtkWidget *foreground;
  GtkWidget *background;
  GtkWidget *brush;
  GtkWidget *pattern;
  GtkWidget *gradient;
};


static void gimp_device_status_class_init      (GimpDeviceStatusClass *klass);
static void gimp_device_status_init            (GimpDeviceStatus      *editor);

static void gimp_device_status_destroy         (GtkObject             *object);

static void gimp_device_status_update_entry    (GimpDeviceInfo        *device_info,
                                                GimpDeviceStatusEntry *entry);
static void gimp_device_status_save_clicked    (GtkWidget             *button,
                                                GimpDeviceStatus      *status);
static void gimp_device_status_preview_clicked (GtkWidget             *widget,
                                                GdkModifierType        state,
                                                const gchar           *identifier);


static GimpEditorClass *parent_class = NULL;


GType
gimp_device_status_get_type (void)
{
  static GType view_type = 0;

  if (! view_type)
    {
      static const GTypeInfo view_info =
      {
        sizeof (GimpDeviceStatusClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gimp_device_status_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpDeviceStatus),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_device_status_init,
      };

      view_type = g_type_register_static (GIMP_TYPE_EDITOR,
                                          "GimpDeviceStatus",
                                          &view_info, 0);
    }

  return view_type;
}

static void
gimp_device_status_class_init (GimpDeviceStatusClass *klass)
{
117
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
118 119 120 121 122 123 124 125 126

  parent_class = g_type_class_peek_parent (klass);

  object_class->destroy = gimp_device_status_destroy;
}

static void
gimp_device_status_init (GimpDeviceStatus *status)
{
127 128 129 130 131
  GdkDisplay *display;
  GList      *list;
  gint        i;

  display = gtk_widget_get_display (GTK_WIDGET (status));
132 133 134

  status->gimp           = NULL;
  status->current_device = NULL;
135
  status->num_devices    = g_list_length (gdk_display_list_devices (display));
136 137 138 139
  status->entries        = g_new0 (GimpDeviceStatusEntry,
                                   status->num_devices);

  status->table = gtk_table_new (status->num_devices * 3, 7, FALSE);
140 141
  gtk_container_set_border_width (GTK_CONTAINER (status->table), 6);
  gtk_table_set_col_spacings (GTK_TABLE (status->table), 6);
142
  gtk_container_add (GTK_CONTAINER (status), status->table);
143 144
  gtk_widget_show (status->table);

145
  for (list = gdk_display_list_devices (display), i = 0;
146
       list;
147
       list = list->next, i++)
148 149 150 151 152 153
    {
      GimpDeviceInfo        *device_info;
      GimpContext           *context;
      GimpDeviceStatusEntry *entry = &status->entries[i];
      gint                   row    = i * 3;
      gchar                 *markup;
154
      GClosure              *closure;
155 156 157 158 159 160

      entry->device = GDK_DEVICE (list->data);

      device_info = gimp_device_info_get_by_device (entry->device);
      context     = GIMP_CONTEXT (device_info);

161 162 163 164
      closure = g_cclosure_new (G_CALLBACK (gimp_device_status_update_entry),
                                entry, NULL);
      g_object_watch_closure (G_OBJECT (status), closure);
      g_signal_connect_closure (device_info, "changed", closure, FALSE);
165 166 167 168 169

      /*  the separator  */

      entry->separator = gtk_hbox_new (FALSE, 0);
      gtk_table_attach (GTK_TABLE (status->table), entry->separator,
170 171
                        0, 7, row, row + 1,
                        GTK_FILL, GTK_FILL, 0, 2);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

      row++;

      /*  the device name  */

      entry->label = gtk_label_new (NULL);

      markup = g_strdup_printf ("<b>%s</b>", GIMP_OBJECT (device_info)->name);
      gtk_label_set_markup (GTK_LABEL (entry->label), markup);
      g_free (markup);

      gtk_widget_set_size_request (entry->label, -1, CELL_SIZE);
      gtk_misc_set_alignment (GTK_MISC (entry->label), 0.0, 0.5);
      gtk_table_attach (GTK_TABLE (status->table), entry->label,
                        1, 7, row, row + 1,
                        GTK_FILL, GTK_FILL, 0, 2);

      entry->arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_OUT);
      gtk_widget_set_size_request (entry->arrow, CELL_SIZE, CELL_SIZE);
      gtk_table_attach (GTK_TABLE (status->table), entry->arrow,
                        0, 1, row, row + 1,
                        GTK_FILL, GTK_FILL, 0, 0);

      row++;

      /*  the tool  */

      entry->tool = gimp_prop_preview_new (G_OBJECT (context),
                                            "tool", CELL_SIZE);
201
      GIMP_VIEW (entry->tool)->clickable = TRUE;
202 203 204 205 206 207
      gtk_table_attach (GTK_TABLE (status->table), entry->tool,
                        1, 2, row, row + 1,
                        0, 0, 0, 0);

      g_signal_connect (entry->tool, "clicked",
                        G_CALLBACK (gimp_device_status_preview_clicked),
208
                        "gimp-tool-list|gimp-tool-grid");
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

      /*  the foreground color  */

      entry->foreground = gimp_prop_color_area_new (G_OBJECT (context),
                                                     "foreground",
                                                     CELL_SIZE, CELL_SIZE,
                                                     GIMP_COLOR_AREA_FLAT);
      gtk_widget_add_events (entry->foreground,
                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
      gtk_table_attach (GTK_TABLE (status->table), entry->foreground,
                        2, 3, row, row + 1,
                        0, 0, 0, 0);

      /*  the background color  */

      entry->background = gimp_prop_color_area_new (G_OBJECT (context),
                                                     "background",
                                                     CELL_SIZE, CELL_SIZE,
                                                     GIMP_COLOR_AREA_FLAT);
      gtk_widget_add_events (entry->background,
                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
      gtk_table_attach (GTK_TABLE (status->table), entry->background,
                        3, 4, row, row + 1,
                        0, 0, 0, 0);

      /*  the brush  */

      entry->brush = gimp_prop_preview_new (G_OBJECT (context),
                                             "brush", CELL_SIZE);
238 239
      GIMP_VIEW (entry->brush)->clickable  = TRUE;
      GIMP_VIEW (entry->brush)->show_popup = TRUE;
240 241 242 243 244 245
      gtk_table_attach (GTK_TABLE (status->table), entry->brush,
                        4, 5, row, row + 1,
                        0, 0, 0, 0);

      g_signal_connect (entry->brush, "clicked",
                        G_CALLBACK (gimp_device_status_preview_clicked),
246
                        "gimp-brush-grid|gimp-brush-list");
247 248 249 250 251

      /*  the pattern  */

      entry->pattern = gimp_prop_preview_new (G_OBJECT (context),
                                               "pattern", CELL_SIZE);
252 253
      GIMP_VIEW (entry->pattern)->clickable  = TRUE;
      GIMP_VIEW (entry->pattern)->show_popup = TRUE;
254 255 256 257 258 259
      gtk_table_attach (GTK_TABLE (status->table), entry->pattern,
                        5, 6, row, row + 1,
                        0, 0, 0, 0);

      g_signal_connect (entry->pattern, "clicked",
                        G_CALLBACK (gimp_device_status_preview_clicked),
260
                        "gimp-pattern-grid|gimp-pattern-list");
261 262 263 264 265

      /*  the gradient  */

      entry->gradient = gimp_prop_preview_new (G_OBJECT (context),
                                                "gradient", 2 * CELL_SIZE);
266 267
      GIMP_VIEW (entry->gradient)->clickable  = TRUE;
      GIMP_VIEW (entry->gradient)->show_popup = TRUE;
268 269 270 271 272 273
      gtk_table_attach (GTK_TABLE (status->table), entry->gradient,
                        6, 7, row, row + 1,
                        0, 0, 0, 0);

      g_signal_connect (entry->gradient, "clicked",
                        G_CALLBACK (gimp_device_status_preview_clicked),
274
                        "gimp-gradient-list|gimp-gradient-grid");
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

      gimp_device_status_update_entry (device_info, entry);
    }

  status->save_button =
    gimp_editor_add_button (GIMP_EDITOR (status), GTK_STOCK_SAVE,
                            _("Save device status"), NULL,
                            G_CALLBACK (gimp_device_status_save_clicked),
                            NULL,
                            status);
}

static void
gimp_device_status_destroy (GtkObject *object)
{
  GimpDeviceStatus *status = GIMP_DEVICE_STATUS (object);

  if (status->entries)
    {
      gint i;

      for (i = 0; i < status->num_devices; i++)
        {
          GimpDeviceStatusEntry *entry = &status->entries[i];

          g_signal_handlers_disconnect_by_func (entry->device,
                                                gimp_device_status_update_entry,
                                                entry);
        }

      g_free (status->entries);
      status->entries     = NULL;
      status->num_devices = 0;
    }

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

GtkWidget *
gimp_device_status_new (Gimp *gimp)
{
  GimpDeviceStatus *status;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  status = g_object_new (GIMP_TYPE_DEVICE_STATUS, NULL);

  status->gimp = gimp;

  gimp_device_status_update (status);

  return GTK_WIDGET (status);
}

void
gimp_device_status_update (GimpDeviceStatus *status)
{
  gint i;

  g_return_if_fail (GIMP_IS_DEVICE_STATUS (status));

  status->current_device = gimp_devices_get_current (status->gimp);

  for (i = 0; i < status->num_devices; i++)
    {
      GimpDeviceStatusEntry *entry = &status->entries[i];

      if (entry->device == status->current_device)
        gtk_widget_show (entry->arrow);
      else
        gtk_widget_hide (entry->arrow);
    }
}


/*  private functions  */

static void
gimp_device_status_update_entry (GimpDeviceInfo        *device_info,
                                 GimpDeviceStatusEntry *entry)
{
  if (device_info->device->mode == GDK_MODE_DISABLED)
    {
      gtk_widget_hide (entry->separator);
      gtk_widget_hide (entry->label);
      gtk_widget_hide (entry->tool);
      gtk_widget_hide (entry->foreground);
      gtk_widget_hide (entry->background);
      gtk_widget_hide (entry->brush);
      gtk_widget_hide (entry->pattern);
      gtk_widget_hide (entry->gradient);
    }
  else
    {
369
      GimpContext *context = GIMP_CONTEXT (device_info);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
      GimpRGB      color;
      guchar       r, g, b;
      gchar        buf[64];

      gtk_widget_show (entry->separator);
      gtk_widget_show (entry->label);
      gtk_widget_show (entry->tool);
      gtk_widget_show (entry->foreground);
      gtk_widget_show (entry->background);
      gtk_widget_show (entry->brush);
      gtk_widget_show (entry->pattern);
      gtk_widget_show (entry->gradient);

      gimp_context_get_foreground (context, &color);
      gimp_rgb_get_uchar (&color, &r, &g, &b);
      g_snprintf (buf, sizeof (buf), _("Foreground: %d, %d, %d"), r, g, b);
      gimp_help_set_help_data (entry->foreground, buf, NULL);

      gimp_context_get_background (context, &color);
      gimp_rgb_get_uchar (&color, &r, &g, &b);
      g_snprintf (buf, sizeof (buf), _("Background: %d, %d, %d"), r, g, b);
      gimp_help_set_help_data (entry->background, buf, NULL);
    }
}

static void
gimp_device_status_save_clicked (GtkWidget        *button,
                                 GimpDeviceStatus *status)
{
399
  gimp_devices_save (status->gimp, TRUE);
400 401 402 403 404 405 406 407 408 409 410
}

static void
gimp_device_status_preview_clicked (GtkWidget       *widget,
                                    GdkModifierType  state,
                                    const gchar     *identifier)
{
  GimpDialogFactory *dialog_factory;

  dialog_factory = gimp_dialog_factory_from_name ("dock");

411 412 413
  gimp_dialog_factory_dialog_raise (dialog_factory,
                                    gtk_widget_get_screen (widget),
                                    identifier, -1);
414
}