gfig.c 260 KB
Newer Older
Manish Singh's avatar
Manish Singh committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This is a plug-in for the GIMP.
 *
 * Generates images containing vector type drawings.
 *
 * Copyright (C) 1997 Andy Thomas  alt@picnic.demon.co.uk
 *
 * 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
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Manish Singh's avatar
Manish Singh committed
23 24 25 26 27 28 29 30 31
 * 
 * Some of this code was taken from the Whirl plug-in
 * which was copyrighted by Federico Mena Quintero (as below).
 * 
 * Whirl plug-in --- distort an image into a whirlpool
 * Copyright (C) 1997 Federico Mena Quintero           
 *
 */

32
/* Change log:
Manish Singh's avatar
Manish Singh committed
33 34 35 36 37 38 39 40 41 42 43
 * 0.9 First public release. 
 * 0.95 Second release.
 * 
 * 0.96 Added patch from  Rob Saunders that introduces a isometric type grid
 *      Removed use of gtk_idle* stuff on position update. Not required.
 *
 * 1.0  Fixed to work with the new gtk+-0.99.4 (tooltips stuff has changed).  
 * 
 * 1.1  Fixed crashes when objects not fully defined
 * 
 * 1.2  More bug fixes and prevent gtk warning when creating new figs
44 45 46
 * 
 * 1.3  Portability fixes and fixed bug reports 257 and 258 from and 81 & 101 & 133
 *      http://www.wilberworks.com/bugs.cgi
Manish Singh's avatar
Manish Singh committed
47 48
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
49 50
#include "config.h"

51 52
#include <glib.h>

Manish Singh's avatar
Manish Singh committed
53 54
#include <stdio.h>
#include <stdlib.h>
55
#include <sys/types.h>
Manish Singh's avatar
Manish Singh committed
56
#include <sys/stat.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
57
#ifdef HAVE_UNISTD_H
Manish Singh's avatar
Manish Singh committed
58
#include <unistd.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
59
#endif
Manish Singh's avatar
Manish Singh committed
60
#include <string.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
61
#ifdef HAVE_DIRENT_H
Manish Singh's avatar
Manish Singh committed
62
#include <dirent.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
63
#endif
Manish Singh's avatar
Manish Singh committed
64
#include <ctype.h>
Manish Singh's avatar
Manish Singh committed
65
#include <gtk/gtk.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
66

67
#ifdef G_OS_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
68 69 70 71 72 73 74 75 76 77 78 79
#  include <io.h>
#  ifndef W_OK
#    define W_OK 2
#  endif
#  ifndef S_ISDIR
#    define S_ISDIR(m) ((m) & _S_IFDIR)
#  endif
#  ifndef S_ISREG
#    define S_ISREG(m) ((m) & _S_IFREG)
#  endif
#endif

80
#if defined (GTK_CHECK_VERSION) && GTK_CHECK_VERSION (1,3,0)
81
#define gdk_root_parent (*gdk_parent_root)
82 83 84 85 86 87 88 89
#endif

#ifdef G_OS_WIN32
extern __declspec(dllimport) void *gdk_root_parent;
#else
extern void * gdk_root_parent;
#endif

90 91
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
Manish Singh's avatar
Manish Singh committed
92

93 94 95
#include "libgimp/stdplugins-intl.h"

#include "pix_data.h"
Manish Singh's avatar
Manish Singh committed
96 97 98

/***** Magic numbers *****/

99
#define PREVIEW_SIZE 400
Manish Singh's avatar
Manish Singh committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#define SCALE_WIDTH  120

#define MIN_GRID 10
#define MAX_GRID 50
#define MAX_UNDO 10
#define MIN_UNDO 1
#define MAX_LOAD_LINE 256
#define SMALL_PREVIEW_SZ 48
#define BRUSH_PREVIEW_SZ 32
#define GFIG_HEADER "GFIG Version 0.1\n"

#define PREVIEW_MASK   GDK_EXPOSURE_MASK | \
                       GDK_MOTION_NOTIFY | \
		       GDK_POINTER_MOTION_MASK | \
                       GDK_BUTTON_PRESS_MASK | \
		       GDK_BUTTON_RELEASE_MASK | \
		       GDK_BUTTON_MOTION_MASK | \
		       GDK_KEY_PRESS_MASK | \
		       GDK_KEY_RELEASE_MASK 

120
static GimpDrawable *gfig_select_drawable;
121 122 123 124 125 126 127 128
static GtkWidget *gfig_preview;
static GtkWidget *pic_preview;
static GtkWidget *gfig_gtk_list;
static gint       gfig_preview_exp_id;
static gint32     gfig_image;
static gint32     gfig_drawable;
static GtkWidget *brush_page_pw;
static GtkWidget *brush_sel_button;
Manish Singh's avatar
Manish Singh committed
129 130

static gint   tile_width, tile_height;
131
static gint   img_width, img_height, img_bpp, real_img_bpp;
Manish Singh's avatar
Manish Singh committed
132 133 134 135

static void      query  (void);
static void      run    (gchar    *name,
			 gint      nparams,
136
			 GimpParam   *param,
Manish Singh's avatar
Manish Singh committed
137
			 gint     *nreturn_vals,
138
			 GimpParam  **return_vals);
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

static gint      gfig_dialog               (void);
static void      gfig_ok_callback          (GtkWidget *widget,
					    gpointer   data);
static void      gfig_paint_callback       (GtkWidget *widget,
					    gpointer   data);
static void      gfig_clear_callback       (GtkWidget *widget,
					    gpointer   data);
static void      gfig_undo_callback        (GtkWidget *widget,
					    gpointer   data);
