gimpdisplayshell-layer-select.c 12.3 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18
19
20
21
#include "config.h"

#include <gdk/gdkkeysyms.h>

Elliot Lee's avatar
Elliot Lee committed
22
23
24
25
26
27
#include "appenv.h"
#include "gdisplay.h"
#include "gimprc.h"
#include "layer_select.h"
#include "layers_dialogP.h"

28
29
#include "libgimp/gimpintl.h"

Elliot Lee's avatar
Elliot Lee committed
30
31
32
33
34

#define PREVIEW_EVENT_MASK GDK_EXPOSURE_MASK | GDK_ENTER_NOTIFY_MASK

typedef struct _LayerSelect LayerSelect;

35
36
struct _LayerSelect
{
Elliot Lee's avatar
Elliot Lee committed
37
38
39
40
41
42
  GtkWidget *shell;
  GtkWidget *layer_preview;
  GtkWidget *label;
  GdkPixmap *layer_pixmap;
  GtkWidget *preview;

43
44
45
46
47
48
  GImage    *gimage;
  Layer     *current_layer;
  gboolean   dirty;
  gint       image_width;
  gint       image_height;
  gdouble    ratio;
Elliot Lee's avatar
Elliot Lee committed
49
50
51
};

/*  layer widget function prototypes  */
52
53
54
55
static void layer_select_advance    (LayerSelect *, gint);
static void layer_select_forward    (LayerSelect *);
static void layer_select_backward   (LayerSelect *);
static void layer_select_end        (LayerSelect *, guint32);
Elliot Lee's avatar
Elliot Lee committed
56
static void layer_select_set_gimage (LayerSelect *, GImage *);
57
58
59
60
static void layer_select_set_layer  (LayerSelect *);
static gint layer_select_events     (GtkWidget *, GdkEvent *);
static gint preview_events          (GtkWidget *, GdkEvent *);
static void preview_redraw          (LayerSelect *);
Elliot Lee's avatar
Elliot Lee committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/*
 *  Local variables
 */
LayerSelect *layer_select = NULL;


/**********************/
/*  Public functions  */
/**********************/


void
layer_select_init (GImage  *gimage,
75
		   gint     dir,
Elliot Lee's avatar
Elliot Lee committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		   guint32  time)
{
  GtkWidget *frame1;
  GtkWidget *frame2;
  GtkWidget *hbox;
  GtkWidget *alignment;

  if (!layer_select)
    {
      layer_select = g_malloc (sizeof (LayerSelect));
      layer_select->layer_pixmap = NULL;
      layer_select->layer_preview = NULL;
      layer_select->preview = NULL;
      layer_select->image_width = layer_select->image_height = 0;
      layer_select_set_gimage (layer_select, gimage);
      layer_select_advance (layer_select, dir);

      if (preview_size)
	{
	  layer_select->preview = gtk_preview_new (GTK_PREVIEW_COLOR);
96
97
	  gtk_preview_size (GTK_PREVIEW (layer_select->preview), 
			    preview_size, preview_size);
Elliot Lee's avatar
Elliot Lee committed
98
99
100
101
	}

      /*  The shell and main vbox  */
      layer_select->shell = gtk_window_new (GTK_WINDOW_POPUP);
102
103
104
105
106
107
      gtk_window_set_wmclass (GTK_WINDOW (layer_select->shell), 
			      "layer_select", "Gimp");
      gtk_window_set_title (GTK_WINDOW (layer_select->shell), 
			    _("Layer Select"));
      gtk_window_set_position (GTK_WINDOW (layer_select->shell), 
			       GTK_WIN_POS_MOUSE);
Elliot Lee's avatar
Elliot Lee committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
      gtk_signal_connect (GTK_OBJECT (layer_select->shell), "event",
			  (GtkSignalFunc) layer_select_events,
			  layer_select);
      gtk_widget_set_events (layer_select->shell, (GDK_KEY_PRESS_MASK |
						   GDK_KEY_RELEASE_MASK |
						   GDK_BUTTON_PRESS_MASK));

      frame1 = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame1), GTK_SHADOW_OUT);
      gtk_container_add (GTK_CONTAINER (layer_select->shell), frame1);
      frame2 = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_IN);
      gtk_container_add (GTK_CONTAINER (frame1), frame2);

      hbox = gtk_hbox_new (FALSE, 1);
      gtk_container_add (GTK_CONTAINER (frame2), hbox);

      /*  The preview  */
      alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
      gtk_box_pack_start (GTK_BOX (hbox), alignment, FALSE, FALSE, 0);
      gtk_widget_show (alignment);

      layer_select->layer_preview = gtk_drawing_area_new ();
      gtk_drawing_area_size (GTK_DRAWING_AREA (layer_select->layer_preview),
132
133
			     layer_select->image_width, 
			     layer_select->image_height);
