Coverage Report

Created: 2023-02-22 06:06

/src/wxwidgets/include/wx/utils.h
Line
Count
Source (jump to first uncovered line)
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/utils.h
3
// Purpose:     Miscellaneous utilities
4
// Author:      Julian Smart
5
// Modified by:
6
// Created:     29/01/98
7
// Copyright:   (c) 1998 Julian Smart
8
// Licence:     wxWindows licence
9
/////////////////////////////////////////////////////////////////////////////
10
11
#ifndef _WX_UTILS_H_
12
#define _WX_UTILS_H_
13
14
// ----------------------------------------------------------------------------
15
// headers
16
// ----------------------------------------------------------------------------
17
18
#include "wx/object.h"
19
#include "wx/list.h"
20
#include "wx/filefn.h"
21
#include "wx/hashmap.h"
22
#include "wx/versioninfo.h"
23
#include "wx/meta/implicitconversion.h"
24
25
#if wxUSE_GUI
26
    #include "wx/gdicmn.h"
27
    #include "wx/mousestate.h"
28
    #include "wx/vector.h"
29
#endif
30
31
class WXDLLIMPEXP_FWD_BASE wxArrayString;
32
class WXDLLIMPEXP_FWD_BASE wxArrayInt;
33
34
// need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
35
// wxLongLong
36
#include "wx/longlong.h"
37
38
// needed for wxOperatingSystemId, wxLinuxDistributionInfo
39
#include "wx/platinfo.h"
40
41
#if defined(__X__)
42
    #include <dirent.h>
43
    #include <unistd.h>
44
#endif
45
46
#include <stdio.h>
47
48
// ----------------------------------------------------------------------------
49
// Forward declaration
50
// ----------------------------------------------------------------------------
51
52
class WXDLLIMPEXP_FWD_BASE wxProcess;
53
class WXDLLIMPEXP_FWD_CORE wxFrame;
54
class WXDLLIMPEXP_FWD_CORE wxWindow;
55
class WXDLLIMPEXP_FWD_CORE wxEventLoop;
56
57
// ----------------------------------------------------------------------------
58
// Arithmetic functions
59
// ----------------------------------------------------------------------------
60
61
template<typename T1, typename T2>
62
inline typename wxImplicitConversionType<T1,T2>::value
63
wxMax(T1 a, T2 b)
64
5.33k
{
65
5.33k
    typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
66
67
    // Cast both operands to the same type before comparing them to avoid
68
    // warnings about signed/unsigned comparisons from some compilers:
69
5.33k
    return static_cast<ResultType>(a) > static_cast<ResultType>(b) ? a : b;
70
5.33k
}
wxImplicitConversionType<long, int>::value wxMax<long, int>(long, int)
Line
Count
Source
64
2.04k
{
65
2.04k
    typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
66
67
    // Cast both operands to the same type before comparing them to avoid
68
    // warnings about signed/unsigned comparisons from some compilers:
69
2.04k
    return static_cast<ResultType>(a) > static_cast<ResultType>(b) ? a : b;
70
2.04k
}
wxImplicitConversionType<long, long>::value wxMax<long, long>(long, long)
Line
Count
Source
64
3.28k
{
65
3.28k
    typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
66
67
    // Cast both operands to the same type before comparing them to avoid
68
    // warnings about signed/unsigned comparisons from some compilers:
69
3.28k
    return static_cast<ResultType>(a) > static_cast<ResultType>(b) ? a : b;
70
3.28k
}
71
72
template<typename T1, typename T2>
73
inline typename wxImplicitConversionType<T1,T2>::value
74
wxMin(T1 a, T2 b)
75
2.67k
{
76
2.67k
    typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
77
78
2.67k
    return static_cast<ResultType>(a) < static_cast<ResultType>(b) ? a : b;
79
2.67k
}
Unexecuted instantiation: wxImplicitConversionType<unsigned long, unsigned long>::value wxMin<unsigned long, unsigned long>(unsigned long, unsigned long)
wxImplicitConversionType<unsigned short, int>::value wxMin<unsigned short, int>(unsigned short, int)
Line
Count
Source
75
2.67k
{
76
2.67k
    typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
77
78
2.67k
    return static_cast<ResultType>(a) < static_cast<ResultType>(b) ? a : b;
79
2.67k
}
80
81
template<typename T1, typename T2, typename T3>
82
inline typename wxImplicitConversionType3<T1,T2,T3>::value
83
wxClip(T1 a, T2 b, T3 c)
84
{
85
    typedef typename wxImplicitConversionType3<T1,T2,T3>::value ResultType;
86
87
    if ( static_cast<ResultType>(a) < static_cast<ResultType>(b) )
88
        return b;
89
90
    if ( static_cast<ResultType>(a) > static_cast<ResultType>(c) )
91
        return c;
92
93
    return a;
94
}
95
96
// ----------------------------------------------------------------------------
97
// wxMemorySize
98
// ----------------------------------------------------------------------------
99
100
// wxGetFreeMemory can return huge amount of memory on 32-bit platforms as well
101
// so to always use long long for its result type on all platforms which
102
// support it
103
#if wxUSE_LONGLONG
104
    typedef wxLongLong wxMemorySize;
