gfig.c 118 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
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
25
26
#include "config.h"

Manish Singh's avatar
Manish Singh committed
27
28
#include <stdio.h>
#include <stdlib.h>
29
#include <errno.h>
30

Tor Lillqvist's avatar
Tor Lillqvist committed
31
#ifdef HAVE_UNISTD_H
Manish Singh's avatar
Manish Singh committed
32
#include <unistd.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
33
#endif
34

Manish Singh's avatar
Manish Singh committed
35
#include <string.h>
36
37
38
39
40
41

#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

Manish Singh's avatar
Manish Singh committed
42
#include <gtk/gtk.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
43

44
#ifdef G_OS_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
45
46
47
48
#  include <io.h>
#  ifndef W_OK
#    define W_OK 2
#  endif
49
50
#endif

51
52
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
Manish Singh's avatar
Manish Singh committed
53

54
55
#include "libgimp/stdplugins-intl.h"

56
#include "gfig.h"
57
58
59
60
61
#include "gfig-arc.h"
#include "gfig-bezier.h"
#include "gfig-circle.h"
#include "gfig-dobject.h"
#include "gfig-ellipse.h"
62
#include "gfig-grid.h"
63
64
#include "gfig-line.h"
#include "gfig-poly.h"
65
#include "gfig-preview.h"
66
67
#include "gfig-spiral.h"
#include "gfig-star.h"
68
69
#include "gfig-stock.h"

70
#include "pix-data.h"
Manish Singh's avatar
Manish Singh committed
71

72

Manish Singh's avatar
Manish Singh committed
73
74
/***** Magic numbers *****/

75
76
77
78
79
#define RESPONSE_UNDO    1
#define RESPONSE_CLEAR   2
#define RESPONSE_SAVE    3
#define RESPONSE_PAINT   4

80
#define PREVIEW_SIZE     400
81

82
#define SCALE_WIDTH      120
Manish Singh's avatar
Manish Singh committed
83

84
85
#define MAX_UNDO         10
#define MIN_UNDO         1
Manish Singh's avatar
Manish Singh committed
86
87
#define SMALL_PREVIEW_SZ 48
#define BRUSH_PREVIEW_SZ 32
88
#define GFIG_HEADER      "GFIG Version 0.1\n"
Manish Singh's avatar
Manish Singh committed
89

90
#define PREVIEW_MASK  (GDK_EXPOSURE_MASK       | \
Manish Singh's avatar
Manish Singh committed
91
		       GDK_POINTER_MOTION_MASK | \
92
                       GDK_BUTTON_PRESS_MASK   | \
Manish Singh's avatar
Manish Singh committed
93
		       GDK_BUTTON_RELEASE_MASK | \
94
95
96
		       GDK_BUTTON_MOTION_MASK  | \
		       GDK_KEY_PRESS_MASK      | \
		       GDK_KEY_RELEASE_MASK)
Manish Singh's avatar
Manish Singh committed
97

98
static GtkWidget *top_level_dlg;
99
static GimpDrawable *gfig_select_drawable;
100
101
GtkWidget    *gfig_preview;
GtkWidget    *pic_preview;
102
static GtkWidget    *gfig_gtk_list;
103
104
gint32        gfig_image;
gint32        gfig_drawable;
105
106
static GtkWidget    *brush_page_pw;
static GtkWidget    *brush_sel_button;
Manish Singh's avatar
Manish Singh committed
107

108
static gint   img_width, img_height;
Manish Singh's avatar
Manish Singh committed
109
110

static void      query  (void);
111
112
113
114
115
static void      run    (const gchar      *name,
			 gint              nparams,
			 const GimpParam  *param,
			 gint             *nreturn_vals,
			 GimpParam       **return_vals);
116
117

static gint      gfig_dialog               (void);
118
119
static void      gfig_response             (GtkWidget *widget,
                                            gint       response_id,
120
					    gpointer   data);
121
static void      gfig_paint_callback       (void);
Sven Neumann's avatar
Sven Neumann committed
122
static gboolean  pic_preview_expose        (GtkWidget *widget,
123
					    GdkEvent  *event);
124

125
126
127
128
129
130
131
132
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);
133
134
static gint      gfig_scale_x    	   (gint       x);
static gint      gfig_scale_y    	   (gint       y);
135
136
137
138
139
140
141
142
143
144
145

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      new_button_callback       (GtkWidget *widget,
					    gpointer   data);
146
147
148
static void   	gfig_do_delete_gfig_callback (GtkWidget *widget,
					      gboolean   delete,
					      gpointer   data);
149
150
151
152
153
154
155
156
157
158
159
160
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);
161
static void      toggle_show_image         (void);
162
static void      gfig_new_gc               (void);
Manish Singh's avatar
Manish Singh committed
163

