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
25 #include "wx/apptrait.h"
27 #include "wx/dcmemory.h"
28 #include "wx/bitmap.h"
29 #include "wx/evtloop.h"
33 #if (defined(__SUNCC__) || defined(__CLCC__))
38 #pragma message disable nosimpint
41 #include "wx/unix/execute.h"
46 #include "wx/motif/private.h"
49 #include "X11/Xresource.h"
52 #include "X11/Xutil.h"
55 #pragma message enable nosimpint
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Yuck this is really BOTH site and platform dependent
63 // so we should use some other strategy!
65 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
67 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
71 static char *GetIniFile (char *dest
, const char *filename
);
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // async event processing
80 // ----------------------------------------------------------------------------
82 // Consume all events until no more left
83 void wxFlushEvents(WXDisplay
* wxdisplay
)
85 Display
*display
= (Display
*)wxdisplay
;
88 XSync (display
, FALSE
);
90 while (evtLoop
.Pending())
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
104 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
106 wxHandleProcessTermination(proc_data
);
108 // VZ: I think they should be the same...
109 wxASSERT( (int)*id
== proc_data
->tag
);
114 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
116 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
118 (XtPointer
*) XtInputReadMask
,
119 (XtInputCallbackProc
) xt_notify_end_process
,
120 (XtPointer
) proc_data
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
132 // Use current setting for the bell
133 XBell (wxGlobalDisplay(), 0);
136 int wxGUIAppTraits::GetOSVersion(int *majorVsn
, int *minorVsn
)
139 // This code is WRONG!! Does NOT return the
140 // Motif version of the libs but the X protocol
142 Display
*display
= wxGlobalDisplay();
144 *majorVsn
= ProtocolVersion (display
);
146 *minorVsn
= ProtocolRevision (display
);
151 // ----------------------------------------------------------------------------
152 // Reading and writing resources (eg WIN.INI, .Xdefaults)
153 // ----------------------------------------------------------------------------
157 // Read $HOME for what it says is home, if not
158 // read $USER or $LOGNAME for user name else determine
159 // the Real User, then determine the Real home dir.
160 static char * GetIniFile (char *dest
, const char *filename
)
163 if (filename
&& wxIsAbsolutePath(filename
))
165 strcpy(dest
, filename
);
167 else if ((home
= wxGetUserHome("")) != NULL
)
170 if (dest
[strlen(dest
) - 1] != '/')
172 if (filename
== NULL
)
174 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
175 filename
= ".Xdefaults";
177 else if (*filename
!= '.')
179 strcat (dest
, filename
);
187 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
189 if (create
&& wxFileExists (name
) ) {
191 return buf
; // Exists so ...
197 // Put in standard place for resource files if not absolute
198 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
200 strcat (buf
, wxFileNameFromPath (name
).c_str());
204 // Touch the file to create it
205 FILE *fd
= fopen (buf
, "w");
212 * We have a cache for writing different resource files,
213 * which will only get flushed when we call wxFlushResources().
214 * Build up a list of resource databases waiting to be written.
218 wxList
wxResourceCache (wxKEY_STRING
);
221 wxFlushResources (void)
223 char nameBuffer
[512];
225 wxNode
*node
= wxResourceCache
.First ();
228 const char *file
= node
->GetKeyString();
229 // If file doesn't exist, create it first.
230 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
232 XrmDatabase database
= (XrmDatabase
) node
->Data ();
233 XrmPutFileDatabase (database
, nameBuffer
);
234 XrmDestroyDatabase (database
);
235 wxNode
*next
= node
->Next ();
241 static XrmDatabase wxResourceDatabase
= 0;
243 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
245 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
249 (void) GetIniFile (buffer
, file
);
251 XrmDatabase database
;
252 wxNode
*node
= wxResourceCache
.Find (buffer
);
254 database
= (XrmDatabase
) node
->Data ();
257 database
= XrmGetFileDatabase (buffer
);
258 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
262 strcpy (resName
, section
.c_str());
263 strcat (resName
, ".");
264 strcat (resName
, entry
.c_str());
266 XrmPutStringResource (&database
, resName
, value
);
270 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
273 sprintf(buf
, "%.4f", value
);
274 return wxWriteResource(section
, entry
, buf
, file
);
277 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
280 sprintf(buf
, "%ld", value
);
281 return wxWriteResource(section
, entry
, buf
, file
);
284 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
287 sprintf(buf
, "%d", value
);
288 return wxWriteResource(section
, entry
, buf
, file
);
291 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
293 if (!wxResourceDatabase
)
295 Display
*display
= wxGlobalDisplay();
296 wxXMergeDatabases (wxTheApp
, display
);
299 XrmDatabase database
;
305 // Is this right? Trying to get it to look in the user's
306 // home directory instead of current directory -- JACS
307 (void) GetIniFile (buffer
, file
);
309 wxNode
*node
= wxResourceCache
.Find (buffer
);
311 database
= (XrmDatabase
) node
->Data ();
314 database
= XrmGetFileDatabase (buffer
);
315 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
319 database
= wxResourceDatabase
;
324 strcpy (buf
, section
);
328 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
330 // Try different combinations of upper/lower case, just in case...
333 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
334 success
= XrmGetResource (database
, buf
, "*", str_type
,
342 *value
= new char[xvalue
.size
+ 1];
343 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
349 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
352 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
355 *value
= (float)strtod(s
, NULL
);
362 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
365 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
368 *value
= strtol(s
, NULL
, 10);
375 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
378 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
381 // Handle True, False here
382 // True, Yes, Enables, Set or Activated
383 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
385 // False, No, Disabled, Reset, Cleared, Deactivated
386 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
390 *value
= (int) strtol (s
, NULL
, 10);
398 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
400 XrmDatabase homeDB
, serverDB
, applicationDB
;
401 char filenamebuf
[1024];
403 char *filename
= &filenamebuf
[0];
405 wxString classname
= theApp
->GetClassName();
407 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
408 (void) strcat (name
, classname
.c_str());
410 /* Get application defaults file, if any */
411 applicationDB
= XrmGetFileDatabase (name
);
412 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
414 /* Merge server defaults, created by xrdb, loaded as a property of the root
415 * window when the server initializes and loaded into the display
416 * structure on XOpenDisplay;
417 * if not defined, use .Xdefaults
420 if (XResourceManagerString (display
) != NULL
)
422 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
426 (void) GetIniFile (filename
, NULL
);
427 serverDB
= XrmGetFileDatabase (filename
);
429 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
431 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
432 * and merge into existing database
435 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
438 environment
= GetIniFile (filename
, NULL
);
439 len
= strlen (environment
);
440 wxString hostname
= wxGetHostName();
442 strncat(environment
, hostname
, 1024 - len
);
444 homeDB
= XrmGetFileDatabase (environment
);
445 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
451 * Not yet used but may be useful.
455 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
458 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
460 XrmDatabase rdb
= NULL
; // A resource data base
462 // Create an empty resource database
463 rdb
= XrmGetStringDatabase ("");
465 // Add the Component resources, prepending the name of the component
468 while (resourceSpec
[i
] != NULL
)
472 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
473 XrmPutLineResource (&rdb
, buf
);
476 // Merge them into the Xt database, with lowest precendence
480 #if (XlibSpecificationRelease>=5)
481 XrmDatabase db
= XtDatabase (dpy
);
482 XrmCombineDatabase (rdb
, &db
, FALSE
);
484 XrmMergeDatabases (dpy
->db
, &rdb
);
492 #endif // wxUSE_RESOURCES
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
498 void wxGetMousePosition( int* x
, int* y
)
507 XQueryPointer(wxGlobalDisplay(),
508 DefaultRootWindow(wxGlobalDisplay()),
510 &(xev
.x_root
), &(xev
.y_root
),
518 // Return TRUE if we have a colour display
519 bool wxColourDisplay()
521 return wxDisplayDepth() > 1;
524 // Returns depth of screen
527 Display
*dpy
= wxGlobalDisplay();
529 return DefaultDepth (dpy
, DefaultScreen (dpy
));
532 // Get size of display
533 void wxDisplaySize(int *width
, int *height
)
535 Display
*dpy
= wxGlobalDisplay();
538 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
540 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
543 void wxDisplaySizeMM(int *width
, int *height
)
545 Display
*dpy
= wxGlobalDisplay();
548 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
550 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
553 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
555 // This is supposed to return desktop dimensions minus any window
556 // manager panels, menus, taskbars, etc. If there is a way to do that
557 // for this platform please fix this function, otherwise it defaults
558 // to the entire desktop.
561 wxDisplaySize(width
, height
);
565 // Configurable display in wxX11 and wxMotif
566 static WXDisplay
*gs_currentDisplay
= NULL
;
567 static wxString gs_displayName
;
569 WXDisplay
*wxGetDisplay()
571 if (gs_currentDisplay
)
572 return gs_currentDisplay
;
574 return wxTheApp
->GetInitialDisplay();
578 bool wxSetDisplay(const wxString
& display_name
)
580 gs_displayName
= display_name
;
582 if ( display_name
.IsEmpty() )
584 gs_currentDisplay
= NULL
;
592 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
593 display_name
.c_str(),
594 wxTheApp
->GetAppName().c_str(),
595 wxTheApp
->GetClassName().c_str(),
597 #if XtSpecificationRelease < 5
606 gs_currentDisplay
= (WXDisplay
*) display
;
614 wxString
wxGetDisplayName()
616 return gs_displayName
;
619 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
621 return wxGenericFindWindowAtPoint(pt
);
624 // ----------------------------------------------------------------------------
625 // keycode translations
626 // ----------------------------------------------------------------------------
628 #include <X11/keysym.h>
630 // FIXME what about tables??
632 int wxCharCodeXToWX(KeySym keySym
)
639 id
= WXK_SHIFT
; break;
642 id
= WXK_CONTROL
; break;
644 id
= WXK_BACK
; break;
646 id
= WXK_DELETE
; break;
648 id
= WXK_CLEAR
; break;
654 id
= WXK_RETURN
; break;
656 id
= WXK_ESCAPE
; break;
659 id
= WXK_PAUSE
; break;
661 id
= WXK_NUMLOCK
; break;
663 id
= WXK_SCROLL
; break;
666 id
= WXK_HOME
; break;
670 id
= WXK_LEFT
; break;
672 id
= WXK_RIGHT
; break;
676 id
= WXK_DOWN
; break;
678 id
= WXK_NEXT
; break;
680 id
= WXK_PRIOR
; break;
682 id
= WXK_MENU
; break;
684 id
= WXK_SELECT
; break;
686 id
= WXK_CANCEL
; break;
688 id
= WXK_PRINT
; break;
690 id
= WXK_EXECUTE
; break;
692 id
= WXK_INSERT
; break;
694 id
= WXK_HELP
; break;
697 id
= WXK_MULTIPLY
; break;
701 id
= WXK_SUBTRACT
; break;
703 id
= WXK_DIVIDE
; break;
705 id
= WXK_DECIMAL
; break;
713 id
= WXK_RETURN
; break;
715 id
= WXK_NUMPAD0
; break;
717 id
= WXK_NUMPAD1
; break;
719 id
= WXK_NUMPAD2
; break;
721 id
= WXK_NUMPAD3
; break;
723 id
= WXK_NUMPAD4
; break;
725 id
= WXK_NUMPAD5
; break;
727 id
= WXK_NUMPAD6
; break;
729 id
= WXK_NUMPAD7
; break;
731 id
= WXK_NUMPAD8
; break;
733 id
= WXK_NUMPAD9
; break;
783 id
= (keySym
<= 255) ? (int)keySym
: -1;
789 KeySym
wxCharCodeWXToX(int id
)
795 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
796 case WXK_BACK
: keySym
= XK_BackSpace
; break;
797 case WXK_TAB
: keySym
= XK_Tab
; break;
798 case WXK_CLEAR
: keySym
= XK_Clear
; break;
799 case WXK_RETURN
: keySym
= XK_Return
; break;
800 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
801 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
802 case WXK_MENU
: keySym
= XK_Menu
; break;
803 case WXK_PAUSE
: keySym
= XK_Pause
; break;
804 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
805 case WXK_SPACE
: keySym
= ' '; break;
806 case WXK_PRIOR
: keySym
= XK_Prior
; break;
807 case WXK_NEXT
: keySym
= XK_Next
; break;
808 case WXK_END
: keySym
= XK_End
; break;
809 case WXK_HOME
: keySym
= XK_Home
; break;
810 case WXK_LEFT
: keySym
= XK_Left
; break;
811 case WXK_UP
: keySym
= XK_Up
; break;
812 case WXK_RIGHT
: keySym
= XK_Right
; break;
813 case WXK_DOWN
: keySym
= XK_Down
; break;
814 case WXK_SELECT
: keySym
= XK_Select
; break;
815 case WXK_PRINT
: keySym
= XK_Print
; break;
816 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
817 case WXK_INSERT
: keySym
= XK_Insert
; break;
818 case WXK_DELETE
: keySym
= XK_Delete
; break;
819 case WXK_HELP
: keySym
= XK_Help
; break;
820 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
821 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
822 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
823 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
824 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
825 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
826 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
827 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
828 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
829 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
830 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
831 case WXK_ADD
: keySym
= XK_KP_Add
; break;
832 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
833 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
834 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
835 case WXK_F1
: keySym
= XK_F1
; break;
836 case WXK_F2
: keySym
= XK_F2
; break;
837 case WXK_F3
: keySym
= XK_F3
; break;
838 case WXK_F4
: keySym
= XK_F4
; break;
839 case WXK_F5
: keySym
= XK_F5
; break;
840 case WXK_F6
: keySym
= XK_F6
; break;
841 case WXK_F7
: keySym
= XK_F7
; break;
842 case WXK_F8
: keySym
= XK_F8
; break;
843 case WXK_F9
: keySym
= XK_F9
; break;
844 case WXK_F10
: keySym
= XK_F10
; break;
845 case WXK_F11
: keySym
= XK_F11
; break;
846 case WXK_F12
: keySym
= XK_F12
; break;
847 case WXK_F13
: keySym
= XK_F13
; break;
848 case WXK_F14
: keySym
= XK_F14
; break;
849 case WXK_F15
: keySym
= XK_F15
; break;
850 case WXK_F16
: keySym
= XK_F16
; break;
851 case WXK_F17
: keySym
= XK_F17
; break;
852 case WXK_F18
: keySym
= XK_F18
; break;
853 case WXK_F19
: keySym
= XK_F19
; break;
854 case WXK_F20
: keySym
= XK_F20
; break;
855 case WXK_F21
: keySym
= XK_F21
; break;
856 case WXK_F22
: keySym
= XK_F22
; break;
857 case WXK_F23
: keySym
= XK_F23
; break;
858 case WXK_F24
: keySym
= XK_F24
; break;
859 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
860 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
861 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
867 // ----------------------------------------------------------------------------
868 // Some colour manipulation routines
869 // ----------------------------------------------------------------------------
871 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
876 int r
= 0, g
= 0, b
= 0;
879 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
880 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
882 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
885 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
886 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
887 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
890 case 0: r
= v
, g
= t
, b
= p
; break;
891 case 1: r
= q
, g
= v
, b
= p
; break;
892 case 2: r
= p
, g
= v
, b
= t
; break;
893 case 3: r
= p
, g
= q
, b
= v
; break;
894 case 4: r
= t
, g
= p
, b
= v
; break;
895 case 5: r
= v
, g
= p
, b
= q
; break;
902 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
904 int r
= rgb
->red
>> 8;
905 int g
= rgb
->green
>> 8;
906 int b
= rgb
->blue
>> 8;
907 int maxv
= wxMax3(r
, g
, b
);
908 int minv
= wxMin3(r
, g
, b
);
911 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
916 int rc
, gc
, bc
, hex
= 0;
917 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
918 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
919 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
920 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
921 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
922 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
923 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
927 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
928 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
931 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
936 int screen
= DefaultScreen(d
);
937 int num_colors
= DisplayCells(d
,screen
);
939 XColor
*color_defs
= new XColor
[num_colors
];
940 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
941 XQueryColors(d
,cmp
,color_defs
,num_colors
);
944 wxXColorToHSV(&hsv
,xc
);
946 int diff
, min_diff
= 0, pixel
= 0;
948 for(llp
= 0;llp
< num_colors
;llp
++)
950 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
951 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
952 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
953 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
954 if (llp
== 0) min_diff
= diff
;
955 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
956 if (min_diff
== 0) break;
959 xc
-> red
= color_defs
[pixel
].red
;
960 xc
-> green
= color_defs
[pixel
].green
;
961 xc
-> blue
= color_defs
[pixel
].blue
;
962 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
965 if (!XAllocColor(d,cmp,xc))
966 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
973 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
975 if (!XAllocColor(d
,cmp
,xc
))
977 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
978 wxAllocNearestColor(d
,cmp
,xc
);
983 wxString
wxGetXEventName(XEvent
& event
)
986 wxString
str(wxT("(some event)"));
989 int type
= event
.xany
.type
;
990 static char* event_name
[] = {
991 "", "unknown(-)", // 0-1
992 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
993 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
994 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
995 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
996 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
997 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
998 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
999 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1000 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1001 "ClientMessage", "MappingNotify", // 33-34
1002 "unknown(+)"}; // 35
1003 type
= wxMin(35, type
); type
= wxMax(1, type
);
1004 wxString
str(event_name
[type
]);
1010 // ----------------------------------------------------------------------------
1012 // ----------------------------------------------------------------------------
1014 // Find the letter corresponding to the mnemonic, for Motif
1015 char wxFindMnemonic (const char *s
)
1018 int len
= strlen (s
);
1021 for (i
= 0; i
< len
; i
++)
1025 // Carefully handle &&
1026 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1038 char* wxFindAccelerator( const char *s
)
1041 // VZ: this function returns incorrect keysym which completely breaks kbd
1045 // The accelerator text is after the \t char.
1046 s
= strchr( s
, '\t' );
1048 if( !s
) return NULL
;
1051 Now we need to format it as X standard:
1056 Ctrl+N --> Ctrl<Key>N
1057 Alt+k --> Meta<Key>k
1058 Ctrl+Shift+A --> Ctrl Shift<Key>A
1060 and handle Ctrl-N & similia
1063 static char buf
[256];
1066 wxString tmp
= s
+ 1; // skip TAB
1069 while( index
< tmp
.length() )
1071 size_t plus
= tmp
.find( '+', index
);
1072 size_t minus
= tmp
.find( '-', index
);
1074 // neither '+' nor '-', add <Key>
1075 if( plus
== wxString::npos
&& minus
== wxString::npos
)
1077 strcat( buf
, "<Key>" );
1078 strcat( buf
, tmp
.c_str() + index
);
1083 // OK: npos is big and positive
1084 size_t sep
= wxMin( plus
, minus
);
1085 wxString mod
= tmp
.substr( index
, sep
- index
);
1096 strcat( buf
, mod
.c_str() );
1105 XmString
wxFindAcceleratorText (const char *s
)
1108 // VZ: this function returns incorrect keysym which completely breaks kbd
1112 // The accelerator text is after the \t char.
1113 s
= strchr( s
, '\t' );
1115 if( !s
) return NULL
;
1117 return wxStringToXmString( s
+ 1 ); // skip TAB!
1121 // Change a widget's foreground and background colours.
1122 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1124 // When should we specify the foreground, if it's calculated
1125 // by wxComputeColours?
1126 // Solution: say we start with the default (computed) foreground colour.
1127 // If we call SetForegroundColour explicitly for a control or window,
1128 // then the foreground is changed.
1129 // Therefore SetBackgroundColour computes the foreground colour, and
1130 // SetForegroundColour changes the foreground colour. The ordering is
1133 XtVaSetValues ((Widget
) widget
,
1134 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1138 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1140 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1143 XtVaSetValues ((Widget
) widget
,
1144 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1145 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1146 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1147 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1150 if (changeArmColour
)
1151 XtVaSetValues ((Widget
) widget
,
1152 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1156 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1158 // Lesstif 0.87 hangs here, but 0.93 does not
1159 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1160 Widget w
= (Widget
)widget
;
1162 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1168 wxString
wxXmStringToString( const XmString
& xmString
)
1171 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1178 return wxEmptyString
;
1181 XmString
wxStringToXmString( const wxString
& str
)
1183 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1186 XmString
wxStringToXmString( const char* str
)
1188 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1191 // ----------------------------------------------------------------------------
1192 // wxBitmap utility functions
1193 // ----------------------------------------------------------------------------
1195 // Creates a bitmap with transparent areas drawn in
1196 // the given colour.
1197 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1199 wxBitmap
newBitmap(bitmap
.GetWidth(),
1205 srcDC
.SelectObject(bitmap
);
1206 destDC
.SelectObject(newBitmap
);
1208 wxBrush
brush(colour
, wxSOLID
);
1209 // destDC.SetOptimization(FALSE);
1210 destDC
.SetBackground(brush
);
1212 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1213 &srcDC
, 0, 0, wxCOPY
, TRUE
);
1218 // ----------------------------------------------------------------------------
1219 // Miscellaneous functions
1220 // ----------------------------------------------------------------------------
1222 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
1224 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
1226 if (style
& wxSIMPLE_BORDER
)
1228 borderWidget
= XtVaCreateManagedWidget
1231 xmFrameWidgetClass
, parentWidget
,
1232 XmNshadowType
, XmSHADOW_ETCHED_IN
,
1233 XmNshadowThickness
, 1,
1237 else if (style
& wxSUNKEN_BORDER
)
1239 borderWidget
= XtVaCreateManagedWidget
1242 xmFrameWidgetClass
, parentWidget
,
1243 XmNshadowType
, XmSHADOW_IN
,
1247 else if (style
& wxRAISED_BORDER
)
1249 borderWidget
= XtVaCreateManagedWidget
1252 xmFrameWidgetClass
, parentWidget
,
1253 XmNshadowType
, XmSHADOW_OUT
,
1258 return borderWidget
;