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