164
GimpPlugInInfo PLUG_IN_INFO =
Manish Singh's avatar
Manish Singh committed
165
{
166
167
168
169
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
Manish Singh's avatar
Manish Singh committed
170
171
};

172
#define GRID_TYPE_MENU   1
Manish Singh's avatar
Manish Singh committed
173
#define GRID_RENDER_MENU 2
174
#define GRID_IGNORE      0
175
176
#define GRID_HIGHTLIGHT  1
#define GRID_RESTORE     2
Manish Singh's avatar
Manish Singh committed
177
178

#define PAINT_LAYERS_MENU 1
179
180
#define PAINT_BGS_MENU    2
#define PAINT_TYPE_MENU   3
Manish Singh's avatar
Manish Singh committed
181

182
183
#define SELECT_TYPE_MENU      1
#define SELECT_ARCTYPE_MENU   2
Manish Singh's avatar
Manish Singh committed
184
185
186
187
188
189
190
191
#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

/* Must keep in step with the above */
192
193
194
195
196
197
198
199
200
201
202
203
typedef struct
{
  void      *gridspacing;
  GtkWidget *gridtypemenu;
  GtkWidget *drawgrid;
  GtkWidget *snap2grid;
  GtkWidget *lockongrid;
  GtkWidget *showcontrol;
} GfigOptWidgets;

static GfigOptWidgets gfig_opt_widget;

Manish Singh's avatar
Manish Singh committed
204
/* Values when first invoked */
205
SelectItVals selvals =
Manish Singh's avatar
Manish Singh committed
206
207
{
  {
Sven Neumann's avatar
Sven Neumann committed
208
209
210
211
212
213
    MIN_GRID + (MAX_GRID - MIN_GRID)/2, /* Gridspacing     */
    RECT_GRID,            /* Default to rectangle type     */
    FALSE,                /* drawgrid                      */
    FALSE,                /* snap2grid                     */
    FALSE,                /* lockongrid                    */
    TRUE                  /* show control points           */
Manish Singh's avatar
Manish Singh committed
214
  },
Sven Neumann's avatar
Sven Neumann committed
215
  FALSE,                  /* show image                    */
Manish Singh's avatar
Manish Singh committed
216
  MIN_UNDO + (MAX_UNDO - MIN_UNDO)/2,  /* Max level of undos */
Sven Neumann's avatar
Sven Neumann committed
217
218
219
220
221
222
223
224
225
226
227
228
229
  TRUE,                   /* Show pos updates              */
  0.0,                    /* Brush fade                    */
  0.0,                    /* Brush gradient                */
  20.0,                   /* Air bursh pressure            */
  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           */
Manish Singh's avatar
Manish Singh committed
230
231
};

232
selection_option selopt =
233
{
234
235
236
237
  ADD,          /* type */
  FALSE,        /* Antia */
  FALSE,        /* Feather */
  10.0,         /* feather radius */
Manish Singh's avatar
Manish Singh committed
238
239
  ARC_SEGMENT,  /* Arc as a segment */
  FILL_PATTERN, /* Fill as pattern */
240
241
  FILL_EACH,    /* Fill after each selection */
  100.0,        /* Max opacity */
Manish Singh's avatar
Manish Singh committed
242
243
244
};


245
246
static gchar *gfig_path       = NULL;
static GList *gfig_list       = NULL;
247
gint   line_no;
Manish Singh's avatar
Manish Singh committed
248

249
gint obj_show_single   = -1; /* -1 all >= 0 object number */
Manish Singh's avatar
Manish Singh committed
250
251
252
253
254

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


255
256
Dobject *obj_creating; /* Object we are creating */
Dobject *tmp_line;     /* Needed when drawing lines */
Manish Singh's avatar
Manish Singh committed
257

258
259
typedef struct BrushDesc
{
260
261
262
263
264
265
266
267
268
  gchar                *name;
  gdouble               opacity;
  gint                  spacing;
  GimpLayerModeEffects  paint_mode;
  gint                  width;
  gint                  height;
  guchar               *pv_buf;  /* Buffer where brush placed */
  gint16                x_off;
  gint16                y_off;
269
  const gchar          *popup;
270
} BrushDesc;
Manish Singh's avatar
Manish Singh committed
271

272

273
GFigObj  *current_obj;
274
GFigObj  *pic_obj;
275
static DAllObjs *undo_table[MAX_UNDO];
276
gint      need_to_scale;
277
278
static gint32    brush_image_ID = -1;

Sven Neumann's avatar
Sven Neumann committed
279
280
static GtkWidget *gfig_op_menu;    /* Popup menu in the list box */
static GtkWidget *object_list;     /* Top preview frame window */
281
static GtkWidget *fade_out_hbox;   /* Fade out widget in brush page */
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
282
static GtkWidget *gradient_hbox;   /* Gradient widget in brush page */
283
284
285
286
287
static GtkWidget *pressure_hbox;   /* Pressure widget in brush page */
static GtkWidget *pencil_hbox;     /* Dummy widget in brush page */
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
288

