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