105
#else
106
    typedef long wxMemorySize;
107
#endif
108
109
// ----------------------------------------------------------------------------
110
// Miscellaneous functions
111
// ----------------------------------------------------------------------------
112
113
// Sound the bell
114
WXDLLIMPEXP_CORE void wxBell();
115
116
#if wxUSE_MSGDLG
117
// Show wxWidgets information
118
WXDLLIMPEXP_CORE void wxInfoMessageBox(wxWindow* parent);
119
#endif // wxUSE_MSGDLG
120
121
WXDLLIMPEXP_CORE wxVersionInfo wxGetLibraryVersionInfo();
122
123
// Get OS description as a user-readable string
124
WXDLLIMPEXP_BASE wxString wxGetOsDescription();
125
126
// Get OS version
127
WXDLLIMPEXP_BASE wxOperatingSystemId wxGetOsVersion(int *verMaj = nullptr,
128
                                                    int *verMin = nullptr,
129
                                                    int *verMicro = nullptr);
130
131
// Check is OS version is at least the specified major and minor version
132
WXDLLIMPEXP_BASE bool wxCheckOsVersion(int majorVsn, int minorVsn = 0, int microVsn = 0);
133
134
// Get platform endianness
135
WXDLLIMPEXP_BASE bool wxIsPlatformLittleEndian();
136
137
// Get platform architecture bitness
138
WXDLLIMPEXP_BASE bool wxIsPlatform64Bit();
139
140
// Get machine CPU architecture
141
WXDLLIMPEXP_BASE wxString wxGetCpuArchitectureName();
142
143
// Get native machine CPU architecture
144
WXDLLIMPEXP_BASE wxString wxGetNativeCpuArchitectureName();
145
146
#ifdef __LINUX__
147
// Get linux-distro information
148
WXDLLIMPEXP_BASE wxLinuxDistributionInfo wxGetLinuxDistributionInfo();
149
#endif
150
151
// Return a string with the current date/time
152
WXDLLIMPEXP_BASE wxString wxNow();
153
154
// Return path where wxWidgets is installed (mostly useful in Unices)
155
WXDLLIMPEXP_BASE wxString wxGetInstallPrefix();
156
// Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
157
WXDLLIMPEXP_BASE wxString wxGetDataDir();
158
159
#if wxUSE_GUI
160
161
// Get the state of a key (true if pressed, false if not)
162
// This is generally most useful getting the state of
163
// the modifier or toggle keys.
164
WXDLLIMPEXP_CORE bool wxGetKeyState(wxKeyCode key);
165
166
// Don't synthesize KeyUp events holding down a key and producing
167
// KeyDown events with autorepeat. On by default and always on
168
// in wxMSW.
169
WXDLLIMPEXP_CORE bool wxSetDetectableAutoRepeat( bool flag );
170
171
// Returns the current state of the mouse position, buttons and modifiers
172
WXDLLIMPEXP_CORE wxMouseState wxGetMouseState();
173
174
#endif // wxUSE_GUI
175
176
// ----------------------------------------------------------------------------
177
// wxPlatform
178
// ----------------------------------------------------------------------------
179
180
/*
181
 * Class to make it easier to specify platform-dependent values
182
 *
183
 * Examples:
184
 *  long val = wxPlatform::If(wxMac, 1).ElseIf(wxGTK, 2).ElseIf(stPDA, 5).Else(3);
185
 *  wxString strVal = wxPlatform::If(wxMac, wxT("Mac")).ElseIf(wxMSW, wxT("MSW")).Else(wxT("Other"));
186
 *
187
 * A custom platform symbol:
188
 *
189
 *  #define stPDA 100
190
 *  #ifdef __WXMSW__
191
 *      wxPlatform::AddPlatform(stPDA);
192
 *  #endif
193
 *
194
 *  long windowStyle = wxCAPTION | (long) wxPlatform::IfNot(stPDA, wxRESIZE_BORDER);
195
 *
196
 */