static gint      gfig_preview_expose       (GtkWidget *widget,
					    GdkEvent  *event);
static gint      pic_preview_expose        (GtkWidget *widget,
					    GdkEvent  *event);
static gint      gfig_preview_events       (GtkWidget *widget,
					    GdkEvent  *event);
static gint      gfig_brush_preview_events (GtkWidget *widget,
					    GdkEvent  *event);

static void      gfig_scale_update_scale   (GtkAdjustment *adjustment,
					    gdouble       *value);

static void      gfig_scale2img_update     (GtkWidget *widget,
					    gpointer   data);

static gint      gfig_scale_x              (gint       x);
static gint      gfig_scale_y              (gint       y);
static gint      gfig_invscale_x           (gint       x);
static gint      gfig_invscale_y           (gint       y);
static GdkGC *   gfig_get_grid_gc          (GtkWidget *widget,
					    gint       gctype);
static void      gfig_pos_enable           (GtkWidget *widget,
					    gpointer   data);

static gint      list_button_press         (GtkWidget      *widget,
					    GdkEventButton *event,
					    gpointer        data);

static void      rescan_button_callback    (GtkWidget *widget,
					    gpointer   data);
static void      load_button_callback      (GtkWidget *widget,
					    gpointer   data);
static void      save_button_callback      (GtkWidget *widget,
					    gpointer   data);
static void      new_button_callback       (GtkWidget *widget,
					    gpointer   data);
static void   gfig_do_delete_gfig_callback (GtkWidget *widget,
					    gboolean   delete,
					    gpointer   data);
static void      gfig_delete_gfig_callback (GtkWidget *widget,
					    gpointer   data);
static void      edit_button_callback      (GtkWidget *widget,
					    gpointer   data);
static void      merge_button_callback     (GtkWidget *widget,
					    gpointer   data);
static void      about_button_callback     (GtkWidget *widget,
					    gpointer   data);
static void      reload_button_callback    (GtkWidget *widget,
					    gpointer   data);

static void      do_gfig                   (void);
static void      dialog_update_preview     (void);

static void      draw_grid_clear           (GtkWidget *widget,
					    gpointer   data);
static void      toggle_show_image         (GtkWidget *widget,
					    gpointer   data);
static void      toggle_tooltips           (GtkWidget *widget,
					    gpointer   data);
static void      toggle_obj_type           (GtkWidget *widget,
					    gpointer   data);
static void      draw_grid                 (GtkWidget *widget,
					    gpointer   data);

static void      gfig_new_gc               (void);
static void      find_grid_pos             (GdkPoint  *p,
					    GdkPoint  *gp,
					    guint      state);

static void      brush_list_button_callback (GtkWidget *widget,
					     gpointer   data);
static gint      calculate_point_to_line_distance (GdkPoint *p,
						   GdkPoint *A,
						   GdkPoint *B,
						   GdkPoint *I);
Manish Singh's avatar
Manish Singh committed
224

225
GimpPlugInInfo PLUG_IN_INFO =
Manish Singh's avatar
Manish Singh committed
226
{
227 228 229 230
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
Manish Singh's avatar
Manish Singh committed
231 232 233 234 235
};

/* The types of an object */
/* Also includes actions that can be performed on objects */

236 237
typedef enum
{
Manish Singh's avatar
Manish Singh committed
238 239 240 241 242 243 244 245 246 247 248 249 250
  LINE,
  CIRCLE,
  ELLIPSE,
  ARC,
  POLY,
  STAR,
  SPIRAL,
  BEZIER,
  MOVE_OBJ,
  MOVE_POINT,
  COPY_OBJ,
  MOVE_COPY_OBJ,
  DEL_OBJ,
Adrian Likins's avatar
Adrian Likins committed
251
  NULL_OPER
252
} DobjType;
Manish Singh's avatar
Manish Singh committed
253

254 255
typedef enum
{
Manish Singh's avatar
Manish Singh committed
256 257
  RECT_GRID = 0,
  POLAR_GRID,
Adrian Likins's avatar
Adrian Likins committed
258
  ISO_GRID
259
} GridType;
Manish Singh's avatar
Manish Singh committed
260

261 262
typedef enum
{
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
263 264
  ORIGINAL_LAYER = 0,
  SINGLE_LAYER,
Adrian Likins's avatar
Adrian Likins committed
265
  MULTI_LAYER
266
} DrawonLayers;
Manish Singh's avatar
Manish Singh committed
267

268 269
typedef enum
{
Manish Singh's avatar
Manish Singh committed
270 271
  LAYER_TRANS_BG = 0,
  LAYER_BG_BG,
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
272
  LAYER_FG_BG,
Manish Singh's avatar
Manish Singh committed
273
  LAYER_WHITE_BG,
Adrian Likins's avatar
Adrian Likins committed
274
  LAYER_COPY_BG
275
} LayersBGType;
Manish Singh's avatar
Manish Singh committed
276

277 278
typedef enum
{
Manish Singh's avatar
Manish Singh committed
279 280
  PAINT_BRUSH_TYPE = 0,
  PAINT_SELECTION_TYPE,
Adrian Likins's avatar
Adrian Likins committed
281
  PAINT_SELECTION_FILL_TYPE
282
} PaintType;
Manish Singh's avatar
Manish Singh committed
283

284 285
typedef enum
{
286
  BRUSH_BRUSH_TYPE = 0,
Manish Singh's avatar
Manish Singh committed
287 288
  BRUSH_PENCIL_TYPE,
  BRUSH_AIRBRUSH_TYPE,
Adrian Likins's avatar
Adrian Likins committed
289
  BRUSH_PATTERN_TYPE
290
} BrushType;
Manish Singh's avatar
Manish Singh committed
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


