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 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
138 static wxToolkitInfo info
;
140 info
.shortName
= _T("motif");
141 info
.name
= _T("wxMotif");
142 #ifdef __WXUNIVERSAL__
143 info
.shortName
<< _T("univ");
144 info
.name
<< _T("/wxUniversal");
147 // This code is WRONG!! Does NOT return the
148 // Motif version of the libs but the X protocol
150 Display
*display
= wxGlobalDisplay();
151 info
.versionMajor
= ProtocolVersion (display
);
152 info
.versionMinor
= ProtocolRevision (display
);
157 // ----------------------------------------------------------------------------
158 // Reading and writing resources (eg WIN.INI, .Xdefaults)
159 // ----------------------------------------------------------------------------
163 // Read $HOME for what it says is home, if not
164 // read $USER or $LOGNAME for user name else determine
165 // the Real User, then determine the Real home dir.
166 static char * GetIniFile (char *dest
, const char *filename
)
169 if (filename
&& wxIsAbsolutePath(filename
))
171 strcpy(dest
, filename
);
173 else if ((home
= wxGetUserHome("")) != NULL
)
176 if (dest
[strlen(dest
) - 1] != '/')
178 if (filename
== NULL
)
180 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
181 filename
= ".Xdefaults";
183 else if (*filename
!= '.')
185 strcat (dest
, filename
);
193 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
195 if (create
&& wxFileExists (name
) ) {
197 return buf
; // Exists so ...
203 // Put in standard place for resource files if not absolute
204 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
206 strcat (buf
, wxFileNameFromPath (name
).c_str());
210 // Touch the file to create it
211 FILE *fd
= fopen (buf
, "w");
218 * We have a cache for writing different resource files,
219 * which will only get flushed when we call wxFlushResources().
220 * Build up a list of resource databases waiting to be written.
224 wxList
wxResourceCache (wxKEY_STRING
);
227 wxFlushResources (void)
229 char nameBuffer
[512];
231 wxNode
*node
= wxResourceCache
.First ();
234 const char *file
= node
->GetKeyString();
235 // If file doesn't exist, create it first.
236 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
238 XrmDatabase database
= (XrmDatabase
) node
->Data ();
239 XrmPutFileDatabase (database
, nameBuffer
);
240 XrmDestroyDatabase (database
);
241 wxNode
*next
= node
->Next ();
247 static XrmDatabase wxResourceDatabase
= 0;
249 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
251 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
255 (void) GetIniFile (buffer
, file
);
257 XrmDatabase database
;
258 wxNode
*node
= wxResourceCache
.Find (buffer
);
260 database
= (XrmDatabase
) node
->Data ();
263 database
= XrmGetFileDatabase (buffer
);
264 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
268 strcpy (resName
, section
.c_str());
269 strcat (resName
, ".");
270 strcat (resName
, entry
.c_str());
272 XrmPutStringResource (&database
, resName
, value
);
276 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
279 sprintf(buf
, "%.4f", value
);
280 return wxWriteResource(section
, entry
, buf
, file
);
283 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
286 sprintf(buf
, "%ld", value
);
287 return wxWriteResource(section
, entry
, buf
, file
);
290 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
293 sprintf(buf
, "%d", value
);
294 return wxWriteResource(section
, entry
, buf
, file
);
297 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
299 if (!wxResourceDatabase
)
301 Display
*display
= wxGlobalDisplay();
302 wxXMergeDatabases (wxTheApp
, display
);
305 XrmDatabase database
;
311 // Is this right? Trying to get it to look in the user's
312 // home directory instead of current directory -- JACS
313 (void) GetIniFile (buffer
, file
);
315 wxNode
*node
= wxResourceCache
.Find (buffer
);
317 database
= (XrmDatabase
) node
->Data ();
320 database
= XrmGetFileDatabase (buffer
);
321 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
325 database
= wxResourceDatabase
;
330 strcpy (buf
, section
);
334 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
336 // Try different combinations of upper/lower case, just in case...
339 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
340 success
= XrmGetResource (database
, buf
, "*", str_type
,
348 *value
= new char[xvalue
.size
+ 1];
349 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
355 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
358 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
361 *value
= (float)strtod(s
, NULL
);
368 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
371 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
374 *value
= strtol(s
, NULL
, 10);
381 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
384 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
387 // Handle True, False here
388 // True, Yes, Enables, Set or Activated
389 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
391 // False, No, Disabled, Reset, Cleared, Deactivated
392 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
396 *value
= (int) strtol (s
, NULL
, 10);
404 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
406 XrmDatabase homeDB
, serverDB
, applicationDB
;
407 char filenamebuf
[1024];
409 char *filename
= &filenamebuf
[0];
411 wxString classname
= theApp
->GetClassName();
413 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
414 (void) strcat (name
, classname
.c_str());
416 /* Get application defaults file, if any */
417 applicationDB
= XrmGetFileDatabase (name
);
418 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
420 /* Merge server defaults, created by xrdb, loaded as a property of the root
421 * window when the server initializes and loaded into the display
422 * structure on XOpenDisplay;
423 * if not defined, use .Xdefaults
426 if (XResourceManagerString (display
) != NULL
)
428 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
432 (void) GetIniFile (filename
, NULL
);
433 serverDB
= XrmGetFileDatabase (filename
);
435 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
437 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
438 * and merge into existing database
441 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
444 environment
= GetIniFile (filename
, NULL
);
445 len
= strlen (environment
);
446 wxString hostname
= wxGetHostName();
448 strncat(environment
, hostname
, 1024 - len
);
450 homeDB
= XrmGetFileDatabase (environment
);
451 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
457 * Not yet used but may be useful.
461 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
464 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
466 XrmDatabase rdb
= NULL
; // A resource data base
468 // Create an empty resource database
469 rdb
= XrmGetStringDatabase ("");
471 // Add the Component resources, prepending the name of the component
474 while (resourceSpec
[i
] != NULL
)
478 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
479 XrmPutLineResource (&rdb
, buf
);
482 // Merge them into the Xt database, with lowest precendence
486 #if (XlibSpecificationRelease>=5)
487 XrmDatabase db
= XtDatabase (dpy
);
488 XrmCombineDatabase (rdb
, &db
, FALSE
);
490 XrmMergeDatabases (dpy
->db
, &rdb
);
498 #endif // wxUSE_RESOURCES
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 void wxGetMousePosition( int* x
, int* y
)
513 XQueryPointer(wxGlobalDisplay(),
514 DefaultRootWindow(wxGlobalDisplay()),
516 &(xev
.x_root
), &(xev
.y_root
),
524 // Return TRUE if we have a colour display
525 bool wxColourDisplay()
527 return wxDisplayDepth() > 1;
530 // Returns depth of screen
533 Display
*dpy
= wxGlobalDisplay();
535 return DefaultDepth (dpy
, DefaultScreen (dpy
));
538 // Get size of display
539 void wxDisplaySize(int *width
, int *height
)
541 Display
*dpy
= wxGlobalDisplay();
544 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
546 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
549 void wxDisplaySizeMM(int *width
, int *height
)
551 Display
*dpy
= wxGlobalDisplay();
554 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
556 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
559 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
561 // This is supposed to return desktop dimensions minus any window
562 // manager panels, menus, taskbars, etc. If there is a way to do that
563 // for this platform please fix this function, otherwise it defaults
564 // to the entire desktop.
567 wxDisplaySize(width
, height
);
571 // Configurable display in wxX11 and wxMotif
572 static WXDisplay
*gs_currentDisplay
= NULL
;
573 static wxString gs_displayName
;
575 WXDisplay
*wxGetDisplay()
577 if (gs_currentDisplay
)
578 return gs_currentDisplay
;
580 return wxTheApp
->GetInitialDisplay();
584 bool wxSetDisplay(const wxString
& display_name
)
586 gs_displayName
= display_name
;
588 if ( display_name
.IsEmpty() )
590 gs_currentDisplay
= NULL
;
598 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
599 display_name
.c_str(),
600 wxTheApp
->GetAppName().c_str(),
601 wxTheApp
->GetClassName().c_str(),
603 #if XtSpecificationRelease < 5
612 gs_currentDisplay
= (WXDisplay
*) display
;
620 wxString
wxGetDisplayName()
622 return gs_displayName
;
625 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
627 return wxGenericFindWindowAtPoint(pt
);
630 // ----------------------------------------------------------------------------
631 // keycode translations
632 // ----------------------------------------------------------------------------
634 #include <X11/keysym.h>
636 // FIXME what about tables??
638 int wxCharCodeXToWX(KeySym keySym
)
645 id
= WXK_SHIFT
; break;
648 id
= WXK_CONTROL
; break;
650 id
= WXK_BACK
; break;
652 id
= WXK_DELETE
; break;
654 id
= WXK_CLEAR
; break;
660 id
= WXK_RETURN
; break;
662 id
= WXK_ESCAPE
; break;
665 id
= WXK_PAUSE
; break;
667 id
= WXK_NUMLOCK
; break;
669 id
= WXK_SCROLL
; break;
672 id
= WXK_HOME
; break;
676 id
= WXK_LEFT
; break;
678 id
= WXK_RIGHT
; break;
682 id
= WXK_DOWN
; break;
684 id
= WXK_NEXT
; break;
686 id
= WXK_PRIOR
; break;
688 id
= WXK_MENU
; break;
690 id
= WXK_SELECT
; break;
692 id
= WXK_CANCEL
; break;
694 id
= WXK_PRINT
; break;
696 id
= WXK_EXECUTE
; break;
698 id
= WXK_INSERT
; break;
700 id
= WXK_HELP
; break;
703 id
= WXK_MULTIPLY
; break;
707 id
= WXK_SUBTRACT
; break;
709 id
= WXK_DIVIDE
; break;
711 id
= WXK_DECIMAL
; break;
719 id
= WXK_RETURN
; break;
721 id
= WXK_NUMPAD0
; break;
723 id
= WXK_NUMPAD1
; break;
725 id
= WXK_NUMPAD2
; break;
727 id
= WXK_NUMPAD3
; break;
729 id
= WXK_NUMPAD4
; break;
731 id
= WXK_NUMPAD5
; break;
733 id
= WXK_NUMPAD6
; break;
735 id
= WXK_NUMPAD7
; break;
737 id
= WXK_NUMPAD8
; break;
739 id
= WXK_NUMPAD9
; break;
789 id
= (keySym
<= 255) ? (int)keySym
: -1;
795 KeySym
wxCharCodeWXToX(int id
)
801 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
802 case WXK_BACK
: keySym
= XK_BackSpace
; break;
803 case WXK_TAB
: keySym
= XK_Tab
; break;
804 case WXK_CLEAR
: keySym
= XK_Clear
; break;
805 case WXK_RETURN
: keySym
= XK_Return
; break;
806 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
807 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
808 case WXK_MENU
: keySym
= XK_Menu
; break;
809 case WXK_PAUSE
: keySym
= XK_Pause
; break;
810 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
811 case WXK_SPACE
: keySym
= ' '; break;
812 case WXK_PRIOR
: keySym
= XK_Prior
; break;
813 case WXK_NEXT
: keySym
= XK_Next
; break;
814 case WXK_END
: keySym
= XK_End
; break;
815 case WXK_HOME
: keySym
= XK_Home
; break;
816 case WXK_LEFT
: keySym
= XK_Left
; break;
817 case WXK_UP
: keySym
= XK_Up
; break;
818 case WXK_RIGHT
: keySym
= XK_Right
; break;
819 case WXK_DOWN
: keySym
= XK_Down
; break;
820 case WXK_SELECT
: keySym
= XK_Select
; break;
821 case WXK_PRINT
: keySym
= XK_Print
; break;
822 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
823 case WXK_INSERT
: keySym
= XK_Insert
; break;
824 case WXK_DELETE
: keySym
= XK_Delete
; break;
825 case WXK_HELP
: keySym
= XK_Help
; break;
826 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
827 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
828 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
829 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
830 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
831 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
832 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
833 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
834 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
835 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
836 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
837 case WXK_ADD
: keySym
= XK_KP_Add
; break;
838 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
839 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
840 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
841 case WXK_F1
: keySym
= XK_F1
; break;
842 case WXK_F2
: keySym
= XK_F2
; break;
843 case WXK_F3
: keySym
= XK_F3
; break;
844 case WXK_F4
: keySym
= XK_F4
; break;
845 case WXK_F5
: keySym
= XK_F5
; break;
846 case WXK_F6
: keySym
= XK_F6
; break;
847 case WXK_F7
: keySym
= XK_F7
; break;
848 case WXK_F8
: keySym
= XK_F8
; break;
849 case WXK_F9
: keySym
= XK_F9
; break;
850 case WXK_F10
: keySym
= XK_F10
; break;
851 case WXK_F11
: keySym
= XK_F11
; break;
852 case WXK_F12
: keySym
= XK_F12
; break;
853 case WXK_F13
: keySym
= XK_F13
; break;
854 case WXK_F14
: keySym
= XK_F14
; break;
855 case WXK_F15
: keySym
= XK_F15
; break;
856 case WXK_F16
: keySym
= XK_F16
; break;
857 case WXK_F17
: keySym
= XK_F17
; break;
858 case WXK_F18
: keySym
= XK_F18
; break;
859 case WXK_F19
: keySym
= XK_F19
; break;
860 case WXK_F20
: keySym
= XK_F20
; break;
861 case WXK_F21
: keySym
= XK_F21
; break;
862 case WXK_F22
: keySym
= XK_F22
; break;
863 case WXK_F23
: keySym
= XK_F23
; break;
864 case WXK_F24
: keySym
= XK_F24
; break;
865 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
866 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
867 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
873 // ----------------------------------------------------------------------------
874 // Some colour manipulation routines
875 // ----------------------------------------------------------------------------
877 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
882 int r
= 0, g
= 0, b
= 0;
885 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
886 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
888 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
891 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
892 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
893 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
896 case 0: r
= v
, g
= t
, b
= p
; break;
897 case 1: r
= q
, g
= v
, b
= p
; break;
898 case 2: r
= p
, g
= v
, b
= t
; break;
899 case 3: r
= p
, g
= q
, b
= v
; break;
900 case 4: r
= t
, g
= p
, b
= v
; break;
901 case 5: r
= v
, g
= p
, b
= q
; break;
908 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
910 int r
= rgb
->red
>> 8;
911 int g
= rgb
->green
>> 8;
912 int b
= rgb
->blue
>> 8;
913 int maxv
= wxMax3(r
, g
, b
);
914 int minv
= wxMin3(r
, g
, b
);
917 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
922 int rc
, gc
, bc
, hex
= 0;
923 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
924 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
925 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
926 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
927 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
928 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
929 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
933 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
934 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
937 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
942 int screen
= DefaultScreen(d
);
943 int num_colors
= DisplayCells(d
,screen
);
945 XColor
*color_defs
= new XColor
[num_colors
];
946 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
947 XQueryColors(d
,cmp
,color_defs
,num_colors
);
950 wxXColorToHSV(&hsv
,xc
);
952 int diff
, min_diff
= 0, pixel
= 0;
954 for(llp
= 0;llp
< num_colors
;llp
++)
956 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
957 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
958 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
959 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
960 if (llp
== 0) min_diff
= diff
;
961 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
962 if (min_diff
== 0) break;
965 xc
-> red
= color_defs
[pixel
].red
;
966 xc
-> green
= color_defs
[pixel
].green
;
967 xc
-> blue
= color_defs
[pixel
].blue
;
968 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
971 if (!XAllocColor(d,cmp,xc))
972 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
979 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
981 if (!XAllocColor(d
,cmp
,xc
))
983 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
984 wxAllocNearestColor(d
,cmp
,xc
);
989 wxString
wxGetXEventName(XEvent
& event
)
992 wxString
str(wxT("(some event)"));
995 int type
= event
.xany
.type
;
996 static char* event_name
[] = {
997 "", "unknown(-)", // 0-1
998 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
999 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1000 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1001 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1002 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1003 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1004 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1005 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1006 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1007 "ClientMessage", "MappingNotify", // 33-34
1008 "unknown(+)"}; // 35
1009 type
= wxMin(35, type
); type
= wxMax(1, type
);
1010 wxString
str(event_name
[type
]);
1016 // ----------------------------------------------------------------------------
1018 // ----------------------------------------------------------------------------
1020 // Find the letter corresponding to the mnemonic, for Motif
1021 char wxFindMnemonic (const char *s
)
1024 int len
= strlen (s
);
1027 for (i
= 0; i
< len
; i
++)
1031 // Carefully handle &&
1032 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1044 char* wxFindAccelerator( const char *s
)
1047 // VZ: this function returns incorrect keysym which completely breaks kbd
1051 // The accelerator text is after the \t char.
1052 s
= strchr( s
, '\t' );
1054 if( !s
) return NULL
;
1057 Now we need to format it as X standard:
1062 Ctrl+N --> Ctrl<Key>N
1063 Alt+k --> Meta<Key>k
1064 Ctrl+Shift+A --> Ctrl Shift<Key>A
1066 and handle Ctrl-N & similia
1069 static char buf
[256];
1072 wxString tmp
= s
+ 1; // skip TAB
1075 while( index
< tmp
.length() )
1077 size_t plus
= tmp
.find( '+', index
);
1078 size_t minus
= tmp
.find( '-', index
);
1080 // neither '+' nor '-', add <Key>
1081 if( plus
== wxString::npos
&& minus
== wxString::npos
)
1083 strcat( buf
, "<Key>" );
1084 strcat( buf
, tmp
.c_str() + index
);
1089 // OK: npos is big and positive
1090 size_t sep
= wxMin( plus
, minus
);
1091 wxString mod
= tmp
.substr( index
, sep
- index
);
1102 strcat( buf
, mod
.c_str() );
1111 XmString
wxFindAcceleratorText (const char *s
)
1114 // VZ: this function returns incorrect keysym which completely breaks kbd
1118 // The accelerator text is after the \t char.
1119 s
= strchr( s
, '\t' );
1121 if( !s
) return NULL
;
1123 return wxStringToXmString( s
+ 1 ); // skip TAB!
1127 // Change a widget's foreground and background colours.
1128 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1130 // When should we specify the foreground, if it's calculated
1131 // by wxComputeColours?
1132 // Solution: say we start with the default (computed) foreground colour.
1133 // If we call SetForegroundColour explicitly for a control or window,
1134 // then the foreground is changed.
1135 // Therefore SetBackgroundColour computes the foreground colour, and
1136 // SetForegroundColour changes the foreground colour. The ordering is
1139 XtVaSetValues ((Widget
) widget
,
1140 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1144 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1146 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1149 XtVaSetValues ((Widget
) widget
,
1150 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1151 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1152 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1153 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1156 if (changeArmColour
)
1157 XtVaSetValues ((Widget
) widget
,
1158 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1162 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1164 // Lesstif 0.87 hangs here, but 0.93 does not
1165 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1166 Widget w
= (Widget
)widget
;
1168 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1174 wxString
wxXmStringToString( const XmString
& xmString
)
1177 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1184 return wxEmptyString
;
1187 XmString
wxStringToXmString( const wxString
& str
)
1189 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1192 XmString
wxStringToXmString( const char* str
)
1194 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1197 // ----------------------------------------------------------------------------
1198 // wxBitmap utility functions
1199 // ----------------------------------------------------------------------------
1201 // Creates a bitmap with transparent areas drawn in
1202 // the given colour.
1203 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1205 wxBitmap
newBitmap(bitmap
.GetWidth(),
1211 srcDC
.SelectObject(bitmap
);
1212 destDC
.SelectObject(newBitmap
);
1214 wxBrush
brush(colour
, wxSOLID
);
1215 // destDC.SetOptimization(FALSE);
1216 destDC
.SetBackground(brush
);
1218 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1219 &srcDC
, 0, 0, wxCOPY
, TRUE
);
1224 // ----------------------------------------------------------------------------
1225 // Miscellaneous functions
1226 // ----------------------------------------------------------------------------
1228 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
1230 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
1232 if (style
& wxSIMPLE_BORDER
)
1234 borderWidget
= XtVaCreateManagedWidget
1237 xmFrameWidgetClass
, parentWidget
,
1238 XmNshadowType
, XmSHADOW_ETCHED_IN
,
1239 XmNshadowThickness
, 1,
1243 else if (style
& wxSUNKEN_BORDER
)
1245 borderWidget
= XtVaCreateManagedWidget
1248 xmFrameWidgetClass
, parentWidget
,
1249 XmNshadowType
, XmSHADOW_IN
,
1253 else if (style
& wxRAISED_BORDER
)
1255 borderWidget
= XtVaCreateManagedWidget
1258 xmFrameWidgetClass
, parentWidget
,
1259 XmNshadowType
, XmSHADOW_OUT
,
1264 return borderWidget
;