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"
35 #include "wx/msw/winundef.h"
40 // Returns the image type, or -1, determined from the extension.
41 int apDetermineImageType(const wxString
& filename
)
43 wxString path
, name
, ext
;
45 wxSplitPath(filename
, & path
, & name
, & ext
);
48 if (ext
== "jpg" || ext
== "jpeg")
49 return wxBITMAP_TYPE_JPEG
;
50 else if (ext
== "gif")
51 return wxBITMAP_TYPE_GIF
;
52 else if (ext
== "bmp")
53 return wxBITMAP_TYPE_BMP
;
54 else if (ext
== "png")
55 return wxBITMAP_TYPE_PNG
;
56 else if (ext
== "pcx")
57 return wxBITMAP_TYPE_PCX
;
58 else if (ext
== "tif" || ext
== "tiff")
59 return wxBITMAP_TYPE_TIF
;
64 // Convert a colour to a 6-digit hex string
65 wxString
apColourToHexString(const wxColour
& col
)
69 hex
+= wxDecToHex(col
.Red());
70 hex
+= wxDecToHex(col
.Green());
71 hex
+= wxDecToHex(col
.Blue());
76 // Convert 6-digit hex string to a colour
77 wxColour
apHexStringToColour(const wxString
& hex
)
82 r
= wxHexToDec(hex
.Mid(0, 2));
83 g
= wxHexToDec(hex
.Mid(2, 2));
84 b
= wxHexToDec(hex
.Mid(4, 2));
86 return wxColour(r
, g
, b
);
89 // Convert a wxFont to a string
90 wxString
apFontToString(const wxFont
& font
)
93 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
94 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
95 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
100 static inline int StringToInt(const wxString
& s
)
108 // Convert a string to a wxFont
109 wxFont
apStringToFont(const wxString
& str
)
112 int family
= wxSWISS
;
113 int style
= wxNORMAL
;
114 int weight
= wxNORMAL
;
116 wxString
facename(wxT(""));
118 wxStringTokenizer
tkz(str
, wxT(","));
120 while (tkz
.HasMoreTokens())
122 wxString token
= tkz
.GetNextToken();
126 pointSize
= StringToInt(token
);
127 #if defined(__WXGTK__) || defined(__WXMAC__)
135 family
= StringToInt(token
);
137 style
= StringToInt(token
);
139 weight
= StringToInt(token
);
141 underlined
= StringToInt(token
);
145 #if defined(__WXGTK__)
146 if (facename
== wxT("Arial"))
147 facename
= wxT("helvetica");
153 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
157 // Get the index of the given named wxNotebook page
158 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
161 for (i
= 0; i
< notebook
->GetPageCount(); i
++)
162 if (name
== notebook
->GetPageText(i
))
171 void apViewHTMLFile(const wxString
& url
)
175 TCHAR szCmdName
[1024];
176 DWORD dwType
, dw
= sizeof(szCmdName
);
178 lRes
= RegOpenKey(HKEY_CLASSES_ROOT
, "htmlfile\\shell\\open\\command", &hKey
);
179 if(lRes
== ERROR_SUCCESS
&& RegQueryValueEx(hKey
,(LPTSTR
)NULL
, NULL
,
180 &dwType
, (LPBYTE
)szCmdName
, &dw
) == ERROR_SUCCESS
)
182 strcat(szCmdName
, (const char*) url
);
183 PROCESS_INFORMATION piProcInfo
;
184 STARTUPINFO siStartInfo
;
185 memset(&siStartInfo
, 0, sizeof(STARTUPINFO
));
186 siStartInfo
.cb
= sizeof(STARTUPINFO
);
187 CreateProcess(NULL
, szCmdName
, NULL
, NULL
, FALSE
, 0, NULL
,
188 NULL
, &siStartInfo
, &piProcInfo
);
190 if(lRes
== ERROR_SUCCESS
)
193 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(wxT("html"));
196 wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types."));
201 bool ok
= ft
->GetOpenCommand(&cmd
,
202 wxFileType::MessageParameters(url
, _T("")));
207 // TODO: some kind of configuration dialog here.
208 wxMessageBox(_("Could not determine the command for running the browser."),
209 wxT("Browsing problem"), wxOK
|wxICON_EXCLAMATION
);
213 ok
= (wxExecute(cmd
, FALSE
) != 0);
217 wxString
wxGetTempDir()
220 #if defined(__WXMAC__) && !defined(__DARWIN__)
221 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
223 dir
= wxGetenv(_T("TMP"));
226 dir
= wxGetenv(_T("TEMP"));
242 // Invoke app for file type
243 // Eventually we should allow the user to select an app.
244 bool apInvokeAppForFile(const wxString
& filename
)
246 wxString path
, file
, ext
;
247 wxSplitPath(filename
, & path
, & file
, & ext
);
249 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
253 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
255 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
260 bool ok
= ft
->GetOpenCommand(&cmd
,
261 wxFileType::MessageParameters(filename
, _T("")));
264 ok
= (wxExecute(cmd
, FALSE
) != 0);
269 // Find the absolute path where this application has been run from.
270 // argv0 is wxTheApp->argv[0]
271 // cwd is the current working directory (at startup)
272 // appVariableName is the name of a variable containing the directory for this app, e.g.
273 // MYAPPDIR. This is checked first.
275 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
279 // Try appVariableName
280 if (!appVariableName
.IsEmpty())
282 str
= wxGetenv(appVariableName
);
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
;
296 str
= currentDir
+ argv0
;
297 if (wxFileExists(str
))
298 return wxPathOnly(str
);
301 // OK, it's neither an absolute path nor a relative path.
305 pathList
.AddEnvList(wxT("PATH"));
306 str
= pathList
.FindAbsoluteValidPath(argv0
);
308 return wxPathOnly(str
);
311 return wxEmptyString
;
314 // Adds a context-sensitive help button, for non-Windows platforms
315 void apAddContextHelpButton(wxWindow
* parent
, wxSizer
* sizer
, int sizerFlags
, int sizerBorder
)
317 #if defined(__WXGTK__) || defined(__WXMAC__)
319 wxSize
buttonSize(20, 20);
321 wxSize
buttonSize(-1, -1);
323 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
324 wxDefaultPosition
, buttonSize
);
325 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
327 // Add a bit of space on the right, to allow for the dialog resizing
330 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
333 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
335 if (wxGetApp().UsingTooltips())
337 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
343 // Get selected wxNotebook page
344 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
346 int sel
= notebook
->GetSelection();
349 return notebook
->GetPage(sel
);
358 wxIconInfo::wxIconInfo(const wxString
& name
)
363 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
367 int wxIconInfo::GetIconId(int state
, bool enabled
) const
369 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
370 wxASSERT ( state
< m_maxStates
);
372 return m_states
[state
* 2 + (enabled
? 0 : 1)];
375 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
377 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
378 if (state
+1 > m_maxStates
)
379 m_maxStates
= state
+1;
381 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
386 * Contains a list of wxIconInfos
389 wxIconTable::wxIconTable(wxImageList
* imageList
)
391 m_imageList
= imageList
;
392 DeleteContents(TRUE
);
395 void wxIconTable::AppendInfo(wxIconInfo
* info
)
400 // Easy way of initialising both the image list and the
401 // table. It will generate image ids itself while appending the icon.
402 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
404 wxASSERT (m_imageList
!= NULL
);
406 wxIconInfo
* info
= FindInfo(name
);
409 info
= new wxIconInfo(name
);
412 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
416 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
418 wxNode
* node
= GetFirst();
421 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
422 if (info
->GetName() == name
)
424 node
= node
->GetNext();
429 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
431 wxIconInfo
* info
= FindInfo(name
);
434 return info
->GetIconId(state
, enabled
);
437 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
439 wxIconInfo
* info
= FindInfo(name
);
442 info
->SetIconId(state
, enabled
, iconId
);
446 // Output stream operators
448 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
450 stream
.Write(s
, s
.Length());
454 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
457 str
.Printf(_T("%ld"), l
);
458 return stream
<< str
;
461 wxOutputStream
& operator <<(wxOutputStream
& stream
, const char c
)
464 str
.Printf(_T("%c"), c
);
465 return stream
<< str
;
468 // Convert characters to HTML equivalents
469 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
472 size_t len
= str
.Length();
474 for (i
= 0; i
< len
; i
++)
476 wxChar c
= str
.GetChar(i
);
479 else if (c
== _T('>'))
481 else if (c
== _T('&'))
489 // Match 'matchText' against 'matchAgainst', optionally constraining to
491 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
493 // Fast operation if not matching against whole words only
495 return (matchAgainst
.Find(matchText
) != -1);
497 wxString
left(matchAgainst
);
498 bool success
= FALSE
;
500 int matchTextLen
= (int) matchText
.Length();
501 while (!success
&& !matchAgainst
.IsEmpty())
503 pos
= left
.Find(matchText
);
507 bool firstCharOK
= FALSE
;
508 bool lastCharOK
= FALSE
;
509 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
512 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
515 if (firstCharOK
&& lastCharOK
)
518 left
= left
.Mid(pos
+1);