#define GRID_TYPE_MENU 1
#define GRID_RENDER_MENU 2
#define GRID_IGNORE 0 
#define GRID_HIGHTLIGHT 1
#define GRID_RESTORE 2

#define GFIG_BLACK_GC -2
#define GFIG_WHITE_GC -3
#define GFIG_GREY_GC -4

#define PAINT_LAYERS_MENU 1
#define PAINT_BGS_MENU 2
#define PAINT_TYPE_MENU 3

#define SELECT_TYPE_MENU 1
#define SELECT_ARCTYPE_MENU 2
#define SELECT_TYPE_MENU_FILL 3
#define SELECT_TYPE_MENU_WHEN 4

#define OBJ_SELECT_GT 1
#define OBJ_SELECT_LT 2
#define OBJ_SELECT_EQ 4


317
typedef struct
Manish Singh's avatar
Manish Singh committed
318
{
319 320 321 322 323 324 325
  gint     gridspacing;
  GridType gridtype;
  gint     drawgrid;
  gint     snap2grid;
  gint     lockongrid;
  gint     showcontrol;
} GfigOpts;
Manish Singh's avatar
Manish Singh committed
326 327

/* Must keep in step with the above */
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
typedef struct
{
  void      *gridspacing;
  GtkWidget *gridtypemenu;
  GtkWidget *drawgrid;
  GtkWidget *snap2grid;
  GtkWidget *lockongrid;
  GtkWidget *showcontrol;
} GfigOptWidgets;

static GfigOptWidgets gfig_opt_widget;

typedef struct
{
  GfigOpts      opts;
  gint          showimage;
  gint          maxundo;
  gint          showpos;
  gdouble       brushfade;
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
347
  gdouble       brushgradient;
348 349 350 351 352 353 354 355 356 357 358
  gdouble       airbrushpressure;
  gint          showtooltips;
  DrawonLayers  onlayers;
  LayersBGType  onlayerbg;
  PaintType     painttype;
  gint          reverselines;
  gint          scaletoimage;
  gdouble       scaletoimagefp;
  gint          approxcircles;
  BrushType    brshtype;
  DobjType      otype;
Manish Singh's avatar
Manish Singh committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
} SelectItVals;

/* Values when first invoked */
static SelectItVals selvals =
{
  {
    MIN_GRID + (MAX_GRID - MIN_GRID)/2, /* Gridspacing */
    RECT_GRID, /* Default to rectangle type */
    0,  /* drawgrid */
    0,  /* snap2grid */
    0,  /* lockongrid */
    1,  /* show control points */
  },
  0,  /* show image */
  MIN_UNDO + (MAX_UNDO - MIN_UNDO)/2,  /* Max level of undos */
  FALSE, /* Show pos updates */
  0.0, /* Brush fade */
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
376
  0.0, /* Brush gradient */
Manish Singh's avatar
Manish Singh committed
377 378 379 380 381 382 383 384 385 386 387 388 389
  20.0, /* Air bursh pressure */
  TRUE,  /* show Tool tips */
  ORIGINAL_LAYER, /* Draw all objects on one layer */
  LAYER_TRANS_BG, /* New layers background */
  PAINT_BRUSH_TYPE, /* Default to use brushes */
  FALSE, /* reverse lines */
  TRUE, /* Scale to image when painting */
  1.0, /* Scale to image fp */
  FALSE, /* Approx circles by drawing lines */
  BRUSH_BRUSH_TYPE, /* Default to use a brush */
  LINE /* Initial object type */
};

390 391
typedef enum
{
Manish Singh's avatar
Manish Singh committed
392 393 394
  ADD=0,
  SUBTRACT=1,
  REPLACE=2,
Adrian Likins's avatar
Adrian Likins committed
395
  INTERSECT=3
396
} SelectionType;
Manish Singh's avatar
Manish Singh committed
397 398
    

399 400
typedef enum
{
Manish Singh's avatar
Manish Singh committed
401
  ARC_SEGMENT,
Adrian Likins's avatar
Adrian Likins committed
402
  ARC_SECTOR
403
} ArcType;
Manish Singh's avatar
Manish Singh committed
404

405 406
typedef enum
{
Manish Singh's avatar
Manish Singh committed
407 408
  FILL_FOREGROUND = 0,
  FILL_BACKGROUND = 1,
Adrian Likins's avatar
Adrian Likins committed
409
  FILL_PATTERN = 2
410
} FillType;
Manish Singh's avatar
Manish Singh committed
411

412 413
typedef enum
{
Manish Singh's avatar
Manish Singh committed
414
  FILL_EACH = 0,
Adrian Likins's avatar
Adrian Likins committed
415
  FILL_AFTER
416
} FillWhen;
Manish Singh's avatar
Manish Singh committed
417

418 419 420
struct selection_option
{
  SelectionType type; /* ADD etc .. */
Manish Singh's avatar
Manish Singh committed
421 422 423
  gint antia; /* Boolean for Antia */
  gint feather; /* Feather it ? */
  gdouble feather_radius; /* Radius to feather */
424 425 426
  ArcType as_pie; /* Arc type selection segment/sector */
  FillType fill_type; /* Fill type for selection */
  FillWhen fill_when; /* Fill on each selection or after all? */
Manish Singh's avatar
Manish Singh committed
427
  gdouble fill_opacity; /* You can guess this one */
428 429
} selopt =
{
Manish Singh's avatar
Manish Singh committed
430 431 432 433 434 435 436 437 438 439 440
  ADD, /* type */
  FALSE, /* Antia */
  FALSE, /* Feather */
  10.0, /* feather radius */
  ARC_SEGMENT,  /* Arc as a segment */
  FILL_PATTERN, /* Fill as pattern */
  FILL_EACH, /* Fill after each selection */
  100.0, /* Max opacity */
};


