Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] (PR#8781) Change city dialog to use specvecs instead of re
Home

[Freeciv-Dev] (PR#8781) Change city dialog to use specvecs instead of re

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8781) Change city dialog to use specvecs instead of realloc'ed array.
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Mon, 24 May 2004 05:55:33 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8781 >

> [vasc - Wed May 19 20:44:51 2004]:
> 
> Subject says it all really. This should also remove some warnings when
> the array gets realloced to size 0, but I think fc_realloc should be
> fixed not to warn for that as well. This can be done later on.

Christian Knoke found a bug in this patch, this should fix it.

Index: client/gui-gtk-2.0/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/citydlg.c,v
retrieving revision 1.87
diff -u -r1.87 citydlg.c
--- client/gui-gtk-2.0/citydlg.c        21 May 2004 19:26:30 -0000      1.87
+++ client/gui-gtk-2.0/citydlg.c        24 May 2004 12:51:45 -0000
@@ -1502,7 +1502,8 @@
 static void city_dialog_update_supported_units(struct city_dialog *pdialog)
 {
   struct unit_list *units;
-  int n, i;
+  struct unit_node_vector *nodes;
+  int n, m, i;
   char buf[30];
 
   if (pdialog->pcity->owner != game.player_idx) {
@@ -1511,48 +1512,54 @@
     units = &(pdialog->pcity->units_supported);
   }
 
-  n = unit_list_size(units);
+  nodes = &pdialog->overview.supported_units;
 
-  i = 0;
-  unit_node_vector_iterate(&pdialog->overview.supported_units, elt) {
-    if (i++ >= n) {
-      gtk_widget_destroy(elt->cmd);
-    }
-  } unit_node_vector_iterate_end;
+  n = unit_list_size(units);
+  m = unit_node_vector_size(nodes);
 
   gtk_table_resize(GTK_TABLE(pdialog->overview.supported_unit_table),
                   1, MAX(n, 1));
 
-  for (; i<n; i++) {
-    int unit_height = (is_isometric) ?
-       UNIT_TILE_HEIGHT : UNIT_TILE_HEIGHT + UNIT_TILE_HEIGHT / 2;
-    GtkWidget *cmd, *pix;
-    struct unit_node node;
-
-    cmd = gtk_button_new();
-    node.cmd = cmd;
-
-    gtk_button_set_relief(GTK_BUTTON(cmd), GTK_RELIEF_NONE);
-    gtk_widget_add_events(cmd,
-      GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
-    
-    pix = gtk_pixcomm_new(UNIT_TILE_WIDTH, unit_height);
-    node.pix = pix;
+  if (m > n) {
+    i = 0;
+    unit_node_vector_iterate(nodes, elt) {
+      if (i++ >= n) {
+       gtk_widget_destroy(elt->cmd);
+      }
+    } unit_node_vector_iterate_end;
+
+    unit_node_vector_reserve(nodes, n);
+  } else {
+    for (i = m; i < n; i++) {
+      GtkWidget *cmd, *pix;
+      struct unit_node node;
+
+      cmd = gtk_button_new();
+      node.cmd = cmd;
+
+      gtk_button_set_relief(GTK_BUTTON(cmd), GTK_RELIEF_NONE);
+      gtk_widget_add_events(cmd,
+         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
 
-    gtk_container_add(GTK_CONTAINER(cmd), pix);
+      pix = gtk_pixcomm_new(UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
+      node.pix = pix;
 
-    
gtk_table_attach_defaults(GTK_TABLE(pdialog->overview.supported_unit_table),
-                             cmd, i, i+1, 0, 1);
-    unit_node_vector_append(&pdialog->overview.supported_units, &node);
+      gtk_container_add(GTK_CONTAINER(cmd), pix);
+
+      gtk_table_attach_defaults(
+         GTK_TABLE(pdialog->overview.supported_unit_table),
+         cmd, i, i+1, 0, 1);
+      unit_node_vector_append(nodes, &node);
+    }
   }
 
   gtk_tooltips_disable(pdialog->tips);
 
-  i=0;
+  i = 0;
   unit_list_iterate(*units, punit) {
     struct unit_node *pnode;
     
-    pnode = unit_node_vector_get(&pdialog->overview.supported_units, i);
+    pnode = unit_node_vector_get(nodes, i);
     if (pnode) {
       GtkWidget *cmd, *pix;
 
@@ -1608,7 +1615,8 @@
 static void city_dialog_update_present_units(struct city_dialog *pdialog)
 {
   struct unit_list *units;
-  int n, i;
+  struct unit_node_vector *nodes;
+  int n, m, i;
   char buf[30];
 
   if (pdialog->pcity->owner != game.player_idx) {
@@ -1617,46 +1625,54 @@
     units = &(map_get_tile(pdialog->pcity->x, pdialog->pcity->y)->units);
   }
 
-  n = unit_list_size(units);
+  nodes = &pdialog->overview.present_units;
 
-  i = 0;
-  unit_node_vector_iterate(&pdialog->overview.present_units, elt) {
-    if (i++ >= n) {
-      gtk_widget_destroy(elt->cmd);
-    }
-  } unit_node_vector_iterate_end;
+  n = unit_list_size(units);
+  m = unit_node_vector_size(nodes);
 
   gtk_table_resize(GTK_TABLE(pdialog->overview.present_unit_table),
                   1, MAX(n, 1));
 
-  for (; i<n; i++) {
-    GtkWidget *cmd, *pix;
-    struct unit_node node;
-
-    cmd = gtk_button_new();
-    node.cmd = cmd;
-
-    gtk_button_set_relief(GTK_BUTTON(cmd), GTK_RELIEF_NONE);
-    gtk_widget_add_events(cmd,
-      GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
-    
-    pix = gtk_pixcomm_new(UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
-    node.pix = pix;
+  if (m > n) {
+    i = 0;
+    unit_node_vector_iterate(nodes, elt) {
+      if (i++ >= n) {
+       gtk_widget_destroy(elt->cmd);
+      }
+    } unit_node_vector_iterate_end;
+
+    unit_node_vector_reserve(nodes, n);
+  } else {
+    for (i = m; i < n; i++) {
+      GtkWidget *cmd, *pix;
+      struct unit_node node;
+
+      cmd = gtk_button_new();
+      node.cmd = cmd;
+
+      gtk_button_set_relief(GTK_BUTTON(cmd), GTK_RELIEF_NONE);
+      gtk_widget_add_events(cmd,
+         GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
 
-    gtk_container_add(GTK_CONTAINER(cmd), pix);
+      pix = gtk_pixcomm_new(UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
+      node.pix = pix;
 
-    gtk_table_attach_defaults(GTK_TABLE(pdialog->overview.present_unit_table),
-                             cmd, i, i+1, 0, 1);
-    unit_node_vector_append(&pdialog->overview.present_units, &node);
+      gtk_container_add(GTK_CONTAINER(cmd), pix);
+
+      gtk_table_attach_defaults(
+         GTK_TABLE(pdialog->overview.present_unit_table),
+         cmd, i, i+1, 0, 1);
+      unit_node_vector_append(nodes, &node);
+    }
   }
 
   gtk_tooltips_disable(pdialog->tips);
 
-  i=0;
+  i = 0;
   unit_list_iterate(*units, punit) {
     struct unit_node *pnode;
     
-    pnode = unit_node_vector_get(&pdialog->overview.present_units, i);
+    pnode = unit_node_vector_get(nodes, i);
     if (pnode) {
       GtkWidget *cmd, *pix;
 

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