289
static gint       undo_water_mark = -1; /* Last slot filled in -1 = no undo */
290
gboolean       drawing_pic = FALSE;  /* If true drawing to the small preview */
291
292
static GFigObj   *gfig_obj_for_menu; /* More static data -
				      * need to know which object was selected*/
293
static GtkWidget *save_menu_item;
Manish Singh's avatar
Manish Singh committed
294
295
296
297


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

298
299
static GFigObj  * gfig_load               (const gchar *filename,
                                           const gchar *name);
300
301
302
303
304
305
306
307
308
309
310
311
static void       free_all_objs           (DAllObjs * objs);
static GFigObj  * gfig_new                (void);
static void       clear_undo              (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       new_obj_2edit           (GFigObj *obj);
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
312

313
314
static void      brush_list_button_callback (BrushDesc *bdesc);

Manish Singh's avatar
Manish Singh committed
315
316
317

/* globals */

318
static gint    gfig_run;
319
GdkGC  *gfig_gc;
Manish Singh's avatar
Manish Singh committed
320
321

/* Stuff for the preview bit */
322
323
static gint    sel_x1, sel_y1, sel_x2, sel_y2;
static gint    sel_width, sel_height;
324
325
gint    preview_width, preview_height;
gdouble scale_x_factor, scale_y_factor;
326
static gdouble org_scale_x_factor, org_scale_y_factor;
Manish Singh's avatar
Manish Singh committed
327

Adrian Likins's avatar
Adrian Likins committed
328
MAIN ()
Manish Singh's avatar
Manish Singh committed
329
330

static void
331
query (void)
Manish Singh's avatar
Manish Singh committed
332
{
333
  static GimpParamDef args[] =
Manish Singh's avatar
Manish Singh committed
334
  {
335
336
337
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
338
    { GIMP_PDB_INT32, "dummy", "dummy" }
Manish Singh's avatar
Manish Singh committed
339
  };
David Monniaux's avatar
David Monniaux committed
340

Manish Singh's avatar
Manish Singh committed
341
  gimp_install_procedure ("plug_in_gfig",
Marc Lehmann's avatar
Marc Lehmann committed
342
343
			  "Create Geometrical shapes with the Gimp",
			  "More here later",
Manish Singh's avatar
Manish Singh committed
344
345
346
			  "Andy Thomas",
			  "Andy Thomas",
			  "1997",
347
			  N_("<Image>/Filters/Render/_Gfig..."),
Manish Singh's avatar
Manish Singh committed
348
			  "RGB*, GRAY*",
349
			  GIMP_PLUGIN,
350
			  G_N_ELEMENTS (args), 0,
351
			  args, NULL);
Manish Singh's avatar
Manish Singh committed
352
353
354
}

static void
355
356
357
358
359
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
Manish Singh's avatar
Manish Singh committed
360
{
361
362
363
364
  GimpParam         *values = g_new (GimpParam, 1);
  GimpDrawable      *drawable;
  GimpRunMode        run_mode;
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
Manish Singh's avatar
Manish Singh committed
365

366
  gint pwidth, pheight;
Manish Singh's avatar
Manish Singh committed
367

368
369
  INIT_I18N ();

Manish Singh's avatar
Manish Singh committed
370
371
372
373
374
375
376
  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;

377
  values[0].type = GIMP_PDB_STATUS;
Manish Singh's avatar
Manish Singh committed
378
379
  values[0].data.d_status = status;

380
  gfig_select_drawable = drawable = gimp_drawable_get (param[2].data.d_drawable);
Manish Singh's avatar
Manish Singh committed
381
382

  /* TMP Hack - clear any selections */
383
384
  if (! gimp_selection_is_empty (gfig_image))
    gimp_selection_clear (gfig_image);
Manish Singh's avatar
Manish Singh committed
385

386
387
  gimp_drawable_mask_bounds (drawable->drawable_id,
			     &sel_x1, &sel_y1, &sel_x2, &sel_y2);
Manish Singh's avatar
Manish Singh committed
388
389
390
391
392

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

  /* Calculate preview size */
393

394
395
396
397
398
399
400
401
402
  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;
403
    }
404

405
  preview_width  = MAX (pwidth, 2);  /* Min size is 2 */
406
  preview_height = MAX (pheight, 2);
Manish Singh's avatar
Manish Singh committed
407

408
409
410
411
  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;
412

Manish Singh's avatar
Manish Singh committed
413
414
  switch (run_mode)
    {
415
    case GIMP_RUN_INTERACTIVE:
Manish Singh's avatar
Manish Singh committed
416
      /*gimp_get_data ("plug_in_gfig", &selvals);*/
417
      if (! gfig_dialog ())
Manish Singh's avatar
Manish Singh committed
418
419
420
421
422
423
	{
	  gimp_drawable_detach (drawable);
	  return;
	}
      break;

424
425
    case GIMP_RUN_NONINTERACTIVE:
      status = GIMP_PDB_CALLING_ERROR;
Manish Singh's avatar
Manish Singh committed
426
427
      break;

428
    case GIMP_RUN_WITH_LAST_VALS:
Manish Singh's avatar
Manish Singh committed
429
430
431
432
433
434
435
      /*gimp_get_data ("plug_in_gfig", &selvals);*/
      break;

    default:
      break;
    }

436
437
  if (gimp_drawable_is_rgb (drawable->drawable_id) ||
      gimp_drawable_is_gray (drawable->drawable_id))
Manish Singh's avatar
Manish Singh committed
438
439
    {
      /* Set the tile cache size */
440
441
      gimp_tile_cache_ntiles ((drawable->width + gimp_tile_width () - 1) /
			      gimp_tile_width ());
Manish Singh's avatar
Manish Singh committed
442

443
      do_gfig ();
Manish Singh's avatar
Manish Singh committed
444

445
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
Manish Singh's avatar
Manish Singh committed
446
447
448
	gimp_displays_flush ();

#if 0
449
      if (run_mode == GIMP_RUN_INTERACTIVE)
Manish Singh's avatar
Manish Singh committed
450
451
452
453
454
	gimp_set_data ("plug_in_gfig", &selvals, sizeof (SelectItVals));
#endif /* 0 */
    }
  else
    {
455
      status = GIMP_PDB_EXECUTION_ERROR;
Manish Singh's avatar
Manish Singh committed
456
457
458
459
460
461
462
463
464
    }

  values[0].data.d_status = status;

  gimp_drawable_detach (drawable);
}