441 442 443
static GList *gfig_path_list = NULL;
static GList *gfig_list      = NULL;
static gint   line_no;
Manish Singh's avatar
Manish Singh committed
444

445 446 447 448 449
static gint poly_num_sides    = 3; /* Default to three sided object */
static gint star_num_sides    = 3; /* Default to three sided object */
static gint spiral_num_turns  = 4; /* Default to 4 turns */
static gint spiral_toggle     = 0; /* 0 = clockwise -1 = anti-clockwise */
static gint bezier_closed     = 0; /* Closed curve 0 = false 1 = true */
Manish Singh's avatar
Manish Singh committed
450 451 452 453 454 455 456
static gint bezier_line_frame = 0; /* Show frame = false 1 = true */

static gint obj_show_single = -1; /* -1 all >= 0 object number */

/* Structures etc for the objects */
/* Points used to draw the object  */

457 458
typedef struct DobjPoints
{
Manish Singh's avatar
Manish Singh committed
459 460 461
  struct DobjPoints * next;
  GdkPoint pnt;
  gint found_me;
462
} DobjPoints;
Manish Singh's avatar
Manish Singh committed
463

464

465
struct Dobject; /* fwd declaration for DobjFunc */
466

467 468 469 470
typedef void            (*DobjFunc) (struct Dobject *);
typedef struct Dobject *(*DobjGenFunc) (struct Dobject *);
typedef struct Dobject *(*DobjLoadFunc) (FILE *);
typedef void            (*DobjSaveFunc) (struct Dobject *, FILE *);
Manish Singh's avatar
Manish Singh committed
471 472

/* The object itself */
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
typedef struct Dobject
{
  DobjType      type; /* What is the type? */
  gpointer      type_data; /* Extra data needed by the object */
  DobjPoints   *points; /* List of points */
  DobjFunc      drawfunc; /* How do I draw myself */
  DobjFunc      paintfunc; /* Draw me on canvas */
  DobjGenFunc   copyfunc;  /* copy */
  DobjLoadFunc  loadfunc;  /* Load this type of object */
  DobjSaveFunc  savefunc;  /* Save me out */
} Dobject;


static Dobject *obj_creating; /* Object we are creating */
static Dobject *tmp_line; /* Needed when drawing lines */
static Dobject *tmp_bezier; /* Neeed when drawing bezier curves */

typedef struct DAllObjs
{
  struct DAllObjs *next; 
  Dobject         *obj; /* Object on list */
} DAllObjs;
Manish Singh's avatar
Manish Singh committed
495 496 497 498 499 500

/* States of the object */
#define GFIG_OK       0x0
#define GFIG_MODIFIED 0x1
#define GFIG_READONLY 0x2

501 502 503 504 505 506 507 508 509
typedef struct DFigObj
{
  gchar     *name;     /* Trailing name of file  */
  gchar     *filename; /* Filename itself */
  gchar     *draw_name;/* Name of the drawing */
  gfloat     version;     /* Version number of data file */
  GfigOpts   opts;    /* Options enforced when fig saved */
  DAllObjs  *obj_list; /* Objects that make up this list */
  gint       obj_status;    /* See above for possible values */
Manish Singh's avatar
Manish Singh committed
510 511 512
  GtkWidget *list_item;
  GtkWidget *label_widget;
  GtkWidget *pixmap_widget;
513
} GFigObj;  
Manish Singh's avatar
Manish Singh committed
514 515


516 517 518 519 520
typedef struct BrushDesc
{
  gchar  *bname; /* name of the brush */
  gint32  width;  /* Width of brush */
  gint32  height;  /* Height of brush */
Adrian Likins's avatar
Adrian Likins committed
521
  guchar *pv_buf; /* Buffer where brush placed */
522 523 524 525
  gint16  x_off;
  gint16  y_off;
  gint    bpp; /* Depth - should ALWAYS be the same for all BrushDesc */
} BrushDesc;
Manish Singh's avatar
Manish Singh committed
526

527 528
static GFigObj  *current_obj;
static Dobject  *operation_obj;
Manish Singh's avatar
Manish Singh committed
529
static GdkPoint *move_all_pnt; /* Point moving all from */
530 531 532 533 534 535 536 537 538
static GFigObj  *pic_obj;
static DAllObjs *undo_table[MAX_UNDO];
static gint      need_to_scale;
static gint32    brush_image_ID = -1;

static GtkWidget *undo_widget;
static GtkWidget *gfig_op_menu; /* Popup menu in the list box */
static GtkWidget *delete_frame_to_freeze; /* Top preview frame window */
static GtkWidget *fade_out_hbox;   /* Fade out widget in brush page */
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
539
static GtkWidget *gradient_hbox;   /* Gradient widget in brush page */
540 541
static GtkWidget *pressure_hbox;   /* Pressure widget in brush page */
static GtkWidget *pencil_hbox;     /* Dummy widget in brush page */
542
static GtkWidget *pos_label;       /* XY pos marker */
543 544 545
static GtkWidget *brush_page_widget; /* Widget for the brush part of notebook */
static GtkWidget *select_page_widget; /* Widget for the selection part
				       * of notebook */
Manish Singh's avatar
Manish Singh committed
546 547

static gint undo_water_mark = -1; /* Last slot filled in -1 = no undo */
548 549 550 551 552 553 554
static gint drawing_pic = FALSE;  /* If true drawing to the small preview */
static GtkWidget *status_label_dname;
static GtkWidget *status_label_fname;
static GFigObj   *gfig_obj_for_menu; /* More static data -
				      * need to know which object was selected*/
