Complete.Org: Mailing Lists: Archives: freeciv-dev: July 1999:
Re: [Freeciv-Dev] patch: makes city dialog units scrollable (PR#79)
Home

Re: [Freeciv-Dev] patch: makes city dialog units scrollable (PR#79)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: Re: [Freeciv-Dev] patch: makes city dialog units scrollable (PR#79)
From: "P.E.Jean" <pejean.lists@xxxxxxxxx>
Date: Thu, 29 Jul 1999 23:00:13 -0400

pejean.lists@xxxxxxxxx wrote:
> Currently, in the city dialog, only 12 supported units
> and 12 present units can be shown.
> 
> This patch allows the player to see more of them and
> adds a scroll bar under the supported units list and/or
> the present units list when there isn't enough place to
> show them all.

Since I posted this patch a few hours ago, many things
changed in cvs and it no longer works.

So I modified it to match what changed.

It applies to the gtk client of the latest source
from cvs (July 29 1999).

  Pierre.
Index: client/gui-gtk/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/citydlg.c,v
retrieving revision 1.17
diff -u -r1.17 citydlg.c
--- citydlg.c   1999/07/29 16:23:59     1.17
+++ citydlg.c   1999/07/30 01:31:09
@@ -52,7 +52,7 @@
 
 extern GtkStyle *city_dialog_style;
 
-#define NUM_UNITS_SHOWN  12
+#define NUM_UNITS_SHOWN  50
 #define NUM_CITIZENS_SHOWN 25
 
 
@@ -75,9 +75,11 @@
   GtkWidget *building_label, *progress_label, *buy_command, *change_command;
   GtkWidget *improvement_viewport, *improvement_list;
   GtkWidget *support_unit_label;
+  GtkWidget *support_unit_box;
   GtkWidget *support_unit_boxes                [NUM_UNITS_SHOWN];
   GtkWidget *support_unit_pixmaps      [NUM_UNITS_SHOWN];
   GtkWidget *present_unit_label;
+  GtkWidget *present_unit_box;
   GtkWidget *present_unit_boxes                [NUM_UNITS_SHOWN];
   GtkWidget *present_unit_pixmaps      [NUM_UNITS_SHOWN];
   GtkWidget *change_list;
@@ -293,11 +295,23 @@
 /****************************************************************
 ...
 *****************************************************************/
+void unit_scrollbar_callback (GtkAdjustment *adj, GtkWidget *scrollbar) {
+  if (adj->page_size < adj->upper && adj->page_size != 1)
+    gtk_widget_show (scrollbar);
+  else
+    gtk_widget_hide (scrollbar);
+}
+
+/****************************************************************
+...
+*****************************************************************/
 struct city_dialog *create_city_dialog(struct city *pcity, int make_modal)
 {
   int i;
   struct city_dialog *pdialog;
   GtkWidget *box, *table, *frame, *vbox, *scrolled;
+  GtkWidget *viewport, *support_units_scrollbar, *present_units_scrollbar;
+  GtkAdjustment *adjustment;
   GtkAccelGroup *accel=gtk_accel_group_new();
 
   pdialog=fc_malloc(sizeof(struct city_dialog));
@@ -442,61 +456,64 @@
        FALSE, FALSE, 0);
   gtk_widget_realize(pdialog->support_unit_label);
 
-  box=gtk_hbox_new(TRUE, 1);
+  box = gtk_vbox_new (FALSE, 1);
   gtk_container_add(GTK_CONTAINER(pdialog->support_unit_label),box);
   gtk_widget_realize(box);
 
-  for(i=0; i<NUM_UNITS_SHOWN; i++) {
-    pdialog->support_unit_boxes[i]=gtk_event_box_new();
-    gtk_widget_set_events(pdialog->support_unit_boxes[i],
-       GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK);
-
-    gtk_box_pack_start(GTK_BOX(box), pdialog->support_unit_boxes[i],
-                      TRUE, FALSE, 0);
-    gtk_widget_show(pdialog->support_unit_boxes[i]);
-
-    pdialog->support_unit_pixmaps[i]=
-     gtk_pixcomm_new(root_window, NORMAL_TILE_WIDTH, 
NORMAL_TILE_HEIGHT+NORMAL_TILE_HEIGHT/2);
-    gtk_container_add(GTK_CONTAINER(pdialog->support_unit_boxes[i]),
-       pdialog->support_unit_pixmaps[i]);
+  viewport = gtk_viewport_new (NULL, NULL);
+  gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport), GTK_SHADOW_NONE);
+  /* This gives the viewport a minimum size. */
+  gtk_widget_set_usize (viewport,
+          NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT + NORMAL_TILE_HEIGHT / 2);
+  /* The viewport seems to ignore the resize requests of it's child,
+     so I tell it to pass them to it's parent widget. If I don't do that,
+     the scrollbar doesn't get adjusted if a unit is added or removed. */
+  gtk_container_set_resize_mode (GTK_CONTAINER (viewport),
+          GTK_RESIZE_PARENT);
+  gtk_box_pack_start (GTK_BOX (box), viewport, FALSE, FALSE, 0);
+
+  pdialog->support_unit_box = gtk_hbox_new (FALSE, 4);
+  gtk_container_add (GTK_CONTAINER (viewport), pdialog->support_unit_box);
+
+  adjustment = gtk_viewport_get_hadjustment (GTK_VIEWPORT (viewport));
+  support_units_scrollbar = gtk_hscrollbar_new (adjustment);
+  gtk_box_pack_start (GTK_BOX (box), support_units_scrollbar,
+          FALSE, FALSE, 0);
+  gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
+          GTK_SIGNAL_FUNC (unit_scrollbar_callback), support_units_scrollbar);
 
