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