1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #define XtDisplay XTDISPLAY
29 #include "wx/apptrait.h"
31 #include "wx/dcmemory.h"
32 #include "wx/bitmap.h"
33 #include "wx/evtloop.h"
37 #if (defined(__SUNCC__) || defined(__CLCC__))
42 #pragma message disable nosimpint
45 #include "wx/unix/execute.h"
50 #include "wx/motif/private.h"
53 #include "X11/Xresource.h"
56 #include "X11/Xutil.h"
59 #pragma message enable nosimpint
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // Yuck this is really BOTH site and platform dependent
67 // so we should use some other strategy!
69 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
71 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
75 static char *GetIniFile (char *dest
, const char *filename
);
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
83 // async event processing
84 // ----------------------------------------------------------------------------
86 // Consume all events until no more left
87 void wxFlushEvents(WXDisplay
* wxdisplay
)
89 Display
*display
= (Display
*)wxdisplay
;
92 XSync (display
, FALSE
);
94 while (evtLoop
.Pending())
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
108 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
110 wxHandleProcessTermination(proc_data
);
112 // VZ: I think they should be the same...
113 wxASSERT( (int)*id
== proc_data
->tag
);
118 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
120 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
122 (XtPointer
*) XtInputReadMask
,
123 (XtInputCallbackProc
) xt_notify_end_process
,
124 (XtPointer
) proc_data
);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
135 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
138 // Use current setting for the bell
139 XBell (wxGlobalDisplay(), 0);
143 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
145 static wxToolkitInfo info
;
147 info
.shortName
= _T("motif");
148 info
.name
= _T("wxMotif");
149 #ifdef __WXUNIVERSAL__
150 info
.shortName
<< _T("univ");
151 info
.name
<< _T("/wxUniversal");
154 // This code is WRONG!! Does NOT return the
155 // Motif version of the libs but the X protocol
157 Display
*display
= wxGlobalDisplay();
158 info
.versionMajor
= ProtocolVersion (display
);
159 info
.versionMinor
= ProtocolRevision (display
);
164 // ----------------------------------------------------------------------------
165 // Reading and writing resources (eg WIN.INI, .Xdefaults)
166 // ----------------------------------------------------------------------------
170 // Read $HOME for what it says is home, if not
171 // read $USER or $LOGNAME for user name else determine
172 // the Real User, then determine the Real home dir.
173 static char * GetIniFile (char *dest
, const char *filename
)
176 if (filename
&& wxIsAbsolutePath(filename
))
178 strcpy(dest
, filename
);
180 else if ((home
= wxGetUserHome("")) != NULL
)
183 if (dest
[strlen(dest
) - 1] != '/')
185 if (filename
== NULL
)
187 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
188 filename
= ".Xdefaults";
190 else if (*filename
!= '.')
192 strcat (dest
, filename
);
200 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
202 if (create
&& wxFileExists (name
) ) {
204 return buf
; // Exists so ...
210 // Put in standard place for resource files if not absolute
211 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
213 strcat (buf
, wxFileNameFromPath (name
).c_str());
217 // Touch the file to create it
218 FILE *fd
= fopen (buf
, "w");
225 * We have a cache for writing different resource files,
226 * which will only get flushed when we call wxFlushResources().
227 * Build up a list of resource databases waiting to be written.
231 wxList
wxResourceCache (wxKEY_STRING
);
234 wxFlushResources (void)
236 char nameBuffer
[512];
238 wxNode
*node
= wxResourceCache
.First ();
241 const char *file
= node
->GetKeyString();
242 // If file doesn't exist, create it first.
243 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
245 XrmDatabase database
= (XrmDatabase
) node
->Data ();
246 XrmPutFileDatabase (database
, nameBuffer
);
247 XrmDestroyDatabase (database
);
248 wxNode
*next
= node
->Next ();
254 static XrmDatabase wxResourceDatabase
= 0;
256 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
258 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
262 (void) GetIniFile (buffer
, file
);
264 XrmDatabase database
;
265 wxNode
*node
= wxResourceCache
.Find (buffer
);
267 database
= (XrmDatabase
) node
->Data ();
270 database
= XrmGetFileDatabase (buffer
);
271 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
275 strcpy (resName
, section
.c_str());
276 strcat (resName
, ".");
277 strcat (resName
, entry
.c_str());
279 XrmPutStringResource (&database
, resName
, value
);
283 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
286 sprintf(buf
, "%.4f", value
);
287 return wxWriteResource(section
, entry
, buf
, file
);
290 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
293 sprintf(buf
, "%ld", value
);
294 return wxWriteResource(section
, entry
, buf
, file
);
297 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
300 sprintf(buf
, "%d", value
);
301 return wxWriteResource(section
, entry
, buf
, file
);
304 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
306 if (!wxResourceDatabase
)
308 Display
*display
= wxGlobalDisplay();
309 wxXMergeDatabases (wxTheApp
, display
);
312 XrmDatabase database
;
318 // Is this right? Trying to get it to look in the user's
319 // home directory instead of current directory -- JACS
320 (void) GetIniFile (buffer
, file
);
322 wxNode
*node
= wxResourceCache
.Find (buffer
);
324 database
= (XrmDatabase
) node
->Data ();
327 database
= XrmGetFileDatabase (buffer
);
328 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
332 database
= wxResourceDatabase
;
337 strcpy (buf
, section
);
341 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
343 // Try different combinations of upper/lower case, just in case...
346 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
347 success
= XrmGetResource (database
, buf
, "*", str_type
,
355 *value
= new char[xvalue
.size
+ 1];
356 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
362 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
365 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
368 *value
= (float)strtod(s
, NULL
);
375 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
378 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
381 *value
= strtol(s
, NULL
, 10);
388 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
391 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
394 // Handle True, False here
395 // True, Yes, Enables, Set or Activated
396 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
398 // False, No, Disabled, Reset, Cleared, Deactivated
399 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
403 *value
= (int) strtol (s
, NULL
, 10);
411 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
413 XrmDatabase homeDB
, serverDB
, applicationDB
;
414 char filenamebuf
[1024];
416 char *filename
= &filenamebuf
[0];
418 wxString classname
= theApp
->GetClassName();
420 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
421 (void) strcat (name
, classname
.c_str());
423 /* Get application defaults file, if any */
424 applicationDB
= XrmGetFileDatabase (name
);
425 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
427 /* Merge server defaults, created by xrdb, loaded as a property of the root
428 * window when the server initializes and loaded into the display
429 * structure on XOpenDisplay;
430 * if not defined, use .Xdefaults
433 if (XResourceManagerString (display
) != NULL
)
435 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
439 (void) GetIniFile (filename
, NULL
);
440 serverDB
= XrmGetFileDatabase (filename
);
442 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
444 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
445 * and merge into existing database
448 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
451 environment
= GetIniFile (filename
, NULL
);
452 len
= strlen (environment
);
453 wxString hostname
= wxGetHostName();
455 strncat(environment
, hostname
, 1024 - len
);
457 homeDB
= XrmGetFileDatabase (environment
);
458 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
464 * Not yet used but may be useful.
468 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
471 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
473 XrmDatabase rdb
= NULL
; // A resource data base
475 // Create an empty resource database
476 rdb
= XrmGetStringDatabase ("");
478 // Add the Component resources, prepending the name of the component
481 while (resourceSpec
[i
] != NULL
)
485 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
486 XrmPutLineResource (&rdb
, buf
);
489 // Merge them into the Xt database, with lowest precendence
493 #if (XlibSpecificationRelease>=5)
494 XrmDatabase db
= XtDatabase (dpy
);
495 XrmCombineDatabase (rdb
, &db
, FALSE
);
497 XrmMergeDatabases (dpy
->db
, &rdb
);
505 #endif // wxUSE_RESOURCES
507 // ----------------------------------------------------------------------------
509 // ----------------------------------------------------------------------------
511 void wxGetMousePosition( int* x
, int* y
)
520 XQueryPointer(wxGlobalDisplay(),
521 DefaultRootWindow(wxGlobalDisplay()),
523 &(xev
.x_root
), &(xev
.y_root
),
531 // Return TRUE if we have a colour display
532 bool wxColourDisplay()
534 return wxDisplayDepth() > 1;
537 // Returns depth of screen
540 Display
*dpy
= wxGlobalDisplay();
542 return DefaultDepth (dpy
, DefaultScreen (dpy
));
545 // Get size of display
546 void wxDisplaySize(int *width
, int *height
)
548 Display
*dpy
= wxGlobalDisplay();
551 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
553 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
556 void wxDisplaySizeMM(int *width
, int *height
)
558 Display
*dpy
= wxGlobalDisplay();
561 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
563 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
566 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
568 // This is supposed to return desktop dimensions minus any window
569 // manager panels, menus, taskbars, etc. If there is a way to do that
570 // for this platform please fix this function, otherwise it defaults
571 // to the entire desktop.
574 wxDisplaySize(width
, height
);
578 // Configurable display in wxX11 and wxMotif
579 static WXDisplay
*gs_currentDisplay
= NULL
;
580 static wxString gs_displayName
;
582 WXDisplay
*wxGetDisplay()
584 if (gs_currentDisplay
)
585 return gs_currentDisplay
;
587 return wxTheApp
->GetInitialDisplay();
591 bool wxSetDisplay(const wxString
& display_name
)
593 gs_displayName
= display_name
;
595 if ( display_name
.IsEmpty() )
597 gs_currentDisplay
= NULL
;
605 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
606 display_name
.c_str(),
607 wxTheApp
->GetAppName().c_str(),
608 wxTheApp
->GetClassName().c_str(),
610 #if XtSpecificationRelease < 5
619 gs_currentDisplay
= (WXDisplay
*) display
;
627 wxString
wxGetDisplayName()
629 return gs_displayName
;
632 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
634 return wxGenericFindWindowAtPoint(pt
);
637 // ----------------------------------------------------------------------------
638 // keycode translations
639 // ----------------------------------------------------------------------------
641 #include <X11/keysym.h>
643 // FIXME what about tables??
645 int wxCharCodeXToWX(KeySym keySym
)
652 id
= WXK_SHIFT
; break;
655 id
= WXK_CONTROL
; break;
657 id
= WXK_BACK
; break;
659 id
= WXK_DELETE
; break;
661 id
= WXK_CLEAR
; break;
667 id
= WXK_RETURN
; break;
669 id
= WXK_ESCAPE
; break;
672 id
= WXK_PAUSE
; break;
674 id
= WXK_NUMLOCK
; break;
676 id
= WXK_SCROLL
; break;
679 id
= WXK_HOME
; break;
683 id
= WXK_LEFT
; break;
685 id
= WXK_RIGHT
; break;
689 id
= WXK_DOWN
; break;
691 id
= WXK_NEXT
; break;
693 id
= WXK_PRIOR
; break;
695 id
= WXK_MENU
; break;
697 id
= WXK_SELECT
; break;
699 id
= WXK_CANCEL
; break;
701 id
= WXK_PRINT
; break;
703 id
= WXK_EXECUTE
; break;
705 id
= WXK_INSERT
; break;
707 id
= WXK_HELP
; break;
710 id
= WXK_MULTIPLY
; break;
714 id
= WXK_SUBTRACT
; break;
716 id
= WXK_DIVIDE
; break;
718 id
= WXK_DECIMAL
; break;
726 id
= WXK_RETURN
; break;
728 id
= WXK_NUMPAD0
; break;
730 id
= WXK_NUMPAD1
; break;
732 id
= WXK_NUMPAD2
; break;
734 id
= WXK_NUMPAD3
; break;
736 id
= WXK_NUMPAD4
; break;
738 id
= WXK_NUMPAD5
; break;
740 id
= WXK_NUMPAD6
; break;
742 id
= WXK_NUMPAD7
; break;
744 id
= WXK_NUMPAD8
; break;
746 id
= WXK_NUMPAD9
; break;
796 id
= (keySym
<= 255) ? (int)keySym
: -1;
802 KeySym
wxCharCodeWXToX(int id
)
808 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
809 case WXK_BACK
: keySym
= XK_BackSpace
; break;
810 case WXK_TAB
: keySym
= XK_Tab
; break;
811 case WXK_CLEAR
: keySym
= XK_Clear
; break;
812 case WXK_RETURN
: keySym
= XK_Return
; break;
813 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
814 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
815 case WXK_MENU
: keySym
= XK_Menu
; break;
816 case WXK_PAUSE
: keySym
= XK_Pause
; break;
817 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
818 case WXK_SPACE
: keySym
= ' '; break;
819 case WXK_PRIOR
: keySym
= XK_Prior
; break;
820 case WXK_NEXT
: keySym
= XK_Next
; break;
821 case WXK_END
: keySym
= XK_End
; break;
822 case WXK_HOME
: keySym
= XK_Home
; break;
823 case WXK_LEFT
: keySym
= XK_Left
; break;
824 case WXK_UP
: keySym
= XK_Up
; break;
825 case WXK_RIGHT
: keySym
= XK_Right
; break;
826 case WXK_DOWN
: keySym
= XK_Down
; break;
827 case WXK_SELECT
: keySym
= XK_Select
; break;
828 case WXK_PRINT
: keySym
= XK_Print
; break;
829 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
830 case WXK_INSERT
: keySym
= XK_Insert
; break;
831 case WXK_DELETE
: keySym
= XK_Delete
; break;
832 case WXK_HELP
: keySym
= XK_Help
; break;
833 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
834 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
835 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
836 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
837 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
838 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
839 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
840 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
841 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
842 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
843 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
844 case WXK_ADD
: keySym
= XK_KP_Add
; break;
845 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
846 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
847 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
848 case WXK_F1
: keySym
= XK_F1
; break;
849 case WXK_F2
: keySym
= XK_F2
; break;
850 case WXK_F3
: keySym
= XK_F3
; break;
851 case WXK_F4
: keySym
= XK_F4
; break;
852 case WXK_F5
: keySym
= XK_F5
; break;
853 case WXK_F6
: keySym
= XK_F6
; break;
854 case WXK_F7
: keySym
= XK_F7
; break;
855 case WXK_F8
: keySym
= XK_F8
; break;
856 case WXK_F9
: keySym
= XK_F9
; break;
857 case WXK_F10
: keySym
= XK_F10
; break;
858 case WXK_F11
: keySym
= XK_F11
; break;
859 case WXK_F12
: keySym
= XK_F12
; break;
860 case WXK_F13
: keySym
= XK_F13
; break;
861 case WXK_F14
: keySym
= XK_F14
; break;
862 case WXK_F15
: keySym
= XK_F15
; break;
863 case WXK_F16
: keySym
= XK_F16
; break;
864 case WXK_F17
: keySym
= XK_F17
; break;
865 case WXK_F18
: keySym
= XK_F18
; break;
866 case WXK_F19
: keySym
= XK_F19
; break;
867 case WXK_F20
: keySym
= XK_F20
; break;
868 case WXK_F21
: keySym
= XK_F21
; break;
869 case WXK_F22
: keySym
= XK_F22
; break;
870 case WXK_F23
: keySym
= XK_F23
; break;
871 case WXK_F24
: keySym
= XK_F24
; break;
872 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
873 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
874 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
880 // ----------------------------------------------------------------------------
881 // Some colour manipulation routines
882 // ----------------------------------------------------------------------------
884 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
889 int r
= 0, g
= 0, b
= 0;
892 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
893 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
895 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
898 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
899 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
900 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
903 case 0: r
= v
, g
= t
, b
= p
; break;
904 case 1: r
= q
, g
= v
, b
= p
; break;
905 case 2: r
= p
, g
= v
, b
= t
; break;
906 case 3: r
= p
, g
= q
, b
= v
; break;
907 case 4: r
= t
, g
= p
, b
= v
; break;
908 case 5: r
= v
, g
= p
, b
= q
; break;
915 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
917 int r
= rgb
->red
>> 8;
918 int g
= rgb
->green
>> 8;
919 int b
= rgb
->blue
>> 8;
920 int maxv
= wxMax3(r
, g
, b
);
921 int minv
= wxMin3(r
, g
, b
);
924 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
929 int rc
, gc
, bc
, hex
= 0;
930 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
931 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
932 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
933 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
934 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
935 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
936 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
940 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
941 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
944 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
949 int screen
= DefaultScreen(d
);
950 int num_colors
= DisplayCells(d
,screen
);
952 XColor
*color_defs
= new XColor
[num_colors
];
953 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
954 XQueryColors(d
,cmp
,color_defs
,num_colors
);
957 wxXColorToHSV(&hsv
,xc
);
959 int diff
, min_diff
= 0, pixel
= 0;
961 for(llp
= 0;llp
< num_colors
;llp
++)
963 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
964 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
965 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
966 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
967 if (llp
== 0) min_diff
= diff
;
968 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
969 if (min_diff
== 0) break;
972 xc
-> red
= color_defs
[pixel
].red
;
973 xc
-> green
= color_defs
[pixel
].green
;
974 xc
-> blue
= color_defs
[pixel
].blue
;
975 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
978 if (!XAllocColor(d,cmp,xc))
979 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
986 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
988 if (!XAllocColor(d
,cmp
,xc
))
990 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
991 wxAllocNearestColor(d
,cmp
,xc
);
996 wxString
wxGetXEventName(XEvent
& event
)
999 wxString
str(wxT("(some event)"));
1002 int type
= event
.xany
.type
;
1003 static char* event_name
[] = {
1004 "", "unknown(-)", // 0-1
1005 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1006 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1007 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1008 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1009 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1010 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1011 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1012 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1013 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1014 "ClientMessage", "MappingNotify", // 33-34
1015 "unknown(+)"}; // 35
1016 type
= wxMin(35, type
); type
= wxMax(1, type
);
1017 wxString
str(event_name
[type
]);
1023 // ----------------------------------------------------------------------------
1025 // ----------------------------------------------------------------------------
1027 // Find the letter corresponding to the mnemonic, for Motif
1028 char wxFindMnemonic (const char *s
)
1031 int len
= strlen (s
);
1034 for (i
= 0; i
< len
; i
++)
1038 // Carefully handle &&
1039 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1051 char* wxFindAccelerator( const char *s
)
1054 // VZ: this function returns incorrect keysym which completely breaks kbd
1058 // The accelerator text is after the \t char.
1059 s
= strchr( s
, '\t' );
1061 if( !s
) return NULL
;
1064 Now we need to format it as X standard:
1069 Ctrl+N --> Ctrl<Key>N
1070 Alt+k --> Meta<Key>k
1071 Ctrl+Shift+A --> Ctrl Shift<Key>A
1073 and handle Ctrl-N & similia
1076 static char buf
[256];
1079 wxString tmp
= s
+ 1; // skip TAB
1082 while( index
< tmp
.length() )
1084 size_t plus
= tmp
.find( '+', index
);
1085 size_t minus
= tmp
.find( '-', index
);
1087 // neither '+' nor '-', add <Key>
1088 if( plus
== wxString::npos
&& minus
== wxString::npos
)
1090 strcat( buf
, "<Key>" );
1091 strcat( buf
, tmp
.c_str() + index
);
1096 // OK: npos is big and positive
1097 size_t sep
= wxMin( plus
, minus
);
1098 wxString mod
= tmp
.substr( index
, sep
- index
);
1109 strcat( buf
, mod
.c_str() );
1118 XmString
wxFindAcceleratorText (const char *s
)
1121 // VZ: this function returns incorrect keysym which completely breaks kbd
1125 // The accelerator text is after the \t char.
1126 s
= strchr( s
, '\t' );
1128 if( !s
) return NULL
;
1130 return wxStringToXmString( s
+ 1 ); // skip TAB!
1134 // Change a widget's foreground and background colours.
1135 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1137 // When should we specify the foreground, if it's calculated
1138 // by wxComputeColours?
1139 // Solution: say we start with the default (computed) foreground colour.
1140 // If we call SetForegroundColour explicitly for a control or window,
1141 // then the foreground is changed.
1142 // Therefore SetBackgroundColour computes the foreground colour, and
1143 // SetForegroundColour changes the foreground colour. The ordering is
1146 XtVaSetValues ((Widget
) widget
,
1147 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1151 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1153 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1156 XtVaSetValues ((Widget
) widget
,
1157 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1158 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1159 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1160 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1163 if (changeArmColour
)
1164 XtVaSetValues ((Widget
) widget
,
1165 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1169 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1171 // Lesstif 0.87 hangs here, but 0.93 does not
1172 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1173 Widget w
= (Widget
)widget
;
1175 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1181 wxString
wxXmStringToString( const XmString
& xmString
)
1184 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1191 return wxEmptyString
;
1194 XmString
wxStringToXmString( const wxString
& str
)
1196 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1199 XmString
wxStringToXmString( const char* str
)
1201 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1204 // ----------------------------------------------------------------------------
1205 // wxBitmap utility functions
1206 // ----------------------------------------------------------------------------
1208 // Creates a bitmap with transparent areas drawn in
1209 // the given colour.
1210 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1212 wxBitmap
newBitmap(bitmap
.GetWidth(),
1218 srcDC
.SelectObject(bitmap
);
1219 destDC
.SelectObject(newBitmap
);
1221 wxBrush
brush(colour
, wxSOLID
);
1222 // destDC.SetOptimization(FALSE);
1223 destDC
.SetBackground(brush
);
1225 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1226 &srcDC
, 0, 0, wxCOPY
, TRUE
);
1231 // ----------------------------------------------------------------------------
1232 // Miscellaneous functions
1233 // ----------------------------------------------------------------------------
1235 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
1237 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
1239 if (style
& wxSIMPLE_BORDER
)
1241 borderWidget
= XtVaCreateManagedWidget
1244 xmFrameWidgetClass
, parentWidget
,
1245 XmNshadowType
, XmSHADOW_ETCHED_IN
,
1246 XmNshadowThickness
, 1,
1250 else if (style
& wxSUNKEN_BORDER
)
1252 borderWidget
= XtVaCreateManagedWidget
1255 xmFrameWidgetClass
, parentWidget
,
1256 XmNshadowType
, XmSHADOW_IN
,
1260 else if (style
& wxRAISED_BORDER
)
1262 borderWidget
= XtVaCreateManagedWidget
1265 xmFrameWidgetClass
, parentWidget
,
1266 XmNshadowType
, XmSHADOW_OUT
,
1271 return borderWidget
;