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"
42 #include "wx/wfstream.h"
43 #include "wx/cshelp.h"
45 #include "wx/imaglist.h"
46 #include "wx/tokenzr.h"
47 #include "wx/notebook.h"
48 #include "wx/mimetype.h"
51 // Returns the image type, or -1, determined from the extension.
52 int apDetermineImageType(const wxString
& filename
)
54 wxString path
, name
, ext
;
56 wxSplitPath(filename
, & path
, & name
, & ext
);
59 if (ext
== _T("jpg") || ext
== _T("jpeg"))
60 return wxBITMAP_TYPE_JPEG
;
61 else if (ext
== _T("gif"))
62 return wxBITMAP_TYPE_GIF
;
63 else if (ext
== _T("bmp"))
64 return wxBITMAP_TYPE_BMP
;
65 else if (ext
== _T("png"))
66 return wxBITMAP_TYPE_PNG
;
67 else if (ext
== _T("pcx"))
68 return wxBITMAP_TYPE_PCX
;
69 else if (ext
== _T("tif") || ext
== _T("tiff"))
70 return wxBITMAP_TYPE_TIF
;
75 // Convert a colour to a 6-digit hex string
76 wxString
apColourToHexString(const wxColour
& col
)
80 hex
+= wxDecToHex(col
.Red());
81 hex
+= wxDecToHex(col
.Green());
82 hex
+= wxDecToHex(col
.Blue());
87 // Convert 6-digit hex string to a colour
88 wxColour
apHexStringToColour(const wxString
& hex
)
90 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
91 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
92 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
94 return wxColour(r
, g
, b
);
97 // Convert a wxFont to a string
98 wxString
apFontToString(const wxFont
& font
)
101 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
102 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
103 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
108 static inline int StringToInt(const wxString
& s
)
116 // Convert a string to a wxFont
117 wxFont
apStringToFont(const wxString
& str
)
120 int family
= wxSWISS
;
121 int style
= wxNORMAL
;
122 int weight
= wxNORMAL
;
126 wxStringTokenizer
tkz(str
, wxT(","));
128 while (tkz
.HasMoreTokens())
130 wxString token
= tkz
.GetNextToken();
134 pointSize
= StringToInt(token
);
135 #if defined(__WXGTK__) || defined(__WXMAC__)
143 family
= StringToInt(token
);
145 style
= StringToInt(token
);
147 weight
= StringToInt(token
);
149 underlined
= StringToInt(token
);
153 #if defined(__WXGTK__)
154 if (facename
== wxT("Arial"))
155 facename
= wxT("helvetica");
161 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
165 // Get the index of the given named wxNotebook page
166 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
169 for (i
= 0; i
< (int)notebook
->GetPageCount(); i
++)
170 if (name
== notebook
->GetPageText(i
))
179 void apViewHTMLFile(const wxString
& url
)
183 TCHAR szCmdName
[1024];
184 DWORD dwType
, dw
= sizeof(szCmdName
);
186 lRes
= RegOpenKey(HKEY_CLASSES_ROOT
, _T("htmlfile\\shell\\open\\command"), &hKey
);
187 if(lRes
== ERROR_SUCCESS
&& RegQueryValueEx(hKey
,(LPTSTR
)NULL
, NULL
,
188 &dwType
, (LPBYTE
)szCmdName
, &dw
) == ERROR_SUCCESS
)
190 wxStrcat(szCmdName
, (const wxChar
*) url
);
191 PROCESS_INFORMATION piProcInfo
;
192 STARTUPINFO siStartInfo
;
193 memset(&siStartInfo
, 0, sizeof(STARTUPINFO
));
194 siStartInfo
.cb
= sizeof(STARTUPINFO
);
195 CreateProcess(NULL
, szCmdName
, NULL
, NULL
, false, 0, NULL
,
196 NULL
, &siStartInfo
, &piProcInfo
);
198 if(lRes
== ERROR_SUCCESS
)
201 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(wxT("html"));
204 wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types."));
209 bool ok
= ft
->GetOpenCommand(&cmd
,
210 wxFileType::MessageParameters(url
, wxEmptyString
));
215 // TODO: some kind of configuration dialog here.
216 wxMessageBox(_("Could not determine the command for running the browser."),
217 wxT("Browsing problem"), wxOK
|wxICON_EXCLAMATION
);
221 ok
= (wxExecute(cmd
, false) != 0);
225 wxString
wxGetTempDir()
228 #if defined(__WXMAC__) && !defined(__DARWIN__)
229 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
231 wxString
dirEnv(wxGetenv(_T("TMP")));
235 wxString
envVar(wxGetenv(_T("TEMP")));
252 // Invoke app for file type
253 // Eventually we should allow the user to select an app.
254 bool apInvokeAppForFile(const wxString
& filename
)
256 wxString path
, file
, ext
;
257 wxSplitPath(filename
, & path
, & file
, & ext
);
259 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
263 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
265 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
270 ft
->GetOpenCommand(&cmd
, wxFileType::MessageParameters(filename
, wxEmptyString
));
273 return (wxExecute(cmd
, false) != 0);
276 // Find the absolute path where this application has been run from.
277 // argv0 is wxTheApp->argv[0]
278 // cwd is the current working directory (at startup)
279 // appVariableName is the name of a variable containing the directory for this app, e.g.
280 // MYAPPDIR. This is checked first.
282 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
284 // Try appVariableName
285 if (!appVariableName
.empty())
287 wxString
strVar(wxGetenv(appVariableName
.c_str()));
292 if (wxIsAbsolutePath(argv0
))
293 return wxPathOnly(argv0
);
296 // Is it a relative path?
297 wxString
currentDir(cwd
);
298 if (currentDir
.Last() != wxFILE_SEP_PATH
)
299 currentDir
+= wxFILE_SEP_PATH
;
302 if (wxFileExists(currentDir
))
303 return wxPathOnly(currentDir
);
306 // OK, it's neither an absolute path nor a relative path.
310 pathList
.AddEnvList(wxT("PATH"));
311 wxString strPath
= pathList
.FindAbsoluteValidPath(argv0
);
312 if (!strPath
.empty())
313 return wxPathOnly(strPath
);
316 return wxEmptyString
;
319 // Adds a context-sensitive help button, for non-Windows platforms
320 void apAddContextHelpButton(wxWindow
*
321 #if defined(__WXGTK__) || defined(__WXMAC__)
327 #if defined(__WXGTK__) || defined(__WXMAC__)
333 #if defined(__WXGTK__) || defined(__WXMAC__)
339 #if defined(__WXGTK__) || defined(__WXMAC__)
342 WXUNUSED(sizerBorder
)
346 #if defined(__WXGTK__) || defined(__WXMAC__)
348 wxSize
buttonSize(20, 20);
350 wxSize buttonSize
= wxDefaultSize
;
352 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
353 wxDefaultPosition
, buttonSize
);
354 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
356 // Add a bit of space on the right, to allow for the dialog resizing
359 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
362 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
364 if (wxGetApp().UsingTooltips())
366 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
372 // Get selected wxNotebook page
373 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
375 int sel
= notebook
->GetSelection();
378 return notebook
->GetPage(sel
);
387 wxIconInfo::wxIconInfo(const wxString
& name
)
392 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
396 int wxIconInfo::GetIconId(int state
, bool enabled
) const
398 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
399 wxASSERT ( state
< m_maxStates
);
401 return m_states
[state
* 2 + (enabled
? 0 : 1)];
404 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
406 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
407 if (state
+1 > m_maxStates
)
408 m_maxStates
= state
+1;
410 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
415 * Contains a list of wxIconInfos
418 wxIconTable::wxIconTable(wxImageList
* imageList
)
420 m_imageList
= imageList
;
421 WX_CLEAR_LIST(wxIconTable
,*this);
424 void wxIconTable::AppendInfo(wxIconInfo
* info
)
429 // Easy way of initialising both the image list and the
430 // table. It will generate image ids itself while appending the icon.
431 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
433 wxASSERT (m_imageList
!= NULL
);
435 wxIconInfo
* info
= FindInfo(name
);
438 info
= new wxIconInfo(name
);
441 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
445 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
447 wxObjectList::compatibility_iterator node
= GetFirst();
450 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
451 if (info
->GetName() == name
)
453 node
= node
->GetNext();
458 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
460 wxIconInfo
* info
= FindInfo(name
);
463 return info
->GetIconId(state
, enabled
);
466 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
468 wxIconInfo
* info
= FindInfo(name
);
471 info
->SetIconId(state
, enabled
, iconId
);
475 // Output stream operators
477 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
479 stream
.Write(s
, s
.Length());
483 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
486 str
.Printf(_T("%ld"), l
);
487 return stream
<< str
;
490 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxChar c
)
493 str
.Printf(_T("%c"), c
);
494 return stream
<< str
;
497 // Convert characters to HTML equivalents
498 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
501 size_t len
= str
.Length();
503 for (i
= 0; i
< len
; i
++)
505 wxChar c
= str
.GetChar(i
);
508 else if (c
== _T('>'))
510 else if (c
== _T('&'))
518 // Match 'matchText' against 'matchAgainst', optionally constraining to
520 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
522 // Fast operation if not matching against whole words only
524 return (matchAgainst
.Find(matchText
) != wxNOT_FOUND
);
526 wxString
left(matchAgainst
);
527 bool success
= false;
528 int matchTextLen
= (int) matchText
.Length();
529 while (!success
&& !matchAgainst
.empty())
531 int pos
= left
.Find(matchText
);
532 if (pos
== wxNOT_FOUND
)
535 bool firstCharOK
= false;
536 bool lastCharOK
= false;
537 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
540 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
543 if (firstCharOK
&& lastCharOK
)
546 left
= left
.Mid(pos
+1);