diff lwlib/lwlib.c @ 442:abe6d1db359e r21-2-36

Import from CVS: tag r21-2-36
author cvs
date Mon, 13 Aug 2007 11:35:02 +0200
parents 84b14dcb0985
children 98528da0b7fc
line wrap: on
line diff
--- a/lwlib/lwlib.c	Mon Aug 13 11:33:40 2007 +0200
+++ b/lwlib/lwlib.c	Mon Aug 13 11:35:02 2007 +0200
@@ -76,7 +76,7 @@
 
 /* utility functions for widget_instance and widget_info */
 static char *
-safe_strdup (CONST char *s)
+safe_strdup (const char *s)
 {
   char *result;
   if (! s) return 0;
@@ -257,11 +257,10 @@
       lw_copy_widget_value_args (old, new);
       changed = True;
     }
-  else if (new->args && old->args)
+  else if (new->args && old->args && new->args != old->args)
     {
       /* #### Do something more sensible here than just copying the
          new values (like actually merging the values). */
-      free_widget_value_args (old);
       lw_copy_widget_value_args (new, old);
       changed = True;
     }
@@ -273,7 +272,7 @@
 /* Make a complete copy of a widget_value tree.  Store CHANGE into
    the widget_value tree's `change' field. */
 
-static widget_value *
+widget_value *
 copy_widget_value_tree (widget_value *val, change_type change)
 {
   widget_value *copy;
@@ -330,7 +329,7 @@
 }
 
 static widget_info *
-allocate_widget_info (CONST char *type, CONST char *name,
+allocate_widget_info (const char *type, const char *name,
                       LWLIB_ID id, widget_value *val,
 		      lw_callback pre_activate_cb, lw_callback selection_cb,
 		      lw_callback post_activate_cb)
@@ -503,13 +502,13 @@
 
 /* utility function for widget_value */
 static Boolean
-safe_strcmp (CONST char *s1, CONST char *s2)
+safe_strcmp (const char *s1, const char *s2)
 {
   if (!!s1 ^ !!s2) return True;
   return (s1 && s2) ? strcmp (s1, s2) : s1 ? False : !!s2;
 }
 
-#ifndef WINDOWSNT
+#ifndef WIN32_NATIVE
 static change_type
 max (change_type i1, change_type i2)
 {
@@ -690,7 +689,7 @@
 
 /* modifying the widgets */
 static Widget
-name_to_widget (widget_instance *instance, CONST char *name)
+name_to_widget (widget_instance *instance, const char *name)
 {
   Widget widget = NULL;
 
@@ -825,19 +824,34 @@
     val->change = NO_CHANGE;
 }
 
+/* strcasecmp() is not sufficiently portable or standard,
+   and it's easier just to write our own. */
+static int
+ascii_strcasecmp (const char *s1, const char *s2)
+{
+  while (1)
+    {
+      char c1 = *s1++;
+      char c2 = *s2++;
+      if (c1 >= 'A' && c1 <= 'Z') c1 += 'a' - 'A';
+      if (c2 >= 'A' && c2 <= 'Z') c2 += 'a' - 'A';
+      if (c1 != c2) return c1 - c2;
+      if (c1 == '\0') return 0;
+    }
+}
 
 static widget_creation_function
-find_in_table (CONST char *type, widget_creation_entry *table)
+find_in_table (const char *type, widget_creation_entry *table)
 {
   widget_creation_entry *cur;
   for (cur = table; cur->type; cur++)
-    if (!strcasecmp (type, cur->type))
+    if (!ascii_strcasecmp (type, cur->type))
       return cur->function;
   return NULL;
 }
 
 static Boolean
-dialog_spec_p (CONST char *name)
+dialog_spec_p (const char *name)
 {
   /* return True if name matches [EILPQeilpq][1-9][Bb] or
      [EILPQeilpq][1-9][Bb][Rr][1-9] */
@@ -921,7 +935,7 @@
 }
 
 void
-lw_register_widget (CONST char *type, CONST char *name,
+lw_register_widget (const char *type, const char *name,
                     LWLIB_ID id, widget_value *val,
 		    lw_callback pre_activate_cb, lw_callback selection_cb,
 		    lw_callback post_activate_cb)
@@ -957,7 +971,7 @@
 }
 
 Widget
-lw_create_widget (CONST char *type, CONST char *name,
+lw_create_widget (const char *type, const char *name,
                   LWLIB_ID id, widget_value *val,
 		  Widget parent, Boolean pop_up_p, lw_callback pre_activate_cb,
 		  lw_callback selection_cb, lw_callback post_activate_cb)
@@ -1049,14 +1063,14 @@
 }
 
 void
-lw_destroy_everything ()
+lw_destroy_everything (void)
 {
   while (all_widget_info)
     lw_destroy_all_widgets (all_widget_info->id);
 }
 
 void
-lw_destroy_all_pop_ups ()
+lw_destroy_all_pop_ups (void)
 {
   widget_info *info;
   widget_info *next;
@@ -1365,6 +1379,7 @@
 
 void lw_add_widget_value_arg (widget_value* wv, String name, XtArgVal value)
 {
+  int i = 0;
   if (!wv->args)
     {
       wv->args = (widget_args *) malloc (sizeof (widget_args));
@@ -1378,7 +1393,19 @@
   if (wv->args->nargs > 10)
     return;
 
-  XtSetArg (wv->args->args [wv->args->nargs], name, value);   wv->args->nargs++;
+  /* If the arg is already there then we must replace it. */
+  for (i = 0; i < wv->args->nargs; i++)
+    {
+      if (!strcmp (wv->args->args[i].name, name))
+	{
+	  XtSetArg (wv->args->args [i], name, value);
+	  break;
+	}
+    }
+  if (i >= wv->args->nargs)
+    {
+      XtSetArg (wv->args->args [wv->args->nargs], name, value);   wv->args->nargs++;
+    }
 }
 
 static void free_widget_value_args (widget_value* wv)
@@ -1397,23 +1424,46 @@
 #endif
 	  free (wv->args->args);
 	  free (wv->args);
-	  wv->args = (widget_args*)0xDEADBEEF;
+	  wv->args = 0;
 	}
     }
 }
 
 void lw_copy_widget_value_args (widget_value* val, widget_value* copy)
 {
-  if (!val->args)
+  if (val == copy || val->args == copy->args)
+    return;
+
+  if (copy->args)
     {
-      if (copy->args)
-	free_widget_value_args (copy);
-      copy->args = 0;
+      free_widget_value_args (copy);
     }
-  else
+
+  if (val->args)
     {
       copy->args = val->args;
       copy->args->ref_count++;
     }
 }
 
+/* Remove %_ and convert %% to %.  We can do this in-place because we
+   are always shortening, never lengthening, the string. */
+void
+lw_remove_accelerator_spec (char *val)
+{
+  char *foo = val, *bar = val;
+
+  while (*bar)
+    {
+      if (*bar == '%' && *(bar+1) == '_')
+	bar += 2;
+      else if (*bar == '%' && *(bar+1) == '%')
+	{
+	  *foo++ = *bar++;
+	  bar++;
+	}
+      else
+	*foo++ = *bar++;
+    }
+  *foo = '\0';
+}