color_panel.c 8.36 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 <math.h>
#include <stdlib.h>
#include "appenv.h"
#include "color_panel.h"
22
#include "color_notebook.h"
Elliot Lee's avatar
Elliot Lee committed
23
#include "colormaps.h"
24
#include "gimpdnd.h"
Elliot Lee's avatar
Elliot Lee committed
25

26 27 28
#define EVENT_MASK  GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | \
                    GDK_BUTTON_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK | \
                    GDK_LEAVE_NOTIFY_MASK
Elliot Lee's avatar
Elliot Lee committed
29 30 31 32 33 34

typedef struct _ColorPanelPrivate ColorPanelPrivate;

struct _ColorPanelPrivate
{
  GtkWidget *drawing_area;
35
  GdkGC     *gc;
Elliot Lee's avatar
Elliot Lee committed
36

37 38 39 40
  gboolean   button_down;

  ColorNotebookP  color_notebook;
  gboolean        color_notebook_active;
Elliot Lee's avatar
Elliot Lee committed
41 42
};

43 44 45 46 47 48
/*  local function prototypes  */
static void color_panel_draw            (ColorPanel *);
static gint color_panel_events          (GtkWidget *, GdkEvent *);
static void color_panel_select_callback (gint, gint, gint,
					 ColorNotebookState, void *);

49 50 51 52
static void color_panel_drag_color (GtkWidget *,
				    guchar *, guchar *, guchar *, gpointer);
static void color_panel_drop_color (GtkWidget *,
				    guchar, guchar, guchar, gpointer);
53 54

/*  dnd stuff  */
55 56 57 58 59 60
static GtkTargetEntry color_panel_target_table[] =
{
  GIMP_TARGET_COLOR
};
static guint n_color_panel_targets = (sizeof (color_panel_target_table) /
				      sizeof (color_panel_target_table[0]));
Elliot Lee's avatar
Elliot Lee committed
61

62 63
/*  public functions  */

Elliot Lee's avatar
Elliot Lee committed
64
ColorPanel *
65 66 67
color_panel_new (guchar *initial,
		 gint    width,
		 gint    height)