-    pdialog->support_unit_ids[i]=-1;
 
-    if(pcity->owner != game.player_idx)
-      gtk_widget_set_sensitive(pdialog->support_unit_boxes[i], FALSE);    
-  }
-
-
   /* "present units" frame */
   pdialog->present_unit_label=gtk_frame_new("Units present");
   gtk_box_pack_start(GTK_BOX(vbox), pdialog->present_unit_label,
        FALSE, FALSE, 0);
   gtk_widget_realize(pdialog->present_unit_label);
 
-  box=gtk_hbox_new(TRUE, 1);
+  box = gtk_vbox_new (FALSE, 1);
   gtk_container_add(GTK_CONTAINER(pdialog->present_unit_label),box);
   gtk_widget_realize(box);
 
-  for(i=0; i<NUM_UNITS_SHOWN; i++) {
-    pdialog->present_unit_boxes[i]=gtk_event_box_new();
-    gtk_widget_set_events(pdialog->present_unit_boxes[i],
-       GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK);
-
-    gtk_box_pack_start(GTK_BOX(box), pdialog->present_unit_boxes[i],
-                      TRUE, FALSE, 0);
-    gtk_widget_show(pdialog->present_unit_boxes[i]);
-
-    pdialog->present_unit_pixmaps[i]=
-     gtk_pixcomm_new(root_window, NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT);
-    gtk_container_add(GTK_CONTAINER(pdialog->present_unit_boxes[i]),
-       pdialog->present_unit_pixmaps[i]);
+  viewport = gtk_viewport_new (NULL, NULL);
+  gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport), GTK_SHADOW_NONE);
+  /* Gives a minimum size to this viewport. */
+  gtk_widget_set_usize (viewport, NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT);
+  /* Again, the viewport seems to ignore the resize requests of it's child,
+     so I tell it to pass them to it's parent widget. */
+  gtk_container_set_resize_mode (GTK_CONTAINER (viewport),
+          GTK_RESIZE_PARENT);
+  gtk_box_pack_start (GTK_BOX (box), viewport, FALSE, FALSE, 0);
+
+  pdialog->present_unit_box = gtk_hbox_new (FALSE, 4);
+  gtk_container_add (GTK_CONTAINER (viewport), pdialog->present_unit_box);
+
+  adjustment = gtk_viewport_get_hadjustment (GTK_VIEWPORT (viewport));
+  present_units_scrollbar = gtk_hscrollbar_new (adjustment);
+  gtk_box_pack_start (GTK_BOX (box), present_units_scrollbar,
+          FALSE, FALSE, 0);
+  gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
+          GTK_SIGNAL_FUNC (unit_scrollbar_callback), present_units_scrollbar);
 
