1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
10 /////////////////////////////////////////////////////////////////////////////
14 * \brief A file of utility functions and classes.
20 #include "wx/imaglist.h"
25 * \defgroup ForwardDeclarations Forward Declations
30 class WXDLLEXPORT wxImage
;
31 class WXDLLEXPORT wxNotebook
;
32 class WXDLLEXPORT wxInputStream
;
33 class WXDLLEXPORT wxOutputStream
;
34 class WXDLLEXPORT wxFileInputStream
;
35 class WXDLLEXPORT wxFileOutputStream
;
36 class WXDLLEXPORT wxDataInputStream
;
37 class WXDLLEXPORT wxDataOutputStream
;
38 class WXDLLEXPORT wxSplitterWindow
;
39 class WXDLLEXPORT wxVariant
;
40 class WXDLLEXPORT wxListCtrl
;
50 #define wxNEWLINE wxT("\r\n")
52 #define wxNEWLINE wxT("\n")
55 /// Returns the image type, or -1, determined from the extension.
56 int apDetermineImageType(const wxString
& filename
);
58 /// Convert a colour to a 6-digit hex string
59 wxString
apColourToHexString(const wxColour
& col
);
61 /// Convert 6-digit hex string to a colour
62 wxColour
apHexStringToColour(const wxString
& hex
);
64 /// Convert a wxFont to a string
65 wxString
apFontToString(const wxFont
& font
);
67 /// Convert a string to a wxFont
68 wxFont
apStringToFont(const wxString
& str
);
70 /// Get the index of the given named wxNotebook page
71 int apFindNotebookPage(wxNotebook
* notebook
, const wxString
& name
);
73 /// Returns the system temporary directory.
74 wxString
wxGetTempDir();
76 /// Launch the application associated with the filename's extension
77 bool apInvokeAppForFile(const wxString
& filename
);
79 /// \brief Find the absolute path where this application has been run from.
81 /// \param argv0 wxTheApp->argv[0]
82 /// \param cwd The current working directory (at startup)
83 /// \param appVariableName The name of a variable containing the directory for this app, e.g.
84 /// MYAPPDIR. This is used as a last resort.
85 wxString
apFindAppPath(const wxString
& argv0
, const wxString
& cwd
, const wxString
& appVariableName
= wxEmptyString
);
87 /// Adds a context-sensitive help button, for non-Windows platforms
88 void apAddContextHelpButton(wxWindow
* parent
, wxSizer
* sizer
, int sizerFlags
= wxALIGN_CENTRE
|wxALL
, int sizerBorder
= 5);
90 /// Get selected wxNotebook page
91 wxWindow
* apNotebookGetSelectedPage(wxNotebook
* notebook
);
93 #define wxMAX_ICON_STATES 4
97 wxIconInfo, wxIconTable
98 associate multiple state icons with items in tree controls
99 (and potentially other controls).
101 So instead of having to remember a lot of image list ids,
102 you have a named state info object which contains up to 4 different states
103 (identified by the integers 0 - 3). Each of these states can
104 be in a further 2 sub-states - enabled or disabled.
106 wxIconTable holds a list of these state info objects
107 and has a convenient API. For example, the following adds
108 icons for a checkbox item that can be: on/enabled, off/enabled,
109 on/disabled,off/disabled.
111 m_iconTable.AddInfo("Checkbox", wxICON(checked), 0, true);
112 m_iconTable.AddInfo("Checkbox", wxICON(checked_dis), 0, false);
113 m_iconTable.AddInfo("Checkbox", wxICON(unchecked), 1, true);
114 m_iconTable.AddInfo("Checkbox", wxICON(unchecked_dis), 1, false);
116 When you update the item image in response to (e.g.) user interaction,
117 you can say something like this:
119 int iconId = m_iconTable.GetIconId("Checkbox", 0, false);
121 treeCtrl.SetItemImage(itemId, iconId, wxTreeItemIcon_Normal);
122 treeCtrl.SetItemImage(itemId, iconId, wxTreeItemIcon_Selected);
128 * Stores information about the visual state of an item in a tree control
131 class wxIconInfo
: public wxObject
134 wxIconInfo(const wxString
& name
);
136 // How many states? (Each state
137 // has enabled/disabled state)
138 // Max (say) 4 states, each with
140 int GetStateCount() const { return m_maxStates
; };
142 void SetStateCount(int count
) { m_maxStates
= count
; }
143 int GetIconId(int state
, bool enabled
= true) const;
144 void SetIconId(int state
, bool enabled
, int iconId
);
146 const wxString
& GetName() const { return m_name
; }
150 int m_states
[wxMAX_ICON_STATES
* 2]; // Enabled/disabled
151 wxString m_name
; // Name of icon, e.g. "Package"
156 * Contains a list of wxIconInfos
159 class wxIconTable
: public wxList
162 wxIconTable(wxImageList
* imageList
= NULL
);
164 void AppendInfo(wxIconInfo
* info
);
166 // Easy way of initialising both the image list and the
167 // info db. It will generate image ids itself while appending the icon.
168 // 'state' is an integer from 0 up to the max allowed, representing a different
169 // state. There may be only one, or (for a checkbox) there may be two.
170 // A folder that can be open or closed would have two states.
171 // Enabled/disabled is taken as a special case.
172 bool AddInfo(const wxString
& name
, const wxIcon
& icon
, int state
, bool enabled
);
174 wxIconInfo
* FindInfo(const wxString
& name
) const;
176 int GetIconId(const wxString
& name
, int state
, bool enabled
= true) const;
177 bool SetIconId(const wxString
& name
, int state
, bool enabled
, int iconId
) ;
179 void SetImageList(wxImageList
* imageList
) { m_imageList
= imageList
; }
180 wxImageList
* GetImageList() const { return m_imageList
; }
183 wxImageList
* m_imageList
;
186 /// Useful insertion operators for wxOutputStream.
187 wxOutputStream
& operator <<(wxOutputStream
&, const wxString
& s
);
188 wxOutputStream
& operator <<(wxOutputStream
&, const char c
);
189 wxOutputStream
& operator <<(wxOutputStream
&, long l
);
191 // Convert characters to HTML equivalents
192 wxString
ctEscapeHTMLCharacters(const wxString
& str
);
194 // Match 'matchText' against 'matchAgainst', optionally constraining to
196 bool ctMatchString(const wxString
& matchAgainst
, const wxString
& matchText
, bool wholeWordOnly
);