197
198
class WXDLLIMPEXP_BASE wxPlatform
199
{
200
public:
201
0
    wxPlatform() { Init(); }
202
0
    wxPlatform(const wxPlatform& platform) { Copy(platform); }
203
0
    void operator = (const wxPlatform& platform) { if (&platform != this) Copy(platform); }
204
    void Copy(const wxPlatform& platform);
205
206
    // Specify an optional default value
207
0
    wxPlatform(int defValue) { Init(); m_longValue = (long)defValue; }
208
0
    wxPlatform(long defValue) { Init(); m_longValue = defValue; }
209
0
    wxPlatform(const wxString& defValue) { Init(); m_stringValue = defValue; }
210
0
    wxPlatform(double defValue) { Init(); m_doubleValue = defValue; }
211
212
    static wxPlatform If(int platform, long value);
213
    static wxPlatform IfNot(int platform, long value);
214
    wxPlatform& ElseIf(int platform, long value);
215
    wxPlatform& ElseIfNot(int platform, long value);
216
    wxPlatform& Else(long value);
217
218
0
    static wxPlatform If(int platform, int value) { return If(platform, (long)value); }
219
0
    static wxPlatform IfNot(int platform, int value) { return IfNot(platform, (long)value); }
220
0
    wxPlatform& ElseIf(int platform, int value) { return ElseIf(platform, (long) value); }
221
0
    wxPlatform& ElseIfNot(int platform, int value) { return ElseIfNot(platform, (long) value); }
222
0
    wxPlatform& Else(int value) { return Else((long) value); }
223
224
    static wxPlatform If(int platform, double value);
225
    static wxPlatform IfNot(int platform, double value);
226
    wxPlatform& ElseIf(int platform, double value);
227
    wxPlatform& ElseIfNot(int platform, double value);
228
    wxPlatform& Else(double value);
229
230
    static wxPlatform If(int platform, const wxString& value);
231
    static wxPlatform IfNot(int platform, const wxString& value);
232
    wxPlatform& ElseIf(int platform, const wxString& value);
233
    wxPlatform& ElseIfNot(int platform, const wxString& value);
234
    wxPlatform& Else(const wxString& value);
235
236
0
    long GetInteger() const { return m_longValue; }
237
0
    const wxString& GetString() const { return m_stringValue; }
238
0
    double GetDouble() const { return m_doubleValue; }
239
240
0
    operator int() const { return (int) GetInteger(); }
241
0
    operator long() const { return GetInteger(); }
242
0
    operator double() const { return GetDouble(); }
243
0
    operator const wxString&() const { return GetString(); }
244
245
    static void AddPlatform(int platform);
246
    static bool Is(int platform);
247
    static void ClearPlatforms();
248
249
private:
250
251
0
    void Init() { m_longValue = 0; m_doubleValue = 0.0; }
252
253
    long                m_longValue;
254
    double              m_doubleValue;
255
    wxString            m_stringValue;
256
    static wxArrayInt*  sm_customPlatforms;
257
};
258
259
/// Function for testing current platform
260
0
inline bool wxPlatformIs(int platform) { return wxPlatform::Is(platform); }
261
262
// ----------------------------------------------------------------------------
263
// Window ID management
264
// ----------------------------------------------------------------------------
265
266
// Ensure subsequent IDs don't clash with this one
267
WXDLLIMPEXP_BASE void wxRegisterId(wxWindowID id);
268
269
// Return the current ID
270
WXDLLIMPEXP_BASE wxWindowID wxGetCurrentId();
271
272
// Generate a unique ID
273
WXDLLIMPEXP_BASE wxWindowID wxNewId();
274
275
// ----------------------------------------------------------------------------
276
// Various conversions
277
// ----------------------------------------------------------------------------
278
279
// Convert 2-digit hex number to decimal
280
WXDLLIMPEXP_BASE int wxHexToDec(const wxString& buf);
281
282
// Convert 2-digit hex number to decimal
283
inline int wxHexToDec(const char* buf)
284
0
{
285
0
    int firstDigit, secondDigit;
286
287
0
    if (buf[0] >= 'A')
288
0
        firstDigit = buf[0] - 'A' + 10;
289
0
    else if (buf[0] >= '0')
290
0
        firstDigit = buf[0] - '0';
291
0
    else
292
0
        firstDigit = -1;
293
294
0
    wxCHECK_MSG( firstDigit >= 0 && firstDigit <= 15, -1, wxS("Invalid argument") );
295
296
0
    if (buf[1] >= 'A')
297
0
        secondDigit = buf[1] - 'A' + 10;
298
0
    else if (buf[1] >= '0')
299
0
        secondDigit = buf[1] - '0';
300
0
    else
301
0
        secondDigit = -1;
302
303
0
    wxCHECK_MSG( secondDigit >= 0 && secondDigit <= 15, -1, wxS("Invalid argument") );
304
305
0
    return firstDigit * 16 + secondDigit;
306
0
}
307
308
309
// Convert decimal integer to 2-character hex string
310
WXDLLIMPEXP_BASE void wxDecToHex(unsigned char dec, wxChar *buf);
311
WXDLLIMPEXP_BASE void wxDecToHex(unsigned char dec, char* ch1, char* ch2);
312
WXDLLIMPEXP_BASE wxString wxDecToHex(unsigned char dec);
313
314
// ----------------------------------------------------------------------------
315
// Security
316
// ----------------------------------------------------------------------------
317
318
WXDLLIMPEXP_BASE void wxSecureZeroMemory(void *p, size_t n);
319
320
// ----------------------------------------------------------------------------
321
// Process management
322
// ----------------------------------------------------------------------------
323
324
// NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
325
//     be 0 and 1, don't change!
326
327
enum
328
{
329
    // execute the process asynchronously
330
    wxEXEC_ASYNC    = 0,
331
332
    // execute it synchronously, i.e. wait until it finishes
333
    wxEXEC_SYNC     = 1,
334
335
    // under Windows, don't hide the child even if it's IO is redirected (this
336
    // is done by default)
337
    wxEXEC_SHOW_CONSOLE   = 2,
338
339
    // deprecated synonym for wxEXEC_SHOW_CONSOLE, use the new name as it's
340
    // more clear
341
    wxEXEC_NOHIDE = wxEXEC_SHOW_CONSOLE,
342
343
    // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
344
    // kills all children as well as pid
345
    // under Windows (NT family only), sets the CREATE_NEW_PROCESS_GROUP flag,
346
    // which allows to target Ctrl-Break signal to the spawned process.
347
    // applies to console processes only.
348
    wxEXEC_MAKE_GROUP_LEADER = 4,
349
350
    // by default synchronous execution disables all program windows to avoid
351
    // that the user interacts with the program while the child process is
352
    // running, you can use this flag to prevent this from happening
353
    wxEXEC_NODISABLE = 8,
354
355
    // by default, the event loop is run while waiting for synchronous execution
356
    // to complete and this flag can be used to simply block the main process
357
    // until the child process finishes
358
    wxEXEC_NOEVENTS = 16,
359
360
    // under Windows, hide the console of the child process if it has one, even
361
    // if its IO is not redirected
362
    wxEXEC_HIDE_CONSOLE = 32,
363
364
    // convenient synonym for flags given system()-like behaviour
365
    wxEXEC_BLOCK = wxEXEC_SYNC | wxEXEC_NOEVENTS
366
};
367
368
// Map storing environment variables.
369
typedef wxStringToStringHashMap wxEnvVariableHashMap;
370
371
// Used to pass additional parameters for child process to wxExecute(). Could
372
// be extended with other fields later.
373
struct wxExecuteEnv
374
{
375
    wxString cwd;               // If empty, CWD is not changed.
376
    wxEnvVariableHashMap env;   // If empty, environment is unchanged.
377
};
378
379
// Execute another program.
380
//
381
// If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
382
// process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
383
// failure and the PID of the launched process if ok.
384
WXDLLIMPEXP_BASE long wxExecute(const wxString& command,
385
                                int flags = wxEXEC_ASYNC,
386
                                wxProcess *process = nullptr,
387
                                const wxExecuteEnv *env = nullptr);
