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"
34 #include "wx/msgdlg.h"
41 #include "wx/wfstream.h"
42 #include "wx/cshelp.h"
44 #include "wx/imaglist.h"
45 #include "wx/tokenzr.h"
46 #include "wx/notebook.h"
47 #include "wx/mimetype.h"
50 // Returns the image type, or -1, determined from the extension.
51 int apDetermineImageType(const wxString
& filename
)
53 wxString path
, name
, ext
;
55 wxSplitPath(filename
, & path
, & name
, & ext
);
58 if (ext
== _T("jpg") || ext
== _T("jpeg"))
59 return wxBITMAP_TYPE_JPEG
;
60 else if (ext
== _T("gif"))
61 return wxBITMAP_TYPE_GIF
;
62 else if (ext
== _T("bmp"))
63 return wxBITMAP_TYPE_BMP
;
64 else if (ext
== _T("png"))
65 return wxBITMAP_TYPE_PNG
;
66 else if (ext
== _T("pcx"))
67 return wxBITMAP_TYPE_PCX
;
68 else if (ext
== _T("tif") || ext
== _T("tiff"))
69 return wxBITMAP_TYPE_TIF
;
74 // Convert a colour to a 6-digit hex string
75 wxString
apColourToHexString(const wxColour
& col
)
79 hex
+= wxDecToHex(col
.Red());
80 hex
+= wxDecToHex(col
.Green());
81 hex
+= wxDecToHex(col
.Blue());
86 // Convert 6-digit hex string to a colour
87 wxColour
apHexStringToColour(const wxString
& hex
)
89 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
90 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
91 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
93 return wxColour(r
, g
, b
);
96 // Convert a wxFont to a string
97 wxString
apFontToString(const wxFont
& font
)
100 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
101 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
102 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
107 static inline int StringToInt(const wxString
& s
)
115 // Convert a string to a wxFont
116 wxFont
apStringToFont(const wxString
& str
)
119 int family
= wxSWISS
;
120 int style
= wxNORMAL
;
121 int weight
= wxNORMAL
;
123 wxString
facename(wxT(""));
125 wxStringTokenizer
tkz(str
, wxT(","));
127 while (tkz
.HasMoreTokens())
129 wxString token
= tkz
.GetNextToken();
133 pointSize
= StringToInt(token
);
134 #if defined(__WXGTK__) || defined(__WXMAC__)
142 family
= StringToInt(token
);
144 style
= StringToInt(token
);
146 weight
= StringToInt(token
);
148 underlined
= StringToInt(token
);
152 #if defined(__WXGTK__)
153 if (facename
== wxT("Arial"))
154 facename
= wxT("helvetica");
160 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
164 // Get the index of the given named wxNotebook page
165 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
168 for (i
= 0; i
< (int)notebook
->GetPageCount(); i
++)
169 if (name
== notebook
->GetPageText(i
))
178 void apViewHTMLFile(const wxString
& url
)
182 TCHAR szCmdName
[1024];
183 DWORD dwType
, dw
= sizeof(szCmdName
);
185 lRes
= RegOpenKey(HKEY_CLASSES_ROOT
, _T("htmlfile\\shell\\open\\command"), &hKey
);
186 if(lRes
== ERROR_SUCCESS
&& RegQueryValueEx(hKey
,(LPTSTR
)NULL
, NULL
,
187 &dwType
, (LPBYTE
)szCmdName
, &dw
) == ERROR_SUCCESS
)
189 wxStrcat(szCmdName
, (const wxChar
*) url
);
190 PROCESS_INFORMATION piProcInfo
;
191 STARTUPINFO siStartInfo
;
192 memset(&siStartInfo
, 0, sizeof(STARTUPINFO
));
193 siStartInfo
.cb
= sizeof(STARTUPINFO
);
194 CreateProcess(NULL
, szCmdName
, NULL
, NULL
, false, 0, NULL
,
195 NULL
, &siStartInfo
, &piProcInfo
);
197 if(lRes
== ERROR_SUCCESS
)
200 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(wxT("html"));
203 wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types."));
208 bool ok
= ft
->GetOpenCommand(&cmd
,
209 wxFileType::MessageParameters(url
, _T("")));
214 // TODO: some kind of configuration dialog here.
215 wxMessageBox(_("Could not determine the command for running the browser."),
216 wxT("Browsing problem"), wxOK
|wxICON_EXCLAMATION
);
220 ok
= (wxExecute(cmd
, false) != 0);
224 wxString
wxGetTempDir()
227 #if defined(__WXMAC__) && !defined(__DARWIN__)
228 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
230 wxString
dirEnv(wxGetenv(_T("TMP")));
234 wxString
envVar(wxGetenv(_T("TEMP")));
251 // Invoke app for file type
252 // Eventually we should allow the user to select an app.
253 bool apInvokeAppForFile(const wxString
& filename
)
255 wxString path
, file
, ext
;
256 wxSplitPath(filename
, & path
, & file
, & ext
);
258 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
262 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
264 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
269 ft
->GetOpenCommand(&cmd
, wxFileType::MessageParameters(filename
, _T("")));
272 return (wxExecute(cmd
, false) != 0);
275 // Find the absolute path where this application has been run from.
276 // argv0 is wxTheApp->argv[0]
277 // cwd is the current working directory (at startup)
278 // appVariableName is the name of a variable containing the directory for this app, e.g.
279 // MYAPPDIR. This is checked first.
281 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
283 // Try appVariableName
284 if (!appVariableName
.IsEmpty())
286 wxString
strVar(wxGetenv(appVariableName
.c_str()));
287 if (!strVar
.IsEmpty())
291 if (wxIsAbsolutePath(argv0
))
292 return wxPathOnly(argv0
);
295 // Is it a relative path?
296 wxString
currentDir(cwd
);
297 if (currentDir
.Last() != wxFILE_SEP_PATH
)
298 currentDir
+= wxFILE_SEP_PATH
;
301 if (wxFileExists(currentDir
))
302 return wxPathOnly(currentDir
);
305 // OK, it's neither an absolute path nor a relative path.
309 pathList
.AddEnvList(wxT("PATH"));
310 wxString strPath
= pathList
.FindAbsoluteValidPath(argv0
);
311 if (!strPath
.IsEmpty())
312 return wxPathOnly(strPath
);
315 return wxEmptyString
;
318 // Adds a context-sensitive help button, for non-Windows platforms
319 void apAddContextHelpButton(wxWindow
*
320 #if defined(__WXGTK__) || defined(__WXMAC__)
326 #if defined(__WXGTK__) || defined(__WXMAC__)
332 #if defined(__WXGTK__) || defined(__WXMAC__)
338 #if defined(__WXGTK__) || defined(__WXMAC__)
341 WXUNUSED(sizerBorder
)
345 #if defined(__WXGTK__) || defined(__WXMAC__)
347 wxSize
buttonSize(20, 20);
349 wxSize buttonSize
= wxDefaultSize
;
351 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
352 wxDefaultPosition
, buttonSize
);
353 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
355 // Add a bit of space on the right, to allow for the dialog resizing
358 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
361 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
363 if (wxGetApp().UsingTooltips())
365 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
371 // Get selected wxNotebook page
372 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
374 int sel
= notebook
->GetSelection();
377 return notebook
->GetPage(sel
);
386 wxIconInfo::wxIconInfo(const wxString
& name
)
391 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
395 int wxIconInfo::GetIconId(int state
, bool enabled
) const
397 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
398 wxASSERT ( state
< m_maxStates
);
400 return m_states
[state
* 2 + (enabled
? 0 : 1)];
403 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
405 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
406 if (state
+1 > m_maxStates
)
407 m_maxStates
= state
+1;
409 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
414 * Contains a list of wxIconInfos
417 wxIconTable::wxIconTable(wxImageList
* imageList
)
419 m_imageList
= imageList
;
420 WX_CLEAR_LIST(wxIconTable
,*this);
423 void wxIconTable::AppendInfo(wxIconInfo
* info
)
428 // Easy way of initialising both the image list and the
429 // table. It will generate image ids itself while appending the icon.
430 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
432 wxASSERT (m_imageList
!= NULL
);
434 wxIconInfo
* info
= FindInfo(name
);
437 info
= new wxIconInfo(name
);
440 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
444 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
446 wxObjectList::compatibility_iterator node
= GetFirst();
449 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
450 if (info
->GetName() == name
)
452 node
= node
->GetNext();
457 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
459 wxIconInfo
* info
= FindInfo(name
);
462 return info
->GetIconId(state
, enabled
);
465 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
467 wxIconInfo
* info
= FindInfo(name
);
470 info
->SetIconId(state
, enabled
, iconId
);
474 // Output stream operators
476 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
478 stream
.Write(s
, s
.Length());
482 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
485 str
.Printf(_T("%ld"), l
);
486 return stream
<< str
;
489 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxChar c
)
492 str
.Printf(_T("%c"), c
);
493 return stream
<< str
;
496 // Convert characters to HTML equivalents
497 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
500 size_t len
= str
.Length();
502 for (i
= 0; i
< len
; i
++)
504 wxChar c
= str
.GetChar(i
);
507 else if (c
== _T('>'))
509 else if (c
== _T('&'))
517 // Match 'matchText' against 'matchAgainst', optionally constraining to
519 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
521 // Fast operation if not matching against whole words only
523 return (matchAgainst
.Find(matchText
) != wxNOT_FOUND
);
525 wxString
left(matchAgainst
);
526 bool success
= false;
527 int matchTextLen
= (int) matchText
.Length();
528 while (!success
&& !matchAgainst
.IsEmpty())
530 int pos
= left
.Find(matchText
);
531 if (pos
== wxNOT_FOUND
)
534 bool firstCharOK
= false;
535 bool lastCharOK
= false;
536 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
539 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
542 if (firstCharOK
&& lastCharOK
)
545 left
= left
.Mid(pos
+1);