Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2003:
[Freeciv-Dev] (PR#5125) Sorting in the players dialogs
Home

[Freeciv-Dev] (PR#5125) Sorting in the players dialogs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#5125) Sorting in the players dialogs
From: "Remi Bonnet" <remi.bonnet@xxxxxxxxxxx>
Date: Tue, 7 Oct 2003 13:12:18 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Patch updated in latest CVS.
Now, it also add a more logical sorting for reputation (by reputation
and not by alphabetical order)
diff -u -r -Xdiff_ignore ../freeciv/client/gui-gtk-2.0/plrdlg.c 
./client/gui-gtk-2.0/plrdlg.c
--- ../freeciv/client/gui-gtk-2.0/plrdlg.c      2003-07-23 15:46:02.000000000 
+0200
+++ ./client/gui-gtk-2.0/plrdlg.c       2003-10-07 21:15:44.000000000 +0200
@@ -26,6 +26,7 @@
 #include "diptreaty.h"
 #include "fcintl.h"
 #include "game.h"
+#include "log.h"
 #include "packets.h"
 #include "nation.h"
 #include "player.h"
@@ -56,6 +57,13 @@
 static GtkWidget *players_sship_command;
 static GtkListStore *store;
 
+static void players_sort_column_callback(GtkTreeSortable *tree, gpointer data);
+static gint diplomatic_state_compare_func(GtkTreeModel *model,
+                                         GtkTreeIter *a, GtkTreeIter *b,
+                                         gpointer user_data);
+static gint reputation_compare_func(GtkTreeModel *model,
+                                   GtkTreeIter *a, GtkTreeIter *b,
+                                   gpointer user_data);
 static void create_players_dialog(void);
 static void players_meet_callback(GtkMenuItem *item, gpointer data);
 static void players_war_callback(GtkMenuItem *item, gpointer data);
@@ -64,10 +72,15 @@
 static void players_sship_callback(GtkMenuItem *item, gpointer data);
 
 #define NUM_COLUMNS 14                /* number of columns in total */
-#define DEF_SORT_COLUMN 2             /* default sort column (2 = nation) */
+#define DEF_SEARCH_COLUMN 2
+#define DIPL_STATE_COLUMN 7           /* the column for diplomatic status */
+#define REPUTATION_COLUMN 9           /* the column for reputation */
 #define COLOR_COLUMN (NUM_COLUMNS)    /* color column */
 #define PLRNO_COLUMN (NUM_COLUMNS+1)  /* plrno column */
 
+static int def_sort_column = DIPL_STATE_COLUMN;     /* default sort column */
+static GtkSortType def_sort_order = GTK_SORT_ASCENDING;
+
 /**************************************************************************
 popup the dialog 10% inside the main-window 
 **************************************************************************/
@@ -99,6 +112,86 @@
 }
 
 /**************************************************************************
+  Callback call when the user select a column for sorting. It saves the user
+ choice for later.
+**************************************************************************/
+static void players_sort_column_callback(GtkTreeSortable *tree, gpointer data)
+{
+  gtk_tree_sortable_get_sort_column_id(tree, &def_sort_column, 
+                                            &def_sort_order);
+}
+
+/**************************************************************************
+  Compare function for dipl state. Even if the classment here will not be
+ accepted by everybody, it's probably better than alphatical order:
+  - You
+  - Allied
+  - War
+  - Cease-Fire
+  - Peace
+  - Neutral
+  - No Contact
+**************************************************************************/
+static gint diplomatic_state_compare_func(GtkTreeModel *model,
+                                         GtkTreeIter *a, GtkTreeIter *b,
+                                         gpointer user_data)
+{
+  int i;
+  gint plrno[2];
+  gint values[2];
+
+  /* Don't want to copy this block twice */
+  for (i = 0; i < 2; i++) {
+    gtk_tree_model_get(model, (i == 0)? a : b, PLRNO_COLUMN, &plrno[i], -1); 
+
+    if (game.player_ptr->player_no == plrno[i]) {
+      values[i] = 0;
+    } else {
+      switch(pplayer_get_diplstate(game.player_ptr, get_player(plrno[i]))
+            ->type) {
+      case DS_ALLIANCE:
+       values[i] = 1;
+       break;
+      case DS_WAR:
+       values[i] = 2;
+       break;
+      case DS_CEASEFIRE:
+       values[i] = 3;
+       break;
+      case DS_PEACE:
+       values[i] = 4;
+       break;
+      case DS_NEUTRAL:
+       values[i] = 5;
+       break;
+      case DS_NO_CONTACT:
+       values[i] = 6;
+       break;
+      default:
+       freelog(LOG_FATAL, "Unknown diplstate");
+       exit(EXIT_FAILURE);
+      }
+    }
+  }
+
+  return values[0] - values[1];
+}
+
+/**************************************************************************
+ Compare func for reputation
+**************************************************************************/
+static gint reputation_compare_func(GtkTreeModel *model, GtkTreeIter *a,
+                                   GtkTreeIter *b, gpointer user_data)
+{
+  gint plr_a, plr_b;
+
+  gtk_tree_model_get(model, a, PLRNO_COLUMN, &plr_a, -1);
+  gtk_tree_model_get(model, b, PLRNO_COLUMN, &plr_b, -1);
+
+  return get_player(plr_a)->reputation - get_player(plr_b)->reputation;
+}
+
+/**************************************************************************
 ...
 **************************************************************************/
 static void update_players_menu(void)
@@ -267,7 +360,8 @@
     gtk_tree_view_append_column(GTK_TREE_VIEW(players_list), col);
   }
 
-  gtk_tree_view_set_search_column(GTK_TREE_VIEW(players_list), 
DEF_SORT_COLUMN);
+  gtk_tree_view_set_search_column(GTK_TREE_VIEW(players_list), 
+                                 DEF_SEARCH_COLUMN);
 
   sw = gtk_scrolled_window_new(NULL, NULL);
   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
@@ -340,6 +434,22 @@
   g_signal_connect(players_sship_command, "activate",
     G_CALLBACK(players_sship_callback), NULL);
 
+  /* Reload all sorting stuff and add a more logical callback for the
+   * diplomatic status and reputation */
+  gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
+                                      def_sort_column,
+                                      def_sort_order);
+  g_signal_connect(store, "sort-column-changed",
+                  G_CALLBACK(players_sort_column_callback), NULL);
+  gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store),
+                                 DIPL_STATE_COLUMN,
+                                 diplomatic_state_compare_func,
+                                 NULL, NULL);  
+  gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store),
+                                 REPUTATION_COLUMN,
+                                 reputation_compare_func,
+                                 NULL, NULL);
+
   gtk_list_store_clear(store);
   update_players_dialog();
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#5125) Sorting in the players dialogs, Remi Bonnet <=