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