1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
18 #include "wx/notebook.h"
19 #include "wx/splitter.h"
20 #include "wx/wfstream.h"
21 #include "wx/datstrm.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 #include "wx/imaglist.h"
36 #include "wx/msw/winundef.h"
41 // Returns the image type, or -1, determined from the extension.
42 int apDetermineImageType(const wxString
& filename
)
44 wxString path
, name
, ext
;
46 wxSplitPath(filename
, & path
, & name
, & ext
);
49 if (ext
== _T("jpg") || ext
== _T("jpeg"))
50 return wxBITMAP_TYPE_JPEG
;
51 else if (ext
== _T("gif"))
52 return wxBITMAP_TYPE_GIF
;
53 else if (ext
== _T("bmp"))
54 return wxBITMAP_TYPE_BMP
;
55 else if (ext
== _T("png"))
56 return wxBITMAP_TYPE_PNG
;
57 else if (ext
== _T("pcx"))
58 return wxBITMAP_TYPE_PCX
;
59 else if (ext
== _T("tif") || ext
== _T("tiff"))
60 return wxBITMAP_TYPE_TIF
;
65 // Convert a colour to a 6-digit hex string
66 wxString
apColourToHexString(const wxColour
& col
)
70 hex
+= wxDecToHex(col
.Red());
71 hex
+= wxDecToHex(col
.Green());
72 hex
+= wxDecToHex(col
.Blue());
77 // Convert 6-digit hex string to a colour
78 wxColour
apHexStringToColour(const wxString
& hex
)
80 unsigned int r
= wxHexToDec(hex
.Mid(0, 2));
81 unsigned int g
= wxHexToDec(hex
.Mid(2, 2));
82 unsigned int b
= wxHexToDec(hex
.Mid(4, 2));
84 return wxColour(r
, g
, b
);
87 // Convert a wxFont to a string
88 wxString
apFontToString(const wxFont
& font
)
91 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
92 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
93 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
98 static inline int StringToInt(const wxString
& s
)
106 // Convert a string to a wxFont
107 wxFont
apStringToFont(const wxString
& str
)
110 int family
= wxSWISS
;
111 int style
= wxNORMAL
;
112 int weight
= wxNORMAL
;
114 wxString
facename(wxT(""));
116 wxStringTokenizer
tkz(str
, wxT(","));
118 while (tkz
.HasMoreTokens())
120 wxString token
= tkz
.GetNextToken();
124 pointSize
= StringToInt(token
);
125 #if defined(__WXGTK__) || defined(__WXMAC__)
133 family
= StringToInt(token
);
135 style
= StringToInt(token
);
137 weight
= StringToInt(token
);
139 underlined
= StringToInt(token
);
143 #if defined(__WXGTK__)
144 if (facename
== wxT("Arial"))
145 facename
= wxT("helvetica");
151 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
155 // Get the index of the given named wxNotebook page
156 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
159 for (i
= 0; i
< (int)notebook
->GetPageCount(); i
++)
160 if (name
== notebook
->GetPageText(i
))
169 void apViewHTMLFile(const wxString
& url
)
173 TCHAR szCmdName
[1024];
174 DWORD dwType
, dw
= sizeof(szCmdName
);
176 lRes
= RegOpenKey(HKEY_CLASSES_ROOT
, _T("htmlfile\\shell\\open\\command"), &hKey
);
177 if(lRes
== ERROR_SUCCESS
&& RegQueryValueEx(hKey
,(LPTSTR
)NULL
, NULL
,
178 &dwType
, (LPBYTE
)szCmdName
, &dw
) == ERROR_SUCCESS
)
180 wxStrcat(szCmdName
, (const wxChar
*) url
);
181 PROCESS_INFORMATION piProcInfo
;
182 STARTUPINFO siStartInfo
;
183 memset(&siStartInfo
, 0, sizeof(STARTUPINFO
));
184 siStartInfo
.cb
= sizeof(STARTUPINFO
);
185 CreateProcess(NULL
, szCmdName
, NULL
, NULL
, false, 0, NULL
,
186 NULL
, &siStartInfo
, &piProcInfo
);
188 if(lRes
== ERROR_SUCCESS
)
191 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(wxT("html"));
194 wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types."));
199 bool ok
= ft
->GetOpenCommand(&cmd
,
200 wxFileType::MessageParameters(url
, _T("")));
205 // TODO: some kind of configuration dialog here.
206 wxMessageBox(_("Could not determine the command for running the browser."),
207 wxT("Browsing problem"), wxOK
|wxICON_EXCLAMATION
);
211 ok
= (wxExecute(cmd
, false) != 0);
215 wxString
wxGetTempDir()
218 #if defined(__WXMAC__) && !defined(__DARWIN__)
219 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
221 dir
= wxGetenv(_T("TMP"));
224 dir
= wxGetenv(_T("TEMP"));
240 // Invoke app for file type
241 // Eventually we should allow the user to select an app.
242 bool apInvokeAppForFile(const wxString
& filename
)
244 wxString path
, file
, ext
;
245 wxSplitPath(filename
, & path
, & file
, & ext
);
247 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
251 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
253 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
258 ft
->GetOpenCommand(&cmd
, wxFileType::MessageParameters(filename
, _T("")));
261 return (wxExecute(cmd
, false) != 0);
264 // Find the absolute path where this application has been run from.
265 // argv0 is wxTheApp->argv[0]
266 // cwd is the current working directory (at startup)
267 // appVariableName is the name of a variable containing the directory for this app, e.g.
268 // MYAPPDIR. This is checked first.
270 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
274 // Try appVariableName
275 if (!appVariableName
.IsEmpty())
277 str
= wxGetenv(appVariableName
);
282 if (wxIsAbsolutePath(argv0
))
283 return wxPathOnly(argv0
);
286 // Is it a relative path?
287 wxString
currentDir(cwd
);
288 if (currentDir
.Last() != wxFILE_SEP_PATH
)
289 currentDir
+= wxFILE_SEP_PATH
;
291 str
= currentDir
+ argv0
;
292 if (wxFileExists(str
))
293 return wxPathOnly(str
);
296 // OK, it's neither an absolute path nor a relative path.
300 pathList
.AddEnvList(wxT("PATH"));
301 str
= pathList
.FindAbsoluteValidPath(argv0
);
303 return wxPathOnly(str
);
306 return wxEmptyString
;
309 // Adds a context-sensitive help button, for non-Windows platforms
310 void apAddContextHelpButton(wxWindow
*
311 #if defined(__WXGTK__) || defined(__WXMAC__)
317 #if defined(__WXGTK__) || defined(__WXMAC__)
323 #if defined(__WXGTK__) || defined(__WXMAC__)
329 #if defined(__WXGTK__) || defined(__WXMAC__)
332 WXUNUSED(sizerBorder
)
336 #if defined(__WXGTK__) || defined(__WXMAC__)
338 wxSize
buttonSize(20, 20);
340 wxSize buttonSize
= wxDefaultSize
;
342 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
343 wxDefaultPosition
, buttonSize
);
344 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
346 // Add a bit of space on the right, to allow for the dialog resizing
349 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
352 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
354 if (wxGetApp().UsingTooltips())
356 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
362 // Get selected wxNotebook page
363 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
365 int sel
= notebook
->GetSelection();
368 return notebook
->GetPage(sel
);
377 wxIconInfo::wxIconInfo(const wxString
& name
)
382 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
386 int wxIconInfo::GetIconId(int state
, bool enabled
) const
388 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
389 wxASSERT ( state
< m_maxStates
);
391 return m_states
[state
* 2 + (enabled
? 0 : 1)];
394 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
396 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
397 if (state
+1 > m_maxStates
)
398 m_maxStates
= state
+1;
400 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
405 * Contains a list of wxIconInfos
408 wxIconTable::wxIconTable(wxImageList
* imageList
)
410 m_imageList
= imageList
;
411 DeleteContents(true);
414 void wxIconTable::AppendInfo(wxIconInfo
* info
)
419 // Easy way of initialising both the image list and the
420 // table. It will generate image ids itself while appending the icon.
421 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
423 wxASSERT (m_imageList
!= NULL
);
425 wxIconInfo
* info
= FindInfo(name
);
428 info
= new wxIconInfo(name
);
431 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
435 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
437 wxNode
* node
= GetFirst();
440 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
441 if (info
->GetName() == name
)
443 node
= node
->GetNext();
448 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
450 wxIconInfo
* info
= FindInfo(name
);
453 return info
->GetIconId(state
, enabled
);
456 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
458 wxIconInfo
* info
= FindInfo(name
);
461 info
->SetIconId(state
, enabled
, iconId
);
465 // Output stream operators
467 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
469 stream
.Write(s
, s
.Length());
473 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
476 str
.Printf(_T("%ld"), l
);
477 return stream
<< str
;
480 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxChar c
)
483 str
.Printf(_T("%c"), c
);
484 return stream
<< str
;
487 // Convert characters to HTML equivalents
488 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
491 size_t len
= str
.Length();
493 for (i
= 0; i
< len
; i
++)
495 wxChar c
= str
.GetChar(i
);
498 else if (c
== _T('>'))
500 else if (c
== _T('&'))
508 // Match 'matchText' against 'matchAgainst', optionally constraining to
510 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
512 // Fast operation if not matching against whole words only
514 return (matchAgainst
.Find(matchText
) != wxNOT_FOUND
);
516 wxString
left(matchAgainst
);
517 bool success
= false;
518 int matchTextLen
= (int) matchText
.Length();
519 while (!success
&& !matchAgainst
.IsEmpty())
521 int pos
= left
.Find(matchText
);
522 if (pos
== wxNOT_FOUND
)
525 bool firstCharOK
= false;
526 bool lastCharOK
= false;
527 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
530 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
533 if (firstCharOK
&& lastCharOK
)
536 left
= left
.Mid(pos
+1);