summaryrefslogtreecommitdiff
path: root/iup/src/mot/iupmot_common.c
blob: 286e075922f7a67d1ff119f365fa0f0dc5b4bab9 (plain)
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
/** \file
 * \brief Motif Base Functions
 *
 * See Copyright Notice in "iup.h"
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <Xm/Xm.h>
#include <Xm/ScrollBar.h>
#include <X11/cursorfont.h>

#include "iup.h"
#include "iupkey.h"
#include "iupcbs.h"

#include "iup_object.h"
#include "iup_childtree.h"
#include "iup_key.h"
#include "iup_str.h"
#include "iup_class.h"
#include "iup_attrib.h"
#include "iup_focus.h"
#include "iup_key.h"
#include "iup_drv.h"
#include "iup_image.h"
#include "iup_drv.h"

#include "iupmot_color.h"
#include "iupmot_drv.h"



void iupdrvActivate(Ihandle* ih)
{
  if (iupStrEqual(ih->iclass->name, "text") || iupStrEqual(ih->iclass->name, "multiline"))
    XmProcessTraversal(ih->handle, XmTRAVERSE_CURRENT);
  else
    XtCallActionProc(ih->handle, "ArmAndActivate", 0, 0, 0 );
}

static int motActivateMnemonic(Ihandle *dialog, int c)
{
  Ihandle *ih;
  char attrib[19] = "_IUPMOT_MNEMONIC_ ";
  attrib[17] = (char)c;
  ih = (Ihandle*)iupAttribGet(dialog, attrib);
  if (iupObjectCheck(ih))
  {
    iupdrvActivate(ih);
    return IUP_IGNORE;
  }
  return IUP_CONTINUE;
}

void iupmotSetMnemonicTitle(Ihandle *ih, const char* value)
{
  char c;
  char* str;

  if (!value) 
    value = "";

  str = iupStrProcessMnemonic(value, &c, -1);  /* remove & and return in c */
  if (str != value)
  {
    KeySym keysym = iupmotKeyCharToKeySym(c);
    XtVaSetValues(ih->handle, XmNmnemonic, keysym, NULL);   /* works only for menus, but underlines the letter */

    if (ih->iclass->nativetype != IUP_TYPEMENU)
    {
      Ihandle* dialog = IupGetDialog(ih);
      char attrib[22] = "_IUPMOT_MNEMONIC_ \0CB";
      attrib[17] = (char)toupper(c);

      /* used by motActivateMnemonic */
      if (iupStrEqual(ih->iclass->name, "label"))
        iupAttribSetStr(dialog, attrib, (char*)iupFocusNextInteractive(ih));
      else
        iupAttribSetStr(dialog, attrib, (char*)ih);

      /* used by iupmotKeyPressEvent */
      attrib[18] = '_';
      IupSetCallback(dialog, attrib, (Icallback)motActivateMnemonic);
    }

    iupmotSetString(ih->handle, XmNlabelString, str);
    free(str);
  }
  else
  {
    XtVaSetValues (ih->handle, XmNmnemonic, NULL, NULL);
    iupmotSetString(ih->handle, XmNlabelString, str);
  }
}

void iupmotSetString(Widget w, const char *resource, const char* value)
{
  XmString xm_str = XmStringCreateLocalized((String)value);
  XtVaSetValues(w, resource, xm_str, NULL);
  XmStringFree(xm_str);
}

char* iupmotConvertString(XmString str)
{
  char* text = (char*)XmStringUnparse(str, NULL, XmCHARSET_TEXT, XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL);
  char* buf = iupStrGetMemoryCopy(text);
  XtFree(text);
  return buf;
}

