1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/splitter.h"
28 #include "wx/datstrm.h"
30 #include "wx/listctrl.h"
31 #include "wx/process.h"
32 #include "wx/variant.h"
33 #include "wx/cmdline.h"
37 #include "wx/wfstream.h"
38 #include "wx/cshelp.h"
40 #include "wx/imaglist.h"
41 #include "wx/tokenzr.h"
42 #include "wx/notebook.h"
43 #include "wx/mimetype.h"
46 // Returns the image type, or -1, determined from the extension.
47 int apDetermineImageType(const wxString
& filename
)
49 wxString path
, name
, ext
;
51 wxSplitPath(filename
, & path
, & name
, & ext
);
54 if (ext
== _T("jpg") || ext
== _T("jpeg"))
55 return wxBITMAP_TYPE_JPEG
;
56 else if (ext
== _T("gif"))
57 return wxBITMAP_TYPE_GIF
;
58 else if (ext
== _T("bmp"))
59 return wxBITMAP_TYPE_BMP
;
60 else if (ext
== _T("png"))
61 return wxBITMAP_TYPE_PNG
;
62 else if (ext
== _T("pcx"))
63 return wxBITMAP_TYPE_PCX
;
64 else if (ext
== _T("tif") || ext
== _T("tiff"))
65 return wxBITMAP_TYPE_TIF
;
70 // Convert a colour to a 6-digit hex string
71 wxString
apColourToHexString(const wxColour
& col
)
75 hex
+= wxDecToHex(col
.Red());
76 hex
+= wxDecToHex(col
.Green());
77 hex
+= wxDecToHex(col
.Blue());
82 // Convert 6-digit hex string to a colour
83 wxColour
apHexStringToColour(const wxString
& hex
)
85 unsigned int r
= wxHexToDec(hex
.Mid(0, 2));
86 unsigned int g
= wxHexToDec(hex
.Mid(2, 2));
87 unsigned int b
= wxHexToDec(hex
.Mid(4, 2));
89 return wxColour(r
, g
, b
);
92 // Convert a wxFont to a string
93 wxString
apFontToString(const wxFont
& font
)
96 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
97 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
98 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
103 static inline int StringToInt(const wxString
& s
)
111 // Convert a string to a wxFont
112 wxFont
apStringToFont(const wxString
& str
)
115 int family
= wxSWISS
;
116 int style
= wxNORMAL
;
117 int weight
= wxNORMAL
;
119 wxString
facename(wxT(""));
121 wxStringTokenizer
tkz(str
, wxT(","));
123 while (tkz
.HasMoreTokens())
125 wxString token
= tkz
.GetNextToken();
129 pointSize
= StringToInt(token
);
130 #if defined(__WXGTK__) || defined(__WXMAC__)
138 family
= StringToInt(token
);
140 style
= StringToInt(token
);
142 weight
= StringToInt(token
);
144 underlined
= StringToInt(token
);
148 #if defined(__WXGTK__)
149 if (facename
== wxT("Arial"))
150 facename
= wxT("helvetica");
156 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
160 // Get the index of the given named wxNotebook page
161 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
164 for (i
= 0; i
< (int)notebook
->GetPageCount(); i
++)
165 if (name
== notebook
->GetPageText(i
))
174 void apViewHTMLFile(const wxString
& url
)
178 TCHAR szCmdName
[1024];
179 DWORD dwType
, dw
= sizeof(szCmdName
);
181 lRes
= RegOpenKey(HKEY_CLASSES_ROOT
, _T("htmlfile\\shell\\open\\command"), &hKey
);
182 if(lRes
== ERROR_SUCCESS
&& RegQueryValueEx(hKey
,(LPTSTR
)NULL
, NULL
,
183 &dwType
, (LPBYTE
)szCmdName
, &dw
) == ERROR_SUCCESS
)
185 wxStrcat(szCmdName
, (const wxChar
*) url
);
186 PROCESS_INFORMATION piProcInfo
;
187 STARTUPINFO siStartInfo
;
188 memset(&siStartInfo
, 0, sizeof(STARTUPINFO
));
189 siStartInfo
.cb
= sizeof(STARTUPINFO
);
190 CreateProcess(NULL
, szCmdName
, NULL
, NULL
, false, 0, NULL
,
191 NULL
, &siStartInfo
, &piProcInfo
);
193 if(lRes
== ERROR_SUCCESS
)
196 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(wxT("html"));
199 wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types."));
204 bool ok
= ft
->GetOpenCommand(&cmd
,
205 wxFileType::MessageParameters(url
, _T("")));
210 // TODO: some kind of configuration dialog here.
211 wxMessageBox(_("Could not determine the command for running the browser."),
212 wxT("Browsing problem"), wxOK
|wxICON_EXCLAMATION
);
216 ok
= (wxExecute(cmd
, false) != 0);
220 wxString
wxGetTempDir()
223 #if defined(__WXMAC__) && !defined(__DARWIN__)
224 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
226 wxString
dirEnv(wxGetenv(_T("TMP")));
230 wxString
envVar(wxGetenv(_T("TEMP")));
247 // Invoke app for file type
248 // Eventually we should allow the user to select an app.
249 bool apInvokeAppForFile(const wxString
& filename
)
251 wxString path
, file
, ext
;
252 wxSplitPath(filename
, & path
, & file
, & ext
);
254 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
258 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
260 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
265 ft
->GetOpenCommand(&cmd
, wxFileType::MessageParameters(filename
, _T("")));
268 return (wxExecute(cmd
, false) != 0);
271 // Find the absolute path where this application has been run from.
272 // argv0 is wxTheApp->argv[0]
273 // cwd is the current working directory (at startup)
274 // appVariableName is the name of a variable containing the directory for this app, e.g.
275 // MYAPPDIR. This is checked first.
277 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
279 // Try appVariableName
280 if (!appVariableName
.IsEmpty())
282 wxString
strVar(wxGetenv(appVariableName
.c_str()));
283 if (!strVar
.IsEmpty())
287 if (wxIsAbsolutePath(argv0
))
288 return wxPathOnly(argv0
);
291 // Is it a relative path?
292 wxString
currentDir(cwd
);
293 if (currentDir
.Last() != wxFILE_SEP_PATH
)
294 currentDir
+= wxFILE_SEP_PATH
;
297 if (wxFileExists(currentDir
))
298 return wxPathOnly(currentDir
);
301 // OK, it's neither an absolute path nor a relative path.
305 pathList
.AddEnvList(wxT("PATH"));
306 wxString strPath
= pathList
.FindAbsoluteValidPath(argv0
);
307 if (!strPath
.IsEmpty())
308 return wxPathOnly(strPath
);
311 return wxEmptyString
;
314 // Adds a context-sensitive help button, for non-Windows platforms
315 void apAddContextHelpButton(wxWindow
*
316 #if defined(__WXGTK__) || defined(__WXMAC__)
322 #if defined(__WXGTK__) || defined(__WXMAC__)
328 #if defined(__WXGTK__) || defined(__WXMAC__)
334 #if defined(__WXGTK__) || defined(__WXMAC__)
337 WXUNUSED(sizerBorder
)
341 #if defined(__WXGTK__) || defined(__WXMAC__)
343 wxSize
buttonSize(20, 20);
345 wxSize buttonSize
= wxDefaultSize
;
347 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
348 wxDefaultPosition
, buttonSize
);
349 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
351 // Add a bit of space on the right, to allow for the dialog resizing
354 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
357 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
359 if (wxGetApp().UsingTooltips())
361 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
367 // Get selected wxNotebook page
368 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
370 int sel
= notebook
->GetSelection();
373 return notebook
->GetPage(sel
);
382 wxIconInfo::wxIconInfo(const wxString
& name
)
387 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
391 int wxIconInfo::GetIconId(int state
, bool enabled
) const
393 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
394 wxASSERT ( state
< m_maxStates
);
396 return m_states
[state
* 2 + (enabled
? 0 : 1)];
399 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
401 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
402 if (state
+1 > m_maxStates
)
403 m_maxStates
= state
+1;
405 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
410 * Contains a list of wxIconInfos
413 wxIconTable::wxIconTable(wxImageList
* imageList
)
415 m_imageList
= imageList
;
416 WX_CLEAR_LIST(wxIconTable
,*this);
419 void wxIconTable::AppendInfo(wxIconInfo
* info
)
424 // Easy way of initialising both the image list and the
425 // table. It will generate image ids itself while appending the icon.
426 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
428 wxASSERT (m_imageList
!= NULL
);
430 wxIconInfo
* info
= FindInfo(name
);
433 info
= new wxIconInfo(name
);
436 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
440 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
442 wxObjectList::compatibility_iterator node
= GetFirst();
445 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
446 if (info
->GetName() == name
)
448 node
= node
->GetNext();
453 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
455 wxIconInfo
* info
= FindInfo(name
);
458 return info
->GetIconId(state
, enabled
);
461 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
463 wxIconInfo
* info
= FindInfo(name
);
466 info
->SetIconId(state
, enabled
, iconId
);
470 // Output stream operators
472 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
474 stream
.Write(s
, s
.Length());
478 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
481 str
.Printf(_T("%ld"), l
);
482 return stream
<< str
;
485 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxChar c
)
488 str
.Printf(_T("%c"), c
);
489 return stream
<< str
;
492 // Convert characters to HTML equivalents
493 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
496 size_t len
= str
.Length();
498 for (i
= 0; i
< len
; i
++)
500 wxChar c
= str
.GetChar(i
);
503 else if (c
== _T('>'))
505 else if (c
== _T('&'))
513 // Match 'matchText' against 'matchAgainst', optionally constraining to
515 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
517 // Fast operation if not matching against whole words only
519 return (matchAgainst
.Find(matchText
) != wxNOT_FOUND
);
521 wxString
left(matchAgainst
);
522 bool success
= false;
523 int matchTextLen
= (int) matchText
.Length();
524 while (!success
&& !matchAgainst
.IsEmpty())
526 int pos
= left
.Find(matchText
);
527 if (pos
== wxNOT_FOUND
)
530 bool firstCharOK
= false;
531 bool lastCharOK
= false;
532 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
535 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
538 if (firstCharOK
&& lastCharOK
)
541 left
= left
.Mid(pos
+1);