If HAVE_CONFIG_H, include config.h.
[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 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <X11/Xatom.h>
25 #include <X11/IntrinsicP.h>
26 #include <X11/ObjectP.h>
27 #include "lwlib-utils.h"
28
29 /* Redisplay the contents of the widget, without first clearing it. */
30 void
31 XtNoClearRefreshWidget (widget)
32 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 (w, proc, arg)
57 Widget w;
58 XtApplyToWidgetsProc proc;
59 XtPointer arg;
60 {
61 if (XtIsComposite (w))
62 {
63 CompositeWidget cw = (CompositeWidget) w;
64 /* We have to copy the children list before mapping over it, because
65 the procedure might add/delete elements, which would lose badly.
66 */
67 int nkids = cw->composite.num_children;
68 Widget *kids = (Widget *) malloc (sizeof (Widget) * nkids);
69 int i;
70 lwlib_bcopy (cw->composite.children, kids, sizeof (Widget) * nkids);
71 for (i = 0; i < nkids; i++)
72 /* This prevent us from using gadgets, why is it here? */
73 /* if (XtIsWidget (kids [i])) */
74 {
75 /* do the kiddies first in case we're destroying */
76 XtApplyToWidgets (kids [i], proc, arg);
77 proc (kids [i], arg);
78 }
79 free (kids);
80 }
81 }
82
83
84 /*
85 * Apply a function to all the subwidgets of a given widget recursively.
86 * Stop as soon as the function returns non NULL and returns this as a value.
87 */
88 void *
89 XtApplyUntilToWidgets (w, proc, arg)
90 Widget w;
91 XtApplyUntilToWidgetsProc proc;
92 XtPointer arg;
93 {
94 void* result;
95 if (XtIsComposite (w))
96 {
97 CompositeWidget cw = (CompositeWidget)w;
98 int i;
99 for (i = 0; i < cw->composite.num_children; i++)
100 if (XtIsWidget (cw->composite.children [i])){
101 result = proc (cw->composite.children [i], arg);
102 if (result)
103 return result;
104 result = XtApplyUntilToWidgets (cw->composite.children [i], proc,
105 arg);
106 if (result)
107 return result;
108 }
109 }
110 return NULL;
111 }
112
113
114 /*
115 * Returns a copy of the list of all children of a composite widget
116 */
117 Widget *
118 XtCompositeChildren (widget, number)
119 Widget widget;
120 unsigned int* number;
121 {
122 CompositeWidget cw = (CompositeWidget)widget;
123 Widget* result;
124 int n;
125 int i;
126
127 if (!XtIsComposite (widget))
128 {
129 *number = 0;
130 return NULL;
131 }
132 n = cw->composite.num_children;
133 result = (Widget*)XtMalloc (n * sizeof (Widget));
134 *number = n;
135 for (i = 0; i < n; i++)
136 result [i] = cw->composite.children [i];
137 return result;
138 }
139
140 Boolean
141 XtWidgetBeingDestroyedP (widget)
142 Widget widget;
143 {
144 return widget->core.being_destroyed;
145 }
146
147 void
148 XtSafelyDestroyWidget (widget)
149 Widget widget;
150 {
151 #if 0
152
153 /* this requires IntrinsicI.h (actually, InitialI.h) */
154
155 XtAppContext app = XtWidgetToApplicationContext(widget);
156
157 if (app->dispatch_level == 0)
158 {
159 app->dispatch_level = 1;
160 XtDestroyWidget (widget);
161 /* generates an event so that the event loop will be called */
162 XChangeProperty (XtDisplay (widget), XtWindow (widget),
163 XA_STRING, XA_STRING, 32, PropModeAppend, NULL, 0);
164 app->dispatch_level = 0;
165 }
166 else
167 XtDestroyWidget (widget);
168
169 #else
170 abort ();
171 #endif
172 }