File: Xm.c

package info (click to toggle)
motif 2.3.4-13
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 81,160 kB
  • ctags: 51,769
  • sloc: ansic: 596,938; cpp: 3,951; yacc: 2,854; makefile: 2,070; csh: 1,199; sh: 1,070; lex: 455
file content (706 lines) | stat: -rw-r--r-- 18,200 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/* $XConsortium: Xm.c /main/6 1995/10/25 20:28:03 cde-sun $ */
/*
 * Motif
 *
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
 *
 * These libraries and programs are free software; you can
 * redistribute them and/or modify them under the terms of the GNU
 * Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * These libraries and programs are distributed in the hope that
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with these librararies and programs; if not, write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301 USA
 */
/*
 * HISTORY
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#include "XmI.h"
#include "MessagesI.h"
#include <Xm/PrimitiveP.h>
#include <Xm/ManagerP.h>
#include <Xm/GadgetP.h>
#include <Xm/IconGP.h>
#include <Xm/LabelGP.h>
#ifdef FIX_345
#include <X11/keysym.h>
#endif
#ifdef FIX_1565
#include <Xm/GrabShell.h>
#include <Xm/MenuShell.h>
#endif


/**************************************************************************
 *   This is Xm.c
 *    It contains global API that:
 *      - it's not widget specific 
 *      - it's already used by various widgets (you don't get useless
 *          code by linking with this module: all the functions
 *          are useful and used. 
 *   For example, TrackingLocate or ResolvePartOffset do not belong
 *   here because they are not used by everybody.
 *************************************************************************/

#ifdef FIX_345
Boolean _init_modifiers = TRUE;
unsigned int NumLockMask = 0;
unsigned int ScrollLockMask = 0;


/*************************************<->*************************************
 *
 *  _XmInitModifiers (void)
 *
 *   Description:
 *   -----------
 *     Sets the appropriate mask for NumLock and ScrollLock
 *
 *
 *   Inputs:
 *   ------
 *     None
 * 
 *   Outputs:
 *   -------
 *     None
 *
 *   Procedures Called
 *   -----------------
 *     None
 *
 *************************************<->***********************************/
void
_XmInitModifiers (void)
{
    XModifierKeymap *modmap;
    Display *dpy;
    KeySym *keymap;
    unsigned int keycode;
    int min_keycode;
    int max_keycode;
    int keysyms_per_keycode;
    int i;

    dpy = _XmGetDefaultDisplay();
    NumLockMask = 0;
    ScrollLockMask = 0;
    keysyms_per_keycode = 0;
    min_keycode = 0;
    max_keycode = 0;

    XDisplayKeycodes (dpy, &min_keycode, &max_keycode);
    modmap = XGetModifierMapping (dpy);
    keymap = XGetKeyboardMapping (dpy, min_keycode, max_keycode - min_keycode + 1, &keysyms_per_keycode);

    if (modmap && keymap) {
	for (i = 3 * modmap->max_keypermod; i < 8 * modmap->max_keypermod; i++) {
	    keycode = modmap->modifiermap[i];
	    if ((keycode >= min_keycode) && (keycode <= max_keycode)) {
		int j;
		KeySym *syms = keymap + (keycode - min_keycode) * keysyms_per_keycode;

		for (j = 0; j < keysyms_per_keycode; j++)
		    if (!NumLockMask && (syms[j] == XK_Num_Lock))
			NumLockMask = (1 << (i / modmap->max_keypermod));
		    else if (!ScrollLockMask && (syms[j] == XK_Scroll_Lock))
			ScrollLockMask = (1 << (i / modmap->max_keypermod));
	    }
	}
    }

    /* Cleanup memory */
    if (modmap)
	XFreeModifiermap (modmap);

    if (keymap)
	XFree (keymap);
}
#endif


/**************************************************************************
 *                                                                        *
 * _XmSocorro - Help dispatch function.  Start at the widget help was     *
 *   invoked on, find the first non-null help callback list, and call it. *
 *   -- Called by various widgets across Xm                               *
 *                                                                        *
 *************************************************************************/
