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"
43 #include "wx/motif/private.h"
46 #include "X11/Xresource.h"
49 #include "X11/Xutil.h"
52 #pragma message enable nosimpint
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Yuck this is really BOTH site and platform dependent
60 // so we should use some other strategy!
62 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
64 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
68 static char *GetIniFile (char *dest
, const char *filename
);
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // async event processing
77 // ----------------------------------------------------------------------------
79 // Consume all events until no more left
80 void wxFlushEvents(WXDisplay
* wxdisplay
)
82 Display
*display
= (Display
*)wxdisplay
;
85 XSync (display
, FALSE
);
87 while (evtLoop
.Pending())
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
101 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
103 wxHandleProcessTermination(proc_data
);
105 // VZ: I think they should be the same...
106 wxASSERT( (int)*id
== proc_data
->tag
);
111 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
113 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
115 (XtPointer
*) XtInputReadMask
,
116 (XtInputCallbackProc
) xt_notify_end_process
,
117 (XtPointer
) proc_data
);
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
129 // Use current setting for the bell
130 XBell (wxGlobalDisplay(), 0);
133 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
136 // This code is WRONG!! Does NOT return the
137 // Motif version of the libs but the X protocol
139 Display
*display
= wxGlobalDisplay();
141 *majorVsn
= ProtocolVersion (display
);
143 *minorVsn
= ProtocolRevision (display
);
148 // ----------------------------------------------------------------------------
149 // Reading and writing resources (eg WIN.INI, .Xdefaults)
150 // ----------------------------------------------------------------------------
154 // Read $HOME for what it says is home, if not
155 // read $USER or $LOGNAME for user name else determine
156 // the Real User, then determine the Real home dir.
157 static char * GetIniFile (char *dest
, const char *filename
)
160 if (filename
&& wxIsAbsolutePath(filename
))
162 strcpy(dest
, filename
);
164 else if ((home
= wxGetUserHome("")) != NULL
)
167 if (dest
[strlen(dest
) - 1] != '/')
169 if (filename
== NULL
)
171 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
172 filename
= ".Xdefaults";
174 else if (*filename
!= '.')
176 strcat (dest
, filename
);
184 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
186 if (create
&& wxFileExists (name
) ) {
188 return buf
; // Exists so ...
194 // Put in standard place for resource files if not absolute
195 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
197 strcat (buf
, wxFileNameFromPath (name
).c_str());
201 // Touch the file to create it
202 FILE *fd
= fopen (buf
, "w");
209 * We have a cache for writing different resource files,
210 * which will only get flushed when we call wxFlushResources().
211 * Build up a list of resource databases waiting to be written.
215 wxList
wxResourceCache (wxKEY_STRING
);
218 wxFlushResources (void)
220 char nameBuffer
[512];
222 wxNode
*node
= wxResourceCache
.First ();
225 const char *file
= node
->GetKeyString();
226 // If file doesn't exist, create it first.
227 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
229 XrmDatabase database
= (XrmDatabase
) node
->Data ();
230 XrmPutFileDatabase (database
, nameBuffer
);
231 XrmDestroyDatabase (database
);
232 wxNode
*next
= node
->Next ();
238 static XrmDatabase wxResourceDatabase
= 0;
240 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
242 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
246 (void) GetIniFile (buffer
, file
);
248 XrmDatabase database
;
249 wxNode
*node
= wxResourceCache
.Find (buffer
);
251 database
= (XrmDatabase
) node
->Data ();
254 database
= XrmGetFileDatabase (buffer
);
255 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
259 strcpy (resName
, section
.c_str());
260 strcat (resName
, ".");
261 strcat (resName
, entry
.c_str());
263 XrmPutStringResource (&database
, resName
, value
);
267 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
270 sprintf(buf
, "%.4f", value
);
271 return wxWriteResource(section
, entry
, buf
, file
);
274 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
277 sprintf(buf
, "%ld", value
);
278 return wxWriteResource(section
, entry
, buf
, file
);
281 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
284 sprintf(buf
, "%d", value
);
285 return wxWriteResource(section
, entry
, buf
, file
);
288 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
290 if (!wxResourceDatabase
)
292 Display
*display
= wxGlobalDisplay();
293 wxXMergeDatabases (wxTheApp
, display
);
296 XrmDatabase database
;
302 // Is this right? Trying to get it to look in the user's
303 // home directory instead of current directory -- JACS
304 (void) GetIniFile (buffer
, file
);
306 wxNode
*node
= wxResourceCache
.Find (buffer
);
308 database
= (XrmDatabase
) node
->Data ();
311 database
= XrmGetFileDatabase (buffer
);
312 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
316 database
= wxResourceDatabase
;
321 strcpy (buf
, section
);
325 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
327 // Try different combinations of upper/lower case, just in case...
330 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
331 success
= XrmGetResource (database
, buf
, "*", str_type
,
339 *value
= new char[xvalue
.size
+ 1];
340 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
346 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
349 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
352 *value
= (float)strtod(s
, NULL
);
359 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
362 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
365 *value
= strtol(s
, NULL
, 10);
372 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
375 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
378 // Handle True, False here
379 // True, Yes, Enables, Set or Activated
380 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
382 // False, No, Disabled, Reset, Cleared, Deactivated
383 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
387 *value
= (int) strtol (s
, NULL
, 10);
395 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
397 XrmDatabase homeDB
, serverDB
, applicationDB
;
398 char filenamebuf
[1024];
400 char *filename
= &filenamebuf
[0];
402 wxString classname
= theApp
->GetClassName();
404 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
405 (void) strcat (name
, classname
.c_str());
407 /* Get application defaults file, if any */
408 applicationDB
= XrmGetFileDatabase (name
);
409 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
411 /* Merge server defaults, created by xrdb, loaded as a property of the root
412 * window when the server initializes and loaded into the display
413 * structure on XOpenDisplay;
414 * if not defined, use .Xdefaults
417 if (XResourceManagerString (display
) != NULL
)
419 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
423 (void) GetIniFile (filename
, NULL
);
424 serverDB
= XrmGetFileDatabase (filename
);
426 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
428 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
429 * and merge into existing database
432 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
435 environment
= GetIniFile (filename
, NULL
);
436 len
= strlen (environment
);
437 wxString hostname
= wxGetHostName();
439 strncat(environment
, hostname
, 1024 - len
);
441 homeDB
= XrmGetFileDatabase (environment
);
442 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
448 * Not yet used but may be useful.
452 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
455 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
457 XrmDatabase rdb
= NULL
; // A resource data base
459 // Create an empty resource database
460 rdb
= XrmGetStringDatabase ("");
462 // Add the Component resources, prepending the name of the component
465 while (resourceSpec
[i
] != NULL
)
469 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
470 XrmPutLineResource (&rdb
, buf
);
473 // Merge them into the Xt database, with lowest precendence
477 #if (XlibSpecificationRelease>=5)
478 XrmDatabase db
= XtDatabase (dpy
);
479 XrmCombineDatabase (rdb
, &db
, FALSE
);
481 XrmMergeDatabases (dpy
->db
, &rdb
);
489 #endif // wxUSE_RESOURCES
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 void wxGetMousePosition( int* x
, int* y
)
504 XQueryPointer(wxGlobalDisplay(),
505 DefaultRootWindow(wxGlobalDisplay()),
507 &(xev
.x_root
), &(xev
.y_root
),
515 // Return TRUE if we have a colour display
516 bool wxColourDisplay()
518 return wxDisplayDepth() > 1;
521 // Returns depth of screen
524 Display
*dpy
= wxGlobalDisplay();
526 return DefaultDepth (dpy
, DefaultScreen (dpy
));
529 // Get size of display
530 void wxDisplaySize(int *width
, int *height
)
532 Display
*dpy
= wxGlobalDisplay();
535 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
537 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
540 void wxDisplaySizeMM(int *width
, int *height
)
542 Display
*dpy
= wxGlobalDisplay();
545 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
547 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
550 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
552 // This is supposed to return desktop dimensions minus any window
553 // manager panels, menus, taskbars, etc. If there is a way to do that
554 // for this platform please fix this function, otherwise it defaults
555 // to the entire desktop.
558 wxDisplaySize(width
, height
);
562 // Configurable display in wxX11 and wxMotif
563 static WXDisplay
*gs_currentDisplay
= NULL
;
564 static wxString gs_displayName
;
566 WXDisplay
*wxGetDisplay()
568 if (gs_currentDisplay
)
569 return gs_currentDisplay
;
571 return wxTheApp
->GetInitialDisplay();
575 bool wxSetDisplay(const wxString
& display_name
)
577 gs_displayName
= display_name
;
579 if ( display_name
.IsEmpty() )
581 gs_currentDisplay
= NULL
;
589 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
590 display_name
.c_str(),
591 wxTheApp
->GetAppName().c_str(),
592 wxTheApp
->GetClassName().c_str(),
594 #if XtSpecificationRelease < 5
603 gs_currentDisplay
= (WXDisplay
*) display
;
611 wxString
wxGetDisplayName()
613 return gs_displayName
;
616 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
618 return wxGenericFindWindowAtPoint(pt
);
621 // ----------------------------------------------------------------------------
622 // keycode translations
623 // ----------------------------------------------------------------------------
625 #include <X11/keysym.h>
627 // FIXME what about tables??
629 int wxCharCodeXToWX(KeySym keySym
)
636 id
= WXK_SHIFT
; break;
639 id
= WXK_CONTROL
; break;
641 id
= WXK_BACK
; break;
643 id
= WXK_DELETE
; break;
645 id
= WXK_CLEAR
; break;
651 id
= WXK_RETURN
; break;
653 id
= WXK_ESCAPE
; break;
656 id
= WXK_PAUSE
; break;
658 id
= WXK_NUMLOCK
; break;
660 id
= WXK_SCROLL
; break;
663 id
= WXK_HOME
; break;
667 id
= WXK_LEFT
; break;
669 id
= WXK_RIGHT
; break;
673 id
= WXK_DOWN
; break;
675 id
= WXK_NEXT
; break;
677 id
= WXK_PRIOR
; break;
679 id
= WXK_MENU
; break;
681 id
= WXK_SELECT
; break;
683 id
= WXK_CANCEL
; break;
685 id
= WXK_PRINT
; break;
687 id
= WXK_EXECUTE
; break;
689 id
= WXK_INSERT
; break;
691 id
= WXK_HELP
; break;
694 id
= WXK_MULTIPLY
; break;
698 id
= WXK_SUBTRACT
; break;
700 id
= WXK_DIVIDE
; break;
702 id
= WXK_DECIMAL
; break;
710 id
= WXK_RETURN
; break;
712 id
= WXK_NUMPAD0
; break;
714 id
= WXK_NUMPAD1
; break;
716 id
= WXK_NUMPAD2
; break;
718 id
= WXK_NUMPAD3
; break;
720 id
= WXK_NUMPAD4
; break;
722 id
= WXK_NUMPAD5
; break;
724 id
= WXK_NUMPAD6
; break;
726 id
= WXK_NUMPAD7
; break;
728 id
= WXK_NUMPAD8
; break;
730 id
= WXK_NUMPAD9
; break;
780 id
= (keySym
<= 255) ? (int)keySym
: -1;
786 KeySym
wxCharCodeWXToX(int id
)
792 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
793 case WXK_BACK
: keySym
= XK_BackSpace
; break;
794 case WXK_TAB
: keySym
= XK_Tab
; break;
795 case WXK_CLEAR
: keySym
= XK_Clear
; break;
796 case WXK_RETURN
: keySym
= XK_Return
; break;
797 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
798 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
799 case WXK_MENU
: keySym
= XK_Menu
; break;
800 case WXK_PAUSE
: keySym
= XK_Pause
; break;
801 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
802 case WXK_SPACE
: keySym
= ' '; break;
803 case WXK_PRIOR
: keySym
= XK_Prior
; break;
804 case WXK_NEXT
: keySym
= XK_Next
; break;
805 case WXK_END
: keySym
= XK_End
; break;
806 case WXK_HOME
: keySym
= XK_Home
; break;
807 case WXK_LEFT
: keySym
= XK_Left
; break;
808 case WXK_UP
: keySym
= XK_Up
; break;
809 case WXK_RIGHT
: keySym
= XK_Right
; break;
810 case WXK_DOWN
: keySym
= XK_Down
; break;
811 case WXK_SELECT
: keySym
= XK_Select
; break;
812 case WXK_PRINT
: keySym
= XK_Print
; break;
813 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
814 case WXK_INSERT
: keySym
= XK_Insert
; break;
815 case WXK_DELETE
: keySym
= XK_Delete
; break;
816 case WXK_HELP
: keySym
= XK_Help
; break;
817 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
818 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
819 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
820 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
821 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
822 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
823 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
824 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
825 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
826 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
827 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
828 case WXK_ADD
: keySym
= XK_KP_Add
; break;
829 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
830 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
831 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
832 case WXK_F1
: keySym
= XK_F1
; break;
833 case WXK_F2
: keySym
= XK_F2
; break;
834 case WXK_F3
: keySym
= XK_F3
; break;
835 case WXK_F4
: keySym
= XK_F4
; break;
836 case WXK_F5
: keySym
= XK_F5
; break;
837 case WXK_F6
: keySym
= XK_F6
; break;
838 case WXK_F7
: keySym
= XK_F7
; break;
839 case WXK_F8
: keySym
= XK_F8
; break;
840 case WXK_F9
: keySym
= XK_F9
; break;
841 case WXK_F10
: keySym
= XK_F10
; break;
842 case WXK_F11
: keySym
= XK_F11
; break;
843 case WXK_F12
: keySym
= XK_F12
; break;
844 case WXK_F13
: keySym
= XK_F13
; break;
845 case WXK_F14
: keySym
= XK_F14
; break;
846 case WXK_F15
: keySym
= XK_F15
; break;
847 case WXK_F16
: keySym
= XK_F16
; break;
848 case WXK_F17
: keySym
= XK_F17
; break;
849 case WXK_F18
: keySym
= XK_F18
; break;
850 case WXK_F19
: keySym
= XK_F19
; break;
851 case WXK_F20
: keySym
= XK_F20
; break;
852 case WXK_F21
: keySym
= XK_F21
; break;
853 case WXK_F22
: keySym
= XK_F22
; break;
854 case WXK_F23
: keySym
= XK_F23
; break;
855 case WXK_F24
: keySym
= XK_F24
; break;
856 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
857 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
858 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
864 // ----------------------------------------------------------------------------
865 // Some colour manipulation routines
866 // ----------------------------------------------------------------------------
868 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
873 int r
= 0, g
= 0, b
= 0;
876 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
877 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
879 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
882 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
883 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
884 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
887 case 0: r
= v
, g
= t
, b
= p
; break;
888 case 1: r
= q
, g
= v
, b
= p
; break;
889 case 2: r
= p
, g
= v
, b
= t
; break;
890 case 3: r
= p
, g
= q
, b
= v
; break;
891 case 4: r
= t
, g
= p
, b
= v
; break;
892 case 5: r
= v
, g
= p
, b
= q
; break;
899 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
901 int r
= rgb
->red
>> 8;
902 int g
= rgb
->green
>> 8;
903 int b
= rgb
->blue
>> 8;
904 int maxv
= wxMax3(r
, g
, b
);
905 int minv
= wxMin3(r
, g
, b
);
908 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
913 int rc
, gc
, bc
, hex
= 0;
914 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
915 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
916 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
917 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
918 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
919 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
920 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
924 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
925 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
928 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
933 int screen
= DefaultScreen(d
);
934 int num_colors
= DisplayCells(d
,screen
);
936 XColor
*color_defs
= new XColor
[num_colors
];
937 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
938 XQueryColors(d
,cmp
,color_defs
,num_colors
);
941 wxXColorToHSV(&hsv
,xc
);
943 int diff
, min_diff
= 0, pixel
= 0;
945 for(llp
= 0;llp
< num_colors
;llp
++)
947 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
948 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
949 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
950 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
951 if (llp
== 0) min_diff
= diff
;
952 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
953 if (min_diff
== 0) break;
956 xc
-> red
= color_defs
[pixel
].red
;
957 xc
-> green
= color_defs
[pixel
].green
;
958 xc
-> blue
= color_defs
[pixel
].blue
;
959 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
962 if (!XAllocColor(d,cmp,xc))
963 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
970 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
972 if (!XAllocColor(d
,cmp
,xc
))
974 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
975 wxAllocNearestColor(d
,cmp
,xc
);
980 wxString
wxGetXEventName(XEvent
& event
)
983 wxString
str(wxT("(some event)"));
986 int type
= event
.xany
.type
;
987 static char* event_name
[] = {
988 "", "unknown(-)", // 0-1
989 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
990 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
991 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
992 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
993 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
994 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
995 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
996 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
997 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
998 "ClientMessage", "MappingNotify", // 33-34
1000 type
= wxMin(35, type
); type
= wxMax(1, type
);
1001 wxString
str(event_name
[type
]);
1007 // ----------------------------------------------------------------------------
1009 // ----------------------------------------------------------------------------
1011 // Find the letter corresponding to the mnemonic, for Motif
1012 char wxFindMnemonic (const char *s
)
1015 int len
= strlen (s
);
1018 for (i
= 0; i
< len
; i
++)
1022 // Carefully handle &&
1023 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1035 char* wxFindAccelerator( const char *s
)
1038 // VZ: this function returns incorrect keysym which completely breaks kbd
1042 // The accelerator text is after the \t char.
1043 s
= strchr( s
, '\t' );
1045 if( !s
) return NULL
;
1048 Now we need to format it as X standard:
1053 Ctrl+N --> Ctrl<Key>N
1054 Alt+k --> Meta<Key>k
1055 Ctrl+Shift+A --> Ctrl Shift<Key>A
1057 and handle Ctrl-N & similia
1060 static char buf
[256];
1063 wxString tmp
= s
+ 1; // skip TAB
1066 while( index
< tmp
.length() )
1068 size_t plus
= tmp
.find( '+', index
);
1069 size_t minus
= tmp
.find( '-', index
);
1071 // neither '+' nor '-', add <Key>
1072 if( plus
== wxString::npos
&& minus
== wxString::npos
)
1074 strcat( buf
, "<Key>" );
1075 strcat( buf
, tmp
.c_str() + index
);
1080 // OK: npos is big and positive
1081 size_t sep
= wxMin( plus
, minus
);
1082 wxString mod
= tmp
.substr( index
, sep
- index
);
1093 strcat( buf
, mod
.c_str() );
1102 XmString
wxFindAcceleratorText (const char *s
)
1105 // VZ: this function returns incorrect keysym which completely breaks kbd
1109 // The accelerator text is after the \t char.
1110 s
= strchr( s
, '\t' );
1112 if( !s
) return NULL
;
1114 return wxStringToXmString( s
+ 1 ); // skip TAB!
1118 // Change a widget's foreground and background colours.
1119 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1121 // When should we specify the foreground, if it's calculated
1122 // by wxComputeColours?
1123 // Solution: say we start with the default (computed) foreground colour.
1124 // If we call SetForegroundColour explicitly for a control or window,
1125 // then the foreground is changed.
1126 // Therefore SetBackgroundColour computes the foreground colour, and
1127 // SetForegroundColour changes the foreground colour. The ordering is
1130 XtVaSetValues ((Widget
) widget
,
1131 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1135 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1137 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1140 XtVaSetValues ((Widget
) widget
,
1141 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1142 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1143 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1144 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1147 if (changeArmColour
)
1148 XtVaSetValues ((Widget
) widget
,
1149 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1153 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1155 // Lesstif 0.87 hangs here, but 0.93 does not
1156 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1157 Widget w
= (Widget
)widget
;
1159 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1165 wxString
wxXmStringToString( const XmString
& xmString
)
1168 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1175 return wxEmptyString
;
1178 XmString
wxStringToXmString( const wxString
& str
)
1180 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1183 XmString
wxStringToXmString( const char* str
)
1185 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1188 // ----------------------------------------------------------------------------
1189 // wxBitmap utility functions
1190 // ----------------------------------------------------------------------------
1192 // Creates a bitmap with transparent areas drawn in
1193 // the given colour.
1194 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1196 wxBitmap
newBitmap(bitmap
.GetWidth(),
1202 srcDC
.SelectObject(bitmap
);
1203 destDC
.SelectObject(newBitmap
);
1205 wxBrush
brush(colour
, wxSOLID
);
1206 // destDC.SetOptimization(FALSE);
1207 destDC
.SetBackground(brush
);
1209 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1210 &srcDC
, 0, 0, wxCOPY
, TRUE
);