388
WXDLLIMPEXP_BASE long wxExecute(const char* const* argv,
389
                                int flags = wxEXEC_ASYNC,
390
                                wxProcess *process = nullptr,
391
                                const wxExecuteEnv *env = nullptr);
392
WXDLLIMPEXP_BASE long wxExecute(const wchar_t* const* argv,
393
                                int flags = wxEXEC_ASYNC,
394
                                wxProcess *process = nullptr,
395
                                const wxExecuteEnv *env = nullptr);
396
397
// execute the command capturing its output into an array line by line, this is
398
// always synchronous
399
WXDLLIMPEXP_BASE long wxExecute(const wxString& command,
400
                                wxArrayString& output,
401
                                int flags = 0,
402
                                const wxExecuteEnv *env = nullptr);
403
404
// also capture stderr (also synchronous)
405
WXDLLIMPEXP_BASE long wxExecute(const wxString& command,
406
                                wxArrayString& output,
407
                                wxArrayString& error,
408
                                int flags = 0,
409
                                const wxExecuteEnv *env = nullptr);
410
411
#if defined(__WINDOWS__) && wxUSE_IPC
412
// ask a DDE server to execute the DDE request with given parameters
413
WXDLLIMPEXP_BASE bool wxExecuteDDE(const wxString& ddeServer,
414
                                   const wxString& ddeTopic,
415
                                   const wxString& ddeCommand);