/* ARGSUSED */
void 
_XmSocorro(
        Widget w,
        XEvent *event,
        String *params,		/* unused */
        Cardinal *num_params )	/* unused */
{
    XmAnyCallbackStruct cb;    

    if (w == NULL) return;

    cb.reason = XmCR_HELP;
    cb.event = event;

    do {
        if ((XtHasCallbacks(w, XmNhelpCallback) == XtCallbackHasSome))
        {
            XtCallCallbacks (w, XmNhelpCallback, &cb);
            return;
        }
        else
            w = XtParent(w);
    }    
    while (w != NULL);
}


/****************************************************************
 *
 * _XmParentProcess
 *    This is the entry point for parent processing.
 *   -- Called by various widgets across Xm                              
 *
 ****************************************************************/
Boolean 
_XmParentProcess(
        Widget widget,
        XmParentProcessData data )
{
    XmManagerWidgetClass manClass ;
	    
    manClass = (XmManagerWidgetClass) widget->core.widget_class ;
    
    if(    XmIsManager( widget)
       && manClass->manager_class.parent_process    ) {   
	return( (*manClass->manager_class.parent_process)( widget, data)) ;
    } 
	    
    return( FALSE) ;
}



/************************************************************************
 *
 *  _XmDestroyParentCallback
 *     Destroy parent. Used by various dialog subclasses
 *
 ************************************************************************/
/* ARGSUSED */
void 
_XmDestroyParentCallback(
        Widget w,
        XtPointer client_data,	/* unused */
        XtPointer call_data )	/* unused */
{
   XtDestroyWidget (XtParent (w));
}



/************************************************************************
 *
 *  _XmClearShadowType
 *	Clear the right and bottom border area and save 
 *	the old width, height and shadow type.
 *      Used by various subclasses for resize larger situation, where the
 *      inside shadow is not exposed.
 *   Maybe that should be moved in Draw.c, maybe not, since it's a widget API
 *
 ************************************************************************/
void 
_XmClearShadowType(
        Widget w,
#if NeedWidePrototypes
        int old_width,
        int old_height,
        int old_shadow_thickness,
        int old_highlight_thickness )
#else
        Dimension old_width,
        Dimension old_height,
        Dimension old_shadow_thickness,
        Dimension old_highlight_thickness )
#endif /* NeedWidePrototypes */
{

    if (old_shadow_thickness == 0) return;

    if (XtIsRealized(w)) {
      if (old_width <= w->core.width)
	  XClearArea (XtDisplay (w), XtWindow (w),
		      old_width - old_shadow_thickness - 
		      old_highlight_thickness, 0,
		      old_shadow_thickness, old_height - 
		      old_highlight_thickness, 
		      False);

      if (old_height <= w->core.height)
	  XClearArea (XtDisplay (w), XtWindow (w),
		      0, old_height - old_shadow_thickness - 
		      old_highlight_thickness, 
		      old_width - old_highlight_thickness, 
		      old_shadow_thickness, 
		      False);
   }
}



/**********************************************************************
 *
 * _XmReOrderResourceList
 *   This procedure moves the given resource right after the
 *   insert_after name in this class resource list.  
 *   (+ insert_after NULL means insert in front)
 *
 *   ----Replace by a call to an Xt function in R6.-----
 **********************************************************************/