#ifdef OLD_CONVERT
char* iupmotConvertString(XmString str)
{
  XmStringContext context;
  char *text, *p, *buf;
  unsigned int length;
  XmStringComponentType type;

  if (!XmStringInitContext (&context, str))
    return NULL;

  buf = iupStrGetMemory(XmStringLength(str));  /* always greatter than strlen */

  /* p keeps a running pointer through buf as text is read */
  p = buf;
  while ((type = XmStringGetNextTriple(context, &length, (XtPointer)&text)) != XmSTRING_COMPONENT_END) 
  {
    switch (type) 
    {
    case XmSTRING_COMPONENT_TEXT:
      memcpy(p, text, length);
      p += length;
      *p = 0;
      break;
    case XmSTRING_COMPONENT_TAB:
      *p++ = '\t';
      *p = 0;
      break;
    case XmSTRING_COMPONENT_SEPARATOR:
      *p++ = '\n';
      *p = 0;
      break;
    }
    XtFree(text);
  }

  XmStringFreeContext(context);

  return buf;
}
#endif

void iupdrvReparent(Ihandle* ih)
{
  Widget native_parent = iupChildTreeGetNativeParentHandle(ih);
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;
  XReparentWindow(iupmot_display, XtWindow(widget), XtWindow(native_parent), 0, 0);
}

void iupdrvBaseLayoutUpdateMethod(Ihandle *ih)
{
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;

  /* avoid abort in X */
  if (ih->currentwidth == 0) ih->currentwidth = 1;
  if (ih->currentheight == 0) ih->currentheight = 1;

  XtVaSetValues(widget,
    XmNx, (XtArgVal)ih->x,
    XmNy, (XtArgVal)ih->y,
    XmNwidth, (XtArgVal)ih->currentwidth,
    XmNheight, (XtArgVal)ih->currentheight,
    NULL);
}

void iupdrvBaseUnMapMethod(Ihandle* ih)
{
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;

  XtUnrealizeWidget(widget); /* To match the call to XtRealizeWidget */
  XtDestroyWidget(widget);   /* To match the call to XtCreateManagedWidget */
}

void iupdrvPostRedraw(Ihandle *ih)
{
  XExposeEvent evt;
  Dimension w, h;

  XtVaGetValues(ih->handle, XmNwidth, &w, 
                            XmNheight, &h, 
                            NULL);

  evt.type = Expose;
  evt.display = iupmot_display;
  evt.send_event = True;
  evt.window = XtWindow(ih->handle);

  evt.x = 0;
  evt.y = 0;
  evt.width = w;
  evt.height = h;

  evt.count = 0;

  /* POST a Redraw */
  XSendEvent(iupmot_display, XtWindow(ih->handle), False, ExposureMask, (XEvent*)&evt);
}

void iupdrvRedrawNow(Ihandle *ih)
{
  Widget w;

  /* POST a Redraw */
  iupdrvPostRedraw(ih);

  /* if this element has an inner native parent (like IupTabs), 
     then redraw that native parent if different from the element. */
  w = (Widget)iupClassObjectGetInnerNativeContainerHandle(ih, (Ihandle*)IupGetAttribute(ih, "VALUE_HANDLE"));
  if (w && w != ih->handle)
  {
    Widget handle = ih->handle;
    ih->handle = w;
    iupdrvPostRedraw(ih);
    ih->handle = handle;
  }

  /* flush exposure events. */
  XmUpdateDisplay(ih->handle);
}

void iupdrvScreenToClient(Ihandle* ih, int *x, int *y)
{
  Window child;
  XTranslateCoordinates(iupmot_display, RootWindow(iupmot_display, iupmot_screen),
                                        XtWindow(ih->handle),
                                        *x, *y, x, y, &child);
}

void iupmotHelpCallback(Widget w, Ihandle *ih, XtPointer call_data)
{
  Icallback cb = IupGetCallback(ih, "HELP_CB");
  if (cb && cb(ih) == IUP_CLOSE)
    IupExitLoop();

  (void)call_data;
  (void)w;
}

void iupmotEnterLeaveWindowEvent(Widget w, Ihandle *ih, XEvent *evt, Boolean *cont)
{
  Icallback cb = NULL;
  (void)cont;
  (void)w;

  if (evt->type == EnterNotify)
  {
    iupmotTipEnterNotify(ih);

    cb = IupGetCallback(ih, "ENTERWINDOW_CB");
  }
  else  if (evt->type == LeaveNotify)
  {
    iupmotTipLeaveNotify();

    cb = IupGetCallback(ih, "LEAVEWINDOW_CB");
  }

  if (cb)
    cb(ih);
}

