Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Widgets / Simple.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $Id: Simple.c,v 1.1 1992/03/20 13:52:41 jbm Exp $
25  *
26  * Copyright (c) 1991 HaL Computer Systems, Inc.  All rights reserved.
27  * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28  * States.  Use of a copyright notice is precautionary only and does not
29  * imply publication or disclosure.
30  * 
31  * This software contains confidential information and trade secrets of HaL
32  * Computer Systems, Inc.  Use, disclosure, or reproduction is prohibited
33  * without the prior express written permission of HaL Computer Systems, Inc.
34  * 
35  *                         RESTRICTED RIGHTS LEGEND
36  * Use, duplication, or disclosure by the Government is subject to
37  * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38  * Technical Data and Computer Software clause at DFARS 252.227-7013.
39  *                        HaL Computer Systems, Inc.
40  *                  1315 Dell Avenue, Campbell, CA  95008
41  * 
42  */
43
44 /* $XConsortium: Simple.c,v 1.33 91/07/21 13:55:23 converse Exp $ */
45
46 /***********************************************************
47 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
48 and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
49
50                         All Rights Reserved
51
52 Permission to use, copy, modify, and distribute this software and its 
53 documentation for any purpose and without fee is hereby granted, 
54 provided that the above copyright notice appear in all copies and that
55 both that copyright notice and this permission notice appear in 
56 supporting documentation, and that the names of Digital or MIT not be
57 used in advertising or publicity pertaining to distribution of the
58 software without specific, written prior permission.  
59
60 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
61 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
62 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
63 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
64 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
65 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
66 SOFTWARE.
67
68 ******************************************************************/
69
70 #include <stdio.h>
71 #include <X11/IntrinsicP.h>
72 #include <X11/StringDefs.h>
73 #include <X11/Xaw/XawInit.h>
74 #include <X11/Xaw/SimpleP.h>
75 #include <X11/Xmu/Drawing.h>
76
77 #define offset(field) XtOffsetOf(SimpleRec, simple.field)
78
79 static XtResource resources[] = {
80   {XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
81      offset(cursor), XtRImmediate, (XtPointer) None},
82   {XtNinsensitiveBorder, XtCInsensitive, XtRPixmap, sizeof(Pixmap),
83      offset(insensitive_border), XtRImmediate, (XtPointer) NULL},
84   {XtNpointerColor, XtCForeground, XtRPixel, sizeof(Pixel),
85      offset(pointer_fg), XtRString, XtDefaultForeground},
86   {XtNpointerColorBackground, XtCBackground, XtRPixel, sizeof(Pixel),
87      offset(pointer_bg), XtRString, XtDefaultBackground},
88   {XtNcursorName, XtCCursor, XtRString, sizeof(String),
89      offset(cursor_name), XtRString, NULL},
90 #undef offset
91 };
92
93 static void ClassPartInitialize(), ClassInitialize(),Realize(),ConvertCursor();
94 static Boolean SetValues(), ChangeSensitive();
95
96 SimpleClassRec simpleClassRec = {
97   { /* core fields */
98     /* superclass               */      (WidgetClass) &widgetClassRec,
99     /* class_name               */      "Simple",
100     /* widget_size              */      sizeof(SimpleRec),
101     /* class_initialize         */      ClassInitialize,
102     /* class_part_initialize    */      ClassPartInitialize,
103     /* class_inited             */      FALSE,
104     /* initialize               */      NULL,
105     /* initialize_hook          */      NULL,
106     /* realize                  */      Realize,
107     /* actions                  */      NULL,
108     /* num_actions              */      0,
109     /* resources                */      resources,
110     /* num_resources            */      XtNumber(resources),
111     /* xrm_class                */      NULLQUARK,
112     /* compress_motion          */      TRUE,
113     /* compress_exposure        */      TRUE,
114     /* compress_enterleave      */      TRUE,
115     /* visible_interest         */      FALSE,
116     /* destroy                  */      NULL,
117     /* resize                   */      NULL,
118     /* expose                   */      NULL,
119     /* set_values               */      SetValues,
120     /* set_values_hook          */      NULL,
121     /* set_values_almost        */      XtInheritSetValuesAlmost,
122     /* get_values_hook          */      NULL,
123     /* accept_focus             */      NULL,
124     /* version                  */      XtVersion,
125     /* callback_private         */      NULL,
126     /* tm_table                 */      NULL,
127     /* query_geometry           */      XtInheritQueryGeometry,
128     /* display_accelerator      */      XtInheritDisplayAccelerator,
129     /* extension                */      NULL
130   },
131   { /* simple fields */
132     /* change_sensitive         */      ChangeSensitive
133   }
134 };
135
136 WidgetClass simpleWidgetClass = (WidgetClass)&simpleClassRec;
137
138 static void ClassInitialize()
139 {
140     static XtConvertArgRec convertArg[] = {
141         {XtWidgetBaseOffset, (XtPointer) XtOffsetOf(WidgetRec, core.screen),
142              sizeof(Screen *)},
143         {XtResourceString, (XtPointer) XtNpointerColor, sizeof(Pixel)},
144         {XtResourceString, (XtPointer) XtNpointerColorBackground, 
145              sizeof(Pixel)},
146         {XtWidgetBaseOffset, (XtPointer) XtOffsetOf(WidgetRec, core.colormap),
147              sizeof(Colormap)}
148     };
149
150     XawInitializeWidgetSet();
151     XtSetTypeConverter( XtRString, XtRColorCursor, XmuCvtStringToColorCursor,
152                        convertArg, XtNumber(convertArg), 
153                        XtCacheByDisplay, NULL);
154 }
155
156 static void ClassPartInitialize(class)
157     WidgetClass class;
158 {
159     register SimpleWidgetClass c = (SimpleWidgetClass)class;
160     register SimpleWidgetClass super = (SimpleWidgetClass) 
161         c->core_class.superclass;
162
163     if (c->simple_class.change_sensitive == NULL) {
164         char buf[BUFSIZ];
165
166         sprintf(buf,
167                 "%s Widget: The Simple Widget class method 'change_sensitive' is undefined.\nA function must be defined or inherited.",
168                 c->core_class.class_name);
169         XtWarning(buf);
170         c->simple_class.change_sensitive = ChangeSensitive;
171     }
172
173     if (c->simple_class.change_sensitive == XtInheritChangeSensitive)
174         c->simple_class.change_sensitive = super->simple_class.change_sensitive;
175 }
176
177 static void Realize(w, valueMask, attributes)
178     register Widget w;
179     Mask *valueMask;
180     XSetWindowAttributes *attributes;
181 {
182     Pixmap border_pixmap;
183
184     if (!XtIsSensitive(w)) {
185         /* change border to gray; have to remember the old one,
186          * so XtDestroyWidget deletes the proper one */
187         if (((SimpleWidget)w)->simple.insensitive_border == None)
188             ((SimpleWidget)w)->simple.insensitive_border =
189                 XmuCreateStippledPixmap(XtScreen(w),
190                                         w->core.border_pixel, 
191                                         w->core.background_pixel,
192                                         w->core.depth);
193         border_pixmap = w->core.border_pixmap;
194         attributes->border_pixmap =
195           w->core.border_pixmap = ((SimpleWidget)w)->simple.insensitive_border;
196
197         *valueMask |= CWBorderPixmap;
198         *valueMask &= ~CWBorderPixel;
199     }
200
201     ConvertCursor(w);
202
203     if ((attributes->cursor = ((SimpleWidget)w)->simple.cursor) != None)
204         *valueMask |= CWCursor;
205
206     XtCreateWindow( w, (unsigned int)InputOutput, (Visual *)CopyFromParent,
207                     *valueMask, attributes );
208
209     if (!XtIsSensitive(w))
210         w->core.border_pixmap = border_pixmap;
211 }
212
213 /*      Function Name: ConvertCursor
214  *      Description: Converts a name to a new cursor.
215  *      Arguments: w - the simple widget.
216  *      Returns: none.
217  */
218
219 static void
220 ConvertCursor(w)
221 Widget w;
222 {
223     SimpleWidget simple = (SimpleWidget) w;
224     XrmValue from, to;
225     Cursor cursor;
226    
227     if (simple->simple.cursor_name == NULL)
228         return;
229
230     from.addr = (XPointer) simple->simple.cursor_name;
231     from.size = strlen((char *) from.addr) + 1;
232
233     to.size = sizeof(Cursor);
234     to.addr = (XPointer) &cursor;
235
236     if (XtConvertAndStore(w, XtRString, &from, XtRColorCursor, &to)) {
237         if ( cursor !=  None) 
238             simple->simple.cursor = cursor;
239     } 
240     else {
241         XtAppErrorMsg(XtWidgetToApplicationContext(w),
242                       "convertFailed","ConvertCursor","XawError",
243                       "Simple: ConvertCursor failed.",
244                       (String *)NULL, (Cardinal *)NULL);
245     }
246 }
247
248
249 /* ARGSUSED */
250 static Boolean SetValues(current, request, new)
251     Widget current, request, new;
252 {
253     SimpleWidget s_old = (SimpleWidget) current;
254     SimpleWidget s_new = (SimpleWidget) new;
255     Boolean new_cursor = FALSE;
256
257     if ( XtIsSensitive(current) != XtIsSensitive(new) )
258         (*((SimpleWidgetClass)XtClass(new))->
259              simple_class.change_sensitive) ( new );
260
261     if (s_old->simple.cursor != s_new->simple.cursor) {
262         new_cursor = TRUE;
263     }
264         
265 /*
266  * We are not handling the string cursor_name correctly here.
267  */
268
269     if ( (s_old->simple.pointer_fg != s_new->simple.pointer_fg) ||
270         (s_old->simple.pointer_bg != s_new->simple.pointer_bg) ||
271         (s_old->simple.cursor_name != s_new->simple.cursor_name) ) {
272         ConvertCursor(new);
273         new_cursor = TRUE;
274     }
275
276     if (new_cursor && XtIsRealized(new))
277         XDefineCursor(XtDisplay(new), XtWindow(new), s_new->simple.cursor);
278
279     return False;   
280 }
281
282
283 static Boolean ChangeSensitive(w)
284     register Widget w;
285 {
286     if (XtIsRealized(w)) {
287         if (XtIsSensitive(w))
288             if (w->core.border_pixmap != XtUnspecifiedPixmap)
289                 XSetWindowBorderPixmap( XtDisplay(w), XtWindow(w),
290                                         w->core.border_pixmap );
291             else
292                 XSetWindowBorder( XtDisplay(w), XtWindow(w), 
293                                   w->core.border_pixel );
294         else {
295             if (((SimpleWidget)w)->simple.insensitive_border == None)
296                 ((SimpleWidget)w)->simple.insensitive_border =
297                     XmuCreateStippledPixmap(XtScreen(w),
298                                             w->core.border_pixel, 
299                                             w->core.background_pixel,
300                                             w->core.depth);
301             XSetWindowBorderPixmap( XtDisplay(w), XtWindow(w),
302                                     ((SimpleWidget)w)->
303                                         simple.insensitive_border );
304         }
305     }
306     return False;
307 }