Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2002:
[Freeciv-Dev] (PR#1935) client option change callback patch updated
Home

[Freeciv-Dev] (PR#1935) client option change callback patch updated

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#1935) client option change callback patch updated
From: "Jason Short via RT" <rt@xxxxxxxxxxxxxx>
Date: Wed, 6 Nov 2002 23:43:00 -0800
Reply-to: rt@xxxxxxxxxxxxxx

Here's an update of this patch.

The patch provides for an optional callback when an option is changed. 
This is necessary when an option change requires external action - for 
instance, when choosing to switch tilesets.

Changes:

- Don't use gcc-extension struct initializer form.
- s/char */const char */ to prevent a compiler warning in the GTK2 client.

jason

(I sent this same e-mail yesterday.  But since I sent it with the wrong 
PR#, and later merged it into another wrong PR#, it was irretrievably 
put into the wrong place.  So here it is again.)

Index: client/options.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.c,v
retrieving revision 1.66
diff -u -r1.66 options.c
--- client/options.c    2002/11/01 18:11:42     1.66
+++ client/options.c    2002/11/06 03:07:28
@@ -68,28 +68,33 @@
 bool meta_accelerators = TRUE;
 bool map_scrollbars = TRUE;
 
-#define GEN_INT_OPTION(name, desc) { #name, desc, COT_INT, \
-                                     &name, NULL, NULL, 0, NULL, NULL }
-#define GEN_BOOL_OPTION(name, desc) { #name, desc, COT_BOOL, \
-                                      NULL, &name, NULL, 0, NULL, NULL }
-#define GEN_STR_OPTION(name, desc, dflt) { #name, desc, COT_STR, \
-                                     NULL, NULL, name, sizeof(name), \
-                                     dflt, NULL }
+#define GEN_INT_OPTION(oname, desc) { #oname, desc, COT_INT, \
+                                      &oname, NULL, NULL, 0, NULL, \
+                                       NULL, NULL }
+#define GEN_BOOL_OPTION(oname, desc) { #oname, desc, COT_BOOL, \
+                                       NULL, &oname, NULL, 0, NULL, \
+                                       NULL, NULL }
+#define GEN_STR_OPTION(oname, desc, str_defaults, callback) \
+                                    { #oname, desc, COT_STR, \
+                                      NULL, NULL, oname, sizeof(oname), \
+                                      callback, str_defaults, NULL }
 #define GEN_OPTION_TERMINATOR { NULL, NULL, COT_BOOL, \
                                 NULL, NULL, NULL, 0, NULL }
 
 client_option options[] = {
   GEN_STR_OPTION(default_player_name,       N_("Default player's username"),
-                NULL), 
-  GEN_STR_OPTION(default_server_host,       N_("Default server"), NULL),
+                NULL, NULL), 
+  GEN_STR_OPTION(default_server_host,       N_("Default server"),
+                NULL, NULL),
   GEN_INT_OPTION(default_server_port,       N_("Default server's port")),
-  GEN_STR_OPTION(default_metaserver,        N_("Default metaserver"), NULL),
-  GEN_STR_OPTION(default_tile_set_name,     N_("Default tileset"),
-                get_tileset_list),
+  GEN_STR_OPTION(default_metaserver,        N_("Default metaserver"),
+                NULL, NULL),
+  GEN_STR_OPTION(default_tile_set_name,     N_("Tileset"),
+                get_tileset_list, NULL),
   GEN_STR_OPTION(default_sound_set_name,    N_("Default name of sound set"),
-                get_soundset_list),
+                get_soundset_list, NULL),
   GEN_STR_OPTION(default_sound_plugin_name, N_("Default sound plugin"),
-                get_soundplugin_list),
+                get_soundplugin_list, NULL),
 
   GEN_BOOL_OPTION(solid_color_behind_units, N_("Solid unit background color")),
   GEN_BOOL_OPTION(sound_bell_at_new_turn,   N_("Sound bell at new turn")),