int iupdrvBaseSetZorderAttrib(Ihandle* ih, const char* value)
{
  if (iupdrvIsVisible(ih))
  {
    Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
    if (iupStrEqualNoCase(value, "TOP"))
      XRaiseWindow(iupmot_display, XtWindow(widget));
    else
      XLowerWindow(iupmot_display, XtWindow(widget));
  }

  return 0;
}

void iupdrvSetVisible(Ihandle* ih, int visible)
{
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;

  if (visible)
    XtMapWidget(widget);
  else
    XtUnmapWidget(widget);
}

int iupdrvIsVisible(Ihandle* ih)
{
  XWindowAttributes wa;
  XGetWindowAttributes(iupmot_display, XtWindow(ih->handle), &wa);
  return (wa.map_state == IsViewable);
}

int iupdrvIsActive(Ihandle* ih)
{
  return XtIsSensitive(ih->handle);
}

void iupdrvSetActive(Ihandle* ih, int enable)
{
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;

  XtSetSensitive(widget, enable);
}

char* iupmotGetXWindowAttrib(Ihandle *ih)
{
  return (char*)XtWindow(ih->handle);
}

char* iupdrvBaseGetXAttrib(Ihandle *ih)
{
  int x, y;
  Window child;
  char* str = iupStrGetMemory(20);
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;

  /* Translating to absolute screen coordinates */
                     /* source            destination */
  XTranslateCoordinates(iupmot_display, 
                        XtWindow(widget), RootWindow(iupmot_display, iupmot_screen), 
                        0, 0,             &x, &y, 
                        &child);

  sprintf(str, "%d", x);
  return str;
}

char* iupdrvBaseGetYAttrib(Ihandle *ih)
{
  int x, y;
  Window child;
  char* str = iupStrGetMemory(20);
  Widget widget = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (!widget) widget = ih->handle;

  /* Translating to absolute screen coordinates */
                     /* source            destination */
  XTranslateCoordinates(iupmot_display, 
                        XtWindow(widget), RootWindow(iupmot_display, iupmot_screen), 
                        0, 0,             &x, &y, 
                        &child);

  sprintf(str, "%d", y);
  return str;
}

void iupmotSetBgColor(Widget w, Pixel color)
{
  Pixel fgcolor;
  XtVaGetValues(w, XmNforeground, &fgcolor, NULL);
  XmChangeColor(w, color);
  /* XmChangeColor also sets the XmNforeground color, so we must reset to the previous one. */
  XtVaSetValues(w, XmNforeground, fgcolor, NULL);
  XtVaSetValues(w, XmNbackgroundPixmap, XmUNSPECIFIED_PIXMAP, NULL);
}

int iupdrvBaseSetBgColorAttrib(Ihandle* ih, const char* value)
{
  Pixel color = iupmotColorGetPixelStr(value);
  if (color != (Pixel)-1)
  {
    iupmotSetBgColor(ih->handle, color);

    /* update internal image cache for controls that have the IMAGE attribute */
    iupImageUpdateParent(ih);
  }
  return 1;
}

char* iupmotGetBgColorAttrib(Ihandle* ih)
{
  unsigned char r, g, b;
  Pixel color;
  char* str = iupStrGetMemory(20);
  XtVaGetValues(ih->handle, XmNbackground, &color, NULL); 
  iupmotColorGetRGB(color, &r, &g, &b);
  sprintf(str, "%d %d %d", (int)r, (int)g, (int)b);
  return str;
}

int iupdrvBaseSetFgColorAttrib(Ihandle* ih, const char* value)
{
  Pixel color = iupmotColorGetPixelStr(value);
  if (color != (Pixel)-1)
    XtVaSetValues(ih->handle, XmNforeground, color, NULL);
  return 1;
}

