gimpbrushmenu.c 11.7 KB
Newer Older
1 2 3 4 5
/* LIBGIMP - The GIMP Library                                                   
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball                
 * Copyright (C) 1998 Andy Thomas                
 *
 * This library is free software; you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
6
 * modify it under the terms of the GNU Lesser General Public
7 8 9 10 11 12 13 14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.             
 *                                                                              
 * 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            
 * Library General Public License for more details.
 *
Marc Lehmann's avatar
Marc Lehmann committed
15
 * You should have received a copy of the GNU Lesser General Public
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 45 46 47 48 49 50 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 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 227 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 272 273 274 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 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */                                                                             
#include <stdio.h>
#include <string.h>

#include "gimp.h"
#include "gimpui.h"


/* Idea is to have a function to call that returns a widget that 
 * completely controls the selection of a brush.
 * you get a widget returned that you can use in a table say.
 * In:- Initial brush name. Null means use current selection.
 *      pointer to func to call when brush changes (GRunBrushCallback).
 * Returned:- Pointer to a widget that you can use in UI.
 * 
 * Widget simply made up of a preview widget (20x20) containing the brush mask
 * and a button that can be clicked on to change the brush.
 */


#define BSEL_DATA_KEY "__bsel_data"
#define CELL_SIZE 20
#define BRUSH_EVENT_MASK  GDK_EXPOSURE_MASK | \
                          GDK_BUTTON_PRESS_MASK | \
			  GDK_BUTTON_RELEASE_MASK | \
                          GDK_BUTTON1_MOTION_MASK 

struct __brushes_sel {
  gchar * dname;
  GRunBrushCallback cback;
  GtkWidget *brush_preview;
  GtkWidget *device_brushpopup; 
  GtkWidget *device_brushpreview;
  GtkWidget *button;
  GtkWidget *top_hbox;
  gchar *brush_name; /* Local copy */
  gdouble opacity;
  gint spacing;
  gint paint_mode;
  gint width;
  gint height;
  gchar *mask_data; /* local copy */
  void *brush_popup_pnt; /* POinter use to control the popup */
  gpointer udata;
};

typedef struct __brushes_sel BSelect, * BSelectP;

static void
brush_popup_open (int          x,
		    int          y,
		    BSelectP   bsel)
{
  gint x_org, y_org;
  gint scr_w, scr_h;
  gchar *src, *buf;
  unsigned char *b,*s;

  /* make sure the popup exists and is not visible */
  if (bsel->device_brushpopup == NULL)
    {
      GtkWidget *frame;

      bsel->device_brushpopup = gtk_window_new (GTK_WINDOW_POPUP);
      gtk_window_set_policy (GTK_WINDOW (bsel->device_brushpopup), FALSE, FALSE, TRUE);

      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
      gtk_container_add (GTK_CONTAINER (bsel->device_brushpopup), frame);
      gtk_widget_show (frame);
      bsel->device_brushpreview = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
      gtk_container_add (GTK_CONTAINER (frame), bsel->device_brushpreview);
      gtk_widget_show (bsel->device_brushpreview);
    }
  else
    {
      gtk_widget_hide (bsel->device_brushpopup);
    }

  /* decide where to put the popup */
  gdk_window_get_origin (bsel->brush_preview->window, &x_org, &y_org);
  scr_w = gdk_screen_width ();
  scr_h = gdk_screen_height ();
  x = x_org + x - (bsel->width >> 1);
  y = y_org + y - (bsel->height >> 1);
  x = (x < 0) ? 0 : x;
  y = (y < 0) ? 0 : y;
  x = (x + bsel->width > scr_w) ? scr_w - bsel->width : x;
  y = (y + bsel->height > scr_h) ? scr_h - bsel->height : y;
  gtk_preview_size (GTK_PREVIEW (bsel->device_brushpreview), bsel->width, bsel->height);

  gtk_widget_popup (bsel->device_brushpopup, x, y);
  
  /*  Draw the brush  */
  buf = g_new (gchar, bsel->width);
  
  memset(buf, 255, sizeof(bsel->width));
  
/*   for (i = 0; i < CELL_SIZE; i++) */
/*     gtk_preview_draw_row (GTK_PREVIEW(bsel->device_brushpreview), (guchar *)buf, 0, i, CELL_SIZE); */
  
  src = bsel->mask_data;
  
  for (y = 0; y < bsel->height; y++)
    {
      int j;
      s = src;
      b = buf;
      for (j = 0; j < bsel->width ; j++)
	*b++ = 255 - *s++;

      gtk_preview_draw_row (GTK_PREVIEW (bsel->device_brushpreview), (guchar *)buf, 0, y, bsel->width);
      src += bsel->width;
    }
  g_free(buf);
  
  /*  Draw the brush preview  */
  gtk_widget_draw (bsel->device_brushpreview, NULL);
}