Elliot Lee's avatar
Elliot Lee committed
68 69 70
{
  ColorPanel *color_panel;
  ColorPanelPrivate *private;
71
  gint i;
Elliot Lee's avatar
Elliot Lee committed
72 73

  private = g_new (ColorPanelPrivate, 1);
74 75 76 77 78 79
  private->color_notebook        = NULL;
  private->color_notebook_active = FALSE;
  private->gc                    = NULL;
  private->button_down           = FALSE;

  color_panel = g_new (ColorPanel, 1);
Elliot Lee's avatar
Elliot Lee committed
80 81 82 83 84 85 86
  color_panel->private_part = private;

  /*  set the initial color  */
  for (i = 0; i < 3; i++)
    color_panel->color[i] = (initial) ? initial[i] : 0;

  color_panel->color_panel_widget = gtk_frame_new (NULL);
87 88
  gtk_frame_set_shadow_type (GTK_FRAME (color_panel->color_panel_widget),
			     GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
89 90 91

  /*  drawing area  */
  private->drawing_area = gtk_drawing_area_new ();
92 93
  gtk_drawing_area_size (GTK_DRAWING_AREA (private->drawing_area),
			 width, height);
Elliot Lee's avatar
Elliot Lee committed
94 95 96 97 98
  gtk_widget_set_events (private->drawing_area, EVENT_MASK);
  gtk_signal_connect (GTK_OBJECT (private->drawing_area), "event",
		      (GtkSignalFunc) color_panel_events,
		      color_panel);
  gtk_object_set_user_data (GTK_OBJECT (private->drawing_area), color_panel);
99 100
  gtk_container_add (GTK_CONTAINER (color_panel->color_panel_widget),
		     private->drawing_area);
Elliot Lee's avatar
Elliot Lee committed
101 102
  gtk_widget_show (private->drawing_area);

103
  /*  dnd stuff  */
104
  gtk_drag_source_set (private->drawing_area,
105
                       GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
106 107 108
                       color_panel_target_table, n_color_panel_targets,
                       GDK_ACTION_COPY | GDK_ACTION_MOVE);
  gimp_dnd_color_source_set (private->drawing_area,
109
			     color_panel_drag_color, color_panel);
110

111 112 113 114 115 116
  gtk_drag_dest_set (private->drawing_area,
		     GTK_DEST_DEFAULT_HIGHLIGHT |
		     GTK_DEST_DEFAULT_MOTION |
		     GTK_DEST_DEFAULT_DROP,
		     color_panel_target_table, n_color_panel_targets,
		     GDK_ACTION_COPY);
117
  gimp_dnd_color_dest_set (private->drawing_area,
118
			   color_panel_drop_color, color_panel);
119

Elliot Lee's avatar
Elliot Lee committed
120 121 122 123 124 125 126 127 128 129
  return color_panel;
}

void
color_panel_free (ColorPanel *color_panel)
{
  ColorPanelPrivate *private;

  private = (ColorPanelPrivate *) color_panel->private_part;
  
130 131
  /* make sure we hide and free color_notebook */
  if (private->color_notebook)
Elliot Lee's avatar
Elliot Lee committed
132
    {
133 134
      color_notebook_hide (private->color_notebook);
      color_notebook_free (private->color_notebook);
Elliot Lee's avatar
Elliot Lee committed
135 136 137 138 139 140 141 142
    }
  
  if (private->gc)
    gdk_gc_destroy (private->gc);
  g_free (color_panel->private_part);
  g_free (color_panel);
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
void
color_panel_set_color (ColorPanel *color_panel,
		       guchar     *col)
{
  ColorPanelPrivate *private = color_panel->private_part;

  color_panel->color[0] = col[0];
  color_panel->color[1] = col[1];
  color_panel->color[2] = col[2];

  if (private->color_notebook_active)
    color_notebook_set_color (private->color_notebook,
			      col[0], col[1], col[2], TRUE);

  if (private->gc)
    color_panel_draw (color_panel);
}

161 162
/*  private functions  */

Elliot Lee's avatar
Elliot Lee committed
163 164 165 166 167 168 169 170 171 172
static void
color_panel_draw (ColorPanel *color_panel)
{
  GtkWidget *widget;
  ColorPanelPrivate *private;
  GdkColor fg;

  private = (ColorPanelPrivate *) color_panel->private_part;
  widget = private->drawing_area;

173 174 175
  fg.pixel = get_color (color_panel->color[0],
		        color_panel->color[1],
		        color_panel->color[2]);
Elliot Lee's avatar
Elliot Lee committed
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 201 202 203 204 205

  gdk_gc_set_foreground (private->gc, &fg);
  gdk_draw_rectangle (widget->window, private->gc, 1, 0, 0,
		      widget->allocation.width, widget->allocation.height);
}

static gint
color_panel_events (GtkWidget *widget,
		    GdkEvent  *event)
{
  GdkEventButton *bevent;
  ColorPanel *color_panel;
  ColorPanelPrivate *private;

  color_panel = (ColorPanel *) gtk_object_get_user_data (GTK_OBJECT (widget));
  private = (ColorPanelPrivate *) color_panel->private_part;

  switch (event->type)
    {
    case GDK_EXPOSE:
      if (!private->gc)
	private->gc = gdk_gc_new (widget->window);

      color_panel_draw (color_panel);
      break;

    case GDK_BUTTON_PRESS:
      bevent = (GdkEventButton *) event;

      if (bevent->button == 1)
206 207 208 209 210 211 212 213
	private->button_down = TRUE;
      break;

    case GDK_BUTTON_RELEASE:
      bevent = (GdkEventButton *) event;

      if (bevent->button == 1 &&
	  private->button_down)
Elliot Lee's avatar
Elliot Lee committed
214
	{
215
	  if (! private->color_notebook)
Elliot Lee's avatar
Elliot Lee committed
216
	    {
217 218 219 220 221 222 223 224
	      private->color_notebook =
		color_notebook_new (color_panel->color[0],
				    color_panel->color[1],
				    color_panel->color[2],
				    color_panel_select_callback,
				    color_panel,
				    FALSE);
	      private->color_notebook_active = TRUE;
Elliot Lee's avatar
Elliot Lee committed
225 226 227
	    }
	  else
	    {
228
	      if (! private->color_notebook_active)
229 230 231 232
		{
		  color_notebook_show (private->color_notebook);
		  private->color_notebook_active = TRUE;
		}
233 234 235 236
	      color_notebook_set_color (private->color_notebook,
					color_panel->color[0],
					color_panel->color[1],
					color_panel->color[2], 1);
Elliot Lee's avatar
Elliot Lee committed
237
	    }
238
	  private->button_down = FALSE;
Elliot Lee's avatar
Elliot Lee committed
239 240 241
	}
      break;

242 243 244 245
    case GDK_LEAVE_NOTIFY:
      private->button_down = FALSE;
      break;

Elliot Lee's avatar
Elliot Lee committed
246 247 248 249 250 251 252 253
    default:
      break;
    }

  return FALSE;
}

static void
254 255 256 257 258
color_panel_select_callback (gint                r,
			     gint                g,
			     gint                b,
			     ColorNotebookState  state,
			     void               *client_data)
Elliot Lee's avatar
Elliot Lee committed
259 260 261 262 263 264 265
{
  ColorPanel *color_panel;
  ColorPanelPrivate *private;

  color_panel = (ColorPanel *) client_data;
  private = (ColorPanelPrivate *) color_panel->private_part;

266
  if (private->color_notebook)
Elliot Lee's avatar
Elliot Lee committed
267
    {
268 269 270 271 272 273 274 275
      switch (state)
	{
	case COLOR_NOTEBOOK_UPDATE:
	  break;
	case COLOR_NOTEBOOK_OK:
	  color_panel->color[0] = r;
	  color_panel->color[1] = g;
	  color_panel->color[2] = b;
Elliot Lee's avatar
Elliot Lee committed
276
	
277 278 279 280 281 282 283 284 285 286
	  color_panel_draw (color_panel);
	  /* Fallthrough */
	case COLOR_NOTEBOOK_CANCEL:
	  color_notebook_hide (private->color_notebook);
	  private->color_notebook_active = FALSE;
	}
    }
}

static void
287 288 289 290 291
color_panel_drag_color (GtkWidget *widget,
			guchar    *r,
			guchar    *g,
			guchar    *b,
			gpointer   data)
292 293 294
{
  ColorPanel *color_panel = data;

295 296 297
  *r = color_panel->color[0];
  *g = color_panel->color[1];
  *b = color_panel->color[2];
298 299 300
}

static void
301 302 303 304 305
color_panel_drop_color (GtkWidget *widget,
			guchar     r,
			guchar     g,
			guchar     b,
			gpointer   data)
306 307 308 309
{
  ColorPanel *color_panel = data;
  ColorPanelPrivate *private = (ColorPanelPrivate *) color_panel->private_part;

310 311 312
  color_panel->color[0] = r;
  color_panel->color[1] = g;
  color_panel->color[2] = b;
313 314

  if (private->color_notebook_active)
315
    color_notebook_set_color (private->color_notebook, r, g, b, TRUE);
316 317 318

  color_panel_draw (color_panel);
}