]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/utils.h
Cleaned up paint DC cache in ~wxPaintDC to avoid spurious memory warning
[wxWidgets.git] / include / wx / utils.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.h
3// Purpose: Miscellaneous utilities
4// Author: Julian Smart
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Julian Smart
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_UTILSH__
13#define _WX_UTILSH__
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19#ifdef __GNUG__
20 #pragma interface "utils.h"
21#endif
22
23#include "wx/setup.h"
24#include "wx/object.h"
25#include "wx/list.h"
26#include "wx/filefn.h"
27
28#ifdef __X__
29 #include <dirent.h>
30 #include <unistd.h>
31#endif
32
33#include <stdio.h>
34
35// ----------------------------------------------------------------------------
36// Forward declaration
37// ----------------------------------------------------------------------------
38
39class WXDLLEXPORT wxProcess;
40class WXDLLEXPORT wxFrame;
41class WXDLLEXPORT wxWindow;
42
43// FIXME should use wxStricmp() instead
44#if defined(__GNUWIN32__)
45 #define stricmp strcasecmp
46 #define strnicmp strncasecmp
47#endif
48
49// ----------------------------------------------------------------------------
50// Macros
51// ----------------------------------------------------------------------------
52
53#define wxMax(a,b) (((a) > (b)) ? (a) : (b))
54#define wxMin(a,b) (((a) < (b)) ? (a) : (b))
55
56// ----------------------------------------------------------------------------
57// String functions (deprecated, use wxString)
58// ----------------------------------------------------------------------------
59
60// Useful buffer (FIXME VZ: yeah, that is. To be removed!)
61WXDLLEXPORT_DATA(extern wxChar*) wxBuffer;
62
63// Make a copy of this string using 'new'
64WXDLLEXPORT wxChar* copystring(const wxChar *s);
65
66// Matches string one within string two regardless of case
67WXDLLEXPORT bool StringMatch(wxChar *one, wxChar *two, bool subString = TRUE, bool exact = FALSE);
68
69// A shorter way of using strcmp
70#define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
71
72// ----------------------------------------------------------------------------
73// Miscellaneous functions
74// ----------------------------------------------------------------------------
75
76// Sound the bell
77WXDLLEXPORT void wxBell();
78
79// Get OS version
80WXDLLEXPORT int wxGetOsVersion(int *majorVsn= (int *) NULL,int *minorVsn= (int *) NULL) ;
81
82// Return a string with the current date/time
83WXDLLEXPORT wxString wxNow();
84
85#if wxUSE_GUI
86// Don't synthesize KeyUp events holding down a key and producing
87// KeyDown events with autorepeat. On by default and always on
88// in wxMSW.
89WXDLLEXPORT bool wxSetDetectableAutoRepeat( bool flag );
90
91// ----------------------------------------------------------------------------
92// Window ID management
93// ----------------------------------------------------------------------------
94
95// Generate a unique ID
96WXDLLEXPORT long wxNewId();
97#if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
98 #define NewId wxNewId
99#endif
100
101// Ensure subsequent IDs don't clash with this one
102WXDLLEXPORT void wxRegisterId(long id);
103#if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
104 #define RegisterId wxRegisterId
105#endif
106
107// Return the current ID
108WXDLLEXPORT long wxGetCurrentId();
109
110#endif // wxUSE_GUI
111
112// ----------------------------------------------------------------------------
113// Various conversions
114// ----------------------------------------------------------------------------
115
116WXDLLEXPORT_DATA(extern const wxChar*) wxFloatToStringStr;
117WXDLLEXPORT_DATA(extern const wxChar*) wxDoubleToStringStr;
118
119WXDLLEXPORT void StringToFloat(wxChar *s, float *number);
120WXDLLEXPORT wxChar* FloatToString(float number, const wxChar *fmt = wxFloatToStringStr);
121WXDLLEXPORT void StringToDouble(wxChar *s, double *number);
122WXDLLEXPORT wxChar* DoubleToString(double number, const wxChar *fmt = wxDoubleToStringStr);
123WXDLLEXPORT void StringToInt(wxChar *s, int *number);
124WXDLLEXPORT void StringToLong(wxChar *s, long *number);
125WXDLLEXPORT wxChar* IntToString(int number);
126WXDLLEXPORT wxChar* LongToString(long number);
127
128// Convert 2-digit hex number to decimal
129WXDLLEXPORT int wxHexToDec(const wxString& buf);
130
131// Convert decimal integer to 2-character hex string
132WXDLLEXPORT void wxDecToHex(int dec, wxChar *buf);
133WXDLLEXPORT wxString wxDecToHex(int dec);
134
135// ----------------------------------------------------------------------------
136// Process management
137// ----------------------------------------------------------------------------
138
139// Execute another program. Returns 0 if there was an error, a PID otherwise.
140WXDLLEXPORT long wxExecute(wxChar **argv, bool sync = FALSE,
141 wxProcess *process = (wxProcess *) NULL);
142WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE,
143 wxProcess *process = (wxProcess *) NULL);
144
145enum wxSignal
146{
147 wxSIGNONE = 0, // verify if the process exists under Unix
148 wxSIGHUP,
149 wxSIGINT,
150 wxSIGQUIT,
151 wxSIGILL,
152 wxSIGTRAP,
153 wxSIGABRT,
154 wxSIGIOT = wxSIGABRT, // another name
155 wxSIGEMT,
156 wxSIGFPE,
157 wxSIGKILL,
158 wxSIGBUS,
159 wxSIGSEGV,
160 wxSIGSYS,
161 wxSIGPIPE,
162 wxSIGALRM,
163 wxSIGTERM
164
165 // further signals are different in meaning between different Unix systems
166};
167
168// the argument is ignored under Windows - the process is always killed
169WXDLLEXPORT int wxKill(long pid, wxSignal sig = wxSIGTERM);
170
171// Execute a command in an interactive shell window
172// If no command then just the shell
173WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString);
174
175// Sleep for nSecs seconds under UNIX, do nothing under Windows
176WXDLLEXPORT void wxSleep(int nSecs);
177
178// Sleep for a given amount of milliseconds
179WXDLLEXPORT void wxUsleep(unsigned long milliseconds);
180
181// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
182WXDLLEXPORT long wxGetFreeMemory();
183
184// ----------------------------------------------------------------------------
185// Network and username functions.
186// ----------------------------------------------------------------------------
187
188// NB: "char *" functions are deprecated, use wxString ones!
189
190// Get eMail address
191WXDLLEXPORT bool wxGetEmailAddress(wxChar *buf, int maxSize);
192WXDLLEXPORT wxString wxGetEmailAddress();
193
194// Get hostname.
195WXDLLEXPORT bool wxGetHostName(wxChar *buf, int maxSize);
196WXDLLEXPORT wxString wxGetHostName();
197
198// Get FQDN
199WXDLLEXPORT wxString wxGetFullHostName();
200WXDLLEXPORT bool wxGetFullHostName(wxChar *buf, int maxSize);
201
202// Get user ID e.g. jacs (this is known as login name under Unix)
203WXDLLEXPORT bool wxGetUserId(wxChar *buf, int maxSize);
204WXDLLEXPORT wxString wxGetUserId();
205
206// Get user name e.g. Julian Smart
207WXDLLEXPORT bool wxGetUserName(wxChar *buf, int maxSize);
208WXDLLEXPORT wxString wxGetUserName();
209
210// Get current Home dir and copy to dest (returns pstr->c_str())
211WXDLLEXPORT const wxChar* wxGetHomeDir(wxString *pstr);
212
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
216WXDLLEXPORT const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
217#else
218WXDLLEXPORT wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
219#endif
220
221#if wxUSE_GUI // GUI only things from now on
222
223// ----------------------------------------------------------------------------
224// Menu accelerators related things
225// ----------------------------------------------------------------------------
226
227WXDLLEXPORT wxChar* wxStripMenuCodes(wxChar *in, wxChar *out = (wxChar *) NULL);
228WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str);
229
230#if wxUSE_ACCEL
231class WXDLLEXPORT wxAcceleratorEntry;
232WXDLLEXPORT wxAcceleratorEntry *wxGetAccelFromString(const wxString& label);
233#endif // wxUSE_ACCEL
234
235// ----------------------------------------------------------------------------
236// Window search
237// ----------------------------------------------------------------------------
238
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
241// all windows.
242WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL);
243
244// Find window by name, and if that fails, by label.
245WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL);
246
247// Returns menu item id or -1 if none.
248WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
249
250// ----------------------------------------------------------------------------
251// Message/event queue helpers
252// ----------------------------------------------------------------------------
253
254// Yield to other apps/messages
255WXDLLEXPORT bool wxYield();
256
257// Yield to other apps/messages and disable user input
258WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL);
259
260// Enable or disable input to all top level windows
261WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE);
262
263// Check whether this window wants to process messages, e.g. Stop button
264// in long calculations.
265WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
266
267// Consume all events until no more left
268WXDLLEXPORT void wxFlushEvents();
269
270// ----------------------------------------------------------------------------
271// Cursors
272// ----------------------------------------------------------------------------
273
274// Set the cursor to the busy cursor for all windows
275class WXDLLEXPORT wxCursor;
276WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
277WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
278
279// Restore cursor to normal
280WXDLLEXPORT void wxEndBusyCursor();
281
282// TRUE if we're between the above two calls
283WXDLLEXPORT bool wxIsBusy();
284
285// Convenience class so we can just create a wxBusyCursor object on the stack
286class WXDLLEXPORT wxBusyCursor
287{
288public:
289 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR)
290 { wxBeginBusyCursor(cursor); }
291 ~wxBusyCursor()
292 { wxEndBusyCursor(); }
293};
294
295// ----------------------------------------------------------------------------
296// Error message functions used by wxWindows (deprecated, use wxLog)
297// ----------------------------------------------------------------------------
298
299// Format a message on the standard error (UNIX) or the debugging
300// stream (Windows)
301WXDLLEXPORT void wxDebugMsg(const wxChar *fmt ...) ;
302
303// Non-fatal error (continues)
304WXDLLEXPORT_DATA(extern const wxChar*) wxInternalErrorStr;
305WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
306
307// Fatal error (exits)
308WXDLLEXPORT_DATA(extern const wxChar*) wxFatalErrorStr;
309WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
310
311// ----------------------------------------------------------------------------
312// Reading and writing resources (eg WIN.INI, .Xdefaults)
313// ----------------------------------------------------------------------------
314
315#if wxUSE_RESOURCES
316WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
317WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
318WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
319WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
320
321WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString);
322WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
323WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
324WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
325#endif // wxUSE_RESOURCES
326
327void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
328
329// MSW only: get user-defined resource from the .res file.
330// Returns NULL or newly-allocated memory, so use delete[] to clean up.
331#ifdef __WXMSW__
332WXDLLEXPORT extern const wxChar* wxUserResourceStr;
333WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
334
335// Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
336// input/output
337WXDLLEXPORT void wxRedirectIOToConsole();
338
339#endif // MSW
340
341// ----------------------------------------------------------------------------
342// Display and colorss (X only)
343// ----------------------------------------------------------------------------
344
345#ifdef __WXGTK__
346 void *wxGetDisplay();
347#endif
348
349#ifdef __X__
350 WXDisplay *wxGetDisplay();
351 bool wxSetDisplay(const wxString& display_name);
352 wxString wxGetDisplayName();
353#endif // X or GTK+
354
355#ifdef __X__
356
357#include <X11/Xlib.h>
358
359#define wxMAX_RGB 0xff
360#define wxMAX_SV 1000
361#define wxSIGN(x) ((x < 0) ? -x : x)
362#define wxH_WEIGHT 4
363#define wxS_WEIGHT 1
364#define wxV_WEIGHT 2
365
366typedef struct wx_hsv {
367 int h,s,v;
368 } wxHSV;
369
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))
372
373void wxHSVToXColor(wxHSV *hsv,XColor *xcolor);
374void wxXColorToHSV(wxHSV *hsv,XColor *xcolor);
375void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor);
376void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor);
377
378#endif //__X__
379
380// ----------------------------------------------------------------------------
381// font-related functions (X and GTK)
382// ----------------------------------------------------------------------------
383
384#if defined(__X__) || defined(__WXGTK__)
385
386#ifdef __X__
387 typedef XFontStruct *wxNativeFont;
388#else // GDK
389 typedef GdkFont *wxNativeFont;
390#endif
391
392#include "wx/font.h" // for wxFontEncoding
393
394// returns the handle of the nearest available font or 0
395extern wxNativeFont wxLoadQueryNearestFont(int pointSize,
396 int family,
397 int style,
398 int weight,
399 bool underlined,
400 const wxString &facename,
401 wxFontEncoding encoding);
402
403// fills xencoding and xregistry with the X font spec parts for the given
404// encoding ('*' if encoding == wxFONTENCODING_SYSTEM) and returns TRUE if any
405// fonts with this encoding exist or FALSE if it's unknown (it does *not* mean
406// that they don't exist!)
407extern bool wxGetXFontEncoding(wxFontEncoding encoding,
408 wxString *xencoding, wxString *xregistry);
409
410#endif // X || GTK
411
412#endif // wxUSE_GUI
413
414#endif
415 // _WX_UTILSH__