gfig.c 128 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       | \
91
                       GDK_POINTER_MOTION_MASK | \
92
                       GDK_BUTTON_PRESS_MASK   | \
93 94 95 96
                       GDK_BUTTON_RELEASE_MASK | \
                       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 109
static gint          img_width;
static gint          img_height;
Manish Singh's avatar
Manish Singh committed
110 111

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

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

126
static gint      gfig_brush_preview_events (GtkWidget *widget,
127
                                            GdkEvent  *event);
128 129

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

static void      gfig_scale2img_update     (GtkWidget *widget,
133 134 135
                                            gpointer   data);
static gint      gfig_scale_x              (gint       x);
static gint      gfig_scale_y              (gint       y);
136 137

static gint      list_button_press         (GtkWidget      *widget,
138 139
                                            GdkEventButton *event,
                                            gpointer        data);
140 141

static void      rescan_button_callback    (GtkWidget *widget,
142
                                            gpointer   data);
143
static void      load_button_callback      (GtkWidget *widget,
144
                                            gpointer   data);
145
static void      new_button_callback       (GtkWidget *widget,
146
                                            gpointer   data);
147 148 149
static void   gfig_do_delete_gfig_callback (GtkWidget *widget,
                                            gboolean   delete,
                                            gpointer   data);
150
static void      gfig_delete_gfig_callback (GtkWidget *widget,
151
                                            gpointer   data);
152
static void      edit_button_callback      (GtkWidget *widget,
153
                                            gpointer   data);
154
static void      merge_button_callback     (GtkWidget *widget,
155
                                            gpointer   data);
156
static void      about_button_callback     (GtkWidget *widget,
157
                                            gpointer   data);
158
static void      reload_button_callback    (GtkWidget *widget,
159
                                            gpointer   data);
160 161

static void      do_gfig                   (void);
162
static void      toggle_show_image         (void);
163
static void      gfig_new_gc               (void);
Manish Singh's avatar
Manish Singh committed
164

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

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

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

183 184
#define SELECT_TYPE_MENU      1
#define SELECT_ARCTYPE_MENU   2
Manish Singh's avatar
Manish Singh committed
185 186 187 188 189 190 191 192
#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 */
193 194 195 196 197 198 199 200 201 202 203 204
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
205
/* Values when first invoked */
206
SelectItVals selvals =
Manish Singh's avatar
Manish Singh committed
207 208
{
  {
Sven Neumann's avatar
Sven Neumann committed
209 210 211 212 213 214
    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
215
  },
Sven Neumann's avatar
Sven Neumann committed
216
  FALSE,                  /* show image                    */
Manish Singh's avatar
Manish Singh committed
217
  MIN_UNDO + (MAX_UNDO - MIN_UNDO)/2,  /* Max level of undos */
Sven Neumann's avatar
Sven Neumann committed
218 219 220 221 222 223 224 225 226 227 228 229 230
  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
231 232
};

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


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

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

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


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

259 260
typedef struct BrushDesc
{
261 262 263 264 265 266 267 268 269
  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;
270
  const gchar          *popup;
271
} BrushDesc;
Manish Singh's avatar
Manish Singh committed
272

273

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

Sven Neumann's avatar
Sven Neumann committed
280 281
static GtkWidget *gfig_op_menu;    /* Popup menu in the list box */
static GtkWidget *object_list;     /* Top preview frame window */
282
static GtkWidget *fade_out_hbox;   /* Fade out widget in brush page */
BST 2000 Andy Thomas's avatar
BST 2000 Andy Thomas committed
283
static GtkWidget *gradient_hbox;   /* Gradient widget in brush page */
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
288
                                       * of notebook */
Manish Singh's avatar
Manish Singh committed
289

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


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

299 300
static GFigObj  * gfig_load               (const gchar *filename,
                                           const gchar *name);
301 302 303 304 305
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);
306
static void       gridtype_combo_callback (GtkWidget *widget, gpointer data);
307 308 309 310 311 312

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
313

314 315
static void      brush_list_button_callback (BrushDesc *bdesc);

Manish Singh's avatar
Manish Singh committed
316 317 318

/* globals */

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

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

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

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

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

  gimp_plugin_menu_register ("plug_in_gfig",
                             N_("<Image>/Filters/Render"));
Manish Singh's avatar
Manish Singh committed
356 357 358
}

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

370
  gint pwidth, pheight;
Manish Singh's avatar
Manish Singh committed
371

372 373
  INIT_I18N ();

Manish Singh's avatar
Manish Singh committed
374 375 376 377 378 379 380
  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;

381
  values[0].type = GIMP_PDB_STATUS;
Manish Singh's avatar
Manish Singh committed
382 383
  values[0].data.d_status = status;

384
  gfig_select_drawable = drawable = gimp_drawable_get (param[2].data.d_drawable);
Manish Singh's avatar
Manish Singh committed
385 386

  /* TMP Hack - clear any selections */
