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