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