/*
  Translate SPACE to "\\040", etc.
Manish Singh's avatar
Manish Singh committed
465
  Taken from gflare plugin
Manish Singh's avatar
Manish Singh committed
466
 */
467
468
469
static void
gfig_name_encode (gchar *dest,
		  gchar *src)
Manish Singh's avatar
Manish Singh committed
470
{
471
  gint cnt = MAX_LOAD_LINE - 1;
Manish Singh's avatar
Manish Singh committed
472
473
474

  while (*src && cnt--)
    {
475
      if (g_ascii_iscntrl (*src) || g_ascii_isspace (*src) || *src == '\\')
Manish Singh's avatar
Manish Singh committed
476
477
478
479
480
481
482
483
484
485
486
487
488
	{
	  sprintf (dest, "\\%03o", *src++);
	  dest += 4;
	}
      else
	*dest++ = *src++;
    }
  *dest = '\0';
}

/*
  Translate "\\040" to SPACE, etc.
 */
489
static void
490
491
gfig_name_decode (gchar       *dest,
		  const gchar *src)
Manish Singh's avatar
Manish Singh committed
492
{
493
494
  gint  cnt = MAX_LOAD_LINE - 1;
  guint tmp;
Manish Singh's avatar
Manish Singh committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

  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.
 */

517
518
static gint
gfig_list_pos (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
519
{
520
521
  GFigObj *g;
  gint n;
Manish Singh's avatar
Manish Singh committed
522
523
524
  GList *tmp;

  n = 0;
525

526
  for (tmp = gfig_list; tmp; tmp = g_list_next (tmp))
Manish Singh's avatar
Manish Singh committed
527
528
    {
      g = tmp->data;
529

Manish Singh's avatar
Manish Singh committed
530
531
      if (strcmp (gfig->draw_name, g->draw_name) <= 0)
	break;
532

Manish Singh's avatar
Manish Singh committed
533
534
      n++;
    }
535
  return n;
Manish Singh's avatar
Manish Singh committed
536
537
}

538
539
540
541
/*
 *	Insert gfigs in alphabetical order
 */

542
543
static gint
gfig_list_insert (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
544
{
545
  gint n;
Manish Singh's avatar
Manish Singh committed
546

547
  n = gfig_list_pos (gfig);
Manish Singh's avatar
Manish Singh committed
548
549
550
551
552
553

  gfig_list = g_list_insert (gfig_list, gfig, n);

  return n;
}

554
555
static void
gfig_free (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
556
557
558
{
  g_assert (gfig != NULL);

559
  free_all_objs (gfig->obj_list);
560
561
562
563
564

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

Manish Singh's avatar
Manish Singh committed
565
566
567
  g_free (gfig);
}

568
569
static void
gfig_free_everything (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
570
571
572
{
  g_assert (gfig != NULL);

573
  if (gfig->filename)
Manish Singh's avatar
Manish Singh committed
574
    {
575
      remove (gfig->filename);
Manish Singh's avatar
Manish Singh committed
576
    }
577
  gfig_free (gfig);
Manish Singh's avatar
Manish Singh committed
578
579
}

580
581
static void
gfig_list_free_all (void)
Manish Singh's avatar
Manish Singh committed
582
{
583
  g_list_foreach (gfig_list, (GFunc) gfig_free, NULL);
Manish Singh's avatar
Manish Singh committed
584
585
586
587
  g_list_free (gfig_list);
  gfig_list = NULL;
}

588
static void
589
590
gfig_list_load_one (const GimpDatafileData *file_data,
                    gpointer                user_data)
Manish Singh's avatar
Manish Singh committed
591
{
592
  GFigObj *gfig;
Manish Singh's avatar
Manish Singh committed
593

594
  gfig = gfig_load (file_data->filename, file_data->basename);
Manish Singh's avatar
Manish Singh committed
595

596
  if (gfig)
Manish Singh's avatar
Manish Singh committed
597
    {
598
599
600
      /* Read only ?*/
      if (access (file_data->filename, W_OK))
        gfig->obj_status |= GFIG_READONLY;
Manish Singh's avatar
Manish Singh committed
601

602
603
604
      gfig_list_insert (gfig);
    }
}
Manish Singh's avatar
Manish Singh committed
605

606
607
608
609
610
611
static void
gfig_list_load_all (const gchar *path)
{
  /*  Make sure to clear any existing gfigs  */
  current_obj = pic_obj = NULL;
  gfig_list_free_all ();
Manish Singh's avatar
Manish Singh committed
612

613
614
615
  gimp_datafiles_read_directories (path, G_FILE_TEST_EXISTS,
                                   gfig_list_load_one,
                                   NULL);
Manish Singh's avatar
Manish Singh committed
616

617
  if (! gfig_list)
Manish Singh's avatar
Manish Singh committed
618
    {
619
620
      GFigObj *gfig;

Manish Singh's avatar
Manish Singh committed
621
      /* lets have at least one! */
622
623
624
      gfig = gfig_new ();
      gfig->draw_name = g_strdup (_("First Gfig"));
      gfig_list_insert (gfig);
Manish Singh's avatar
Manish Singh committed
625
626
    }

627
  pic_obj = current_obj = gfig_list->data;  /* set to first entry */
Manish Singh's avatar
Manish Singh committed
628
629
}

630
631
static GFigObj *
gfig_new (void)
Manish Singh's avatar
Manish Singh committed
632
{
633
  return g_new0 (GFigObj, 1);
Manish Singh's avatar
Manish Singh committed
634
635
}

636
637
638
639
static void
gfig_load_objs (GFigObj *gfig,
		gint     load_count,
		FILE    *fp)
Manish Singh's avatar
Manish Singh committed
640
{
641
  Dobject *obj;
Manish Singh's avatar
Manish Singh committed
642
643
644
  gchar load_buf[MAX_LOAD_LINE];

  /* Loading object */
645
  /*kill (getpid (), 19);*/
Manish Singh's avatar
Manish Singh committed
646
  /* Read first line */
647
  while (load_count-- > 0)
Manish Singh's avatar
Manish Singh committed
648
649
    {
      obj = NULL;
650
651
652
      get_line (load_buf, MAX_LOAD_LINE, fp, 0);

      if (!strcmp (load_buf, "<LINE>"))
Manish Singh's avatar
Manish Singh committed
653
	{
654
	  obj = d_load_line (fp);
Manish Singh's avatar
Manish Singh committed
655
	}
656
      else if (!strcmp (load_buf, "<CIRCLE>"))
Manish Singh's avatar
Manish Singh committed
657
	{
658
	  obj = d_load_circle (fp);
Manish Singh's avatar
Manish Singh committed
659
	}
660
      else if (!strcmp (load_buf, "<ELLIPSE>"))
Manish Singh's avatar
Manish Singh committed
661
	{
662
	  obj = d_load_ellipse (fp);
Manish Singh's avatar
Manish Singh committed
663
	}
664
      else if (!strcmp (load_buf, "<POLY>"))
Manish Singh's avatar
Manish Singh committed
665
	{
666
	  obj = d_load_poly (fp);
Manish Singh's avatar
Manish Singh committed
667
	}
668
      else if (!strcmp (load_buf, "<STAR>"))
Manish Singh's avatar
Manish Singh committed
669
	{
670
	  obj = d_load_star (fp);
Manish Singh's avatar
Manish Singh committed
671
	}
672
      else if (!strcmp (load_buf, "<SPIRAL>"))
Manish Singh's avatar
Manish Singh committed
673
	{
674
	  obj = d_load_spiral (fp);
Manish Singh's avatar
Manish Singh committed
675
	}
676
      else if (!strcmp (load_buf, "<BEZIER>"))
Manish Singh's avatar
Manish Singh committed
677
	{
678
	  obj = d_load_bezier (fp);
Manish Singh's avatar
Manish Singh committed
679
	}
680
      else if (!strcmp (load_buf, "<ARC>"))
Manish Singh's avatar
Manish Singh committed
681
	{
682
	  obj = d_load_arc (fp);
Manish Singh's avatar
Manish Singh committed
683
684
685
	}
      else
	{
686
	  g_warning ("Unknown obj type file %s line %d\n", gfig->filename, line_no);
Manish Singh's avatar
Manish Singh committed
687
	}
688

689
      if (obj)
Manish Singh's avatar
Manish Singh committed
690
	{
691
	  add_to_all_obj (gfig, obj);
Manish Singh's avatar
Manish Singh committed
692
693
694
695
	}
    }
}

696
static GFigObj *
697
698
gfig_load (const gchar *filename,
           const gchar *name)
Manish Singh's avatar
Manish Singh committed
699
{
700
701
702
703
704
705
706
  GFigObj *gfig;
  FILE    *fp;
  gchar    load_buf[MAX_LOAD_LINE];
  gchar    str_buf[MAX_LOAD_LINE];
  gint     chk_count;
  gint     load_count = 0;

Manish Singh's avatar
Manish Singh committed
707
708
709
  g_assert (filename != NULL);

#ifdef DEBUG
710
  printf ("Loading %s (%s)\n", filename, name);
Manish Singh's avatar
Manish Singh committed
711
712
713
714
715
#endif /* DEBUG */

  fp = fopen (filename, "r");
  if (!fp)
    {
716
717
      g_message (_("Could not open '%s' for reading: %s"),
                 filename, g_strerror (errno));
Manish Singh's avatar
Manish Singh committed
718
719
720
      return NULL;
    }

721
  gfig = gfig_new ();
Manish Singh's avatar
Manish Singh committed
722

723
724
  gfig->name = g_strdup (name);
  gfig->filename = g_strdup (filename);
Manish Singh's avatar
Manish Singh committed
725
726
727
728
729
730
731
732


  /* HEADER
   * draw_name
   * version
   * obj_list
   */

733
  get_line (load_buf, MAX_LOAD_LINE, fp, 1);
Manish Singh's avatar
Manish Singh committed
734

735
  if (strncmp (GFIG_HEADER, load_buf, strlen (load_buf)))
Manish Singh's avatar
Manish Singh committed
736
    {
737
738
      g_message ("File '%s' is not a gfig file", gfig->filename);
      return NULL;
Manish Singh's avatar
Manish Singh committed
739
    }
740

741
742
743
744
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
  sscanf (load_buf, "Name: %100s", str_buf);
  gfig_name_decode (load_buf, str_buf);
  gfig->draw_name = g_strdup (load_buf);
Manish Singh's avatar
Manish Singh committed
745

746
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
747
748
  if (strncmp (load_buf, "Version: ", 9) == 0)
    gfig->version = g_ascii_strtod (load_buf + 9, NULL);
Manish Singh's avatar
Manish Singh committed
749

750
751
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
  sscanf (load_buf, "ObjCount: %d", &load_count);
Manish Singh's avatar
Manish Singh committed
752

753
  if (load_options (gfig, fp))
Manish Singh's avatar
Manish Singh committed
754
    {
755
756
757
      g_message ("File '%s' corrupt file - Line %d Option section incorrect",
		 filename, line_no);
      return NULL;
Manish Singh's avatar
Manish Singh committed
758
759
    }

760
  /*return (NULL);*/
Manish Singh's avatar
Manish Singh committed
761

762
  gfig_load_objs (gfig, load_count, fp);
Manish Singh's avatar
Manish Singh committed
763
764

  /* Check count ? */
765

766
  chk_count = gfig_obj_counts (gfig->obj_list);
Manish Singh's avatar
Manish Singh committed
767

768
  if (chk_count != load_count)
Manish Singh's avatar
Manish Singh committed
769
    {
770
771
772
      g_message ("File '%s' corrupt file - Line %d Object count to small",
		 filename, line_no);
      return NULL;
Manish Singh's avatar
Manish Singh committed
773
774
    }

775
  fclose (fp);
Manish Singh's avatar
Manish Singh committed
776

777
  if (!pic_obj)
Manish Singh's avatar
Manish Singh committed
778
779
780
781
    pic_obj = gfig;

  gfig->obj_status = GFIG_OK;

782
  return gfig;
Manish Singh's avatar
Manish Singh committed
783
784
}

785
786
static void
save_options (FILE *fp)
Manish Singh's avatar
Manish Singh committed
787
788
{
  /* Save options */
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  fprintf (fp, "<OPTIONS>\n");
  fprintf (fp, "GridSpacing: %d\n", selvals.opts.gridspacing);
  if (selvals.opts.gridtype == RECT_GRID)
    fprintf (fp, "GridType: RECT_GRID\n");
  else if (selvals.opts.gridtype == POLAR_GRID)
    fprintf (fp, "GridType: POLAR_GRID\n");
  else if (selvals.opts.gridtype == ISO_GRID)
    fprintf (fp, "GridType: ISO_GRID\n");
  else fprintf (fp, "GridType: RECT_GRID\n"); /* If in doubt, default to RECT_GRID */
  fprintf (fp, "DrawGrid: %s\n", (selvals.opts.drawgrid)?"TRUE":"FALSE");
  fprintf (fp, "Snap2Grid: %s\n", (selvals.opts.snap2grid)?"TRUE":"FALSE");
  fprintf (fp, "LockOnGrid: %s\n", (selvals.opts.lockongrid)?"TRUE":"FALSE");
  /*  fprintf (fp, "ShowImage: %s\n", (selvals.opts.showimage)?"TRUE":"FALSE");*/
  fprintf (fp, "ShowControl: %s\n", (selvals.opts.showcontrol)?"TRUE":"FALSE");
  fprintf (fp, "</OPTIONS>\n");
}

static gint
load_bool (gchar *opt_buf,
	   gint  *toset)
{
  if (!strcmp (opt_buf, "TRUE"))
Manish Singh's avatar
Manish Singh committed
811
    *toset = 1;
812
  else if (!strcmp (opt_buf, "FALSE"))
Manish Singh's avatar
Manish Singh committed
813
814
    *toset = 0;
  else
815
    return (-1);
Manish Singh's avatar
Manish Singh committed
816

817
  return (0);
Manish Singh's avatar
Manish Singh committed
818
819
820
}

static void
821
update_options (GFigObj *old_obj)
Manish Singh's avatar
Manish Singh committed
822
823
{
  /* Save old vals */
824
  if (selvals.opts.gridspacing != old_obj->opts.gridspacing)
Manish Singh's avatar
Manish Singh committed
825
826
827
    {
      old_obj->opts.gridspacing = selvals.opts.gridspacing;
    }
828
  if (selvals.opts.gridtype != old_obj->opts.gridtype)
Manish Singh's avatar
Manish Singh committed
829
830
831
    {
      old_obj->opts.gridtype = selvals.opts.gridtype;
    }
832
  if (selvals.opts.drawgrid != old_obj->opts.drawgrid)
Manish Singh's avatar
Manish Singh committed
833
834
835
    {
      old_obj->opts.drawgrid = selvals.opts.drawgrid;
    }
836
  if (selvals.opts.snap2grid != old_obj->opts.snap2grid)
Manish Singh's avatar
Manish Singh committed
837
838
839
    {
      old_obj->opts.snap2grid = selvals.opts.snap2grid;
    }
840
  if (selvals.opts.lockongrid != old_obj->opts.lockongrid)
Manish Singh's avatar
Manish Singh committed
841
842
843
    {
      old_obj->opts.lockongrid = selvals.opts.lockongrid;
    }
844
  if (selvals.opts.showcontrol != old_obj->opts.showcontrol)
Manish Singh's avatar
Manish Singh committed
845
846
847
848
849
    {
      old_obj->opts.showcontrol = selvals.opts.showcontrol;
    }

  /* New vals */
850
  if (selvals.opts.gridspacing != current_obj->opts.gridspacing)
Manish Singh's avatar
Manish Singh committed
851
    {
852
853
854
      gtk_adjustment_set_value
	(GTK_ADJUSTMENT (gfig_opt_widget.gridspacing),
	 current_obj->opts.gridspacing);
Manish Singh's avatar
Manish Singh committed
855
    }
856
  if (selvals.opts.drawgrid != current_obj->opts.drawgrid)
Manish Singh's avatar
Manish Singh committed
857
    {
858
859
860
      gtk_toggle_button_set_active
	(GTK_TOGGLE_BUTTON (gfig_opt_widget.drawgrid),
	 current_obj->opts.drawgrid);
Manish Singh's avatar
Manish Singh committed
861
    }
862
  if (selvals.opts.snap2grid != current_obj->opts.snap2grid)
Manish Singh's avatar
Manish Singh committed
863
    {
864
865
866
      gtk_toggle_button_set_active
	(GTK_TOGGLE_BUTTON (gfig_opt_widget.snap2grid),
	 current_obj->opts.snap2grid);
Manish Singh's avatar
Manish Singh committed
867
    }
868
869
  if (selvals.opts.lockongrid != current_obj->opts.lockongrid)
    {
870
871
#if 0
      /* Maurits: code not implemented */
872
873
874
      gtk_toggle_button_set_active
	(GTK_TOGGLE_BUTTON (gfig_opt_widget.lockongrid),
	 current_obj->opts.lockongrid);
875
#endif
876
    }
877
  if (selvals.opts.showcontrol != current_obj->opts.showcontrol)
Manish Singh's avatar
Manish Singh committed
878
    {
879
880
881
      gtk_toggle_button_set_active
	(GTK_TOGGLE_BUTTON (gfig_opt_widget.showcontrol),
	 current_obj->opts.showcontrol);
Manish Singh's avatar
Manish Singh committed
882
    }
883
  if (selvals.opts.gridtype != current_obj->opts.gridtype)
Manish Singh's avatar
Manish Singh committed
884
    {
885
886
      gtk_option_menu_set_history
	(GTK_OPTION_MENU (gfig_opt_widget.gridtypemenu),
887
	 current_obj->opts.gridtype);
Manish Singh's avatar
Manish Singh committed
888

889
890
891
892
      gridtype_menu_callback
	(gtk_menu_get_active
	 (GTK_MENU (gtk_option_menu_get_menu
		    (GTK_OPTION_MENU (gfig_opt_widget.gridtypemenu)))),
893
	 GINT_TO_POINTER (GRID_TYPE_MENU));
894

Manish Singh's avatar
Manish Singh committed
895
#ifdef DEBUG
896
      printf ("Gridtype set in options to ");
Manish Singh's avatar
Manish Singh committed
897
      if (current_obj->opts.gridtype == RECT_GRID)
898
	printf ("RECT_GRID\n");
Manish Singh's avatar
Manish Singh committed
899
      else if (current_obj->opts.gridtype == POLAR_GRID)
900
	printf ("POLAR_GRID\n");
Manish Singh's avatar
Manish Singh committed
901
      else if (current_obj->opts.gridtype == ISO_GRID)
902
903
	printf ("ISO_GRID\n");
      else printf ("NONE\n");
Manish Singh's avatar
Manish Singh committed
904
905
906
907
#endif /* DEBUG */
    }
}

908
909
910
static gint
load_options (GFigObj *gfig,
	      FILE    *fp)
Manish Singh's avatar
Manish Singh committed
911
912
913
914
915
{
  gchar load_buf[MAX_LOAD_LINE];
  gchar str_buf[MAX_LOAD_LINE];
  gchar opt_buf[MAX_LOAD_LINE];

916
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
Manish Singh's avatar
Manish Singh committed
917
918

#ifdef DEBUG
919
  printf ("load '%s'\n", load_buf);
Manish Singh's avatar
Manish Singh committed
920
921
#endif /* DEBUG */

922
923
  if (strcmp (load_buf, "<OPTIONS>"))
    return (-1);
924

925
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
Manish Singh's avatar
Manish Singh committed
926
927

#ifdef DEBUG
928
  printf ("opt line '%s'\n", load_buf);
Manish Singh's avatar
Manish Singh committed
929
930
#endif /* DEBUG */

931
  while (strcmp (load_buf, "</OPTIONS>"))
Manish Singh's avatar
Manish Singh committed
932
933
934
    {
      /* Get option name */
#ifdef DEBUG
935
      printf ("num = %d\n", sscanf (load_buf, "%s %s", str_buf, opt_buf));
Manish Singh's avatar
Manish Singh committed
936

937
      printf ("option %s val %s\n", str_buf, opt_buf);
Manish Singh's avatar
Manish Singh committed
938
#else
939
      sscanf (load_buf, "%s %s", str_buf, opt_buf);
Manish Singh's avatar
Manish Singh committed
940
941
#endif /* DEBUG */

942
      if (!strcmp (str_buf, "GridSpacing:"))
Manish Singh's avatar
Manish Singh committed
943
944
945
	{
	  /* Value is decimal */
	  int sp = 0;
946
947
948
	  sp = atoi (opt_buf);
	  if (sp <= 0)
	    return (-1);
Manish Singh's avatar
Manish Singh committed
949
950
	  gfig->opts.gridspacing = sp;
	}
951
      else if (!strcmp (str_buf, "DrawGrid:"))
Manish Singh's avatar
Manish Singh committed
952
953
	{
	  /* Value is bool */
954
955
	  if (load_bool (opt_buf, &gfig->opts.drawgrid))
	    return (-1);
Manish Singh's avatar
Manish Singh committed
956
	}
957
      else if (!strcmp (str_buf, "Snap2Grid:"))
Manish Singh's avatar
Manish Singh committed
958
959
	{
	  /* Value is bool */
960
961
	  if (load_bool (opt_buf, &gfig->opts.snap2grid))
	    return (-1);
Manish Singh's avatar
Manish Singh committed
962
	}
963
      else if (!strcmp (str_buf, "LockOnGrid:"))
Manish Singh's avatar
Manish Singh committed
964
965
	{
	  /* Value is bool */
966
967
	  if (load_bool (opt_buf, &gfig->opts.lockongrid))
	    return (-1);
Manish Singh's avatar
Manish Singh committed
968
	}
969
      else if (!strcmp (str_buf, "ShowControl:"))
Manish Singh's avatar
Manish Singh committed
970
971
	{
	  /* Value is bool */