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