Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / examples / motif / dogs / Square.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  * (c) Copyright 1989, 1990, 1991, 1992 OPEN SOFTWARE FOUNDATION, INC. 
25  * ALL RIGHTS RESERVED 
26 */ 
27 /* 
28  * Motif Release 1.2
29 */ 
30 #ifdef REV_INFO
31 #ifndef lint
32 static char rcsid[] = "$XConsortium: Square.c /main/3 1995/10/27 10:42:28 rswiston $"
33 #endif
34 #endif
35
36 /*****************************************************************************
37 *
38 *  Square.c - widget source file
39 *
40 ******************************************************************************/
41         
42 #include <stdio.h>
43 #include <Xm/Xm.h>
44 #include <Mrm/MrmPublic.h>
45 #include <Xm/BulletinBP.h>
46 #include "SquareP.h"
47
48 #define Width(w) XmField((w),square_offsets,Core,width,Dimension)
49 #define Height(w) XmField((w),square_offsets,Core,height,Dimension)
50 #define BorderWidth(w) XmField((w),square_offsets,Core,border_width,Dimension)
51 #define MajorDimension(w) XmField((w),square_offsets,Square,major_dimension,int)
52
53 #define MakeSquare(w) XmConstraintField((w),square_constraint_offsets, \
54                         Square,make_square, Boolean)
55
56 static void ClassInitialize();
57 static void Initialize();
58 static Boolean SetValues();
59 static void ConstraintInitialize();
60 static Boolean ConstraintSetValues();
61
62 static XmPartResource resources[] = {
63     {
64     SquareNmajorDimension, SquareCMajorDimension, XmRInt, sizeof(int),
65     XmPartOffset(Square,major_dimension), XmRImmediate, (XtPointer)SquareWIDTH
66     }
67 };
68
69 static XmPartResource constraints[] = {
70    {
71     SquareNmakeSquare, SquareCMakeSquare, XmRBoolean, sizeof(Boolean),
72     XmConstraintPartOffset(Square,make_square),
73     XmRImmediate, (XtPointer)False
74    }
75 };
76
77
78 externaldef(squareclassrec) SquareClassRec squareClassRec =
79 {
80    {                    /* core_class fields    */
81       (WidgetClass) &xmBulletinBoardClassRec,   /* superclass   */
82       "Square",                 /* class_name           */
83       sizeof(SquarePart),       /* widget_size          */
84       ClassInitialize,          /* class_initialize     */
85       NULL,                     /* class init part proc */
86       False,                    /* class_inited         */
87       Initialize,               /* initialize           */
88       NULL,                     /* initialize_notify    */
89       XtInheritRealize,         /* realize              */
90       NULL,                     /* actions              */
91       0,                        /* num_actions          */
92       (XtResourceList)resources,/* resources            */
93       XtNumber(resources),      /* num_resources        */
94       NULLQUARK,                /* xrm_class            */
95       False,                    /* compress_motion      */
96       XtExposeCompressMaximal,  /* compress_exposure    */
97       False,                    /* compress_enterleave  */
98       False,                    /* visible_interest     */
99       NULL,                     /* destroy              */
100       XtInheritResize,          /* resize               */
101       XtInheritExpose,          /* expose               */
102       SetValues,                /* set_values           */
103       NULL,                     /* set_values_hook      */
104       XtInheritSetValuesAlmost, /* set_values_almost    */
105       NULL,                     /* get_values_hook      */
106       NULL,                     /* accept_focus         */
107       XtVersionDontCheck,       /* version              */
108       NULL,                     /* callback_private     */
109       XtInheritTranslations,    /* tm_table             */
110       XtInheritQueryGeometry,   /* Query Geometry proc  */
111       NULL,                     /* disp accelerator     */
112       NULL,                     /* extension            */    
113    },
114
115    {                    /* composite_class fields */
116       XtInheritGeometryManager, /* geometry_manager       */
117       XtInheritChangeManaged,   /* change_managed         */
118       XtInheritInsertChild,     /* insert_child           */
119       XtInheritDeleteChild,     /* delete_child           */
120       NULL,                     /* extension              */
121    },
122
123    {                    /* constraint_class fields */
124       (XtResourceList)constraints,  /* constraint resource     */
125       XtNumber(constraints),        /* number of constraints   */
126       sizeof(SquareConstraintPart), /* size of constraint      */
127       ConstraintInitialize,         /* initialization          */
128       NULL,                         /* destroy proc            */
129       ConstraintSetValues,          /* set_values proc         */
130       NULL,                         /* extension               */
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    {                    /* bulletin_board_class fields */
144       FALSE,                        /* always_install_accelerators */
145       NULL,                         /* geo_matrix_create  */
146       XtInheritFocusMovedProc,      /* focus_moved_proc   */
147       NULL,                         /* extension          */
148    },
149
150    {                    /* square_class fields  */
151       NULL,                         /* extension          */
152    }
153 };
154
155 externaldef(squarewidgetclass) WidgetClass 
156         squareWidgetClass = (WidgetClass) &squareClassRec;
157
158 static XmOffsetPtr square_offsets;              /* Part offsets table */
159 static XmOffsetPtr square_constraint_offsets;   /* Constraint offsets table */
160
161 /**********************************************************************
162  *
163  * SquareCreate - Convenience routine, used by Uil/Mrm.
164  *
165  *********************************************************************/
166
167 Widget SquareCreate(parent, name, arglist, nargs)
168     Widget parent;
169     char *name;
170     Arg *arglist;
171     int nargs;
172 {
173     return(XtCreateWidget (name, squareWidgetClass, parent, arglist, nargs));
174 }
175
176 /**********************************************************************
177  *
178  * SquareMrmInitialize - register Square widget class with Mrm
179  *
180  *********************************************************************/
181
182 int SquareMrmInitialize()
183 {
184     return(MrmRegisterClass (MrmwcUnknown, "Square", "SquareCreate",
185                              SquareCreate, (WidgetClass)&squareClassRec));
186 }
187
188 /**********************************************************************
189  *
190  * Class methods
191  *
192  *********************************************************************/
193
194 static void ClassInitialize()
195 {
196     XmResolveAllPartOffsets(squareWidgetClass, &square_offsets,
197                                 &square_constraint_offsets);
198 }
199
200 static void Initialize(req, new)
201     SquareWidget req;
202     SquareWidget new;
203 {
204     if (MajorDimension(new) != SquareWIDTH &&
205         MajorDimension(new) != SquareHEIGHT) {
206         XtWarning("Square: invalid majorDimension");
207         MajorDimension(new) = SquareWIDTH;
208     }
209 }
210
211 static Boolean SetValues(curr, req, new)
212     SquareWidget curr;
213     SquareWidget req;
214     SquareWidget new;
215 {
216     if (MajorDimension(new) != SquareWIDTH &&
217         MajorDimension(new) != SquareHEIGHT) {
218         XtWarning("Square: invalid majorDimension");
219         MajorDimension(new) = MajorDimension(curr);
220     }
221     return (False);
222 }
223
224 static void ConstraintInitialize (req, new)
225     Widget req;
226     Widget new;
227 {
228     Dimension m;
229
230     if(MakeSquare(new) == True) {
231         if (MajorDimension(XtParent(new))==SquareWIDTH)
232             m = Width(new);
233         else
234             m = Height(new);
235
236         XtResizeWidget(new, m, m, BorderWidth(new));
237     }
238 }
239
240 static Boolean ConstraintSetValues (old, ref, new)
241     Widget old;
242     Widget ref;
243     Widget new;
244 {
245     Boolean redraw = False;
246
247     if (MakeSquare(new) != MakeSquare(old)) {
248         if(MakeSquare(new)==True) {
249             if (MajorDimension(XtParent(new))==SquareWIDTH)
250                 Height(new) = Width(new);
251             else
252                 Width(new) = Height(new);
253         }
254         else {
255             XtWidgetGeometry gi;
256             XtWidgetGeometry gp;
257
258             if (MajorDimension(XtParent(new))==SquareWIDTH)
259                 Height(new) = Height(new)/2;
260             else
261                 Width(new) = Width(new)/2;
262
263             gi.request_mode = CWWidth | CWHeight;
264             gi.width = Width(new);
265             gi.height = Height(new);
266             if (XtQueryGeometry(new, &gi, &gp) == XtGeometryAlmost) {
267                 if (gp.request_mode && CWWidth != 0) Width(new) = gp.width;
268                 if (gp.request_mode && CWHeight != 0) Height(new) = gp.height;
269             }
270         }
271         redraw = True;
272     }
273     return (redraw);
274 }
275