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