Debian Codegraph @

demos/unsupported/dogs/Dog.c

raw
1 /*
2 * Motif
3 *
4 * Copyright (c) 1987-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 * HISTORY
25 */
26 #ifdef REV_INFO
27 #ifndef lint
28 static char rcsid[] = "$TOG: Dog.c /main/7 1997/05/02 10:11:08 dbl $"
29 #endif
30 #endif
31
32 /*****************************************************************************
33 *
34 * Dog.c - Dog widget source file
35 *
36 ******************************************************************************/
37
38 #include <stdio.h>
39 #include <X11/StringDefs.h>
40 #include <Xm/XmP.h>
41 #include <Xm/DrawP.h>
42 #include "DogP.h"
43
44 #ifdef USING_UIL
45 #include <Mrm/MrmAppl.h>
46 #endif /* USING_UIL */
47
48 #include "up.bm"
49 #include "down.bm"
50 #include "bark.bm"
51
52 #ifndef MAX
53 #define MAX(x,y) ((x) > (y) ? (x) : (y))
54 #endif
55
56 #define WagTime(w) XmField(w,offsets,Dog,wag_time, int)
57 #define BarkTime(w) XmField(w,offsets,Dog,bark_time, int)
58 #define BarkCallback(w) XmField(w,offsets,Dog,bark_callback,XtCallbackList)
59 #define UpPixmap(w) XmField(w,offsets,Dog,up_pixmap,Pixmap)
60 #define DownPixmap(w) XmField(w,offsets,Dog,down_pixmap,Pixmap)
61 #define BarkPixmap(w) XmField(w,offsets,Dog,bark_pixmap,Pixmap)
62 #define CurrPixmap(w) XmField(w,offsets,Dog,curr_pixmap,Pixmap)
63 #define CurrPx(w) XmField(w,offsets,Dog,curr_px,int)
64 #define Wagging(w) XmField(w,offsets,Dog,wagging,Boolean)
65 #define Barking(w) XmField(w,offsets,Dog,barking,Boolean)
66 #define DrawGC(w) XmField(w,offsets,Dog,draw_GC,GC)
67 #define PixmapX(w) XmField(w,offsets,Dog,pixmap_x,Position)
68 #define PixmapY(w) XmField(w,offsets,Dog,pixmap_y,Position)
69 #define DrawX(w) XmField(w,offsets,Dog,draw_x,Position)
70 #define DrawY(w) XmField(w,offsets,Dog,draw_y,Position)
71 #define DrawWidth(w) XmField(w,offsets,Dog,draw_width,Dimension)
72 #define DrawHeight(w) XmField(w,offsets,Dog,draw_height,Dimension)
73 #define CurrWidth(w) XmField(w,offsets,Dog,curr_width,Dimension)
74 #define CurrHeight(w) XmField(w,offsets,Dog,curr_height,Dimension)
75 #define HighlightThickness(w) \
76 XmField(w,offsets,XmPrimitive,highlight_thickness,Dimension)
77 #define ShadowThickness(w) \
78 XmField(w,offsets,XmPrimitive,shadow_thickness,Dimension)
79 #define Foreground(w) XmField(w,offsets,XmPrimitive,foreground,Pixel)
80 #define Highlighted(w) XmField(w,offsets,XmPrimitive,highlighted,Boolean)
81 #define TopShadowGC(w) XmField(w,offsets,XmPrimitive,top_shadow_GC,GC)
82 #define BottomShadowGC(w) XmField(w,offsets,XmPrimitive,bottom_shadow_GC,GC)
83 #define BackgroundPixel(w) XmField(w,offsets,Core,background_pixel,Pixel)
84 #define Width(w) XmField(w,offsets,Core,width,Dimension)
85 #define Height(w) XmField(w,offsets,Core,height,Dimension)
86 #define WagId(w) XmField(w,offsets,Dog,wagId,XtIntervalId)
87 #define BarkId(w) XmField(w,offsets,Dog,barkId,XtIntervalId)
88
89 #define SetPixmap(w, px, pixmap, width, height) \
90 CurrPx(w) = px; CurrPixmap(w) = pixmap; \
91 CurrWidth(w) = width; CurrHeight(w) = height
92
93 #define MakePixmap(b,wd,ht) \
94 XCreatePixmapFromBitmapData(XtDisplay(w),RootWindowOfScreen(XtScreen(w)), \
95 (char *)(b), (wd), (ht), Foreground(w), BackgroundPixel(w), \
96 DefaultDepthOfScreen(XtScreen(w)))
97
98 static void ClassInitialize();
99 static void Initialize();
100 static void Redisplay();
101 static void Resize();
102 static void Destroy();
103 static Boolean SetValues();
104 static XtGeometryResult QueryGeometry();
105
106 static void bark_dog();
107 static void end_bark();
108 static void start_wag();
109 static void stop_wag();
110 static void do_wag();
111 static void create_GC();
112 static void create_pixmaps();
113 static void destroy_pixmaps();
114
115 static char defaultTranslations[] =
116 "<Btn1Down>: Bark()\n\
117 ~Shift<Btn2Down>: StartWag()\n\
118 Shift<Btn2Down>: StopWag()\n\
119 <Key>Return: Bark()\n\
120 Ctrl <Key>Return: Bark()\n\
121 <Key>osfActivate: Bark()\n\
122 <Key>space: Bark()\n\
123 Ctrl <Key>space: Bark()\n\
124 <Key>osfSelect: Bark()\n\
125 <Key>W: StartWag()\n\
126 <Key>S: StopWag()\n\
127 <Key>osfHelp: PrimitiveHelp()";
128
129 static XtActionsRec actionsList[] = {
130 { "Bark", (XtActionProc) bark_dog},
131 { "StartWag", (XtActionProc) start_wag},
132 { "StopWag", (XtActionProc) stop_wag}
133 };
134
135 static XmPartResource resources[] = {
136 {DogNwagTime, DogCWagTime, XtRInt, sizeof(int),
137 XmPartOffset(Dog,wag_time), XmRImmediate, (caddr_t)100},
138 {DogNbarkTime, DogCBarkTime, XtRInt, sizeof(int),
139 XmPartOffset(Dog,bark_time), XmRImmediate, (caddr_t)1000},
140 {DogNbarkCallback, XtCCallback, XtRCallback, sizeof(caddr_t),
141 XmPartOffset(Dog,bark_callback), XtRCallback, NULL}
142 };
143
144 DogClassRec dogClassRec = {
145 { /* core_class fields */
146 (WidgetClass) &xmPrimitiveClassRec, /* superclass */
147 "Dog", /* class_name */
148 sizeof(DogPart), /* widget_size */
149 ClassInitialize, /* class_initialize */
150 NULL, /* class_part_initialize*/
151 False, /* class_inited */
152 Initialize, /* initialize */
153 NULL, /* initialize_notify */
154 XtInheritRealize, /* realize */
155 actionsList, /* actions */
156 XtNumber(actionsList), /* num_actions */
157 (XtResourceList)resources, /* resources */
158 XtNumber(resources), /* num_resources */
159 NULLQUARK, /* xrm_class */
160 True, /* compress_motion */
161 True, /* compress_exposure */
162 True, /* compress_enterleave */
163 False, /* visible_interest */
164 Destroy, /* destroy */
165 Resize, /* resize */
166 Redisplay, /* expose */
167 SetValues, /* set_values */
168 NULL, /* set_values_hook */
169 XtInheritSetValuesAlmost, /* set_values_almost */
170 NULL, /* get_values_hook */
171 NULL, /* accept_focus */
172 XtVersionDontCheck, /* version */
173 NULL, /* callback_private */
174 defaultTranslations, /* tm_table */
175 QueryGeometry, /* query_geometry */
176 NULL, /* disp accelerator */
177 NULL /* extension */
178 },
179 { /* primitive_class record */
180 XmInheritWidgetProc, /* border_highlight */
181 XmInheritWidgetProc, /* border_unhighlight */
182 XtInheritTranslations, /* translations */
183 bark_dog, /* arm_and_activate */
184 NULL, /* syn resources */
185 0, /* num syn_resources */
186 NULL, /* extension */
187 },
188 { /* dog_class record */
189 NULL, /* extension */
190 }
191 };
192
193 externaldef(dogwidgetclass) WidgetClass dogWidgetClass =
194 (WidgetClass) &dogClassRec;
195
196 static XmOffsetPtr offsets; /* Part Offset table for XmResolvePartOffsets */
197
198 /**********************************************************************
199 *
200 * DogCreate - Convenience routine, used by Uil/Mrm.
201 *
202 *********************************************************************/
203
204 Widget DogCreate(parent, name, arglist, nargs)
205 Widget parent;
206 char *name;
207 Arg *arglist;
208 int nargs;
209 {
210 return(XtCreateWidget (name, dogWidgetClass, parent, arglist, nargs));
211 }
212
213 #ifdef USING_UIL
214 /**********************************************************************
215 *
216 * DogMrmInitialize - register Dog widget class with Mrm
217 *
218 *********************************************************************/
219
220 int DogMrmInitialize()
221 {
222 return(MrmRegisterClass (MrmwcUnknown, "Dog" , "DogCreate", DogCreate,
223 (WidgetClass)&dogClassRec));
224 }
225 #endif /* USING_UIL */
226
227 /**********************************************************************
228 *
229 * _DogDrawPixmap - draw the current pixmap
230 *
231 *********************************************************************/
232
233 void _DogDrawPixmap(dw)
234 DogWidget dw;
235 {
236 Widget w = (Widget) dw;
237
238 if (XtIsRealized(w)) {
239 XCopyArea(XtDisplay(w),CurrPixmap(w),
240 XtWindow(w),DrawGC(w),
241 PixmapX(w), PixmapY(w),
242 DrawWidth(w), DrawHeight(w),
243 DrawX(w), DrawY(w));
244 }
245 }
246
247 /**********************************************************************
248 *
249 * _DogPosition(w) - position the current pixmap
250 *
251 *********************************************************************/
252
253 void _DogPosition(w)
254 DogWidget w;
255 {
256 Dimension margin = ShadowThickness(w) + HighlightThickness(w);
257
258 if (CurrWidth(w) < MAX(Width(w) - 2 * margin,0)) {
259 PixmapX(w) = 0;
260 DrawX(w) = Width(w)/2 - CurrWidth(w)/2;
261 DrawWidth(w) = CurrWidth(w);
262 }
263 else {
264 PixmapX(w) = (CurrWidth(w) - (Width(w) - 2 * margin))/2;
265 DrawX(w) = margin;
266 DrawWidth(w) = Width(w) - 2 * margin;
267 }
268
269 if (CurrHeight(w) < MAX(Height(w) - 2 * margin,0)) {
270 PixmapY(w)= 0;
271 DrawY(w) = Height(w)/2 - CurrHeight(w)/2;
272 DrawHeight(w) = CurrHeight(w);
273 }
274 else {
275 PixmapY(w) = (CurrHeight(w) - (Height(w) - 2 * margin))/2;
276 DrawY(w) = margin;
277 DrawHeight(w) = Height(w) - 2 * margin;
278 }
279 }
280
281 /**********************************************************************
282 *
283 * Class methods
284 *
285 *********************************************************************/
286
287 static void ClassInitialize()
288 {
289 XmResolvePartOffsets(dogWidgetClass, &offsets);
290 }
291
292
293 static void create_GC(w)
294 DogWidget w;
295 {
296 XGCValues values;
297 XtGCMask valueMask;
298
299 valueMask = GCForeground | GCBackground | GCGraphicsExposures;
300 values.foreground = Foreground(w);
301 values.background = BackgroundPixel(w);
302 values.graphics_exposures = False;
303 DrawGC(w) = XtGetGC((Widget)w,valueMask,&values);
304 }
305
306 static void create_pixmaps(w)
307 DogWidget w;
308 {
309 UpPixmap(w) = MakePixmap(up_bits, up_width, up_height);
310 DownPixmap(w) = MakePixmap(down_bits, down_width, down_height);
311 BarkPixmap(w) = MakePixmap(bark_bits, bark_width, bark_height);
312 }
313
314 static void Initialize(request, new)
315 DogWidget request;
316 DogWidget new;
317 {
318 if (Width(request) == 0)
319 Width(new) = MAX(MAX(up_width, down_width),bark_width) +
320 2*(ShadowThickness(new)+HighlightThickness(new));
321 if (Height(request) == 0)
322 Height(new) = MAX(MAX(up_height, down_height),bark_height) +
323 2*(ShadowThickness(new)+HighlightThickness(new));
324 create_GC(new);
325 create_pixmaps(new);
326 SetPixmap(new, DownPx, DownPixmap(new), down_width, down_height);
327 Wagging(new) = False;
328 Barking(new) = False;
329 WagId(new) = 0;
330 BarkId(new) = 0;
331
332 Resize(new);
333 }
334
335 static void destroy_pixmaps(w)
336 DogWidget w;
337 {
338 XFreePixmap (XtDisplay(w), UpPixmap(w));
339 XFreePixmap (XtDisplay(w), DownPixmap(w));
340 XFreePixmap (XtDisplay(w), BarkPixmap(w));
341 }
342
343
344 static void Destroy(w)
345 DogWidget w;
346 {
347 XtReleaseGC ((Widget)w, DrawGC(w));
348 destroy_pixmaps(w);
349
350 if (WagId(w) != 0)
351 XtRemoveTimeOut( WagId(w));
352 if (BarkId(w) != 0)
353 XtRemoveTimeOut( BarkId(w));
354
355 XtRemoveAllCallbacks ((Widget)w, DogNbarkCallback);
356 }
357
358 static void Resize(w)
359 DogWidget w;
360 {
361 _DogPosition(w);
362 }
363
364 static Boolean DifferentBackground(w, p)
365 Widget w;
366 Widget p;
367 {
368 if (XmIsPrimitive(w) && XmIsManager(p))
369 {
370 Pixel w_bg, p_bg;
371 Pixmap w_px, p_px;
372
373 XtVaGetValues(w, XmNbackground, &w_bg, XmNbackgroundPixmap, &w_px, NULL);
374 XtVaGetValues(p, XmNbackground, &p_bg, XmNbackgroundPixmap, &p_px, NULL);
375
376 return ((w_bg == p_bg) && (w_px == p_px));
377 }
378
379 return (False);
380 }
381
382 static void Redisplay(w, event, region)
383 Widget w;
384 XEvent *event;
385 Region region;
386 {
387 if (XtIsRealized(w)) {
388 XmeDrawShadows(XtDisplay (w), XtWindow (w),
389 TopShadowGC(w), BottomShadowGC(w),
390 HighlightThickness(w), HighlightThickness(w),
391 Width(w) - 2 * HighlightThickness(w),
392 Height(w) - 2 * HighlightThickness(w),
393 ShadowThickness(w),
394 XmSHADOW_OUT);
395
396 if (Highlighted(w))
397 (*((DogWidgetClass)XtClass(w)) ->
398 primitive_class.border_highlight)((Widget)w);
399 else if (DifferentBackground ((Widget)w, XtParent (w)))
400 (*((DogWidgetClass)XtClass(w)) ->
401 primitive_class.border_unhighlight)((Widget)w);
402
403 _DogDrawPixmap((DogWidget)w);
404 }
405 }
406
407 static Boolean SetValues(current, request, new)
408 DogWidget current;
409 DogWidget request;
410 DogWidget new;
411
412 {
413 Boolean redraw = False;
414
415 if (ShadowThickness(new) != ShadowThickness(current) ||
416 HighlightThickness(new) != HighlightThickness(current)) {
417 _DogPosition(new);
418 redraw = True;
419 }
420 if (Foreground(new) != Foreground(current) ||
421 BackgroundPixel(new) != BackgroundPixel(current)) {
422 XtReleaseGC ((Widget)current, DrawGC(current));
423 create_GC(new);
424 destroy_pixmaps(new);
425 create_pixmaps(new);
426 switch (CurrPx(new)) {
427 case(UpPx) :
428 SetPixmap(new,UpPx,UpPixmap(new),up_width,up_height);
429 break;
430 case(DownPx) :
431 SetPixmap(new,DownPx,DownPixmap(new),down_width,down_height);
432 break;
433 case(BarkPx) :
434 SetPixmap(new,BarkPx,BarkPixmap(new),bark_width,bark_height);
435 break;
436 }
437 redraw = True;
438 }
439 return (redraw);
440 }
441
442 static XtGeometryResult QueryGeometry (w, intended, reply)
443 DogWidget w;
444 XtWidgetGeometry *intended;
445 XtWidgetGeometry *reply;
446 {
447 reply->request_mode = 0;
448
449 if ((intended->request_mode & (~(CWWidth | CWHeight))) != 0)
450 return (XtGeometryNo);
451
452 reply->request_mode = (CWWidth | CWHeight);
453 reply->width = MAX(MAX(down_width,up_width),bark_width) +
454 2*(ShadowThickness(w)+HighlightThickness(w));
455 reply->height = MAX(MAX(down_height,up_height),bark_height) +
456 2*(ShadowThickness(w)+HighlightThickness(w));
457
458 if (reply->width != intended->width ||
459 reply->height != intended->height ||
460 intended->request_mode != reply->request_mode)
461 return (XtGeometryAlmost);
462 else {
463 reply->request_mode = 0;
464 return (XtGeometryYes);
465 }
466 }
467 /**********************************************************************
468 *
469 * Widget actions
470 *
471 *********************************************************************/
472
473 static void bark_dog(w, event)
474 DogWidget w;
475 XEvent *event;
476 {
477 XmProcessTraversal((Widget)w, XmTRAVERSE_CURRENT);
478 XtCallCallbacks ((Widget)w, DogNbarkCallback, NULL);
479 if (Barking(w) == True) return;
480 Barking(w) = True;
481 SetPixmap(w,BarkPx,BarkPixmap(w),bark_width,bark_height);
482 _DogPosition(w);
483 _DogDrawPixmap(w);
484 BarkId(w) = XtAppAddTimeOut (XtWidgetToApplicationContext((Widget)w),
485 BarkTime(w), end_bark, w);
486 }
487
488 static void end_bark(w, t)
489 DogWidget w;
490 XtIntervalId *t;
491 {
492 SetPixmap(w,DownPx,DownPixmap(w),down_width,down_height);
493 _DogPosition(w);
494 _DogDrawPixmap(w);
495 Barking(w) = False;
496 BarkId(w) = 0;
497 if (Wagging(w) == True)
498 WagId(w) = XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)w),
499 WagTime(w), do_wag, w);
500
501 }
502
503 static void start_wag(w, event)
504 DogWidget w;
505 XEvent *event;
506 {
507 XmProcessTraversal((Widget)w, XmTRAVERSE_CURRENT);
508 if (Wagging(w) == True) return;
509 Wagging(w) = True;
510 if (Barking(w) == True) return;
511 WagId(w) = XtAppAddTimeOut (XtWidgetToApplicationContext((Widget)w),
512 WagTime(w), do_wag, w);
513 }
514
515 static void stop_wag(w, event)
516 DogWidget w;
517 XEvent *event;
518 {
519 XmProcessTraversal((Widget)w, XmTRAVERSE_CURRENT);
520 Wagging(w) = False;
521 if (WagId(w) != 0)
522 XtRemoveTimeOut( WagId(w));
523 WagId(w) = 0;
524
525 }
526
527 static void do_wag(w, t)
528 XtPointer w;
529 XtIntervalId *t;
530 {
531 DogWidget dw = (DogWidget)w;
532
533 if (Barking(dw) == True) return;
534 if (Wagging(dw) == False) return;
535 switch(CurrPx(dw)) {
536 case(UpPx):
537 SetPixmap(dw,DownPx,DownPixmap(dw),down_width,down_height);
538 break;
539 case(DownPx):
540 SetPixmap(dw,UpPx,UpPixmap(dw),up_width,up_height);
541 break;
542 }
543 _DogPosition(dw);
544 _DogDrawPixmap(dw);
545 XtAppAddTimeOut (XtWidgetToApplicationContext((Widget)dw),
546 WagTime(dw), do_wag, dw);
547 }
548