1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #define XtDisplay XTDISPLAY
26 #include "wx/dcmemory.h"
27 #include "wx/bitmap.h"
28 #include "wx/evtloop.h"
32 #if (defined(__SUNCC__) || defined(__CLCC__))
37 #pragma message disable nosimpint
40 #include "wx/unix/execute.h"
45 #include "wx/motif/private.h"
48 #include "X11/Xresource.h"
51 #include "X11/Xutil.h"
54 #pragma message enable nosimpint
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Yuck this is really BOTH site and platform dependent
62 // so we should use some other strategy!
64 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
66 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
70 static char *GetIniFile (char *dest
, const char *filename
);
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
78 // async event processing
79 // ----------------------------------------------------------------------------
81 // Consume all events until no more left
82 void wxFlushEvents(WXDisplay
* wxdisplay
)
84 Display
*display
= (Display
*)wxdisplay
;
87 XSync (display
, FALSE
);
89 while (evtLoop
.Pending())
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
103 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
105 wxHandleProcessTermination(proc_data
);
107 // VZ: I think they should be the same...
108 wxASSERT( (int)*id
== proc_data
->tag
);
113 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
115 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
117 (XtPointer
*) XtInputReadMask
,
118 (XtInputCallbackProc
) xt_notify_end_process
,
119 (XtPointer
) proc_data
);
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
131 // Use current setting for the bell
132 XBell (wxGlobalDisplay(), 0);
135 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
138 // This code is WRONG!! Does NOT return the
139 // Motif version of the libs but the X protocol
141 Display
*display
= wxGlobalDisplay();
143 *majorVsn
= ProtocolVersion (display
);
145 *minorVsn
= ProtocolRevision (display
);
150 // ----------------------------------------------------------------------------
151 // Reading and writing resources (eg WIN.INI, .Xdefaults)
152 // ----------------------------------------------------------------------------
156 // Read $HOME for what it says is home, if not
157 // read $USER or $LOGNAME for user name else determine
158 // the Real User, then determine the Real home dir.
159 static char * GetIniFile (char *dest
, const char *filename
)
162 if (filename
&& wxIsAbsolutePath(filename
))
164 strcpy(dest
, filename
);
166 else if ((home
= wxGetUserHome("")) != NULL
)
169 if (dest
[strlen(dest
) - 1] != '/')
171 if (filename
== NULL
)
173 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
174 filename
= ".Xdefaults";
176 else if (*filename
!= '.')
178 strcat (dest
, filename
);
186 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
188 if (create
&& wxFileExists (name
) ) {
190 return buf
; // Exists so ...
196 // Put in standard place for resource files if not absolute
197 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
199 strcat (buf
, wxFileNameFromPath (name
).c_str());
203 // Touch the file to create it
204 FILE *fd
= fopen (buf
, "w");
211 * We have a cache for writing different resource files,
212 * which will only get flushed when we call wxFlushResources().
213 * Build up a list of resource databases waiting to be written.
217 wxList
wxResourceCache (wxKEY_STRING
);
220 wxFlushResources (void)
222 char nameBuffer
[512];
224 wxNode
*node
= wxResourceCache
.First ();
227 const char *file
= node
->GetKeyString();
228 // If file doesn't exist, create it first.
229 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
231 XrmDatabase database
= (XrmDatabase
) node
->Data ();
232 XrmPutFileDatabase (database
, nameBuffer
);
233 XrmDestroyDatabase (database
);
234 wxNode
*next
= node
->Next ();
240 static XrmDatabase wxResourceDatabase
= 0;
242 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
244 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
248 (void) GetIniFile (buffer
, file
);
250 XrmDatabase database
;
251 wxNode
*node
= wxResourceCache
.Find (buffer
);
253 database
= (XrmDatabase
) node
->Data ();
256 database
= XrmGetFileDatabase (buffer
);
257 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
261 strcpy (resName
, section
.c_str());
262 strcat (resName
, ".");
263 strcat (resName
, entry
.c_str());
265 XrmPutStringResource (&database
, resName
, value
);
269 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
272 sprintf(buf
, "%.4f", value
);
273 return wxWriteResource(section
, entry
, buf
, file
);
276 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
279 sprintf(buf
, "%ld", value
);
280 return wxWriteResource(section
, entry
, buf
, file
);
283 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
286 sprintf(buf
, "%d", value
);
287 return wxWriteResource(section
, entry
, buf
, file
);
290 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
292 if (!wxResourceDatabase
)
294 Display
*display
= wxGlobalDisplay();
295 wxXMergeDatabases (wxTheApp
, display
);
298 XrmDatabase database
;
304 // Is this right? Trying to get it to look in the user's
305 // home directory instead of current directory -- JACS
306 (void) GetIniFile (buffer
, file
);
308 wxNode
*node
= wxResourceCache
.Find (buffer
);
310 database
= (XrmDatabase
) node
->Data ();
313 database
= XrmGetFileDatabase (buffer
);
314 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
318 database
= wxResourceDatabase
;
323 strcpy (buf
, section
);
327 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
329 // Try different combinations of upper/lower case, just in case...
332 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
333 success
= XrmGetResource (database
, buf
, "*", str_type
,
341 *value
= new char[xvalue
.size
+ 1];
342 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
348 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
351 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
354 *value
= (float)strtod(s
, NULL
);
361 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
364 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
367 *value
= strtol(s
, NULL
, 10);
374 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
377 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
380 // Handle True, False here
381 // True, Yes, Enables, Set or Activated
382 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
384 // False, No, Disabled, Reset, Cleared, Deactivated
385 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
389 *value
= (int) strtol (s
, NULL
, 10);
397 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
399 XrmDatabase homeDB
, serverDB
, applicationDB
;
400 char filenamebuf
[1024];
402 char *filename
= &filenamebuf
[0];
404 wxString classname
= theApp
->GetClassName();
406 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
407 (void) strcat (name
, classname
.c_str());
409 /* Get application defaults file, if any */
410 applicationDB
= XrmGetFileDatabase (name
);
411 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
413 /* Merge server defaults, created by xrdb, loaded as a property of the root
414 * window when the server initializes and loaded into the display
415 * structure on XOpenDisplay;
416 * if not defined, use .Xdefaults
419 if (XResourceManagerString (display
) != NULL
)
421 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
425 (void) GetIniFile (filename
, NULL
);
426 serverDB
= XrmGetFileDatabase (filename
);
428 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
430 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
431 * and merge into existing database
434 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
437 environment
= GetIniFile (filename
, NULL
);
438 len
= strlen (environment
);
439 wxString hostname
= wxGetHostName();
441 strncat(environment
, hostname
, 1024 - len
);
443 homeDB
= XrmGetFileDatabase (environment
);
444 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
450 * Not yet used but may be useful.
454 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
457 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
459 XrmDatabase rdb
= NULL
; // A resource data base
461 // Create an empty resource database
462 rdb
= XrmGetStringDatabase ("");
464 // Add the Component resources, prepending the name of the component
467 while (resourceSpec
[i
] != NULL
)
471 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
472 XrmPutLineResource (&rdb
, buf
);
475 // Merge them into the Xt database, with lowest precendence
479 #if (XlibSpecificationRelease>=5)
480 XrmDatabase db
= XtDatabase (dpy
);
481 XrmCombineDatabase (rdb
, &db
, FALSE
);
483 XrmMergeDatabases (dpy
->db
, &rdb
);
491 #endif // wxUSE_RESOURCES
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 void wxGetMousePosition( int* x
, int* y
)
506 XQueryPointer(wxGlobalDisplay(),
507 DefaultRootWindow(wxGlobalDisplay()),
509 &(xev
.x_root
), &(xev
.y_root
),
517 // Return TRUE if we have a colour display
518 bool wxColourDisplay()
520 return wxDisplayDepth() > 1;
523 // Returns depth of screen
526 Display
*dpy
= wxGlobalDisplay();
528 return DefaultDepth (dpy
, DefaultScreen (dpy
));
531 // Get size of display
532 void wxDisplaySize(int *width
, int *height
)
534 Display
*dpy
= wxGlobalDisplay();
537 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
539 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
542 void wxDisplaySizeMM(int *width
, int *height
)
544 Display
*dpy
= wxGlobalDisplay();
547 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
549 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
552 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
554 // This is supposed to return desktop dimensions minus any window
555 // manager panels, menus, taskbars, etc. If there is a way to do that
556 // for this platform please fix this function, otherwise it defaults
557 // to the entire desktop.
560 wxDisplaySize(width
, height
);
564 // Configurable display in wxX11 and wxMotif
565 static WXDisplay
*gs_currentDisplay
= NULL
;
566 static wxString gs_displayName
;
568 WXDisplay
*wxGetDisplay()
570 if (gs_currentDisplay
)
571 return gs_currentDisplay
;
573 return wxTheApp
->GetInitialDisplay();
577 bool wxSetDisplay(const wxString
& display_name
)
579 gs_displayName
= display_name
;
581 if ( display_name
.IsEmpty() )
583 gs_currentDisplay
= NULL
;
591 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
592 display_name
.c_str(),
593 wxTheApp
->GetAppName().c_str(),
594 wxTheApp
->GetClassName().c_str(),
596 #if XtSpecificationRelease < 5
605 gs_currentDisplay
= (WXDisplay
*) display
;
613 wxString
wxGetDisplayName()
615 return gs_displayName
;
618 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
620 return wxGenericFindWindowAtPoint(pt
);
623 // ----------------------------------------------------------------------------
624 // keycode translations
625 // ----------------------------------------------------------------------------
627 #include <X11/keysym.h>
629 // FIXME what about tables??
631 int wxCharCodeXToWX(KeySym keySym
)
638 id
= WXK_SHIFT
; break;
641 id
= WXK_CONTROL
; break;
643 id
= WXK_BACK
; break;
645 id
= WXK_DELETE
; break;
647 id
= WXK_CLEAR
; break;
653 id
= WXK_RETURN
; break;
655 id
= WXK_ESCAPE
; break;
658 id
= WXK_PAUSE
; break;
660 id
= WXK_NUMLOCK
; break;
662 id
= WXK_SCROLL
; break;
665 id
= WXK_HOME
; break;
669 id
= WXK_LEFT
; break;
671 id
= WXK_RIGHT
; break;
675 id
= WXK_DOWN
; break;
677 id
= WXK_NEXT
; break;
679 id
= WXK_PRIOR
; break;
681 id
= WXK_MENU
; break;
683 id
= WXK_SELECT
; break;
685 id
= WXK_CANCEL
; break;
687 id
= WXK_PRINT
; break;
689 id
= WXK_EXECUTE
; break;
691 id
= WXK_INSERT
; break;
693 id
= WXK_HELP
; break;
696 id
= WXK_MULTIPLY
; break;
700 id
= WXK_SUBTRACT
; break;
702 id
= WXK_DIVIDE
; break;
704 id
= WXK_DECIMAL
; break;
712 id
= WXK_RETURN
; break;
714 id
= WXK_NUMPAD0
; break;
716 id
= WXK_NUMPAD1
; break;
718 id
= WXK_NUMPAD2
; break;
720 id
= WXK_NUMPAD3
; break;
722 id
= WXK_NUMPAD4
; break;
724 id
= WXK_NUMPAD5
; break;
726 id
= WXK_NUMPAD6
; break;
728 id
= WXK_NUMPAD7
; break;
730 id
= WXK_NUMPAD8
; break;
732 id
= WXK_NUMPAD9
; break;
782 id
= (keySym
<= 255) ? (int)keySym
: -1;
788 KeySym
wxCharCodeWXToX(int id
)
794 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
795 case WXK_BACK
: keySym
= XK_BackSpace
; break;
796 case WXK_TAB
: keySym
= XK_Tab
; break;
797 case WXK_CLEAR
: keySym
= XK_Clear
; break;
798 case WXK_RETURN
: keySym
= XK_Return
; break;
799 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
800 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
801 case WXK_MENU
: keySym
= XK_Menu
; break;
802 case WXK_PAUSE
: keySym
= XK_Pause
; break;
803 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
804 case WXK_SPACE
: keySym
= ' '; break;
805 case WXK_PRIOR
: keySym
= XK_Prior
; break;
806 case WXK_NEXT
: keySym
= XK_Next
; break;
807 case WXK_END
: keySym
= XK_End
; break;
808 case WXK_HOME
: keySym
= XK_Home
; break;
809 case WXK_LEFT
: keySym
= XK_Left
; break;
810 case WXK_UP
: keySym
= XK_Up
; break;
811 case WXK_RIGHT
: keySym
= XK_Right
; break;
812 case WXK_DOWN
: keySym
= XK_Down
; break;
813 case WXK_SELECT
: keySym
= XK_Select
; break;
814 case WXK_PRINT
: keySym
= XK_Print
; break;
815 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
816 case WXK_INSERT
: keySym
= XK_Insert
; break;
817 case WXK_DELETE
: keySym
= XK_Delete
; break;
818 case WXK_HELP
: keySym
= XK_Help
; break;
819 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
820 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
821 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
822 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
823 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
824 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
825 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
826 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
827 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
828 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
829 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
830 case WXK_ADD
: keySym
= XK_KP_Add
; break;
831 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
832 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
833 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
834 case WXK_F1
: keySym
= XK_F1
; break;
835 case WXK_F2
: keySym
= XK_F2
; break;
836 case WXK_F3
: keySym
= XK_F3
; break;
837 case WXK_F4
: keySym
= XK_F4
; break;
838 case WXK_F5
: keySym
= XK_F5
; break;
839 case WXK_F6
: keySym
= XK_F6
; break;
840 case WXK_F7
: keySym
= XK_F7
; break;
841 case WXK_F8
: keySym
= XK_F8
; break;
842 case WXK_F9
: keySym
= XK_F9
; break;
843 case WXK_F10
: keySym
= XK_F10
; break;
844 case WXK_F11
: keySym
= XK_F11
; break;
845 case WXK_F12
: keySym
= XK_F12
; break;
846 case WXK_F13
: keySym
= XK_F13
; break;
847 case WXK_F14
: keySym
= XK_F14
; break;
848 case WXK_F15
: keySym
= XK_F15
; break;
849 case WXK_F16
: keySym
= XK_F16
; break;
850 case WXK_F17
: keySym
= XK_F17
; break;
851 case WXK_F18
: keySym
= XK_F18
; break;
852 case WXK_F19
: keySym
= XK_F19
; break;
853 case WXK_F20
: keySym
= XK_F20
; break;
854 case WXK_F21
: keySym
= XK_F21
; break;
855 case WXK_F22
: keySym
= XK_F22
; break;
856 case WXK_F23
: keySym
= XK_F23
; break;
857 case WXK_F24
: keySym
= XK_F24
; break;
858 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
859 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
860 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
866 // ----------------------------------------------------------------------------
867 // Some colour manipulation routines
868 // ----------------------------------------------------------------------------
870 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
875 int r
= 0, g
= 0, b
= 0;
878 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
879 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
881 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
884 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
885 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
886 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
889 case 0: r
= v
, g
= t
, b
= p
; break;
890 case 1: r
= q
, g
= v
, b
= p
; break;
891 case 2: r
= p
, g
= v
, b
= t
; break;
892 case 3: r
= p
, g
= q
, b
= v
; break;
893 case 4: r
= t
, g
= p
, b
= v
; break;
894 case 5: r
= v
, g
= p
, b
= q
; break;
901 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
903 int r
= rgb
->red
>> 8;
904 int g
= rgb
->green
>> 8;
905 int b
= rgb
->blue
>> 8;
906 int maxv
= wxMax3(r
, g
, b
);
907 int minv
= wxMin3(r
, g
, b
);
910 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
915 int rc
, gc
, bc
, hex
= 0;
916 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
917 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
918 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
919 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
920 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
921 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
922 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
926 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
927 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
930 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
935 int screen
= DefaultScreen(d
);
936 int num_colors
= DisplayCells(d
,screen
);
938 XColor
*color_defs
= new XColor
[num_colors
];
939 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
940 XQueryColors(d
,cmp
,color_defs
,num_colors
);
943 wxXColorToHSV(&hsv
,xc
);
945 int diff
, min_diff
= 0, pixel
= 0;
947 for(llp
= 0;llp
< num_colors
;llp
++)
949 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
950 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
951 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
952 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
953 if (llp
== 0) min_diff
= diff
;
954 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
955 if (min_diff
== 0) break;
958 xc
-> red
= color_defs
[pixel
].red
;
959 xc
-> green
= color_defs
[pixel
].green
;
960 xc
-> blue
= color_defs
[pixel
].blue
;
961 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
964 if (!XAllocColor(d,cmp,xc))
965 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
972 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
974 if (!XAllocColor(d
,cmp
,xc
))
976 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
977 wxAllocNearestColor(d
,cmp
,xc
);
982 wxString
wxGetXEventName(XEvent
& event
)
985 wxString
str(wxT("(some event)"));
988 int type
= event
.xany
.type
;
989 static char* event_name
[] = {
990 "", "unknown(-)", // 0-1
991 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
992 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
993 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
994 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
995 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
996 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
997 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
998 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
999 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1000 "ClientMessage", "MappingNotify", // 33-34
1001 "unknown(+)"}; // 35
1002 type
= wxMin(35, type
); type
= wxMax(1, type
);
1003 wxString
str(event_name
[type
]);
1009 // ----------------------------------------------------------------------------
1011 // ----------------------------------------------------------------------------
1013 // Find the letter corresponding to the mnemonic, for Motif
1014 char wxFindMnemonic (const char *s
)
1017 int len
= strlen (s
);
1020 for (i
= 0; i
< len
; i
++)
1024 // Carefully handle &&
1025 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1037 char* wxFindAccelerator( const char *s
)
1040 // VZ: this function returns incorrect keysym which completely breaks kbd
1044 // The accelerator text is after the \t char.
1045 s
= strchr( s
, '\t' );
1047 if( !s
) return NULL
;
1050 Now we need to format it as X standard:
1055 Ctrl+N --> Ctrl<Key>N
1056 Alt+k --> Meta<Key>k
1057 Ctrl+Shift+A --> Ctrl Shift<Key>A
1059 and handle Ctrl-N & similia
1062 static char buf
[256];
1065 wxString tmp
= s
+ 1; // skip TAB
1068 while( index
< tmp
.length() )
1070 size_t plus
= tmp
.find( '+', index
);
1071 size_t minus
= tmp
.find( '-', index
);
1073 // neither '+' nor '-', add <Key>
1074 if( plus
== wxString::npos
&& minus
== wxString::npos
)
1076 strcat( buf
, "<Key>" );
1077 strcat( buf
, tmp
.c_str() + index
);
1082 // OK: npos is big and positive
1083 size_t sep
= wxMin( plus
, minus
);
1084 wxString mod
= tmp
.substr( index
, sep
- index
);
1095 strcat( buf
, mod
.c_str() );
1104 XmString
wxFindAcceleratorText (const char *s
)
1107 // VZ: this function returns incorrect keysym which completely breaks kbd
1111 // The accelerator text is after the \t char.
1112 s
= strchr( s
, '\t' );
1114 if( !s
) return NULL
;
1116 return wxStringToXmString( s
+ 1 ); // skip TAB!
1120 // Change a widget's foreground and background colours.
1121 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1123 // When should we specify the foreground, if it's calculated
1124 // by wxComputeColours?
1125 // Solution: say we start with the default (computed) foreground colour.
1126 // If we call SetForegroundColour explicitly for a control or window,
1127 // then the foreground is changed.
1128 // Therefore SetBackgroundColour computes the foreground colour, and
1129 // SetForegroundColour changes the foreground colour. The ordering is
1132 XtVaSetValues ((Widget
) widget
,
1133 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1137 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1139 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1142 XtVaSetValues ((Widget
) widget
,
1143 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1144 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1145 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1146 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1149 if (changeArmColour
)
1150 XtVaSetValues ((Widget
) widget
,
1151 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1155 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1157 // Lesstif 0.87 hangs here, but 0.93 does not
1158 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1159 Widget w
= (Widget
)widget
;
1161 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1167 wxString
wxXmStringToString( const XmString
& xmString
)
1170 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1177 return wxEmptyString
;
1180 XmString
wxStringToXmString( const wxString
& str
)
1182 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1185 XmString
wxStringToXmString( const char* str
)
1187 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1190 // ----------------------------------------------------------------------------
1191 // wxBitmap utility functions
1192 // ----------------------------------------------------------------------------
1194 // Creates a bitmap with transparent areas drawn in
1195 // the given colour.
1196 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1198 wxBitmap
newBitmap(bitmap
.GetWidth(),
1204 srcDC
.SelectObject(bitmap
);
1205 destDC
.SelectObject(newBitmap
);
1207 wxBrush
brush(colour
, wxSOLID
);
1208 // destDC.SetOptimization(FALSE);
1209 destDC
.SetBackground(brush
);
1211 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1212 &srcDC
, 0, 0, wxCOPY
, TRUE
);
1217 // ----------------------------------------------------------------------------
1218 // Miscellaneous functions
1219 // ----------------------------------------------------------------------------
1221 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
1223 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
1225 if (style
& wxSIMPLE_BORDER
)
1227 borderWidget
= XtVaCreateManagedWidget
1230 xmFrameWidgetClass
, parentWidget
,
1231 XmNshadowType
, XmSHADOW_ETCHED_IN
,
1232 XmNshadowThickness
, 1,
1236 else if (style
& wxSUNKEN_BORDER
)
1238 borderWidget
= XtVaCreateManagedWidget
1241 xmFrameWidgetClass
, parentWidget
,
1242 XmNshadowType
, XmSHADOW_IN
,
1246 else if (style
& wxRAISED_BORDER
)
1248 borderWidget
= XtVaCreateManagedWidget
1251 xmFrameWidgetClass
, parentWidget
,
1252 XmNshadowType
, XmSHADOW_OUT
,
1257 return borderWidget
;