Elliot Lee's avatar
Elliot Lee committed
134
135
136
      gtk_widget_set_events (layer_select->layer_preview, PREVIEW_EVENT_MASK);
      gtk_signal_connect (GTK_OBJECT (layer_select->layer_preview), "event",
			  (GtkSignalFunc) preview_events, layer_select);
137
138
139
140
      gtk_object_set_user_data (GTK_OBJECT (layer_select->layer_preview), 
				layer_select);
      gtk_container_add (GTK_CONTAINER (alignment), 
			 layer_select->layer_preview);
Elliot Lee's avatar
Elliot Lee committed
141
142
143
144
      gtk_widget_show (layer_select->layer_preview);
      gtk_widget_show (alignment);

      /*  the layer name label */
145
      layer_select->label = gtk_label_new (_("Layer"));
146
147
      gtk_box_pack_start (GTK_BOX (hbox), 
			  layer_select->label, FALSE, FALSE, 2);
Elliot Lee's avatar
Elliot Lee committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
      gtk_widget_show (layer_select->label);

      gtk_widget_show (hbox);
      gtk_widget_show (frame1);
      gtk_widget_show (frame2);
      gtk_widget_show (layer_select->shell);
    }
  else
    {
      layer_select_set_gimage (layer_select, gimage);
      layer_select_advance (layer_select, dir);

      if (! GTK_WIDGET_VISIBLE (layer_select->shell))
	gtk_widget_show (layer_select->shell);
      else
	gtk_widget_draw (layer_select->layer_preview, NULL);
    }

  gdk_key_repeat_disable ();
  gdk_keyboard_grab (layer_select->shell->window, FALSE, time);
}

void
171
layer_select_update_preview_size (void)
Elliot Lee's avatar
Elliot Lee committed
172
173
174
{
  if (layer_select != NULL)
    {
175
176
      gtk_preview_size (GTK_PREVIEW (layer_select->preview), 
			preview_size, preview_size);
Elliot Lee's avatar
Elliot Lee committed
177
178
179
180
181
182
183
184
185
186
187
188
      if (GTK_WIDGET_VISIBLE (layer_select->shell))
        gtk_widget_draw (layer_select->layer_preview, NULL);
    }  
}


/***********************/
/*  Private functions  */
/***********************/

static void
layer_select_advance (LayerSelect *layer_select,
189
		      gint         dir)
Elliot Lee's avatar
Elliot Lee committed
190
{
191
192
193
  gint    index;
  gint    length;
  gint    count;
194
195
  GSList *list;
  GSList *nth;
196
  Layer  *layer;
Elliot Lee's avatar
Elliot Lee committed
197
198
199
200
201
202
203
204
205
206
207
208

  index = 0;

  /*  If there is a floating selection, allow no advancement  */
  if (gimage_floating_sel (layer_select->gimage))
    return;

  count = 0;
  list = layer_select->gimage->layer_stack;
  while (list)
    {
      layer = (Layer *) list->data;
209
      if (layer == layer_select->current_layer)
Elliot Lee's avatar
Elliot Lee committed
210
211
	index = count;
      count++;
212
      list = g_slist_next (list);
Elliot Lee's avatar
Elliot Lee committed
213
214
    }

215
  length = g_slist_length (layer_select->gimage->layer_stack);
Elliot Lee's avatar
Elliot Lee committed
216
217
218
219
220
221

  if (dir == 1)
    index = (index == length - 1) ? 0 : (index + 1);
  else
    index = (index == 0) ? (length - 1) : (index - 1);

222
  nth = g_slist_nth (layer_select->gimage->layer_stack, index);
Elliot Lee's avatar
Elliot Lee committed
223
224
225
226

  if (nth)
    {
      layer = (Layer *) nth->data;
227
      layer_select->current_layer = layer;
Elliot Lee's avatar
Elliot Lee committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    }
}


