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 // Now in utilsx11.cpp
640 // ----------------------------------------------------------------------------
641 // keycode translations
642 // ----------------------------------------------------------------------------
644 #include <X11/keysym.h>
646 // FIXME what about tables??
648 int wxCharCodeXToWX(KeySym keySym
)
655 id
= WXK_SHIFT
; break;
658 id
= WXK_CONTROL
; break;
660 id
= WXK_BACK
; break;
662 id
= WXK_DELETE
; break;
664 id
= WXK_CLEAR
; break;
670 id
= WXK_RETURN
; break;
672 id
= WXK_ESCAPE
; break;
675 id
= WXK_PAUSE
; break;
677 id
= WXK_NUMLOCK
; break;
679 id
= WXK_SCROLL
; break;
682 id
= WXK_HOME
; break;
686 id
= WXK_LEFT
; break;
688 id
= WXK_RIGHT
; break;
692 id
= WXK_DOWN
; break;
694 id
= WXK_NEXT
; break;
696 id
= WXK_PRIOR
; break;
698 id
= WXK_MENU
; break;
700 id
= WXK_SELECT
; break;
702 id
= WXK_CANCEL
; break;
704 id
= WXK_PRINT
; break;
706 id
= WXK_EXECUTE
; break;
708 id
= WXK_INSERT
; break;
710 id
= WXK_HELP
; break;
713 id
= WXK_MULTIPLY
; break;
717 id
= WXK_SUBTRACT
; break;
719 id
= WXK_DIVIDE
; break;
721 id
= WXK_DECIMAL
; break;
729 id
= WXK_RETURN
; break;
731 id
= WXK_NUMPAD0
; break;
733 id
= WXK_NUMPAD1
; break;
735 id
= WXK_NUMPAD2
; break;
737 id
= WXK_NUMPAD3
; break;
739 id
= WXK_NUMPAD4
; break;
741 id
= WXK_NUMPAD5
; break;
743 id
= WXK_NUMPAD6
; break;
745 id
= WXK_NUMPAD7
; break;
747 id
= WXK_NUMPAD8
; break;
749 id
= WXK_NUMPAD9
; break;
799 id
= (keySym
<= 255) ? (int)keySym
: -1;
805 KeySym
wxCharCodeWXToX(int id
)
811 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
812 case WXK_BACK
: keySym
= XK_BackSpace
; break;
813 case WXK_TAB
: keySym
= XK_Tab
; break;
814 case WXK_CLEAR
: keySym
= XK_Clear
; break;
815 case WXK_RETURN
: keySym
= XK_Return
; break;
816 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
817 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
818 case WXK_MENU
: keySym
= XK_Menu
; break;
819 case WXK_PAUSE
: keySym
= XK_Pause
; break;
820 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
821 case WXK_SPACE
: keySym
= ' '; break;
822 case WXK_PRIOR
: keySym
= XK_Prior
; break;
823 case WXK_NEXT
: keySym
= XK_Next
; break;
824 case WXK_END
: keySym
= XK_End
; break;
825 case WXK_HOME
: keySym
= XK_Home
; break;
826 case WXK_LEFT
: keySym
= XK_Left
; break;
827 case WXK_UP
: keySym
= XK_Up
; break;
828 case WXK_RIGHT
: keySym
= XK_Right
; break;
829 case WXK_DOWN
: keySym
= XK_Down
; break;
830 case WXK_SELECT
: keySym
= XK_Select
; break;
831 case WXK_PRINT
: keySym
= XK_Print
; break;
832 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
833 case WXK_INSERT
: keySym
= XK_Insert
; break;
834 case WXK_DELETE
: keySym
= XK_Delete
; break;
835 case WXK_HELP
: keySym
= XK_Help
; break;
836 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
837 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
838 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
839 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
840 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
841 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
842 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
843 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
844 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
845 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
846 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
847 case WXK_ADD
: keySym
= XK_KP_Add
; break;
848 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
849 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
850 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
851 case WXK_F1
: keySym
= XK_F1
; break;
852 case WXK_F2
: keySym
= XK_F2
; break;
853 case WXK_F3
: keySym
= XK_F3
; break;
854 case WXK_F4
: keySym
= XK_F4
; break;
855 case WXK_F5
: keySym
= XK_F5
; break;
856 case WXK_F6
: keySym
= XK_F6
; break;
857 case WXK_F7
: keySym
= XK_F7
; break;
858 case WXK_F8
: keySym
= XK_F8
; break;
859 case WXK_F9
: keySym
= XK_F9
; break;
860 case WXK_F10
: keySym
= XK_F10
; break;
861 case WXK_F11
: keySym
= XK_F11
; break;
862 case WXK_F12
: keySym
= XK_F12
; break;
863 case WXK_F13
: keySym
= XK_F13
; break;
864 case WXK_F14
: keySym
= XK_F14
; break;
865 case WXK_F15
: keySym
= XK_F15
; break;
866 case WXK_F16
: keySym
= XK_F16
; break;
867 case WXK_F17
: keySym
= XK_F17
; break;
868 case WXK_F18
: keySym
= XK_F18
; break;
869 case WXK_F19
: keySym
= XK_F19
; break;
870 case WXK_F20
: keySym
= XK_F20
; break;
871 case WXK_F21
: keySym
= XK_F21
; break;
872 case WXK_F22
: keySym
= XK_F22
; break;
873 case WXK_F23
: keySym
= XK_F23
; break;
874 case WXK_F24
: keySym
= XK_F24
; break;
875 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
876 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
877 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
884 // ----------------------------------------------------------------------------
885 // Some colour manipulation routines
886 // ----------------------------------------------------------------------------
888 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
893 int r
= 0, g
= 0, b
= 0;
896 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
897 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
899 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
902 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
903 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
904 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
907 case 0: r
= v
, g
= t
, b
= p
; break;
908 case 1: r
= q
, g
= v
, b
= p
; break;
909 case 2: r
= p
, g
= v
, b
= t
; break;
910 case 3: r
= p
, g
= q
, b
= v
; break;
911 case 4: r
= t
, g
= p
, b
= v
; break;
912 case 5: r
= v
, g
= p
, b
= q
; break;
919 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
921 int r
= rgb
->red
>> 8;
922 int g
= rgb
->green
>> 8;
923 int b
= rgb
->blue
>> 8;
924 int maxv
= wxMax3(r
, g
, b
);
925 int minv
= wxMin3(r
, g
, b
);
928 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
933 int rc
, gc
, bc
, hex
= 0;
934 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
935 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
936 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
937 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
938 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
939 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
940 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
944 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
945 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
948 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
953 int screen
= DefaultScreen(d
);
954 int num_colors
= DisplayCells(d
,screen
);
956 XColor
*color_defs
= new XColor
[num_colors
];
957 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
958 XQueryColors(d
,cmp
,color_defs
,num_colors
);
961 wxXColorToHSV(&hsv
,xc
);
963 int diff
, min_diff
= 0, pixel
= 0;
965 for(llp
= 0;llp
< num_colors
;llp
++)
967 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
968 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
969 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
970 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
971 if (llp
== 0) min_diff
= diff
;
972 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
973 if (min_diff
== 0) break;
976 xc
-> red
= color_defs
[pixel
].red
;
977 xc
-> green
= color_defs
[pixel
].green
;
978 xc
-> blue
= color_defs
[pixel
].blue
;
979 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
982 if (!XAllocColor(d,cmp,xc))
983 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
990 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
992 if (!XAllocColor(d
,cmp
,xc
))
994 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
995 wxAllocNearestColor(d
,cmp
,xc
);
1000 wxString
wxGetXEventName(XEvent
& event
)
1003 wxString
str(wxT("(some event)"));
1006 int type
= event
.xany
.type
;
1007 static char* event_name
[] = {
1008 "", "unknown(-)", // 0-1
1009 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1010 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1011 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1012 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1013 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1014 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1015 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1016 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1017 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1018 "ClientMessage", "MappingNotify", // 33-34
1019 "unknown(+)"}; // 35
1020 type
= wxMin(35, type
); type
= wxMax(1, type
);
1021 wxString
str(event_name
[type
]);
1027 // ----------------------------------------------------------------------------
1029 // ----------------------------------------------------------------------------
1031 // Find the letter corresponding to the mnemonic, for Motif
1032 char wxFindMnemonic (const char *s
)
1035 int len
= strlen (s
);
1038 for (i
= 0; i
< len
; i
++)
1042 // Carefully handle &&
1043 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1055 char* wxFindAccelerator( const char *s
)
1058 // VZ: this function returns incorrect keysym which completely breaks kbd
1062 // The accelerator text is after the \t char.
1063 s
= strchr( s
, '\t' );
1065 if( !s
) return NULL
;
1068 Now we need to format it as X standard:
1073 Ctrl+N --> Ctrl<Key>N
1074 Alt+k --> Meta<Key>k
1075 Ctrl+Shift+A --> Ctrl Shift<Key>A
1077 and handle Ctrl-N & similia
1080 static char buf
[256];
1083 wxString tmp
= s
+ 1; // skip TAB
1086 while( index
< tmp
.length() )
1088 size_t plus
= tmp
.find( '+', index
);
1089 size_t minus
= tmp
.find( '-', index
);
1091 // neither '+' nor '-', add <Key>
1092 if( plus
== wxString::npos
&& minus
== wxString::npos
)
1094 strcat( buf
, "<Key>" );
1095 strcat( buf
, tmp
.c_str() + index
);
1100 // OK: npos is big and positive
1101 size_t sep
= wxMin( plus
, minus
);
1102 wxString mod
= tmp
.substr( index
, sep
- index
);
1113 strcat( buf
, mod
.c_str() );
1122 XmString
wxFindAcceleratorText (const char *s
)
1125 // VZ: this function returns incorrect keysym which completely breaks kbd
1129 // The accelerator text is after the \t char.
1130 s
= strchr( s
, '\t' );
1132 if( !s
) return NULL
;
1134 return wxStringToXmString( s
+ 1 ); // skip TAB!
1138 // Change a widget's foreground and background colours.
1139 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1141 // When should we specify the foreground, if it's calculated
1142 // by wxComputeColours?
1143 // Solution: say we start with the default (computed) foreground colour.
1144 // If we call SetForegroundColour explicitly for a control or window,
1145 // then the foreground is changed.
1146 // Therefore SetBackgroundColour computes the foreground colour, and
1147 // SetForegroundColour changes the foreground colour. The ordering is
1150 XtVaSetValues ((Widget
) widget
,
1151 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1155 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1157 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1160 XtVaSetValues ((Widget
) widget
,
1161 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1162 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1163 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1164 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1167 if (changeArmColour
)
1168 XtVaSetValues ((Widget
) widget
,
1169 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1173 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1175 // Lesstif 0.87 hangs here, but 0.93 does not
1176 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1177 Widget w
= (Widget
)widget
;
1179 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1185 wxString
wxXmStringToString( const XmString
& xmString
)
1188 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1195 return wxEmptyString
;
1198 XmString
wxStringToXmString( const wxString
& str
)
1200 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1203 XmString
wxStringToXmString( const char* str
)
1205 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1208 // ----------------------------------------------------------------------------
1209 // wxBitmap utility functions
1210 // ----------------------------------------------------------------------------
1212 // Creates a bitmap with transparent areas drawn in
1213 // the given colour.
1214 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1216 wxBitmap
newBitmap(bitmap
.GetWidth(),
1222 srcDC
.SelectObject(bitmap
);
1223 destDC
.SelectObject(newBitmap
);
1225 wxBrush
brush(colour
, wxSOLID
);
1226 // destDC.SetOptimization(FALSE);
1227 destDC
.SetBackground(brush
);
1229 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1230 &srcDC
, 0, 0, wxCOPY
, TRUE
);
1235 // ----------------------------------------------------------------------------
1236 // Miscellaneous functions
1237 // ----------------------------------------------------------------------------
1239 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
1241 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
1243 if (style
& wxSIMPLE_BORDER
)
1245 borderWidget
= XtVaCreateManagedWidget
1248 xmFrameWidgetClass
, parentWidget
,
1249 XmNshadowType
, XmSHADOW_ETCHED_IN
,
1250 XmNshadowThickness
, 1,
1254 else if (style
& wxSUNKEN_BORDER
)
1256 borderWidget
= XtVaCreateManagedWidget
1259 xmFrameWidgetClass
, parentWidget
,
1260 XmNshadowType
, XmSHADOW_IN
,
1264 else if (style
& wxRAISED_BORDER
)
1266 borderWidget
= XtVaCreateManagedWidget
1269 xmFrameWidgetClass
, parentWidget
,
1270 XmNshadowType
, XmSHADOW_OUT
,
1275 return borderWidget
;