]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_listc.cpp
Add wxDataViewRendererBase::PrepareForItem() helper.
[wxWidgets.git] / src / xrc / xh_listc.cpp
CommitLineData
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
VS
5// Created: 2000/09/09
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Brian Gavin
ef18e792 8// (c) 2009 Vadim Zeitlin
78d14f80
VS
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
f80ea77b 11
78d14f80
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
fec9cc08 19#if wxUSE_XRC && wxUSE_LISTCTRL
a1e4ec87 20
78d14f80 21#include "wx/xrc/xh_listc.h"
fec9cc08
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/textctrl.h"
25#endif
26
78d14f80 27#include "wx/listctrl.h"
326462ae 28#include "wx/imaglist.h"
78d14f80 29
ef18e792
VZ
30namespace
31{
32
33const char *LISTCTRL_CLASS_NAME = "wxListCtrl";
34const char *LISTITEM_CLASS_NAME = "listitem";
35const char *LISTCOL_CLASS_NAME = "listcol";
36
37} // anonymous namespace
38
78d14f80 39
854e189f
VS
40IMPLEMENT_DYNAMIC_CLASS(wxListCtrlXmlHandler, wxXmlResourceHandler)
41
f80ea77b 42wxListCtrlXmlHandler::wxListCtrlXmlHandler()
ef18e792 43 : wxXmlResourceHandler()
78d14f80 44{
326462ae
VZ
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);
326462ae
VZ
55 XRC_ADD_STYLE(wxLIST_STATE_FOCUSED);
56 XRC_ADD_STYLE(wxLIST_STATE_SELECTED);
326462ae
VZ
57
58 // wxListCtrl styles
544fee32
VS
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);
0689c79e 72 XRC_ADD_STYLE(wxLC_VIRTUAL);
e7a0050f
VS
73 XRC_ADD_STYLE(wxLC_HRULES);
74 XRC_ADD_STYLE(wxLC_VRULES);
75 XRC_ADD_STYLE(wxLC_NO_SORT_HEADER);
78d14f80
VS
76 AddWindowStyles();
77}
78
78d14f80 79wxObject *wxListCtrlXmlHandler::DoCreateResource()
326462ae 80{
ef18e792
VZ
81 if ( m_class == LISTITEM_CLASS_NAME )
82 {
83 HandleListItem();
84 }
85 else if ( m_class == LISTCOL_CLASS_NAME )
326462ae 86 {
ef18e792 87 HandleListCol();
326462ae
VZ
88 }
89 else
ef18e792
VZ
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;
326462ae
VZ
98}
99
100bool wxListCtrlXmlHandler::CanHandle(wxXmlNode *node)
101{
ef18e792
VZ
102 return IsOfClass(node, LISTCTRL_CLASS_NAME) ||
103 IsOfClass(node, LISTITEM_CLASS_NAME) ||
104 IsOfClass(node, LISTCOL_CLASS_NAME);
326462ae
VZ
105}
106
ef18e792
VZ
107void 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
115void wxListCtrlXmlHandler::HandleListCol()
326462ae
VZ
116{
117 wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl);
ef18e792
VZ
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 }
326462ae
VZ
125
126 wxListItem item;
127
ef18e792
VZ
128 HandleCommonItemAttrs(item);
129 if (HasParam(wxT("width")))
130 item.SetWidth((int)GetLong(wxT("width")));
131
132 list->InsertColumn(list->GetColumnCount(), item);
133}
134
135void 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
326462ae
VZ
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());
326462ae
VZ
152 if (HasParam(wxT("state")))
153 item.SetState(GetStyle(wxT("state")));
326462ae
VZ
154 if (HasParam(wxT("textcolour")))
155 item.SetTextColour(GetColour(wxT("textcolour")));
156 if (HasParam(wxT("textcolor")))
157 item.SetTextColour(GetColour(wxT("textcolor")));
326462ae
VZ
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
ef18e792 174 list->InsertItem(item);
326462ae
VZ
175}
176
ef18e792 177wxListCtrl *wxListCtrlXmlHandler::HandleListCtrl()
f80ea77b 178{
544fee32 179 XRC_MAKE_INSTANCE(list, wxListCtrl)
f2588180 180
544fee32
VS
181 list->Create(m_parentAsWindow,
182 GetID(),
183 GetPosition(), GetSize(),
184 GetStyle(),
185 wxDefaultValidator,
186 GetName());
f2588180 187
326462ae
VZ
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);
f80ea77b 196
326462ae 197 CreateChildrenPrivately(list);
78d14f80 198 SetupWindow(list);
f80ea77b 199
78d14f80
VS
200 return list;
201}
202
326462ae 203long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl *listctrl, int which)
78d14f80 204{
326462ae
VZ
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;
78d14f80 262}
a1e4ec87 263
fec9cc08 264#endif // wxUSE_XRC && wxUSE_LISTCTRL