static void
brush_popup_close (BSelectP   bsel)
{
  if (bsel->device_brushpopup != NULL)
    gtk_widget_hide (bsel->device_brushpopup);
}

static gint
brush_preview_events (GtkWidget    *widget,
			GdkEvent     *event,
			gpointer     udata)
{
  GdkEventButton *bevent;
  BSelectP bsel = (BSelectP)udata;

  if(bsel->mask_data)
    {
      switch (event->type)
	{
	case GDK_EXPOSE:
	  break;
	  
	case GDK_BUTTON_PRESS:
	  bevent = (GdkEventButton *) event;
	  
	  if (bevent->button == 1)
	    {
	      brush_popup_open (bevent->x, bevent->y, bsel);
	    }
	  break;
	  
	case GDK_BUTTON_RELEASE:
	  bevent = (GdkEventButton *) event;
	  
	  if (bevent->button == 1)
	    {
	      /*  Ungrab the pointer  */
	      gdk_pointer_ungrab (bevent->time);
	      
	      /*  Close the device preview popup window  */
	      brush_popup_close (bsel);
	    }
	  break;
	case GDK_DELETE:
	  break;
	  
	default:
	  break;
	}
    }

  return FALSE;
}

static void
brush_pre_update(GtkWidget *brush_preview,
		   gint brush_width,
		   gint brush_height,
		   gchar *mask_data)
{
  gint y,i;
  gchar *src, *buf;
  unsigned char *b,*s;
  int offset_x, offset_y;
  int yend;
  int ystart;
  gint width,height;
  /*  Draw the brush  */
  buf = g_new (gchar, CELL_SIZE); 

 /* Limit to cell size */
  width = (brush_width > CELL_SIZE) ? CELL_SIZE: brush_width;
  height = (brush_height > CELL_SIZE) ? CELL_SIZE: brush_height;

  /* Set buffer to white */  
  memset(buf, 255, CELL_SIZE);
  for (i = 0; i < CELL_SIZE; i++)
    gtk_preview_draw_row (GTK_PREVIEW(brush_preview), (guchar *)buf, 0, i, CELL_SIZE);
  
  offset_x = ((CELL_SIZE - width) >> 1);
  offset_y = ((CELL_SIZE - height) >> 1);

  ystart = CLAMP (offset_y, 0, CELL_SIZE);
  yend = CLAMP (offset_y + height, 0, CELL_SIZE);

  src = mask_data;

  for (y = ystart; y < yend; y++)
    {
      int j;
      s = src;
      b = buf;
      for (j = 0; j < width ; j++)
	*b++ = 255 - *s++;

      gtk_preview_draw_row (GTK_PREVIEW (brush_preview), (guchar *)buf, offset_x, y, width);
      src += brush_width;
    }
  g_free(buf);

  /*  Draw the brush preview  */
  gtk_widget_draw (brush_preview, NULL);
}

static void
brush_select_invoker(gchar    *name,
		     gdouble  opacity,
		     gint     spacing,
		     gint     paint_mode,
		     gint     width,
		     gint     height,
		     gchar *  mask_data,
		     gint     closing,
		     gpointer udata)
{
  gint mask_d_sz;
  BSelectP bsel = (BSelectP)udata;
  if(bsel->mask_data != NULL)
    g_free(bsel->mask_data);
  bsel->width = width;
  bsel->height = height;
  mask_d_sz = width*height;
  bsel->mask_data = g_malloc(mask_d_sz);
  g_memmove(bsel->mask_data,mask_data,mask_d_sz); 
  brush_pre_update(bsel->brush_preview,bsel->width,bsel->height,bsel->mask_data);
  bsel->opacity = opacity;
  bsel->spacing = spacing;
  bsel->paint_mode = paint_mode;
  if(bsel->cback != NULL)
    (bsel->cback)(name,opacity,spacing,paint_mode,width,height,mask_data,closing,bsel->udata);
  if(closing)
    {
      gtk_widget_set_sensitive(bsel->button,TRUE);
      bsel->brush_popup_pnt = NULL;
    }
}


static void
brush_select_callback (GtkWidget *widget,
		       gpointer   data)
{
  BSelectP bsel = (BSelectP)data;
  gtk_widget_set_sensitive(bsel->button,FALSE);
  bsel->brush_popup_pnt = 
    gimp_interactive_selection_brush((bsel->dname)?bsel->dname:"Brush Plugin Selection",
				       bsel->brush_name,
				       bsel->opacity,
				       bsel->spacing,
				       bsel->paint_mode,
				       brush_select_invoker,bsel);
}

