]>
Commit | Line | Data |
---|---|---|
d7463f75 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utils.cpp | |
3 | // Purpose: Utility functions and classes | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 2002-09-04 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | // #pragma implementation | |
14 | #endif | |
15 | ||
16 | #include "wx/wx.h" | |
17 | #include "wx/image.h" | |
18 | #include "wx/notebook.h" | |
19 | #include "wx/splitter.h" | |
20 | #include "wx/wfstream.h" | |
21 | #include "wx/datstrm.h" | |
22 | #include "wx/file.h" | |
23 | #include "wx/listctrl.h" | |
24 | #include "wx/tokenzr.h" | |
25 | #include "wx/process.h" | |
26 | #include "wx/mimetype.h" | |
27 | #include "wx/variant.h" | |
28 | #include "wx/cshelp.h" | |
29 | #include "wx/cmdline.h" | |
30 | ||
31 | #include <math.h> | |
32 | ||
33 | #ifdef __WXMSW__ | |
34 | #include <windows.h> | |
35 | #include "wx/msw/winundef.h" | |
36 | #endif | |
37 | ||
38 | #include "utils.h" | |
39 | ||
40 | // Returns the image type, or -1, determined from the extension. | |
41 | int apDetermineImageType(const wxString& filename) | |
42 | { | |
43 | wxString path, name, ext; | |
44 | ||
45 | wxSplitPath(filename, & path, & name, & ext); | |
46 | ||
47 | ext.MakeLower(); | |
48 | if (ext == "jpg" || ext == "jpeg") | |
49 | return wxBITMAP_TYPE_JPEG; | |
50 | else if (ext == "gif") | |
51 | return wxBITMAP_TYPE_GIF; | |
52 | else if (ext == "bmp") | |
53 | return wxBITMAP_TYPE_BMP; | |
54 | else if (ext == "png") | |
55 | return wxBITMAP_TYPE_PNG; | |
56 | else if (ext == "pcx") | |
57 | return wxBITMAP_TYPE_PCX; | |
58 | else if (ext == "tif" || ext == "tiff") | |
59 | return wxBITMAP_TYPE_TIF; | |
60 | else | |
61 | return -1; | |
62 | } | |
63 | ||
64 | // Convert a colour to a 6-digit hex string | |
65 | wxString apColourToHexString(const wxColour& col) | |
66 | { | |
67 | wxString hex; | |
68 | ||
69 | hex += wxDecToHex(col.Red()); | |
70 | hex += wxDecToHex(col.Green()); | |
71 | hex += wxDecToHex(col.Blue()); | |
72 | ||
73 | return hex; | |
74 | } | |
75 | ||
76 | // Convert 6-digit hex string to a colour | |
77 | wxColour apHexStringToColour(const wxString& hex) | |
78 | { | |
79 | unsigned int r = 0; | |
80 | unsigned int g = 0; | |
81 | unsigned int b = 0; | |
82 | r = wxHexToDec(hex.Mid(0, 2)); | |
83 | g = wxHexToDec(hex.Mid(2, 2)); | |
84 | b = wxHexToDec(hex.Mid(4, 2)); | |
85 | ||
86 | return wxColour(r, g, b); | |
87 | } | |
88 | ||
89 | // Convert a wxFont to a string | |
90 | wxString apFontToString(const wxFont& font) | |
91 | { | |
92 | wxString str; | |
93 | str.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font.GetPointSize(), | |
94 | (int) font.GetFamily(), (int) font.GetStyle(), (int) font.GetWeight(), | |
95 | (int) font.GetUnderlined(), font.GetFaceName().c_str()); | |
96 | ||
97 | return str; | |
98 | } | |
99 | ||
3ac35b57 MB |
100 | static inline int StringToInt(const wxString& s) |
101 | { | |
102 | long tmp; | |
103 | s.ToLong(&tmp); | |
104 | ||
105 | return int(tmp); | |
106 | } | |
107 | ||
d7463f75 JS |
108 | // Convert a string to a wxFont |
109 | wxFont apStringToFont(const wxString& str) | |
110 | { | |
111 | int pointSize = 12; | |
112 | int family = wxSWISS; | |
113 | int style = wxNORMAL; | |
114 | int weight = wxNORMAL; | |
115 | int underlined = 0; | |
116 | wxString facename(wxT("")); | |
117 | ||
118 | wxStringTokenizer tkz(str, wxT(",")); | |
119 | int i = 0; | |
120 | while (tkz.HasMoreTokens()) | |
121 | { | |
122 | wxString token = tkz.GetNextToken(); | |
123 | ||
124 | if (i == 0) | |
125 | { | |
3ac35b57 | 126 | pointSize = StringToInt(token); |
d7463f75 JS |
127 | #if defined(__WXGTK__) || defined(__WXMAC__) |
128 | if (pointSize < 8) | |
129 | pointSize = 8; | |
130 | if (pointSize == 9) | |
131 | pointSize = 10; | |
132 | #endif | |
133 | } | |
134 | else if (i == 1) | |
3ac35b57 | 135 | family = StringToInt(token); |
d7463f75 | 136 | else if (i == 2) |
3ac35b57 | 137 | style = StringToInt(token); |
d7463f75 | 138 | else if (i == 3) |
3ac35b57 | 139 | weight = StringToInt(token); |
d7463f75 | 140 | else if (i == 4) |
3ac35b57 | 141 | underlined = StringToInt(token); |
d7463f75 JS |
142 | else if (i == 5) |
143 | { | |
144 | facename = token; | |
145 | #if defined(__WXGTK__) | |
146 | if (facename == wxT("Arial")) | |
147 | facename = wxT("helvetica"); | |
148 | #endif | |
149 | } | |
150 | i ++; | |
151 | ||
152 | } | |
153 | return wxFont(pointSize, family, style, weight, (underlined != 0), facename); | |
154 | } | |
155 | ||
156 | ||
157 | // Get the index of the given named wxNotebook page | |
158 | int apFindNotebookPage(wxNotebook* notebook, const wxString& name) | |
159 | { | |
160 | int i; | |
161 | for (i = 0; i < notebook->GetPageCount(); i++) | |
162 | if (name == notebook->GetPageText(i)) | |
163 | return i; | |
164 | return -1; | |
165 | } | |
166 | ||
167 | /* | |
168 | * View an HTML file | |
169 | */ | |
170 | ||
171 | void apViewHTMLFile(const wxString& url) | |
172 | { | |
173 | #ifdef __WXMSW__ | |
174 | HKEY hKey; | |
175 | TCHAR szCmdName[1024]; | |
176 | DWORD dwType, dw = sizeof(szCmdName); | |
177 | LONG lRes; | |
178 | lRes = RegOpenKey(HKEY_CLASSES_ROOT, "htmlfile\\shell\\open\\command", &hKey); | |
179 | if(lRes == ERROR_SUCCESS && RegQueryValueEx(hKey,(LPTSTR)NULL, NULL, | |
180 | &dwType, (LPBYTE)szCmdName, &dw) == ERROR_SUCCESS) | |
181 | { | |
182 | strcat(szCmdName, (const char*) url); | |
183 | PROCESS_INFORMATION piProcInfo; | |
184 | STARTUPINFO siStartInfo; | |
185 | memset(&siStartInfo, 0, sizeof(STARTUPINFO)); | |
186 | siStartInfo.cb = sizeof(STARTUPINFO); | |
187 | CreateProcess(NULL, szCmdName, NULL, NULL, FALSE, 0, NULL, | |
188 | NULL, &siStartInfo, &piProcInfo ); | |
189 | } | |
190 | if(lRes == ERROR_SUCCESS) | |
191 | RegCloseKey(hKey); | |
192 | #else | |
193 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(wxT("html")); | |
194 | if ( !ft ) | |
195 | { | |
196 | wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types.")); | |
197 | return ; | |
198 | } | |
199 | ||
200 | wxString cmd; | |
201 | bool ok = ft->GetOpenCommand(&cmd, | |
202 | wxFileType::MessageParameters(url, _T(""))); | |
203 | delete ft; | |
204 | ||
205 | if (!ok) | |
206 | { | |
207 | // TODO: some kind of configuration dialog here. | |
208 | wxMessageBox(_("Could not determine the command for running the browser."), | |
209 | wxT("Browsing problem"), wxOK|wxICON_EXCLAMATION); | |
210 | return ; | |
211 | } | |
212 | ||
213 | ok = (wxExecute(cmd, FALSE) != 0); | |
214 | #endif | |
215 | } | |
216 | ||
217 | wxString wxGetTempDir() | |
218 | { | |
219 | wxString dir; | |
220 | #if defined(__WXMAC__) && !defined(__DARWIN__) | |
221 | dir = wxMacFindFolder( (short) kOnSystemDisk, kTemporaryFolderType, kCreateFolder ) ; | |
222 | #else // !Mac | |
223 | dir = wxGetenv(_T("TMP")); | |
224 | if ( dir.empty() ) | |
225 | { | |
226 | dir = wxGetenv(_T("TEMP")); | |
227 | } | |
228 | ||
229 | if ( dir.empty() ) | |
230 | { | |
231 | // default | |
232 | #ifdef __DOS__ | |
233 | dir = _T("."); | |
234 | #else | |
235 | dir = _T("/tmp"); | |
236 | #endif | |
237 | } | |
238 | #endif // Mac/!Mac | |
239 | return dir; | |
240 | } | |
241 | ||
242 | // Invoke app for file type | |
243 | // Eventually we should allow the user to select an app. | |
244 | bool apInvokeAppForFile(const wxString& filename) | |
245 | { | |
246 | wxString path, file, ext; | |
247 | wxSplitPath(filename, & path, & file, & ext); | |
248 | ||
249 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); | |
250 | if ( !ft ) | |
251 | { | |
252 | wxString msg; | |
253 | msg.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."), | |
254 | ext.c_str()); | |
255 | wxMessageBox(msg, wxT("Application Invocation"), wxICON_EXCLAMATION|wxOK); | |
256 | return FALSE; | |
257 | } | |
258 | ||
259 | wxString cmd; | |
260 | bool ok = ft->GetOpenCommand(&cmd, | |
261 | wxFileType::MessageParameters(filename, _T(""))); | |
262 | delete ft; | |
263 | ||
264 | ok = (wxExecute(cmd, FALSE) != 0); | |
265 | ||
266 | return ok; | |
267 | } | |
268 | ||
269 | // Find the absolute path where this application has been run from. | |
270 | // argv0 is wxTheApp->argv[0] | |
271 | // cwd is the current working directory (at startup) | |
272 | // appVariableName is the name of a variable containing the directory for this app, e.g. | |
273 | // MYAPPDIR. This is checked first. | |
274 | ||
275 | wxString apFindAppPath(const wxString& argv0, const wxString& cwd, const wxString& appVariableName) | |
276 | { | |
277 | wxString str; | |
278 | ||
279 | // Try appVariableName | |
280 | if (!appVariableName.IsEmpty()) | |
281 | { | |
282 | str = wxGetenv(appVariableName); | |
283 | if (!str.IsEmpty()) | |
284 | return str; | |
285 | } | |
286 | ||
287 | if (wxIsAbsolutePath(argv0)) | |
288 | return wxPathOnly(argv0); | |
289 | else | |
290 | { | |
291 | // Is it a relative path? | |
292 | wxString currentDir(cwd); | |
293 | if (currentDir.Last() != wxFILE_SEP_PATH) | |
294 | currentDir += wxFILE_SEP_PATH; | |
295 | ||
296 | str = currentDir + argv0; | |
297 | if (wxFileExists(str)) | |
298 | return wxPathOnly(str); | |
299 | } | |
300 | ||
301 | // OK, it's neither an absolute path nor a relative path. | |
302 | // Search PATH. | |
303 | ||
304 | wxPathList pathList; | |
305 | pathList.AddEnvList(wxT("PATH")); | |
306 | str = pathList.FindAbsoluteValidPath(argv0); | |
307 | if (!str.IsEmpty()) | |
308 | return wxPathOnly(str); | |
309 | ||
310 | // Failed | |
311 | return wxEmptyString; | |
312 | } | |
313 | ||
314 | // Adds a context-sensitive help button, for non-Windows platforms | |
315 | void apAddContextHelpButton(wxWindow* parent, wxSizer* sizer, int sizerFlags, int sizerBorder) | |
316 | { | |
317 | #if defined(__WXGTK__) || defined(__WXMAC__) | |
318 | #ifdef __WXMAC__ | |
319 | wxSize buttonSize(20, 20); | |
320 | #else | |
321 | wxSize buttonSize(-1, -1); | |
322 | #endif | |
323 | wxButton *contextButton = new wxContextHelpButton( parent, wxID_CONTEXT_HELP, | |
324 | wxDefaultPosition, buttonSize); | |
325 | sizer->Add( contextButton, 0, sizerFlags, sizerBorder ); | |
326 | ||
327 | // Add a bit of space on the right, to allow for the dialog resizing | |
328 | // handle | |
329 | #ifdef __WXMAC__ | |
330 | sizer->Add(0, 0, 0, wxRIGHT, 10); | |
331 | #endif | |
332 | ||
333 | contextButton->SetHelpText(_("Invokes context-sensitive help for the clicked-on window.")); | |
02ae000a | 334 | #if 0 |
d7463f75 JS |
335 | if (wxGetApp().UsingTooltips()) |
336 | { | |
337 | contextButton->SetToolTip(_("Invokes context-sensitive help for the clicked-on window.")); | |
338 | } | |
339 | #endif | |
02ae000a | 340 | #endif |
d7463f75 JS |
341 | } |
342 | ||
343 | // Get selected wxNotebook page | |
344 | wxWindow* apNotebookGetSelectedPage(wxNotebook* notebook) | |
345 | { | |
346 | int sel = notebook->GetSelection(); | |
347 | if (sel > -1) | |
348 | { | |
349 | return notebook->GetPage(sel); | |
350 | } | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | /* | |
355 | * wxIconInfo | |
356 | */ | |
357 | ||
358 | wxIconInfo::wxIconInfo(const wxString& name) | |
359 | { | |
360 | m_maxStates = 0; | |
361 | m_name = name; | |
362 | int i; | |
363 | for (i = 0; i < wxMAX_ICON_STATES; i++) | |
364 | m_states[i] = 0; | |
365 | } | |
366 | ||
367 | int wxIconInfo::GetIconId(int state, bool enabled) const | |
368 | { | |
369 | wxASSERT ( state < (wxMAX_ICON_STATES * 2) ); | |
370 | wxASSERT ( state < m_maxStates ); | |
371 | ||
372 | return m_states[state * 2 + (enabled ? 0 : 1)]; | |
373 | } | |
374 | ||
375 | void wxIconInfo::SetIconId(int state, bool enabled, int iconId) | |
376 | { | |
377 | wxASSERT ( state < (wxMAX_ICON_STATES * 2) ); | |
378 | if (state+1 > m_maxStates) | |
379 | m_maxStates = state+1; | |
380 | ||
381 | m_states[state * 2 + (enabled ? 0 : 1)] = iconId; | |
382 | } | |
383 | ||
384 | /* | |
385 | * wxIconTable | |
386 | * Contains a list of wxIconInfos | |
387 | */ | |
388 | ||
389 | wxIconTable::wxIconTable(wxImageList* imageList) | |
390 | { | |
391 | m_imageList = imageList; | |
392 | DeleteContents(TRUE); | |
393 | } | |
394 | ||
395 | void wxIconTable::AppendInfo(wxIconInfo* info) | |
396 | { | |
397 | Append(info); | |
398 | } | |
399 | ||
400 | // Easy way of initialising both the image list and the | |
401 | // table. It will generate image ids itself while appending the icon. | |
402 | bool wxIconTable::AddInfo(const wxString& name, const wxIcon& icon, int state, bool enabled) | |
403 | { | |
404 | wxASSERT (m_imageList != NULL); | |
405 | ||
406 | wxIconInfo* info = FindInfo(name); | |
407 | if (!info) | |
408 | { | |
409 | info = new wxIconInfo(name); | |
410 | Append(info); | |
411 | } | |
412 | info->SetIconId(state, enabled, m_imageList->Add(icon)); | |
413 | return TRUE; | |
414 | } | |
415 | ||
416 | wxIconInfo* wxIconTable::FindInfo(const wxString& name) const | |
417 | { | |
f8105809 | 418 | wxNode* node = GetFirst(); |
d7463f75 JS |
419 | while (node) |
420 | { | |
f8105809 | 421 | wxIconInfo* info = (wxIconInfo*) node->GetData(); |
d7463f75 JS |
422 | if (info->GetName() == name) |
423 | return info; | |
f8105809 | 424 | node = node->GetNext(); |
d7463f75 JS |
425 | } |
426 | return NULL; | |
427 | } | |
428 | ||
429 | int wxIconTable::GetIconId(const wxString& name, int state, bool enabled) const | |
430 | { | |
431 | wxIconInfo* info = FindInfo(name); | |
432 | if (!info) | |
433 | return -1; | |
434 | return info->GetIconId(state, enabled); | |
435 | } | |
436 | ||
437 | bool wxIconTable::SetIconId(const wxString& name, int state, bool enabled, int iconId) | |
438 | { | |
439 | wxIconInfo* info = FindInfo(name); | |
440 | if (!info) | |
441 | return FALSE; | |
442 | info->SetIconId(state, enabled, iconId); | |
443 | return TRUE; | |
444 | } | |
445 | ||
446 | // Output stream operators | |
447 | ||
448 | wxOutputStream& operator <<(wxOutputStream& stream, const wxString& s) | |
449 | { | |
450 | stream.Write(s, s.Length()); | |
451 | return stream; | |
452 | } | |
453 | ||
454 | wxOutputStream& operator <<(wxOutputStream& stream, long l) | |
455 | { | |
456 | wxString str; | |
457 | str.Printf(_T("%ld"), l); | |
458 | return stream << str; | |
459 | } | |
460 | ||
461 | wxOutputStream& operator <<(wxOutputStream& stream, const char c) | |
462 | { | |
463 | wxString str; | |
464 | str.Printf(_T("%c"), c); | |
465 | return stream << str; | |
466 | } | |
467 | ||
468 | // Convert characters to HTML equivalents | |
469 | wxString ctEscapeHTMLCharacters(const wxString& str) | |
470 | { | |
471 | wxString s; | |
472 | size_t len = str.Length(); | |
473 | size_t i; | |
474 | for (i = 0; i < len; i++) | |
475 | { | |
476 | wxChar c = str.GetChar(i); | |
477 | if (c == _T('<')) | |
478 | s += _T("<"); | |
479 | else if (c == _T('>')) | |
480 | s += _T(">"); | |
481 | else if (c == _T('&')) | |
482 | s += _T("&"); | |
483 | else | |
484 | s += c; | |
485 | } | |
486 | return s; | |
f8105809 | 487 | } |
e7767867 JS |
488 | |
489 | // Match 'matchText' against 'matchAgainst', optionally constraining to | |
490 | // whole-word only. | |
491 | bool ctMatchString(const wxString& matchAgainst, const wxString& matchText, bool wholeWordOnly) | |
492 | { | |
493 | // Fast operation if not matching against whole words only | |
494 | if (!wholeWordOnly) | |
495 | return (matchAgainst.Find(matchText) != -1); | |
496 | ||
497 | wxString left(matchAgainst); | |
498 | bool success = FALSE; | |
499 | int pos = 0; | |
500 | int matchTextLen = (int) matchText.Length(); | |
501 | while (!success && !matchAgainst.IsEmpty()) | |
502 | { | |
503 | pos = left.Find(matchText); | |
504 | if (pos == -1) | |
505 | return FALSE; | |
506 | ||
507 | bool firstCharOK = FALSE; | |
508 | bool lastCharOK = FALSE; | |
509 | if (pos == 0 || !wxIsalnum(left[(size_t) (pos-1)])) | |
510 | firstCharOK = TRUE; | |
511 | ||
512 | if (((pos + matchTextLen) == (int) left.Length()) || !wxIsalnum(left[(size_t) (pos + matchTextLen)])) | |
513 | lastCharOK = TRUE; | |
514 | ||
515 | if (firstCharOK && lastCharOK) | |
516 | success = TRUE; | |
517 | ||
518 | left = left.Mid(pos+1); | |
519 | } | |
520 | return success; | |
521 | } |