Coverity fixes from Nelson
[sxemacs] / src / ui / X11 / EmacsManager.c
1 /* Emacs manager widget.
2    Copyright (C) 1993-1995 Sun Microsystems, Inc.
3    Copyright (C) 1995 Ben Wing.
4
5 This file is part of SXEmacs
6
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Synched up with: Not in FSF. */
22
23 /* Written by Ben Wing, May, 1994. */
24
25 #include <config.h>
26
27 #include <X11/StringDefs.h>
28 #include "EmacsManagerP.h"
29 #ifdef LWLIB_MENUBARS_MOTIF
30 #include <Xm/RowColumn.h>
31 #endif                          /* LWLIB_MENUBARS_MOTIF */
32
33 /* For I, Emacs, am a kind god.  Unlike the goddess Athena and the
34    Titan Motif, I require no ritual sacrifices to placate the lesser
35    daemons of geometry management. */
36
37 static XtResource resources[] = {
38 #define offset(field) XtOffset(EmacsManagerWidget, emacs_manager.field)
39         {XtNresizeCallback, XtCCallback,
40          XtRCallback, sizeof(XtCallbackList),
41          offset(resize_callback), XtRImmediate, (XtPointer) 0}
42         ,
43         {XtNqueryGeometryCallback, XtCCallback,
44          XtRCallback, sizeof(XtCallbackList),
45          offset(query_geometry_callback), XtRImmediate, (XtPointer) 0}
46         ,
47         {XtNuserData, XtCUserData,
48          XtRPointer, sizeof(XtPointer),
49          offset(user_data), XtRImmediate, (XtPointer) 0}
50         ,
51 };
52
53 /****************************************************************
54  *
55  * Full class record constant
56  *
57  ****************************************************************/
58
59 static XtGeometryResult QueryGeometry(Widget wid,
60                                       XtWidgetGeometry * request,
61                                       XtWidgetGeometry * reply);
62 static void Resize(Widget w);
63 static XtGeometryResult GeometryManager(Widget w, XtWidgetGeometry * request,
64                                         XtWidgetGeometry * reply);
65 static void ChangeManaged(Widget w);
66 static void Realize(Widget w, Mask * valueMask,
67                     XSetWindowAttributes * attributes);
68 static void ClassInitialize(void);
69
70 EmacsManagerClassRec emacsManagerClassRec = {
71         {
72 /* core_class fields      */
73 #ifdef LWLIB_USES_MOTIF
74          /* superclass         */ (WidgetClass) & xmManagerClassRec,
75 #else
76          /* superclass         */ (WidgetClass) & compositeClassRec,
77 #endif
78          /* class_name         */ "EmacsManager",
79          /* widget_size        */ sizeof(EmacsManagerRec),
80          /* class_initialize   */ ClassInitialize,
81          /* class_part_init    */ NULL,
82          /* class_inited       */ FALSE,
83          /* initialize         */ NULL,
84          /* initialize_hook    */ NULL,
85          /* realize            */ Realize,
86          /* actions            */ NULL,
87          /* num_actions        */ 0,
88          /* resources          */ resources,
89          /* num_resources      */ XtNumber(resources),
90          /* xrm_class          */ NULLQUARK,
91          /* compress_motion    */ TRUE,
92          /* compress_exposure  */ XtExposeCompressMaximal | XtExposeNoRegion,
93          /* compress_enterleave */ TRUE,
94          /* visible_interest   */ FALSE,
95          /* destroy            */ NULL,
96          /* resize             */ Resize,
97          /* expose             */ NULL,
98          /* set_values         */ NULL,
99          /* set_values_hook    */ NULL,
100          /* set_values_almost  */ XtInheritSetValuesAlmost,
101          /* get_values_hook    */ NULL,
102          /* accept_focus       */ NULL,
103          /* version            */ XtVersion,
104          /* callback_private   */ NULL,
105          /* tm_table           */ XtInheritTranslations,
106          /* query_geometry     */ QueryGeometry,
107          /* display_accelerator */ XtInheritDisplayAccelerator,
108          /* extension          */ NULL
109          }
110         ,
111         {
112 /* composite_class fields */
113          /* geometry_manager   */ GeometryManager,
114          /* change_managed     */ ChangeManaged,
115          /* insert_child       */ XtInheritInsertChild,
116          /* delete_child       */ XtInheritDeleteChild,
117          /* extension          */ NULL
118          }
119         ,
120 #ifdef LWLIB_USES_MOTIF
121         {
122          /* constraint_class fields */
123          NULL,                  /* resource list        */
124          0,                     /* num resources        */
125          0,                     /* constraint size      */
126          (XtInitProc) NULL,     /* init proc            */
127          (XtWidgetProc) NULL,   /* destroy proc         */
128          (XtSetValuesFunc) NULL,        /* set values proc      */
129          NULL,                  /* extension            */
130          }
131         ,
132         {
133 /* manager_class fields */
134          XtInheritTranslations, /* translations           */
135          NULL,                  /* syn_resources          */
136          0,                     /* num_syn_resources      */
137          NULL,                  /* syn_cont_resources     */
138          0,                     /* num_syn_cont_resources */
139          XmInheritParentProcess,        /* parent_process         */
140          NULL,                  /* extension              */
141          }
142         ,
143 #endif
144         {
145 /* emacs_manager_class fields */
146          /* empty              */ 0,
147          }
148 };
149
150 WidgetClass emacsManagerWidgetClass = (WidgetClass) & emacsManagerClassRec;
151
152 /* What is my preferred size?  A suggested size may be given. */
153
154 static XtGeometryResult
155 QueryGeometry(Widget w, XtWidgetGeometry * request, XtWidgetGeometry * reply)
156 {
157         EmacsManagerWidget emw = (EmacsManagerWidget) w;
158         EmacsManagerQueryGeometryStruct struc;
159         int request_mode = request->request_mode;
160
161         struc.request_mode = request_mode;
162         struc.proposed_width = (request_mode & CWWidth) ? request->width : 0;
163         struc.proposed_height = (request_mode & CWHeight) ? request->height : 0;
164         XtCallCallbackList(w, emw->emacs_manager.query_geometry_callback,
165                            &struc);
166         reply->request_mode = CWWidth | CWHeight;
167         reply->width = struc.proposed_width;
168         reply->height = struc.proposed_height;
169         if (((request_mode & CWWidth) && (request->width != reply->width)) ||
170             ((request_mode & CWHeight) && (request->height != reply->height)))
171                 return XtGeometryAlmost;
172         return XtGeometryYes;
173 }
174
175 static void Resize(Widget w)
176 {
177         EmacsManagerWidget emw = (EmacsManagerWidget) w;
178         EmacsManagerResizeStruct struc;
179
180         struc.width = w->core.width;
181         struc.height = w->core.height;
182         XtCallCallbackList(w, emw->emacs_manager.resize_callback, &struc);
183 }
184
185 static XtGeometryResult
186 GeometryManager(Widget w, XtWidgetGeometry * request, XtWidgetGeometry * reply)
187 {
188         /* Sure, any changes are fine. */
189
190 #ifdef LWLIB_MENUBARS_MOTIF
191         /* The Motif menubar will merrily request a new size every time a
192            child is added or deleted.  Blow it off because it doesn't know
193            what it's talking about. */
194         if (XtClass(w) != xmRowColumnWidgetClass)
195 #endif                          /* LWLIB_MENUBARS_MOTIF */
196         {
197                 if (request->request_mode & CWWidth)
198                         w->core.width = request->width;
199                 if (request->request_mode & CWHeight)
200                         w->core.height = request->height;
201         }
202         if (request->request_mode & CWBorderWidth)
203                 w->core.border_width = request->border_width;
204         if (request->request_mode & CWX)
205                 w->core.x = request->x;
206         if (request->request_mode & CWY)
207                 w->core.y = request->y;
208
209         return XtGeometryYes;
210 }
211
212 static void ChangeManaged(Widget w)
213 {
214         if (!XtIsRealized(w)) {
215                 XtWidgetGeometry request, reply;
216
217                 /* find out how big we'd like to be ... */
218
219                 request.request_mode = 0;
220                 XtQueryGeometry(w, &request, &reply);
221                 EmacsManagerChangeSize(w, reply.width, reply.height);
222         }
223 }
224
225 static void
226 Realize(Widget w, Mask * valueMask, XSetWindowAttributes * attributes)
227 {
228         attributes->bit_gravity = NorthWestGravity;
229         *valueMask |= CWBitGravity;
230
231         XtCreateWindow(w, (unsigned)InputOutput, (Visual *) CopyFromParent,
232                        *valueMask, attributes);
233 }
234
235 static void ClassInitialize(void)
236 {
237         return;
238 }
239
240 void EmacsManagerChangeSize(Widget w, Dimension width, Dimension height)
241 {
242         if (width == 0)
243                 width = w->core.width;
244         if (height == 0)
245                 height = w->core.height;
246
247         /* do nothing if we're already that size */
248         if (w->core.width != width || w->core.height != height) {
249                 XtGeometryResult result =
250                     XtMakeResizeRequest(w, width, height, &w->core.width,
251                                         &w->core.height);
252                 if (result == XtGeometryNo)
253                         return;
254                 if (result == XtGeometryAlmost)
255                         XtMakeResizeRequest(w, w->core.width, w->core.height,
256                                             NULL, NULL);
257                 Resize(w);
258         }
259 }