void iupmotGetWindowSize(Ihandle *ih, int *width, int *height)
{
  Dimension w, h;
  XtVaGetValues(ih->handle, XmNwidth, &w, 
                            XmNheight, &h, 
                            NULL);
  *width = w;
  *height = h;
}

static Cursor motEmptyCursor(Ihandle* ih)
{
  /* creates an empty cursor */
  XColor cursor_color = {0L,0,0,0,0,0};
  char bitsnull[1] = {0x00};
  Pixmap pixmapnull;
  Cursor cur;

  pixmapnull = XCreateBitmapFromData(iupmot_display,
    XtWindow(ih->handle),
    bitsnull,
    1,1);

  cur = XCreatePixmapCursor(iupmot_display,
    pixmapnull,
    pixmapnull,
    &cursor_color,
    &cursor_color,
    0,0);

  XFreePixmap(iupmot_display, pixmapnull);

  return cur;
}

static Cursor motGetCursor(Ihandle* ih, const char* name)
{
  static struct {
    const char* iupname;
    int         sysname;
  } table[] = {
    { "NONE",      0}, 
    { "NULL",      0}, 
    { "ARROW",     XC_left_ptr},
    { "BUSY",      XC_watch},
    { "CROSS",     XC_crosshair},
    { "HAND",      XC_hand2},
    { "HELP",      XC_question_arrow},
    { "IUP",       XC_question_arrow},
    { "MOVE",      XC_fleur},
    { "PEN",       XC_pencil},
    { "RESIZE_N",  XC_top_side},
    { "RESIZE_S",  XC_bottom_side},
    { "RESIZE_NS", XC_sb_v_double_arrow},
    { "SPLITTER_HORIZ", XC_sb_v_double_arrow},
    { "RESIZE_W",  XC_left_side},
    { "RESIZE_E",  XC_right_side},
    { "RESIZE_WE", XC_sb_h_double_arrow},
    { "SPLITTER_VERT", XC_sb_h_double_arrow},
    { "RESIZE_NE", XC_top_right_corner},
    { "RESIZE_SE", XC_bottom_right_corner},
    { "RESIZE_NW", XC_top_left_corner},
    { "RESIZE_SW", XC_bottom_left_corner},
    { "TEXT",      XC_xterm}, 
    { "UPARROW",   XC_center_ptr} 
  };

  Cursor cur;
  char str[50];
  int i, count = sizeof(table)/sizeof(table[0]);

  /* check the cursor cache first (per control)*/
  sprintf(str, "_IUPMOT_CURSOR_%s", name);
  cur = (Cursor)iupAttribGet(ih, str);
  if (cur)
    return cur;

  /* check the pre-defined IUP names first */
  for (i = 0; i < count; i++)
  {
    if (iupStrEqualNoCase(name, table[i].iupname)) 
    {
      if (table[i].sysname)
        cur = XCreateFontCursor(iupmot_display, table[i].sysname);
      else
        cur = motEmptyCursor(ih);

      break;
    }
  }

  if (i == count)
  {
    /* check for a name defined cursor */
    cur = (Cursor)iupImageGetCursor(name);
  }

  iupAttribSetStr(ih, str, (char*)cur);
  return cur;
}

int iupdrvBaseSetCursorAttrib(Ihandle* ih, const char* value)
{
  Cursor cur = motGetCursor(ih, value);
  if (cur)
  {
    XDefineCursor(iupmot_display, XtWindow(ih->handle), cur);
    return 1;
  }
  return 0;
}

#include <Xm/XmP.h>
#include <Xm/DrawP.h>

void iupdrvDrawFocusRect(Ihandle* ih, void* _gc, int x, int y, int w, int h)
{
  Drawable wnd = (Drawable)IupGetAttribute(ih, "XWINDOW");  /* Use IupGetAttribute to consult the native implemetation */
  GC gc = (GC)_gc;
  XmeDrawHighlight(iupmot_display, wnd, gc, x, y, w, h, 1);
}