static void
layer_select_forward (LayerSelect *layer_select)
{
  layer_select_advance (layer_select, 1);
  layer_select->dirty = TRUE;
  gtk_widget_draw (layer_select->layer_preview, NULL);
}


static void
layer_select_backward (LayerSelect *layer_select)
{
  layer_select_advance (layer_select, -1);
  layer_select->dirty = TRUE;
  gtk_widget_draw (layer_select->layer_preview, NULL);
}


static void
layer_select_end (LayerSelect *layer_select,
		  guint32      time)
{
  gdk_key_repeat_restore ();
  gdk_keyboard_ungrab (time);

  gtk_widget_hide (layer_select->shell);

  /*  only reset the active layer if a new layer was specified  */
  if (layer_select->current_layer != layer_select->gimage->active_layer)
    {
      gimage_set_active_layer (layer_select->gimage, layer_select->current_layer);
      gdisplays_flush ();
    }
}


static void
layer_select_set_gimage (LayerSelect *layer_select,
			 GImage      *gimage)
{
272
273
  gint image_width;
  gint image_height;
Elliot Lee's avatar
Elliot Lee committed
274
275
276
277
278
279
280

  layer_select->gimage = gimage;
  layer_select->current_layer = gimage->active_layer;
  layer_select->dirty = TRUE;

  /*  Get the image width and height variables, based on the gimage  */
  if (gimage->width > gimage->height)
281
282
    layer_select->ratio = 
      MIN (1.0, (gdouble) preview_size / (gdouble) gimage->width);
Elliot Lee's avatar
Elliot Lee committed
283
  else
284
285
    layer_select->ratio = 
      MIN (1.0, (gdouble) preview_size / (gdouble) gimage->height);
Elliot Lee's avatar
Elliot Lee committed
286

287
288
  image_width = (gint) (layer_select->ratio * gimage->width);
  image_height = (gint) (layer_select->ratio * gimage->height);
Elliot Lee's avatar
Elliot Lee committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302

  if (layer_select->image_width != image_width ||
      layer_select->image_height != image_height)
    {
      layer_select->image_width = image_width;
      layer_select->image_height = image_height;

      if (layer_select->layer_preview)
	gtk_widget_set_usize (layer_select->layer_preview,
			      layer_select->image_width,
			      layer_select->image_height);

      if (layer_select->layer_pixmap)
	{
303
	  gdk_pixmap_unref (layer_select->layer_pixmap);
Elliot Lee's avatar
Elliot Lee committed
304
305
306
307
308
309
310
311
312
313
314
	  layer_select->layer_pixmap = NULL;
	}
    }
}


static void
layer_select_set_layer (LayerSelect *layer_select)
{
  Layer *layer;

315
  if (! (layer =  (layer_select->current_layer)))
Elliot Lee's avatar
Elliot Lee committed
316
317
318
    return;

  /*  Set the layer label  */
319
320
  gtk_label_set_text (GTK_LABEL (layer_select->label),
		      drawable_get_name (GIMP_DRAWABLE (layer)));
Elliot Lee's avatar
Elliot Lee committed
321
322
323
324
325
326
327
}