387 388
  if (! gimp_selection_is_empty (gfig_image))
    gimp_selection_clear (gfig_image);
Manish Singh's avatar
Manish Singh committed
389

390
  gimp_drawable_mask_bounds (drawable->drawable_id,
391
                             &sel_x1, &sel_y1, &sel_x2, &sel_y2);
Manish Singh's avatar
Manish Singh committed
392 393 394 395 396

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

  /* Calculate preview size */
397

398 399 400 401 402 403 404 405 406
  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;
407
    }
408

409
  preview_width  = MAX (pwidth, 2);  /* Min size is 2 */
410
  preview_height = MAX (pheight, 2);
Manish Singh's avatar
Manish Singh committed
411

412 413 414 415
  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;
416

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

428 429
    case GIMP_RUN_NONINTERACTIVE:
      status = GIMP_PDB_CALLING_ERROR;
Manish Singh's avatar
Manish Singh committed
430 431
      break;

432
    case GIMP_RUN_WITH_LAST_VALS:
Manish Singh's avatar
Manish Singh committed
433 434 435 436 437 438 439
      /*gimp_get_data ("plug_in_gfig", &selvals);*/
      break;

    default:
      break;
    }

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

447
      do_gfig ();
Manish Singh's avatar
Manish Singh committed
448

449
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
450
        gimp_displays_flush ();
Manish Singh's avatar
Manish Singh committed
451 452

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

  values[0].data.d_status = status;

  gimp_drawable_detach (drawable);
}

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

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

/*
  Translate "\\040" to SPACE, etc.
 */
493
static void
494
gfig_name_decode (gchar       *dest,
495
                  const gchar *src)
