Initial revision
[bpt/emacs.git] / lwlib / lwlib-utils.c
1 /* Defines some widget utility functions.
2 Copyright (C) 1992 Lucid, Inc.
3
4 This file is part of the Lucid Widget Library.
5
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <memory.h>
24
25 #include <X11/Xatom.h>
26 #include <X11/IntrinsicP.h>
27 #include <X11/ObjectP.h>
28 #include "lwlib-utils.h"
29
30 /* Redisplay the contents of the widget, without first clearing it. */
31 void
32 XtNoClearRefreshWidget (Widget widget)
33 {
34 XEvent event;
35
36 event.type = Expose;
37 event.xexpose.serial = 0;
38 event.xexpose.send_event = 0;
39 event.xexpose.display = XtDisplay (widget);
40 event.xexpose.window = XtWindow (widget);
41 event.xexpose.x = 0;
42 event.xexpose.y = 0;
43 event.xexpose.width = widget->core.width;
44 event.xexpose.height = widget->core.height;
45 event.xexpose.count = 0;
46
47 (*widget->core.widget_class->core_class.expose)
48 (widget, &event, (Region)NULL);
49 }
50
51
52 /*
53 * Apply a function to all the subwidgets of a given widget recursively.
54 */
55 void
56 XtApplyToWidgets (Widget w, XtApplyToWidgetsProc proc, XtPointer arg)
57 {
58 if (XtIsComposite (w))
59 {
60 CompositeWidget cw = (CompositeWidget) w;
61 /* We have to copy the children list before mapping over it, because
62 the procedure might add/delete elements, which would lose badly.
63 */
64 int nkids = cw->composite.num_children;
65 Widget *kids = (Widget *) malloc (sizeof (Widget) * nkids);
66 int i;
67 memcpy (kids, cw->composite.children, sizeof (Widget) * nkids);
68 for (i = 0; i < nkids; i++)
69 /* This prevent us from using gadgets, why is it here? */
70 /* if (XtIsWidget (kids [i])) */
71 {
72 /* do the kiddies first in case we're destroying */
73 XtApplyToWidgets (kids [i], proc, arg);
74 proc (kids [i], arg);
75 }
76 free (kids);
77 }
78 }
79
80
81 /*
82 * Apply a function to all the subwidgets of a given widget recursively.
83 * Stop as soon as the function returns non NULL and returns this as a value.
84 */
85 void *
86 XtApplyUntilToWidgets (Widget w, XtApplyUntilToWidgetsProc proc, XtPointer arg)
87 {
88 void* result;
89 if (XtIsComposite (w))
90 {
91 CompositeWidget cw = (CompositeWidget)w;
92 int i;
93 for (i = 0; i < cw->composite.num_children; i++)
94 if (XtIsWidget (cw->composite.children [i])){
95 result = proc (cw->composite.children [i], arg);
96 if (result)
97 return result;
98 result = XtApplyUntilToWidgets (cw->composite.children [i], proc,
99 arg);
100 if (result)
101 return result;
102 }
103 }
104 return NULL;
105 }
106
107
108 /*
109 * Returns a copy of the list of all children of a composite widget
110 */
111 Widget *
112 XtCompositeChildren (Widget widget, unsigned int* number)
113 {
114 CompositeWidget cw = (CompositeWidget)widget;
115 Widget* result;
116 int n;
117 int i;
118
119 if (!XtIsComposite (widget))
120 {
121 *number = 0;
122 return NULL;
123 }
124 n = cw->composite.num_children;
125 result = (Widget*)XtMalloc (n * sizeof (Widget));
126 *number = n;
127 for (i = 0; i < n; i++)
128 result [i] = cw->composite.children [i];
129 return result;
130 }
131
132 Boolean
133 XtWidgetBeingDestroyedP (Widget widget)
134 {
135 return widget->core.being_destroyed;
136 }
137
138 void
139 XtSafelyDestroyWidget (Widget widget)
140 {
141 #if 0
142
143 /* this requires IntrinsicI.h (actually, InitialI.h) */
144
145 XtAppContext app = XtWidgetToApplicationContext(widget);
146
147 if (app->dispatch_level == 0)
148 {
149 app->dispatch_level = 1;
150 XtDestroyWidget (widget);
151 /* generates an event so that the event loop will be called */
152 XChangeProperty (XtDisplay (widget), XtWindow (widget),
153 XA_STRING, XA_STRING, 32, PropModeAppend, NULL, 0);
154 app->dispatch_level = 0;
155 }
156 else
157 XtDestroyWidget (widget);
158
159 #else
160 abort ();
161 #endif
162 }