1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/splitter.h"
24 #include "wx/datstrm.h"
26 #include "wx/listctrl.h"
27 #include "wx/process.h"
28 #include "wx/variant.h"
29 #include "wx/cmdline.h"
30 #include "wx/msgdlg.h"
38 #include "wx/wfstream.h"
39 #include "wx/cshelp.h"
41 #include "wx/imaglist.h"
42 #include "wx/tokenzr.h"
43 #include "wx/notebook.h"
44 #include "wx/mimetype.h"
47 // Returns the image type, or -1, determined from the extension.
48 int apDetermineImageType(const wxString
& filename
)
50 wxString path
, name
, ext
;
52 wxSplitPath(filename
, & path
, & name
, & ext
);
55 if (ext
== _T("jpg") || ext
== _T("jpeg"))
56 return wxBITMAP_TYPE_JPEG
;
57 else if (ext
== _T("gif"))
58 return wxBITMAP_TYPE_GIF
;
59 else if (ext
== _T("bmp"))
60 return wxBITMAP_TYPE_BMP
;
61 else if (ext
== _T("png"))
62 return wxBITMAP_TYPE_PNG
;
63 else if (ext
== _T("pcx"))
64 return wxBITMAP_TYPE_PCX
;
65 else if (ext
== _T("tif") || ext
== _T("tiff"))
66 return wxBITMAP_TYPE_TIF
;
71 // Convert a colour to a 6-digit hex string
72 wxString
apColourToHexString(const wxColour
& col
)
76 hex
+= wxDecToHex(col
.Red());
77 hex
+= wxDecToHex(col
.Green());
78 hex
+= wxDecToHex(col
.Blue());
83 // Convert 6-digit hex string to a colour
84 wxColour
apHexStringToColour(const wxString
& hex
)
86 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
87 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
88 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
90 return wxColour(r
, g
, b
);
93 // Convert a wxFont to a string
94 wxString
apFontToString(const wxFont
& font
)
97 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
98 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
99 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
104 static inline int StringToInt(const wxString
& s
)
112 // Convert a string to a wxFont
113 wxFont
apStringToFont(const wxString
& str
)
116 int family
= wxSWISS
;
117 int style
= wxNORMAL
;
118 int weight
= wxNORMAL
;
122 wxStringTokenizer
tkz(str
, wxT(","));
124 while (tkz
.HasMoreTokens())
126 wxString token
= tkz
.GetNextToken();
130 pointSize
= StringToInt(token
);
131 #if defined(__WXGTK__) || defined(__WXMAC__)
139 family
= StringToInt(token
);
141 style
= StringToInt(token
);
143 weight
= StringToInt(token
);
145 underlined
= StringToInt(token
);
149 #if defined(__WXGTK__)
150 if (facename
== wxT("Arial"))
151 facename
= wxT("helvetica");
157 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
161 // Get the index of the given named wxNotebook page
162 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
165 for (i
= 0; i
< (int)notebook
->GetPageCount(); i
++)
166 if (name
== notebook
->GetPageText(i
))
171 wxString
wxGetTempDir()
174 #if defined(__WXMAC__) && !defined(__DARWIN__)
175 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
177 wxString
dirEnv(wxGetenv(_T("TMP")));
181 wxString
envVar(wxGetenv(_T("TEMP")));
198 // Invoke app for file type
199 // Eventually we should allow the user to select an app.
200 bool apInvokeAppForFile(const wxString
& filename
)
202 wxString path
, file
, ext
;
203 wxSplitPath(filename
, & path
, & file
, & ext
);
205 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
209 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
211 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
216 ft
->GetOpenCommand(&cmd
, wxFileType::MessageParameters(filename
, wxEmptyString
));
219 return (wxExecute(cmd
, false) != 0);
222 // Find the absolute path where this application has been run from.
223 // argv0 is wxTheApp->argv[0]
224 // cwd is the current working directory (at startup)
225 // appVariableName is the name of a variable containing the directory for this app, e.g.
226 // MYAPPDIR. This is checked first.
228 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
230 // Try appVariableName
231 if (!appVariableName
.empty())
233 wxString
strVar(wxGetenv(appVariableName
.c_str()));
238 if (wxIsAbsolutePath(argv0
))
239 return wxPathOnly(argv0
);
242 // Is it a relative path?
243 wxString
currentDir(cwd
);
244 if (currentDir
.Last() != wxFILE_SEP_PATH
)
245 currentDir
+= wxFILE_SEP_PATH
;
248 if (wxFileExists(currentDir
))
249 return wxPathOnly(currentDir
);
252 // OK, it's neither an absolute path nor a relative path.
256 pathList
.AddEnvList(wxT("PATH"));
257 wxString strPath
= pathList
.FindAbsoluteValidPath(argv0
);
258 if (!strPath
.empty())
259 return wxPathOnly(strPath
);
262 return wxEmptyString
;
265 // Adds a context-sensitive help button, for non-Windows platforms
266 void apAddContextHelpButton(wxWindow
*
267 #if defined(__WXGTK__) || defined(__WXMAC__)
273 #if defined(__WXGTK__) || defined(__WXMAC__)
279 #if defined(__WXGTK__) || defined(__WXMAC__)
285 #if defined(__WXGTK__) || defined(__WXMAC__)
288 WXUNUSED(sizerBorder
)
292 #if defined(__WXGTK__) || defined(__WXMAC__)
294 wxSize
buttonSize(20, 20);
296 wxSize buttonSize
= wxDefaultSize
;
298 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
299 wxDefaultPosition
, buttonSize
);
300 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
302 // Add a bit of space on the right, to allow for the dialog resizing
305 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
308 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
310 if (wxGetApp().UsingTooltips())
312 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
318 // Get selected wxNotebook page
319 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
321 int sel
= notebook
->GetSelection();
324 return notebook
->GetPage(sel
);
333 wxIconInfo::wxIconInfo(const wxString
& name
)
338 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
342 int wxIconInfo::GetIconId(int state
, bool enabled
) const
344 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
345 wxASSERT ( state
< m_maxStates
);
347 return m_states
[state
* 2 + (enabled
? 0 : 1)];
350 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
352 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
353 if (state
+1 > m_maxStates
)
354 m_maxStates
= state
+1;
356 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
361 * Contains a list of wxIconInfos
364 wxIconTable::wxIconTable(wxImageList
* imageList
)
366 m_imageList
= imageList
;
367 WX_CLEAR_LIST(wxIconTable
,*this);
370 void wxIconTable::AppendInfo(wxIconInfo
* info
)
375 // Easy way of initialising both the image list and the
376 // table. It will generate image ids itself while appending the icon.
377 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
379 wxASSERT (m_imageList
!= NULL
);
381 wxIconInfo
* info
= FindInfo(name
);
384 info
= new wxIconInfo(name
);
387 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
391 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
393 wxObjectList::compatibility_iterator node
= GetFirst();
396 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
397 if (info
->GetName() == name
)
399 node
= node
->GetNext();
404 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
406 wxIconInfo
* info
= FindInfo(name
);
409 return info
->GetIconId(state
, enabled
);
412 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
414 wxIconInfo
* info
= FindInfo(name
);
417 info
->SetIconId(state
, enabled
, iconId
);
421 // Output stream operators
423 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
425 stream
.Write(s
, s
.Length());
429 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
432 str
.Printf(_T("%ld"), l
);
433 return stream
<< str
;
436 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxChar c
)
439 str
.Printf(_T("%c"), c
);
440 return stream
<< str
;
443 // Convert characters to HTML equivalents
444 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
447 size_t len
= str
.Length();
449 for (i
= 0; i
< len
; i
++)
451 wxChar c
= str
.GetChar(i
);
454 else if (c
== _T('>'))
456 else if (c
== _T('&'))
464 // Match 'matchText' against 'matchAgainst', optionally constraining to
466 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
468 // Fast operation if not matching against whole words only
470 return (matchAgainst
.Find(matchText
) != wxNOT_FOUND
);
472 wxString
left(matchAgainst
);
473 bool success
= false;
474 int matchTextLen
= (int) matchText
.Length();
475 while (!success
&& !matchAgainst
.empty())
477 int pos
= left
.Find(matchText
);
478 if (pos
== wxNOT_FOUND
)
481 bool firstCharOK
= false;
482 bool lastCharOK
= false;
483 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
486 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
489 if (firstCharOK
&& lastCharOK
)
492 left
= left
.Mid(pos
+1);