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