Manish Singh's avatar
Manish Singh committed
496
{
497 498
  gint  cnt = MAX_LOAD_LINE - 1;
  guint tmp;
Manish Singh's avatar
Manish Singh committed
499 500 501 502

  while (*src && cnt--)
    {
      if (*src == '\\' && *(src+1) && *(src+2) && *(src+3))
503 504 505 506 507
        {
          sscanf (src+1, "%3o", &tmp);
          *dest++ = tmp;
          src += 4;
        }
Manish Singh's avatar
Manish Singh committed
508
      else
509
        *dest++ = *src++;
Manish Singh's avatar
Manish Singh committed
510 511 512 513 514 515 516 517 518 519 520
    }
  *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.
 */

521 522
static gint
gfig_list_pos (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
523
{
524 525
  GFigObj *g;
  gint n;
Manish Singh's avatar
Manish Singh committed
526 527 528
  GList *tmp;

  n = 0;
529

530
  for (tmp = gfig_list; tmp; tmp = g_list_next (tmp))
Manish Singh's avatar
Manish Singh committed
531 532
    {
      g = tmp->data;
533

Manish Singh's avatar
Manish Singh committed
534
      if (strcmp (gfig->draw_name, g->draw_name) <= 0)
535
        break;
536

Manish Singh's avatar
Manish Singh committed
537 538
      n++;
    }
539
  return n;
Manish Singh's avatar
Manish Singh committed
540 541
}

542
/*
543
 *      Insert gfigs in alphabetical order
544 545
 */

546 547
static gint
gfig_list_insert (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
548
{
549
  gint n;
Manish Singh's avatar
Manish Singh committed
550

551
  n = gfig_list_pos (gfig);
Manish Singh's avatar
Manish Singh committed
552 553 554 555 556 557

  gfig_list = g_list_insert (gfig_list, gfig, n);

  return n;
}

558 559
static void
gfig_free (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
560 561 562
{
  g_assert (gfig != NULL);

563
  free_all_objs (gfig->obj_list);
564 565 566 567 568

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

Manish Singh's avatar
Manish Singh committed
569 570 571
  g_free (gfig);
}

572 573
static void
gfig_free_everything (GFigObj *gfig)
Manish Singh's avatar
Manish Singh committed
574 575 576
{
  g_assert (gfig != NULL);

577
  if (gfig->filename)
Manish Singh's avatar
Manish Singh committed
578
    {
579
      remove (gfig->filename);
Manish Singh's avatar
Manish Singh committed
580
    }
581
  gfig_free (gfig);
Manish Singh's avatar
Manish Singh committed
582 583
}

584 585
static void
gfig_list_free_all (void)
Manish Singh's avatar
Manish Singh committed
586
{
587
  g_list_foreach (gfig_list, (GFunc) gfig_free, NULL);
Manish Singh's avatar
Manish Singh committed
588 589 590 591
  g_list_free (gfig_list);
  gfig_list = NULL;
}

592
static void
593 594
gfig_list_load_one (const GimpDatafileData *file_data,
                    gpointer                user_data)
Manish Singh's avatar
Manish Singh committed
595
{
596
  GFigObj *gfig;
Manish Singh's avatar
Manish Singh committed
597

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

600
  if (gfig)
Manish Singh's avatar
Manish Singh committed
601
    {
602 603 604
      /* Read only ?*/
      if (access (file_data->filename, W_OK))
        gfig->obj_status |= GFIG_READONLY;
Manish Singh's avatar
Manish Singh committed
605

606 607 608
      gfig_list_insert (gfig);
    }
}
Manish Singh's avatar
Manish Singh committed
609

610 611 612 613 614 615
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
616

617 618 619
  gimp_datafiles_read_directories (path, G_FILE_TEST_EXISTS,
                                   gfig_list_load_one,
                                   NULL);
Manish Singh's avatar
Manish Singh committed
620

621
  if (! gfig_list)
Manish Singh's avatar
Manish Singh committed
622
    {
623 624
      GFigObj *gfig;

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

631
  pic_obj = current_obj = gfig_list->data;  /* set to first entry */
Manish Singh's avatar
Manish Singh committed
632 633
}

634 635
static GFigObj *
gfig_new (void)
Manish Singh's avatar
Manish Singh committed
636
{
637
  return g_new0 (GFigObj, 1);
Manish Singh's avatar
Manish Singh committed
638 639
}

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

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

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

693
      if (obj)
694 695 696
        {
          add_to_all_obj (gfig, obj);
        }
Manish Singh's avatar
Manish Singh committed
697 698 699
    }
}

700
static GFigObj *
701 702
gfig_load (const gchar *filename,
           const gchar *name)
Manish Singh's avatar
Manish Singh committed
703
{
704 705 706 707 708 709 710
  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
711 712 713
  g_assert (filename != NULL);

#ifdef DEBUG
714
  printf ("Loading %s (%s)\n", filename, name);
Manish Singh's avatar
Manish Singh committed
715 716 717 718 719
#endif /* DEBUG */

  fp = fopen (filename, "r");
  if (!fp)
    {
720
      g_message (_("Could not open '%s' for reading: %s"),
721
                  gimp_filename_to_utf8 (filename), g_strerror (errno));
Manish Singh's avatar
Manish Singh committed
722 723 724
      return NULL;
    }

725
  gfig = gfig_new ();
Manish Singh's avatar
Manish Singh committed
726

727 728
  gfig->name = g_strdup (name);
  gfig->filename = g_strdup (filename);
Manish Singh's avatar
Manish Singh committed
729 730 731 732 733 734 735 736


  /* HEADER
   * draw_name
   * version
   * obj_list
   */

737
  get_line (load_buf, MAX_LOAD_LINE, fp, 1);
Manish Singh's avatar
Manish Singh committed
738

739
  if (strncmp (GFIG_HEADER, load_buf, strlen (load_buf)))
Manish Singh's avatar
Manish Singh committed
740
    {
741 742
      g_message ("File '%s' is not a gfig file",
                  gimp_filename_to_utf8 (gfig->filename));
743
      return NULL;
Manish Singh's avatar
Manish Singh committed
744
    }
745

746 747 748 749
  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
750

751
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
752 753
  if (strncmp (load_buf, "Version: ", 9) == 0)
    gfig->version = g_ascii_strtod (load_buf + 9, NULL);
Manish Singh's avatar
Manish Singh committed
754

755 756
  get_line (load_buf, MAX_LOAD_LINE, fp, 0);
  sscanf (load_buf, "ObjCount: %d", &load_count);
Manish Singh's avatar
Manish Singh committed
757

758
  if (load_options (gfig, fp))
Manish Singh's avatar
Manish Singh committed
759
    {
760
      g_message ("File '%s' corrupt file - Line %d Option section incorrect",
761
                  gimp_filename_to_utf8 (filename), line_no);
762
      return NULL;
Manish Singh's avatar
Manish Singh committed
763 764
    }

765
  /*return (NULL);*/
Manish Singh's avatar
Manish Singh committed
766

767
  gfig_load_objs (gfig, load_count, fp);
Manish Singh's avatar
Manish Singh committed
768 769

  /* Check count ? */
770

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

773
  if (chk_count != load_count)
Manish Singh's avatar
Manish Singh committed
774
    {
775
      g_message ("File '%s' corrupt file - Line %d Object count to small",
776
                  gimp_filename_to_utf8 (filename), line_no);
777
      return NULL;
Manish Singh's avatar
Manish Singh committed
778 779
    }

780
  fclose (fp);
Manish Singh's avatar
Manish Singh committed
781

782
  if (!pic_obj)
Manish Singh's avatar
Manish Singh committed
783 784 785 786
    pic_obj = gfig;

  gfig->obj_status = GFIG_OK;

787
  return gfig;
Manish Singh's avatar
Manish Singh committed
788 789
}

790 791
static void
save_options (FILE *fp)
Manish Singh's avatar
Manish Singh committed
792 793
{
  /* Save options */
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
  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, "</OPTION