]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xh_listc.cpp
Add wxDocManager::FindDocumentByPath() helper.
[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 if (HasParam(wxT("image")))
132 item.SetImage((int)GetLong(wxT("image")));
133
134 list->InsertColumn(list->GetColumnCount(), item);
135 }
136
137 void 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
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")))
153 item.SetFont(GetFont(wxT("font"), list));
154 if (HasParam(wxT("state")))
155 item.SetState(GetStyle(wxT("state")));
156 if (HasParam(wxT("textcolour")))
157 item.SetTextColour(GetColour(wxT("textcolour")));
158 if (HasParam(wxT("textcolor")))
159 item.SetTextColour(GetColour(wxT("textcolor")));
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);
165 else if ( list->HasFlag(wxLC_SMALL_ICON) || list->HasFlag(wxLC_REPORT) || list->HasFlag(wxLC_LIST) )
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
176 list->InsertItem(item);
177 }
178
179 wxListCtrl *wxListCtrlXmlHandler::HandleListCtrl()
180 {
181 XRC_MAKE_INSTANCE(list, wxListCtrl)
182
183 list->Create(m_parentAsWindow,
184 GetID(),
185 GetPosition(), GetSize(),
186 GetStyle(),
187 wxDefaultValidator,
188 GetName());
189
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);
198
199 CreateChildrenPrivately(list);
200 SetupWindow(list);
201
202 return list;
203 }
204
205 long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl *listctrl, int which)
206 {
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;
264 }
265
266 #endif // wxUSE_XRC && wxUSE_LISTCTRL