void 
_XmReOrderResourceList(
	WidgetClass widget_class,
        String res_name,
        String insert_after)
{
    XrmResource **list;
    int len;
    XrmQuark res_nameQ = XrmPermStringToQuark(res_name);
    XrmResource *tmp ;
    int n ;

    _XmProcessLock();
    list = (XrmResource **) widget_class->core_class.resources ;
    len = widget_class->core_class.num_resources;

    /* look for the named resource slot */
    n = 0; 
    while ((n < len) && (list[n]->xrm_name != res_nameQ))
      n++;

    if (n < len) {
	int m, i;
	XrmQuark insert_afterQ ;  
		
	if (insert_after) {
	    insert_afterQ = XrmPermStringToQuark(insert_after) ;
	    /* now look for the insert_after resource slot */
	    m = 0; 
	    while ((m < len) && (list[m]->xrm_name != insert_afterQ))
	      m++;
	} else m = len ;
	if (m == len) m = -1 ;

	/* now do the insertion/packing, both cases */
	tmp = list[n] ;

	if (n > m) {
	    for (i = n; i > m+1; i--)
		list[i] = list[i-1];
	    list[m+1] = tmp;
	} else {
	    for (i = n; i < m; i++)
		list[i] = list[i+1];
	    list[m] = tmp;
	}
    }
    _XmProcessUnlock();
}



/************************************************************************
 *
 *  _XmWarningMsg
 *	Add XME_WARNING to Message list so MotifWarningHandler will
 *      add Name: & Class:
 *
 ************************************************************************/
void 
_XmWarningMsg(Widget w,
	      char *type,
	      char *message,
	      char **params,
	      Cardinal num_params)
{
  Display * dpy;
  char *new_params[11];
  Cardinal num_new_params = num_params + 1;
  int i;

  if (num_new_params > 11) num_new_params = 11;
  for (i = 0; i < num_new_params-1; i++)
    new_params[i] = params[i];
  new_params[num_new_params-1] = XME_WARNING;

  if (w != NULL) {
    XtAppWarningMsg (XtWidgetToApplicationContext(w),
		     XrmQuarkToString (w->core.xrm_name),
		     type,
		     w->core.widget_class->core_class.class_name, 
		     message, new_params, &num_new_params);
  } else
    XtWarning(message);
}

/* ARGSUSED */
Boolean
_XmIsISO10646(Display *dpy, XFontStruct *font)
{
    Boolean ok;
    int i;
    char *regname;
    Atom registry;
    XFontProp *xfp;

    ok = False;
    registry = XInternAtom(dpy, "CHARSET_REGISTRY", False);

    for (i = 0, xfp = font->properties;
	 ok == False && i < font->n_properties; xfp++, i++) {
	if (xfp->name == registry) {
	    regname = XGetAtomName(dpy, (Atom) xfp->card32);
	    if (strcmp(regname, "ISO10646") == 0 ||
		strcmp(regname, "iso10646") == 0)
	      ok = True;
	    XFree(regname);
	}
    }
    return ok;
}

XChar2b*
_XmUtf8ToUcs2(char *draw_text, size_t seg_len, size_t *ret_str_len)
{
    char *ep;
    unsigned short codepoint;
    XChar2b *ptr;
    XChar2b *buf2b;

    /*
     * Convert to UCS2 string on the fly.
     */

     buf2b = (XChar2b *)XtMalloc(seg_len * sizeof(XChar2b));

     ep = draw_text + seg_len;
     for (ptr = buf2b; draw_text < ep; ptr++) {
	if((draw_text[0]&0x80)==0) {
	    codepoint=draw_text[0];
	    draw_text++;
	} else if((draw_text[0]&0x20)==0) {
	    codepoint = (draw_text[0]&0x1F)<<6 | (draw_text[1]&0x3F);
	    draw_text+=2;
        } else if((draw_text[0]&0x10)==0) {
	    codepoint = (draw_text[0]&0x0F)<<12
			    | (draw_text[1]&0x3F)<<6
			    | (draw_text[2]&0x3F);
	    draw_text+=3;
	} else {                    /* wrong UTF-8 */
	    codepoint=(unsigned)'?';
	    draw_text++;
	}
	ptr->byte1 = (codepoint >> 8) & 0xff;;
	ptr->byte2 = codepoint & 0xff;
     }
     *ret_str_len = ptr - buf2b;
     return buf2b;
}


/***************************************/
/********---- PUBLIC API ----**********/
/*************************************/




/************************************************************************
 *
 *  XmeWarning
 *	Build up a warning message and call Xt to get it displayed.
 *
 ************************************************************************/