static gint
layer_select_events (GtkWidget *widget,
		     GdkEvent  *event)
{
328
  GdkEventKey    *kevent;
Elliot Lee's avatar
Elliot Lee committed
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
369
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  GdkEventButton *bevent;

  switch (event->type)
    {
    case GDK_BUTTON_PRESS:
      bevent = (GdkEventButton *) event;
      layer_select_end (layer_select, bevent->time);
      break;

    case GDK_KEY_PRESS:
      kevent = (GdkEventKey *) event;

      switch (kevent->keyval)
	{
	case GDK_Tab:
	  if (kevent->state & GDK_MOD1_MASK)
	    layer_select_forward (layer_select);
	  else if (kevent->state & GDK_CONTROL_MASK)
	    layer_select_backward (layer_select);
	  break;
	}
      return TRUE;
      break;

    case GDK_KEY_RELEASE:
      kevent = (GdkEventKey *) event;

      switch (kevent->keyval)
	{
	case GDK_Alt_L: case GDK_Alt_R:
	  kevent->state &= ~GDK_MOD1_MASK;
	  break;
	case GDK_Control_L: case GDK_Control_R:
	  kevent->state &= ~GDK_CONTROL_MASK;
	  break;
	}

      if (! (kevent->state & (GDK_MOD1_MASK | GDK_CONTROL_MASK)))
	layer_select_end (layer_select, kevent->time);

      return TRUE;
      break;

    default:
      break;
    }

  return FALSE;
}


static gint
preview_events (GtkWidget *widget,
		GdkEvent  *event)
{
  switch (event->type)
    {
    case GDK_EXPOSE:
      if (layer_select->dirty)
	{
	  /*  If a preview exists, draw it  */
	  if (preview_size)
	    preview_redraw (layer_select);

	  /*  Change the layer name label  */
	  layer_select_set_layer (layer_select);

	  layer_select->dirty = FALSE;
	}

      if (preview_size)
	gdk_draw_pixmap (layer_select->layer_preview->window,
			 layer_select->layer_preview->style->black_gc,
			 layer_select->layer_pixmap,
			 0, 0, 0, 0,
			 layer_select->image_width,
			 layer_select->image_height);
      break;

    default:
      break;
    }

  return FALSE;
}


static void
preview_redraw (LayerSelect *layer_select)
{
419
420
  Layer   *layer;
  TempBuf *preview_buf;
421
422
  gint     w, h;
  gint     offx, offy;
Elliot Lee's avatar
Elliot Lee committed
423

424
  if (! (layer =  (layer_select->current_layer)))
Elliot Lee's avatar
Elliot Lee committed
425
426
427
    return;

  if (! layer_select->layer_pixmap)
428
429
430
431
432
    layer_select->layer_pixmap = 
      gdk_pixmap_new (layer_select->layer_preview->window,
		      layer_select->image_width,
		      layer_select->image_height,
		      -1);
Elliot Lee's avatar
Elliot Lee committed
433
434

  if (layer_is_floating_sel (layer))
435
436
    render_fs_preview (layer_select->layer_preview, 
		       layer_select->layer_pixmap);
Elliot Lee's avatar
Elliot Lee committed
437
438
  else
    {
439
440
441
      gint off_x;
      gint off_y;

442
      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
443

Elliot Lee's avatar
Elliot Lee committed
444
      /*  determine width and height  */
445
446
447
448
449
450
      w = (gint) (layer_select->ratio * 
		  drawable_width (GIMP_DRAWABLE(layer)));
      h = (gint) (layer_select->ratio * 
		  drawable_height (GIMP_DRAWABLE(layer)));
      offx = (gint) (layer_select->ratio * off_x);
      offy = (gint) (layer_select->ratio * off_y);
Elliot Lee's avatar
Elliot Lee committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

      preview_buf = layer_preview (layer, w, h);
      preview_buf->x = offx;
      preview_buf->y = offy;

      render_preview (preview_buf,
		      layer_select->preview,
		      layer_select->image_width,
		      layer_select->image_height,
		      -1);

      /*  Set the layer pixmap  */
      gtk_preview_put (GTK_PREVIEW (layer_select->preview),
		       layer_select->layer_pixmap,
		       layer_select->layer_preview->style->black_gc,
466
467
		       0, 0, 0, 0,
		       layer_select->image_width, layer_select->image_height);
Elliot Lee's avatar
Elliot Lee committed
468

469
470
      /*  make sure the image has been transfered completely 
       *  to the pixmap before we use it again...
Elliot Lee's avatar
Elliot Lee committed
471
472
473
474
       */
      gdk_flush ();
    }
}