Debian Codegraph @

wxcrafter/xrc_handlers/myxh_listc.cpp

raw
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 #include "myxh_listc.h"
13 #include <wx/imaglist.h>
14 #include <wx/listctrl.h>
15 #include <wx/textctrl.h>
16
17 namespace
18 {
19
20 const wxString LISTCTRL_CLASS_NAME = wxT("wxListCtrl");
21 const wxString LISTITEM_CLASS_NAME = wxT("listitem");
22 const wxString LISTCOL_CLASS_NAME = wxT("listcol");
23
24 } // anonymous namespace
25
26 MYwxListCtrlXmlHandler::MYwxListCtrlXmlHandler()
27 : wxXmlResourceHandler()
28 {
29 // wxListItem styles
30 XRC_ADD_STYLE(wxLIST_FORMAT_LEFT);
31 XRC_ADD_STYLE(wxLIST_FORMAT_RIGHT);
32 XRC_ADD_STYLE(wxLIST_FORMAT_CENTRE);
33 XRC_ADD_STYLE(wxLIST_MASK_STATE);
34 XRC_ADD_STYLE(wxLIST_MASK_TEXT);
35 XRC_ADD_STYLE(wxLIST_MASK_IMAGE);
36 XRC_ADD_STYLE(wxLIST_MASK_DATA);
37 XRC_ADD_STYLE(wxLIST_MASK_WIDTH);
38 XRC_ADD_STYLE(wxLIST_MASK_FORMAT);
39 XRC_ADD_STYLE(wxLIST_STATE_FOCUSED);
40 XRC_ADD_STYLE(wxLIST_STATE_SELECTED);
41
42 // wxListCtrl styles
43 XRC_ADD_STYLE(wxLC_LIST);
44 XRC_ADD_STYLE(wxLC_REPORT);
45 XRC_ADD_STYLE(wxLC_ICON);
46 XRC_ADD_STYLE(wxLC_SMALL_ICON);
47 XRC_ADD_STYLE(wxLC_ALIGN_TOP);
48 XRC_ADD_STYLE(wxLC_ALIGN_LEFT);
49 XRC_ADD_STYLE(wxLC_AUTOARRANGE);
50 XRC_ADD_STYLE(wxLC_USER_TEXT);
51 XRC_ADD_STYLE(wxLC_EDIT_LABELS);
52 XRC_ADD_STYLE(wxLC_NO_HEADER);
53 XRC_ADD_STYLE(wxLC_SINGLE_SEL);
54 XRC_ADD_STYLE(wxLC_SORT_ASCENDING);
55 XRC_ADD_STYLE(wxLC_SORT_DESCENDING);
56 XRC_ADD_STYLE(wxLC_VIRTUAL);
57 XRC_ADD_STYLE(wxLC_HRULES);
58 XRC_ADD_STYLE(wxLC_VRULES);
59 XRC_ADD_STYLE(wxLC_NO_SORT_HEADER);
60 AddWindowStyles();
61 }
62
63 wxObject* MYwxListCtrlXmlHandler::DoCreateResource()
64 {
65 if(m_class == LISTITEM_CLASS_NAME) {
66 HandleListItem();
67 } else if(m_class == LISTCOL_CLASS_NAME) {
68 HandleListCol();
69 } else {
70 wxASSERT_MSG(m_class == LISTCTRL_CLASS_NAME, wxT("can't handle unknown node"));
71
72 return HandleListCtrl();
73 }
74
75 return m_parentAsWindow;
76 }
77
78 bool MYwxListCtrlXmlHandler::CanHandle(wxXmlNode* node)
79 {
80 return IsOfClass(node, LISTCTRL_CLASS_NAME) || IsOfClass(node, LISTITEM_CLASS_NAME) ||
81 IsOfClass(node, LISTCOL_CLASS_NAME);
82 }
83
84 void MYwxListCtrlXmlHandler::HandleCommonItemAttrs(wxListItem& item)
85 {
86 if(HasParam(wxT("align"))) item.SetAlign((wxListColumnFormat)GetStyle(wxT("align")));
87 if(HasParam(wxT("text"))) item.SetText(GetText(wxT("text")));
88 }
89
90 void MYwxListCtrlXmlHandler::HandleListCol()
91 {
92 wxListCtrl* const list = wxDynamicCast(m_parentAsWindow, wxListCtrl);
93 wxCHECK_RET(list, wxT("must have wxListCtrl parent"));
94
95 if(!list->HasFlag(wxLC_REPORT)) { return; }
96
97 wxListItem item;
98
99 HandleCommonItemAttrs(item);
100 if(HasParam(wxT("width"))) item.SetWidth((int)GetLong(wxT("width")));
101 if(HasParam(wxT("image"))) item.SetImage((int)GetLong(wxT("image")));
102
103 list->InsertColumn(list->GetColumnCount(), item);
104 }
105
106 void MYwxListCtrlXmlHandler::HandleListItem()
107 {
108 wxListCtrl* const list = wxDynamicCast(m_parentAsWindow, wxListCtrl);
109 wxCHECK_RET(list, wxT("must have wxListCtrl parent"));
110
111 wxListItem item;
112
113 HandleCommonItemAttrs(item);
114
115 if(HasParam(wxT("bg"))) item.SetBackgroundColour(GetColour(wxT("bg")));
116 if(HasParam(wxT("col"))) item.SetColumn((int)GetLong(wxT("col")));
117 if(HasParam(wxT("data"))) item.SetData(GetLong(wxT("data")));
118 if(HasParam(wxT("font"))) item.SetFont(GetFont());
119 if(HasParam(wxT("state"))) item.SetState(GetStyle(wxT("state")));
120 if(HasParam(wxT("textcolour"))) item.SetTextColour(GetColour(wxT("textcolour")));
121 if(HasParam(wxT("textcolor"))) item.SetTextColour(GetColour(wxT("textcolor")));
122
123 // the list control icon style, may be 0
124 int image;
125 if(list->HasFlag(wxLC_ICON))
126 image = GetImageIndex(list, wxIMAGE_LIST_NORMAL);
127 else if(list->HasFlag(wxLC_SMALL_ICON) || list->HasFlag(wxLC_REPORT) || list->HasFlag(wxLC_LIST))
128 image = GetImageIndex(list, wxIMAGE_LIST_SMALL);
129 else
130 image = wxNOT_FOUND;
131
132 if(image != wxNOT_FOUND) item.SetImage(image);
133
134 // append the list item to the control
135 item.SetId(list->GetItemCount());
136
137 list->InsertItem(item);
138 }
139
140 wxListCtrl* MYwxListCtrlXmlHandler::HandleListCtrl()
141 {
142 XRC_MAKE_INSTANCE(list, wxListCtrl)
143
144 list->Create(m_parentAsWindow, GetID(), GetPosition(), GetSize(), GetStyle(), wxDefaultValidator, GetName());
145
146 #if wxVERSION_NUMBER > 2900
147 // we can optionally have normal and/or small image lists
148 wxImageList* imagelist;
149 imagelist = GetImageList(wxT("imagelist"));
150 if(imagelist) list->AssignImageList(imagelist, wxIMAGE_LIST_NORMAL);
151 imagelist = GetImageList(wxT("imagelist-small"));
152 if(imagelist) list->AssignImageList(imagelist, wxIMAGE_LIST_SMALL);
153 #endif
154
155 CreateChildrenPrivately(list);
156 SetupWindow(list);
157
158 return list;
159 }
160
161 long MYwxListCtrlXmlHandler::GetImageIndex(wxListCtrl* listctrl, int which)
162 {
163 // use different tag names depending on whether we need a normal or small
164 // image
165 wxString bmpParam(wxT("bitmap")), imgParam(wxT("image"));
166 switch(which) {
167 case wxIMAGE_LIST_SMALL:
168 bmpParam += wxT("-small");
169 imgParam += wxT("-small");
170 break;
171
172 case wxIMAGE_LIST_NORMAL:
173 // nothing to do
174 break;
175
176 default:
177 wxFAIL_MSG(wxT("unsupported image list kind"));
178 return wxNOT_FOUND;
179 }
180
181 // look for either bitmap or image tags
182 int imgIndex = wxNOT_FOUND;
183 if(HasParam(bmpParam)) {
184 // we implicitly construct an image list containing the specified
185 // bitmaps
186 wxBitmap bmp = GetBitmap(bmpParam, wxART_OTHER);
187
188 // create the image list on demand for the first bitmap
189 wxImageList* imgList = listctrl->GetImageList(which);
190 if(!imgList) {
191 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight());
192 listctrl->AssignImageList(imgList, which);
193 }
194
195 imgIndex = imgList->Add(bmp);
196 }
197
198 if(HasParam(imgParam)) {
199
200 // just use the specified index directly
201 imgIndex = GetLong(imgParam);
202 }
203
204 return imgIndex;
205 }