-    pdialog->present_unit_ids[i]=-1;
 
-    if(pcity->owner != game.player_idx)
-      gtk_widget_set_sensitive(pdialog->present_unit_boxes[i], FALSE);    
-  }
-
   /* "action area" buttons */
   pdialog->close_command=gtk_accelbutton_new("C_lose", accel);
   GTK_WIDGET_SET_FLAGS(pdialog->close_command, GTK_CAN_DEFAULT);
@@ -563,6 +580,9 @@
   for(i=0; i<NUM_CITIZENS_SHOWN; i++)
     pdialog->citizen_type[i]=-1;
 
+  for(i=0; i<NUM_UNITS_SHOWN; i++)
+    pdialog->support_unit_ids[i] = pdialog->present_unit_ids[i] = -1;
+
   refresh_city_dialog(pdialog->pcity);
 
   if(make_modal)
@@ -574,6 +594,10 @@
   gtk_widget_show_all(GTK_DIALOG(pdialog->shell)->vbox);
   gtk_widget_show_all(GTK_DIALOG(pdialog->shell)->action_area);
 
+  /* The units scrollbars are hidden until they are needed. */
+  gtk_widget_hide (support_units_scrollbar);
+  gtk_widget_hide (present_units_scrollbar);
+
   gtk_signal_connect(GTK_OBJECT(pdialog->map_canvas), "expose_event",
        GTK_SIGNAL_FUNC(city_map_canvas_expose), pdialog);
   gtk_signal_connect(GTK_OBJECT(pdialog->map_canvas), "button_press_event",
@@ -1190,6 +1214,26 @@
     if(unitid && punit->id!=unitid)
       continue;
 
+    if (pdialog->support_unit_ids[i] >= 0) {
+      gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->support_unit_pixmaps[i]));
+      gtk_signal_handlers_destroy (GTK_OBJECT 
(pdialog->support_unit_boxes[i]));
+    }
+
+    else {
+      pdialog->support_unit_boxes[i] = gtk_event_box_new();
+      gtk_widget_set_events (pdialog->support_unit_boxes[i],
+              GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
+      gtk_box_pack_start (GTK_BOX (pdialog->support_unit_box),
+              pdialog->support_unit_boxes[i], FALSE, FALSE, 0);
+      gtk_widget_show (pdialog->support_unit_boxes[i]);
+
+      pdialog->support_unit_pixmaps[i] = gtk_pixcomm_new (root_window,
+              NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT + NORMAL_TILE_HEIGHT / 2);
+      gtk_container_add (GTK_CONTAINER (pdialog->support_unit_boxes[i]),
+              pdialog->support_unit_pixmaps[i]);
+      gtk_widget_show (pdialog->support_unit_pixmaps[i]);
+    }
+
     if (flags_are_transparent)
       gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->support_unit_pixmaps[i])); /* STG 
*/
 
@@ -1202,20 +1246,21 @@
 
     pdialog->support_unit_ids[i]=punit->id;
 