416
#endif // __WINDOWS__ && wxUSE_IPC
417
418
enum wxSignal
419
{
420
    wxSIGNONE = 0,  // verify if the process exists under Unix
421
    wxSIGHUP,
422
    wxSIGINT,
423
    wxSIGQUIT,
424
    wxSIGILL,
425
    wxSIGTRAP,
426
    wxSIGABRT,
427
    wxSIGIOT = wxSIGABRT,   // another name
428
    wxSIGEMT,
429
    wxSIGFPE,
430
    wxSIGKILL,
431
    wxSIGBUS,
432
    wxSIGSEGV,
433
    wxSIGSYS,
434
    wxSIGPIPE,
435
    wxSIGALRM,
436
    wxSIGTERM
437
438
    // further signals are different in meaning between different Unix systems
439
};
440
441
enum wxKillError
442
{
443
    wxKILL_OK,              // no error
444
    wxKILL_BAD_SIGNAL,      // no such signal
445
    wxKILL_ACCESS_DENIED,   // permission denied
446
    wxKILL_NO_PROCESS,      // no such process
447
    wxKILL_ERROR            // another, unspecified error
448
};
449
450
enum wxKillFlags
451
{
452
    wxKILL_NOCHILDREN = 0,  // don't kill children
453
    wxKILL_CHILDREN = 1     // kill children
454
};
455
456
enum wxShutdownFlags
457
{
458
    wxSHUTDOWN_FORCE    = 1,// can be combined with other flags (MSW-only)
459
    wxSHUTDOWN_POWEROFF = 2,// power off the computer
460
    wxSHUTDOWN_REBOOT   = 4,// shutdown and reboot
461
    wxSHUTDOWN_LOGOFF   = 8 // close session (currently MSW-only)
462
};
463
464
// Shutdown or reboot the PC
465
WXDLLIMPEXP_BASE bool wxShutdown(int flags = wxSHUTDOWN_POWEROFF);
466
467
// send the given signal to the process (only NONE and KILL are supported under
468
// Windows, all others mean TERM), return 0 if ok and -1 on error
469
//
470
// return detailed error in rc is not null
471
WXDLLIMPEXP_BASE int wxKill(long pid,
472
                       wxSignal sig = wxSIGTERM,
473
                       wxKillError *rc = nullptr,
474
                       int flags = wxKILL_NOCHILDREN);
