Factored out some common code.
[wxWidgets.git] / src / motif / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __VMS
21 #define XtDisplay XTDISPLAY
22 #endif
23 #include "wx/setup.h"
24 #include "wx/utils.h"
25 #include "wx/app.h"
26 #include "wx/dcmemory.h"
27 #include "wx/bitmap.h"
28 #include "wx/evtloop.h"
29
30 #include <string.h>
31
32 #if (defined(__SUNCC__) || defined(__CLCC__))
33 #include <sysent.h>
34 #endif
35
36 #ifdef __VMS__
37 #pragma message disable nosimpint
38 #endif
39
40 #include "wx/unix/execute.h"
41
42 #include <Xm/Xm.h>
43 #include <Xm/Frame.h>
44
45 #include "wx/motif/private.h"
46
47 #if wxUSE_RESOURCES
48 #include "X11/Xresource.h"
49 #endif
50
51 #include "X11/Xutil.h"
52
53 #ifdef __VMS__
54 #pragma message enable nosimpint
55 #endif
56
57 // ----------------------------------------------------------------------------
58 // private functions
59 // ----------------------------------------------------------------------------
60
61 // Yuck this is really BOTH site and platform dependent
62 // so we should use some other strategy!
63 #ifdef sun
64 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
65 #else
66 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
67 #endif
68
69 #if wxUSE_RESOURCES
70 static char *GetIniFile (char *dest, const char *filename);
71 #endif
72
73 // ============================================================================
74 // implementation
75 // ============================================================================
76
77 // ----------------------------------------------------------------------------
78 // async event processing
79 // ----------------------------------------------------------------------------
80
81 // Consume all events until no more left
82 void wxFlushEvents(WXDisplay* wxdisplay)
83 {
84 Display *display = (Display*)wxdisplay;
85 wxEventLoop evtLoop;
86
87 XSync (display, FALSE);
88
89 while (evtLoop.Pending())
90 {
91 XFlush (display);
92 evtLoop.Dispatch();
93 }
94 }
95
96 // ----------------------------------------------------------------------------
97 // wxExecute stuff
98 // ----------------------------------------------------------------------------
99
100 static void xt_notify_end_process(XtPointer data, int *WXUNUSED(fid),
101 XtInputId *id)
102 {
103 wxEndProcessData *proc_data = (wxEndProcessData *)data;
104
105 wxHandleProcessTermination(proc_data);
106
107 // VZ: I think they should be the same...
108 wxASSERT( (int)*id == proc_data->tag );
109
110 XtRemoveInput(*id);
111 }
112
113 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
114 {
115 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
116 fd,
117 (XtPointer *) XtInputReadMask,
118 (XtInputCallbackProc) xt_notify_end_process,
119 (XtPointer) proc_data);
120
121 return (int)id;
122 }
123
124 // ----------------------------------------------------------------------------
125 // misc
126 // ----------------------------------------------------------------------------
127
128 // Emit a beeeeeep
129 void wxBell()
130 {
131 // Use current setting for the bell
132 XBell (wxGlobalDisplay(), 0);
133 }
134
135 int wxGetOsVersion(int *majorVsn, int *minorVsn)
136 {
137 // FIXME TODO
138 // This code is WRONG!! Does NOT return the
139 // Motif version of the libs but the X protocol
140 // version!
141 Display *display = wxGlobalDisplay();
142 if (majorVsn)
143 *majorVsn = ProtocolVersion (display);
144 if (minorVsn)
145 *minorVsn = ProtocolRevision (display);
146
147 return wxMOTIF_X;
148 }
149
150 // ----------------------------------------------------------------------------
151 // Reading and writing resources (eg WIN.INI, .Xdefaults)
152 // ----------------------------------------------------------------------------
153
154 #if wxUSE_RESOURCES
155
156 // Read $HOME for what it says is home, if not
157 // read $USER or $LOGNAME for user name else determine
158 // the Real User, then determine the Real home dir.
159 static char * GetIniFile (char *dest, const char *filename)
160 {
161 char *home = NULL;
162 if (filename && wxIsAbsolutePath(filename))
163 {
164 strcpy(dest, filename);
165 }
166 else if ((home = wxGetUserHome("")) != NULL)
167 {
168 strcpy(dest, home);
169 if (dest[strlen(dest) - 1] != '/')
170 strcat (dest, "/");
171 if (filename == NULL)
172 {
173 if ((filename = getenv ("XENVIRONMENT")) == NULL)
174 filename = ".Xdefaults";
175 }
176 else if (*filename != '.')
177 strcat (dest, ".");
178 strcat (dest, filename);
179 } else
180 {
181 dest[0] = '\0';
182 }
183 return dest;
184 }
185
186 static char *GetResourcePath(char *buf, const char *name, bool create = FALSE)
187 {
188 if (create && wxFileExists (name) ) {
189 strcpy(buf, name);
190 return buf; // Exists so ...
191 }
192
193 if (*name == '/')
194 strcpy(buf, name);
195 else {
196 // Put in standard place for resource files if not absolute
197 strcpy (buf, DEFAULT_XRESOURCE_DIR);
198 strcat (buf, "/");
199 strcat (buf, wxFileNameFromPath (name).c_str());
200 }
201
202 if (create) {
203 // Touch the file to create it
204 FILE *fd = fopen (buf, "w");
205 if (fd) fclose (fd);
206 }
207 return buf;
208 }
209
210 /*
211 * We have a cache for writing different resource files,
212 * which will only get flushed when we call wxFlushResources().
213 * Build up a list of resource databases waiting to be written.
214 *
215 */
216
217 wxList wxResourceCache (wxKEY_STRING);
218
219 void
220 wxFlushResources (void)
221 {
222 char nameBuffer[512];
223
224 wxNode *node = wxResourceCache.First ();
225 while (node)
226 {
227 const char *file = node->GetKeyString();
228 // If file doesn't exist, create it first.
229 (void)GetResourcePath(nameBuffer, file, TRUE);
230
231 XrmDatabase database = (XrmDatabase) node->Data ();
232 XrmPutFileDatabase (database, nameBuffer);
233 XrmDestroyDatabase (database);
234 wxNode *next = node->Next ();
235 delete node;
236 node = next;
237 }
238 }
239
240 static XrmDatabase wxResourceDatabase = 0;
241
242 void wxXMergeDatabases (wxApp * theApp, Display * display);
243
244 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
245 {
246 char buffer[500];
247
248 (void) GetIniFile (buffer, file);
249
250 XrmDatabase database;
251 wxNode *node = wxResourceCache.Find (buffer);
252 if (node)
253 database = (XrmDatabase) node->Data ();
254 else
255 {
256 database = XrmGetFileDatabase (buffer);
257 wxResourceCache.Append (buffer, (wxObject *) database);
258 }
259
260 char resName[300];
261 strcpy (resName, section.c_str());
262 strcat (resName, ".");
263 strcat (resName, entry.c_str());
264
265 XrmPutStringResource (&database, resName, value);
266 return TRUE;
267 }
268
269 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
270 {
271 char buf[50];
272 sprintf(buf, "%.4f", value);
273 return wxWriteResource(section, entry, buf, file);
274 }
275
276 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
277 {
278 char buf[50];
279 sprintf(buf, "%ld", value);
280 return wxWriteResource(section, entry, buf, file);
281 }
282
283 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
284 {
285 char buf[50];
286 sprintf(buf, "%d", value);
287 return wxWriteResource(section, entry, buf, file);
288 }
289
290 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
291 {
292 if (!wxResourceDatabase)
293 {
294 Display *display = wxGlobalDisplay();
295 wxXMergeDatabases (wxTheApp, display);
296 }
297
298 XrmDatabase database;
299
300 if (file != "")
301 {
302 char buffer[500];
303
304 // Is this right? Trying to get it to look in the user's
305 // home directory instead of current directory -- JACS
306 (void) GetIniFile (buffer, file);
307
308 wxNode *node = wxResourceCache.Find (buffer);
309 if (node)
310 database = (XrmDatabase) node->Data ();
311 else
312 {
313 database = XrmGetFileDatabase (buffer);
314 wxResourceCache.Append (buffer, (wxObject *) database);
315 }
316 }
317 else
318 database = wxResourceDatabase;
319
320 XrmValue xvalue;
321 char *str_type[20];
322 char buf[150];
323 strcpy (buf, section);
324 strcat (buf, ".");
325 strcat (buf, entry);
326
327 Bool success = XrmGetResource (database, buf, "*", str_type,
328 &xvalue);
329 // Try different combinations of upper/lower case, just in case...
330 if (!success)
331 {
332 buf[0] = (isupper (buf[0]) ? tolower (buf[0]) : toupper (buf[0]));
333 success = XrmGetResource (database, buf, "*", str_type,
334 &xvalue);
335 }
336 if (success)
337 {
338 if (*value)
339 delete[] *value;
340
341 *value = new char[xvalue.size + 1];
342 strncpy (*value, xvalue.addr, (int) xvalue.size);
343 return TRUE;
344 }
345 return FALSE;
346 }
347
348 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
349 {
350 char *s = NULL;
351 bool succ = wxGetResource(section, entry, (char **)&s, file);
352 if (succ)
353 {
354 *value = (float)strtod(s, NULL);
355 delete[] s;
356 return TRUE;
357 }
358 else return FALSE;
359 }
360
361 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
362 {
363 char *s = NULL;
364 bool succ = wxGetResource(section, entry, (char **)&s, file);
365 if (succ)
366 {
367 *value = strtol(s, NULL, 10);
368 delete[] s;
369 return TRUE;
370 }
371 else return FALSE;
372 }
373
374 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
375 {
376 char *s = NULL;
377 bool succ = wxGetResource(section, entry, (char **)&s, file);
378 if (succ)
379 {
380 // Handle True, False here
381 // True, Yes, Enables, Set or Activated
382 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
383 *value = TRUE;
384 // False, No, Disabled, Reset, Cleared, Deactivated
385 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
386 *value = FALSE;
387 // Handle as Integer
388 else
389 *value = (int) strtol (s, NULL, 10);
390 delete[] s;
391 return TRUE;
392 }
393 else
394 return FALSE;
395 }
396
397 void wxXMergeDatabases (wxApp * theApp, Display * display)
398 {
399 XrmDatabase homeDB, serverDB, applicationDB;
400 char filenamebuf[1024];
401
402 char *filename = &filenamebuf[0];
403 char *environment;
404 wxString classname = theApp->GetClassName();
405 char name[256];
406 (void) strcpy (name, "/usr/lib/X11/app-defaults/");
407 (void) strcat (name, classname.c_str());
408
409 /* Get application defaults file, if any */
410 applicationDB = XrmGetFileDatabase (name);
411 (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase);
412
413 /* Merge server defaults, created by xrdb, loaded as a property of the root
414 * window when the server initializes and loaded into the display
415 * structure on XOpenDisplay;
416 * if not defined, use .Xdefaults
417 */
418
419 if (XResourceManagerString (display) != NULL)
420 {
421 serverDB = XrmGetStringDatabase (XResourceManagerString (display));
422 }
423 else
424 {
425 (void) GetIniFile (filename, NULL);
426 serverDB = XrmGetFileDatabase (filename);
427 }
428 XrmMergeDatabases (serverDB, &wxResourceDatabase);
429
430 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
431 * and merge into existing database
432 */
433
434 if ((environment = getenv ("XENVIRONMENT")) == NULL)
435 {
436 size_t len;
437 environment = GetIniFile (filename, NULL);
438 len = strlen (environment);
439 wxString hostname = wxGetHostName();
440 if ( !!hostname )
441 strncat(environment, hostname, 1024 - len);
442 }
443 homeDB = XrmGetFileDatabase (environment);
444 XrmMergeDatabases (homeDB, &wxResourceDatabase);
445 }
446
447 #if 0
448
449 /*
450 * Not yet used but may be useful.
451 *
452 */
453 void
454 wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name)
455 {
456 int i;
457 Display *dpy = XtDisplay (w); // Retrieve the display pointer
458
459 XrmDatabase rdb = NULL; // A resource data base
460
461 // Create an empty resource database
462 rdb = XrmGetStringDatabase ("");
463
464 // Add the Component resources, prepending the name of the component
465
466 i = 0;
467 while (resourceSpec[i] != NULL)
468 {
469 char buf[1000];
470
471 sprintf (buf, "*%s%s", name, resourceSpec[i++]);
472 XrmPutLineResource (&rdb, buf);
473 }
474
475 // Merge them into the Xt database, with lowest precendence
476
477 if (rdb)
478 {
479 #if (XlibSpecificationRelease>=5)
480 XrmDatabase db = XtDatabase (dpy);
481 XrmCombineDatabase (rdb, &db, FALSE);
482 #else
483 XrmMergeDatabases (dpy->db, &rdb);
484 dpy->db = rdb;
485 #endif
486 }
487 }
488 #endif
489 // 0
490
491 #endif // wxUSE_RESOURCES
492
493 // ----------------------------------------------------------------------------
494 // display info
495 // ----------------------------------------------------------------------------
496
497 void wxGetMousePosition( int* x, int* y )
498 {
499 #if wxUSE_NANOX
500 // TODO
501 *x = 0;
502 *y = 0;
503 #else
504 XMotionEvent xev;
505 Window root, child;
506 XQueryPointer(wxGlobalDisplay(),
507 DefaultRootWindow(wxGlobalDisplay()),
508 &root, &child,
509 &(xev.x_root), &(xev.y_root),
510 &(xev.x), &(xev.y),
511 &(xev.state));
512 *x = xev.x_root;
513 *y = xev.y_root;
514 #endif
515 };
516
517 // Return TRUE if we have a colour display
518 bool wxColourDisplay()
519 {
520 return wxDisplayDepth() > 1;
521 }
522
523 // Returns depth of screen
524 int wxDisplayDepth()
525 {
526 Display *dpy = wxGlobalDisplay();
527
528 return DefaultDepth (dpy, DefaultScreen (dpy));
529 }
530
531 // Get size of display
532 void wxDisplaySize(int *width, int *height)
533 {
534 Display *dpy = wxGlobalDisplay();
535
536 if ( width )
537 *width = DisplayWidth (dpy, DefaultScreen (dpy));
538 if ( height )
539 *height = DisplayHeight (dpy, DefaultScreen (dpy));
540 }
541
542 void wxDisplaySizeMM(int *width, int *height)
543 {
544 Display *dpy = wxGlobalDisplay();
545
546 if ( width )
547 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
548 if ( height )
549 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
550 }
551
552 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
553 {
554 // This is supposed to return desktop dimensions minus any window
555 // manager panels, menus, taskbars, etc. If there is a way to do that
556 // for this platform please fix this function, otherwise it defaults
557 // to the entire desktop.
558 if (x) *x = 0;
559 if (y) *y = 0;
560 wxDisplaySize(width, height);
561 }
562
563
564 // Configurable display in wxX11 and wxMotif
565 static WXDisplay *gs_currentDisplay = NULL;
566 static wxString gs_displayName;
567
568 WXDisplay *wxGetDisplay()
569 {
570 if (gs_currentDisplay)
571 return gs_currentDisplay;
572 else if (wxTheApp)
573 return wxTheApp->GetInitialDisplay();
574 return NULL;
575 }
576
577 bool wxSetDisplay(const wxString& display_name)
578 {
579 gs_displayName = display_name;
580
581 if ( display_name.IsEmpty() )
582 {
583 gs_currentDisplay = NULL;
584
585 return TRUE;
586 }
587 else
588 {
589 Cardinal argc = 0;
590
591 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
592 display_name.c_str(),
593 wxTheApp->GetAppName().c_str(),
594 wxTheApp->GetClassName().c_str(),
595 NULL,
596 #if XtSpecificationRelease < 5
597 0, &argc,
598 #else
599 0, (int *)&argc,
600 #endif
601 NULL);
602
603 if (display)
604 {
605 gs_currentDisplay = (WXDisplay*) display;
606 return TRUE;
607 }
608 else
609 return FALSE;
610 }
611 }
612
613 wxString wxGetDisplayName()
614 {
615 return gs_displayName;
616 }
617
618 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
619 {
620 return wxGenericFindWindowAtPoint(pt);
621 }
622
623 // ----------------------------------------------------------------------------
624 // keycode translations
625 // ----------------------------------------------------------------------------
626
627 #include <X11/keysym.h>
628
629 // FIXME what about tables??
630
631 int wxCharCodeXToWX(KeySym keySym)
632 {
633 int id;
634 switch (keySym)
635 {
636 case XK_Shift_L:
637 case XK_Shift_R:
638 id = WXK_SHIFT; break;
639 case XK_Control_L:
640 case XK_Control_R:
641 id = WXK_CONTROL; break;
642 case XK_BackSpace:
643 id = WXK_BACK; break;
644 case XK_Delete:
645 id = WXK_DELETE; break;
646 case XK_Clear:
647 id = WXK_CLEAR; break;
648 case XK_Tab:
649 id = WXK_TAB; break;
650 case XK_numbersign:
651 id = '#'; break;
652 case XK_Return:
653 id = WXK_RETURN; break;
654 case XK_Escape:
655 id = WXK_ESCAPE; break;
656 case XK_Pause:
657 case XK_Break:
658 id = WXK_PAUSE; break;
659 case XK_Num_Lock:
660 id = WXK_NUMLOCK; break;
661 case XK_Scroll_Lock:
662 id = WXK_SCROLL; break;
663
664 case XK_Home:
665 id = WXK_HOME; break;
666 case XK_End:
667 id = WXK_END; break;
668 case XK_Left:
669 id = WXK_LEFT; break;
670 case XK_Right:
671 id = WXK_RIGHT; break;
672 case XK_Up:
673 id = WXK_UP; break;
674 case XK_Down:
675 id = WXK_DOWN; break;
676 case XK_Next:
677 id = WXK_NEXT; break;
678 case XK_Prior:
679 id = WXK_PRIOR; break;
680 case XK_Menu:
681 id = WXK_MENU; break;
682 case XK_Select:
683 id = WXK_SELECT; break;
684 case XK_Cancel:
685 id = WXK_CANCEL; break;
686 case XK_Print:
687 id = WXK_PRINT; break;
688 case XK_Execute:
689 id = WXK_EXECUTE; break;
690 case XK_Insert:
691 id = WXK_INSERT; break;
692 case XK_Help:
693 id = WXK_HELP; break;
694
695 case XK_KP_Multiply:
696 id = WXK_MULTIPLY; break;
697 case XK_KP_Add:
698 id = WXK_ADD; break;
699 case XK_KP_Subtract:
700 id = WXK_SUBTRACT; break;
701 case XK_KP_Divide:
702 id = WXK_DIVIDE; break;
703 case XK_KP_Decimal:
704 id = WXK_DECIMAL; break;
705 case XK_KP_Equal:
706 id = '='; break;
707 case XK_KP_Space:
708 id = ' '; break;
709 case XK_KP_Tab:
710 id = WXK_TAB; break;
711 case XK_KP_Enter:
712 id = WXK_RETURN; break;
713 case XK_KP_0:
714 id = WXK_NUMPAD0; break;
715 case XK_KP_1:
716 id = WXK_NUMPAD1; break;
717 case XK_KP_2:
718 id = WXK_NUMPAD2; break;
719 case XK_KP_3:
720 id = WXK_NUMPAD3; break;
721 case XK_KP_4:
722 id = WXK_NUMPAD4; break;
723 case XK_KP_5:
724 id = WXK_NUMPAD5; break;
725 case XK_KP_6:
726 id = WXK_NUMPAD6; break;
727 case XK_KP_7:
728 id = WXK_NUMPAD7; break;
729 case XK_KP_8:
730 id = WXK_NUMPAD8; break;
731 case XK_KP_9:
732 id = WXK_NUMPAD9; break;
733 case XK_F1:
734 id = WXK_F1; break;
735 case XK_F2:
736 id = WXK_F2; break;
737 case XK_F3:
738 id = WXK_F3; break;
739 case XK_F4:
740 id = WXK_F4; break;
741 case XK_F5:
742 id = WXK_F5; break;
743 case XK_F6:
744 id = WXK_F6; break;
745 case XK_F7:
746 id = WXK_F7; break;
747 case XK_F8:
748 id = WXK_F8; break;
749 case XK_F9:
750 id = WXK_F9; break;
751 case XK_F10:
752 id = WXK_F10; break;
753 case XK_F11:
754 id = WXK_F11; break;
755 case XK_F12:
756 id = WXK_F12; break;
757 case XK_F13:
758 id = WXK_F13; break;
759 case XK_F14:
760 id = WXK_F14; break;
761 case XK_F15:
762 id = WXK_F15; break;
763 case XK_F16:
764 id = WXK_F16; break;
765 case XK_F17:
766 id = WXK_F17; break;
767 case XK_F18:
768 id = WXK_F18; break;
769 case XK_F19:
770 id = WXK_F19; break;
771 case XK_F20:
772 id = WXK_F20; break;
773 case XK_F21:
774 id = WXK_F21; break;
775 case XK_F22:
776 id = WXK_F22; break;
777 case XK_F23:
778 id = WXK_F23; break;
779 case XK_F24:
780 id = WXK_F24; break;
781 default:
782 id = (keySym <= 255) ? (int)keySym : -1;
783 }
784
785 return id;
786 }
787
788 KeySym wxCharCodeWXToX(int id)
789 {
790 KeySym keySym;
791
792 switch (id)
793 {
794 case WXK_CANCEL: keySym = XK_Cancel; break;
795 case WXK_BACK: keySym = XK_BackSpace; break;
796 case WXK_TAB: keySym = XK_Tab; break;
797 case WXK_CLEAR: keySym = XK_Clear; break;
798 case WXK_RETURN: keySym = XK_Return; break;
799 case WXK_SHIFT: keySym = XK_Shift_L; break;
800 case WXK_CONTROL: keySym = XK_Control_L; break;
801 case WXK_MENU : keySym = XK_Menu; break;
802 case WXK_PAUSE: keySym = XK_Pause; break;
803 case WXK_ESCAPE: keySym = XK_Escape; break;
804 case WXK_SPACE: keySym = ' '; break;
805 case WXK_PRIOR: keySym = XK_Prior; break;
806 case WXK_NEXT : keySym = XK_Next; break;
807 case WXK_END: keySym = XK_End; break;
808 case WXK_HOME : keySym = XK_Home; break;
809 case WXK_LEFT : keySym = XK_Left; break;
810 case WXK_UP: keySym = XK_Up; break;
811 case WXK_RIGHT: keySym = XK_Right; break;
812 case WXK_DOWN : keySym = XK_Down; break;
813 case WXK_SELECT: keySym = XK_Select; break;
814 case WXK_PRINT: keySym = XK_Print; break;
815 case WXK_EXECUTE: keySym = XK_Execute; break;
816 case WXK_INSERT: keySym = XK_Insert; break;
817 case WXK_DELETE: keySym = XK_Delete; break;
818 case WXK_HELP : keySym = XK_Help; break;
819 case WXK_NUMPAD0: keySym = XK_KP_0; break;
820 case WXK_NUMPAD1: keySym = XK_KP_1; break;
821 case WXK_NUMPAD2: keySym = XK_KP_2; break;
822 case WXK_NUMPAD3: keySym = XK_KP_3; break;
823 case WXK_NUMPAD4: keySym = XK_KP_4; break;
824 case WXK_NUMPAD5: keySym = XK_KP_5; break;
825 case WXK_NUMPAD6: keySym = XK_KP_6; break;
826 case WXK_NUMPAD7: keySym = XK_KP_7; break;
827 case WXK_NUMPAD8: keySym = XK_KP_8; break;
828 case WXK_NUMPAD9: keySym = XK_KP_9; break;
829 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
830 case WXK_ADD: keySym = XK_KP_Add; break;
831 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
832 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
833 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
834 case WXK_F1: keySym = XK_F1; break;
835 case WXK_F2: keySym = XK_F2; break;
836 case WXK_F3: keySym = XK_F3; break;
837 case WXK_F4: keySym = XK_F4; break;
838 case WXK_F5: keySym = XK_F5; break;
839 case WXK_F6: keySym = XK_F6; break;
840 case WXK_F7: keySym = XK_F7; break;
841 case WXK_F8: keySym = XK_F8; break;
842 case WXK_F9: keySym = XK_F9; break;
843 case WXK_F10: keySym = XK_F10; break;
844 case WXK_F11: keySym = XK_F11; break;
845 case WXK_F12: keySym = XK_F12; break;
846 case WXK_F13: keySym = XK_F13; break;
847 case WXK_F14: keySym = XK_F14; break;
848 case WXK_F15: keySym = XK_F15; break;
849 case WXK_F16: keySym = XK_F16; break;
850 case WXK_F17: keySym = XK_F17; break;
851 case WXK_F18: keySym = XK_F18; break;
852 case WXK_F19: keySym = XK_F19; break;
853 case WXK_F20: keySym = XK_F20; break;
854 case WXK_F21: keySym = XK_F21; break;
855 case WXK_F22: keySym = XK_F22; break;
856 case WXK_F23: keySym = XK_F23; break;
857 case WXK_F24: keySym = XK_F24; break;
858 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
859 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
860 default: keySym = id <= 255 ? (KeySym)id : 0;
861 }
862
863 return keySym;
864 }
865
866 // ----------------------------------------------------------------------------
867 // Some colour manipulation routines
868 // ----------------------------------------------------------------------------
869
870 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
871 {
872 int h = hsv->h;
873 int s = hsv->s;
874 int v = hsv->v;
875 int r = 0, g = 0, b = 0;
876 int i, f;
877 int p, q, t;
878 s = (s * wxMAX_RGB) / wxMAX_SV;
879 v = (v * wxMAX_RGB) / wxMAX_SV;
880 if (h == 360) h = 0;
881 if (s == 0) { h = 0; r = g = b = v; }
882 i = h / 60;
883 f = h % 60;
884 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
885 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
886 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
887 switch (i)
888 {
889 case 0: r = v, g = t, b = p; break;
890 case 1: r = q, g = v, b = p; break;
891 case 2: r = p, g = v, b = t; break;
892 case 3: r = p, g = q, b = v; break;
893 case 4: r = t, g = p, b = v; break;
894 case 5: r = v, g = p, b = q; break;
895 }
896 rgb->red = r << 8;
897 rgb->green = g << 8;
898 rgb->blue = b << 8;
899 }
900
901 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
902 {
903 int r = rgb->red >> 8;
904 int g = rgb->green >> 8;
905 int b = rgb->blue >> 8;
906 int maxv = wxMax3(r, g, b);
907 int minv = wxMin3(r, g, b);
908 int h = 0, s, v;
909 v = maxv;
910 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
911 else s = 0;
912 if (s == 0) h = 0;
913 else
914 {
915 int rc, gc, bc, hex = 0;
916 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
917 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
918 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
919 if (r == maxv) { h = bc - gc, hex = 0; }
920 else if (g == maxv) { h = rc - bc, hex = 2; }
921 else if (b == maxv) { h = gc - rc, hex = 4; }
922 h = hex * 60 + (h * 60 / wxMAX_RGB);
923 if (h < 0) h += 360;
924 }
925 hsv->h = h;
926 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
927 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
928 }
929
930 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
931 {
932 #if !wxUSE_NANOX
933 int llp;
934
935 int screen = DefaultScreen(d);
936 int num_colors = DisplayCells(d,screen);
937
938 XColor *color_defs = new XColor[num_colors];
939 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
940 XQueryColors(d,cmp,color_defs,num_colors);
941
942 wxHSV hsv_defs, hsv;
943 wxXColorToHSV(&hsv,xc);
944
945 int diff, min_diff = 0, pixel = 0;
946
947 for(llp = 0;llp < num_colors;llp++)
948 {
949 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
950 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
951 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
952 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
953 if (llp == 0) min_diff = diff;
954 if (min_diff > diff) { min_diff = diff; pixel = llp; }
955 if (min_diff == 0) break;
956 }
957
958 xc -> red = color_defs[pixel].red;
959 xc -> green = color_defs[pixel].green;
960 xc -> blue = color_defs[pixel].blue;
961 xc -> flags = DoRed | DoGreen | DoBlue;
962
963 /* FIXME, TODO
964 if (!XAllocColor(d,cmp,xc))
965 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
966 */
967
968 delete[] color_defs;
969 #endif
970 }
971
972 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
973 {
974 if (!XAllocColor(d,cmp,xc))
975 {
976 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
977 wxAllocNearestColor(d,cmp,xc);
978 }
979 }
980
981 #ifdef __WXDEBUG__
982 wxString wxGetXEventName(XEvent& event)
983 {
984 #if wxUSE_NANOX
985 wxString str(wxT("(some event)"));
986 return str;
987 #else
988 int type = event.xany.type;
989 static char* event_name[] = {
990 "", "unknown(-)", // 0-1
991 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
992 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
993 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
994 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
995 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
996 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
997 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
998 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
999 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1000 "ClientMessage", "MappingNotify", // 33-34
1001 "unknown(+)"}; // 35
1002 type = wxMin(35, type); type = wxMax(1, type);
1003 wxString str(event_name[type]);
1004 return str;
1005 #endif
1006 }
1007 #endif
1008
1009 // ----------------------------------------------------------------------------
1010 // accelerators
1011 // ----------------------------------------------------------------------------
1012
1013 // Find the letter corresponding to the mnemonic, for Motif
1014 char wxFindMnemonic (const char *s)
1015 {
1016 char mnem = 0;
1017 int len = strlen (s);
1018 int i;
1019
1020 for (i = 0; i < len; i++)
1021 {
1022 if (s[i] == '&')
1023 {
1024 // Carefully handle &&
1025 if ((i + 1) <= len && s[i + 1] == '&')
1026 i++;
1027 else
1028 {
1029 mnem = s[i + 1];
1030 break;
1031 }
1032 }
1033 }
1034 return mnem;
1035 }
1036
1037 char* wxFindAccelerator( const char *s )
1038 {
1039 #if 1
1040 // VZ: this function returns incorrect keysym which completely breaks kbd
1041 // handling
1042 return NULL;
1043 #else
1044 // The accelerator text is after the \t char.
1045 s = strchr( s, '\t' );
1046
1047 if( !s ) return NULL;
1048
1049 /*
1050 Now we need to format it as X standard:
1051
1052 input output
1053
1054 F7 --> <Key>F7
1055 Ctrl+N --> Ctrl<Key>N
1056 Alt+k --> Meta<Key>k
1057 Ctrl+Shift+A --> Ctrl Shift<Key>A
1058
1059 and handle Ctrl-N & similia
1060 */
1061
1062 static char buf[256];
1063
1064 buf[0] = '\0';
1065 wxString tmp = s + 1; // skip TAB
1066 size_t index = 0;
1067
1068 while( index < tmp.length() )
1069 {
1070 size_t plus = tmp.find( '+', index );
1071 size_t minus = tmp.find( '-', index );
1072
1073 // neither '+' nor '-', add <Key>
1074 if( plus == wxString::npos && minus == wxString::npos )
1075 {
1076 strcat( buf, "<Key>" );
1077 strcat( buf, tmp.c_str() + index );
1078
1079 return buf;
1080 }
1081
1082 // OK: npos is big and positive
1083 size_t sep = wxMin( plus, minus );
1084 wxString mod = tmp.substr( index, sep - index );
1085
1086 // Ctrl -> Ctrl
1087 // Shift -> Shift
1088 // Alt -> Meta
1089 if( mod == "Alt" )
1090 mod = "Meta";
1091
1092 if( buf[0] )
1093 strcat( buf, " " );
1094
1095 strcat( buf, mod.c_str() );
1096
1097 index = sep + 1;
1098 }
1099
1100 return NULL;
1101 #endif
1102 }
1103
1104 XmString wxFindAcceleratorText (const char *s)
1105 {
1106 #if 1
1107 // VZ: this function returns incorrect keysym which completely breaks kbd
1108 // handling
1109 return NULL;
1110 #else
1111 // The accelerator text is after the \t char.
1112 s = strchr( s, '\t' );
1113
1114 if( !s ) return NULL;
1115
1116 return wxStringToXmString( s + 1 ); // skip TAB!
1117 #endif
1118 }
1119
1120 // Change a widget's foreground and background colours.
1121 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
1122 {
1123 // When should we specify the foreground, if it's calculated
1124 // by wxComputeColours?
1125 // Solution: say we start with the default (computed) foreground colour.
1126 // If we call SetForegroundColour explicitly for a control or window,
1127 // then the foreground is changed.
1128 // Therefore SetBackgroundColour computes the foreground colour, and
1129 // SetForegroundColour changes the foreground colour. The ordering is
1130 // important.
1131
1132 XtVaSetValues ((Widget) widget,
1133 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
1134 NULL);
1135 }
1136
1137 void wxDoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour)
1138 {
1139 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
1140 (wxColour*) NULL);
1141
1142 XtVaSetValues ((Widget) widget,
1143 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
1144 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
1145 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
1146 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
1147 NULL);
1148
1149 if (changeArmColour)
1150 XtVaSetValues ((Widget) widget,
1151 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
1152 NULL);
1153 }
1154
1155 extern void wxDoChangeFont(WXWidget widget, wxFont& font)
1156 {
1157 // Lesstif 0.87 hangs here, but 0.93 does not
1158 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1159 Widget w = (Widget)widget;
1160 XtVaSetValues( w,
1161 wxFont::GetFontTag(), font.GetFontType( XtDisplay(w) ),
1162 NULL );
1163 #endif
1164
1165 }
1166
1167 wxString wxXmStringToString( const XmString& xmString )
1168 {
1169 char *txt;
1170 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
1171 {
1172 wxString str(txt);
1173 XtFree (txt);
1174 return str;
1175 }
1176
1177 return wxEmptyString;
1178 }
1179
1180 XmString wxStringToXmString( const wxString& str )
1181 {
1182 return XmStringCreateLtoR((char *)str.c_str(), XmSTRING_DEFAULT_CHARSET);
1183 }
1184
1185 XmString wxStringToXmString( const char* str )
1186 {
1187 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
1188 }
1189
1190 // ----------------------------------------------------------------------------
1191 // wxBitmap utility functions
1192 // ----------------------------------------------------------------------------
1193
1194 // Creates a bitmap with transparent areas drawn in
1195 // the given colour.
1196 wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, wxColour& colour)
1197 {
1198 wxBitmap newBitmap(bitmap.GetWidth(),
1199 bitmap.GetHeight(),
1200 bitmap.GetDepth());
1201 wxMemoryDC destDC;
1202 wxMemoryDC srcDC;
1203
1204 srcDC.SelectObject(bitmap);
1205 destDC.SelectObject(newBitmap);
1206
1207 wxBrush brush(colour, wxSOLID);
1208 // destDC.SetOptimization(FALSE);
1209 destDC.SetBackground(brush);
1210 destDC.Clear();
1211 destDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(),
1212 &srcDC, 0, 0, wxCOPY, TRUE);
1213
1214 return newBitmap;
1215 }
1216
1217 // ----------------------------------------------------------------------------
1218 // Miscellaneous functions
1219 // ----------------------------------------------------------------------------
1220
1221 WXWidget wxCreateBorderWidget( WXWidget parent, long style )
1222 {
1223 Widget borderWidget = (Widget)NULL, parentWidget = (Widget)parent;
1224
1225 if (style & wxSIMPLE_BORDER)
1226 {
1227 borderWidget = XtVaCreateManagedWidget
1228 (
1229 "simpleBorder",
1230 xmFrameWidgetClass, parentWidget,
1231 XmNshadowType, XmSHADOW_ETCHED_IN,
1232 XmNshadowThickness, 1,
1233 NULL
1234 );
1235 }
1236 else if (style & wxSUNKEN_BORDER)
1237 {
1238 borderWidget = XtVaCreateManagedWidget
1239 (
1240 "sunkenBorder",
1241 xmFrameWidgetClass, parentWidget,
1242 XmNshadowType, XmSHADOW_IN,
1243 NULL
1244 );
1245 }
1246 else if (style & wxRAISED_BORDER)
1247 {
1248 borderWidget = XtVaCreateManagedWidget
1249 (
1250 "raisedBorder",
1251 xmFrameWidgetClass, parentWidget,
1252 XmNshadowType, XmSHADOW_OUT,
1253 NULL
1254 );
1255 }
1256
1257 return borderWidget;
1258 }