]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
Added menu_highlight behaviour as in wxMSW (statusbar)
[wxWidgets.git] / include / wx / utils.h
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 #ifdef __GNUG__
16 #pragma interface "utils.h"
17 #endif
18
19 #include "wx/setup.h"
20 #include "wx/object.h"
21 #include "wx/list.h"
22 #include "wx/window.h"
23 #include "wx/filefn.h"
24 #include "wx/process.h"
25
26 #include "wx/ioswrap.h"
27
28 #ifdef __X__
29 #ifndef __VMS__
30 /*steve: these two are not known under VMS */
31 #include <dirent.h>
32 #include <unistd.h>
33 #endif
34 #endif
35
36 #include <stdio.h>
37
38 #ifdef __GNUWIN32__
39 #define stricmp strcasecmp
40 #define strnicmp strncasecmp
41 #endif
42
43 // Forward declaration
44 class WXDLLEXPORT wxFrame;
45
46 // Stupid ASCII macros
47 #define wxToUpper(C) (((C) >= 'a' && (C) <= 'z')? (C) - 'a' + 'A': (C))
48 #define wxToLower(C) (((C) >= 'A' && (C) <= 'Z')? (C) - 'A' + 'a': (C))
49
50 // Return a string with the current date/time
51 WXDLLEXPORT wxString wxNow(void);
52
53 // Make a copy of this string using 'new'
54 WXDLLEXPORT char* copystring(const char *s);
55
56 // Generate a unique ID
57 WXDLLEXPORT long wxNewId(void);
58 #define NewId wxNewId
59
60 // Ensure subsequent IDs don't clash with this one
61 WXDLLEXPORT void wxRegisterId(long id);
62 #define RegisterId wxRegisterId
63
64 // Return the current ID
65 WXDLLEXPORT long wxGetCurrentId(void);
66
67 // Useful buffer
68 WXDLLEXPORT_DATA(extern char*) wxBuffer;
69
70 WXDLLEXPORT_DATA(extern const char*) wxFloatToStringStr;
71 WXDLLEXPORT_DATA(extern const char*) wxDoubleToStringStr;
72
73 // Various conversions
74 WXDLLEXPORT void StringToFloat(char *s, float *number);
75 WXDLLEXPORT char* FloatToString(float number, const char *fmt = wxFloatToStringStr);
76 WXDLLEXPORT void StringToDouble(char *s, double *number);
77 WXDLLEXPORT char* DoubleToString(double number, const char *fmt = wxDoubleToStringStr);
78 WXDLLEXPORT void StringToInt(char *s, int *number);
79 WXDLLEXPORT void StringToLong(char *s, long *number);
80 WXDLLEXPORT char* IntToString(int number);
81 WXDLLEXPORT char* LongToString(long number);
82
83 // Matches string one within string two regardless of case
84 WXDLLEXPORT bool StringMatch(char *one, char *two, bool subString = TRUE, bool exact = FALSE);
85
86 // A shorter way of using strcmp
87 #define wxStringEq(s1, s2) (s1 && s2 && (strcmp(s1, s2) == 0))
88
89 // Convert 2-digit hex number to decimal
90 WXDLLEXPORT int wxHexToDec(const wxString& buf);
91
92 // Convert decimal integer to 2-character hex string
93 WXDLLEXPORT void wxDecToHex(int dec, char *buf);
94 WXDLLEXPORT wxString wxDecToHex(int dec);
95
96 // Execute another program. Returns 0 if there was an error, a PID otherwise.
97 WXDLLEXPORT long wxExecute(char **argv, bool sync = FALSE,
98 wxProcess *process = (wxProcess *) NULL);
99 WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE,
100 wxProcess *process = (wxProcess *) NULL);
101
102 #define wxSIGTERM 1
103
104 WXDLLEXPORT int wxKill(long pid, int sig=wxSIGTERM);
105
106 // Execute a command in an interactive shell window
107 // If no command then just the shell
108 WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString);
109
110 // Sleep for nSecs seconds under UNIX, do nothing under Windows
111 WXDLLEXPORT void wxSleep(int nSecs);
112
113 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
114 WXDLLEXPORT long wxGetFreeMemory(void);
115
116 // Consume all events until no more left
117 WXDLLEXPORT void wxFlushEvents(void);
118
119 /*
120 * Network and username functions.
121 *
122 */
123
124 // Get eMail address
125 WXDLLEXPORT bool wxGetEmailAddress(char *buf, int maxSize);
126
127 // Get hostname.
128 WXDLLEXPORT bool wxGetHostName(char *buf, int maxSize);
129 WXDLLEXPORT bool wxGetHostName(wxString& buf);
130
131 // Get user ID e.g. jacs
132 WXDLLEXPORT bool wxGetUserId(char *buf, int maxSize);
133 WXDLLEXPORT bool wxGetUserId(wxString& buf);
134
135 // Get user name e.g. Julian Smart
136 WXDLLEXPORT bool wxGetUserName(char *buf, int maxSize);
137 WXDLLEXPORT bool wxGetUserName(wxString& buf);
138
139 /*
140 * Strip out any menu codes
141 */
142 WXDLLEXPORT char* wxStripMenuCodes(char *in, char *out = (char *) NULL);
143 WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str);
144
145 // Find the window/widget with the given title or label.
146 // Pass a parent to begin the search from, or NULL to look through
147 // all windows.
148 WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL);
149
150 // Find window by name, and if that fails, by label.
151 WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL);
152
153 // Returns menu item id or -1 if none.
154 WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
155
156 /*
157 #if (!defined(__MINMAX_DEFINED) && !defined(max))
158 #define max(a,b) (((a) > (b)) ? (a) : (b))
159 #define min(a,b) (((a) < (b)) ? (a) : (b))
160 #define __MINMAX_DEFINED 1
161 #endif
162 */
163
164 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
165 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
166
167 // Yield to other apps/messages
168 WXDLLEXPORT bool wxYield(void);
169
170 // Format a message on the standard error (UNIX) or the debugging
171 // stream (Windows)
172 WXDLLEXPORT void wxDebugMsg(const char *fmt ...) ;
173
174 // Sound the bell
175 WXDLLEXPORT void wxBell(void) ;
176
177 // Get OS version
178 WXDLLEXPORT int wxGetOsVersion(int *majorVsn= (int *) NULL,int *minorVsn= (int *) NULL) ;
179
180 // Set the cursor to the busy cursor for all windows
181 class WXDLLEXPORT wxCursor;
182 WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
183 WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
184
185 // Restore cursor to normal
186 WXDLLEXPORT void wxEndBusyCursor(void);
187
188 // TRUE if we're between the above two calls
189 WXDLLEXPORT bool wxIsBusy(void);
190
191 // Convenience class so we can just create a wxBusyCursor object on the stack
192 class WXDLLEXPORT wxBusyCursor
193 {
194 public:
195 inline wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR) { wxBeginBusyCursor(cursor); }
196 inline ~wxBusyCursor() { wxEndBusyCursor(); }
197 };
198
199 /* Error message functions used by wxWindows */
200
201 // Non-fatal error (continues)
202 WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr;
203 WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
204
205 // Fatal error (exits)
206 WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr;
207 WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
208
209 // Reading and writing resources (eg WIN.INI, .Xdefaults)
210 #if wxUSE_RESOURCES
211 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
212 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
213 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
214 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
215
216 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file = wxEmptyString);
217 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
218 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
219 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
220 #endif // wxUSE_RESOURCES
221
222 // Get current Home dir and copy to dest (returns pstr->c_str())
223 WXDLLEXPORT const char* wxGetHomeDir(wxString *pstr);
224
225 // Get the user's home dir (caller must copy--- volatile)
226 // returns NULL is no HOME dir is known
227 WXDLLEXPORT char* wxGetUserHome(const wxString& user = wxEmptyString);
228
229 // Check whether this window wants to process messages, e.g. Stop button
230 // in long calculations.
231 WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
232
233 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
234
235 // MSW only: get user-defined resource from the .res file.
236 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
237 #ifdef __WXMSW__
238 WXDLLEXPORT extern const char* wxUserResourceStr;
239 WXDLLEXPORT char* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
240
241 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
242 // input/output
243 WXDLLEXPORT void wxRedirectIOToConsole();
244
245 #endif
246
247 // X only
248 #ifdef __X__
249 WXDisplay *wxGetDisplay();
250 bool wxSetDisplay(const wxString& display_name);
251 wxString wxGetDisplayName();
252 #endif
253
254 #ifdef __X__
255
256 #include <X11/Xlib.h>
257
258 #define wxMAX_RGB 0xff
259 #define wxMAX_SV 1000
260 #define wxSIGN(x) ((x < 0) ? -x : x)
261 #define wxH_WEIGHT 4
262 #define wxS_WEIGHT 1
263 #define wxV_WEIGHT 2
264
265 typedef struct wx_hsv {
266 int h,s,v;
267 } wxHSV;
268
269 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
270 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
271
272 #define wxMax2(x,y) ((x > y) ? x : y)
273 #define wxMin2(x,y) ((x < y) ? x : y)
274
275 void wxHSVToXColor(wxHSV *hsv,XColor *xcolor);
276 void wxXColorToHSV(wxHSV *hsv,XColor *xcolor);
277 void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor);
278 void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor);
279
280 #endif //__X__
281
282 #endif
283 // _WX_UTILSH__