/
GeneralWxUtils.cpp
368 lines (335 loc) · 9.66 KB
/
GeneralWxUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* GeoDa TM, Copyright (C) 2011-2014 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GeneralWxUtils.h"
#include <wx/tokenzr.h>
#include <wx/filename.h>
#include <wx/platinfo.h>
#include <wx/app.h> // needed for wxTheApp
#ifdef __WXMSW__
#include <Windows.h> // needed for GetModuleFleName
#endif
#include <wx/log.h>
#include "logger.h"
wxString GeneralWxUtils::wxFindAppPath(const wxString& argv0,
const wxString& cwd,
const wxString& appVariableName)
{
wxString str;
/*
// Try appVariableName
if (!appVariableName.IsEmpty())
{
str = wxGetenv(appVariableName);
if (!str.IsEmpty())
return str;
}
#if defined(__WXMAC__) && !defined(__DARWIN__)
// On Mac, the current directory is the relevant one when
// the application starts.
return cwd;
#endif
if (wxIsAbsolutePath(argv0)) {
return wxPathOnly(argv0);
}
else {
// Is it a relative path?
wxString currentDir(cwd);
if (currentDir.Last() != wxFILE_SEP_PATH)
currentDir += wxFILE_SEP_PATH;
str = currentDir + argv0;
if (wxFileExists(str))
return wxPathOnly(str);
}
// OK, it's neither an absolute path nor a relative path.
// Search PATH.
wxPathList pathList;
pathList.AddEnvList("PATH");
str = pathList.FindAbsoluteValidPath(argv0);
if (!str.IsEmpty())
return wxPathOnly(str);
// Failed
*/
return wxEmptyString;
}
wxOperatingSystemId GeneralWxUtils::GetOsId()
{
static wxOperatingSystemId osId =
wxPlatformInfo::Get().GetOperatingSystemId();
return osId;
}
bool GeneralWxUtils::isMac()
{
static bool r = (GetOsId() & wxOS_MAC ? true : false);
return r;
}
bool GeneralWxUtils::isWindows()
{
static bool r = (GetOsId() & wxOS_WINDOWS ? true : false);
return r;
}
bool GeneralWxUtils::isUnix()
{
static bool r = (GetOsId() & wxOS_UNIX ? true : false);
return r;
}
bool GeneralWxUtils::isXP()
{
static bool r = (GetOsId() & wxOS_WINDOWS ? true : false);
if (r) {
int majorVsn = 0;
int minorVsn = 0;
wxGetOsVersion(&majorVsn, &minorVsn);
r = r && (majorVsn == 5);
// This includes Windows 2000 (5.0),
// XP (5.1) and Windows Server 2003 (5.3)
}
return r;
}
bool GeneralWxUtils::isVista()
{
static bool r = (GetOsId() & wxOS_WINDOWS ? true : false);
if (r) {
int majorVsn = 0;
int minorVsn = 0;
wxGetOsVersion(&majorVsn, &minorVsn);
r = r && (majorVsn == 6) && (minorVsn == 0);
// This includes Windows Server 2008 and Vista
}
return r;
}
bool GeneralWxUtils::isX86()
{
#ifdef _WIN64 || __amd64__
return false;
#else
return true;
#endif
}
bool GeneralWxUtils::isX64()
{
return !isX86();
}
bool GeneralWxUtils::isDebug()
{
#if defined(__WXDEBUG__) || defined(DEBUG)
return true;
#else
return false;
#endif
}
bool GeneralWxUtils::isBigEndian()
{
static bool r =
(wxPlatformInfo::Get().GetEndianness() & wxENDIAN_BIG);
return r;
}
bool GeneralWxUtils::isLittleEndian()
{
static bool r =
(wxPlatformInfo::Get().GetEndianness() & wxENDIAN_LITTLE);
return r;
}
/*
* ReplaceMenu: finds MenuBar menu with given title and replaces with newMenu
* additionally, the previous menu is deleted. Returns true on success.
*/
bool GeneralWxUtils::ReplaceMenu(wxMenuBar* mb, const wxString& title,
wxMenu* newMenu)
{
LOG_MSG("Entering GeneralWxUtils::ReplaceMenu");
LOG(title);
//int menu_count = mb->GetMenuCount();
//LOG(menu_count);
//for (int i=0; i<menu_count; i++) {
// LOG_MSG(mb->GetMenuLabelText(i));
//}
int m_ind = mb->FindMenu(title);
if (m_ind == wxNOT_FOUND) {
delete newMenu;
LOG_MSG("Exiting GeneralWxUtils::ReplaceMenu in unexpected way!");
return false;
}
//wxMenu* prev_opt_menu = mb->GetMenu(m_ind);
//mb->Replace(m_ind, newMenu, title);
wxMenu* prev_opt_menu = mb->Remove(m_ind);
mb->Insert(m_ind, newMenu, title);
// The following line shouldn't be needed, but on wxWidgets 2.9.2, the
// menu label is set to empty after Replace is called.
//mb->SetMenuLabel(m_ind, title);
if (prev_opt_menu) delete prev_opt_menu;
LOG_MSG("Exiting GeneralWxUtils::ReplaceMenu");
return true;
}
/*
* EnableMenuAll: Given a menubar pointer and top-level menu title,
* this method traverses every menu item in the menu tree (menu items
* include actual clickable menu items and submenus) and either enables
* or disables the item according to the value of bool enable. This
* is useful in situations where one wants to disable everything in
* a menu except for one deeply-nested menu item (see EnableMenuItem).
* Boolean value 'true' is returned iff the top-level menu is found.
* The enable/disable state of the actual top-level menu is not changed.
*/
bool GeneralWxUtils::EnableMenuAll(wxMenuBar* mb, const wxString& title,
bool enable)
{
if (!mb ) return false;
int mPos = mb->FindMenu(title);
if (mPos == wxNOT_FOUND) return false;
wxMenu* menu = mb->GetMenu(mPos);
GeneralWxUtils::EnableMenuRecursive(menu, enable);
return true;
}
/*
* This is intended to be a helper function to EnableMenuAll with
* the menubar argument, but can be called directly on an valid wxMenu.
*/
void GeneralWxUtils::EnableMenuRecursive(wxMenu* menu, bool enable)
{
if (!menu) return;
int cnt = menu->GetMenuItemCount();
for (int i=0; i<cnt; i++) {
wxMenuItem* mItem = menu->FindItemByPosition(i);
//wxString msg("menu item '");
//msg << mItem->GetItemLabelText() << "' enable = " << enable;
//LOG_MSG(msg);
mItem->Enable(enable);
//wxString msg2("menu item '");
//msg2 << mItem->GetItemLabelText() << "' IsEnabled() = ";
//msg2 << mItem->IsEnabled();
//LOG_MSG(msg2);
GeneralWxUtils::EnableMenuRecursive(mItem->GetSubMenu(), enable);
}
}
/*
* EnableMenuItem: This method assumes there are no submenus within
* submenus.
*/
bool GeneralWxUtils::EnableMenuItem(wxMenuBar* mb, const wxString& menuTitle,
int id, bool enable)
{
if (!mb) return false;
wxMenu* menu = NULL;
wxMenuItem* mItem = mb->FindItem(id);
menu=mb->GetMenu(mb->FindMenu(menuTitle));
if (!mItem || !menu) return false;
//LOG_MSG("item title:") + mItem->GetText();
mItem->Enable(enable);
// Check if embedded in a submenu and enable as needed.
wxMenu* subMenu = NULL;
menu->FindItem(id, &subMenu);
if (menu == subMenu) return true;
int menuCnt = menu->GetMenuItemCount();
int subMenuCnt = subMenu->GetMenuItemCount();
int subMenuIndex = wxNOT_FOUND;
int i = 0;
while (subMenuIndex == wxNOT_FOUND && i < menuCnt) {
mItem = menu->FindItemByPosition(i);
if (mItem->IsSubMenu() && mItem->GetSubMenu() == subMenu)
subMenuIndex = i;
i++;
}
if (enable) {
menu->FindItemByPosition(subMenuIndex)->Enable(true);
return true;
}
bool anyEnabled = false;
// Check if there are any other items enabled in the submenu.
for (i = 0; i<subMenuCnt; i++) {
mItem = subMenu->FindItemByPosition(i);
if (!mItem->IsSeparator())
anyEnabled = anyEnabled || mItem->IsEnabled();
}
if (!anyEnabled)
menu->FindItemByPosition(subMenuIndex)->Enable(false);
return true;
}
bool GeneralWxUtils::EnableMenuItem(wxMenuBar* m, int id, bool enable)
{
if (!m) return false;
wxMenuItem* mi = m->FindItem(id);
if (mi) {
mi->Enable(enable);
return true;
}
return false;
}
bool GeneralWxUtils::EnableMenuItem(wxMenu* m, int id, bool enable)
{
if (!m) return false;
wxMenuItem* mi = m->FindItem(id);
if (mi) {
mi->Enable(enable);
return true;
}
return false;
}
bool GeneralWxUtils::CheckMenuItem(wxMenuBar* m, int id, bool check)
{
if (!m) return false;
wxMenuItem* mi = m->FindItem(id);
if (mi && mi->IsCheckable()) {
mi->Check(check);
return true;
}
return false;
}
bool GeneralWxUtils::CheckMenuItem(wxMenu* menu, int id, bool check)
{
if (!menu) return false;
wxMenuItem* mItem = menu->FindItem(id);
if (!mItem) return false;
if (!mItem->IsCheckable()) return false;
mItem->Check(check);
return true;
}
bool GeneralWxUtils::SetMenuItemText(wxMenu* menu, int id,
const wxString& text)
{
if (!menu) return false;
wxMenuItem* mItem = menu->FindItem(id);
if (!mItem) return false;
mItem->SetItemLabel(text);
return true;
}
wxMenu* GeneralWxUtils::FindMenu(wxMenuBar* mb, const wxString& menuTitle)
{
if (!mb) return 0;
int menu = mb->FindMenu(menuTitle);
if (menu == wxNOT_FOUND) return 0;
return mb->GetMenu(menu);
}
// Useful functions for tree traversal
// int wxMenuBar::FindMenu(const wxString& title) const:
// - returns wxNOT_FOUND or position of top-level menu
// - ignores "&" characters in string.
// wxMenu* GetMenu(int menuIndex) const:
// - returns menu at menuIndex.
// It appears as though there is no way to easily discover
// which top-level menu a nested menu-item is associated with
// short of traversing through the entire menu-bar. For this
// reason, it will be necessary to pass in the name of top-level
// menu where the menu-item exists.
// This method might be implemented in the future for the purpose
// of doing a tree-traversal of the menu-tree. This will be needed
// if we support sub-menus within sub-menus.
bool GeneralWxUtils::EnableMenuItemHelper(wxMenuItem* mItem, int id)
{
return false;
}