GtkWidget * 
gimp_brush_select_widget(gchar *dname,
			 gchar *ibrush, 
			 gdouble opacity,
			 gint spacing,
			 gint paint_mode,
			 GRunBrushCallback cback,
			 gpointer udata)
{
  GtkWidget * frame;
  GtkWidget * hbox;
  GtkWidget * brush;
  GtkWidget * button;
  gint width,height;
  gint init_spacing,init_paint_mode;
  gdouble init_opacity;
  gchar *mask_data;
  gchar *brush_name;

  BSelectP bsel = g_new(BSelect, sizeof(BSelect));

  hbox = gtk_hbox_new (FALSE, 3);
  gtk_widget_show(hbox);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME(frame), GTK_SHADOW_OUT);
  gtk_widget_show(frame);

  brush = gtk_preview_new (GTK_PREVIEW_GRAYSCALE);
  gtk_preview_size (GTK_PREVIEW (brush), CELL_SIZE, CELL_SIZE); 
  gtk_widget_show(brush);
  gtk_container_add (GTK_CONTAINER (frame),brush); 

  gtk_widget_set_events (brush, BRUSH_EVENT_MASK);

  gtk_signal_connect (GTK_OBJECT (brush), "event",
		      (GtkSignalFunc) brush_preview_events,
		      (gpointer)bsel);

  bsel->cback = cback;
  bsel->udata = udata;
  bsel->mask_data = NULL;
  bsel->device_brushpopup = bsel->device_brushpreview = NULL;
  bsel->brush_preview = brush;
  bsel->brush_name = ibrush;
  bsel->dname = dname;
  bsel->brush_popup_pnt = NULL;

  /* Do initial brush setup */
  brush_name = gimp_brushes_get_brush_data(ibrush,
					   &init_opacity,
					   &init_spacing,
					   &init_paint_mode,
					   &width,
					   &height,
					   &mask_data);
  if(brush_name)
    {
      brush_pre_update(bsel->brush_preview,width,height,mask_data);
      bsel->mask_data = mask_data;
      bsel->brush_name = brush_name;
      bsel->width = width;
      bsel->height = height;
      if(opacity != -1)
	bsel->opacity = opacity;
      else
	bsel->opacity = init_opacity;
      if(spacing != -1)
	bsel->spacing = spacing;
      else
	bsel->spacing = init_spacing;
      if(paint_mode != -1)
	bsel->paint_mode = paint_mode;
      else
	bsel->paint_mode = init_paint_mode;
    }

  gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);

  button = gtk_button_new_with_label ("... ");
  gtk_container_add (GTK_CONTAINER (hbox), button); 
  gtk_widget_show(button);

  bsel->button = button;
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
                      (GtkSignalFunc) brush_select_callback,
                      (gpointer)bsel);

  gtk_object_set_data(GTK_OBJECT(hbox),BSEL_DATA_KEY,(gpointer)bsel);

  return hbox;
}


gint
gimp_brush_select_widget_close_popup(GtkWidget *w)
{
  gint ret_val = FALSE;

  BSelectP bsel = (BSelectP)gtk_object_get_data(GTK_OBJECT(w),BSEL_DATA_KEY);
  if(bsel && bsel->brush_popup_pnt)
    {
      ret_val = gimp_brush_close_popup(bsel->brush_popup_pnt);
      bsel->brush_popup_pnt = NULL;
    }

  return ret_val;
}

gint 
gimp_brush_select_widget_set_popup(GtkWidget *w,
				   gchar *bname,
				   gdouble opacity,
				   gint spacing,
				   gint paint_mode)
{
  gint ret_val = FALSE;
  gint width,height;
  gint init_spacing,init_paint_mode;
  gdouble init_opacity;
  gchar *mask_data;
  gchar *brush_name;

  BSelectP bsel = (BSelectP)gtk_object_get_data(GTK_OBJECT(w),BSEL_DATA_KEY);

  if(bsel)
    {
      brush_name = gimp_brushes_get_brush_data(bname,
					       &init_opacity,
					       &init_spacing,
					       &init_paint_mode,
					       &width,&height,&mask_data);

      if(opacity == -1.0)
	opacity = init_opacity;

      if(spacing == -1)
	spacing = init_spacing;

      if(paint_mode == -1)
	paint_mode = init_paint_mode;
  
      brush_select_invoker(bname,opacity,spacing,paint_mode,width,height,mask_data,0,bsel);

      if(bsel->brush_popup_pnt)
	{
	  if(gimp_brush_set_popup(bsel->brush_popup_pnt,bname,opacity,spacing,paint_mode))
	    {
	      ret_val = TRUE;
	    }
	}
    }
  return ret_val;
}