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