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