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

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

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] client option change callback (PR#1935)
From: Jason Short <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 17 Aug 2002 02:23:14 -0700 (PDT)

Per I. Mathisen wrote:

<snip...>

Style issue: Please use more descriptive names than "dflt", "cb" and "o".

Done (at least partially; "o" is pretty pervasive).

I've also changed the struct initializers to the "new" (C99?) format, which is far more readible IMO. Feel free to change the indentation as you like...

These changes to options.c|h can well be a separate patch. They give a
functionality that I too found that I need for dynamically changing the
sound plugin.

Attached.

Note that although the callback will be called for BOOLs, INTs, and STRs, the initializer only accepts a callback for STRs. This is a local issue - strings will need a callback soon, while ints and bools will not (but one could easily be added). GTK, GTK2, XAW, and WIN32 are supported; only GTK and XAW are tested (with the tileset switching code). The WIN32 implementation is a kludge; I'd recommend letting Andreas clean up and apply this separately (Andreas?).

jason
Index: client/options.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/options.c,v
retrieving revision 1.65
diff -u -r1.65 options.c
--- client/options.c    2002/08/15 20:24:08     1.65
+++ client/options.c    2002/08/17 09:17:08
@@ -68,28 +68,59 @@
 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) \
+  { name: #oname,                   \
+    description: desc,              \
+    type: COT_INT,                  \
+    p_int_value: &oname,            \
+    p_bool_value: NULL,             \
+    p_string_value: NULL,           \
+    string_length: 0,               \
+    change_callback: NULL,          \
+    p_string_vals: NULL,            \
+    p_gui_data: NULL                \
+  }
+#define GEN_BOOL_OPTION(oname, desc) \
+  { name: #oname,                    \
+    description: desc,               \
+    type: COT_BOOL,                  \
+    p_int_value: NULL,               \
+    p_bool_value: &oname,            \
+    p_string_value: NULL,            \
+    string_length: 0,                \
+    change_callback: NULL,           \
+    p_string_vals: NULL,             \
+    p_gui_data: NULL                 \
+  }
+#define GEN_STR_OPTION(oname, desc, str_defaults, callback) \
+  { name: #oname,                                           \
+    description: desc,                                      \
+    type: COT_STR,                                          \
+    p_int_value: NULL,                                      \
+    p_bool_value: NULL,                                     \
+    p_string_value: oname,                                  \
+    string_length: sizeof(oname),                           \
+    change_callback: callback,                              \
+    p_string_vals: str_defaults,                            \
+    p_gui_data: 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.20
diff -u -r1.20 options.h
--- client/options.h    2002/08/15 20:24:08     1.20
+++ client/options.h    2002/08/17 09:17:08
@@ -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/08/17 09:17:09
@@ -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.8
diff -u -r1.8 gamedlgs.c
--- client/gui-gtk-2.0/gamedlgs.c       2002/08/15 20:24:10     1.8
+++ client/gui-gtk-2.0/gamedlgs.c       2002/08/17 09:17:09
@@ -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);
+       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/08/17 09:17:09
@@ -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/08/17 09:17:09
@@ -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] client option change callback (PR#1935), Jason Short <=