475
476
// Execute a command in an interactive shell window (always synchronously)
477
// If no command then just the shell
478
WXDLLIMPEXP_BASE bool wxShell(const wxString& command = wxEmptyString);
479
480
// As wxShell(), but must give a (non interactive) command and its output will
481
// be returned in output array
482
WXDLLIMPEXP_BASE bool wxShell(const wxString& command, wxArrayString& output);
483
484
// Sleep for nSecs seconds
485
WXDLLIMPEXP_BASE void wxSleep(int nSecs);
486
487
// Sleep for a given amount of milliseconds
488
WXDLLIMPEXP_BASE void wxMilliSleep(unsigned long milliseconds);
489
490
// Sleep for a given amount of microseconds
491
WXDLLIMPEXP_BASE void wxMicroSleep(unsigned long microseconds);
492
493
// Get the process id of the current process
494
WXDLLIMPEXP_BASE unsigned long wxGetProcessId();
495
496
// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
497
WXDLLIMPEXP_BASE wxMemorySize wxGetFreeMemory();
498
499
#if wxUSE_ON_FATAL_EXCEPTION
500
501
// should wxApp::OnFatalException() be called?
502
WXDLLIMPEXP_BASE bool wxHandleFatalExceptions(bool doit = true);
503
504
#endif // wxUSE_ON_FATAL_EXCEPTION
505
506
// ----------------------------------------------------------------------------
507
// Environment variables
508
// ----------------------------------------------------------------------------
509
510
// returns true if variable exists (value may be null if you just want to check
511
// for this)
512
WXDLLIMPEXP_BASE bool wxGetEnv(const wxString& var, wxString *value);
513
514
// set the env var name to the given value, return true on success
515
WXDLLIMPEXP_BASE bool wxSetEnv(const wxString& var, const wxString& value);
516
517
// remove the env var from environment
518
WXDLLIMPEXP_BASE bool wxUnsetEnv(const wxString& var);
519
520
// Retrieve the complete environment by filling specified map.
521
// Returns true on success or false if an error occurred.
522
WXDLLIMPEXP_BASE bool wxGetEnvMap(wxEnvVariableHashMap *map);
523
524
// ----------------------------------------------------------------------------
525
// Network and username functions.
526
// ----------------------------------------------------------------------------
527
528
// NB: "char *" functions are deprecated, use wxString ones!
529
530
// Get eMail address
531
WXDLLIMPEXP_BASE bool wxGetEmailAddress(wxChar *buf, int maxSize);
532
WXDLLIMPEXP_BASE wxString wxGetEmailAddress();
533
534
// Get hostname.
535
WXDLLIMPEXP_BASE bool wxGetHostName(wxChar *buf, int maxSize);
536
WXDLLIMPEXP_BASE wxString wxGetHostName();
537
538
// Get FQDN
539
WXDLLIMPEXP_BASE wxString wxGetFullHostName();
540
WXDLLIMPEXP_BASE bool wxGetFullHostName(wxChar *buf, int maxSize);
541
542
// Get user ID e.g. jacs (this is known as login name under Unix)
543
WXDLLIMPEXP_BASE bool wxGetUserId(wxChar *buf, int maxSize);
544
WXDLLIMPEXP_BASE wxString wxGetUserId();
545
546
// Get user name e.g. Julian Smart
547
WXDLLIMPEXP_BASE bool wxGetUserName(wxChar *buf, int maxSize);
548
WXDLLIMPEXP_BASE wxString wxGetUserName();
549
550
// Get current Home dir and copy to dest (returns pstr->c_str())
551
WXDLLIMPEXP_BASE wxString wxGetHomeDir();
552
WXDLLIMPEXP_BASE const wxChar* wxGetHomeDir(wxString *pstr);
553
554
// Get the user's (by default use the current user name) home dir,
555
// return empty string on error
556
WXDLLIMPEXP_BASE wxString wxGetUserHome(const wxString& user = wxEmptyString);
557
558
559
#if wxUSE_LONGLONG
560
    typedef wxLongLong wxDiskspaceSize_t;
561
#else
562
    typedef long wxDiskspaceSize_t;
563
#endif
564
565
// get number of total/free bytes on the disk where path belongs
566
WXDLLIMPEXP_BASE bool wxGetDiskSpace(const wxString& path,
567
                                     wxDiskspaceSize_t *pTotal = nullptr,
568
                                     wxDiskspaceSize_t *pFree = nullptr);
569
570
571
572
typedef int (*wxSortCallback)(const void* pItem1,
573
                              const void* pItem2,
574
                              const void* user_data);
575
576
577
#if WXWIN_COMPATIBILITY_3_2
578
wxDEPRECATED_MSG("Use std::sort instead")
579
WXDLLIMPEXP_BASE void wxQsort(void* pbase, size_t total_elems,
580
                              size_t size, wxSortCallback cmp,
581
                              const void* user_data);
