From ef1db26bf19a1ff20005345e3c99bbc0957490c8 Mon Sep 17 00:00:00 2001 From: David Odin Date: Sun, 14 Nov 2004 15:55:10 +0000 Subject: [PATCH] plug-ins/gfig/gfig-dialog.c plug-ins/gfig/gfig-dialog.h * plug-ins/gfig/gfig-dialog.c * plug-ins/gfig/gfig-dialog.h * plug-ins/gfig/gfig-dobject.c * plug-ins/gfig/gfig-dobject.h * plug-ins/gfig/gfig-types.h * plug-ins/gfig/gfig.c * plug-ins/gfig/gfig.h: replace the crappy DAllObjs struct by a GList. Makes the code cleaner and less error prone. --- ChangeLog | 11 ++ plug-ins/gfig/gfig-dialog.c | 225 +++++++++++------------------------ plug-ins/gfig/gfig-dialog.h | 12 +- plug-ins/gfig/gfig-dobject.c | 142 +++++++--------------- plug-ins/gfig/gfig-dobject.h | 57 ++++----- plug-ins/gfig/gfig-types.h | 1 - plug-ins/gfig/gfig.c | 24 ++-- plug-ins/gfig/gfig.h | 6 +- 8 files changed, 171 insertions(+), 307 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4fae7690aa..7225ddf168 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2004-11-14 DindinX + + * plug-ins/gfig/gfig-dialog.c + * plug-ins/gfig/gfig-dialog.h + * plug-ins/gfig/gfig-dobject.c + * plug-ins/gfig/gfig-dobject.h + * plug-ins/gfig/gfig-types.h + * plug-ins/gfig/gfig.c + * plug-ins/gfig/gfig.h: replace the crappy DAllObjs struct by a GList. + Makes the code cleaner and less error prone. + 2004-11-14 Sven Neumann * plug-ins/pagecurl/pagecurl.c: applied a patch from Karine Proot diff --git a/plug-ins/gfig/gfig-dialog.c b/plug-ins/gfig/gfig-dialog.c index 86f5e9873c..b95692c3bd 100644 --- a/plug-ins/gfig/gfig-dialog.c +++ b/plug-ins/gfig/gfig-dialog.c @@ -939,42 +939,25 @@ static void raise_selected_obj_to_top (GtkWidget *widget, gpointer data) { - DAllObjs *entry; - DAllObjs *prev_entry = NULL; - DAllObjs *found_entry = NULL; - if (!gfig_context->selected_obj) return; - entry = gfig_context->current_obj->obj_list; - - while (entry) + if (g_list_find (gfig_context->current_obj->obj_list, + gfig_context->selected_obj)) { - if (entry->obj == gfig_context->selected_obj) - { - /* Found the entry to raise, remove it from list */ - found_entry = entry; - - if (prev_entry) - prev_entry->next = entry->next; - else - gfig_context->current_obj->obj_list = entry->next; - } - - prev_entry = entry; - entry = entry->next; + gfig_context->current_obj->obj_list = + g_list_remove (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); + gfig_context->current_obj->obj_list = + g_list_append (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); } - - if (!found_entry) + else { g_message ("Trying to raise object that does not exist."); return; } - /* tack found entry onto end of list */ - prev_entry->next = found_entry; - found_entry->next = NULL; - gfig_paint_callback (); } @@ -982,46 +965,25 @@ static void lower_selected_obj_to_bottom (GtkWidget *widget, gpointer data) { - DAllObjs *entry; - DAllObjs *prev_entry = NULL; - DAllObjs *found_entry = NULL; - if (!gfig_context->selected_obj) return; - entry = gfig_context->current_obj->obj_list; - - while (entry) + if (g_list_find (gfig_context->current_obj->obj_list, + gfig_context->selected_obj)) { - if (entry->obj == gfig_context->selected_obj) - { - /* Found the entry to lower, remove it from list */ - found_entry = entry; - - if (prev_entry) - { - prev_entry->next = found_entry->next; - break; - } - else - /* it's already at the bottom */ - return; - } - - prev_entry = entry; - entry = entry->next; + gfig_context->current_obj->obj_list = + g_list_remove (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); + gfig_context->current_obj->obj_list = + g_list_prepend (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); } - - if (!found_entry) + else { - g_message ("Trying to raise object that does not exist."); + g_message ("Trying to lower object that does not exist."); return; } - /* stick found entry into beginning of list */ - found_entry->next = gfig_context->current_obj->obj_list; - gfig_context->current_obj->obj_list = found_entry; - gfig_paint_callback (); } @@ -1029,46 +991,25 @@ static void raise_selected_obj (GtkWidget *widget, gpointer data) { - DAllObjs *entry; - DAllObjs *prev_entry = NULL; - DAllObjs *following_entry = NULL; - DAllObjs *found_entry = NULL; - if (!gfig_context->selected_obj) return; - entry = gfig_context->current_obj->obj_list; - - while (entry) + if (g_list_find (gfig_context->current_obj->obj_list, + gfig_context->selected_obj)) { - if (entry->obj == gfig_context->selected_obj) - { - /* Found the entry to raise, remove it from list */ - found_entry = entry; + int position; - following_entry = found_entry->next; - - /* see if already on top */ - if (!following_entry) - return; - - if (prev_entry) - prev_entry->next = following_entry; - else - gfig_context->current_obj->obj_list = following_entry; - - found_entry->next = following_entry->next; - - following_entry->next = found_entry; - - break; - } - - prev_entry = entry; - entry = entry->next; + position = g_list_index (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); + gfig_context->current_obj->obj_list = + g_list_remove (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); + gfig_context->current_obj->obj_list = + g_list_insert (gfig_context->current_obj->obj_list, + gfig_context->selected_obj, + position + 1); } - - if (!found_entry) + else { g_message ("Trying to raise object that does not exist."); return; @@ -1082,49 +1023,27 @@ static void lower_selected_obj (GtkWidget *widget, gpointer data) { - DAllObjs *entry; - DAllObjs *prev_prev_entry = NULL; - DAllObjs *prev_entry = NULL; - DAllObjs *following_entry = NULL; - DAllObjs *found_entry = NULL; - if (!gfig_context->selected_obj) return; - entry = gfig_context->current_obj->obj_list; - - while (entry) + if (g_list_find (gfig_context->current_obj->obj_list, + gfig_context->selected_obj)) { - if (entry->obj == gfig_context->selected_obj) - { - /* Found the entry to lower, remove it from list */ - found_entry = entry; + int position; - following_entry = found_entry->next; - - /* see if already on bottom */ - if (!prev_entry) - return; - - prev_entry->next = following_entry; - found_entry->next = prev_entry; - - if (prev_prev_entry) - prev_prev_entry->next = found_entry; - else - gfig_context->current_obj->obj_list = found_entry; - - break; - } - - prev_prev_entry = prev_entry; - prev_entry = entry; - entry = entry->next; + position = g_list_index (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); + gfig_context->current_obj->obj_list = + g_list_remove (gfig_context->current_obj->obj_list, + gfig_context->selected_obj); + gfig_context->current_obj->obj_list = + g_list_insert (gfig_context->current_obj->obj_list, + gfig_context->selected_obj, + MAX (0, position - 1)); } - - if (!found_entry) + else { - g_message ("Trying to raise object that does not exist."); + g_message ("Trying to lower object that does not exist."); return; } @@ -1466,39 +1385,37 @@ save_file_chooser_response (GtkFileChooser *chooser, static Dobject * gfig_select_obj_by_number (gint count) { - DAllObjs *objs = gfig_context->current_obj->obj_list; - gint k = 0; + GList *objs; + Dobject *object = NULL; + gint k; gfig_context->selected_obj = NULL; - while (objs) + for (objs = gfig_context->current_obj->obj_list, k = 0; + objs; + objs = g_list_next (objs), k++) { if (k == obj_show_single) { - gfig_context->selected_obj = objs->obj; - gfig_context->current_style = &objs->obj->style; - gfig_style_set_context_from_style (&objs->obj->style); + object = objs->data; + gfig_context->selected_obj = object; + gfig_context->current_style = &object->style; + gfig_style_set_context_from_style (&object->style); break; } - - objs = objs->next; - - k++; } - return objs->obj; + return object; } static void select_button_clicked (gint type) { - gint count = 0; - DAllObjs *objs; + gint count = 0; if (gfig_context->current_obj) { - for (objs = gfig_context->current_obj->obj_list; objs; objs = objs->next) - count++; + count = g_list_length (gfig_context->current_obj->obj_list); } switch (type) @@ -1523,8 +1440,6 @@ select_button_clicked (gint type) break; } - objs = gfig_context->current_obj->obj_list; - if (obj_show_single >= 0) gfig_select_obj_by_number (obj_show_single); @@ -1777,19 +1692,20 @@ paint_layer_fill (void) void gfig_paint_callback (void) { - DAllObjs *objs; - gint layer_count = 0; - gchar buf[128]; - gint count; - gint ccount = 0; - Style *style0; + GList *objs; + gint layer_count = 0; + gchar buf[128]; + gint count; + gint ccount = 0; + Style *style0; + Dobject *object; if (!gfig_context->enable_repaint || !gfig_context->current_obj) return; objs = gfig_context->current_obj->obj_list; - count = gfig_obj_counts (objs); + count = g_list_length (objs); gimp_drawable_fill (gfig_context->drawable_id, GIMP_TRANSPARENT_FILL); @@ -1800,17 +1716,18 @@ gfig_paint_callback (void) { if (ccount == obj_show_single || obj_show_single == -1) { + object = objs->data; sprintf (buf, _("Gfig layer %d"), layer_count++); - gfig_style_apply (&objs->obj->style); + gfig_style_apply (&object->style); - objs->obj->class->paintfunc (objs->obj); + object->class->paintfunc (object); gimp_selection_none (gfig_context->image_id); } - objs = objs->next; + objs = g_list_next (objs); ccount++; } diff --git a/plug-ins/gfig/gfig-dialog.h b/plug-ins/gfig/gfig-dialog.h index 898badf314..279f7eeecc 100644 --- a/plug-ins/gfig/gfig-dialog.h +++ b/plug-ins/gfig/gfig-dialog.h @@ -31,13 +31,13 @@ #define RESPONSE_SAVE 3 #define RESPONSE_PAINT 4 -gint undo_water_mark; /* Last slot filled in -1 = no undo */ -DAllObjs *undo_table[MAX_UNDO]; +gint undo_water_mark; /* Last slot filled in -1 = no undo */ +GList *undo_table[MAX_UNDO]; -gint gfig_dialog (void); -void update_options (GFigObj *old_obj); +gint gfig_dialog (void); +void update_options (GFigObj *old_obj); -void tool_option_page_update (GtkWidget *button, - GtkWidget *notebook); +void tool_option_page_update (GtkWidget *button, + GtkWidget *notebook); #endif /* __GFIG_DIALOG_H__ */ diff --git a/plug-ins/gfig/gfig-dobject.c b/plug-ins/gfig/gfig-dobject.c index 16b264d0da..ab16935972 100644 --- a/plug-ins/gfig/gfig-dobject.c +++ b/plug-ins/gfig/gfig-dobject.c @@ -321,16 +321,16 @@ get_nearest_objs (GFigObj *obj, GdkPoint *pnt) { /* Nearest object to given point or NULL */ - DAllObjs *all; - Dobject *test_obj; - gint count = 0; + GList *all; + Dobject *test_obj; + gint count = 0; if (!obj) return NULL; - for (all = obj->obj_list; all; all = all->next) + for (all = obj->obj_list; all; all = g_list_next (all)) { - test_obj = all->obj; + test_obj = all->data; if (count == obj_show_single || obj_show_single == -1) if (scan_obj_points (test_obj->points, pnt)) @@ -558,41 +558,26 @@ remove_obj_from_list (GFigObj *obj, Dobject *del_obj) { /* Nearest object to given point or NULL */ - DAllObjs *entry; - DAllObjs *prev_entry = NULL; g_assert (del_obj != NULL); - entry = obj->obj_list; - - while (entry) + if (g_list_find (obj->obj_list, del_obj)) { - if (entry->obj == del_obj) + del_obj->class->drawfunc (del_obj); + + obj->obj_list = g_list_remove (obj->obj_list, del_obj); + + free_one_obj (del_obj); + + if (obj_show_single != -1) { - /* Found the one to delete */ - if (prev_entry) - prev_entry->next = entry->next; - else - obj->obj_list = entry->next; - - /* Draw obj (which will actually undraw it! */ - del_obj->class->drawfunc (del_obj); - - free_one_obj (del_obj); - g_free (entry); - - if (obj_show_single != -1) - { - /* We've just deleted the only visible one */ - draw_grid_clear (); - obj_show_single = -1; /* Show entry again */ - } - return; + /* We've just deleted the only visible one */ + draw_grid_clear (); + obj_show_single = -1; /* Show entry again */ } - prev_entry = entry; - entry = entry->next; } - g_warning (_("Hey where has the object gone ?")); + else + g_warning (_("Hey where has the object gone ?")); } static void @@ -605,11 +590,11 @@ do_move_all_obj (GdkPoint *to_pnt) if (xdiff || ydiff) { - DAllObjs *all; + GList *all; for (all = gfig_context->current_obj->obj_list; all; all = all->next) { - Dobject *obj = all->obj; + Dobject *obj = all->data; /* undraw ! */ draw_one_obj (obj); @@ -685,33 +670,22 @@ do_move_obj_pnt (Dobject *obj, } /* copy objs */ -DAllObjs * -copy_all_objs (DAllObjs *objs) +GList * +copy_all_objs (GList *objs) { - DAllObjs *nobj; - DAllObjs *new_all_objs = NULL; - DAllObjs *ret = NULL; + GList *new_all_objs = NULL; while (objs) { - nobj = g_new0 (DAllObjs, 1); + Dobject *object = objs->data; + Dobject *new_object = (Dobject *) object->class->copyfunc (object); - if (!ret) - { - ret = new_all_objs = nobj; - } - else - { - new_all_objs->next = nobj; - new_all_objs = nobj; - } - - nobj->obj = (Dobject *) objs->obj->class->copyfunc (objs->obj); + new_all_objs = g_list_append (new_all_objs, new_object); objs = objs->next; } - return ret; + return new_all_objs; } /* Screen refresh */ @@ -722,8 +696,8 @@ draw_one_obj (Dobject * obj) } void -draw_objects (DAllObjs *objs, - gint show_single) +draw_objects (GList *objs, + gboolean show_single) { /* Show_single - only one object to draw Unless shift * is down in which case show all. @@ -734,35 +708,20 @@ draw_objects (DAllObjs *objs, while (objs) { if (!show_single || count == obj_show_single || obj_show_single == -1) - draw_one_obj (objs->obj); + draw_one_obj (objs->data); - objs = objs->next; + objs = g_list_next (objs); count++; } } void -prepend_to_all_obj (GFigObj *fobj, - DAllObjs *nobj) +prepend_to_all_obj (GFigObj *fobj, + GList *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; + fobj->obj_list = g_list_concat (fobj->obj_list, nobj); } static void @@ -782,11 +741,9 @@ void add_to_all_obj (GFigObj *fobj, Dobject *obj) { - DAllObjs *nobj; + GList *nobj = NULL; - nobj = g_new0 (DAllObjs, 1); - - nobj->obj = obj; + nobj = g_list_append (nobj, obj); if (need_to_scale) scale_obj_points (obj->points, scale_x_factor, scale_y_factor); @@ -981,18 +938,10 @@ free_one_obj (Dobject *obj) } void -free_all_objs (DAllObjs * objs) +free_all_objs (GList * objs) { - /* Free all objects */ - DAllObjs * next; - - while (objs) - { - free_one_obj (objs->obj); - next = objs->next; - g_free (objs); - objs = next; - } + g_list_foreach (objs, (GFunc)free_one_obj, NULL); + g_list_free (objs); } gchar * @@ -1070,7 +1019,7 @@ setup_undo (void) int loop; /* the little one in the bed said "roll over".. */ if (undo_table[0]) - free_one_obj (undo_table[0]->obj); + free_one_obj (undo_table[0]->data); for (loop = 0; loop < undo_water_mark; loop++) { undo_table[loop] = undo_table[loop + 1]; @@ -1125,14 +1074,3 @@ new_obj_2edit (GFigObj *obj) } } -gint -gfig_obj_counts (DAllObjs *objs) -{ - gint count = 0; - - for (; objs; objs = objs->next) - count++; - - return count; -} - diff --git a/plug-ins/gfig/gfig-dobject.h b/plug-ins/gfig/gfig-dobject.h index 4bd5a817fa..1d268482ed 100644 --- a/plug-ins/gfig/gfig-dobject.h +++ b/plug-ins/gfig/gfig-dobject.h @@ -63,12 +63,6 @@ struct _Dobject gint style_no; /* style index of this specific object */ }; -struct _DAllObjs -{ - DAllObjs *next; - Dobject *obj; /* Object on list */ -}; - /* States of the object */ #define GFIG_OK 0x0 #define GFIG_MODIFIED 0x1 @@ -77,41 +71,42 @@ struct _DAllObjs extern Dobject *obj_creating; extern Dobject *tmp_line; -void d_pnt_add_line (Dobject *obj, - gint x, - gint y, - gint pos); -DobjPoints *new_dobjpoint (gint x, gint y); -void do_save_obj (Dobject *obj, - GString *to); +void d_pnt_add_line (Dobject *obj, + gint x, + gint y, + gint pos); -DobjPoints *d_copy_dobjpoints (DobjPoints * pnts); -void free_one_obj (Dobject *obj); -void d_delete_dobjpoints (DobjPoints * pnts); -void object_update (GdkPoint *pnt); -DAllObjs *copy_all_objs (DAllObjs *objs); -void draw_objects (DAllObjs *objs, gint show_single); +DobjPoints *new_dobjpoint (gint x, + gint y); +void do_save_obj (Dobject *obj, + GString *to); -Dobject *d_load_object (gchar *desc, - FILE *fp); +DobjPoints *d_copy_dobjpoints (DobjPoints *pnts); +void free_one_obj (Dobject *obj); +void d_delete_dobjpoints (DobjPoints *pnts); +void object_update (GdkPoint *pnt); +GList *copy_all_objs (GList *objs); +void draw_objects (GList *objs, + gboolean show_single); -Dobject *d_new_object (DobjType type, - gint x, - gint y); +Dobject *d_load_object (gchar *desc, + FILE *fp); -void d_save_object (Dobject *obj, - GString *string); +Dobject *d_new_object (DobjType type, + gint x, + gint y); -void free_all_objs (DAllObjs *objs); +void d_save_object (Dobject *obj, + GString *string); -void clear_undo (void); +void free_all_objs (GList *objs); -void new_obj_2edit (GFigObj *obj); +void clear_undo (void); -gint gfig_obj_counts (DAllObjs *objs); +void new_obj_2edit (GFigObj *obj); -void gfig_init_object_classes (void); +void gfig_init_object_classes (void); #endif /* __GFIG_DOBJECT_H__ */ diff --git a/plug-ins/gfig/gfig-types.h b/plug-ins/gfig/gfig-types.h index 596369a124..caa142ad3b 100644 --- a/plug-ins/gfig/gfig-types.h +++ b/plug-ins/gfig/gfig-types.h @@ -109,7 +109,6 @@ typedef enum } DobjType; typedef struct _GFigObj GFigObj; -typedef struct _DAllObjs DAllObjs; typedef struct _Dobject Dobject; typedef struct _Style Style; diff --git a/plug-ins/gfig/gfig.c b/plug-ins/gfig/gfig.c index 972afb6aeb..5744e742be 100644 --- a/plug-ins/gfig/gfig.c +++ b/plug-ins/gfig/gfig.c @@ -481,7 +481,7 @@ gfig_load (const gchar *filename, /* Check count ? */ - chk_count = gfig_obj_counts (gfig->obj_list); + chk_count = g_list_length (gfig->obj_list); if (chk_count != load_count) { @@ -658,8 +658,8 @@ load_options (GFigObj *gfig, GString * gfig_save_as_string (void) { - DAllObjs *objs; - gint count = 0; + GList *objs; + gint count; gchar buf[G_ASCII_DTOSTR_BUF_SIZE]; gchar conv_buf[MAX_LOAD_LINE * 3 + 1]; GString *string; @@ -673,7 +673,7 @@ gfig_save_as_string (void) gfig_context->current_obj->version)); objs = gfig_context->current_obj->obj_list; - count = gfig_obj_counts (objs); + count = g_list_length (objs); g_string_append_printf (string, "ObjCount: %d\n", count); @@ -681,16 +681,20 @@ gfig_save_as_string (void) gfig_save_styles (string); - for (objs = gfig_context->current_obj->obj_list; objs; objs = objs->next) + for (objs = gfig_context->current_obj->obj_list; + objs; + objs = g_list_next (objs)) { - gfig_save_obj_start (objs->obj, string); + Dobject *object = objs->data; - gfig_save_style (&objs->obj->style, string); + gfig_save_obj_start (object, string); - if (objs->obj->points) - d_save_object (objs->obj, string); + gfig_save_style (&object->style, string); - gfig_save_obj_end (objs->obj, string); + if (object->points) + d_save_object (object, string); + + gfig_save_obj_end (object, string); } return string; diff --git a/plug-ins/gfig/gfig.h b/plug-ins/gfig/gfig.h index 132648b333..5f3d18cdf7 100644 --- a/plug-ins/gfig/gfig.h +++ b/plug-ins/gfig/gfig.h @@ -101,7 +101,7 @@ struct _GFigObj 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 */ + GList *obj_list; /* Objects that make up this list */ gint obj_status; /* See above for possible values */ GtkWidget *list_item; GtkWidget *label_widget; @@ -188,7 +188,7 @@ GtkWidget *num_sides_widget (gchar *d_title, void setup_undo (void); void draw_grid_clear (void); void prepend_to_all_obj (GFigObj *fobj, - DAllObjs *nobj); + GList *nobj); void gfig_draw_arc (gint x, gint y, @@ -223,7 +223,7 @@ void save_options (GString *string); GString *gfig_save_as_string (void); gboolean gfig_save_as_parasite (void); GFigObj *gfig_load_from_parasite (void); -GFigObj * gfig_new (void); +GFigObj *gfig_new (void); void gfig_save_callbk (void); void paint_layer_fill (void);