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