]> git.saurik.com Git - wxWidgets.git/blame - src/motif/utils.cpp
WinCE build fixes.
[wxWidgets.git] / src / motif / utils.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
355b4d3d 2// Name: src/motif/utils.cpp
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
518b5d2f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
4bb6408c 19
1248b41f
MB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
bcd055ae
JJ
23#ifdef __VMS
24#define XtDisplay XTDISPLAY
2b5f62a0 25#endif
1248b41f 26
2b5f62a0
VZ
27#include "wx/setup.h"
28#include "wx/utils.h"
d48568a5 29#include "wx/apptrait.h"
2b5f62a0 30#include "wx/app.h"
1eb1eae7
MB
31#include "wx/dcmemory.h"
32#include "wx/bitmap.h"
eb6fa4b4 33#include "wx/evtloop.h"
2b5f62a0 34
2b5f62a0 35#include <string.h>
2b5f62a0
VZ
36
37#if (defined(__SUNCC__) || defined(__CLCC__))
38 #include <sysent.h>
39#endif
40
41#ifdef __VMS__
42#pragma message disable nosimpint
43#endif
44
45#include "wx/unix/execute.h"
46
2b5f62a0 47#include <Xm/Xm.h>
6769d0cb
MB
48#include <Xm/Frame.h>
49
2b5f62a0 50#include "wx/motif/private.h"
2b5f62a0
VZ
51
52#if wxUSE_RESOURCES
53#include "X11/Xresource.h"
54#endif
55
56#include "X11/Xutil.h"
57
58#ifdef __VMS__
59#pragma message enable nosimpint
60#endif
61
62// ----------------------------------------------------------------------------
63// private functions
64// ----------------------------------------------------------------------------
65
66// Yuck this is really BOTH site and platform dependent
67// so we should use some other strategy!
68#ifdef sun
69 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
4b37e99a 70#else
2b5f62a0
VZ
71 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
72#endif
73
fd304d98 74#if wxUSE_RESOURCES
2b5f62a0 75static char *GetIniFile (char *dest, const char *filename);
fd304d98 76#endif
2b5f62a0
VZ
77
78// ============================================================================
79// implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// async event processing
84// ----------------------------------------------------------------------------
85
86// Consume all events until no more left
eb6fa4b4 87void wxFlushEvents(WXDisplay* wxdisplay)
2b5f62a0 88{
eb6fa4b4
MB
89 Display *display = (Display*)wxdisplay;
90 wxEventLoop evtLoop;
2b5f62a0 91
96be256b 92 XSync (display, False);
2b5f62a0 93
eb6fa4b4 94 while (evtLoop.Pending())
2b5f62a0 95 {
eb6fa4b4
MB
96 XFlush (display);
97 evtLoop.Dispatch();
2b5f62a0 98 }
2b5f62a0
VZ
99}
100
2b5f62a0
VZ
101// ----------------------------------------------------------------------------
102// wxExecute stuff
103// ----------------------------------------------------------------------------
b229c8a9 104
2b5f62a0
VZ
105static void xt_notify_end_process(XtPointer data, int *WXUNUSED(fid),
106 XtInputId *id)
107{
108 wxEndProcessData *proc_data = (wxEndProcessData *)data;
109
110 wxHandleProcessTermination(proc_data);
111
112 // VZ: I think they should be the same...
113 wxASSERT( (int)*id == proc_data->tag );
114
115 XtRemoveInput(*id);
116}
2b5f62a0
VZ
117
118int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
119{
2b5f62a0
VZ
120 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
121 fd,
122 (XtPointer *) XtInputReadMask,
123 (XtInputCallbackProc) xt_notify_end_process,
124 (XtPointer) proc_data);
bcd055ae 125
2b5f62a0 126 return (int)id;
2b5f62a0
VZ
127}
128
129// ----------------------------------------------------------------------------
130// misc
131// ----------------------------------------------------------------------------
132
133// Emit a beeeeeep
189d1ae7
SN
134#ifndef __EMX__
135// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
2b5f62a0
VZ
136void wxBell()
137{
138 // Use current setting for the bell
eb6fa4b4 139 XBell (wxGlobalDisplay(), 0);
2b5f62a0 140}
189d1ae7 141#endif
2b5f62a0 142
324899f6 143wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
2b5f62a0 144{
f9788a11
MB
145 static wxToolkitInfo info;
146
a8eaaeb2
VS
147 info.shortName = _T("motif");
148 info.name = _T("wxMotif");
149#ifdef __WXUNIVERSAL__
150 info.shortName << _T("univ");
151 info.name << _T("/wxUniversal");
152#endif
2b5f62a0
VZ
153 // FIXME TODO
154 // This code is WRONG!! Does NOT return the
155 // Motif version of the libs but the X protocol
156 // version!
eb6fa4b4 157 Display *display = wxGlobalDisplay();
8da782ef
MB
158 if (display)
159 {
160 info.versionMajor = ProtocolVersion (display);
161 info.versionMinor = ProtocolRevision (display);
162 }
a8eaaeb2 163 info.os = wxMOTIF_X;
324899f6 164 return info;
2b5f62a0
VZ
165}
166
167// ----------------------------------------------------------------------------
168// Reading and writing resources (eg WIN.INI, .Xdefaults)
169// ----------------------------------------------------------------------------
170
fd304d98
MB
171#if wxUSE_RESOURCES
172
2b5f62a0
VZ
173// Read $HOME for what it says is home, if not
174// read $USER or $LOGNAME for user name else determine
175// the Real User, then determine the Real home dir.
176static char * GetIniFile (char *dest, const char *filename)
177{
178 char *home = NULL;
179 if (filename && wxIsAbsolutePath(filename))
180 {
181 strcpy(dest, filename);
182 }
355b4d3d 183 else if ((home = wxGetUserHome()) != NULL)
2b5f62a0
VZ
184 {
185 strcpy(dest, home);
186 if (dest[strlen(dest) - 1] != '/')
187 strcat (dest, "/");
188 if (filename == NULL)
189 {
190 if ((filename = getenv ("XENVIRONMENT")) == NULL)
191 filename = ".Xdefaults";
192 }
193 else if (*filename != '.')
194 strcat (dest, ".");
195 strcat (dest, filename);
196 } else
197 {
198 dest[0] = '\0';
199 }
200 return dest;
201}
202
96be256b 203static char *GetResourcePath(char *buf, const char *name, bool create = false)
2b5f62a0
VZ
204{
205 if (create && wxFileExists (name) ) {
206 strcpy(buf, name);
207 return buf; // Exists so ...
208 }
209
210 if (*name == '/')
211 strcpy(buf, name);
212 else {
213 // Put in standard place for resource files if not absolute
214 strcpy (buf, DEFAULT_XRESOURCE_DIR);
215 strcat (buf, "/");
3e2d47e1 216 strcat (buf, wxFileNameFromPath (name).c_str());
2b5f62a0
VZ
217 }
218
219 if (create) {
220 // Touch the file to create it
221 FILE *fd = fopen (buf, "w");
222 if (fd) fclose (fd);
223 }
224 return buf;
225}
226
227/*
228* We have a cache for writing different resource files,
229* which will only get flushed when we call wxFlushResources().
230* Build up a list of resource databases waiting to be written.
231*
232*/
233
234wxList wxResourceCache (wxKEY_STRING);
235
236void
237wxFlushResources (void)
238{
239 char nameBuffer[512];
240
241 wxNode *node = wxResourceCache.First ();
242 while (node)
243 {
244 const char *file = node->GetKeyString();
245 // If file doesn't exist, create it first.
96be256b 246 (void)GetResourcePath(nameBuffer, file, true);
2b5f62a0
VZ
247
248 XrmDatabase database = (XrmDatabase) node->Data ();
249 XrmPutFileDatabase (database, nameBuffer);
250 XrmDestroyDatabase (database);
251 wxNode *next = node->Next ();
252 delete node;
253 node = next;
254 }
255}
256
257static XrmDatabase wxResourceDatabase = 0;
258
259void wxXMergeDatabases (wxApp * theApp, Display * display);
260
261bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
262{
263 char buffer[500];
264
265 (void) GetIniFile (buffer, file);
266
267 XrmDatabase database;
268 wxNode *node = wxResourceCache.Find (buffer);
269 if (node)
270 database = (XrmDatabase) node->Data ();
271 else
272 {
273 database = XrmGetFileDatabase (buffer);
274 wxResourceCache.Append (buffer, (wxObject *) database);
275 }
276
277 char resName[300];
3e2d47e1 278 strcpy (resName, section.c_str());
2b5f62a0 279 strcat (resName, ".");
3e2d47e1 280 strcat (resName, entry.c_str());
2b5f62a0
VZ
281
282 XrmPutStringResource (&database, resName, value);
ea76a6a5 283 return true;
2b5f62a0
VZ
284}
285
286bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
287{
288 char buf[50];
289 sprintf(buf, "%.4f", value);
290 return wxWriteResource(section, entry, buf, file);
291}
292
293bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
294{
295 char buf[50];
296 sprintf(buf, "%ld", value);
297 return wxWriteResource(section, entry, buf, file);
298}
299
300bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
301{
302 char buf[50];
303 sprintf(buf, "%d", value);
304 return wxWriteResource(section, entry, buf, file);
305}
306
307bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
308{
309 if (!wxResourceDatabase)
310 {
eb6fa4b4 311 Display *display = wxGlobalDisplay();
2b5f62a0
VZ
312 wxXMergeDatabases (wxTheApp, display);
313 }
314
315 XrmDatabase database;
316
355b4d3d 317 if (!file.empty())
2b5f62a0
VZ
318 {
319 char buffer[500];
320
321 // Is this right? Trying to get it to look in the user's
322 // home directory instead of current directory -- JACS
323 (void) GetIniFile (buffer, file);
324
325 wxNode *node = wxResourceCache.Find (buffer);
326 if (node)
327 database = (XrmDatabase) node->Data ();
328 else
329 {
330 database = XrmGetFileDatabase (buffer);
331 wxResourceCache.Append (buffer, (wxObject *) database);
332 }
333 }
334 else
335 database = wxResourceDatabase;
336
337 XrmValue xvalue;
338 char *str_type[20];
339 char buf[150];
340 strcpy (buf, section);
341 strcat (buf, ".");
342 strcat (buf, entry);
343
344 Bool success = XrmGetResource (database, buf, "*", str_type,
345 &xvalue);
346 // Try different combinations of upper/lower case, just in case...
347 if (!success)
348 {
349 buf[0] = (isupper (buf[0]) ? tolower (buf[0]) : toupper (buf[0]));
350 success = XrmGetResource (database, buf, "*", str_type,
351 &xvalue);
352 }
353 if (success)
354 {
355 if (*value)
356 delete[] *value;
357
358 *value = new char[xvalue.size + 1];
359 strncpy (*value, xvalue.addr, (int) xvalue.size);
ea76a6a5 360 return true;
2b5f62a0 361 }
96be256b 362 return false;
2b5f62a0
VZ
363}
364
365bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
366{
367 char *s = NULL;
368 bool succ = wxGetResource(section, entry, (char **)&s, file);
369 if (succ)
370 {
371 *value = (float)strtod(s, NULL);
372 delete[] s;
ea76a6a5 373 return true;
2b5f62a0 374 }
96be256b 375 else return false;
2b5f62a0
VZ
376}
377
378bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
379{
380 char *s = NULL;
381 bool succ = wxGetResource(section, entry, (char **)&s, file);
382 if (succ)
383 {
384 *value = strtol(s, NULL, 10);
385 delete[] s;
ea76a6a5 386 return true;
2b5f62a0 387 }
96be256b 388 else return false;
2b5f62a0
VZ
389}
390
391bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
392{
393 char *s = NULL;
394 bool succ = wxGetResource(section, entry, (char **)&s, file);
395 if (succ)
396 {
397 // Handle True, False here
398 // True, Yes, Enables, Set or Activated
399 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
96be256b 400 *value = true;
2b5f62a0
VZ
401 // False, No, Disabled, Reset, Cleared, Deactivated
402 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
96be256b 403 *value = false;
2b5f62a0
VZ
404 // Handle as Integer
405 else
406 *value = (int) strtol (s, NULL, 10);
407 delete[] s;
ea76a6a5 408 return true;
2b5f62a0
VZ
409 }
410 else
96be256b 411 return false;
2b5f62a0
VZ
412}
413
414void wxXMergeDatabases (wxApp * theApp, Display * display)
415{
416 XrmDatabase homeDB, serverDB, applicationDB;
417 char filenamebuf[1024];
418
419 char *filename = &filenamebuf[0];
420 char *environment;
421 wxString classname = theApp->GetClassName();
422 char name[256];
423 (void) strcpy (name, "/usr/lib/X11/app-defaults/");
3e2d47e1 424 (void) strcat (name, classname.c_str());
2b5f62a0
VZ
425
426 /* Get application defaults file, if any */
427 applicationDB = XrmGetFileDatabase (name);
428 (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase);
429
430 /* Merge server defaults, created by xrdb, loaded as a property of the root
431 * window when the server initializes and loaded into the display
432 * structure on XOpenDisplay;
433 * if not defined, use .Xdefaults
434 */
435
436 if (XResourceManagerString (display) != NULL)
437 {
438 serverDB = XrmGetStringDatabase (XResourceManagerString (display));
439 }
440 else
441 {
442 (void) GetIniFile (filename, NULL);
443 serverDB = XrmGetFileDatabase (filename);
444 }
445 XrmMergeDatabases (serverDB, &wxResourceDatabase);
446
447 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
448 * and merge into existing database
449 */
450
451 if ((environment = getenv ("XENVIRONMENT")) == NULL)
452 {
453 size_t len;
454 environment = GetIniFile (filename, NULL);
455 len = strlen (environment);
456 wxString hostname = wxGetHostName();
355b4d3d 457 if ( !hostname.empty() )
2b5f62a0
VZ
458 strncat(environment, hostname, 1024 - len);
459 }
460 homeDB = XrmGetFileDatabase (environment);
461 XrmMergeDatabases (homeDB, &wxResourceDatabase);
462}
463
464#if 0
465
466/*
467* Not yet used but may be useful.
468*
469*/
470void
471wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name)
472{
473 int i;
474 Display *dpy = XtDisplay (w); // Retrieve the display pointer
475
476 XrmDatabase rdb = NULL; // A resource data base
477
478 // Create an empty resource database
479 rdb = XrmGetStringDatabase ("");
480
481 // Add the Component resources, prepending the name of the component
482
483 i = 0;
484 while (resourceSpec[i] != NULL)
485 {
486 char buf[1000];
487
488 sprintf (buf, "*%s%s", name, resourceSpec[i++]);
489 XrmPutLineResource (&rdb, buf);
490 }
491
492 // Merge them into the Xt database, with lowest precendence
493
494 if (rdb)
495 {
496#if (XlibSpecificationRelease>=5)
497 XrmDatabase db = XtDatabase (dpy);
96be256b 498 XrmCombineDatabase (rdb, &db, False);
2b5f62a0
VZ
499#else
500 XrmMergeDatabases (dpy->db, &rdb);
501 dpy->db = rdb;
502#endif
503 }
504}
505#endif
506// 0
507
508#endif // wxUSE_RESOURCES
509
510// ----------------------------------------------------------------------------
511// display info
512// ----------------------------------------------------------------------------
513
514void wxGetMousePosition( int* x, int* y )
515{
516#if wxUSE_NANOX
517 // TODO
518 *x = 0;
519 *y = 0;
520#else
521 XMotionEvent xev;
522 Window root, child;
eb6fa4b4
MB
523 XQueryPointer(wxGlobalDisplay(),
524 DefaultRootWindow(wxGlobalDisplay()),
2b5f62a0
VZ
525 &root, &child,
526 &(xev.x_root), &(xev.y_root),
527 &(xev.x), &(xev.y),
528 &(xev.state));
529 *x = xev.x_root;
530 *y = xev.y_root;
531#endif
e933b5bc 532}
2b5f62a0 533
ea76a6a5 534// Return true if we have a colour display
2b5f62a0
VZ
535bool wxColourDisplay()
536{
537 return wxDisplayDepth() > 1;
538}
539
540// Returns depth of screen
541int wxDisplayDepth()
542{
eb6fa4b4 543 Display *dpy = wxGlobalDisplay();
2b5f62a0
VZ
544
545 return DefaultDepth (dpy, DefaultScreen (dpy));
546}
547
548// Get size of display
549void wxDisplaySize(int *width, int *height)
550{
eb6fa4b4 551 Display *dpy = wxGlobalDisplay();
2b5f62a0
VZ
552
553 if ( width )
554 *width = DisplayWidth (dpy, DefaultScreen (dpy));
555 if ( height )
556 *height = DisplayHeight (dpy, DefaultScreen (dpy));
557}
558
559void wxDisplaySizeMM(int *width, int *height)
560{
eb6fa4b4 561 Display *dpy = wxGlobalDisplay();
2b5f62a0
VZ
562
563 if ( width )
564 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
565 if ( height )
566 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
567}
568
569void wxClientDisplayRect(int *x, int *y, int *width, int *height)
570{
571 // This is supposed to return desktop dimensions minus any window
572 // manager panels, menus, taskbars, etc. If there is a way to do that
573 // for this platform please fix this function, otherwise it defaults
574 // to the entire desktop.
575 if (x) *x = 0;
576 if (y) *y = 0;
577 wxDisplaySize(width, height);
578}
579
580
581// Configurable display in wxX11 and wxMotif
582static WXDisplay *gs_currentDisplay = NULL;
583static wxString gs_displayName;
584
585WXDisplay *wxGetDisplay()
586{
587 if (gs_currentDisplay)
588 return gs_currentDisplay;
2b5f62a0
VZ
589 else if (wxTheApp)
590 return wxTheApp->GetInitialDisplay();
591 return NULL;
2b5f62a0
VZ
592}
593
594bool wxSetDisplay(const wxString& display_name)
595{
596 gs_displayName = display_name;
597
ea76a6a5 598 if ( display_name.empty() )
2b5f62a0
VZ
599 {
600 gs_currentDisplay = NULL;
601
ea76a6a5 602 return true;
2b5f62a0
VZ
603 }
604 else
605 {
2b5f62a0
VZ
606 Cardinal argc = 0;
607
608 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
3e2d47e1
MB
609 display_name.c_str(),
610 wxTheApp->GetAppName().c_str(),
611 wxTheApp->GetClassName().c_str(),
2b5f62a0
VZ
612 NULL,
613#if XtSpecificationRelease < 5
614 0, &argc,
615#else
616 0, (int *)&argc,
617#endif
618 NULL);
619
620 if (display)
621 {
622 gs_currentDisplay = (WXDisplay*) display;
ea76a6a5 623 return true;
2b5f62a0
VZ
624 }
625 else
96be256b 626 return false;
2b5f62a0
VZ
627 }
628}
629
630wxString wxGetDisplayName()
631{
632 return gs_displayName;
633}
634
635wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
636{
637 return wxGenericFindWindowAtPoint(pt);
638}
639
2b5f62a0
VZ
640// ----------------------------------------------------------------------------
641// Some colour manipulation routines
642// ----------------------------------------------------------------------------
643
644void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
645{
646 int h = hsv->h;
647 int s = hsv->s;
648 int v = hsv->v;
649 int r = 0, g = 0, b = 0;
650 int i, f;
651 int p, q, t;
652 s = (s * wxMAX_RGB) / wxMAX_SV;
653 v = (v * wxMAX_RGB) / wxMAX_SV;
654 if (h == 360) h = 0;
655 if (s == 0) { h = 0; r = g = b = v; }
656 i = h / 60;
657 f = h % 60;
658 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
659 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
660 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
661 switch (i)
662 {
663 case 0: r = v, g = t, b = p; break;
664 case 1: r = q, g = v, b = p; break;
665 case 2: r = p, g = v, b = t; break;
666 case 3: r = p, g = q, b = v; break;
667 case 4: r = t, g = p, b = v; break;
668 case 5: r = v, g = p, b = q; break;
669 }
355b4d3d
WS
670 rgb->red = (unsigned short)(r << 8);
671 rgb->green = (unsigned short)(g << 8);
672 rgb->blue = (unsigned short)(b << 8);
2b5f62a0
VZ
673}
674
675void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
676{
677 int r = rgb->red >> 8;
678 int g = rgb->green >> 8;
679 int b = rgb->blue >> 8;
680 int maxv = wxMax3(r, g, b);
681 int minv = wxMin3(r, g, b);
682 int h = 0, s, v;
683 v = maxv;
684 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
685 else s = 0;
686 if (s == 0) h = 0;
687 else
688 {
689 int rc, gc, bc, hex = 0;
690 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
691 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
692 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
693 if (r == maxv) { h = bc - gc, hex = 0; }
694 else if (g == maxv) { h = rc - bc, hex = 2; }
695 else if (b == maxv) { h = gc - rc, hex = 4; }
696 h = hex * 60 + (h * 60 / wxMAX_RGB);
697 if (h < 0) h += 360;
698 }
699 hsv->h = h;
700 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
701 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
702}
703
704void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
705{
706#if !wxUSE_NANOX
707 int llp;
708
709 int screen = DefaultScreen(d);
710 int num_colors = DisplayCells(d,screen);
711
712 XColor *color_defs = new XColor[num_colors];
713 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
714 XQueryColors(d,cmp,color_defs,num_colors);
715
716 wxHSV hsv_defs, hsv;
717 wxXColorToHSV(&hsv,xc);
718
719 int diff, min_diff = 0, pixel = 0;
720
721 for(llp = 0;llp < num_colors;llp++)
722 {
723 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
724 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
725 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
726 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
727 if (llp == 0) min_diff = diff;
728 if (min_diff > diff) { min_diff = diff; pixel = llp; }
729 if (min_diff == 0) break;
730 }
731
732 xc -> red = color_defs[pixel].red;
733 xc -> green = color_defs[pixel].green;
734 xc -> blue = color_defs[pixel].blue;
735 xc -> flags = DoRed | DoGreen | DoBlue;
736
737/* FIXME, TODO
738 if (!XAllocColor(d,cmp,xc))
739 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
740*/
741
742 delete[] color_defs;
743#endif
744}
745
746void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
747{
748 if (!XAllocColor(d,cmp,xc))
749 {
750 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
751 wxAllocNearestColor(d,cmp,xc);
752 }
753}
754
755#ifdef __WXDEBUG__
756wxString wxGetXEventName(XEvent& event)
757{
758#if wxUSE_NANOX
759 wxString str(wxT("(some event)"));
760 return str;
761#else
762 int type = event.xany.type;
ea76a6a5 763 static char* event_name[] = {
f1db433a
VZ
764 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
765 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
766 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
767 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
768 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
769 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
770 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
771 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
772 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
773 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
774 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
775 wxMOTIF_STR("unknown(+)")}; // 35
ea76a6a5
WS
776 type = wxMin(35, type); type = wxMax(1, type);
777 wxString str(event_name[type]);
778 return str;
2b5f62a0
VZ
779#endif
780}
781#endif
782
2b5f62a0
VZ
783// ----------------------------------------------------------------------------
784// accelerators
785// ----------------------------------------------------------------------------
786
787// Find the letter corresponding to the mnemonic, for Motif
788char wxFindMnemonic (const char *s)
789{
790 char mnem = 0;
791 int len = strlen (s);
792 int i;
eb6fa4b4 793
2b5f62a0
VZ
794 for (i = 0; i < len; i++)
795 {
796 if (s[i] == '&')
797 {
798 // Carefully handle &&
799 if ((i + 1) <= len && s[i + 1] == '&')
800 i++;
801 else
802 {
803 mnem = s[i + 1];
804 break;
805 }
806 }
807 }
808 return mnem;
809}
810
eb6fa4b4 811char* wxFindAccelerator( const char *s )
2b5f62a0 812{
eb6fa4b4 813#if 1
355b4d3d 814 wxUnusedVar(s);
2b5f62a0
VZ
815 // VZ: this function returns incorrect keysym which completely breaks kbd
816 // handling
817 return NULL;
eb6fa4b4
MB
818#else
819 // The accelerator text is after the \t char.
820 s = strchr( s, '\t' );
821
822 if( !s ) return NULL;
2b5f62a0 823
2b5f62a0
VZ
824 /*
825 Now we need to format it as X standard:
826
827 input output
828
829 F7 --> <Key>F7
830 Ctrl+N --> Ctrl<Key>N
831 Alt+k --> Meta<Key>k
832 Ctrl+Shift+A --> Ctrl Shift<Key>A
833
eb6fa4b4 834 and handle Ctrl-N & similia
2b5f62a0
VZ
835 */
836
837 static char buf[256];
eb6fa4b4 838
2b5f62a0 839 buf[0] = '\0';
eb6fa4b4
MB
840 wxString tmp = s + 1; // skip TAB
841 size_t index = 0;
2b5f62a0 842
eb6fa4b4 843 while( index < tmp.length() )
2b5f62a0 844 {
eb6fa4b4
MB
845 size_t plus = tmp.find( '+', index );
846 size_t minus = tmp.find( '-', index );
847
848 // neither '+' nor '-', add <Key>
849 if( plus == wxString::npos && minus == wxString::npos )
2b5f62a0 850 {
eb6fa4b4
MB
851 strcat( buf, "<Key>" );
852 strcat( buf, tmp.c_str() + index );
853
854 return buf;
2b5f62a0 855 }
eb6fa4b4
MB
856
857 // OK: npos is big and positive
858 size_t sep = wxMin( plus, minus );
859 wxString mod = tmp.substr( index, sep - index );
860
861 // Ctrl -> Ctrl
862 // Shift -> Shift
863 // Alt -> Meta
864 if( mod == "Alt" )
865 mod = "Meta";
866
867 if( buf[0] )
868 strcat( buf, " " );
869
870 strcat( buf, mod.c_str() );
871
872 index = sep + 1;
2b5f62a0 873 }
eb6fa4b4
MB
874
875 return NULL;
2b5f62a0
VZ
876#endif
877}
878
879XmString wxFindAcceleratorText (const char *s)
880{
eb6fa4b4 881#if 1
355b4d3d 882 wxUnusedVar(s);
2b5f62a0
VZ
883 // VZ: this function returns incorrect keysym which completely breaks kbd
884 // handling
885 return NULL;
eb6fa4b4
MB
886#else
887 // The accelerator text is after the \t char.
888 s = strchr( s, '\t' );
2b5f62a0 889
eb6fa4b4
MB
890 if( !s ) return NULL;
891
892 return wxStringToXmString( s + 1 ); // skip TAB!
2b5f62a0
VZ
893#endif
894}
895
2b5f62a0 896// Change a widget's foreground and background colours.
2b5f62a0
VZ
897void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
898{
899 // When should we specify the foreground, if it's calculated
900 // by wxComputeColours?
901 // Solution: say we start with the default (computed) foreground colour.
902 // If we call SetForegroundColour explicitly for a control or window,
903 // then the foreground is changed.
904 // Therefore SetBackgroundColour computes the foreground colour, and
905 // SetForegroundColour changes the foreground colour. The ordering is
906 // important.
907
908 XtVaSetValues ((Widget) widget,
909 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
910 NULL);
911}
912
913void wxDoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour)
914{
915 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
916 (wxColour*) NULL);
917
918 XtVaSetValues ((Widget) widget,
919 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
920 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
921 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
922 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
923 NULL);
924
925 if (changeArmColour)
926 XtVaSetValues ((Widget) widget,
927 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
928 NULL);
929}
930
fbfb8bcc 931extern void wxDoChangeFont(WXWidget widget, const wxFont& font)
e1aae528 932{
101b4778
MB
933 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
934#if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
e1aae528 935 Widget w = (Widget)widget;
e1aae528 936 XtVaSetValues( w,
73608949 937 wxFont::GetFontTag(), font.GetFontTypeC( XtDisplay(w) ),
e1aae528 938 NULL );
355b4d3d
WS
939#else
940 wxUnusedVar(widget);
941 wxUnusedVar(font);
e1aae528
MB
942#endif
943
944}
945
e1aae528
MB
946wxString wxXmStringToString( const XmString& xmString )
947{
948 char *txt;
949 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
950 {
951 wxString str(txt);
952 XtFree (txt);
953 return str;
954 }
955
956 return wxEmptyString;
957}
958
3e2d47e1
MB
959XmString wxStringToXmString( const wxString& str )
960{
961 return XmStringCreateLtoR((char *)str.c_str(), XmSTRING_DEFAULT_CHARSET);
962}
963
964XmString wxStringToXmString( const char* str )
965{
966 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
967}
aae0472b
MB
968
969// ----------------------------------------------------------------------------
970// wxBitmap utility functions
971// ----------------------------------------------------------------------------
972
973// Creates a bitmap with transparent areas drawn in
974// the given colour.
fbfb8bcc 975wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, const wxColour& colour)
aae0472b
MB
976{
977 wxBitmap newBitmap(bitmap.GetWidth(),
978 bitmap.GetHeight(),
979 bitmap.GetDepth());
980 wxMemoryDC destDC;
981 wxMemoryDC srcDC;
982
983 srcDC.SelectObject(bitmap);
984 destDC.SelectObject(newBitmap);
985
986 wxBrush brush(colour, wxSOLID);
aae0472b
MB
987 destDC.SetBackground(brush);
988 destDC.Clear();
989 destDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(),
ea76a6a5 990 &srcDC, 0, 0, wxCOPY, true);
aae0472b
MB
991
992 return newBitmap;
993}
6769d0cb
MB
994
995// ----------------------------------------------------------------------------
996// Miscellaneous functions
997// ----------------------------------------------------------------------------
998
999WXWidget wxCreateBorderWidget( WXWidget parent, long style )
1000{
1001 Widget borderWidget = (Widget)NULL, parentWidget = (Widget)parent;
1002
1003 if (style & wxSIMPLE_BORDER)
1004 {
1005 borderWidget = XtVaCreateManagedWidget
1006 (
1007 "simpleBorder",
1008 xmFrameWidgetClass, parentWidget,
1009 XmNshadowType, XmSHADOW_ETCHED_IN,
1010 XmNshadowThickness, 1,
1011 NULL
1012 );
1013 }
1014 else if (style & wxSUNKEN_BORDER)
1015 {
1016 borderWidget = XtVaCreateManagedWidget
1017 (
1018 "sunkenBorder",
1019 xmFrameWidgetClass, parentWidget,
1020 XmNshadowType, XmSHADOW_IN,
1021 NULL
1022 );
1023 }
1024 else if (style & wxRAISED_BORDER)
1025 {
1026 borderWidget = XtVaCreateManagedWidget
1027 (
1028 "raisedBorder",
1029 xmFrameWidgetClass, parentWidget,
1030 XmNshadowType, XmSHADOW_OUT,
1031 NULL
1032 );
1033 }
1034
1035 return borderWidget;
1036}