Define _CRT_NONSTDC_NO_WARNINGS for zlib compilation with MSVC.
[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 // Copyright: (c) 2000 Brian Gavin
7 // (c) 2009 Vadim Zeitlin
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 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
38
39 IMPLEMENT_DYNAMIC_CLASS(wxListCtrlXmlHandler, wxXmlResourceHandler)
40
41 wxListCtrlXmlHandler::wxListCtrlXmlHandler()
42 : wxXmlResourceHandler()
43 {
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);
54 XRC_ADD_STYLE(wxLIST_STATE_FOCUSED);
55 XRC_ADD_STYLE(wxLIST_STATE_SELECTED);
56
57 // wxListCtrl styles
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);
71 XRC_ADD_STYLE(wxLC_VIRTUAL);
72 XRC_ADD_STYLE(wxLC_HRULES);
73 XRC_ADD_STYLE(wxLC_VRULES);
74 XRC_ADD_STYLE(wxLC_NO_SORT_HEADER);
75 AddWindowStyles();
76 }
77
78 wxObject *wxListCtrlXmlHandler::DoCreateResource()
79 {
80 if ( m_class == LISTITEM_CLASS_NAME )
81 {
82 HandleListItem();
83 }
84 else if ( m_class == LISTCOL_CLASS_NAME )
85 {
86 HandleListCol();
87 }
88 else
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;
97 }
98
99 bool wxListCtrlXmlHandler::CanHandle(wxXmlNode *node)
100 {
101 return IsOfClass(node, LISTCTRL_CLASS_NAME) ||
102 IsOfClass(node, LISTITEM_CLASS_NAME) ||
103 IsOfClass(node, LISTCOL_CLASS_NAME);
104 }
105
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()
115 {
116 wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl);
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 }
124
125 wxListItem item;
126
127 HandleCommonItemAttrs(item);
128 if (HasParam(wxT("width")))
129 item.SetWidth((int)GetLong(wxT("width")));
130 if (HasParam(wxT("image")))
131 item.SetImage((int)GetLong(wxT("image")));
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
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")))
152 item.SetFont(GetFont(wxT("font"), list));
153 if (HasParam(wxT("state")))
154 item.SetState(GetStyle(wxT("state")));
155 if (HasParam(wxT("textcolour")))
156 item.SetTextColour(GetColour(wxT("textcolour")));
157 if (HasParam(wxT("textcolor")))
158 item.SetTextColour(GetColour(wxT("textcolor")));
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);
164 else if ( list->HasFlag(wxLC_SMALL_ICON) || list->HasFlag(wxLC_REPORT) || list->HasFlag(wxLC_LIST) )
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
175 list->InsertItem(item);
176 }
177
178 wxListCtrl *wxListCtrlXmlHandler::HandleListCtrl()
179 {
180 XRC_MAKE_INSTANCE(list, wxListCtrl)
181
182 list->Create(m_parentAsWindow,
183 GetID(),
184 GetPosition(), GetSize(),
185 GetStyle(),
186 wxDefaultValidator,
187 GetName());
188
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);
197
198 CreateChildrenPrivately(list);
199 SetupWindow(list);
200
201 return list;
202 }
203
204 long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl *listctrl, int which)
205 {
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;
263 }
264
265 #endif // wxUSE_XRC && wxUSE_LISTCTRL