]>
git.saurik.com Git - wxWidgets.git/blob - 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
7 // Copyright: (c) 2000 Brian Gavin
8 // (c) 2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_XRC && wxUSE_LISTCTRL
21 #include "wx/xrc/xh_listc.h"
24 #include "wx/textctrl.h"
27 #include "wx/listctrl.h"
28 #include "wx/imaglist.h"
33 const char *LISTCTRL_CLASS_NAME
= "wxListCtrl";
34 const char *LISTITEM_CLASS_NAME
= "listitem";
35 const char *LISTCOL_CLASS_NAME
= "listcol";
37 } // anonymous namespace
40 IMPLEMENT_DYNAMIC_CLASS(wxListCtrlXmlHandler
, wxXmlResourceHandler
)
42 wxListCtrlXmlHandler::wxListCtrlXmlHandler()
43 : wxXmlResourceHandler()
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
);
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
);
79 wxObject
*wxListCtrlXmlHandler::DoCreateResource()
81 if ( m_class
== LISTITEM_CLASS_NAME
)
85 else if ( m_class
== LISTCOL_CLASS_NAME
)
91 wxASSERT_MSG( m_class
== LISTCTRL_CLASS_NAME
,
92 "can't handle unknown node" );
94 return HandleListCtrl();
97 return m_parentAsWindow
;
100 bool wxListCtrlXmlHandler::CanHandle(wxXmlNode
*node
)
102 return IsOfClass(node
, LISTCTRL_CLASS_NAME
) ||
103 IsOfClass(node
, LISTITEM_CLASS_NAME
) ||
104 IsOfClass(node
, LISTCOL_CLASS_NAME
);
107 void wxListCtrlXmlHandler::HandleCommonItemAttrs(wxListItem
& item
)
109 if (HasParam(wxT("align")))
110 item
.SetAlign((wxListColumnFormat
)GetStyle(wxT("align")));
111 if (HasParam(wxT("text")))
112 item
.SetText(GetText(wxT("text")));
115 void wxListCtrlXmlHandler::HandleListCol()
117 wxListCtrl
* const list
= wxDynamicCast(m_parentAsWindow
, wxListCtrl
);
118 wxCHECK_RET( list
, "must have wxListCtrl parent" );
120 if ( !list
->HasFlag(wxLC_REPORT
) )
122 ReportError("Only report mode list controls can have columns.");
128 HandleCommonItemAttrs(item
);
129 if (HasParam(wxT("width")))
130 item
.SetWidth((int)GetLong(wxT("width")));
132 list
->InsertColumn(list
->GetColumnCount(), item
);
135 void wxListCtrlXmlHandler::HandleListItem()
137 wxListCtrl
* const list
= wxDynamicCast(m_parentAsWindow
, wxListCtrl
);
138 wxCHECK_RET( list
, "must have wxListCtrl parent" );
142 HandleCommonItemAttrs(item
);
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());
152 if (HasParam(wxT("state")))
153 item
.SetState(GetStyle(wxT("state")));
154 if (HasParam(wxT("textcolour")))
155 item
.SetTextColour(GetColour(wxT("textcolour")));
156 if (HasParam(wxT("textcolor")))
157 item
.SetTextColour(GetColour(wxT("textcolor")));
159 // the list control icon style, may be 0
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
);
168 if ( image
!= wxNOT_FOUND
)
169 item
.SetImage(image
);
171 // append the list item to the control
172 item
.SetId(list
->GetItemCount());
174 list
->InsertItem(item
);
177 wxListCtrl
*wxListCtrlXmlHandler::HandleListCtrl()
179 XRC_MAKE_INSTANCE(list
, wxListCtrl
)
181 list
->Create(m_parentAsWindow
,
183 GetPosition(), GetSize(),
188 // we can optionally have normal and/or small image lists
189 wxImageList
*imagelist
;
190 imagelist
= GetImageList(wxT("imagelist"));
192 list
->AssignImageList(imagelist
, wxIMAGE_LIST_NORMAL
);
193 imagelist
= GetImageList(wxT("imagelist-small"));
195 list
->AssignImageList(imagelist
, wxIMAGE_LIST_SMALL
);
197 CreateChildrenPrivately(list
);
203 long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl
*listctrl
, int which
)
205 // use different tag names depending on whether we need a normal or small
212 case wxIMAGE_LIST_SMALL
:
213 bmpParam
+= "-small";
214 imgParam
+= "-small";
217 case wxIMAGE_LIST_NORMAL
:
222 wxFAIL_MSG( "unsupported image list kind" );
226 // look for either bitmap or image tags
227 int imgIndex
= wxNOT_FOUND
;
228 if ( HasParam(bmpParam
) )
230 // we implicitly construct an image list containing the specified
232 wxBitmap bmp
= GetBitmap(bmpParam
, wxART_OTHER
);
234 // create the image list on demand for the first bitmap
235 wxImageList
*imgList
= listctrl
->GetImageList(which
);
238 imgList
= new wxImageList( bmp
.GetWidth(), bmp
.GetHeight() );
239 listctrl
->AssignImageList( imgList
, which
);
242 imgIndex
= imgList
->Add(bmp
);
245 if ( HasParam(imgParam
) )
247 if ( imgIndex
!= wxNOT_FOUND
)
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
252 ReportError(wxString::Format(
253 "listitem %s attribute ignored because %s is also specified",
254 bmpParam
, imgParam
));
257 // just use the specified index directly
258 imgIndex
= GetLong(imgParam
);
264 #endif // wxUSE_XRC && wxUSE_LISTCTRL