Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2002:
[Freeciv-Dev] Re: (PR#2410) Tileset switching - another GTK problem
Home

[Freeciv-Dev] Re: (PR#2410) Tileset switching - another GTK problem

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rf13@xxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#2410) Tileset switching - another GTK problem
From: "Jason Short via RT" <rt@xxxxxxxxxxxxxx>
Date: Tue, 26 Nov 2002 02:09:59 -0800
Reply-to: rt@xxxxxxxxxxxxxx

Raimar Falke via RT wrote:
> On Tue, Nov 26, 2002 at 01:11:26AM -0800, Jason Short via RT wrote:
> 
>>[rfalke - Mon Nov 25 22:03:59 2002]:
>>
>>
>>>Start with trident. Switch to isotrident. Refresh.
>>
>>Fix attached.
> 
> 
>>+  /* We have to force a redraw of the units.  And we explicitly have
>>+   * to force a redraw of the focus unit, which is normally only
>>+   * redrawn when the focus changes. We also have to force the 'more'
> 
> 
>>+   * arrow to go away, both by expicitly hiding it and telling it to
> 
> 
> explicitly

Oops.  New patch attached.

>> void tileset_changed(void)
>> {
>>   reset_city_dialogs();
>>+  reset_unit_table();
>> }
> 
> 
> reset is IMHO a bit too generic for these functions. But this only minor.

Suggestions?

> Have you audited the other issues (grep TILE_WIDTH client/gui-gtk/*.c)?

TILE_WIDTH doesn't do it: it takes UNIT_WIDTH to find the remaining 
problems.  Fortunately I know this now :-).

There is a lot of stuff in mapview (presumably harmless), plus the 
citydlg and unit table issues already covered.  Finally there is the 
help dialog, which can be fixed by calling popdown_help_dialog() 
function (except this function doesn't exist yet...although it is 
probably also needed by PR#533).  I'd rather provide a patch for that 
separately.

I assume the other clients are similar, although except for GTK2 I 
haven't looked at them much.  I will take care of XAW shortly, though.

There may be other minor issues here and there in the miscellaneous 
sprites the tileset provides.

jason

Index: client/gui-gtk/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.c,v
retrieving revision 1.120
diff -u -r1.120 gui_main.c
--- client/gui-gtk/gui_main.c   2002/11/14 09:14:53     1.120
+++ client/gui-gtk/gui_main.c   2002/11/26 09:09:04
@@ -119,6 +119,7 @@
 GtkWidget *unit_info_label;
 GtkWidget *unit_info_frame;
 
+static GtkWidget *unit_pixmap_table;
 static GtkWidget *unit_pixmap;
 static GtkWidget *unit_pixmap_button;
 static GtkWidget *unit_below_pixmap[MAX_NUM_UNITS_BELOW];
@@ -454,6 +455,105 @@
 }
 
 /**************************************************************************
+  Called to build the unit_below pixmap table.  This is the table on the
+  left of the screen that shows all of the inactive units in the current
+  tile.
+
+  It may be called again if the tileset changes.
+**************************************************************************/
+static void populate_unit_pixmap_table(void)
+{
+  int i;
+  GtkWidget *table = unit_pixmap_table;
+ 
+  /* 135 below is rough value (could be more intelligent) --dwp */
+  num_units_below = 135 / (int) NORMAL_TILE_WIDTH;
+  num_units_below = CLIP(1, num_units_below, MAX_NUM_UNITS_BELOW);
+
+  gtk_table_resize(GTK_TABLE(table), 2, num_units_below);
+
+  /* Note, we ref this and other widgets here so that we can unref them
+   * in reset_unit_table. */
+  unit_pixmap = gtk_pixcomm_new(root_window, UNIT_TILE_WIDTH, 
+                                UNIT_TILE_HEIGHT);
+  gtk_widget_ref(unit_pixmap);
+  gtk_pixcomm_clear(GTK_PIXCOMM(unit_pixmap), TRUE);
+  unit_pixmap_button = gtk_event_box_new();
+  gtk_widget_ref(unit_pixmap_button);
+  gtk_container_add(GTK_CONTAINER(unit_pixmap_button), unit_pixmap);
+  gtk_table_attach_defaults(GTK_TABLE(table), unit_pixmap_button, 0, 1, 0, 1);
+  gtk_signal_connect(GTK_OBJECT(unit_pixmap_button), "button_press_event",
+                     GTK_SIGNAL_FUNC(select_unit_pixmap_callback), 
+                     GINT_TO_POINTER(-1));
+
+  for(i = 0; i < num_units_below; i++) {
+    unit_below_pixmap[i] = gtk_pixcomm_new(root_window, UNIT_TILE_WIDTH,
+                                           UNIT_TILE_HEIGHT);
+    gtk_widget_ref(unit_below_pixmap[i]);
+    unit_below_pixmap_button[i] = gtk_event_box_new();
+    gtk_widget_ref(unit_below_pixmap_button[i]);
+    gtk_container_add(GTK_CONTAINER(unit_below_pixmap_button[i]),
+                      unit_below_pixmap[i]);
+    gtk_signal_connect(GTK_OBJECT(unit_below_pixmap_button[i]),
+                       "button_press_event",
+                        GTK_SIGNAL_FUNC(select_unit_pixmap_callback),
+                        GINT_TO_POINTER(i));
+      
+    gtk_table_attach_defaults(GTK_TABLE(table), unit_below_pixmap_button[i],
+                              i, i + 1, 1, 2);
+    gtk_widget_set_usize(unit_below_pixmap[i],
+                         UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
+    gtk_pixcomm_clear(GTK_PIXCOMM(unit_below_pixmap[i]), TRUE);
+  }
+
+  more_arrow_pixmap = gtk_pixmap_new(sprites.right_arrow->pixmap, NULL);
+  gtk_widget_ref(more_arrow_pixmap);
+  gtk_pixmap_set_build_insensitive(GTK_PIXMAP(more_arrow_pixmap), FALSE);
+  gtk_table_attach_defaults(GTK_TABLE(table), more_arrow_pixmap, 4, 5, 1, 2);
+
+  gtk_widget_show_all(table);
+}
+
+/**************************************************************************
+  Called when the tileset is changed to reset the unit pixmap table.
+**************************************************************************/
+void reset_unit_table(void)
+{
+  int i;
+
+  /* Unreference all of the widgets that we're about to reallocate, thus
+   * avoiding a memory leak. Remove them from the container first, just
+   * to be safe. Note, the widgets are ref'd in
+   * populatate_unit_pixmap_table. */
+  gtk_container_remove(GTK_CONTAINER(unit_pixmap_table),
+                      unit_pixmap_button);
+  gtk_widget_unref(unit_pixmap);
+  gtk_widget_unref(unit_pixmap_button);
+  for (i = 0; i < num_units_below; i++) {
+    gtk_container_remove(GTK_CONTAINER(unit_pixmap_table),
+                        unit_below_pixmap_button[i]);
+    gtk_widget_unref(unit_below_pixmap[i]);
+    gtk_widget_unref(unit_below_pixmap_button[i]);
+  }
+  gtk_container_remove(GTK_CONTAINER(unit_pixmap_table),
+                      more_arrow_pixmap);
+  gtk_widget_unref(more_arrow_pixmap);
+
+  populate_unit_pixmap_table();
+
+  /* We have to force a redraw of the units.  And we explicitly have
+   * to force a redraw of the focus unit, which is normally only
+   * redrawn when the focus changes. We also have to force the 'more'
+   * arrow to go away, both by explicitly hiding it and telling it to
+   * do so (this will be reset immediately afterwards if necessary,
+   * but we have to make the *internal* state consistent). */
+  gtk_widget_hide(more_arrow_pixmap);
+  set_unit_icons_more_arrow(FALSE);
+  set_unit_icon(-1, get_unit_in_focus());
+  update_unit_pix_label(get_unit_in_focus());
+}
+
+/**************************************************************************
  do the heavy lifting for the widget setup.
 **************************************************************************/
 static void setup_widgets(void)
@@ -612,44 +712,15 @@
   box = gtk_hbox_new(FALSE,0);
   gtk_box_pack_start(GTK_BOX(avbox), box, FALSE, FALSE, 0);
 
-  table = gtk_table_new(2, num_units_below, FALSE);
+  table = gtk_table_new(0, 0, FALSE);
   gtk_box_pack_start(GTK_BOX(box), table, FALSE, FALSE, 5);
 
   gtk_table_set_row_spacings(GTK_TABLE(table), 2);
   gtk_table_set_col_spacings(GTK_TABLE(table), 2);
 
-  unit_pixmap = gtk_pixcomm_new(root_window, UNIT_TILE_WIDTH, 
-                                UNIT_TILE_HEIGHT);
-  gtk_pixcomm_clear(GTK_PIXCOMM(unit_pixmap), TRUE);
-  unit_pixmap_button = gtk_event_box_new();
-  gtk_container_add(GTK_CONTAINER(unit_pixmap_button), unit_pixmap);
-  gtk_table_attach_defaults(GTK_TABLE(table), unit_pixmap_button, 0, 1, 0, 1);
-  gtk_signal_connect(GTK_OBJECT(unit_pixmap_button), "button_press_event",
-                     GTK_SIGNAL_FUNC(select_unit_pixmap_callback), 
-                     GINT_TO_POINTER(-1));
+  unit_pixmap_table = table;
+  populate_unit_pixmap_table();
 
-  for(i = 0; i < num_units_below; i++) {
-    unit_below_pixmap[i] = gtk_pixcomm_new(root_window, UNIT_TILE_WIDTH,
-                                           UNIT_TILE_HEIGHT);
-    unit_below_pixmap_button[i] = gtk_event_box_new();
-    gtk_container_add(GTK_CONTAINER(unit_below_pixmap_button[i]),
-                      unit_below_pixmap[i]);
-    gtk_signal_connect(GTK_OBJECT(unit_below_pixmap_button[i]),
-                       "button_press_event",
-                        GTK_SIGNAL_FUNC(select_unit_pixmap_callback),
-                        GINT_TO_POINTER(i));
-      
-    gtk_table_attach_defaults(GTK_TABLE(table), unit_below_pixmap_button[i],
-                              i, i + 1, 1, 2);
-    gtk_widget_set_usize(unit_below_pixmap[i],
-                         UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
-    gtk_pixcomm_clear(GTK_PIXCOMM(unit_below_pixmap[i]), TRUE);
-  }
-
-  more_arrow_pixmap = gtk_pixmap_new(sprites.right_arrow->pixmap, NULL);
-  gtk_pixmap_set_build_insensitive(GTK_PIXMAP(more_arrow_pixmap), FALSE);
-  gtk_table_attach_defaults(GTK_TABLE(table), more_arrow_pixmap, 4, 5, 1, 2);
-
   /* Map canvas and scrollbars */
 
   table = gtk_table_new(2, 2, FALSE);
@@ -878,11 +949,6 @@
 
   tilespec_load_tiles();
 
-  /* 135 below is rough value (could be more intelligent) --dwp */
-  num_units_below = 135 / (int) NORMAL_TILE_WIDTH;
-  num_units_below = MIN(num_units_below, MAX_NUM_UNITS_BELOW);
-  num_units_below = MAX(num_units_below, 1);
-  
   setup_widgets();
   load_intro_gfx();
   load_cursors();
Index: client/gui-gtk/gui_main.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.h,v
retrieving revision 1.7
diff -u -r1.7 gui_main.h
--- client/gui-gtk/gui_main.h   2002/07/06 20:35:22     1.7
+++ client/gui-gtk/gui_main.h   2002/11/26 09:09:04
@@ -60,4 +60,6 @@
 extern GtkWidget *      map_vertical_scrollbar;
 extern GdkWindow *      root_window;
 
+void reset_unit_table(void);
+
 #endif  /* FC__GUI_MAIN_H */
Index: client/gui-gtk/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.136
diff -u -r1.136 mapview.c
--- client/gui-gtk/mapview.c    2002/11/24 11:43:40     1.136
+++ client/gui-gtk/mapview.c    2002/11/26 09:09:05
@@ -2182,4 +2182,5 @@
 void tileset_changed(void)
 {
   reset_city_dialogs();
+  reset_unit_table();
 }

[Prev in Thread] Current Thread [Next in Thread]