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