]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xh_listc.cpp
Add missing WXK constants for the control keys
[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")));
7243eb6d
VS
131 if (HasParam(wxT("image")))
132 item.SetImage((int)GetLong(wxT("image")));
ef18e792
VZ
133
134 list->InsertColumn(list->GetColumnCount(), item);
135}
136
137void wxListCtrlXmlHandler::HandleListItem()
138{
139 wxListCtrl * const list = wxDynamicCast(m_parentAsWindow, wxListCtrl);
140 wxCHECK_RET( list, "must have wxListCtrl parent" );
141
142 wxListItem item;
143
144 HandleCommonItemAttrs(item);
145
326462ae
VZ
146 if (HasParam(wxT("bg")))
147 item.SetBackgroundColour(GetColour(wxT("bg")));
148 if (HasParam(wxT("col")))
149 item.SetColumn((int)GetLong(wxT("col")));
150 if (HasParam(wxT("data")))
151 item.SetData(GetLong(wxT("data")));
152 if (HasParam(wxT("font")))
45df4bb6 153 item.SetFont(GetFont(wxT("font"), list));
326462ae
VZ
154 if (HasParam(wxT("state")))
155 item.SetState(GetStyle(wxT("state")));
326462ae
VZ
156 if (HasParam(wxT("textcolour")))
157 item.SetTextColour(GetColour(wxT("textcolour")));
158 if (HasParam(wxT("textcolor")))
159 item.SetTextColour(GetColour(wxT("textcolor")));
326462ae
VZ
160
161 // the list control icon style, may be 0
162 int image;
163 if ( list->HasFlag(wxLC_ICON) )
164 image = GetImageIndex(list, wxIMAGE_LIST_NORMAL);
d8eae94f 165 else if ( list->HasFlag(wxLC_SMALL_ICON) || list->HasFlag(wxLC_REPORT) || list->HasFlag(wxLC_LIST) )
326462ae
VZ
166 image = GetImageIndex(list, wxIMAGE_LIST_SMALL);
167 else
168 image = wxNOT_FOUND;
169
170 if ( image != wxNOT_FOUND )
171 item.SetImage(image);
172
173 // append the list item to the control
174 item.SetId(list->GetItemCount());
175
ef18e792 176 list->InsertItem(item);
326462ae
VZ
177}
178
ef18e792 179wxListCtrl *wxListCtrlXmlHandler::HandleListCtrl()
f80ea77b 180{
544fee32 181 XRC_MAKE_INSTANCE(list, wxListCtrl)
f2588180 182
544fee32
VS
183 list->Create(m_parentAsWindow,
184 GetID(),
185 GetPosition(), GetSize(),
186 GetStyle(),
187 wxDefaultValidator,
188 GetName());
f2588180 189
326462ae
VZ
190 // we can optionally have normal and/or small image lists
191 wxImageList *imagelist;
192 imagelist = GetImageList(wxT("imagelist"));
193 if ( imagelist )
194 list->AssignImageList(imagelist, wxIMAGE_LIST_NORMAL);
195 imagelist = GetImageList(wxT("imagelist-small"));
196 if ( imagelist )
197 list->AssignImageList(imagelist, wxIMAGE_LIST_SMALL);
f80ea77b 198
326462ae 199 CreateChildrenPrivately(list);
78d14f80 200 SetupWindow(list);
f80ea77b 201
78d14f80
VS
202 return list;
203}
204
326462ae 205long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl *listctrl, int which)
78d14f80 206{
326462ae
VZ
207 // use different tag names depending on whether we need a normal or small
208 // image
209 wxString
210 bmpParam("bitmap"),
211 imgParam("image");
212 switch ( which )
213 {
214 case wxIMAGE_LIST_SMALL:
215 bmpParam += "-small";
216 imgParam += "-small";
217 break;
218
219 case wxIMAGE_LIST_NORMAL:
220 // nothing to do
221 break;
222
223 default:
224 wxFAIL_MSG( "unsupported image list kind" );
225 return wxNOT_FOUND;
226 }
227
228 // look for either bitmap or image tags
229 int imgIndex = wxNOT_FOUND;
230 if ( HasParam(bmpParam) )
231 {
232 // we implicitly construct an image list containing the specified
233 // bitmaps
234 wxBitmap bmp = GetBitmap(bmpParam, wxART_OTHER);
235
236 // create the image list on demand for the first bitmap
237 wxImageList *imgList = listctrl->GetImageList(which);
238 if ( !imgList )
239 {
240 imgList = new wxImageList( bmp.GetWidth(), bmp.GetHeight() );
241 listctrl->AssignImageList( imgList, which );
242 }
243
244 imgIndex = imgList->Add(bmp);
245 }
246
247 if ( HasParam(imgParam) )
248 {
249 if ( imgIndex != wxNOT_FOUND )
250 {
251 // TODO: we should really check that only bitmap or only image tags
252 // are used across all items of the control, not just in this
253 // one
254 ReportError(wxString::Format(
255 "listitem %s attribute ignored because %s is also specified",
256 bmpParam, imgParam));
257 }
258
259 // just use the specified index directly
260 imgIndex = GetLong(imgParam);
261 }
262
263 return imgIndex;
78d14f80 264}
a1e4ec87 265
fec9cc08 266#endif // wxUSE_XRC && wxUSE_LISTCTRL