/src/wxwidgets/src/common/sysopt.cpp
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: src/common/sysopt.cpp |
3 | | // Purpose: wxSystemOptions |
4 | | // Author: Julian Smart |
5 | | // Created: 2001-07-10 |
6 | | // Copyright: (c) Julian Smart |
7 | | // Licence: wxWindows licence |
8 | | ///////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | // ============================================================================ |
11 | | // declarations |
12 | | // ============================================================================ |
13 | | |
14 | | // --------------------------------------------------------------------------- |
15 | | // headers |
16 | | // --------------------------------------------------------------------------- |
17 | | |
18 | | // For compilers that support precompilation, includes "wx.h". |
19 | | #include "wx/wxprec.h" |
20 | | |
21 | | |
22 | | #if wxUSE_SYSTEM_OPTIONS |
23 | | |
24 | | #include "wx/sysopt.h" |
25 | | |
26 | | #ifndef WX_PRECOMP |
27 | | #include "wx/app.h" |
28 | | #include "wx/string.h" |
29 | | #include "wx/arrstr.h" |
30 | | #endif |
31 | | |
32 | | // Include header containing wxIsCatchUnhandledExceptionsDisabled() declaration. |
33 | | #include "wx/private/safecall.h" |
34 | | |
35 | | #include <unordered_map> |
36 | | |
37 | | // ---------------------------------------------------------------------------- |
38 | | // private globals |
39 | | // ---------------------------------------------------------------------------- |
40 | | |
41 | | namespace |
42 | | { |
43 | | |
44 | | // Keys of this map are option names and values are their (potentially empty) |
45 | | // values. |
46 | | std::unordered_map<wxString, wxString> gs_options; |
47 | | |
48 | | // Name of a system option that we handle specially for performance reasons. |
49 | | constexpr char CATCH_UNHANDLED_EXCEPTIONS[] = "catch-unhandled-exceptions"; |
50 | | |
51 | | // Cached return value of wxIsCatchUnhandledExceptionsDisabled(). |
52 | | int gs_catchUnhandledExceptionsDisabled = -1; |
53 | | |
54 | | } // anonymous namespace |
55 | | |
56 | | // ============================================================================ |
57 | | // wxSystemOptions implementation |
58 | | // ============================================================================ |
59 | | |
60 | | // Option functions (arbitrary name/value mapping) |
61 | | void wxSystemOptions::SetOption(const wxString& name, const wxString& value) |
62 | 0 | { |
63 | 0 | if ( name == CATCH_UNHANDLED_EXCEPTIONS ) |
64 | 0 | { |
65 | | // Invalidate the cached value. |
66 | 0 | gs_catchUnhandledExceptionsDisabled = -1; |
67 | 0 | } |
68 | |
|
69 | 0 | gs_options[name] = value; |
70 | 0 | } |
71 | | |
72 | | void wxSystemOptions::SetOption(const wxString& name, int value) |
73 | 0 | { |
74 | 0 | SetOption(name, wxString::Format(wxT("%d"), value)); |
75 | 0 | } |
76 | | |
77 | | wxString wxSystemOptions::GetOption(const wxString& name) |
78 | 0 | { |
79 | 0 | auto it = gs_options.find(name); |
80 | |
|
81 | 0 | if ( it == gs_options.end() ) |
82 | 0 | { |
83 | | // look in the environment: first for a variable named "wx_appname_name" |
84 | | // which can be set to affect the behaviour or just this application |
85 | | // and then for "wx_name" which can be set to change the option globally |
86 | 0 | wxString var(name); |
87 | 0 | var.Replace(wxT("."), wxT("_")); // '.'s not allowed in env var names |
88 | 0 | var.Replace(wxT("-"), wxT("_")); // and neither are '-'s |
89 | |
|
90 | 0 | wxString appname; |
91 | 0 | if ( wxTheApp ) |
92 | 0 | appname = wxTheApp->GetAppName(); |
93 | |
|
94 | 0 | wxString val; |
95 | 0 | if ( !appname.empty() ) |
96 | 0 | val = wxGetenv(wxT("wx_") + appname + wxT('_') + var); |
97 | |
|
98 | 0 | if ( val.empty() ) |
99 | 0 | val = wxGetenv(wxT("wx_") + var); |
100 | | |
101 | | // save it even if it is empty to avoid calling wxGetenv() in the |
102 | | // future if this option is requested again |
103 | 0 | it = gs_options.insert({name, val}).first; |
104 | 0 | } |
105 | |
|
106 | 0 | return it->second; |
107 | 0 | } |
108 | | |
109 | | int wxSystemOptions::GetOptionInt(const wxString& name) |
110 | 0 | { |
111 | 0 | return wxAtoi (GetOption(name)); |
112 | 0 | } |
113 | | |
114 | | bool wxSystemOptions::HasOption(const wxString& name) |
115 | 0 | { |
116 | 0 | return !GetOption(name).empty(); |
117 | 0 | } |
118 | | |
119 | | bool wxIsCatchUnhandledExceptionsDisabled() |
120 | 0 | { |
121 | 0 | if ( gs_catchUnhandledExceptionsDisabled == -1 ) |
122 | 0 | { |
123 | 0 | gs_catchUnhandledExceptionsDisabled = |
124 | 0 | wxSystemOptions::IsFalse(CATCH_UNHANDLED_EXCEPTIONS); |
125 | 0 | } |
126 | |
|
127 | 0 | return gs_catchUnhandledExceptionsDisabled; |
128 | 0 | } |
129 | | |
130 | | #endif // wxUSE_SYSTEM_OPTIONS |