Index: client/options.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.h,v
retrieving revision 1.21
diff -u -r1.21 options.h
--- client/options.h    2002/11/01 18:11:42     1.21
+++ client/options.h    2002/11/06 03:07:28
@@ -51,7 +51,7 @@
   COT_STR
 };
 
-typedef struct {
+typedef struct client_option {
   char *name;
   char *description;
   enum client_option_type type;
@@ -59,6 +59,7 @@
   bool *p_bool_value;
   char *p_string_value;
   size_t string_length;
+  void (*change_callback) (struct client_option * option);
 
   /* 
    * A function to return a static NULL-terminated list of possible
Index: client/gui-gtk/gamedlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gamedlgs.c,v
retrieving revision 1.27
diff -u -r1.27 gamedlgs.c
--- client/gui-gtk/gamedlgs.c   2002/08/15 20:24:10     1.27
+++ client/gui-gtk/gamedlgs.c   2002/11/06 03:07:29
@@ -376,22 +376,36 @@
 {
   client_option *o;
   char *dp;
+  bool b;
+  int i;
 
   for (o=options; o->name; ++o) {
     switch (o->type) {
     case COT_BOOL:
+      b = *(o->p_bool_value);
       *(o->p_bool_value) = GTK_TOGGLE_BUTTON(o->p_gui_data)->active;
+      if (b != *(o->p_bool_value) && o->change_callback) {
+       (o->change_callback)(o);
+      }
       break;
     case COT_INT:
+      i = *(o->p_int_value);
       dp = gtk_entry_get_text(GTK_ENTRY(o->p_gui_data));
       sscanf(dp, "%d", o->p_int_value);
+      if (i != *(o->p_int_value) && o->change_callback) {
+       (o->change_callback)(o);
+      }
       break;
     case COT_STR:
       if (o->p_string_vals) {
-       mystrlcpy(o->p_string_value,
-                 gtk_entry_get_text(GTK_ENTRY
-                                    (GTK_COMBO(o->p_gui_data)->entry)),
-                 o->string_length);
+       char* new_value = gtk_entry_get_text(GTK_ENTRY
+                                           (GTK_COMBO(o->p_gui_data)->entry));
+       if (strcmp(o->p_string_value, new_value)) {
+         mystrlcpy(o->p_string_value, new_value, o->string_length);
+         if (o->change_callback) {
+           (o->change_callback)(o);
+         }
+       }
       } else {
        mystrlcpy(o->p_string_value,
                  gtk_entry_get_text(GTK_ENTRY(o->p_gui_data)),
Index: client/gui-gtk-2.0/gamedlgs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gamedlgs.c,v
retrieving revision 1.10
diff -u -r1.10 gamedlgs.c
--- client/gui-gtk-2.0/gamedlgs.c       2002/10/13 23:23:30     1.10
+++ client/gui-gtk-2.0/gamedlgs.c       2002/11/06 03:07:29
@@ -367,22 +367,36 @@
 {
   client_option *o;
   const char *dp;
+  bool b;
+  int i;
 
   for (o=options; o->name; ++o) {
     switch (o->type) {
     case COT_BOOL:
+      b = *(o->p_bool_value);
       *(o->p_bool_value) = GTK_TOGGLE_BUTTON(o->p_gui_data)->active;
+      if (b != *(o->p_bool_value) && o->change_callback) {
+       (o->change_callback)(o);
+      }
       break;
     case COT_INT:
+      i = *(o->p_int_value);
       dp = gtk_entry_get_text(GTK_ENTRY(o->p_gui_data));
       sscanf(dp, "%d", o->p_int_value);
+      if (i != *(o->p_int_value) && o->change_callback) {
+       (o->change_callback)(o);
+      }
       break;
     case COT_STR:
       if (o->p_string_vals) {
-       mystrlcpy(o->p_string_value,
-                 gtk_entry_get_text(GTK_ENTRY
-                                    (GTK_COMBO(o->p_gui_data)->entry)),
-                 o->string_length);
+       const char* new_value = gtk_entry_get_text(GTK_ENTRY
+                                       (GTK_COMBO(o->p_gui_data)->entry));
+       if (strcmp(o->p_string_value, new_value)) {
+         mystrlcpy(o->p_string_value, new_value, o->string_length);
+         if (o->change_callback) {
+           (o->change_callback)(o);
+         }
+       }
       } else {
        mystrlcpy(o->p_string_value,
                  gtk_entry_get_text(GTK_ENTRY(o->p_gui_data)),
Index: client/gui-win32/optiondlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/optiondlg.c,v
retrieving revision 1.9
diff -u -r1.9 optiondlg.c
--- client/gui-win32/optiondlg.c        2002/08/15 20:24:11     1.9
+++ client/gui-win32/optiondlg.c        2002/11/06 03:07:29
@@ -43,23 +43,40 @@
   case WM_COMMAND:
     if (LOWORD(wParam)==IDOK) {
       client_option *o;
-      char dp[20];
+      int len = MAX(o->string_length, 20); /* Make sure it fits */
+      char dp[len];
+      bool b;
+      int i;
       
       for (o=options; o->name; ++o) {
        switch (o->type) {
        case COT_BOOL:
+         b = *(o->p_bool_value);
          
*(o->p_bool_value)=(Button_GetCheck((HWND)(o->p_gui_data))==BST_CHECKED);
+         if (b != *(o->p_bool_value) && o->change_callback) {
+           (o->change_callback)(o);
+         }
          break;
        case COT_INT:
+         i = *(o->p_int_value);
          GetWindowText((HWND)(o->p_gui_data),dp,sizeof(dp));
          sscanf(dp, "%d", o->p_int_value);
+         if (i != *(o->p_int_value) && o->change_callback) {
+           (o->change_callback)(o);
+         }
          break;
        case COT_STR:
          if (!o->p_gui_data) {
            break;
          }
-         GetWindowText((HWND) (o->p_gui_data), o->p_string_value,
+         GetWindowText((HWND) (o->p_gui_data), dp,
                        o->string_length);
+         if (!strcmp(dp, o->p_string_value)) {
+           mystrlcpy(o->p_string_value, dp, o->string_length);
+           if (o->change_callback) {
+             (o->change_callback)(o);
+           }
+         }
          break;
        }
       }
Index: client/gui-xaw/optiondlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/optiondlg.c,v
retrieving revision 1.20
diff -u -r1.20 optiondlg.c
--- client/gui-xaw/optiondlg.c  2002/08/15 20:24:12     1.20
+++ client/gui-xaw/optiondlg.c  2002/11/06 03:07:29
@@ -255,24 +255,37 @@
                               XtPointer call_data)
 {
   Boolean b;
+  int i;
   client_option *o;
   XtPointer dp;
 
   for (o=options; o->name; ++o) {
     switch (o->type) {
     case COT_BOOL:
-      XtVaGetValues((Widget) o->p_gui_data, XtNstate, &b, NULL);
-      *(o->p_bool_value) = b;
+      b = *(o->p_bool_value);
+      XtVaGetValues((Widget) o->p_gui_data, XtNstate, o->p_bool_value, NULL);
+      if (b != *(o->p_bool_value) && o->change_callback) {
+       (o->change_callback)(o);
+      }
       break;
     case COT_INT:
+      i = *(o->p_int_value);
       XtVaGetValues(o->p_gui_data, XtNstring, &dp, NULL);
       sscanf(dp, "%d", o->p_int_value);
+      if (i != *(o->p_int_value) && o->change_callback) {
+       (o->change_callback)(o);
+      }
       break;
     case COT_STR:
       XtVaGetValues(o->p_gui_data,
                    o->p_string_vals ? "label" : XtNstring,
                    &dp, NULL);
-      mystrlcpy(o->p_string_value,dp,o->string_length);
+      if (strcmp(o->p_string_value, dp)) {
+       mystrlcpy(o->p_string_value, dp, o->string_length);
+       if (o->change_callback) {
+         (o->change_callback)(o);
+       }
+      }
       break;
     }
   }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#1935) client option change callback patch updated, Jason Short via RT <=