static GtkWidget *save_menu_item;  
static GtkWidget *save_button;
Manish Singh's avatar
Manish Singh committed
555 556 557 558


/* Don't up just like BIGGG source files? */

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
static void       object_start            (GdkPoint *pnt, gint);
static void       object_operation        (GdkPoint *pnt, gint);
static void       object_operation_start  (GdkPoint *pnt, gint shift_down);
static void       object_operation_end    (GdkPoint *pnt, gint);
static void       object_end              (GdkPoint *pnt, gint shift_down);
static void       object_update           (GdkPoint * pnt);
static void       add_to_all_obj          (GFigObj * fobj, Dobject *obj);
static void       d_delete_dobjpoints     (DobjPoints *);
static Dobject  * d_new_line              (gint x, gint y);
static Dobject  * d_new_circle            (gint x, gint y);
static DAllObjs * copy_all_objs           (DAllObjs *objs);
static void       setup_undo              (void);
static void       d_pnt_add_line          (Dobject *obj,
					   gint x, gint y, gint pos);
static GFigObj  * gfig_load               (gchar *filename, gchar *name);
static void       free_all_objs           (DAllObjs * objs);
static void       draw_objects            (DAllObjs *objs, gint show_single);
static Dobject  * d_load_line             (FILE *from);
static Dobject  * d_load_circle           (FILE *from);
static gchar    * get_line                (gchar *buf, gint s,
					   FILE * from, gint init);
static GFigObj  * gfig_new                (void);
static void       clear_undo              (void);
static void       list_button_update      (GFigObj *obj);
static void       prepend_to_all_obj      (GFigObj *fobj, DAllObjs *nobj);
static void       gfig_update_stat_labels (void);
static void       gfig_obj_modified       (GFigObj *obj, gint stat_type);
static void       gfig_op_menu_create     (GtkWidget *window);
static void       gridtype_menu_callback  (GtkWidget *widget, gpointer data);
static void       draw_one_obj            (Dobject * obj);
static void       d_save_poly             (Dobject * obj, FILE *to);
static Dobject  * d_load_poly             (FILE *from);
static void       d_draw_poly             (Dobject *obj);
static void       d_paint_poly            (Dobject *obj);
static Dobject  * d_copy_poly             (Dobject * obj);
static Dobject  * d_new_poly              (gint x, gint y);
static void       d_update_poly           (GdkPoint *pnt);
static void       d_poly_start            (GdkPoint *pnt, gint shift_down);
static void       d_poly_end              (GdkPoint *pnt, gint shift_down);
static void       d_save_star             (Dobject * obj, FILE *to);
static Dobject  * d_load_star             (FILE *from);
static void       d_draw_star             (Dobject *obj);
static void       d_paint_star            (Dobject *obj);
static Dobject  * d_copy_star             (Dobject * obj);
static Dobject  * d_new_star              (gint x, gint y);
static void       d_update_star           (GdkPoint *pnt);
static void       d_star_start            (GdkPoint *pnt, gint shift_down);
static void       d_star_end              (GdkPoint *pnt, gint shift_down);
static Dobject  * d_load_spiral           (FILE *from);
static void       d_draw_spiral           (Dobject *obj);
static void       d_paint_spiral          (Dobject *obj);
static Dobject  * d_copy_spiral           (Dobject * obj);
static Dobject  * d_new_spiral            (gint x, gint y);
static void       d_update_spiral         (GdkPoint *pnt);
static void       d_spiral_start          (GdkPoint *pnt, gint shift_down);
static void       d_spiral_end            (GdkPoint *pnt, gint shift_down);

static Dobject  * d_load_bezier           (FILE *from);
static void       d_draw_bezier           (Dobject *obj);
static void       d_paint_bezier          (Dobject *obj);
static Dobject  * d_copy_bezier           (Dobject * obj);
static Dobject  * d_new_bezier            (gint x, gint y);
static void       d_update_bezier         (GdkPoint *pnt);
static void       d_bezier_start          (GdkPoint *pnt, gint shift_down);
static void       d_bezier_end            (GdkPoint *pnt, gint shift_down);

static void       new_obj_2edit           (GFigObj *obj);
static Dobject  * d_new_ellipse           (gint x, gint y);
static Dobject  * d_load_ellipse          (FILE *from);
static Dobject  * d_new_arc               (gint x, gint y);
static Dobject  * d_load_arc              (FILE *from);
static gint       load_options            (GFigObj *gfig, FILE *fp);
static gint       gfig_obj_counts         (DAllObjs * objs);

static void    gfig_brush_fill_preview_xy (GtkWidget *pw, gint x , gint y);
Manish Singh's avatar
Manish Singh committed
634 635 636 637


/* globals */

638 639 640 641 642 643
static gint    gfig_run;
static GdkGC  *gfig_gc;
static GdkGC  *grid_hightlight_drawgc;
static gint    grid_gc_type = GTK_STATE_NORMAL;
static guchar *pv_cache = NULL;
static guchar  preview_row[PREVIEW_SIZE*4];
Manish Singh's avatar
Manish Singh committed
644 645

/* Stuff for the preview bit */
646 647 648 649 650 651
static gint    sel_x1, sel_y1, sel_x2, sel_y2;
static gint    sel_width, sel_height;
static gint    preview_width, preview_height;
static gint    has_alpha;
static gdouble scale_x_factor, scale_y_factor;
static gdouble org_scale_x_factor, org_scale_y_factor;
Manish Singh's avatar
Manish Singh committed
652

Adrian Likins's avatar
Adrian Likins committed
653
MAIN ()
Manish Singh's avatar
Manish Singh committed
654 655