void 
XmeWarning(Widget w,
	   char *message )
{
  char *params[1];
  Cardinal num_params = 0;
  Display * dpy;
  
  if (w != NULL) {
    /* the MotifWarningHandler installed in VendorS.c knows about
       this convention */
    params[0] = XME_WARNING;
    num_params++;

    XtAppWarningMsg (XtWidgetToApplicationContext(w),
		     XrmQuarkToString (w->core.xrm_name),
		     "XmeWarning",
		     w->core.widget_class->core_class.class_name, 
		     message, params, &num_params);
  } else 
    	XtWarning(message);
}


/************************************************************************
 *
 *  XmObjectAtPoint
 *	new implementation that ask a manager class method
 *   -- Called by various widgets across Xm                            
 *
 ************************************************************************/
Widget 
XmObjectAtPoint(
        Widget wid,
        Position x,
        Position y )
{
    XmManagerWidgetClass mw = (XmManagerWidgetClass) XtClass(wid);
    XmManagerClassExt *mext ;
    Widget return_wid = NULL;
    _XmWidgetToAppContext(wid);
     
    _XmAppLock(app);

    if (!XmIsManager(wid)) {
	_XmAppUnlock(app);
	return NULL;
    }

    mext = (XmManagerClassExt *)
	_XmGetClassExtensionPtr( (XmGenericClassExt *)
				&(mw->manager_class.extension), NULLQUARK) ;
    if (!*mext) {
	_XmAppUnlock(app);
	return NULL;
    }
    
    if ((*mext)->object_at_point)
	return_wid = ((*mext)->object_at_point)(wid, x, y);
	
    _XmAppUnlock(app);
    return return_wid;
}

#ifdef FIX_1381
/************************************************************************
 *
 *  _XmAssignInsensitiveColor
 *  Allocate the Gray color for display widget like insensitive.
 *
 *
 ************************************************************************/

Pixel
_XmAssignInsensitiveColor(Widget w)
{
	Pixel p;

	if (XmIsPrimitive(w)) {
		XmPrimitiveWidget pw = (XmPrimitiveWidget) w;
		p = pw->primitive.bottom_shadow_color;
	}
	else if (XmIsGadget(w)) {
		if (XmIsLabelGadget(w)) {
			XmLabelGadget lg = (XmLabelGadget) w;
			p = LabG_BottomShadowColor(lg);
		}
		if (XmIsIconGadget(w)) {
			XmIconGadget  ig = (XmIconGadget) w;
			p = IG_BottomShadowColor(ig);
		}
	}
	else {
		p = 0;
	}

	return p;
}
#endif

#ifdef FIX_1565

typedef struct _GrabData GrabData;
struct _GrabData {
  Widget w;
  GrabData *next;
};

static void _XmSendFocusEvent(Widget child, int type);
static void _XmStartDispatcher(Display *display);
static Boolean _XmEventDispatcher(XEvent *event);
static void UnmapHandler(Widget w, XtPointer client_data, XEvent *event, Boolean *cont);
static Boolean _UngrabKeyboard(Widget w);

static GrabData *grabw_top = NULL;
static int xm_dispatcher_on = 0;
static XtEventDispatchProc saved_dispatcher_proc = NULL;
static XtEventDispatchProc xt_dispatcher_proc = NULL;

/*
   XmForceGrabKeyboard function is defined to be a substitutor of XSetInputFocus calls
   for popup and pulldown menus that should grab keyboard focus yet main window at the
   same time should visually stay in focus for window manager. This resolves focus flip
   issue when popup or pulldown menu is raised. ~ochern
 */
