Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2003:
[Freeciv-Dev] (PR#2970) GTK+ 2.0 client un-modalization
Home

[Freeciv-Dev] (PR#2970) GTK+ 2.0 client un-modalization

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#2970) GTK+ 2.0 client un-modalization
From: "Vasco Alexandre Da Silva Costa via RT" <rt@xxxxxxxxxxxxxx>
Date: Sat, 1 Feb 2003 14:36:46 -0800
Reply-to: rt@xxxxxxxxxxxxxx


In what I hope will be the first of a series of patches, this patch
converts the goto dialog to the GTK+ 2.0 API and isn't modal anymore.

---
Vasco Alexandre da Silva Costa @ Instituto Superior Tecnico, Lisboa

Index: client/gui-gtk-2.0/gotodlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gotodlg.c,v
retrieving revision 1.6
diff -u -r1.6 gotodlg.c
--- client/gui-gtk-2.0/gotodlg.c        1 Jan 2003 11:51:33 -0000       1.6
+++ client/gui-gtk-2.0/gotodlg.c        1 Feb 2003 21:57:17 -0000
@@ -40,25 +40,23 @@
 
 #include "gotodlg.h"
 
-static GtkWidget *goto_dialog_shell;
-static GtkWidget *goto_label;
-static GtkWidget *goto_list;
-static GtkWidget *goto_center_command;
-static GtkWidget *goto_airlift_command;
-static GtkWidget *goto_all_toggle;
-static GtkWidget *goto_cancel_command;
-
-static void update_goto_dialog                 (GtkWidget *goto_list);
-
-static void goto_cancel_command_callback       (GtkWidget *w, gpointer data);
-static void goto_goto_command_callback         (GtkWidget *w, gpointer data);
-static void goto_airlift_command_callback      (GtkWidget *w, gpointer data);
-static void goto_all_toggle_callback           (GtkWidget *w, gpointer data);
-static void goto_list_callback                 (GtkWidget *w, gint row, gint 
column);
-static void goto_list_ucallback                (GtkWidget *w, gint row, gint 
column);
-
+static GtkWidget *dshell;
+static GtkWidget *view;
+static GtkWidget *airlift_cmd;
+static GtkWidget *all_toggle;
+static GtkListStore *store;
+static GtkTreeSelection *selection;
 static int original_x, original_y;
 
+static void update_goto_dialog(GtkToggleButton *button);
+static void goto_selection_callback(GtkTreeSelection *selection, gpointer 
data);
+
+static struct city *get_selected_city(void);
+
+enum {
+  CMD_AIRLIFT = 1, CMD_GOTO
+};
+
 /****************************************************************
 ...
 *****************************************************************/
@@ -67,86 +65,156 @@
   popup_goto_dialog();
 }
 
+/**************************************************************************
+...
+**************************************************************************/
+static void goto_cmd_callback(GtkWidget *dlg, gint arg)
+{
+  switch (arg) {
+  case GTK_RESPONSE_CANCEL:
+    center_tile_mapcanvas(original_x, original_y);
+    break;
+
+  case CMD_AIRLIFT:
+    {
+      struct city *pdestcity=get_selected_city();
+
+      if (pdestcity) {
+        struct unit *punit=get_unit_in_focus();
+
+        if (punit) {
+          request_unit_airlift(punit, pdestcity);
+        }
+      }
+    }
+    break;
+
+  case CMD_GOTO:
+    {
+      struct city *pdestcity=get_selected_city();
+
+      if (pdestcity) {
+        struct unit *punit=get_unit_in_focus();
+
+        if (punit) {
+          send_goto_unit(punit, pdestcity->x, pdestcity->y);
+        }
+      }
+    }
+    break;
+
+  default:
+    break;
+  }
+
+  gtk_widget_destroy(dlg);
+}
+
+
+/**************************************************************************
+...
+**************************************************************************/
+void create_goto_dialog(void)
+{
+  GtkWidget *sw, *label, *vbox, *button;
+  GtkCellRenderer *rend;
+  GtkTreeViewColumn *col;
+
+  dshell=gtk_dialog_new_with_buttons(_("Goto/Airlift Unit"),
+    GTK_WINDOW(toplevel),
+    0,
+    GTK_STOCK_CANCEL,
+    GTK_RESPONSE_CANCEL,
+    NULL);
+
+  button = gtk_dialog_add_button(GTK_DIALOG(dshell),
+    _("Air_lift"), CMD_AIRLIFT);
+  airlift_cmd = button;
+  button = gtk_dialog_add_button(GTK_DIALOG(dshell),
+    _("_Goto"), CMD_GOTO);
+
+  gtk_window_set_position(GTK_WINDOW(dshell), GTK_WIN_POS_MOUSE);
+  gtk_dialog_set_default_response(GTK_DIALOG(dshell), CMD_GOTO);
+  g_signal_connect(dshell, "destroy",
+                  G_CALLBACK(gtk_widget_destroyed), &dshell);
+  g_signal_connect(dshell, "response",
+                   G_CALLBACK(goto_cmd_callback), NULL);
+
+  label = gtk_frame_new(_("Select destination"));
+  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dshell)->vbox),
+       label, TRUE, TRUE, 0);
+
+  vbox = gtk_vbox_new(FALSE, 6);
+  gtk_container_add(GTK_CONTAINER(label), vbox);
+
+  store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_BOOLEAN);
+  gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
+    0, GTK_SORT_ASCENDING);
+
+  view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
+  g_object_unref(store);
+  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
+  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
+
+  rend = gtk_cell_renderer_text_new();
+  col = gtk_tree_view_column_new_with_attributes(NULL, rend,
+    "text", 0, NULL);
+  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
+
+  rend = gtk_cell_renderer_toggle_new();
+  col = gtk_tree_view_column_new_with_attributes(NULL, rend,
+    "active", 1, NULL);
+  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
+
+  sw = gtk_scrolled_window_new(NULL, NULL);
+  gtk_container_add(GTK_CONTAINER(sw), view);
+  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
+    GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
+  gtk_widget_set_usize(sw, -1, 300);
+
+  label = g_object_new(GTK_TYPE_LABEL,
+    "use-underline", TRUE,
+    "mnemonic-widget", view,
+    "label", _("Ci_ties:"),
+    "xalign", 0.0,
+    "yalign", 0.5,
+    NULL);
+  gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
+
+  gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
+
+  all_toggle = gtk_check_button_new_with_mnemonic(_("Show _All Cities"));
+  gtk_box_pack_start(GTK_BOX(vbox), all_toggle, TRUE, TRUE, 0);
+
+  g_signal_connect(all_toggle, "toggled", G_CALLBACK(update_goto_dialog), 
NULL);
+
+  g_signal_connect(selection, "changed",
+    G_CALLBACK(goto_selection_callback), NULL);
+
+  gtk_widget_show_all(GTK_DIALOG(dshell)->vbox);
+  gtk_widget_show_all(GTK_DIALOG(dshell)->action_area);
+
+
+  get_center_tile_mapcanvas(&original_x, &original_y);
+
+  update_goto_dialog(GTK_TOGGLE_BUTTON(all_toggle));
+  gtk_tree_view_focus(GTK_TREE_VIEW(view));
+}
+
 /****************************************************************
-popup the dialog 10% inside the main-window 
+popup the dialog
 *****************************************************************/
 void popup_goto_dialog(void)
 {
-  GtkWidget *scrolled;
-  GtkAccelGroup *accel=gtk_accel_group_new();
-
   if (!can_client_issue_orders() || !get_unit_in_focus()) {
     return;
   }
 
-  get_center_tile_mapcanvas(&original_x, &original_y);
-  
-  goto_dialog_shell=gtk_dialog_new_with_buttons(_("Goto/Airlift Unit"),
-    GTK_WINDOW(toplevel),
-    GTK_DIALOG_MODAL,
-    NULL);
-  g_signal_connect(goto_dialog_shell, "destroy",
-                  G_CALLBACK(gtk_widget_destroyed), &goto_dialog_shell);
-  gtk_window_set_position (GTK_WINDOW(goto_dialog_shell), GTK_WIN_POS_MOUSE);
-
-  goto_label=gtk_frame_new(_("Select destination"));
-  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(goto_dialog_shell)->vbox),
-       goto_label, TRUE, TRUE, 0);
-
-  goto_list=gtk_clist_new(1);
-  gtk_clist_set_column_width(GTK_CLIST(goto_list), 0,
-                            GTK_CLIST(goto_list)->clist_window_width);
-  gtk_clist_set_auto_sort (GTK_CLIST (goto_list), TRUE);
-  scrolled=gtk_scrolled_window_new(NULL, NULL);
-  gtk_container_add(GTK_CONTAINER(scrolled), goto_list);
-
-  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
-                                GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
-  gtk_widget_set_usize(scrolled, 250, 300);
-  gtk_container_add(GTK_CONTAINER(goto_label), scrolled);
-
-  goto_center_command=gtk_accelbutton_new(_("_Goto"), accel);
-  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(goto_dialog_shell)->action_area),
-       goto_center_command, TRUE, TRUE, 0);
-  GTK_WIDGET_SET_FLAGS(goto_center_command, GTK_CAN_DEFAULT);
-  gtk_widget_grab_default (goto_center_command);
-
-  goto_airlift_command=gtk_accelbutton_new("Air_lift", accel);
-  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(goto_dialog_shell)->action_area),
-       goto_airlift_command, TRUE, TRUE, 0);
-  GTK_WIDGET_SET_FLAGS(goto_airlift_command, GTK_CAN_DEFAULT);
-
-  goto_all_toggle=gtk_toggle_button_new_with_label(_("All Cities"));
-  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(goto_dialog_shell)->action_area),
-       goto_all_toggle, TRUE, TRUE, 0);
-  GTK_WIDGET_SET_FLAGS(goto_all_toggle, GTK_CAN_DEFAULT);
-
-  goto_cancel_command=gtk_accelbutton_new(_("_Cancel"), accel);
-  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(goto_dialog_shell)->action_area),
-       goto_cancel_command, TRUE, TRUE, 0);
-  GTK_WIDGET_SET_FLAGS(goto_cancel_command, GTK_CAN_DEFAULT);
-
-  gtk_widget_add_accelerator(goto_cancel_command, "clicked",
-       accel, GDK_Escape, 0, 0);
-
-  gtk_signal_connect(GTK_OBJECT(goto_list), "select_row",
-               GTK_SIGNAL_FUNC(goto_list_callback), NULL);
-  gtk_signal_connect(GTK_OBJECT(goto_list), "unselect_row",
-               GTK_SIGNAL_FUNC(goto_list_ucallback), NULL);
-  gtk_signal_connect(GTK_OBJECT(goto_center_command), "clicked",
-               GTK_SIGNAL_FUNC(goto_goto_command_callback), NULL);
-  gtk_signal_connect(GTK_OBJECT(goto_airlift_command), "clicked",
-               GTK_SIGNAL_FUNC(goto_airlift_command_callback), NULL);
-  gtk_signal_connect(GTK_OBJECT(goto_all_toggle), "toggled",
-               GTK_SIGNAL_FUNC(goto_all_toggle_callback), NULL);
-  gtk_signal_connect(GTK_OBJECT(goto_cancel_command), "clicked",
-               GTK_SIGNAL_FUNC(goto_cancel_command_callback), NULL);
-
-  gtk_widget_show_all(GTK_DIALOG(goto_dialog_shell)->vbox);
-  gtk_widget_show_all(GTK_DIALOG(goto_dialog_shell)->action_area);
+  if (!dshell) {
+    create_goto_dialog();
+  }
 
