]>
Commit | Line | Data |
---|---|---|
78d14f80 | 1 | ///////////////////////////////////////////////////////////////////////////// |
fec9cc08 | 2 | // Name: src/xrc/xh_listc.cpp |
b5d6954b | 3 | // Purpose: XRC resource for wxListCtrl |
ef18e792 | 4 | // Author: Brian Gavin, Kinaou Hervé, Vadim Zeitlin |
78d14f80 | 5 | // Created: 2000/09/09 |
78d14f80 | 6 | // Copyright: (c) 2000 Brian Gavin |
ef18e792 | 7 | // (c) 2009 Vadim Zeitlin |
78d14f80 VS |
8 | // Licence: wxWindows licence |
9 | ///////////////////////////////////////////////////////////////////////////// | |
f80ea77b | 10 | |
78d14f80 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
fec9cc08 | 18 | #if wxUSE_XRC && wxUSE_LISTCTRL |
a1e4ec87 | 19 | |
78d14f80 | 20 | #include "wx/xrc/xh_listc.h" |
fec9cc08 WS |
21 | |
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/textctrl.h" | |
24 | #endif | |
25 | ||
78d14f80 | 26 | #include "wx/listctrl.h" |
326462ae | 27 | #include "wx/imaglist.h" |
78d14f80 | 28 | |
ef18e792 VZ |
29 | namespace |
30 | { | |
31 | ||
32 | const char *LISTCTRL_CLASS_NAME = "wxListCtrl"; | |
33 | const char *LISTITEM_CLASS_NAME = "listitem"; | |
34 | const char *LISTCOL_CLASS_NAME = "listcol"; | |
35 | ||
36 | } // anonymous namespace | |
37 | ||
78d14f80 | 38 | |
854e189f VS |
39 | IMPLEMENT_DYNAMIC_CLASS(wxListCtrlXmlHandler, wxXmlResourceHandler) |
40 | ||
f80ea77b | 41 | wxListCtrlXmlHandler::wxListCtrlXmlHandler() |
ef18e792 | 42 | : wxXmlResourceHandler() |
78d14f80 | 43 | { |
326462ae VZ |
44 | // wxListItem styles |
45 | XRC_ADD_STYLE(wxLIST_FORMAT_LEFT); | |
46 | XRC_ADD_STYLE(wxLIST_FORMAT_RIGHT); | |
47 | XRC_ADD_STYLE(wxLIST_FORMAT_CENTRE); | |
48 | XRC_ADD_STYLE(wxLIST_MASK_STATE); | |
49 | XRC_ADD_STYLE(wxLIST_MASK_TEXT); | |
50 | XRC_ADD_STYLE(wxLIST_MASK_IMAGE); | |
51 | XRC_ADD_STYLE(wxLIST_MASK_DATA); | |
52 | XRC_ADD_STYLE(wxLIST_MASK_WIDTH); | |
53 | XRC_ADD_STYLE(wxLIST_MASK_FORMAT); | |
326462ae VZ |
54 | XRC_ADD_STYLE(wxLIST_STATE_FOCUSED); |
55 | XRC_ADD_STYLE(wxLIST_STATE_SELECTED); | |
326462ae VZ |
56 | |
57 | // wxListCtrl styles | |
544fee32 VS |
58 | XRC_ADD_STYLE(wxLC_LIST); |
59 | XRC_ADD_STYLE(wxLC_REPORT); | |
60 | XRC_ADD_STYLE(wxLC_ICON); | |
61 | XRC_ADD_STYLE(wxLC_SMALL_ICON); | |
62 | XRC_ADD_STYLE(wxLC_ALIGN_TOP); | |
63 | XRC_ADD_STYLE(wxLC_ALIGN_LEFT); | |
64 | XRC_ADD_STYLE(wxLC_AUTOARRANGE); | |
65 | XRC_ADD_STYLE(wxLC_USER_TEXT); | |
66 | XRC_ADD_STYLE(wxLC_EDIT_LABELS); | |
67 | XRC_ADD_STYLE(wxLC_NO_HEADER); | |
68 | XRC_ADD_STYLE(wxLC_SINGLE_SEL); | |
69 | XRC_ADD_STYLE(wxLC_SORT_ASCENDING); | |
70 | XRC_ADD_STYLE(wxLC_SORT_DESCENDING); | |
0689c79e | 71 | XRC_ADD_STYLE(wxLC_VIRTUAL); |
e7a0050f VS |
72 | XRC_ADD_STYLE(wxLC_HRULES); |
73 | XRC_ADD_STYLE(wxLC_VRULES); | |
74 | XRC_ADD_STYLE(wxLC_NO_SORT_HEADER); | |
78d14f80 VS |
75 | AddWindowStyles(); |
76 | } | |
77 | ||
78d14f80 | 78 | wxObject *wxListCtrlXmlHandler::DoCreateResource() |
326462ae | 79 | { |
ef18e792 VZ |
80 | if ( m_class == LISTITEM_CLASS_NAME ) |
81 | { | |
82 | HandleListItem(); | |
83 | } | |
84 | else if ( m_class == LISTCOL_CLASS_NAME ) | |
326462ae | 85 | { |
ef18e792 | 86 | HandleListCol(); |
326462ae VZ |
87 | } |
88 | else | |
ef18e792 VZ |
89 | { |
90 | wxASSERT_MSG( m_class == LISTCTRL_CLASS_NAME, | |
91 | "can't handle unknown node" ); | |
92 | ||
93 | return HandleListCtrl(); | |
94 | } | |
95 | ||
96 | return m_parentAsWindow; | |
326462ae VZ |
97 | } |
98 | ||
99 | bool wxListCtrlXmlHandler::CanHandle(wxXmlNode *node) | |
100 | { | |
ef18e792 VZ |
101 | return IsOfClass(node, LISTCTRL_CLASS_NAME) || |
102 | IsOfClass(node, LISTITEM_CLASS_NAME) || | |
103 | IsOfClass(node, LISTCOL_CLASS_NAME); | |
326462ae VZ |
104 | } |
105 | ||
ef18e792 VZ |
106 | void wxListCtrlXmlHandler::HandleCommonItemAttrs(wxListItem& item) |
107 | { | |
108 | if (HasParam(wxT("align"))) | |
109 | item.SetAlign((wxListColumnFormat)GetStyle(wxT("align"))); | |
110 | if (HasParam(wxT("text"))) | |
111 | item.SetText(GetText(wxT("text"))); | |
112 | } | |
113 | ||
114 | void wxListCtrlXmlHandler::HandleListCol() | |
326462ae VZ |
115 | { |
116 | wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl); | |
ef18e792 VZ |
117 | wxCHECK_RET( list, "must have wxListCtrl parent" ); |
118 | ||
119 | if ( !list->HasFlag(wxLC_REPORT) ) | |
120 | { | |
121 | ReportError("Only report mode list controls can have columns."); | |
122 | return; | |
123 | } | |
326462ae VZ |
124 | |
125 | wxListItem item; | |
126 | ||
ef18e792 VZ |
127 | HandleCommonItemAttrs(item); |
128 | if (HasParam(wxT("width"))) | |
129 | item.SetWidth((int)GetLong(wxT("width"))); | |
7243eb6d VS |
130 | if (HasParam(wxT("image"))) |
131 | item.SetImage((int)GetLong(wxT("image"))); | |
ef18e792 VZ |
132 | |
133 | list->InsertColumn(list->GetColumnCount(), item); | |
134 | } | |
135 | ||
136 | void wxListCtrlXmlHandler::HandleListItem() | |
137 | { | |
138 | wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl); | |
139 | wxCHECK_RET( list, "must have wxListCtrl parent" ); | |
140 | ||
141 | wxListItem item; | |
142 | ||
143 | HandleCommonItemAttrs(item); | |
144 | ||
326462ae VZ |
145 | if (HasParam(wxT("bg"))) |
146 | item.SetBackgroundColour(GetColour(wxT("bg"))); | |
147 | if (HasParam(wxT("col"))) | |
148 | item.SetColumn((int)GetLong(wxT("col"))); | |
149 | if (HasParam(wxT("data"))) | |
150 | item.SetData(GetLong(wxT("data"))); | |
151 | if (HasParam(wxT("font"))) | |
45df4bb6 | 152 | item.SetFont(GetFont(wxT("font"), list)); |
326462ae VZ |
153 | if (HasParam(wxT("state"))) |
154 | item.SetState(GetStyle(wxT("state"))); | |
326462ae VZ |
155 | if (HasParam(wxT("textcolour"))) |
156 | item.SetTextColour(GetColour(wxT("textcolour"))); | |
157 | if (HasParam(wxT("textcolor"))) | |
158 | item.SetTextColour(GetColour(wxT("textcolor"))); | |
326462ae VZ |
159 | |
160 | // the list control icon style, may be 0 | |
161 | int image; | |
162 | if ( list->HasFlag(wxLC_ICON) ) | |
163 | image = GetImageIndex(list, wxIMAGE_LIST_NORMAL); | |
d8eae94f | 164 | else if ( list->HasFlag(wxLC_SMALL_ICON) || list->HasFlag(wxLC_REPORT) || list->HasFlag(wxLC_LIST) ) |
326462ae VZ |
165 | image = GetImageIndex(list, wxIMAGE_LIST_SMALL); |
166 | else | |
167 | image = wxNOT_FOUND; | |
168 | ||
169 | if ( image != wxNOT_FOUND ) | |
170 | item.SetImage(image); | |
171 | ||
172 | // append the list item to the control | |
173 | item.SetId(list->GetItemCount()); | |
174 | ||
ef18e792 | 175 | list->InsertItem(item); |
326462ae VZ |
176 | } |
177 | ||
ef18e792 | 178 | wxListCtrl *wxListCtrlXmlHandler::HandleListCtrl() |
f80ea77b | 179 | { |
544fee32 | 180 | XRC_MAKE_INSTANCE(list, wxListCtrl) |
f2588180 | 181 | |
544fee32 VS |
182 | list->Create(m_parentAsWindow, |
183 | GetID(), | |
184 | GetPosition(), GetSize(), | |
185 | GetStyle(), | |
186 | wxDefaultValidator, | |
187 | GetName()); | |
f2588180 | 188 | |
326462ae VZ |
189 | // we can optionally have normal and/or small image lists |
190 | wxImageList *imagelist; | |
191 | imagelist = GetImageList(wxT("imagelist")); | |
192 | if ( imagelist ) | |
193 | list->AssignImageList(imagelist, wxIMAGE_LIST_NORMAL); | |
194 | imagelist = GetImageList(wxT("imagelist-small")); | |
195 | if ( imagelist ) | |
196 | list->AssignImageList(imagelist, wxIMAGE_LIST_SMALL); | |
f80ea77b | 197 | |
326462ae | 198 | CreateChildrenPrivately(list); |
78d14f80 | 199 | SetupWindow(list); |
f80ea77b | 200 | |
78d14f80 VS |
201 | return list; |
202 | } | |
203 | ||
326462ae | 204 | long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl *listctrl, int which) |
78d14f80 | 205 | { |
326462ae VZ |
206 | // use different tag names depending on whether we need a normal or small |
207 | // image | |
208 | wxString | |
209 | bmpParam("bitmap"), | |
210 | imgParam("image"); | |
211 | switch ( which ) | |
212 | { | |
213 | case wxIMAGE_LIST_SMALL: | |
214 | bmpParam += "-small"; | |
215 | imgParam += "-small"; | |
216 | break; | |
217 | ||
218 | case wxIMAGE_LIST_NORMAL: | |
219 | // nothing to do | |
220 | break; | |
221 | ||
222 | default: | |
223 | wxFAIL_MSG( "unsupported image list kind" ); | |
224 | return wxNOT_FOUND; | |
225 | } | |
226 | ||
227 | // look for either bitmap or image tags | |
228 | int imgIndex = wxNOT_FOUND; | |
229 | if ( HasParam(bmpParam) ) | |
230 | { | |
231 | // we implicitly construct an image list containing the specified | |
232 | // bitmaps | |
233 | wxBitmap bmp = GetBitmap(bmpParam, wxART_OTHER); | |
234 | ||
235 | // create the image list on demand for the first bitmap | |
236 | wxImageList *imgList = listctrl->GetImageList(which); | |
237 | if ( !imgList ) | |
238 | { | |
239 | imgList = new wxImageList( bmp.GetWidth(), bmp.GetHeight() ); | |
240 | listctrl->AssignImageList( imgList, which ); | |
241 | } | |
242 | ||
243 | imgIndex = imgList->Add(bmp); | |
244 | } | |
245 | ||
246 | if ( HasParam(imgParam) ) | |
247 | { | |
248 | if ( imgIndex != wxNOT_FOUND ) | |
249 | { | |
250 | // TODO: we should really check that only bitmap or only image tags | |
251 | // are used across all items of the control, not just in this | |
252 | // one | |
253 | ReportError(wxString::Format( | |
254 | "listitem %s attribute ignored because %s is also specified", | |
255 | bmpParam, imgParam)); | |
256 | } | |
257 | ||
258 | // just use the specified index directly | |
259 | imgIndex = GetLong(imgParam); | |
260 | } | |
261 | ||
262 | return imgIndex; | |
78d14f80 | 263 | } |
a1e4ec87 | 264 | |
fec9cc08 | 265 | #endif // wxUSE_XRC && wxUSE_LISTCTRL |