]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
Removed condition of old Watcom times. Open Watcom doesn't have problems with wxAutom...
[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 // Now in utilsx11.cpp
638 #if 0
639
640 // ----------------------------------------------------------------------------
641 // keycode translations
642 // ----------------------------------------------------------------------------
643
644 #include <X11/keysym.h>
645
646 // FIXME what about tables??
647
648 int wxCharCodeXToWX(KeySym keySym)
649 {
650 int id;
651 switch (keySym)
652 {
653 case XK_Shift_L:
654 case XK_Shift_R:
655 id = WXK_SHIFT; break;
656 case XK_Control_L:
657 case XK_Control_R:
658 id = WXK_CONTROL; break;
659 case XK_BackSpace:
660 id = WXK_BACK; break;
661 case XK_Delete:
662 id = WXK_DELETE; break;
663 case XK_Clear:
664 id = WXK_CLEAR; break;
665 case XK_Tab:
666 id = WXK_TAB; break;
667 case XK_numbersign:
668 id = '#'; break;
669 case XK_Return:
670 id = WXK_RETURN; break;
671 case XK_Escape:
672 id = WXK_ESCAPE; break;
673 case XK_Pause:
674 case XK_Break:
675 id = WXK_PAUSE; break;
676 case XK_Num_Lock:
677 id = WXK_NUMLOCK; break;
678 case XK_Scroll_Lock:
679 id = WXK_SCROLL; break;
680
681 case XK_Home:
682 id = WXK_HOME; break;
683 case XK_End:
684 id = WXK_END; break;
685 case XK_Left:
686 id = WXK_LEFT; break;
687 case XK_Right:
688 id = WXK_RIGHT; break;
689 case XK_Up:
690 id = WXK_UP; break;
691 case XK_Down:
692 id = WXK_DOWN; break;
693 case XK_Next:
694 id = WXK_NEXT; break;
695 case XK_Prior:
696 id = WXK_PRIOR; break;
697 case XK_Menu:
698 id = WXK_MENU; break;
699 case XK_Select:
700 id = WXK_SELECT; break;
701 case XK_Cancel:
702 id = WXK_CANCEL; break;
703 case XK_Print:
704 id = WXK_PRINT; break;
705 case XK_Execute:
706 id = WXK_EXECUTE; break;
707 case XK_Insert:
708 id = WXK_INSERT; break;
709 case XK_Help:
710 id = WXK_HELP; break;
711
712 case XK_KP_Multiply:
713 id = WXK_MULTIPLY; break;
714 case XK_KP_Add:
715 id = WXK_ADD; break;
716 case XK_KP_Subtract:
717 id = WXK_SUBTRACT; break;
718 case XK_KP_Divide:
719 id = WXK_DIVIDE; break;
720 case XK_KP_Decimal:
721 id = WXK_DECIMAL; break;
722 case XK_KP_Equal:
723 id = '='; break;
724 case XK_KP_Space:
725 id = ' '; break;
726 case XK_KP_Tab:
727 id = WXK_TAB; break;
728 case XK_KP_Enter:
729 id = WXK_RETURN; break;
730 case XK_KP_0:
731 id = WXK_NUMPAD0; break;
732 case XK_KP_1:
733 id = WXK_NUMPAD1; break;
734 case XK_KP_2:
735 id = WXK_NUMPAD2; break;
736 case XK_KP_3:
737 id = WXK_NUMPAD3; break;
738 case XK_KP_4:
739 id = WXK_NUMPAD4; break;
740 case XK_KP_5:
741 id = WXK_NUMPAD5; break;
742 case XK_KP_6:
743 id = WXK_NUMPAD6; break;
744 case XK_KP_7:
745 id = WXK_NUMPAD7; break;
746 case XK_KP_8:
747 id = WXK_NUMPAD8; break;
748 case XK_KP_9:
749 id = WXK_NUMPAD9; break;
750 case XK_F1:
751 id = WXK_F1; break;
752 case XK_F2:
753 id = WXK_F2; break;
754 case XK_F3:
755 id = WXK_F3; break;
756 case XK_F4:
757 id = WXK_F4; break;
758 case XK_F5:
759 id = WXK_F5; break;
760 case XK_F6:
761 id = WXK_F6; break;
762 case XK_F7:
763 id = WXK_F7; break;
764 case XK_F8:
765 id = WXK_F8; break;
766 case XK_F9:
767 id = WXK_F9; break;
768 case XK_F10:
769 id = WXK_F10; break;
770 case XK_F11:
771 id = WXK_F11; break;
772 case XK_F12:
773 id = WXK_F12; break;
774 case XK_F13:
775 id = WXK_F13; break;
776 case XK_F14:
777 id = WXK_F14; break;
778 case XK_F15:
779 id = WXK_F15; break;
780 case XK_F16:
781 id = WXK_F16; break;
782 case XK_F17:
783 id = WXK_F17; break;
784 case XK_F18:
785 id = WXK_F18; break;
786 case XK_F19:
787 id = WXK_F19; break;
788 case XK_F20:
789 id = WXK_F20; break;
790 case XK_F21:
791 id = WXK_F21; break;
792 case XK_F22:
793 id = WXK_F22; break;
794 case XK_F23:
795 id = WXK_F23; break;
796 case XK_F24:
797 id = WXK_F24; break;
798 default:
799 id = (keySym <= 255) ? (int)keySym : -1;
800 }
801
802 return id;
803 }
804
805 KeySym wxCharCodeWXToX(int id)
806 {
807 KeySym keySym;
808
809 switch (id)
810 {
811 case WXK_CANCEL: keySym = XK_Cancel; break;
812 case WXK_BACK: keySym = XK_BackSpace; break;
813 case WXK_TAB: keySym = XK_Tab; break;
814 case WXK_CLEAR: keySym = XK_Clear; break;
815 case WXK_RETURN: keySym = XK_Return; break;
816 case WXK_SHIFT: keySym = XK_Shift_L; break;
817 case WXK_CONTROL: keySym = XK_Control_L; break;
818 case WXK_MENU : keySym = XK_Menu; break;
819 case WXK_PAUSE: keySym = XK_Pause; break;
820 case WXK_ESCAPE: keySym = XK_Escape; break;
821 case WXK_SPACE: keySym = ' '; break;
822 case WXK_PRIOR: keySym = XK_Prior; break;
823 case WXK_NEXT : keySym = XK_Next; break;
824 case WXK_END: keySym = XK_End; break;
825 case WXK_HOME : keySym = XK_Home; break;
826 case WXK_LEFT : keySym = XK_Left; break;
827 case WXK_UP: keySym = XK_Up; break;
828 case WXK_RIGHT: keySym = XK_Right; break;
829 case WXK_DOWN : keySym = XK_Down; break;
830 case WXK_SELECT: keySym = XK_Select; break;
831 case WXK_PRINT: keySym = XK_Print; break;
832 case WXK_EXECUTE: keySym = XK_Execute; break;
833 case WXK_INSERT: keySym = XK_Insert; break;
834 case WXK_DELETE: keySym = XK_Delete; break;
835 case WXK_HELP : keySym = XK_Help; break;
836 case WXK_NUMPAD0: keySym = XK_KP_0; break;
837 case WXK_NUMPAD1: keySym = XK_KP_1; break;
838 case WXK_NUMPAD2: keySym = XK_KP_2; break;
839 case WXK_NUMPAD3: keySym = XK_KP_3; break;
840 case WXK_NUMPAD4: keySym = XK_KP_4; break;
841 case WXK_NUMPAD5: keySym = XK_KP_5; break;
842 case WXK_NUMPAD6: keySym = XK_KP_6; break;
843 case WXK_NUMPAD7: keySym = XK_KP_7; break;
844 case WXK_NUMPAD8: keySym = XK_KP_8; break;
845 case WXK_NUMPAD9: keySym = XK_KP_9; break;
846 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
847 case WXK_ADD: keySym = XK_KP_Add; break;
848 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
849 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
850 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
851 case WXK_F1: keySym = XK_F1; break;
852 case WXK_F2: keySym = XK_F2; break;
853 case WXK_F3: keySym = XK_F3; break;
854 case WXK_F4: keySym = XK_F4; break;
855 case WXK_F5: keySym = XK_F5; break;
856 case WXK_F6: keySym = XK_F6; break;
857 case WXK_F7: keySym = XK_F7; break;
858 case WXK_F8: keySym = XK_F8; break;
859 case WXK_F9: keySym = XK_F9; break;
860 case WXK_F10: keySym = XK_F10; break;
861 case WXK_F11: keySym = XK_F11; break;
862 case WXK_F12: keySym = XK_F12; break;
863 case WXK_F13: keySym = XK_F13; break;
864 case WXK_F14: keySym = XK_F14; break;
865 case WXK_F15: keySym = XK_F15; break;
866 case WXK_F16: keySym = XK_F16; break;
867 case WXK_F17: keySym = XK_F17; break;
868 case WXK_F18: keySym = XK_F18; break;
869 case WXK_F19: keySym = XK_F19; break;
870 case WXK_F20: keySym = XK_F20; break;
871 case WXK_F21: keySym = XK_F21; break;
872 case WXK_F22: keySym = XK_F22; break;
873 case WXK_F23: keySym = XK_F23; break;
874 case WXK_F24: keySym = XK_F24; break;
875 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
876 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
877 default: keySym = id <= 255 ? (KeySym)id : 0;
878 }
879
880 return keySym;
881 }
882 #endif
883
884 // ----------------------------------------------------------------------------
885 // Some colour manipulation routines
886 // ----------------------------------------------------------------------------
887
888 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
889 {
890 int h = hsv->h;
891 int s = hsv->s;
892 int v = hsv->v;
893 int r = 0, g = 0, b = 0;
894 int i, f;
895 int p, q, t;
896 s = (s * wxMAX_RGB) / wxMAX_SV;
897 v = (v * wxMAX_RGB) / wxMAX_SV;
898 if (h == 360) h = 0;
899 if (s == 0) { h = 0; r = g = b = v; }
900 i = h / 60;
901 f = h % 60;
902 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
903 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
904 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
905 switch (i)
906 {
907 case 0: r = v, g = t, b = p; break;
908 case 1: r = q, g = v, b = p; break;
909 case 2: r = p, g = v, b = t; break;
910 case 3: r = p, g = q, b = v; break;
911 case 4: r = t, g = p, b = v; break;
912 case 5: r = v, g = p, b = q; break;
913 }
914 rgb->red = r << 8;
915 rgb->green = g << 8;
916 rgb->blue = b << 8;
917 }
918
919 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
920 {
921 int r = rgb->red >> 8;
922 int g = rgb->green >> 8;
923 int b = rgb->blue >> 8;
924 int maxv = wxMax3(r, g, b);
925 int minv = wxMin3(r, g, b);
926 int h = 0, s, v;
927 v = maxv;
928 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
929 else s = 0;
930 if (s == 0) h = 0;
931 else
932 {
933 int rc, gc, bc, hex = 0;
934 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
935 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
936 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
937 if (r == maxv) { h = bc - gc, hex = 0; }
938 else if (g == maxv) { h = rc - bc, hex = 2; }
939 else if (b == maxv) { h = gc - rc, hex = 4; }
940 h = hex * 60 + (h * 60 / wxMAX_RGB);
941 if (h < 0) h += 360;
942 }
943 hsv->h = h;
944 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
945 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
946 }
947
948 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
949 {
950 #if !wxUSE_NANOX
951 int llp;
952
953 int screen = DefaultScreen(d);
954 int num_colors = DisplayCells(d,screen);
955
956 XColor *color_defs = new XColor[num_colors];
957 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
958 XQueryColors(d,cmp,color_defs,num_colors);
959
960 wxHSV hsv_defs, hsv;
961 wxXColorToHSV(&hsv,xc);
962
963 int diff, min_diff = 0, pixel = 0;
964
965 for(llp = 0;llp < num_colors;llp++)
966 {
967 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
968 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
969 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
970 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
971 if (llp == 0) min_diff = diff;
972 if (min_diff > diff) { min_diff = diff; pixel = llp; }
973 if (min_diff == 0) break;
974 }
975
976 xc -> red = color_defs[pixel].red;
977 xc -> green = color_defs[pixel].green;
978 xc -> blue = color_defs[pixel].blue;
979 xc -> flags = DoRed | DoGreen | DoBlue;
980
981 /* FIXME, TODO
982 if (!XAllocColor(d,cmp,xc))
983 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
984 */
985
986 delete[] color_defs;
987 #endif
988 }
989
990 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
991 {
992 if (!XAllocColor(d,cmp,xc))
993 {
994 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
995 wxAllocNearestColor(d,cmp,xc);
996 }
997 }
998
999 #ifdef __WXDEBUG__
1000 wxString wxGetXEventName(XEvent& event)
1001 {
1002 #if wxUSE_NANOX
1003 wxString str(wxT("(some event)"));
1004 return str;
1005 #else
1006 int type = event.xany.type;
1007 static char* event_name[] = {
1008 "", "unknown(-)", // 0-1
1009 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1010 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1011 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1012 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1013 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1014 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1015 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1016 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1017 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1018 "ClientMessage", "MappingNotify", // 33-34
1019 "unknown(+)"}; // 35
1020 type = wxMin(35, type); type = wxMax(1, type);
1021 wxString str(event_name[type]);
1022 return str;
1023 #endif
1024 }
1025 #endif
1026
1027 // ----------------------------------------------------------------------------
1028 // accelerators
1029 // ----------------------------------------------------------------------------
1030
1031 // Find the letter corresponding to the mnemonic, for Motif
1032 char wxFindMnemonic (const char *s)
1033 {
1034 char mnem = 0;
1035 int len = strlen (s);
1036 int i;
1037
1038 for (i = 0; i < len; i++)
1039 {
1040 if (s[i] == '&')
1041 {
1042 // Carefully handle &&
1043 if ((i + 1) <= len && s[i + 1] == '&')
1044 i++;
1045 else
1046 {
1047 mnem = s[i + 1];
1048 break;
1049 }
1050 }
1051 }
1052 return mnem;
1053 }
1054
1055 char* wxFindAccelerator( const char *s )
1056 {
1057 #if 1
1058 // VZ: this function returns incorrect keysym which completely breaks kbd
1059 // handling
1060 return NULL;
1061 #else
1062 // The accelerator text is after the \t char.
1063 s = strchr( s, '\t' );
1064
1065 if( !s ) return NULL;
1066
1067 /*
1068 Now we need to format it as X standard:
1069
1070 input output
1071
1072 F7 --> <Key>F7
1073 Ctrl+N --> Ctrl<Key>N
1074 Alt+k --> Meta<Key>k
1075 Ctrl+Shift+A --> Ctrl Shift<Key>A
1076
1077 and handle Ctrl-N & similia
1078 */
1079
1080 static char buf[256];
1081
1082 buf[0] = '\0';
1083 wxString tmp = s + 1; // skip TAB
1084 size_t index = 0;
1085
1086 while( index < tmp.length() )
1087 {
1088 size_t plus = tmp.find( '+', index );
1089 size_t minus = tmp.find( '-', index );
1090
1091 // neither '+' nor '-', add <Key>
1092 if( plus == wxString::npos && minus == wxString::npos )
1093 {
1094 strcat( buf, "<Key>" );
1095 strcat( buf, tmp.c_str() + index );
1096
1097 return buf;
1098 }
1099
1100 // OK: npos is big and positive
1101 size_t sep = wxMin( plus, minus );
1102 wxString mod = tmp.substr( index, sep - index );
1103
1104 // Ctrl -> Ctrl
1105 // Shift -> Shift
1106 // Alt -> Meta
1107 if( mod == "Alt" )
1108 mod = "Meta";
1109
1110 if( buf[0] )
1111 strcat( buf, " " );
1112
1113 strcat( buf, mod.c_str() );
1114
1115 index = sep + 1;
1116 }
1117
1118 return NULL;
1119 #endif
1120 }
1121
1122 XmString wxFindAcceleratorText (const char *s)
1123 {
1124 #if 1
1125 // VZ: this function returns incorrect keysym which completely breaks kbd
1126 // handling
1127 return NULL;
1128 #else
1129 // The accelerator text is after the \t char.
1130 s = strchr( s, '\t' );
1131
1132 if( !s ) return NULL;
1133
1134 return wxStringToXmString( s + 1 ); // skip TAB!
1135 #endif
1136 }
1137
1138 // Change a widget's foreground and background colours.
1139 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
1140 {
1141 // When should we specify the foreground, if it's calculated
1142 // by wxComputeColours?
1143 // Solution: say we start with the default (computed) foreground colour.
1144 // If we call SetForegroundColour explicitly for a control or window,
1145 // then the foreground is changed.
1146 // Therefore SetBackgroundColour computes the foreground colour, and
1147 // SetForegroundColour changes the foreground colour. The ordering is
1148 // important.
1149
1150 XtVaSetValues ((Widget) widget,
1151 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
1152 NULL);
1153 }
1154
1155 void wxDoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour)
1156 {
1157 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
1158 (wxColour*) NULL);
1159
1160 XtVaSetValues ((Widget) widget,
1161 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
1162 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
1163 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
1164 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
1165 NULL);
1166
1167 if (changeArmColour)
1168 XtVaSetValues ((Widget) widget,
1169 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
1170 NULL);
1171 }
1172
1173 extern void wxDoChangeFont(WXWidget widget, wxFont& font)
1174 {
1175 // Lesstif 0.87 hangs here, but 0.93 does not
1176 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1177 Widget w = (Widget)widget;
1178 XtVaSetValues( w,
1179 wxFont::GetFontTag(), font.GetFontType( XtDisplay(w) ),
1180 NULL );
1181 #endif
1182
1183 }
1184
1185 wxString wxXmStringToString( const XmString& xmString )
1186 {
1187 char *txt;
1188 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
1189 {
1190 wxString str(txt);
1191 XtFree (txt);
1192 return str;
1193 }
1194
1195 return wxEmptyString;
1196 }
1197
1198 XmString wxStringToXmString( const wxString& str )
1199 {
1200 return XmStringCreateLtoR((char *)str.c_str(), XmSTRING_DEFAULT_CHARSET);
1201 }
1202
1203 XmString wxStringToXmString( const char* str )
1204 {
1205 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
1206 }
1207
1208 // ----------------------------------------------------------------------------
1209 // wxBitmap utility functions
1210 // ----------------------------------------------------------------------------
1211
1212 // Creates a bitmap with transparent areas drawn in
1213 // the given colour.
1214 wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, wxColour& colour)
1215 {
1216 wxBitmap newBitmap(bitmap.GetWidth(),
1217 bitmap.GetHeight(),
1218 bitmap.GetDepth());
1219 wxMemoryDC destDC;
1220 wxMemoryDC srcDC;
1221
1222 srcDC.SelectObject(bitmap);
1223 destDC.SelectObject(newBitmap);
1224
1225 wxBrush brush(colour, wxSOLID);
1226 // destDC.SetOptimization(FALSE);
1227 destDC.SetBackground(brush);
1228 destDC.Clear();
1229 destDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(),
1230 &srcDC, 0, 0, wxCOPY, TRUE);
1231
1232 return newBitmap;
1233 }
1234
1235 // ----------------------------------------------------------------------------
1236 // Miscellaneous functions
1237 // ----------------------------------------------------------------------------
1238
1239 WXWidget wxCreateBorderWidget( WXWidget parent, long style )
1240 {
1241 Widget borderWidget = (Widget)NULL, parentWidget = (Widget)parent;
1242
1243 if (style & wxSIMPLE_BORDER)
1244 {
1245 borderWidget = XtVaCreateManagedWidget
1246 (
1247 "simpleBorder",
1248 xmFrameWidgetClass, parentWidget,
1249 XmNshadowType, XmSHADOW_ETCHED_IN,
1250 XmNshadowThickness, 1,
1251 NULL
1252 );
1253 }
1254 else if (style & wxSUNKEN_BORDER)
1255 {
1256 borderWidget = XtVaCreateManagedWidget
1257 (
1258 "sunkenBorder",
1259 xmFrameWidgetClass, parentWidget,
1260 XmNshadowType, XmSHADOW_IN,
1261 NULL
1262 );
1263 }
1264 else if (style & wxRAISED_BORDER)
1265 {
1266 borderWidget = XtVaCreateManagedWidget
1267 (
1268 "raisedBorder",
1269 xmFrameWidgetClass, parentWidget,
1270 XmNshadowType, XmSHADOW_OUT,
1271 NULL
1272 );
1273 }
1274
1275 return borderWidget;
1276 }