-  update_goto_dialog(goto_list);
-  gtk_widget_show(goto_dialog_shell);
+  gtk_window_present(GTK_WINDOW(dshell));
 }
 
 /**************************************************************************
@@ -154,70 +222,52 @@
 **************************************************************************/
 static struct city *get_selected_city(void)
 {
-  GList *selection = GTK_CLIST(goto_list)->selection;
-  gchar *string;
-  int len;
+  GtkTreeModel *model;
+  GtkTreeIter it;
+  char *name;
 
-  if (!selection) {
+  if (!gtk_tree_selection_get_selected(selection, NULL, &it))
     return NULL;
-  }
-  
-  gtk_clist_get_text(GTK_CLIST(goto_list),
-                    GPOINTER_TO_INT(selection->data), 0, &string);
-
-  len = strlen(string);
-  if(len>3 && strcmp(string+len-3, "(A)")==0) {
-    char name[MAX_LEN_NAME];
-    mystrlcpy(name, string, MIN(sizeof(name),len-2));
-    return game_find_city_by_name(name);
-  }
-  return game_find_city_by_name(string);
+
+  model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
+
+  gtk_tree_model_get(model, &it, 0, &name, -1);
+  return game_find_city_by_name(name);
 }
 
 /**************************************************************************
 ...
 **************************************************************************/