static void
656
query (void)
Manish Singh's avatar
Manish Singh committed
657
{
658
  static GimpParamDef args[] =
Manish Singh's avatar
Manish Singh committed
659
  {
660 661 662 663
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
    { GIMP_PDB_INT32, "dummy", "dummy" } 
Manish Singh's avatar
Manish Singh committed
664
  };
665
  static gint nargs = sizeof (args) / sizeof (args[0]);
David Monniaux's avatar
David Monniaux committed
666

Manish Singh's avatar
Manish Singh committed
667
  gimp_install_procedure ("plug_in_gfig",
Marc Lehmann's avatar
Marc Lehmann committed
668 669
			  "Create Geometrical shapes with the Gimp",
			  "More here later",
Manish Singh's avatar
Manish Singh committed
670 671 672
			  "Andy Thomas",
			  "Andy Thomas",
			  "1997",
Stanislav Brabec's avatar
Stanislav Brabec committed
673
			  N_("<Image>/Filters/Render/Gfig..."),
Manish Singh's avatar
Manish Singh committed
674
			  "RGB*, GRAY*",
675
			  GIMP_PLUGIN,
676 677
			  nargs, 0,
			  args, NULL);
Manish Singh's avatar
Manish Singh committed
678 679 680
}

static void
681 682
run (gchar    *name,
     gint      nparams,
683
     GimpParam   *param,
684
     gint     *nreturn_vals,
685
     GimpParam  **return_vals)
Manish Singh's avatar
Manish Singh committed
686
{
687 688 689 690
  GimpParam * values = g_new (GimpParam, 1);
  GimpDrawable *drawable;
  GimpRunModeType run_mode;
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
Manish Singh's avatar
Manish Singh committed
691

692
  gint pwidth, pheight;
Manish Singh's avatar
Manish Singh committed
693

694
  /*kill (getpid (), 19);*/
Manish Singh's avatar
Manish Singh committed
695 696 697 698 699 700 701 702

  run_mode = param[0].data.d_int32;
  gfig_image = param[1].data.d_image;
  gfig_drawable = param[2].data.d_drawable;

  *nreturn_vals = 1;
  *return_vals = values;

703
  values[0].type = GIMP_PDB_STATUS;
Manish Singh's avatar
Manish Singh committed
704 705
  values[0].data.d_status = status;

706
  gfig_select_drawable = drawable =
Manish Singh's avatar
Manish Singh committed
707 708
    gimp_drawable_get (param[2].data.d_drawable);

709 710
  tile_width  = gimp_tile_width ();
  tile_height = gimp_tile_height ();
Manish Singh's avatar
Manish Singh committed
711 712

  /* TMP Hack - clear any selections */
713
  gimp_selection_clear (gfig_image);
Manish Singh's avatar
Manish Singh committed
714

715 716
  gimp_drawable_mask_bounds (drawable->drawable_id,
			     &sel_x1, &sel_y1, &sel_x2, &sel_y2);
Manish Singh's avatar
Manish Singh committed
717 718 719 720 721 722

  sel_width  = sel_x2 - sel_x1;
  sel_height = sel_y2 - sel_y1;

  /* Calculate preview size */
  
723 724 725 726 727 728 729 730 731
  if (sel_width > sel_height)
    {
      pwidth  = MIN (sel_width, PREVIEW_SIZE);
      pheight = sel_height * pwidth / sel_width;
    }
  else
    {
      pheight = MIN (sel_height, PREVIEW_SIZE);
      pwidth  = sel_width * pheight / sel_height;
Manish Singh's avatar
Manish Singh committed
732 733
  }
  
734 735
  preview_width  = MAX (pwidth, 2);  /* Min size is 2 */
  preview_height = MAX (pheight, 2); 
Manish Singh's avatar
Manish Singh committed
736

737 738 739 740
  org_scale_x_factor = scale_x_factor =
    (gdouble) sel_width / (gdouble) preview_width;
  org_scale_y_factor = scale_y_factor =
    (gdouble) sel_height / (gdouble) preview_height;
Manish Singh's avatar
Manish Singh committed
741 742 743
  
  switch (run_mode)
    {
744
    case GIMP_RUN_INTERACTIVE:
Manish Singh's avatar
Manish Singh committed
745
      /*gimp_get_data ("plug_in_gfig", &selvals);*/
746 747
      INIT_I18N_UI ();
      if (!gfig_dialog ())
Manish Singh's avatar
Manish Singh committed
748 749 750 751 752 753
	{
	  gimp_drawable_detach (drawable);
	  return;
	}
      break;

754 755
    case GIMP_RUN_NONINTERACTIVE:
      status = GIMP_PDB_CALLING_ERROR;
Manish Singh's avatar
Manish Singh committed
756 757
      break;

758
    case GIMP_RUN_WITH_LAST_VALS:
Manish Singh's avatar
Manish Singh committed
759 760 761 762 763 764 765
      /*gimp_get_data ("plug_in_gfig", &selvals);*/
      break;

    default:
      break;
    }

766 767
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
      gimp_drawable_is_gray (drawable->drawable_id))
Manish Singh's avatar
Manish Singh committed
768 769
    {
      /* Set the tile cache size */
770 771
      gimp_tile_cache_ntiles ((drawable->width + gimp_tile_width () - 1) /
			      gimp_tile_width ());
Manish Singh's avatar
Manish Singh committed
772

773
      do_gfig ();
Manish Singh's avatar
Manish Singh committed
774

775
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
Manish Singh's avatar
Manish Singh committed
776 777 778
	gimp_displays_flush ();

#if 0
779
      if (run_mode == GIMP_RUN_INTERACTIVE)
Manish Singh's avatar
Manish Singh committed
780 781 782 783 784
	gimp_set_data ("plug_in_gfig", &selvals, sizeof (SelectItVals));
#endif /* 0 */
    }
  else
    {
785
      status = GIMP_PDB_EXECUTION_ERROR;
Manish Singh's avatar
Manish Singh committed
786 787 788 789 790 791 792 793
    }

  values[0].data.d_status = status;

  gimp_drawable_detach (drawable);
}