582
#endif // WXWIN_COMPATIBILITY_3_2
583
584
#if wxUSE_GUI // GUI only things from now on
585
586
// ----------------------------------------------------------------------------
587
// Launch default browser
588
// ----------------------------------------------------------------------------
589
590
// flags for wxLaunchDefaultBrowser
591
enum
592
{
593
    wxBROWSER_NEW_WINDOW   = 0x01,
594
    wxBROWSER_NOBUSYCURSOR = 0x02
595
};
596
597
// Launch url in the user's default internet browser
598
WXDLLIMPEXP_CORE bool wxLaunchDefaultBrowser(const wxString& url, int flags = 0);
599
600
// Launch document in the user's default application
601
WXDLLIMPEXP_CORE bool wxLaunchDefaultApplication(const wxString& path, int flags = 0);
602
603
// ----------------------------------------------------------------------------
604
// Menu accelerators related things
605
// ----------------------------------------------------------------------------
606
607
// flags for wxStripMenuCodes
608
enum
609
{
610
    // strip '&' characters
611
    wxStrip_Mnemonics = 1,
612
613
    // strip everything after '\t'
614
    wxStrip_Accel = 2,
615
616
    // strip mnemonics of the form "(&X)" appended to the string (used in CJK
617
    // translations)
618
    wxStrip_CJKMnemonics = 4,
619
620
    // strip everything (this doesn't include wxStrip_CJKMnemonics for
621
    // compatibility)
622
    wxStrip_All = wxStrip_Mnemonics | wxStrip_Accel,
623
624
    // strip everything including CJK mnemonics, suitable for menu items labels
625
    // only (despite its name, wxStripMenuCodes() is currently used for control
626
    // labels too)
627
    wxStrip_Menu = wxStrip_All | wxStrip_CJKMnemonics
628
};
629
630
// strip mnemonics and/or accelerators from the label
631
WXDLLIMPEXP_CORE wxString
632
wxStripMenuCodes(const wxString& str, int flags = wxStrip_All);
633
634
// ----------------------------------------------------------------------------
635
// Window search
636
// ----------------------------------------------------------------------------
637
638
// Returns menu item id or wxNOT_FOUND if none.
639
WXDLLIMPEXP_CORE int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
640
641
// Find the wxWindow at the given point. wxGenericFindWindowAtPoint
642
// is always present but may be less reliable than a native version.
643
WXDLLIMPEXP_CORE wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt);
644
WXDLLIMPEXP_CORE wxWindow* wxFindWindowAtPoint(const wxPoint& pt);
645
646
// NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
647
//
648
// Find the window/widget with the given title or label.
649
// Pass a parent to begin the search from, or nullptr to look through
650
// all windows.
651
WXDLLIMPEXP_CORE wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = nullptr);
652
653
// NB: this function is obsolete, use wxWindow::FindWindowByName() instead
654
//
655
// Find window by name, and if that fails, by label.
656
WXDLLIMPEXP_CORE wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = nullptr);
657
658
// ----------------------------------------------------------------------------
659
// Message/event queue helpers
660
// ----------------------------------------------------------------------------
661
662
// Yield to other apps/messages and disable user input
663
WXDLLIMPEXP_CORE bool wxSafeYield(wxWindow *win = nullptr, bool onlyIfNeeded = false);
664
665
// Enable or disable input to all top level windows
666
WXDLLIMPEXP_CORE void wxEnableTopLevelWindows(bool enable = true);
667
668
// Check whether this window wants to process messages, e.g. Stop button
669
// in long calculations.
670
WXDLLIMPEXP_CORE bool wxCheckForInterrupt(wxWindow *wnd);
671
672
// Consume all events until no more left
673
WXDLLIMPEXP_CORE void wxFlushEvents();
674
675
// a class which disables all windows (except, may be, the given one) in its
676
// ctor and enables them back in its dtor
677
class WXDLLIMPEXP_CORE wxWindowDisabler
678
{
679
public:
680
    // this ctor conditionally disables all windows: if the argument is false,
681
    // it doesn't do anything
682
    explicit wxWindowDisabler(bool disable = true);
683
684
    // ctor disables all windows except the given one(s)
685
    explicit wxWindowDisabler(wxWindow *winToSkip, wxWindow *winToSkip2 = nullptr);
686
687
    // dtor enables back all windows disabled by the ctor
688
    ~wxWindowDisabler();
689
690
private:
691
    // disable all windows not in m_windowsToSkip
692
    void DoDisable();
693
694
#if defined(__WXOSX__) && wxOSX_USE_COCOA
695
    void AfterDisable(wxWindow* winToSkip);
696
    void BeforeEnable();
697
698
    wxEventLoop* m_modalEventLoop = nullptr;
699
#endif
700
    wxVector<wxWindow*> m_windowsToSkip;
701
    bool m_disabled;
702
703
    wxDECLARE_NO_COPY_CLASS(wxWindowDisabler);
704
};
705
706
// ----------------------------------------------------------------------------
707
// Cursors
708
// ----------------------------------------------------------------------------
709
710
// Set the cursor to the busy cursor for all windows
711
WXDLLIMPEXP_CORE void wxBeginBusyCursor(const wxCursor *cursor = wxHOURGLASS_CURSOR);
712
713
// Restore cursor to normal
714
WXDLLIMPEXP_CORE void wxEndBusyCursor();
715
716
// true if we're between the above two calls
717
WXDLLIMPEXP_CORE bool wxIsBusy();
718
719
// Convenience class so we can just create a wxBusyCursor object on the stack
720
class WXDLLIMPEXP_CORE wxBusyCursor
721
{
722
public:
723
    wxBusyCursor(const wxCursor* cursor = wxHOURGLASS_CURSOR)
724
        { wxBeginBusyCursor(cursor); }
725
    ~wxBusyCursor()
726
        { wxEndBusyCursor(); }
727
728
    // FIXME: These two methods are currently only implemented (and needed?)
729
    //        in wxGTK.  BusyCursor handling should probably be moved to
730
    //        common code since the wxGTK and wxMSW implementations are very
731
    //        similar except for wxMSW using HCURSOR directly instead of
732
    //        wxCursor..  -- RL.
733
    static const wxCursor &GetStoredCursor();
734
    static const wxCursor GetBusyCursor();
735
};
736
737
void WXDLLIMPEXP_CORE wxGetMousePosition( int* x, int* y );
738
739
// ----------------------------------------------------------------------------
740
// X11 Display access
741
// ----------------------------------------------------------------------------
742
743
#if defined(__X__) || (defined(__WXGTK__) && defined(__UNIX__))
744
745
#ifdef __WXGTK__
746
    WXDLLIMPEXP_CORE void *wxGetDisplay();