-    gtk_signal_handlers_destroy(GTK_OBJECT(pdialog->support_unit_boxes[i]));
-    gtk_signal_connect(GTK_OBJECT(pdialog->support_unit_boxes[i]),
-       "button_press_event",
-       GTK_SIGNAL_FUNC(support_units_callback), (gpointer)punit->id);
-    gtk_signal_connect(GTK_OBJECT(pdialog->support_unit_boxes[i]),
-       "button_release_event",
-       GTK_SIGNAL_FUNC(s_units_middle_callback), (gpointer)punit->id);
-    gtk_widget_set_sensitive(pdialog->support_unit_boxes[i], TRUE);
+    if (punit->owner==game.player_idx) {
+      gtk_signal_connect(GTK_OBJECT(pdialog->support_unit_boxes[i]),
+          "button_press_event",
+          GTK_SIGNAL_FUNC(support_units_callback), (gpointer)punit->id);
+      gtk_signal_connect(GTK_OBJECT(pdialog->support_unit_boxes[i]),
+          "button_release_event",
+          GTK_SIGNAL_FUNC(s_units_middle_callback), (gpointer)punit->id);
+    }
   }
     
   for(; i<NUM_UNITS_SHOWN; i++) {
-    gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->support_unit_pixmaps[i]));
-    pdialog->support_unit_ids[i]=0;
-    gtk_widget_set_sensitive(pdialog->support_unit_boxes[i], FALSE);
+    if (pdialog->support_unit_ids[i] < 0)
+      break;
+    gtk_widget_destroy (pdialog->support_unit_boxes[i]);
+    pdialog->support_unit_ids[i] = -1;
   }
 }
 
@@ -1245,6 +1290,26 @@
     if(unitid && punit->id!=unitid)
       continue;
 
+    if (pdialog->present_unit_ids[i] >= 0) {
+      gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->present_unit_pixmaps[i]));
+      gtk_signal_handlers_destroy (GTK_OBJECT 
(pdialog->present_unit_boxes[i]));
+    }
+
+    else {
+      pdialog->present_unit_boxes[i] = gtk_event_box_new();
+      gtk_widget_set_events (pdialog->present_unit_boxes[i],
+              GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
+      gtk_box_pack_start (GTK_BOX (pdialog->present_unit_box),
+              pdialog->present_unit_boxes[i], FALSE, FALSE, 0);
+      gtk_widget_show (pdialog->present_unit_boxes[i]);
+
+      pdialog->present_unit_pixmaps[i] = gtk_pixcomm_new (
+              root_window, NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT);
+      gtk_container_add (GTK_CONTAINER (pdialog->present_unit_boxes[i]),
+              pdialog->present_unit_pixmaps[i]);
+      gtk_widget_show (pdialog->present_unit_pixmaps[i]);
+    }
+
     if (flags_are_transparent)
       gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->present_unit_pixmaps[i])); /* STG 
*/
 
@@ -1257,19 +1322,21 @@
 
     pdialog->present_unit_ids[i]=punit->id;
 
-    gtk_signal_handlers_destroy(GTK_OBJECT(pdialog->present_unit_boxes[i]));
-    gtk_signal_connect(GTK_OBJECT(pdialog->present_unit_boxes[i]),
-       "button_press_event",
-       GTK_SIGNAL_FUNC(present_units_callback), (gpointer)punit->id);
-    gtk_signal_connect(GTK_OBJECT(pdialog->present_unit_boxes[i]),
-       "button_release_event",
-       GTK_SIGNAL_FUNC(p_units_middle_callback), (gpointer)punit->id);
-    gtk_widget_set_sensitive(pdialog->present_unit_boxes[i], TRUE);
+    if (punit->owner==game.player_idx) {
+      gtk_signal_connect(GTK_OBJECT(pdialog->present_unit_boxes[i]),
+          "button_press_event",
+          GTK_SIGNAL_FUNC(present_units_callback), (gpointer)punit->id);
+      gtk_signal_connect(GTK_OBJECT(pdialog->present_unit_boxes[i]),
+          "button_release_event",
+          GTK_SIGNAL_FUNC(p_units_middle_callback), (gpointer)punit->id);
+    }
   }
+
   for(; i<NUM_UNITS_SHOWN; i++) {
-    gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->present_unit_pixmaps[i]));
-    pdialog->present_unit_ids[i]=0;
-    gtk_widget_set_sensitive(pdialog->present_unit_boxes[i], FALSE);
+    if (pdialog->present_unit_ids[i] < 0)
+      break;
+    gtk_widget_destroy (pdialog->present_unit_boxes[i]);
+    pdialog->present_unit_ids[i] = -1;
   }
 }
 


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