/*
Michael Natterer's avatar
Michael Natterer committed
794
 *  Query gimprc for gfig-path, and parse it.
Manish Singh's avatar
Manish Singh committed
795 796
 */

797 798
static void
plug_in_parse_gfig_path (void)
Manish Singh's avatar
Manish Singh committed
799
{
Michael Natterer's avatar
Michael Natterer committed
800 801
  GList *fail_list = NULL;
  GList *list;
802
  gchar *gfig_path;
803 804

  if (gfig_path_list)
Michael Natterer's avatar
Michael Natterer committed
805
    gimp_path_free (gfig_path_list);
Manish Singh's avatar
Manish Singh committed
806 807 808
  
  gfig_path_list = NULL;
  
809 810 811
  gfig_path = gimp_gimprc_query ("gfig-path");

  if (!gfig_path)
Manish Singh's avatar
Manish Singh committed
812
    {
813
      gchar *gimprc = gimp_personal_rc_file ("gimprc");
814 815 816 817 818 819 820 821 822 823
      gchar *path = gimp_strescape
	("${gimp_dir}" G_DIR_SEPARATOR_S "gfig"
	 G_SEARCHPATH_SEPARATOR_S
	 "${gimp_data_dir}" G_DIR_SEPARATOR_S "gfig",
	 NULL);
      g_message (_("No gfig-path in gimprc:\n"
		   "You need to add an entry like\n"
		   "(gfig-path \"%s\")\n"
		   "to your %s file."),
		   path, gimprc);
824
      g_free (gimprc);
825
      g_free (path);
Manish Singh's avatar
Manish Singh committed
826 827 828
      return;
    }
  
829
  gfig_path_list = gimp_path_parse (gfig_path, 16, TRUE, &fail_list);
Manish Singh's avatar
Manish Singh committed
830

831
  g_free (gfig_path);
Manish Singh's avatar
Manish Singh committed
832

Michael Natterer's avatar
Michael Natterer committed
833
  if (fail_list)
Manish Singh's avatar
Manish Singh committed
834
    {
Michael Natterer's avatar
Michael Natterer committed
835 836 837
      GString *err =
	g_string_new (_("gfig-path misconfigured - "
			"the following directories were not found"));
Manish Singh's avatar
Manish Singh committed
838

Michael Natterer's avatar
Michael Natterer committed
839
      for (list = fail_list; list; list = g_list_next (list))
Manish Singh's avatar
Manish Singh committed
840
	{
Michael Natterer's avatar
Michael Natterer committed
841 842
	  g_string_append_c (err, '\n');
	  g_string_append (err, (gchar *) list->data);
Manish Singh's avatar
Manish Singh committed
843 844
	}

Michael Natterer's avatar
Michael Natterer committed
845
      g_message (err->str);
Manish Singh's avatar
Manish Singh committed
846

Michael Natterer's avatar
Michael Natterer committed
847 848
      g_string_free (err, TRUE);
      gimp_path_free (fail_list);
Manish Singh's avatar
Manish Singh committed
849 850 851 852 853 854
    }
}


/*
  Translate SPACE to "\\040", etc.
Manish Singh's avatar
Manish Singh committed
855
  Taken from gflare plugin
Manish Singh's avatar
Manish Singh committed
856
 */
857 858 859
static void
gfig_name_encode (gchar *dest,
		  gchar *src)
Manish Singh's avatar
Manish Singh committed
860
{
861
  gint cnt = MAX_LOAD_LINE - 1;
Manish Singh's avatar
Manish Singh committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

  while (*src && cnt--)
    {
      if (iscntrl (*src) || isspace (*src) || *src == '\\')
	{
	  sprintf (dest, "\\%03o", *src++);
	  dest += 4;
	}
      else
	*dest++ = *src++;
    }
  *dest = '\0';
}

/*
  Translate "\\040" to SPACE, etc.
 */
879 880 881
static void
gfig_name_decode (gchar *dest,
		  gchar *src)
Manish Singh's avatar
Manish Singh committed
882
{
883 884
  gint cnt = MAX_LOAD_LINE - 1;
  gint tmp;
Manish Singh's avatar
Manish Singh committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

  while (*src && cnt--)
    {
      if (*src == '\\' && *(src+1) && *(src+2) && *(src+3))
	{
	  sscanf (src+1, "%3o", &tmp);
	  *dest++ = tmp;
	  src += 4;
	}
      else
	*dest++ = *src++;
    }
  *dest = '\0';
}


/*
 * Load all gfig, which are founded in gfig-path-list, into gfig_list.
 * gfig-path-list must be initialized first. (plug_in_parse_gfig_path ())
 * based on code from Gflare.
 */