747
    enum wxDisplayType
748
    {
749
        wxDisplayNone,
750
        wxDisplayX11,
751
        wxDisplayWayland
752
    };
753
    struct wxDisplayInfo
754
    {
755
        void* dpy;
756
        wxDisplayType type;
757
    };
758
    WXDLLIMPEXP_CORE wxDisplayInfo wxGetDisplayInfo();
759
#endif
760
761
#ifdef __X__
762
    WXDLLIMPEXP_CORE WXDisplay *wxGetDisplay();
763
    WXDLLIMPEXP_CORE bool wxSetDisplay(const wxString& display_name);
764
    WXDLLIMPEXP_CORE wxString wxGetDisplayName();
765
#endif // X or GTK+
766
767
// use this function instead of the functions above in implementation code
768
inline struct _XDisplay *wxGetX11Display()
769
{
770
    return (_XDisplay *)wxGetDisplay();
771
}
772
773
#endif // X11 || wxGTK
774
775
#endif // wxUSE_GUI
776
777
// ----------------------------------------------------------------------------
778
// wxYield(): these functions are obsolete, please use wxApp methods instead!
779
// ----------------------------------------------------------------------------
780
781
// avoid redeclaring this function here if it had been already declared by
782
// wx/app.h, this results in warnings from g++ with -Wredundant-decls
783
#ifndef wx_YIELD_DECLARED
784
#define wx_YIELD_DECLARED
785
786
// Yield to other apps/messages
787
WXDLLIMPEXP_CORE bool wxYield();
788
789
#endif // wx_YIELD_DECLARED
790
791
// Like wxYield, but fails silently if the yield is recursive.
792
WXDLLIMPEXP_CORE bool wxYieldIfNeeded();
793
794
// ----------------------------------------------------------------------------
795
// Windows resources access
796
// ----------------------------------------------------------------------------
797
798
// Windows only: get user-defined resource from the .res file.
799
#ifdef __WINDOWS__
800
    // default resource type for wxLoadUserResource()
801
    extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxUserResourceStr;
802
803
    // Return the pointer to the resource data. This pointer is read-only, use
804
    // the overload below if you need to modify the data.
805
    //
806
    // Notice that the resource type can be either a real string or an integer
807
    // produced by MAKEINTRESOURCE(). In particular, any standard resource type,
808
    // i.e any RT_XXX constant, could be passed here.
809
    //
810
    // Returns true on success, false on failure. Doesn't log an error message
811
    // if the resource is not found (because this could be expected) but does
812
    // log one if any other error occurs.
813
    WXDLLIMPEXP_BASE bool
814
    wxLoadUserResource(const void **outData,
815
                       size_t *outLen,
816
                       const wxString& resourceName,
817
                       const wxChar* resourceType = wxUserResourceStr,
818
                       WXHINSTANCE module = nullptr);
819
820
    // This function allocates a new buffer and makes a copy of the resource
821
    // data, remember to delete[] the buffer. And avoid using it entirely if
822
    // the overload above can be used.
823
    //
824
    // Returns nullptr on failure.
825
    WXDLLIMPEXP_BASE char*
826
    wxLoadUserResource(const wxString& resourceName,
827
                       const wxChar* resourceType = wxUserResourceStr,
828
                       int* pLen = nullptr,
829
                       WXHINSTANCE module = nullptr);
830
#endif // __WINDOWS__
831
832
#endif
833
    // _WX_UTILSH__