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