-static void update_goto_dialog(GtkWidget *goto_list)
+static void update_goto_dialog(GtkToggleButton *button)
 {
-  int    i, j;
-  int    all_cities;
-  gchar *row   [1];
-  char   name  [MAX_LEN_NAME+3];
-  
-  all_cities=GTK_TOGGLE_BUTTON(goto_all_toggle)->active;
-
-  gtk_clist_freeze(GTK_CLIST(goto_list));
-  gtk_clist_clear(GTK_CLIST(goto_list));
-  
-  row[0]=name;
+  int i, j;
+  GtkTreeIter it;
+  gboolean all_cities;
+
+  all_cities = gtk_toggle_button_get_active(button);
+
+  gtk_list_store_clear(store);
 
   for(i=0, j=0; i<game.nplayers; i++) {
-    if(!all_cities && i!=game.player_idx) continue;
+    if (!all_cities && i!=game.player_idx)
+      continue;
+
     city_list_iterate(game.players[i].cities, pcity) {
-      sz_strlcpy(name, pcity->name);
-      if (pcity->improvements[B_AIRPORT] == I_ACTIVE)
-       sz_strlcat(name, "(A)");
-      gtk_clist_append( GTK_CLIST( goto_list ), row );
+      gtk_list_store_append(store, &it);
+
+      gtk_list_store_set(store, &it,
+        0, pcity->name,
+        1, (pcity->improvements[B_AIRPORT] == I_ACTIVE),
+        -1);
     }
     city_list_iterate_end;
   }
-  gtk_clist_thaw(GTK_CLIST(goto_list));
-  gtk_widget_show_all(goto_list);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void popdown_goto_dialog(void)
-{
-  gtk_widget_destroy(goto_dialog_shell);
-  gtk_widget_set_sensitive(top_vbox, TRUE);
 }
 
 /**************************************************************************
 ...
 **************************************************************************/
-static void goto_list_callback(GtkWidget *w, gint row, gint column)
+static void goto_selection_callback(GtkTreeSelection *selection, gpointer data)
 {
   struct city *pdestcity;
 
@@ -225,63 +275,9 @@
     struct unit *punit=get_unit_in_focus();
     center_tile_mapcanvas(pdestcity->x, pdestcity->y);
     if(punit && unit_can_airlift_to(punit, pdestcity)) {
-      gtk_widget_set_sensitive(goto_airlift_command, TRUE);
+      gtk_widget_set_sensitive(airlift_cmd, TRUE);
       return;
     }
   }
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void goto_list_ucallback(GtkWidget *w, gint row, gint column)
-{
-  gtk_widget_set_sensitive(goto_airlift_command, FALSE);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void goto_airlift_command_callback(GtkWidget *w, gpointer data)
-{
-  struct city *pdestcity=get_selected_city();
-  if (pdestcity) {
-    struct unit *punit=get_unit_in_focus();
-    if (punit) {
-      request_unit_airlift(punit, pdestcity);
-    }
-  }
-  popdown_goto_dialog();
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void goto_all_toggle_callback(GtkWidget *w, gpointer data)
-{
-  update_goto_dialog(goto_list);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void goto_goto_command_callback(GtkWidget *w, gpointer data)
-{
-  struct city *pdestcity=get_selected_city();
-  if (pdestcity) {
-    struct unit *punit=get_unit_in_focus();
-    if (punit) {
-      send_goto_unit(punit, pdestcity->x, pdestcity->y);
-    }
-  }
-  popdown_goto_dialog();
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void goto_cancel_command_callback(GtkWidget *w, gpointer data)
-{
-  center_tile_mapcanvas(original_x, original_y);
-  popdown_goto_dialog();
+  gtk_widget_set_sensitive(airlift_cmd, FALSE);
 }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#2970) GTK+ 2.0 client un-modalization, Vasco Alexandre Da Silva Costa via RT <=