void XmForceGrabKeyboard(Widget w, Time time)
{
  GrabData *grabw;

  if (!w)
    return;

  while (! XtIsSubclass(w, shellWidgetClass))
    w = XtParent(w);

  if (! (XtIsSubclass(w, xmGrabShellWidgetClass) || XtIsSubclass(w, xmMenuShellWidgetClass)))
    return;

  _XmStartDispatcher(XtDisplay(w));

  _UngrabKeyboard(w);

  grabw = (GrabData *) XtMalloc(sizeof(GrabData));
  grabw->w = w;
  _XmProcessLock();
  grabw->next = grabw_top;
  grabw_top = grabw;
  _XmProcessUnlock();

  XtInsertEventHandler(w, StructureNotifyMask, False, UnmapHandler, NULL, XtListHead);

  _XmSendFocusEvent(w, FocusIn);

  /* Following the XSetInputFocus behaviour we force sending FocusOut (see XGrabKeyboard(3))
     event to a previous keyboard holder */
  XtGrabKeyboard(w, True, GrabModeAsync, GrabModeAsync, time);
}

static void _XmStartDispatcher(Display *display)
{
  if (!display)
    return;

  _XmProcessLock();

  if (xm_dispatcher_on) {
    _XmProcessUnlock();
    return;
  }

  saved_dispatcher_proc = XtSetEventDispatcher(display, KeyPress, _XmEventDispatcher);
  if (! xt_dispatcher_proc)
    xt_dispatcher_proc = saved_dispatcher_proc;
  XtSetEventDispatcher(display, KeyRelease, _XmEventDispatcher);
  xm_dispatcher_on = 1;

  _XmProcessUnlock();
}

static Boolean _XmEventDispatcher(XEvent *event)
{
  _XmProcessLock();
  if (grabw_top) {
    if (event->type == KeyPress || event->type == KeyRelease)
      event->xany.window = XtWindow(grabw_top->w);
  }
  _XmProcessUnlock();

  if (saved_dispatcher_proc) {
    return (*saved_dispatcher_proc)(event);
  } else if (xt_dispatcher_proc) {
    return (*xt_dispatcher_proc)(event);
  } else {
    if (grabw_top)
      XtSetEventDispatcher(XtDisplay(grabw_top->w), event->type, NULL);
    return XtDispatchEvent(event);
  }
}

static void UnmapHandler(Widget w, XtPointer client_data, XEvent *event, Boolean *cont)
{
  if (event->type == UnmapNotify)
    _UngrabKeyboard(w);
  if (! grabw_top) {
    XtSetEventDispatcher(XtDisplay(w), KeyPress, saved_dispatcher_proc);
    XtSetEventDispatcher(XtDisplay(w), KeyRelease, saved_dispatcher_proc);
    xm_dispatcher_on = 0;
  }

  /* we do not call XtUngrabKeyboard since X server automatically performs an
     UngrabKeyboard request if the event window for an active keyboard grab becomes
     not viewable. ~ochern */
}

static Boolean _UngrabKeyboard(Widget w)
{
  GrabData *grabw, *grabw_prev;

  _XmProcessLock();
  if (! grabw_top) {
    _XmProcessUnlock();
    return False;
  }

  grabw = grabw_top;
  grabw_prev = NULL;
  while(grabw && grabw->w != w) {
    grabw_prev = grabw;
    grabw = grabw->next;
  }
  if (grabw) {
    if (grabw_prev)
      grabw_prev->next = grabw->next;
    else
      grabw_top = grabw->next;
    XtFree((char*) grabw);

    _XmProcessUnlock();
    return True;
  }

  _XmProcessUnlock();
  return False;
}

static void _XmSendFocusEvent(Widget child, int type)
{
  child = XtIsWidget(child) ? child : _XtWindowedAncestor(child);
  if (XtIsSensitive(child) && !child->core.being_destroyed
      && XtIsRealized(child) && (XtBuildEventMask(child) & FocusChangeMask))
  {
    XFocusChangeEvent event;
    Display* dpy = XtDisplay (child);

    event.type = type;
    event.serial = LastKnownRequestProcessed(dpy);
    event.send_event = True;
    event.display = dpy;
    event.window = XtWindow(child);
    event.mode = NotifyNormal;
    event.detail = NotifyAncestor;
    if (XFilterEvent((XEvent*)&event, XtWindow(child)))
      return;
    XtDispatchEventToWidget(child, (XEvent*)&event);
  }
}

#endif