Commit 6461262d authored by Maurits Rijk's avatar Maurits Rijk
Browse files

More code clean-up for the gfig plug-in.

parent fb448d80
2003-10-19 Maurits Rijk <lpeek.mrijk@consunet.nl>
* plug-ins/gfig/Makefile.am
* plug-ins/gfig/gfig_dobject.c: new file. More code split from gfig.c
to a seperate file.
* plug-ins/gfig/gfig_dobject.h
* plug-ins/gfig/gfig_*.c
* plug-ins/gfig/gfig.[ch]: More clean-up
2003-10-19 Sven Neumann <sven@gimp.org>
 
* app/tools/gimpcropoptions.c
......@@ -20,7 +30,7 @@
 
2003-10-19 Maurits Rijk <lpeek.mrijk@consunet.nl>
 
* Makefile.am
* plug-ins/gfig/Makefile.am
* plug-ins/gfig/gfig_*.[ch]: new files split from gfig.c
* plug-ins/gfig/gfig.c: fix for #124158 (always show both the Paint
......@@ -39,7 +49,7 @@
 
2003-10-18 Sven Neumann <sven@gimp.org>
 
* app/widgets/gimpdockbook.c: removed the "Select Tab" menu and
* app/widgets/gimpdbockbook.c: removed the "Select Tab" menu and
all the evil hacks that were needed to make it work. Now that we
have an extra dockable menu, the standard menu from GTK+ is good
enough.
......@@ -21,6 +21,8 @@ gfig_SOURCES = \
gfig_bezier.h \
gfig_circle.c \
gfig_circle.h \
gfig_dobject.c \
gfig_dobject.h \
gfig_ellipse.c \
gfig_ellipse.h \
gfig_line.c \
......
......@@ -57,6 +57,7 @@
#include "gfig_arc.h"
#include "gfig_bezier.h"
#include "gfig_circle.h"
#include "gfig_dobject.h"
#include "gfig_ellipse.h"
#include "gfig_line.h"
#include "gfig_poly.h"
......@@ -176,7 +177,6 @@ static void do_gfig (void);
static void dialog_update_preview (void);
static void draw_grid (void);
static void draw_grid_clear (void);
static void toggle_show_image (void);
static void toggle_obj_type (GtkWidget *widget,
gpointer data);
......@@ -276,7 +276,7 @@ static gchar *gfig_path = NULL;
static GList *gfig_list = NULL;
gint line_no;
static gint obj_show_single = -1; /* -1 all >= 0 object number */
gint obj_show_single = -1; /* -1 all >= 0 object number */
/* Structures etc for the objects */
/* Points used to draw the object */
......@@ -301,8 +301,6 @@ typedef struct BrushDesc
GFigObj *current_obj;
static Dobject *operation_obj;
static GdkPoint *move_all_pnt; /* Point moving all from */
static GFigObj *pic_obj;
static DAllObjs *undo_table[MAX_UNDO];
gint need_to_scale;
......@@ -332,27 +330,16 @@ static GtkWidget *save_button;
/* Don't up just like BIGGG source files? */
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 DAllObjs * copy_all_objs (DAllObjs *objs);
static void setup_undo (void);
static GFigObj * gfig_load (const gchar *filename,
const gchar *name);
static void free_all_objs (DAllObjs * objs);
static void draw_objects (DAllObjs *objs, gint show_single);
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 new_obj_2edit (GFigObj *obj);
static gint load_options (GFigObj *gfig, FILE *fp);
......@@ -1110,11 +1097,12 @@ gfig_save_callbk (void)
save_options (fp);
objs = current_obj->obj_list;
while (objs)
for (objs = current_obj->obj_list; objs; objs = objs->next)
{
objs->obj->savefunc (objs->obj, fp);
objs = objs->next;
if (objs->obj->points)
{
objs->obj->savefunc (objs->obj, fp);
}
}
if (ferror (fp))
......@@ -1141,10 +1129,6 @@ file_selection_ok (GtkWidget *w,
filenamebuf = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
#ifdef DEBUG
g_print ("name selected '%s'\n", filenamebuf);
#endif /* DEBUG */
/* Get the name */
if (strlen (filenamebuf) == 0)
{
......@@ -4908,25 +4892,6 @@ get_num_radials (void)
return (gridsp - selvals.opts.gridspacing);
}
#define SQ_SIZE 8
static gint
inside_sqr (GdkPoint *cpnt,
GdkPoint *testpnt)
{
/* Return TRUE if testpnt is near cpnt */
gint16 x = cpnt->x;
gint16 y = cpnt->y;
gint16 tx = testpnt->x;
gint16 ty = testpnt->y;
#ifdef DEBUG
printf ("Testing if (%x,%x) is near (%x,%x)\n", tx, ty, x, y);
#endif /* DEBUG */
return (abs (x - tx) <= SQ_SIZE && abs (y - ty) < SQ_SIZE);
}
/* find_grid_pos - Given an x, y point return the grid position of it */
/* return the new position in the passed point */
......@@ -5184,7 +5149,7 @@ draw_sqr (GdkPoint *p)
/* Draw the grid on the screen
*/
static void
void
draw_grid_clear ()
{
/* wipe slate and start again */
......@@ -5596,7 +5561,7 @@ clear_undo (void)
gtk_widget_set_sensitive (undo_widget, FALSE);
}
static void
void
setup_undo (void)
{
/* Copy object list to undo buffer */
......@@ -5737,692 +5702,7 @@ reverse_pairs_list (gdouble *list,
}
}
/* Delete a list of points */
void
d_delete_dobjpoints (DobjPoints * pnts)
{
DobjPoints * next;
DobjPoints * pnt2del = pnts;
while (pnt2del)
{
next = pnt2del->next;
g_free (pnt2del);
pnt2del = next;
}
}
DobjPoints *
d_copy_dobjpoints (DobjPoints * pnts)
{
DobjPoints *ret = NULL;
DobjPoints *head = NULL;
DobjPoints *newpnt;
DobjPoints *pnt2copy = pnts;
while (pnt2copy)
{
newpnt = g_new0 (DobjPoints, 1);
newpnt->pnt.x = pnt2copy->pnt.x;
newpnt->pnt.y = pnt2copy->pnt.y;
if (!ret)
head = ret = newpnt;
else
{
head->next = newpnt;
head = newpnt;
}
pnt2copy = pnt2copy->next;
}
return ret;
}
static gint
scan_obj_points (DobjPoints *opnt,
GdkPoint *pnt)
{
while (opnt)
{
if (inside_sqr (&opnt->pnt, pnt))
{
opnt->found_me = TRUE;
return TRUE;
}
opnt->found_me = FALSE;
opnt = opnt->next;
}
return FALSE;
}
static Dobject *
get_nearest_objs (GFigObj *obj,
GdkPoint *pnt)
{
/* Nearest object to given point or NULL */
DAllObjs *all;
Dobject *test_obj;
gint count = 0;
if (!obj)
return NULL;
all = obj->obj_list;
while (all)
{
test_obj = all->obj;
if (count == obj_show_single || obj_show_single == -1)
if (scan_obj_points (test_obj->points, pnt))
{
return test_obj;
}
all = all->next;
count++;
}
return NULL;
}
static void
scale_obj_points (DobjPoints *opnt,
gdouble scale_x,
gdouble scale_y)
{
while (opnt)
{
opnt->pnt.x = (gint) (opnt->pnt.x * scale_x);
opnt->pnt.y = (gint) (opnt->pnt.y * scale_y);
opnt = opnt->next;
}
}
static void
remove_obj_from_list (GFigObj *obj,
Dobject *del_obj)
{
/* Nearest object to given point or NULL */
DAllObjs *all;
DAllObjs *prev_all = NULL;
g_assert (del_obj != NULL);
all = obj->obj_list;
while (all)
{
if (all->obj == del_obj)
{
/* Found the one to delete */
#ifdef DEBUG
printf ("Found the one to delete\n");
#endif /* DEBUG */
if (prev_all)
prev_all->next = all->next;
else
obj->obj_list = all->next;
/* Draw obj (which will actually undraw it! */
del_obj->drawfunc (del_obj);
free_one_obj (del_obj);
g_free (all);
if (obj_show_single != -1)
{
/* We've just deleted the only visible one */
draw_grid_clear ();
obj_show_single = -1; /* Show all again */
}
return;
}
prev_all = all;
all = all->next;
}
g_warning (_("Hey where has the object gone ?"));
}
static DobjPoints *
get_diffs (Dobject *obj,
gint16 *xdiff,
gint16 *ydiff,
GdkPoint *to_pnt)
{
DobjPoints *spnt;
g_assert (obj != NULL);
for (spnt = obj->points; spnt; spnt = spnt->next)
{
if (spnt->found_me)
{
*xdiff = spnt->pnt.x - to_pnt->x;
*ydiff = spnt->pnt.y - to_pnt->y;
return spnt;
}
}
return NULL;
}
static void
update_pnts (Dobject *obj,
gint16 xdiff,
gint16 ydiff)
{
DobjPoints *spnt;
g_assert (obj != NULL);
/* Update all pnts */
for (spnt = obj->points; spnt; spnt = spnt->next)
{
spnt->pnt.x -= xdiff;
spnt->pnt.y -= ydiff;
}
}
static void
do_move_all_obj (GdkPoint *to_pnt)
{
/* Move all objects in one go */
/* Undraw/then draw in new pos */
gint16 xdiff = move_all_pnt->x - to_pnt->x;
gint16 ydiff = move_all_pnt->y - to_pnt->y;
if (xdiff || ydiff)
{
DAllObjs *all;
for (all = current_obj->obj_list; all; all = all->next)
{
Dobject *obj = all->obj;
/* undraw ! */
draw_one_obj (obj);
update_pnts (obj, xdiff, ydiff);
/* Draw in new pos */
draw_one_obj (obj);
}
*move_all_pnt = *to_pnt;
}
}
static void
do_move_obj (Dobject *obj,
GdkPoint *to_pnt)
{
/* Move the whole line - undraw the line to start with */
/* Then draw in new pos */
gint16 xdiff = 0;
gint16 ydiff = 0;
get_diffs (obj, &xdiff, &ydiff, to_pnt);
if (xdiff || ydiff)
{
/* undraw ! */
draw_one_obj (obj);
update_pnts (obj, xdiff, ydiff);
/* Draw in new pos */
draw_one_obj (obj);
}
}
static void
do_move_obj_pnt (Dobject *obj,
GdkPoint *to_pnt)
{
/* Move the whole line - undraw the line to start with */
/* Then draw in new pos */
DobjPoints *spnt;
gint16 xdiff = 0;
gint16 ydiff = 0;
spnt = get_diffs (obj, &xdiff, &ydiff, to_pnt);
if ((!xdiff && !ydiff) || !spnt)
return;
/* undraw ! */
draw_one_obj (obj);
spnt->pnt.x = spnt->pnt.x - xdiff;
spnt->pnt.y = spnt->pnt.y - ydiff;
/* Draw in new pos */
draw_one_obj (obj);
}
/* copy objs */
static DAllObjs *
copy_all_objs (DAllObjs *objs)
{
DAllObjs *nobj;
DAllObjs *new_all_objs = NULL;
DAllObjs *ret = NULL;
while (objs)
{
nobj = g_new0 (DAllObjs, 1);
if (!ret)
{
ret = new_all_objs = nobj;
}
else
{
new_all_objs->next = nobj;
new_all_objs = nobj;
}
nobj->obj = (Dobject *) objs->obj->copyfunc (objs->obj);
objs = objs->next;
}
return ret;
}
/* Screen refresh */
static void
draw_one_obj (Dobject * obj)
{
obj->drawfunc (obj);
}
static void
draw_objects (DAllObjs *objs,
gint show_single)
{
/* Show_single - only one object to draw Unless shift
* is down in which case show all.
*/
gint count = 0;
while (objs)
{
if (!show_single || count == obj_show_single || obj_show_single == -1)
draw_one_obj (objs->obj);
objs = objs->next;
count++;
}
}
static void
prepend_to_all_obj (GFigObj *fobj,
DAllObjs *nobj)
{
DAllObjs *cobj;
setup_undo (); /* Remember ME */
if (!fobj->obj_list)
{
fobj->obj_list = nobj;
return;
}
cobj = fobj->obj_list;
while (cobj->next)
{
cobj = cobj->next;
}
cobj->next = nobj;
}
void
add_to_all_obj (GFigObj *fobj,
Dobject *obj)
{
DAllObjs *nobj;
nobj = g_new0 (DAllObjs, 1);
nobj->obj = obj;
if (need_to_scale)
scale_obj_points (obj->points, scale_x_factor, scale_y_factor);
prepend_to_all_obj (fobj, nobj);
}
static void
object_operation_start (GdkPoint *pnt,
gint shift_down)
{
Dobject *new_obj;
/* Find point in given object list */
operation_obj = get_nearest_objs (current_obj, pnt);
/* Special case if shift down && move obj then moving all objs */
if (shift_down && selvals.otype == MOVE_OBJ)
{
move_all_pnt = g_malloc0 (sizeof (*move_all_pnt));
*move_all_pnt = *pnt; /* Structure copy */
setup_undo ();
return;
}
if (!operation_obj)
return;/* None to work on */
setup_undo ();
switch (selvals.otype)
{
case MOVE_OBJ:
if (operation_obj->type == BEZIER)
{
d_draw_bezier (operation_obj);
tmp_bezier = operation_obj;
d_draw_bezier (operation_obj);
}
break;
case MOVE_POINT:
if (operation_obj->type == BEZIER)
{
d_draw_bezier (operation_obj);
tmp_bezier = operation_obj;
d_draw_bezier (operation_obj);
}
/* If shift is down the break into sep lines */
if ((operation_obj->type == POLY
|| operation_obj->type == STAR)
&& shift_down)
{
switch (operation_obj->type)
{
case POLY:
d_poly2lines (operation_obj);
break;
case STAR:
d_star2lines (operation_obj);
break;
default:
break;
}
/* Re calc which object point we are lookin at */
scan_obj_points (operation_obj->points, pnt);
}
break;
case COPY_OBJ:
/* Copy the "operation object" */
/* Then bung us into "copy/move" mode */
#ifdef DEBUG
printf ("In copy obj\n");
#endif /* DEBUG */
new_obj = (Dobject *) operation_obj->copyfunc (operation_obj);
if (new_obj)
{
scan_obj_points (new_obj->points, pnt);
add_to_all_obj (current_obj, new_obj);
operation_obj = new_obj;
selvals.otype = MOVE_COPY_OBJ;
new_obj->drawfunc (new_obj);
}
break;
case DEL_OBJ:
remove_obj_from_list (current_obj, operation_obj);
break;
case MOVE_COPY_OBJ: /* Never when button down */
default:
g_warning ("Internal error selvals.otype object operation start");
break;
}
}