diff lwlib/lwlib.c @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents c5d627a313b1
children a86b2b5e0111
line wrap: on
line diff
--- a/lwlib/lwlib.c	Mon Aug 13 11:12:06 2007 +0200
+++ b/lwlib/lwlib.c	Mon Aug 13 11:13:30 2007 +0200
@@ -40,6 +40,9 @@
 #endif
 #ifdef NEED_MOTIF
 #include "lwlib-Xm.h"
+#ifdef LWLIB_WIDGETS_MOTIF
+#include <Xm/Xm.h>
+#endif
 #endif
 #ifdef NEED_ATHENA
 #include "lwlib-Xaw.h"
@@ -67,13 +70,13 @@
 
 
 /* Forward declarations */
-static void
-instantiate_widget_instance (widget_instance *instance);
+static void instantiate_widget_instance (widget_instance *instance);
+static void free_widget_value_args (widget_value* wv);
 
 
 /* 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;
@@ -108,7 +111,7 @@
     }
   if (wv)
     {
-      memset (wv, 0, sizeof (widget_value));
+      memset (wv, '\0', sizeof (widget_value));
     }
   return wv;
 }
@@ -125,8 +128,6 @@
   widget_value_free_list = wv;
 }
 
-static void free_widget_value_tree (widget_value *wv);
-
 static void
 free_widget_value_contents (widget_value *wv)
 {
@@ -155,6 +156,9 @@
       free_widget_value_tree (wv->contents);
       wv->contents = (widget_value *) 0xDEADBEEF;
     }
+
+  free_widget_value_args (wv);
+
   if (wv->next)
     {
       free_widget_value_tree (wv->next);
@@ -162,7 +166,7 @@
     }
 }
 
-static void
+void
 free_widget_value_tree (widget_value *wv)
 {
   if (!wv)
@@ -184,7 +188,7 @@
   if (val->scrollbar_data)
     *copy->scrollbar_data = *val->scrollbar_data;
   else
-    memset (copy->scrollbar_data, 0, sizeof (scrollbar_values));
+    memset (copy->scrollbar_data, '\0', sizeof (scrollbar_values));
 }
 
 /*
@@ -231,6 +235,41 @@
 
 #endif /* NEED_SCROLLBARS */
 
+#ifdef HAVE_WIDGETS
+/*
+ * Return true if old->args was not equivalent
+ * to new->args.
+ */
+static Boolean
+merge_widget_value_args (widget_value *old, widget_value *new)
+{
+  Boolean changed = False;
+
+  if (new->args && !old->args)
+    {
+      lw_copy_widget_value_args (new, old);
+      changed = True;
+    }
+  /* Generally we don't want to lose values that are already in the
+     widget. */
+  else if (!new->args && old->args)
+    {
+      lw_copy_widget_value_args (old, new);
+      changed = True;
+    }
+  else if (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;
+    }
+
+  return changed;
+}
+#endif /* HAVE_WIDGETS */
+
 /* Make a complete copy of a widget_value tree.  Store CHANGE into
    the widget_value tree's `change' field. */
 
@@ -263,6 +302,8 @@
       copy->next = copy_widget_value_tree (val->next, change);
       copy->toolkit_data = NULL;
       copy->free_toolkit_data = False;
+
+      lw_copy_widget_value_args (val, copy);
 #ifdef NEED_SCROLLBARS
       copy_scrollbar_values (val, copy);
 #endif
@@ -289,7 +330,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)
@@ -317,7 +358,7 @@
   safe_free_str (info->type);
   safe_free_str (info->name);
   free_widget_value_tree (info->val);
-  memset ((void*)info, 0xDEADBEEF, sizeof (widget_info));
+  memset (info, '\0', sizeof (widget_info));
   free (info);
 }
 
@@ -352,7 +393,7 @@
 static void
 free_widget_instance (widget_instance *instance)
 {
-  memset ((void *) instance, 0xDEADBEEF, sizeof (widget_instance));
+  memset (instance, '\0', sizeof (widget_instance));
   free (instance);
 }
 
@@ -462,7 +503,7 @@
 
 /* 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;
@@ -577,6 +618,14 @@
       change = max (change, INVISIBLE_CHANGE);
       val1->call_data = val2->call_data;
     }
+#ifdef HAVE_WIDGETS
+  if (merge_widget_value_args (val1, val2))
+    {
+      EXPLAIN (val1->name, change, VISIBLE_CHANGE, "widget change", 0, 0);
+      change = max (change, VISIBLE_CHANGE);
+    }
+#endif
+
 #ifdef NEED_SCROLLBARS
   if (merge_scrollbar_values (val1, val2))
     {
@@ -641,7 +690,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;
 
@@ -778,7 +827,7 @@
 
 
 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++)
@@ -788,7 +837,7 @@
 }
 
 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] */
@@ -872,7 +921,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)
@@ -908,7 +957,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)
@@ -1300,3 +1349,71 @@
 	}
     }
 }
+
+void lw_add_value_args_to_args (widget_value* wv, ArgList addto, int* offset)
+{
+  int i;
+  if (wv->args && wv->args->nargs)
+    {
+      for (i = 0; i<wv->args->nargs; i++)
+	{
+	  addto[i + *offset] = wv->args->args[i];
+	}
+      *offset += wv->args->nargs;
+    }
+}
+
+void lw_add_widget_value_arg (widget_value* wv, String name, XtArgVal value)
+{
+  if (!wv->args)
+    {
+      wv->args = (widget_args *) malloc (sizeof (widget_args));
+      memset (wv->args, '\0', sizeof (widget_args));
+      wv->args->ref_count = 1;
+      wv->args->nargs = 0;
+      wv->args->args = (ArgList) malloc (sizeof (Arg) * 10);
+      memset (wv->args->args, '\0', sizeof (Arg) * 10);
+    }
+  
+  if (wv->args->nargs > 10)
+    return;
+
+  XtSetArg (wv->args->args [wv->args->nargs], name, value);   wv->args->nargs++;
+}
+
+static void free_widget_value_args (widget_value* wv)
+{
+  if (wv->args)
+    {
+      if (--wv->args->ref_count <= 0)
+	{
+#ifdef LWLIB_WIDGETS_MOTIF
+	  int i;
+	  for (i = 0; i < wv->args->nargs; i++)
+	    {
+	      if (!strcmp (wv->args->args[i].name, XmNfontList))
+		XmFontListFree ((XmFontList)wv->args->args[i].value);
+	    }
+#endif
+	  free (wv->args->args);
+	  free (wv->args);
+	  wv->args = (widget_args*)0xDEADBEEF;
+	}
+    }
+}
+
+void lw_copy_widget_value_args (widget_value* val, widget_value* copy)
+{
+  if (!val->args)
+    {
+      if (copy->args)
+	free_widget_value_args (copy);
+      copy->args = 0;
+    }
+  else
+    {
+      copy->args = val->args;
+      copy->args->ref_count++;
+    }
+}
+