]>
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
6 // Copyright: (c) 2000 Brian Gavin
7 // (c) 2009 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_XRC && wxUSE_LISTCTRL
20 #include "wx/xrc/xh_listc.h"
23 #include "wx/textctrl.h"
26 #include "wx/listctrl.h"
27 #include "wx/imaglist.h"
32 const char *LISTCTRL_CLASS_NAME
= "wxListCtrl";
33 const char *LISTITEM_CLASS_NAME
= "listitem";
34 const char *LISTCOL_CLASS_NAME
= "listcol";
36 } // anonymous namespace
39 IMPLEMENT_DYNAMIC_CLASS(wxListCtrlXmlHandler
, wxXmlResourceHandler
)
41 wxListCtrlXmlHandler::wxListCtrlXmlHandler()
42 : wxXmlResourceHandler()
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
);
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
);
78 wxObject
*wxListCtrlXmlHandler::DoCreateResource()
80 if ( m_class
== LISTITEM_CLASS_NAME
)
84 else if ( m_class
== LISTCOL_CLASS_NAME
)
90 wxASSERT_MSG( m_class
== LISTCTRL_CLASS_NAME
,
91 "can't handle unknown node" );
93 return HandleListCtrl();
96 return m_parentAsWindow
;
99 bool wxListCtrlXmlHandler::CanHandle(wxXmlNode
*node
)
101 return IsOfClass(node
, LISTCTRL_CLASS_NAME
) ||
102 IsOfClass(node
, LISTITEM_CLASS_NAME
) ||
103 IsOfClass(node
, LISTCOL_CLASS_NAME
);
106 void wxListCtrlXmlHandler::HandleCommonItemAttrs(wxListItem
& item
)
108 if (HasParam(wxT("align")))
109 item
.SetAlign((wxListColumnFormat
)GetStyle(wxT("align")));
110 if (HasParam(wxT("text")))
111 item
.SetText(GetText(wxT("text")));
114 void wxListCtrlXmlHandler::HandleListCol()
116 wxListCtrl
* const list
= wxDynamicCast(m_parentAsWindow
, wxListCtrl
);
117 wxCHECK_RET( list
, "must have wxListCtrl parent" );
119 if ( !list
->HasFlag(wxLC_REPORT
) )
121 ReportError("Only report mode list controls can have columns.");
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")));
133 list
->InsertColumn(list
->GetColumnCount(), item
);
136 void wxListCtrlXmlHandler::HandleListItem()
138 wxListCtrl
* const list
= wxDynamicCast(m_parentAsWindow
, wxListCtrl
);
139 wxCHECK_RET( list
, "must have wxListCtrl parent" );
143 HandleCommonItemAttrs(item
);
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")));
160 // the list control icon style, may be 0
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
);
169 if ( image
!= wxNOT_FOUND
)
170 item
.SetImage(image
);
172 // append the list item to the control
173 item
.SetId(list
->GetItemCount());
175 list
->InsertItem(item
);
178 wxListCtrl
*wxListCtrlXmlHandler::HandleListCtrl()
180 XRC_MAKE_INSTANCE(list
, wxListCtrl
)
182 list
->Create(m_parentAsWindow
,
184 GetPosition(), GetSize(),
189 // we can optionally have normal and/or small image lists
190 wxImageList
*imagelist
;
191 imagelist
= GetImageList(wxT("imagelist"));
193 list
->AssignImageList(imagelist
, wxIMAGE_LIST_NORMAL
);
194 imagelist
= GetImageList(wxT("imagelist-small"));
196 list
->AssignImageList(imagelist
, wxIMAGE_LIST_SMALL
);
198 CreateChildrenPrivately(list
);
204 long wxListCtrlXmlHandler::GetImageIndex(wxListCtrl
*listctrl
, int which
)
206 // use different tag names depending on whether we need a normal or small
213 case wxIMAGE_LIST_SMALL
:
214 bmpParam
+= "-small";
215 imgParam
+= "-small";
218 case wxIMAGE_LIST_NORMAL
:
223 wxFAIL_MSG( "unsupported image list kind" );
227 // look for either bitmap or image tags
228 int imgIndex
= wxNOT_FOUND
;
229 if ( HasParam(bmpParam
) )
231 // we implicitly construct an image list containing the specified
233 wxBitmap bmp
= GetBitmap(bmpParam
, wxART_OTHER
);
235 // create the image list on demand for the first bitmap
236 wxImageList
*imgList
= listctrl
->GetImageList(which
);
239 imgList
= new wxImageList( bmp
.GetWidth(), bmp
.GetHeight() );
240 listctrl
->AssignImageList( imgList
, which
);
243 imgIndex
= imgList
->Add(bmp
);
246 if ( HasParam(imgParam
) )
248 if ( imgIndex
!= wxNOT_FOUND
)
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
253 ReportError(wxString::Format(
254 "listitem %s attribute ignored because %s is also specified",
255 bmpParam
, imgParam
));
258 // just use the specified index directly
259 imgIndex
= GetLong(imgParam
);
265 #endif // wxUSE_XRC && wxUSE_LISTCTRL