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
)
83 r
= wxHexToDec(hex
.Mid(0, 2));
84 g
= wxHexToDec(hex
.Mid(2, 2));
85 b
= wxHexToDec(hex
.Mid(4, 2));
87 return wxColour(r
, g
, b
);
90 // Convert a wxFont to a string
91 wxString
apFontToString(const wxFont
& font
)
94 str
.Printf(wxT("%d,%d,%d,%d,%d,%s"), (int) font
.GetPointSize(),
95 (int) font
.GetFamily(), (int) font
.GetStyle(), (int) font
.GetWeight(),
96 (int) font
.GetUnderlined(), font
.GetFaceName().c_str());
101 static inline int StringToInt(const wxString
& s
)
109 // Convert a string to a wxFont
110 wxFont
apStringToFont(const wxString
& str
)
113 int family
= wxSWISS
;
114 int style
= wxNORMAL
;
115 int weight
= wxNORMAL
;
117 wxString
facename(wxT(""));
119 wxStringTokenizer
tkz(str
, wxT(","));
121 while (tkz
.HasMoreTokens())
123 wxString token
= tkz
.GetNextToken();
127 pointSize
= StringToInt(token
);
128 #if defined(__WXGTK__) || defined(__WXMAC__)
136 family
= StringToInt(token
);
138 style
= StringToInt(token
);
140 weight
= StringToInt(token
);
142 underlined
= StringToInt(token
);
146 #if defined(__WXGTK__)
147 if (facename
== wxT("Arial"))
148 facename
= wxT("helvetica");
154 return wxFont(pointSize
, family
, style
, weight
, (underlined
!= 0), facename
);
158 // Get the index of the given named wxNotebook page
159 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
)
162 for (i
= 0; i
< (int)notebook
->GetPageCount(); i
++)
163 if (name
== notebook
->GetPageText(i
))
172 void apViewHTMLFile(const wxString
& url
)
176 TCHAR szCmdName
[1024];
177 DWORD dwType
, dw
= sizeof(szCmdName
);
179 lRes
= RegOpenKey(HKEY_CLASSES_ROOT
, _T("htmlfile\\shell\\open\\command"), &hKey
);
180 if(lRes
== ERROR_SUCCESS
&& RegQueryValueEx(hKey
,(LPTSTR
)NULL
, NULL
,
181 &dwType
, (LPBYTE
)szCmdName
, &dw
) == ERROR_SUCCESS
)
183 wxStrcat(szCmdName
, (const wxChar
*) url
);
184 PROCESS_INFORMATION piProcInfo
;
185 STARTUPINFO siStartInfo
;
186 memset(&siStartInfo
, 0, sizeof(STARTUPINFO
));
187 siStartInfo
.cb
= sizeof(STARTUPINFO
);
188 CreateProcess(NULL
, szCmdName
, NULL
, NULL
, FALSE
, 0, NULL
,
189 NULL
, &siStartInfo
, &piProcInfo
);
191 if(lRes
== ERROR_SUCCESS
)
194 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(wxT("html"));
197 wxLogError(_T("Impossible to determine the file type for extension html. Please edit your MIME types."));
202 bool ok
= ft
->GetOpenCommand(&cmd
,
203 wxFileType::MessageParameters(url
, _T("")));
208 // TODO: some kind of configuration dialog here.
209 wxMessageBox(_("Could not determine the command for running the browser."),
210 wxT("Browsing problem"), wxOK
|wxICON_EXCLAMATION
);
214 ok
= (wxExecute(cmd
, FALSE
) != 0);
218 wxString
wxGetTempDir()
221 #if defined(__WXMAC__) && !defined(__DARWIN__)
222 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
224 dir
= wxGetenv(_T("TMP"));
227 dir
= wxGetenv(_T("TEMP"));
243 // Invoke app for file type
244 // Eventually we should allow the user to select an app.
245 bool apInvokeAppForFile(const wxString
& filename
)
247 wxString path
, file
, ext
;
248 wxSplitPath(filename
, & path
, & file
, & ext
);
250 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
254 msg
.Printf(wxT("Sorry, could not determine what application to invoke for extension %s\nYou may need to edit your MIME types."),
256 wxMessageBox(msg
, wxT("Application Invocation"), wxICON_EXCLAMATION
|wxOK
);
261 bool ok
= ft
->GetOpenCommand(&cmd
,
262 wxFileType::MessageParameters(filename
, _T("")));
265 ok
= (wxExecute(cmd
, FALSE
) != 0);
270 // Find the absolute path where this application has been run from.
271 // argv0 is wxTheApp->argv[0]
272 // cwd is the current working directory (at startup)
273 // appVariableName is the name of a variable containing the directory for this app, e.g.
274 // MYAPPDIR. This is checked first.
276 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
)
280 // Try appVariableName
281 if (!appVariableName
.IsEmpty())
283 str
= wxGetenv(appVariableName
);
288 if (wxIsAbsolutePath(argv0
))
289 return wxPathOnly(argv0
);
292 // Is it a relative path?
293 wxString
currentDir(cwd
);
294 if (currentDir
.Last() != wxFILE_SEP_PATH
)
295 currentDir
+= wxFILE_SEP_PATH
;
297 str
= currentDir
+ argv0
;
298 if (wxFileExists(str
))
299 return wxPathOnly(str
);
302 // OK, it's neither an absolute path nor a relative path.
306 pathList
.AddEnvList(wxT("PATH"));
307 str
= pathList
.FindAbsoluteValidPath(argv0
);
309 return wxPathOnly(str
);
312 return wxEmptyString
;
315 // Adds a context-sensitive help button, for non-Windows platforms
316 void apAddContextHelpButton(wxWindow
*
317 #if defined(__WXGTK__) || defined(__WXMAC__)
323 #if defined(__WXGTK__) || defined(__WXMAC__)
329 #if defined(__WXGTK__) || defined(__WXMAC__)
335 #if defined(__WXGTK__) || defined(__WXMAC__)
338 WXUNUSED(sizerBorder
)
342 #if defined(__WXGTK__) || defined(__WXMAC__)
344 wxSize
buttonSize(20, 20);
346 wxSize
buttonSize(-1, -1);
348 wxButton
*contextButton
= new wxContextHelpButton( parent
, wxID_CONTEXT_HELP
,
349 wxDefaultPosition
, buttonSize
);
350 sizer
->Add( contextButton
, 0, sizerFlags
, sizerBorder
);
352 // Add a bit of space on the right, to allow for the dialog resizing
355 sizer
->Add(0, 0, 0, wxRIGHT
, 10);
358 contextButton
->SetHelpText(_("Invokes context-sensitive help for the clicked-on window."));
360 if (wxGetApp().UsingTooltips())
362 contextButton
->SetToolTip(_("Invokes context-sensitive help for the clicked-on window."));
368 // Get selected wxNotebook page
369 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
)
371 int sel
= notebook
->GetSelection();
374 return notebook
->GetPage(sel
);
383 wxIconInfo::wxIconInfo(const wxString
& name
)
388 for (i
= 0; i
< wxMAX_ICON_STATES
; i
++)
392 int wxIconInfo::GetIconId(int state
, bool enabled
) const
394 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
395 wxASSERT ( state
< m_maxStates
);
397 return m_states
[state
* 2 + (enabled
? 0 : 1)];
400 void wxIconInfo::SetIconId(int state
, bool enabled
, int iconId
)
402 wxASSERT ( state
< (wxMAX_ICON_STATES
* 2) );
403 if (state
+1 > m_maxStates
)
404 m_maxStates
= state
+1;
406 m_states
[state
* 2 + (enabled
? 0 : 1)] = iconId
;
411 * Contains a list of wxIconInfos
414 wxIconTable::wxIconTable(wxImageList
* imageList
)
416 m_imageList
= imageList
;
417 DeleteContents(TRUE
);
420 void wxIconTable::AppendInfo(wxIconInfo
* info
)
425 // Easy way of initialising both the image list and the
426 // table. It will generate image ids itself while appending the icon.
427 bool wxIconTable::AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
)
429 wxASSERT (m_imageList
!= NULL
);
431 wxIconInfo
* info
= FindInfo(name
);
434 info
= new wxIconInfo(name
);
437 info
->SetIconId(state
, enabled
, m_imageList
->Add(icon
));
441 wxIconInfo
* wxIconTable::FindInfo(const wxString
& name
) const
443 wxNode
* node
= GetFirst();
446 wxIconInfo
* info
= (wxIconInfo
*) node
->GetData();
447 if (info
->GetName() == name
)
449 node
= node
->GetNext();
454 int wxIconTable::GetIconId(const wxString
& name
, int state
, bool enabled
) const
456 wxIconInfo
* info
= FindInfo(name
);
459 return info
->GetIconId(state
, enabled
);
462 bool wxIconTable::SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
)
464 wxIconInfo
* info
= FindInfo(name
);
467 info
->SetIconId(state
, enabled
, iconId
);
471 // Output stream operators
473 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxString
& s
)
475 stream
.Write(s
, s
.Length());
479 wxOutputStream
& operator <<(wxOutputStream
& stream
, long l
)
482 str
.Printf(_T("%ld"), l
);
483 return stream
<< str
;
486 wxOutputStream
& operator <<(wxOutputStream
& stream
, const wxChar c
)
489 str
.Printf(_T("%c"), c
);
490 return stream
<< str
;
493 // Convert characters to HTML equivalents
494 wxString
ctEscapeHTMLCharacters(const wxString
& str
)
497 size_t len
= str
.Length();
499 for (i
= 0; i
< len
; i
++)
501 wxChar c
= str
.GetChar(i
);
504 else if (c
== _T('>'))
506 else if (c
== _T('&'))
514 // Match 'matchText' against 'matchAgainst', optionally constraining to
516 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
)
518 // Fast operation if not matching against whole words only
520 return (matchAgainst
.Find(matchText
) != -1);
522 wxString
left(matchAgainst
);
523 bool success
= FALSE
;
525 int matchTextLen
= (int) matchText
.Length();
526 while (!success
&& !matchAgainst
.IsEmpty())
528 pos
= left
.Find(matchText
);
532 bool firstCharOK
= FALSE
;
533 bool lastCharOK
= FALSE
;
534 if (pos
== 0 || !wxIsalnum(left
[(size_t) (pos
-1)]))
537 if (((pos
+ matchTextLen
) == (int) left
.Length()) || !wxIsalnum(left
[(size_t) (pos
+ matchTextLen
)]))
540 if (firstCharOK
&& lastCharOK
)
543 left
= left
.Mid(pos
+1);