void iupdrvBaseRegisterCommonAttrib(Iclass* ic)
{
  iupClassRegisterAttribute(ic, "XMFONTLIST", iupmotGetFontListAttrib, NULL, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT|IUPAF_NO_STRING);
  iupClassRegisterAttribute(ic, "XFONTSTRUCT", iupmotGetFontStructAttrib, NULL, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT|IUPAF_NO_STRING);
  iupClassRegisterAttribute(ic, "XFONTID", iupmotGetFontIdAttrib, NULL, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT|IUPAF_NO_STRING);
}

static void motDoNothing(Widget w, XEvent*  evt, String* params, Cardinal* num_params)
{
  (void)w;
  (void)evt;
  (void)params;
  (void)num_params;
}

void iupmotDisableDragSource(Widget w)
{
  char dragTranslations[] = "#override <Btn2Down>: iupDoNothing()";
  static int do_nothing_rec = 0;
  if (!do_nothing_rec)
  {
    XtActionsRec rec = {"iupDoNothing", (XtActionProc)motDoNothing};
    XtAppAddActions(iupmot_appcontext, &rec, 1);
    do_nothing_rec = 1;
  }
  XtOverrideTranslations(w, XtParseTranslationTable(dragTranslations));
}

int iupdrvGetScrollbarSize(void)
{
  return 15;
}

void iupmotSetPixmap(Ihandle* ih, const char* name, const char* prop, int make_inactive)
{
  if (name)
  {
    Pixmap old_pixmap;
    Pixmap pixmap = (Pixmap)iupImageGetImage(name, ih, make_inactive);
    if (!pixmap) 
      pixmap = XmUNSPECIFIED_PIXMAP;
    XtVaGetValues(ih->handle, prop, &old_pixmap, NULL);
    if (pixmap != old_pixmap)
      XtVaSetValues(ih->handle, prop, pixmap, NULL);
    return;
  }

  /* if not defined */
  XtVaSetValues(ih->handle, prop, XmUNSPECIFIED_PIXMAP, NULL);
}

void iupmotButtonPressReleaseEvent(Widget w, Ihandle* ih, XEvent* evt, Boolean* cont)
{
  unsigned long elapsed;
  static Time last = 0;
  char status[IUPKEY_STATUS_SIZE] = IUPKEY_STATUS_INIT;
  IFniiiis cb;

  XButtonEvent *but_evt = (XButtonEvent*)evt;
  if (but_evt->button!=Button1 &&
      but_evt->button!=Button2 &&
      but_evt->button!=Button3 &&
      but_evt->button!=Button4 &&
      but_evt->button!=Button5) 
    return;

  cb = (IFniiiis) IupGetCallback(ih,"BUTTON_CB");
  if (cb)
  {
    int ret, doubleclick = 0;
    int b = IUP_BUTTON1+(but_evt->button-1);

    /* Double/Single Click */
    if (but_evt->type==ButtonPress)
    {
      elapsed = but_evt->time - last;
      last = but_evt->time;
      if ((int)elapsed <= XtGetMultiClickTime(iupmot_display))
        doubleclick = 1;
    }

    iupmotButtonKeySetStatus(but_evt->state, but_evt->button, status, doubleclick);

    ret = cb(ih, b, (but_evt->type==ButtonPress), but_evt->x, but_evt->y, status);
    if (ret==IUP_CLOSE)
      IupExitLoop();
    else if (ret==IUP_IGNORE)
      *cont=False;
  }         

  (void)w;
}

void iupmotPointerMotionEvent(Widget w, Ihandle *ih, XEvent *evt, Boolean *cont)
{
  IFniis cb = (IFniis)IupGetCallback(ih,"MOTION_CB");
  if (cb)
  {
    XMotionEvent *motion_evt = (XMotionEvent*)evt;
    char status[IUPKEY_STATUS_SIZE] = IUPKEY_STATUS_INIT;
    iupmotButtonKeySetStatus(motion_evt->state, 0, status, 0);
    cb(ih, motion_evt->x, motion_evt->y, status);
  }

  (void)w;
  (void)cont;
}