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