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: vasc@xxxxxxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx, bugs@xxxxxxxxxxxxxxxxxxx
Subject: Re: [Freeciv-Dev] patch: makes city dialog units scrollable (PR#79)
From: "P.E.Jean" <pejean.lists@xxxxxxxxx>
Date: Fri, 30 Jul 1999 12:50:33 -0400

vasc@xxxxxxxxxxxxxxxxxxxxx wrote:
> 
> On Thu, 29 Jul 1999, P.E.Jean wrote:
> > Since I posted this patch a few hours ago, many things
> > changed in cvs and it no longer works.
> 
> Oops, sorry i changed the code in cvs yet again... :-(

Thank you for telling me.

> The changes should be fairly minimal, they mostly involve changing the
> parts of your code which use things like:
> 
> "gtk_pixcomm_clear(pixcomm)" to "gtk_pixcomm_clear(pixcomm, bool)"
> 
> - The bool field should be set to TRUE if you want the pixcomm clear to
> cause an immediate refresh and to FALSE otherwise.

I made the changes and here it is again.

  Pierre.
Index: client/gui-gtk/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/citydlg.c,v
retrieving revision 1.18
diff -u -r1.18 citydlg.c
--- citydlg.c   1999/07/30 10:53:08     1.18
+++ citydlg.c   1999/07/30 16:06:21
@@ -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));
@@ -434,60 +448,63 @@
        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]);
 
-    pdialog->present_unit_ids[i]=-1;
+  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);
 
-    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);
@@ -555,6 +572,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)
@@ -566,6 +586,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",
@@ -1171,6 +1195,26 @@
     if(unitid && punit->id!=unitid)
       continue;
 
+    if (pdialog->support_unit_ids[i] >= 0) {
+      gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->support_unit_pixmaps[i]), FALSE);
+      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]), FALSE);
 
@@ -1183,20 +1227,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]), TRUE);
-    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;
   }
 }
 
@@ -1226,6 +1271,26 @@
     if(unitid && punit->id!=unitid)
       continue;
 
+    if (pdialog->present_unit_ids[i] >= 0) {
+      gtk_pixcomm_clear(GTK_PIXCOMM(pdialog->present_unit_pixmaps[i]), FALSE);
+      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]), FALSE);
 
@@ -1238,19 +1303,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]), TRUE);
-    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]