907 908
static gint
gfig_list_pos (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
909
{
910 911
  GFigObj *g;
  gint n;
Manish Singh's avatar
Manish Singh committed
912 913 914
  GList *tmp;

  n = 0;
915 916

  for (tmp = gfig_list; tmp; tmp = g_list_next (tmp)) 
Manish Singh's avatar
Manish Singh committed
917 918 919 920 921
    {
      g = tmp->data;
      
      if (strcmp (gfig->draw_name, g->draw_name) <= 0)
	break;
922

Manish Singh's avatar
Manish Singh committed
923 924 925
      n++;
    }

926
  return n;
Manish Singh's avatar
Manish Singh committed
927 928
}

929 930
static gint
gfig_list_insert (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
931
{
932
  gint n;
Manish Singh's avatar
Manish Singh committed
933 934 935 936 937

  /*
   *	Insert gfigs in alphabetical order
   */

938
  n = gfig_list_pos (gfig);
Manish Singh's avatar
Manish Singh committed
939 940 941 942

  gfig_list = g_list_insert (gfig_list, gfig, n);

#ifdef DEBUG
943
  printf ("gfig_list_insert %s => %d\n", gfig->draw_name, n);
Manish Singh's avatar
Manish Singh committed
944 945 946 947 948
#endif /* DEBUG */

  return n;
}

949 950
static void
gfig_free (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
951 952 953
{
  g_assert (gfig != NULL);

954 955 956 957 958 959 960
  if (gfig->obj_list)
    free_all_objs (gfig->obj_list);

  g_free (gfig->name);
  g_free (gfig->filename);
  g_free (gfig->draw_name);

Manish Singh's avatar
Manish Singh committed
961 962 963
  g_free (gfig);
}

964 965
static void
gfig_free_everything (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
966 967 968
{
  g_assert (gfig != NULL);

969
  if (gfig->filename)
Manish Singh's avatar
Manish Singh committed
970 971
    {
#ifdef DEBUG
972
      printf ("Removing filename '%s'\n", gfig->filename);
Manish Singh's avatar
Manish Singh committed
973
#endif /* DEBUG */
974
      remove (gfig->filename);
Manish Singh's avatar
Manish Singh committed
975
    }
976 977

  gfig_free (gfig);
Manish Singh's avatar
Manish Singh committed
978 979
}

980 981
static void
gfig_list_free_all (void)
Manish Singh's avatar
Manish Singh committed
982
{
983 984
  GList   *list;
  GFigObj *gfig;
Manish Singh's avatar
Manish Singh committed
985

986
  for (list = gfig_list; list; list = g_list_next (list))
Manish Singh's avatar
Manish Singh committed
987
    {
988
      gfig = (GFigObj *) list->data;
Manish Singh's avatar
Manish Singh committed
989 990 991 992 993 994 995 996
      gfig_free (gfig);
    }

  g_list_free (gfig_list);
  gfig_list = NULL;
}


997 998
static void
gfig_list_load_all (GList *plist)
Manish Singh's avatar
Manish Singh committed
999
{
1000 1001 1002 1003 1004
  GFigObj *gfig;
  GList   *list;
  gchar	  *path;
  gchar	  *filename;
  DIR	  *dir;
Manish Singh's avatar
Manish Singh committed
1005
  struct dirent *dir_ent;
1006 1007
  struct stat    filestat;
  gint err;
Manish Singh's avatar
Manish Singh committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

  /*  Make sure to clear any existing gfigs  */
  current_obj = pic_obj = NULL;
  gfig_list_free_all ();

  list = plist;
  while (list)
    {
      path = list->data;
      list = list->next;

      /* Open directory */
      dir = opendir (path);

      if (!dir)
1023
	g_warning ("Error reading GFig directory \"%s\"", path);
Manish Singh's avatar
Manish Singh committed
1024 1025 1026 1027
      else
	{
	  while ((dir_ent = readdir (dir)))
	    {
1028
	      filename = g_malloc (strlen (path) + strlen (dir_ent->d_name) + 1);
Manish Singh's avatar
Manish Singh committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

	      sprintf (filename, "%s%s", path, dir_ent->d_name);

	      /* Check the file and see that it is not a sub-directory */
	      err = stat (filename, &filestat);

	      if (!err && S_ISREG (filestat.st_mode))
		{
		  gfig = gfig_load (filename, dir_ent->d_name);
		  
		  if (gfig)
		    {
		      /* Read only ?*/
1042
		      if (access (filename, W_OK))
Manish Singh's avatar
Manish Singh committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
			gfig->obj_status |= GFIG_READONLY;

		      gfig_list_insert (gfig);
		    }
		}

	      g_free (filename);
	    } /* while */
	  closedir (dir);
	} /* else */
    }

1055
  if (!gfig_list)
Manish Singh's avatar
Manish Singh committed
1056 1057
    {
      /* lets have at least one! */
1058 1059 1060
      gfig = gfig_new ();
      gfig->draw_name = g_strdup (_("First Gfig"));
      gfig_list_insert (gfig);
Manish Singh's avatar
Manish Singh committed
1061 1062 1063 1064 1065
    }
  pic_obj = current_obj = gfig_list->data;  /* set to first entry */

}

1066 1067
static GFigObj *
gfig_new (void)
Manish Singh's avatar
Manish Singh committed
1068
{
1069 1070 1071
  GFigObj * new;

  new = g_new0 (GFigObj, 1);
Manish Singh's avatar
Manish Singh committed
1072

1073
  return new;
Manish Singh's avatar
Manish Singh committed
1074 1075
}

1076 1077 1078 1079
static void
gfig_load_objs (GFigObj *gfig,
		gint     load_count,
		FILE    *fp)
Manish Singh's avatar
Manish Singh committed
1080
{
1081
  Dobject *obj;
Manish Singh's avatar
Manish Singh committed
1082 1083 1084
  gchar load_buf[MAX_LOAD_LINE];

  /* Loading object */
1085
  /*kill (getpid (), 19);*/
Manish Singh's avatar
Manish Singh committed
1086
  /* Read first line */
1087
  while (load_count-- > 0)
Manish Singh's avatar
Manish Singh committed
1088 1089
    {
      obj = NULL;