1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #pragma interface "utils.h"
24 #include "wx/object.h"
26 #include "wx/filefn.h"
35 // ----------------------------------------------------------------------------
36 // Forward declaration
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxProcess
;
40 class WXDLLEXPORT wxFrame
;
41 class WXDLLEXPORT wxWindow
;
43 // FIXME should use wxStricmp() instead
44 #if defined(__GNUWIN32__)
45 #define stricmp strcasecmp
46 #define strnicmp strncasecmp
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
54 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
56 // ----------------------------------------------------------------------------
57 // String functions (deprecated, use wxString)
58 // ----------------------------------------------------------------------------
60 // Useful buffer (FIXME VZ: yeah, that is. To be removed!)
61 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
63 // Make a copy of this string using 'new'
64 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
66 // Matches string one within string two regardless of case
67 WXDLLEXPORT
bool StringMatch(wxChar
*one
, wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
69 // A shorter way of using strcmp
70 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
72 // ----------------------------------------------------------------------------
73 // Miscellaneous functions
74 // ----------------------------------------------------------------------------
77 WXDLLEXPORT
void wxBell();
80 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,int *minorVsn
= (int *) NULL
) ;
82 // Return a string with the current date/time
83 WXDLLEXPORT wxString
wxNow();
86 // Don't synthesize KeyUp events holding down a key and producing
87 // KeyDown events with autorepeat. On by default and always on
89 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
91 // ----------------------------------------------------------------------------
92 // Window ID management
93 // ----------------------------------------------------------------------------
95 // Generate a unique ID
96 WXDLLEXPORT
long wxNewId();
97 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
101 // Ensure subsequent IDs don't clash with this one
102 WXDLLEXPORT
void wxRegisterId(long id
);
103 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
104 #define RegisterId wxRegisterId
107 // Return the current ID
108 WXDLLEXPORT
long wxGetCurrentId();
112 // ----------------------------------------------------------------------------
113 // Various conversions
114 // ----------------------------------------------------------------------------
116 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
117 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
119 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
120 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
121 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
122 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
123 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
124 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
125 WXDLLEXPORT wxChar
* IntToString(int number
);
126 WXDLLEXPORT wxChar
* LongToString(long number
);
128 // Convert 2-digit hex number to decimal
129 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
131 // Convert decimal integer to 2-character hex string
132 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
133 WXDLLEXPORT wxString
wxDecToHex(int dec
);
135 // ----------------------------------------------------------------------------
136 // Process management
137 // ----------------------------------------------------------------------------
139 // Execute another program. Returns 0 if there was an error, a PID otherwise.
140 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
141 wxProcess
*process
= (wxProcess
*) NULL
);
142 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
143 wxProcess
*process
= (wxProcess
*) NULL
);
147 wxSIGNONE
= 0, // verify if the process exists under Unix
154 wxSIGIOT
= wxSIGABRT
, // another name
165 // further signals are different in meaning between different Unix systems
168 // the argument is ignored under Windows - the process is always killed
169 WXDLLEXPORT
int wxKill(long pid
, wxSignal sig
= wxSIGTERM
);
171 // Execute a command in an interactive shell window
172 // If no command then just the shell
173 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
175 // Sleep for nSecs seconds under UNIX, do nothing under Windows
176 WXDLLEXPORT
void wxSleep(int nSecs
);
178 // Sleep for a given amount of milliseconds
179 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
181 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
182 WXDLLEXPORT
long wxGetFreeMemory();
184 // ----------------------------------------------------------------------------
185 // Network and username functions.
186 // ----------------------------------------------------------------------------
188 // NB: "char *" functions are deprecated, use wxString ones!
191 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
192 WXDLLEXPORT wxString
wxGetEmailAddress();
195 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
196 WXDLLEXPORT wxString
wxGetHostName();
199 WXDLLEXPORT wxString
wxGetFullHostName();
200 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
202 // Get user ID e.g. jacs (this is known as login name under Unix)
203 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
204 WXDLLEXPORT wxString
wxGetUserId();
206 // Get user name e.g. Julian Smart
207 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
208 WXDLLEXPORT wxString
wxGetUserName();
210 // Get current Home dir and copy to dest (returns pstr->c_str())
211 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
213 // Get the user's home dir (caller must copy --- volatile)
214 // returns NULL is no HOME dir is known
215 #if defined(__UNIX__) && wxUSE_UNICODE
216 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
218 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
221 #if wxUSE_GUI // GUI only things from now on
223 // ----------------------------------------------------------------------------
224 // Menu accelerators related things
225 // ----------------------------------------------------------------------------
227 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
228 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
231 class WXDLLEXPORT wxAcceleratorEntry
;
232 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
233 #endif // wxUSE_ACCEL
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 // Find the window/widget with the given title or label.
240 // Pass a parent to begin the search from, or NULL to look through
242 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
244 // Find window by name, and if that fails, by label.
245 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
247 // Returns menu item id or -1 if none.
248 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
250 // ----------------------------------------------------------------------------
251 // Message/event queue helpers
252 // ----------------------------------------------------------------------------
254 // Yield to other apps/messages
255 WXDLLEXPORT
bool wxYield();
257 // Yield to other apps/messages and disable user input
258 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
260 // Enable or disable input to all top level windows
261 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
263 // Check whether this window wants to process messages, e.g. Stop button
264 // in long calculations.
265 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
267 // Consume all events until no more left
268 WXDLLEXPORT
void wxFlushEvents();
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 // Set the cursor to the busy cursor for all windows
275 class WXDLLEXPORT wxCursor
;
276 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
277 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
279 // Restore cursor to normal
280 WXDLLEXPORT
void wxEndBusyCursor();
282 // TRUE if we're between the above two calls
283 WXDLLEXPORT
bool wxIsBusy();
285 // Convenience class so we can just create a wxBusyCursor object on the stack
286 class WXDLLEXPORT wxBusyCursor
289 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
290 { wxBeginBusyCursor(cursor
); }
292 { wxEndBusyCursor(); }
295 // ----------------------------------------------------------------------------
296 // Error message functions used by wxWindows (deprecated, use wxLog)
297 // ----------------------------------------------------------------------------
299 // Format a message on the standard error (UNIX) or the debugging
301 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...) ;
303 // Non-fatal error (continues)
304 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
305 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
307 // Fatal error (exits)
308 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
309 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
311 // ----------------------------------------------------------------------------
312 // Reading and writing resources (eg WIN.INI, .Xdefaults)
313 // ----------------------------------------------------------------------------
316 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
317 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
318 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
319 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
321 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
322 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
323 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
324 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
325 #endif // wxUSE_RESOURCES
327 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
329 // MSW only: get user-defined resource from the .res file.
330 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
332 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
333 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
335 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
337 WXDLLEXPORT
void wxRedirectIOToConsole();
341 // ----------------------------------------------------------------------------
342 // Display and colorss (X only)
343 // ----------------------------------------------------------------------------
346 void *wxGetDisplay();
350 WXDisplay
*wxGetDisplay();
351 bool wxSetDisplay(const wxString
& display_name
);
352 wxString
wxGetDisplayName();
357 #include <X11/Xlib.h>
359 #define wxMAX_RGB 0xff
360 #define wxMAX_SV 1000
361 #define wxSIGN(x) ((x < 0) ? -x : x)
366 typedef struct wx_hsv
{
370 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
371 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
373 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
374 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
375 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
376 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
380 // ----------------------------------------------------------------------------
381 // font-related functions (X and GTK)
382 // ----------------------------------------------------------------------------
384 #if defined(__X__) || defined(__WXGTK__)
387 typedef XFontStruct
*wxNativeFont
;
389 typedef GdkFont
*wxNativeFont
;
392 #include "wx/font.h" // for wxFontEncoding
394 // returns the handle of the nearest available font or 0
395 extern wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
400 const wxString
&facename
,
401 wxFontEncoding encoding
);