]> git.saurik.com Git - wxWidgets.git/blame - src/motif/utils.cpp
more things are done by configure: checks for bool, whether overloading based
[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"
4bb6408c
JS
25
26#include <ctype.h>
4bb6408c 27#include <stdarg.h>
a4294b78
JS
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
5dcf05ae 39#if (defined(__SUNCC__) || defined(__CLCC__))
518b5d2f 40 #include <sysent.h>
5dcf05ae
JS
41#endif
42
4bb6408c
JS
43#include <Xm/Xm.h>
44
1363811b
VZ
45#include "wx/unix/execute.h"
46
50414e24
JS
47#include "wx/motif/private.h"
48
518b5d2f
VZ
49// ----------------------------------------------------------------------------
50// private functions
51// ----------------------------------------------------------------------------
52
a4294b78
JS
53// Yuck this is really BOTH site and platform dependent
54// so we should use some other strategy!
55#ifdef sun
518b5d2f 56 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
a4294b78 57#else
518b5d2f 58 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
a4294b78
JS
59#endif
60
61static char *GetIniFile (char *dest, const char *filename);
62
63extern wxList wxTopLevelWindows;
64
518b5d2f
VZ
65// ============================================================================
66// implementation
67// ============================================================================
4bb6408c 68
518b5d2f
VZ
69// ----------------------------------------------------------------------------
70// async event processing
71// ----------------------------------------------------------------------------
4bb6408c
JS
72
73// Consume all events until no more left
74void wxFlushEvents()
75{
2d120f83 76 Display *display = (Display*) wxGetDisplay();
518b5d2f 77
2d120f83
JS
78 XSync (display, FALSE);
79 XEvent event;
80 // XtAppPending returns availability of events AND timers/inputs, which
518b5d2f 81 // are processed via callbacks, so XtAppNextEvent will not return if
2d120f83
JS
82 // there are no events. So added '& XtIMXEvent' - Sergey.
83 while (XtAppPending ((XtAppContext) wxTheApp->GetAppContext()) & XtIMXEvent)
a4294b78 84 {
2d120f83
JS
85 XFlush (XtDisplay ((Widget) wxTheApp->GetTopLevelWidget()));
86 // Jan Lessner: works better when events are non-X events
87 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMXEvent);
a4294b78 88 }
4bb6408c
JS
89}
90
518b5d2f
VZ
91// Check whether this window wants to process messages, e.g. Stop button
92// in long calculations.
93bool wxCheckForInterrupt(wxWindow *wnd)
4bb6408c 94{
518b5d2f
VZ
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;
4bb6408c
JS
121}
122
518b5d2f
VZ
123// ----------------------------------------------------------------------------
124// wxExecute stuff
125// ----------------------------------------------------------------------------
126
1363811b 127static void xt_notify_end_process(XtPointer data, int *fid,
518b5d2f 128 XtInputId *id)
4bb6408c 129{
518b5d2f
VZ
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);
4bb6408c
JS
138}
139
518b5d2f 140int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
4bb6408c 141{
518b5d2f
VZ
142 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
143 fd,
144 (XtPointer *) XtInputReadMask,
145 (XtInputCallbackProc) xt_notify_end_process,
1363811b 146 (XtPointer) proc_data);
518b5d2f
VZ
147
148 return (int)id;
4bb6408c
JS
149}
150
518b5d2f
VZ
151// ----------------------------------------------------------------------------
152// misc
153// ----------------------------------------------------------------------------
154
4bb6408c
JS
155// Emit a beeeeeep
156void wxBell()
157{
2d120f83
JS
158 // Use current setting for the bell
159 XBell ((Display*) wxGetDisplay(), 0);
4bb6408c
JS
160}
161
162int wxGetOsVersion(int *majorVsn, int *minorVsn)
163{
518b5d2f 164 // FIXME TODO
2d120f83
JS
165 // This code is WRONG!! Does NOT return the
166 // Motif version of the libs but the X protocol
518b5d2f 167 // version!
2d120f83
JS
168 Display *display = XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
169 if (majorVsn)
170 *majorVsn = ProtocolVersion (display);
171 if (minorVsn)
172 *minorVsn = ProtocolRevision (display);
518b5d2f 173
2d120f83 174 return wxMOTIF_X;
4bb6408c
JS
175}
176
518b5d2f 177// ----------------------------------------------------------------------------
4bb6408c 178// Reading and writing resources (eg WIN.INI, .Xdefaults)
518b5d2f
VZ
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.
184static 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
47d67540 211#if wxUSE_RESOURCES
a4294b78
JS
212
213static char *GetResourcePath(char *buf, const char *name, bool create = FALSE)
214{
2d120f83
JS
215 if (create && wxFileExists (name) ) {
216 strcpy(buf, name);
217 return buf; // Exists so ...
218 }
518b5d2f 219
2d120f83
JS
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 }
518b5d2f 228
2d120f83
JS
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;
a4294b78
JS
235}
236
237/*
2d120f83
JS
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*/
a4294b78
JS
243
244wxList wxResourceCache (wxKEY_STRING);
245
518b5d2f 246void
a4294b78
JS
247wxFlushResources (void)
248{
2d120f83 249 char nameBuffer[512];
518b5d2f 250
2d120f83
JS
251 wxNode *node = wxResourceCache.First ();
252 while (node)
a4294b78 253 {
2d120f83
JS
254 const char *file = node->GetKeyString();
255 // If file doesn't exist, create it first.
256 (void)GetResourcePath(nameBuffer, file, TRUE);
518b5d2f 257
2d120f83
JS
258 XrmDatabase database = (XrmDatabase) node->Data ();
259 XrmPutFileDatabase (database, nameBuffer);
260 XrmDestroyDatabase (database);
261 wxNode *next = node->Next ();
262 delete node;
263 node = next;
a4294b78
JS
264 }
265}
266
267static XrmDatabase wxResourceDatabase = 0;
268
269void wxXMergeDatabases (wxApp * theApp, Display * display);
270
4bb6408c
JS
271bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
272{
2d120f83 273 char buffer[500];
518b5d2f 274
2d120f83 275 (void) GetIniFile (buffer, file);
518b5d2f 276
2d120f83
JS
277 XrmDatabase database;
278 wxNode *node = wxResourceCache.Find (buffer);
279 if (node)
280 database = (XrmDatabase) node->Data ();
281 else
a4294b78 282 {
2d120f83
JS
283 database = XrmGetFileDatabase (buffer);
284 wxResourceCache.Append (buffer, (wxObject *) database);
a4294b78 285 }
518b5d2f 286
2d120f83
JS
287 char resName[300];
288 strcpy (resName, (const char*) section);
289 strcat (resName, ".");
290 strcat (resName, (const char*) entry);
518b5d2f 291
2d120f83
JS
292 XrmPutStringResource (&database, resName, value);
293 return TRUE;
4bb6408c
JS
294}
295
296bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
297{
2d120f83
JS
298 char buf[50];
299 sprintf(buf, "%.4f", value);
300 return wxWriteResource(section, entry, buf, file);
4bb6408c
JS
301}
302
303bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
304{
2d120f83
JS
305 char buf[50];
306 sprintf(buf, "%ld", value);
307 return wxWriteResource(section, entry, buf, file);
4bb6408c
JS
308}
309
310bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
311{
2d120f83
JS
312 char buf[50];
313 sprintf(buf, "%d", value);
314 return wxWriteResource(section, entry, buf, file);
4bb6408c
JS
315}
316
317bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
318{
2d120f83 319 if (!wxResourceDatabase)
a4294b78 320 {
2d120f83
JS
321 Display *display = (Display*) wxGetDisplay();
322 wxXMergeDatabases (wxTheApp, display);
a4294b78 323 }
518b5d2f 324
2d120f83 325 XrmDatabase database;
518b5d2f 326
2d120f83 327 if (file != "")
a4294b78 328 {
2d120f83 329 char buffer[500];
518b5d2f 330
2d120f83
JS
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);
518b5d2f 334
2d120f83
JS
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 }
a4294b78 343 }
2d120f83
JS
344 else
345 database = wxResourceDatabase;
518b5d2f 346
2d120f83
JS
347 XrmValue xvalue;
348 char *str_type[20];
349 char buf[150];
350 strcpy (buf, section);
351 strcat (buf, ".");
352 strcat (buf, entry);
518b5d2f 353
2d120f83
JS
354 Bool success = XrmGetResource (database, buf, "*", str_type,
355 &xvalue);
356 // Try different combinations of upper/lower case, just in case...
357 if (!success)
a4294b78 358 {
2d120f83
JS
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;
518b5d2f 367
2d120f83
JS
368 *value = new char[xvalue.size + 1];
369 strncpy (*value, xvalue.addr, (int) xvalue.size);
370 return TRUE;
a4294b78 371 }
2d120f83 372 return FALSE;
4bb6408c
JS
373}
374
375bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
376{
2d120f83
JS
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;
4bb6408c
JS
386}
387
388bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
389{
2d120f83
JS
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;
4bb6408c
JS
399}
400
401bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
402{
2d120f83
JS
403 char *s = NULL;
404 bool succ = wxGetResource(section, entry, (char **)&s, file);
405 if (succ)
406 {
407 // Handle True, False here
518b5d2f 408 // True, Yes, Enables, Set or Activated
2d120f83
JS
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);
a4294b78
JS
417 delete[] s;
418 return TRUE;
2d120f83
JS
419 }
420 else
421 return FALSE;
a4294b78
JS
422}
423
424void wxXMergeDatabases (wxApp * theApp, Display * display)
425{
2d120f83
JS
426 XrmDatabase homeDB, serverDB, applicationDB;
427 char filenamebuf[1024];
518b5d2f 428
2d120f83
JS
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);
518b5d2f 435
2d120f83
JS
436 /* Get application defaults file, if any */
437 applicationDB = XrmGetFileDatabase (name);
438 (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase);
518b5d2f 439
2d120f83
JS
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 */
518b5d2f 445
2d120f83 446 if (XResourceManagerString (display) != NULL)
a4294b78 447 {
2d120f83 448 serverDB = XrmGetStringDatabase (XResourceManagerString (display));
a4294b78 449 }
2d120f83 450 else
a4294b78 451 {
2d120f83
JS
452 (void) GetIniFile (filename, NULL);
453 serverDB = XrmGetFileDatabase (filename);
a4294b78 454 }
2d120f83 455 XrmMergeDatabases (serverDB, &wxResourceDatabase);
518b5d2f 456
2d120f83
JS
457 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
458 * and merge into existing database
459 */
518b5d2f 460
2d120f83 461 if ((environment = getenv ("XENVIRONMENT")) == NULL)
a4294b78 462 {
2d120f83
JS
463 size_t len;
464 environment = GetIniFile (filename, NULL);
465 len = strlen (environment);
1363811b
VZ
466 wxString hostname;
467 if ( wxGetHostName(hostname) )
468 strncat(environment, hostname, 1024 - len);
a4294b78 469 }
2d120f83
JS
470 homeDB = XrmGetFileDatabase (environment);
471 XrmMergeDatabases (homeDB, &wxResourceDatabase);
a4294b78
JS
472}
473
474#if 0
475
476/*
2d120f83
JS
477* Not yet used but may be useful.
478*
479*/
518b5d2f 480void
a4294b78
JS
481wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name)
482{
2d120f83 483 int i;
518b5d2f
VZ
484 Display *dpy = XtDisplay (w); // Retrieve the display pointer
485
486 XrmDatabase rdb = NULL; // A resource data base
487
2d120f83
JS
488 // Create an empty resource database
489 rdb = XrmGetStringDatabase ("");
518b5d2f 490
2d120f83 491 // Add the Component resources, prepending the name of the component
518b5d2f 492
2d120f83
JS
493 i = 0;
494 while (resourceSpec[i] != NULL)
a4294b78 495 {
2d120f83 496 char buf[1000];
518b5d2f 497
2d120f83
JS
498 sprintf (buf, "*%s%s", name, resourceSpec[i++]);
499 XrmPutLineResource (&rdb, buf);
a4294b78 500 }
518b5d2f 501
2d120f83 502 // Merge them into the Xt database, with lowest precendence
518b5d2f 503
2d120f83 504 if (rdb)
a4294b78
JS
505 {
506#if (XlibSpecificationRelease>=5)
2d120f83
JS
507 XrmDatabase db = XtDatabase (dpy);
508 XrmCombineDatabase (rdb, &db, FALSE);
a4294b78 509#else
2d120f83
JS
510 XrmMergeDatabases (dpy->db, &rdb);
511 dpy->db = rdb;
a4294b78
JS
512#endif
513 }
4bb6408c 514}
a4294b78 515#endif
2d120f83 516// 0
a4294b78 517
47d67540 518#endif // wxUSE_RESOURCES
4bb6408c 519
518b5d2f
VZ
520// ----------------------------------------------------------------------------
521// busy cursor stuff
522// ----------------------------------------------------------------------------
523
4bb6408c
JS
524static int wxBusyCursorCount = 0;
525
a4294b78 526// Helper function
518b5d2f 527static void
a4294b78
JS
528wxXSetBusyCursor (wxWindow * win, wxCursor * cursor)
529{
2d120f83 530 Display *display = (Display*) win->GetXDisplay();
518b5d2f 531
2d120f83 532 Window xwin = (Window) win->GetXWindow();
ad813b00
JS
533 if (!xwin)
534 return;
535
2d120f83 536 XSetWindowAttributes attrs;
518b5d2f 537
2d120f83 538 if (cursor)
a4294b78 539 {
2d120f83 540 attrs.cursor = (Cursor) cursor->GetXCursor(display);
a4294b78 541 }
2d120f83 542 else
a4294b78 543 {
2d120f83
JS
544 // Restore old cursor
545 if (win->GetCursor()->Ok())
546 attrs.cursor = (Cursor) win->GetCursor()->GetXCursor(display);
547 else
548 attrs.cursor = None;
a4294b78 549 }
2d120f83
JS
550 if (xwin)
551 XChangeWindowAttributes (display, xwin, CWCursor, &attrs);
518b5d2f 552
2d120f83 553 XFlush (display);
518b5d2f 554
2d120f83
JS
555 for(wxNode *node = win->GetChildren().First (); node; node = node->Next())
556 {
a4294b78 557 wxWindow *child = (wxWindow *) node->Data ();
2d120f83
JS
558 wxXSetBusyCursor (child, cursor);
559 }
a4294b78
JS
560}
561
4bb6408c
JS
562// Set the cursor to the busy cursor for all windows
563void wxBeginBusyCursor(wxCursor *cursor)
564{
2d120f83
JS
565 wxBusyCursorCount++;
566 if (wxBusyCursorCount == 1)
a4294b78 567 {
2d120f83
JS
568 for(wxNode *node = wxTopLevelWindows.First (); node; node = node->Next())
569 {
570 wxWindow *win = (wxWindow *) node->Data ();
571 wxXSetBusyCursor (win, cursor);
572 }
a4294b78 573 }
4bb6408c
JS
574}
575
576// Restore cursor to normal
577void wxEndBusyCursor()
578{
2d120f83
JS
579 if (wxBusyCursorCount == 0)
580 return;
518b5d2f 581
2d120f83
JS
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 }
4bb6408c
JS
591}
592
593// TRUE if we're between the above two calls
594bool wxIsBusy()
595{
2d120f83 596 return (wxBusyCursorCount > 0);
4bb6408c
JS
597}
598
518b5d2f
VZ
599// ----------------------------------------------------------------------------
600// display info
601// ----------------------------------------------------------------------------
4bb6408c
JS
602
603void wxGetMousePosition( int* x, int* y )
604{
a4294b78
JS
605 XMotionEvent xev;
606 Window root, child;
607 XQueryPointer((Display*) wxGetDisplay(),
518b5d2f
VZ
608 DefaultRootWindow((Display*) wxGetDisplay()),
609 &root, &child,
610 &(xev.x_root), &(xev.y_root),
611 &(xev.x), &(xev.y),
612 &(xev.state));
a4294b78
JS
613 *x = xev.x_root;
614 *y = xev.y_root;
4bb6408c
JS
615};
616
617// Return TRUE if we have a colour display
618bool wxColourDisplay()
619{
518b5d2f 620 return wxDisplayDepth() > 1;
4bb6408c
JS
621}
622
623// Returns depth of screen
624int wxDisplayDepth()
625{
dfc54541 626 Display *dpy = (Display*) wxGetDisplay();
518b5d2f 627
dfc54541 628 return DefaultDepth (dpy, DefaultScreen (dpy));
4bb6408c
JS
629}
630
631// Get size of display
632void wxDisplaySize(int *width, int *height)
633{
2d120f83 634 Display *dpy = (Display*) wxGetDisplay();
518b5d2f
VZ
635
636 if ( width )
637 *width = DisplayWidth (dpy, DefaultScreen (dpy));
638 if ( height )
639 *height = DisplayHeight (dpy, DefaultScreen (dpy));
4bb6408c
JS
640}
641
518b5d2f 642// Configurable display in Motif
4bb6408c
JS
643static WXDisplay *gs_currentDisplay = NULL;
644static wxString gs_displayName;
645
646WXDisplay *wxGetDisplay()
647{
2d120f83
JS
648 if (gs_currentDisplay)
649 return gs_currentDisplay;
518b5d2f 650
2d120f83
JS
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;
4bb6408c
JS
657}
658
659bool wxSetDisplay(const wxString& display_name)
660{
2d120f83 661 gs_displayName = display_name;
518b5d2f
VZ
662
663 if ( !display_name )
2d120f83
JS
664 {
665 gs_currentDisplay = NULL;
518b5d2f 666
2d120f83
JS
667 return TRUE;
668 }
669 else
670 {
671 Cardinal argc = 0;
518b5d2f 672
2d120f83
JS
673 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
674 (const char*) display_name,
675 (const char*) wxTheApp->GetAppName(),
676 (const char*) wxTheApp->GetClassName(),
677 NULL,
518b5d2f
VZ
678#if XtSpecificationRelease < 5
679 0, &argc,
680#else
681 0, (int *)&argc,
682#endif
683 NULL);
684
2d120f83
JS
685 if (display)
686 {
687 gs_currentDisplay = (WXDisplay*) display;
688 return TRUE;
518b5d2f
VZ
689 }
690 else
2d120f83
JS
691 return FALSE;
692 }
518b5d2f 693
2d120f83 694 return FALSE;
4bb6408c
JS
695}
696
697wxString wxGetDisplayName()
698{
2d120f83 699 return gs_displayName;
4bb6408c 700}
50414e24 701
518b5d2f
VZ
702// ----------------------------------------------------------------------------
703// accelerators
704// ----------------------------------------------------------------------------
705
50414e24
JS
706// Find the letter corresponding to the mnemonic, for Motif
707char wxFindMnemonic (const char *s)
708{
2d120f83
JS
709 char mnem = 0;
710 int len = strlen (s);
711 int i;
712 for (i = 0; i < len; i++)
50414e24 713 {
2d120f83
JS
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 }
50414e24 725 }
2d120f83 726 return mnem;
50414e24
JS
727}
728
729char * wxFindAccelerator (char *s)
730{
2d120f83
JS
731 // The accelerator text is after the \t char.
732 while (*s && *s != '\t')
733 s++;
734 if (*s == '\0')
735 return (NULL);
50414e24 736 s++;
2d120f83
JS
737 /*
738 Now we need to format it as X standard:
518b5d2f 739
2d120f83 740 input output
518b5d2f 741
2d120f83
JS
742 F7 --> <Key>F7
743 Ctrl+N --> Ctrl<Key>N
744 Alt+k --> Meta<Key>k
745 Ctrl+Shift+A --> Ctrl Shift<Key>A
518b5d2f 746
2d120f83 747 */
518b5d2f 748
2d120f83
JS
749 wxBuffer[0] = '\0';
750 char *tmp = copystring (s);
751 s = tmp;
752 char *p = s;
518b5d2f 753
2d120f83 754 while (1)
50414e24 755 {
2d120f83
JS
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 }
50414e24 776 }
2d120f83
JS
777 delete[]tmp;
778 return wxBuffer;
50414e24
JS
779}
780
781XmString wxFindAcceleratorText (char *s)
782{
2d120f83
JS
783 // The accelerator text is after the \t char.
784 while (*s && *s != '\t')
785 s++;
786 if (*s == '\0')
787 return (NULL);
50414e24 788 s++;
2d120f83
JS
789 XmString text = XmStringCreateSimple (s);
790 return text;
50414e24
JS
791}
792
518b5d2f
VZ
793// ----------------------------------------------------------------------------
794// keycode translations
795// ----------------------------------------------------------------------------
796
50414e24
JS
797#include <X11/keysym.h>
798
518b5d2f
VZ
799// FIXME what about tables??
800
50414e24
JS
801int wxCharCodeXToWX(KeySym keySym)
802{
2d120f83 803 int id;
518b5d2f
VZ
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;
50414e24
JS
956}
957
958KeySym wxCharCodeWXToX(int id)
959{
2d120f83 960 KeySym keySym;
a4294b78 961
518b5d2f 962 switch (id)
2d120f83 963 {
518b5d2f
VZ
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;
2d120f83 1031 }
518b5d2f
VZ
1032
1033 return keySym;
a4294b78
JS
1034}
1035
518b5d2f
VZ
1036// ----------------------------------------------------------------------------
1037// Some colour manipulation routines
1038// ----------------------------------------------------------------------------
2d120f83 1039
a4294b78 1040void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
2d120f83
JS
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;
518b5d2f 1057 switch (i)
2d120f83
JS
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}
a4294b78
JS
1070
1071void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
2d120f83
JS
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;
518b5d2f 1083 else
2d120f83
JS
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);
518b5d2f
VZ
1089 if (r == maxv) { h = bc - gc, hex = 0; }
1090 else if (g == maxv) { h = rc - bc, hex = 2; }
2d120f83
JS
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}
a4294b78
JS
1099
1100void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
2d120f83
JS
1101{
1102 int llp;
518b5d2f 1103
2d120f83
JS
1104 int screen = DefaultScreen(d);
1105 int num_colors = DisplayCells(d,screen);
518b5d2f 1106
2d120f83
JS
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);
518b5d2f 1110
2d120f83
JS
1111 wxHSV hsv_defs, hsv;
1112 wxXColorToHSV(&hsv,xc);
518b5d2f 1113
2d120f83 1114 int diff, min_diff, pixel = 0;
518b5d2f 1115
2d120f83
JS
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 }
518b5d2f 1126
2d120f83
JS
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))
a4294b78 1132 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
518b5d2f 1133
2d120f83
JS
1134 delete[] color_defs;
1135}
a4294b78
JS
1136
1137void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
2d120f83
JS
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}
a4294b78
JS
1145
1146
94b49b93
JS
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
1152void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
1153{
2d120f83
JS
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.
518b5d2f 1162
2d120f83
JS
1163 XtVaSetValues ((Widget) widget,
1164 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
1165 NULL);
94b49b93
JS
1166}
1167
1168void wxDoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour)
1169{
2d120f83
JS
1170 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
1171 (wxColour*) NULL);
518b5d2f 1172
94b49b93 1173 XtVaSetValues ((Widget) widget,
2d120f83
JS
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);
518b5d2f 1179
2d120f83
JS
1180 if (changeArmColour)
1181 XtVaSetValues ((Widget) widget,
1182 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
1183 NULL);
94b49b93
JS
1184}
1185