]> git.saurik.com Git - wxWidgets.git/blob - interface/utils.h
revised archive header; changed overview_arc to overview_archive which reads better...
[wxWidgets.git] / interface / utils.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.h
3 // Purpose: interface of wxWindowDisabler
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxWindowDisabler
11 @wxheader{utils.h}
12
13 This class disables all windows of the application (may be with the exception
14 of one of them) in its constructor and enables them back in its destructor.
15 This comes in handy when you want to indicate to the user that the application
16 is currently busy and cannot respond to user input.
17
18 @library{wxcore}
19 @category{FIXME}
20
21 @see wxBusyCursor
22 */
23 class wxWindowDisabler
24 {
25 public:
26 /**
27 Disables all top level windows of the applications with the exception of
28 @a winToSkip if it is not @NULL.
29 */
30 wxWindowDisabler(wxWindow* winToSkip = NULL);
31
32 /**
33 Reenables back the windows disabled by the constructor.
34 */
35 ~wxWindowDisabler();
36 };
37
38
39
40 /**
41 @class wxBusyCursor
42 @wxheader{utils.h}
43
44 This class makes it easy to tell your user that the program is temporarily busy.
45 Just create a wxBusyCursor object on the stack, and within the current scope,
46 the hourglass will be shown.
47
48 For example:
49
50 @code
51 wxBusyCursor wait;
52
53 for (int i = 0; i 100000; i++)
54 DoACalculation();
55 @endcode
56
57 It works by calling wxBeginBusyCursor() in the constructor,
58 and wxEndBusyCursor() in the destructor.
59
60 @library{wxcore}
61 @category{FIXME}
62
63 @see wxBeginBusyCursor(), wxEndBusyCursor(), wxWindowDisabler
64 */
65 class wxBusyCursor
66 {
67 public:
68 /**
69 Constructs a busy cursor object, calling wxBeginBusyCursor().
70 */
71 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR);
72
73 /**
74 Destroys the busy cursor object, calling wxEndBusyCursor().
75 */
76 ~wxBusyCursor();
77 };
78
79
80
81 // ============================================================================
82 // Global functions/macros
83 // ============================================================================
84
85 /**
86 Returns the type of power source as one of @c wxPOWER_SOCKET,
87 @c wxPOWER_BATTERY or @c wxPOWER_UNKNOWN.
88 @c wxPOWER_UNKNOWN is also the default on platforms where this
89 feature is not implemented (currently everywhere but MS Windows).
90 */
91 wxPowerType wxGetPowerType();
92
93 //@{
94 /**
95 This function returns the "user id" also known as "login name" under Unix i.e.
96 something like "jsmith". It uniquely identifies the current user (on this
97 system).
98 Under Windows or NT, this function first looks in the environment
99 variables USER and LOGNAME; if neither of these is found, the entry @b UserId
100 in the @b wxWidgets section of the WIN.INI file is tried.
101 The first variant of this function returns the login name if successful or an
102 empty string otherwise. The second (deprecated) function returns @true
103 if successful, @false otherwise.
104
105 @see wxGetUserName()
106 */
107 wxString wxGetUserId();
108 bool wxGetUserId(char* buf, int sz);
109 //@}
110
111 /**
112 @b NB: This function is now obsolete, please use
113 wxLogFatalError() instead.
114 Displays @a msg and exits. This writes to standard error under Unix,
115 and pops up a message box under Windows. Used for fatal internal
116 wxWidgets errors. See also wxError().
117 */
118 void wxFatalError(const wxString& msg,
119 const wxString& title = "wxWidgets Fatal Error");
120
121 /**
122 Returns battery state as one of @c wxBATTERY_NORMAL_STATE,
123 @c wxBATTERY_LOW_STATE, @c wxBATTERY_CRITICAL_STATE,
124 @c wxBATTERY_SHUTDOWN_STATE or @c wxBATTERY_UNKNOWN_STATE.
125 @c wxBATTERY_UNKNOWN_STATE is also the default on platforms where
126 this feature is not implemented (currently everywhere but MS Windows).
127 */
128 wxBatteryState wxGetBatteryState();
129
130 /**
131 @b NB: This function is obsolete, please use
132 wxWindow::FindWindowByName instead.
133 Find a window by its name (as given in a window constructor or @b Create
134 function call).
135 If @a parent is @NULL, the search will start from all top-level
136 frames and dialog boxes; if non-@NULL, the search will be limited to the given
137 window hierarchy.
138 The search is recursive in both cases.
139 If no such named window is found, @b wxFindWindowByLabel is called.
140 */
141 wxWindow* wxFindWindowByName(const wxString& name,
142 wxWindow* parent = NULL);
143
144 /**
145 Changes the cursor back to the original cursor, for all windows in the
146 application.
147 Use with wxBeginBusyCursor().
148 See also wxIsBusy(), wxBusyCursor.
149 */
150 void wxEndBusyCursor();
151
152 /**
153 This function is deprecated as the ids generated by it can conflict with the
154 ids defined by the user code, use @c wxID_ANY to assign ids which are
155 guaranteed to not conflict with the user-defined ids for the controls and menu
156 items you create instead of using this function.
157
158 Generates an integer identifier unique to this run of the program.
159 */
160 long wxNewId();
161
162 /**
163 Ensures that ids subsequently generated by @b NewId do not clash with
164 the given @b id.
165 */
166 void wxRegisterId(long id);
167
168 /**
169 @b NB: This function is now obsolete, replaced by Log
170 functions() and wxLogDebug() in particular.
171 Display a debugging message; under Windows, this will appear on the
172 debugger command window, and under Unix, it will be written to standard
173 error.
174 The syntax is identical to @b printf: pass a format string and a
175 variable list of arguments.
176 @b Tip: under Windows, if your application crashes before the
177 message appears in the debugging window, put a wxYield call after
178 each wxDebugMsg call. wxDebugMsg seems to be broken under WIN32s
179 (at least for Watcom C++): preformat your messages and use OutputDebugString
180 instead.
181 */
182 void wxDebugMsg(const wxString& fmt, ... );
183
184 /**
185 For normal keys, returns @true if the specified key is currently down.
186 For togglable keys (Caps Lock, Num Lock and Scroll Lock), returns
187 @true if the key is toggled such that its LED indicator is lit. There is
188 currently no way to test whether togglable keys are up or down.
189 Even though there are virtual key codes defined for mouse buttons, they
190 cannot be used with this function currently.
191 */
192 bool wxGetKeyState(wxKeyCode key);
193
194 /**
195 Returns the string containing the description of the current platform in a
196 user-readable form. For example, this function may return strings like
197 @c Windows NT Version 4.0 or @c Linux 2.2.2 i386.
198
199 @see ::wxGetOsVersion
200 */
201 wxString wxGetOsDescription();
202
203 /**
204 Return the (current) user's home directory.
205
206 @see wxGetUserHome(), wxStandardPaths
207 */
208 wxString wxGetHomeDir();
209
210 /**
211 Sleeps for the specified number of milliseconds. Notice that usage of this
212 function is encouraged instead of calling usleep(3) directly because the
213 standard usleep() function is not MT safe.
214 */
215 void wxMilliSleep(unsigned long milliseconds);
216
217 /**
218 Sleeps for the specified number of microseconds. The microsecond resolution may
219 not, in fact, be available on all platforms (currently only Unix platforms with
220 nanosleep(2) may provide it) in which case this is the same as
221 wxMilliSleep()(@e microseconds/1000).
222 */
223 void wxMicroSleep(unsigned long microseconds);
224
225 /**
226 Shows a message box with the information about the wxWidgets build used,
227 including its version, most important build parameters and the version of the
228 underlying GUI toolkit. This is mainly used for diagnostic purposes and can be
229 invoked by Ctrl-Alt-middle clicking on any wxWindow which doesn't otherwise
230 handle this event.
231
232 @wxsince{2.9.0}
233 */
234 void wxInfoMessageBox(wxWindow ( parent = NULL);
235
236 /**
237 Find a menu item identifier associated with the given frame's menu bar.
238 */
239 int wxFindMenuItemId(wxFrame* frame, const wxString& menuString,
240 const wxString& itemString);
241
242 /**
243 This function enables or disables all top level windows. It is used by
244 ::wxSafeYield.
245 */
246 void wxEnableTopLevelWindows(bool enable = true);
247
248 /**
249 Strips any menu codes from @a str and returns the result.
250 By default, the functions strips both the mnemonics character (@c '')
251 which is used to indicate a keyboard shortkey, and the accelerators, which are
252 used only in the menu items and are separated from the main text by the
253 @c \t (TAB) character. By using @a flags of
254 @c wxStrip_Mnemonics or @c wxStrip_Accel to strip only the former
255 or the latter part, respectively.
256 Notice that in most cases
257 wxMenuItem::GetLabelFromText or
258 wxControl::GetLabelText can be used instead.
259 */
260 wxString wxStripMenuCodes(const wxString& str,
261 int flags = wxStrip_All);
262
263 /**
264 @b NB: This function is now obsolete, please use wxLogError()
265 instead.
266 Displays @a msg and continues. This writes to standard error under
267 Unix, and pops up a message box under Windows. Used for internal
268 wxWidgets errors. See also wxFatalError().
269 */
270 void wxError(const wxString& msg,
271 const wxString& title = "wxWidgets Internal Error");
272
273 /**
274 Open the @a url in user's default browser. If @a flags parameter contains
275 @c wxBROWSER_NEW_WINDOW flag, a new window is opened for the URL
276 (currently this is only supported under Windows). The @a url may also be a
277 local file path (with or without @c file:// prefix), if it doesn't
278 correspond to an existing file and the URL has no scheme @c http:// is
279 prepended to it by default.
280 Returns @true if the application was successfully launched.
281 Note that for some configurations of the running user, the application which
282 is launched to open the given URL may be URL-dependent (e.g. a browser may be
283 used for
284 local URLs while another one may be used for remote URLs).
285 */
286 bool wxLaunchDefaultBrowser(const wxString& url, int flags = 0);
287
288 /**
289 Executes a command in an interactive shell window. If no command is
290 specified, then just the shell is spawned.
291 See also wxExecute(), @ref overview_sampleexec "Exec sample".
292 */
293 bool wxShell(const wxString& command = NULL);
294
295 /**
296 Gets the version and the operating system ID for currently running OS.
297 See wxPlatformInfo for more details about wxOperatingSystemId.
298
299 @see ::wxGetOsDescription, wxPlatformInfo
300 */
301 wxOperatingSystemId wxGetOsVersion(int* major = NULL,
302 int* minor = NULL);
303
304 /**
305 Returns the FQDN (fully qualified domain host name) or an empty string on
306 error.
307
308 @see wxGetHostName()
309 */
310 wxString wxGetFullHostName();
311
312 /**
313 Changes the cursor to the given cursor for all windows in the application.
314 Use wxEndBusyCursor() to revert the cursor back
315 to its previous state. These two calls can be nested, and a counter
316 ensures that only the outer calls take effect.
317 See also wxIsBusy(), wxBusyCursor.
318 */
319 void wxBeginBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR);
320
321 /**
322 Tells the system to delete the specified object when
323 all other events have been processed. In some environments, it is
324 necessary to use this instead of deleting a frame directly with the
325 delete operator, because some GUIs will still send events to a deleted window.
326 Now obsolete: use wxWindow::Close instead.
327 */
328 void wxPostDelete(wxObject* object);
329
330 /**
331 @b NB: This function is obsolete, please use
332 wxWindow::FindWindowByLabel instead.
333 Find a window by its label. Depending on the type of window, the label may be a
334 window title
335 or panel item label. If @a parent is @NULL, the search will start from all
336 top-level
337 frames and dialog boxes; if non-@NULL, the search will be limited to the given
338 window hierarchy.
339 The search is recursive in both cases.
340 */
341 wxWindow* wxFindWindowByLabel(const wxString& label,
342 wxWindow* parent = NULL);
343
344 /**
345 This function is similar to wxYield, except that it disables the user input to
346 all program windows before calling wxYield and re-enables it again
347 afterwards. If @a win is not @NULL, this window will remain enabled,
348 allowing the implementation of some limited user interaction.
349 Returns the result of the call to ::wxYield.
350 */
351 bool wxSafeYield(wxWindow* win = NULL, bool onlyIfNeeded = false);
352
353 /**
354 Returns the mouse position in screen coordinates.
355 */
356 wxPoint wxGetMousePosition();
357
358 /**
359 Loads a user-defined Windows resource as a string. If the resource is found,
360 the function creates
361 a new character array and copies the data into it. A pointer to this data is
362 returned. If unsuccessful, @NULL is returned.
363 The resource must be defined in the @c .rc file using the following syntax:
364
365 @code
366 myResource TEXT file.ext
367 @endcode
368
369 where @c file.ext is a file that the resource compiler can find.
370 This function is available under Windows only.
371 */
372 wxString wxLoadUserResource(const wxString& resourceName,
373 const wxString& resourceType = "TEXT");
374
375 /**
376 Returns the amount of free memory in bytes under environments which
377 support it, and -1 if not supported or failed to perform measurement.
378 */
379 wxMemorySize wxGetFreeMemory();
380
381 /**
382 This is a macro defined as @c getenv() or its wide char version in Unicode
383 mode.
384 Note that under Win32 it may not return correct value for the variables set
385 with wxSetEnv(), use wxGetEnv() function
386 instead.
387 */
388 wxChar* wxGetEnv(const wxString& var);
389
390 //@{
391 /**
392 Copies the current host machine's name into the supplied buffer. Please note
393 that the returned name is @e not fully qualified, i.e. it does not include
394 the domain name.
395 Under Windows or NT, this function first looks in the environment
396 variable SYSTEM_NAME; if this is not found, the entry @b HostName
397 in the @b wxWidgets section of the WIN.INI file is tried.
398 The first variant of this function returns the hostname if successful or an
399 empty string otherwise. The second (deprecated) function returns @true
400 if successful, @false otherwise.
401
402 @see wxGetFullHostName()
403 */
404 wxString wxGetHostName();
405 bool wxGetHostName(char* buf, int sz);
406 //@}
407
408 /**
409 Returns the current value of the environment variable @a var in @e value.
410 @a value may be @NULL if you just want to know if the variable exists
411 and are not interested in its value.
412 Returns @true if the variable exists, @false otherwise.
413 */
414 bool wxGetEnv(const wxString& var, wxString* value);
415
416 /**
417 Under X only, returns the current display name. See also wxSetDisplayName().
418 */
419 wxString wxGetDisplayName();
420
421 /**
422 Ring the system bell.
423 Note that this function is categorized as a GUI one and so is not thread-safe.
424 */
425 void wxBell();
426
427 /**
428 Returns the home directory for the given user. If the @a user is empty
429 (default value), this function behaves like
430 wxGetHomeDir() i.e. returns the current user home
431 directory.
432 If the home directory couldn't be determined, an empty string is returned.
433 */
434 wxString wxGetUserHome(const wxString& user = "");
435
436 //@{
437 /**
438 @b wxPerl note: In wxPerl this function is called @c Wx::ExecuteStdoutStderr
439 and it only takes the @c command argument,
440 and returns a 3-element list @c ( status, output, errors ), where
441 @c output and @c errors are array references.
442 Executes another program in Unix or Windows.
443 The first form takes a command string, such as @c "emacs file.txt".
444 The second form takes an array of values: a command, any number of
445 arguments, terminated by @NULL.
446 The semantics of the third and fourth versions is different from the first two
447 and is described in more details below.
448 If @a flags parameter contains @c wxEXEC_ASYNC flag (the default), flow
449 of control immediately returns. If it contains @c wxEXEC_SYNC, the current
450 application waits until the other program has terminated.
451 In the case of synchronous execution, the return value is the exit code of
452 the process (which terminates by the moment the function returns) and will be
453 -1 if the process couldn't be started and typically 0 if the process
454 terminated successfully. Also, while waiting for the process to
455 terminate, wxExecute will call wxYield(). Because of this, by
456 default this function disables all application windows to avoid unexpected
457 reentrancies which could result from the users interaction with the program
458 while the child process is running. If you are sure that it is safe to not
459 disable the program windows, you may pass @c wxEXEC_NODISABLE flag to
460 prevent this automatic disabling from happening.
461 For asynchronous execution, however, the return value is the process id and
462 zero value indicates that the command could not be executed. As an added
463 complication, the return value of -1 in this case indicates that we didn't
464 launch a new process, but connected to the running one (this can only happen in
465 case of using DDE under Windows for command execution). In particular, in this,
466 and only this, case the calling code will not get the notification about
467 process termination.
468 If callback isn't @NULL and if execution is asynchronous,
469 wxProcess::OnTerminate will be called when
470 the process finishes. Specifying this parameter also allows you to redirect the
471 standard input and/or output of the process being launched by calling
472 wxProcess::Redirect. If the child process IO is redirected,
473 under Windows the process window is not shown by default (this avoids having to
474 flush an unnecessary console for the processes which don't create any windows
475 anyhow) but a @c wxEXEC_NOHIDE flag can be used to prevent this from
476 happening, i.e. with this flag the child process window will be shown normally.
477 Under Unix the flag @c wxEXEC_MAKE_GROUP_LEADER may be used to ensure
478 that the new process is a group leader (this will create a new session if
479 needed). Calling wxKill() passing wxKILL_CHILDREN will
480 kill this process as well as all of its children (except those which have
481 started their own session).
482 The @c wxEXEC_NOEVENTS flag prevents processing of any events from taking
483 place while the child process is running. It should be only used for very
484 short-lived processes as otherwise the application windows risk becoming
485 unresponsive from the users point of view. As this flag only makes sense with
486 @c wxEXEC_SYNC, @c wxEXEC_BLOCK equal to the sum of both of these flags
487 is provided as a convenience.
488 Finally, you may use the third overloaded version of this function to execute
489 a process (always synchronously, the contents of @a flags is or'd with
490 @c wxEXEC_SYNC) and capture its output in the array @e output. The
491 fourth version adds the possibility to additionally capture the messages from
492 standard error output in the @a errors array.
493 @b NB: Currently wxExecute() can only be used from the main thread, calling
494 this function from another thread will result in an assert failure in debug
495 build and won't work.
496
497 @param command
498 The command to execute and any parameters to pass to it as a
499 single string.
500 @param argv
501 The command to execute should be the first element of this
502 array, any additional ones are the command parameters and the array must be
503 terminated with a @NULL pointer.
504 @param flags
505 Combination of bit masks wxEXEC_ASYNC,
506 wxEXEC_SYNC and wxEXEC_NOHIDE
507 @param callback
508 An optional pointer to wxProcess
509
510 @see wxShell(), wxProcess, @ref overview_sampleexec "Exec sample".
511 */
512 long wxExecute(const wxString& command, int sync = wxEXEC_ASYNC,
513 wxProcess* callback = NULL);
514 wxPerl note: long wxExecute(char** argv,
515 int flags = wxEXEC_ASYNC,
516 wxProcess* callback = NULL);
517 wxPerl note: long wxExecute(const wxString& command,
518 wxArrayString& output,
519 int flags = 0);
520 wxPerl note: long wxExecute(const wxString& command,
521 wxArrayString& output,
522 wxArrayString& errors,
523 int flags = 0);
524 //@}
525
526 /**
527 Returns a string representing the current date and time.
528 */
529 wxString wxNow();
530
531 /**
532 Returns @true if the operating system the program is running under is 64 bit.
533 The check is performed at run-time and may differ from the value available at
534 compile-time (at compile-time you can just check if @c sizeof(void*)==8)
535 since the program could be running in emulation mode or in a mixed 32/64 bit
536 system
537 (bi-architecture operating system).
538 Very important: this function is not 100% reliable on some systems given the
539 fact
540 that there isn't always a standard way to do a reliable check on the OS
541 architecture.
542 */
543 bool wxIsPlatform64Bit();
544
545 /**
546 Returns the number uniquely identifying the current process in the system.
547 If an error occurs, 0 is returned.
548 */
549 unsigned long wxGetProcessId();
550
551 /**
552 Equivalent to the Unix kill function: send the given signal @a sig to the
553 process with PID @e pid. The valid signal values are
554
555 @code
556 enum wxSignal
557 {
558 wxSIGNONE = 0, // verify if the process exists under Unix
559 wxSIGHUP,
560 wxSIGINT,
561 wxSIGQUIT,
562 wxSIGILL,
563 wxSIGTRAP,
564 wxSIGABRT,
565 wxSIGEMT,
566 wxSIGFPE,
567 wxSIGKILL, // forcefully kill, dangerous!
568 wxSIGBUS,
569 wxSIGSEGV,
570 wxSIGSYS,
571 wxSIGPIPE,
572 wxSIGALRM,
573 wxSIGTERM // terminate the process gently
574 };
575 @endcode
576
577 @c wxSIGNONE, @c wxSIGKILL and @c wxSIGTERM have the same meaning
578 under both Unix and Windows but all the other signals are equivalent to
579 @c wxSIGTERM under Windows.
580 Returns 0 on success, -1 on failure. If @a rc parameter is not @NULL, it will
581 be filled with an element of @c wxKillError enum:
582
583 @code
584 enum wxKillError
585 {
586 wxKILL_OK, // no error
587 wxKILL_BAD_SIGNAL, // no such signal
588 wxKILL_ACCESS_DENIED, // permission denied
589 wxKILL_NO_PROCESS, // no such process
590 wxKILL_ERROR // another, unspecified error
591 };
592 @endcode
593
594 The @a flags parameter can be wxKILL_NOCHILDREN (the default),
595 or wxKILL_CHILDREN, in which case the child processes of this
596 process will be killed too. Note that under Unix, for wxKILL_CHILDREN
597 to work you should have created the process by passing wxEXEC_MAKE_GROUP_LEADER
598 to wxExecute.
599
600 @see wxProcess::Kill, wxProcess::Exists, @ref overview_sampleexec "Exec sample"
601 */
602 int wxKill(long pid, int sig = wxSIGTERM, wxKillError rc = NULL,
603 int flags = 0);
604
605 /**
606 Returns the current state of the mouse. Returns a wxMouseState
607 instance that contains the current position of the mouse pointer in
608 screen coordinates, as well as boolean values indicating the up/down
609 status of the mouse buttons and the modifier keys.
610 */
611 wxMouseState wxGetMouseState();
612
613 /**
614 Returns @true if between two wxBeginBusyCursor() and
615 wxEndBusyCursor() calls.
616 See also wxBusyCursor.
617 */
618 bool wxIsBusy();
619
620 //@{
621 /**
622 Copies the user's email address into the supplied buffer, by
623 concatenating the values returned by wxGetFullHostName()
624 and wxGetUserId().
625 Returns @true if successful, @false otherwise.
626 */
627 wxString wxGetEmailAddress();
628 bool wxGetEmailAddress(char* buf, int sz);
629 //@}
630
631 /**
632 Sleeps for the specified number of seconds.
633 */
634 void wxSleep(int secs);
635
636 /**
637 Sets the value of the environment variable @a var (adding it if necessary)
638 to @e value.
639 Returns @true on success.
640
641 @see wxUnsetEnv()
642 */
643 bool wxSetEnv(const wxString& var, const wxString& value);
644
645 /**
646 Returns @true if the current platform is little endian (instead of big
647 endian).
648 The check is performed at run-time.
649
650 @see @ref overview_byteordermacros "Byte order macros"
651 */
652 bool wxIsPlatformLittleEndian();
653
654 /**
655 Under X only, sets the current display name. This is the X host and display
656 name such
657 as "colonsay:0.0", and the function indicates which display should be used for
658 creating
659 windows from this point on. Setting the display within an application allows
660 multiple
661 displays to be used.
662 See also wxGetDisplayName().
663 */
664 void wxSetDisplayName(const wxString& displayName);
665