Debian Codegraph @

wxcrafter/controls/menu_item_wrapper.cpp

raw
1 #include "menu_item_wrapper.h"
2
3 #include "allocator_mgr.h"
4 #include "bool_property.h"
5 #include "choice_property.h"
6 #include "file_ficker_property.h"
7 #include "import_from_wxFB.h"
8 #include "import_from_xrc.h"
9 #include "winid_property.h"
10 #include "wxc_bitmap_code_generator.h"
11 #include "wxgui_defs.h"
12 #include "wxgui_helpers.h"
13 #include "xmlutils.h"
14
15 MenuItemWrapper::MenuItemWrapper()
16 : wxcWidget(ID_WXMENUITEM)
17 {
18 static int counter = 0;
19
20 wxString label;
21 label << wxT("Item") << (++counter);
22
23 RegisterEventCommand(wxT("wxEVT_MENU"), _("Menu item has been clicked"));
24
25 m_styles.Clear();
26 m_sizerFlags.Clear();
27 m_properties.DeleteValues();
28
29 SetPropertyString(_("Common Settings"), "wxMenuItem");
30 AddProperty(new CategoryProperty(_("Common Settings")));
31 AddProperty(new WinIdProperty());
32 AddProperty(new StringProperty(PROP_NAME, "", _("C++ variable name")));
33 AddProperty(new CategoryProperty(_("Menu Item")));
34 AddProperty(new StringProperty(PROP_LABEL, label, _("The menu item label")));
35 AddProperty(new StringProperty(PROP_ACCELERATOR, "", _("Keyboard Shortcut")));
36 AddProperty(new StringProperty(PROP_HELP, "", _("Short help string")));
37 AddProperty(new BitmapPickerProperty(PROP_BITMAP_PATH, "", _("Menu item image")));
38
39 m_namePattern = wxT("m_menuItem");
40 SetName(GenerateName());
41
42 AddProperty(new ChoiceProperty(PROP_KIND, wxCrafter::GetToolTypes(), 0,
43 _("The type of menu item: normal, radio, checkable or separator")));
44 AddProperty(new BoolProperty(PROP_CHECKED, false, _("For a checkable menu item, should this be checked")));
45 }
46
47 MenuItemWrapper::~MenuItemWrapper() {}
48
49 wxcWidget* MenuItemWrapper::Clone() const { return new MenuItemWrapper(); }
50
51 wxString MenuItemWrapper::CppCtorCode() const
52 {
53 wxString code;
54 wxString label;
55
56 wxcWidget* parent = GetParent(); // Find the first non-menu parent, to check it's kosher
57 while(parent && (parent->GetType() == ID_WXMENU || parent->GetType() == ID_WXSUBMENU)) {
58 parent = parent->GetParent();
59 }
60 if(parent && parent->GetType() == ID_WXTOOLBARITEM) {
61 // An item in a toolbaritem dropdown menu (probably an auitoolbar, as that's not yet implemented for the
62 // standard one)
63 // This is handled in a completely different way, so don't emit anything
64 return code;
65 }
66
67 wxcCodeGeneratorHelper::Get().AddBitmap(PropertyFile(PROP_BITMAP_PATH));
68 wxString ID = WindowID();
69
70 wxString actualKind;
71 wxCrafter::TOOL_TYPE toolType = wxCrafter::GetToolType(PropertyString(PROP_KIND));
72 switch(toolType) {
73 case wxCrafter::TOOL_TYPE_SEPARATOR:
74 ID = wxT("wxID_SEPARATOR");
75 break;
76
77 case wxCrafter::TOOL_TYPE_NORMAL:
78 actualKind = wxT("wxITEM_NORMAL");
79 break;
80
81 case wxCrafter::TOOL_TYPE_RADIO:
82 actualKind = wxT("wxITEM_RADIO");
83 break;
84
85 default:
86 case wxCrafter::TOOL_TYPE_CHECK:
87 actualKind = wxT("wxITEM_CHECK");
88 break;
89 }
90
91 if(ID == wxT("wxID_SEPARATOR")) {
92 code << GetWindowParent() << wxT("->AppendSeparator();\n");
93
94 } else {
95 label << PropertyString(PROP_LABEL);
96 wxString accel = PropertyString(PROP_ACCELERATOR).Trim().Trim(false);
97 if(!accel.IsEmpty()) {
98 label << wxT("\\t") << accel;
99 }
100 label.Trim().Trim(false);
101
102 code << GetName() << wxT(" = new ") << GetWxClassName() << wxT("(") << GetWindowParent() << wxT(", ")
103 << WindowID() << wxT(", ") << wxCrafter::UNDERSCORE(label) << wxT(", ")
104 << wxCrafter::UNDERSCORE(PropertyString(PROP_HELP)) << wxT(", ");
105
106 code << actualKind << wxT(");\n");
107
108 wxString bmpcode = wxcCodeGeneratorHelper::Get().BitmapCode(PropertyFile(PROP_BITMAP_PATH));
109 if(
110 #if defined(__WXGTK__)
111 (toolType ==
112 wxCrafter::TOOL_TYPE_NORMAL) && // gtk asserts if you try to give a bitmap to a check/radio menuitem
113 #endif
114 (bmpcode != wxT("wxNullBitmap"))) {
115 code << GetName() << wxT("->SetBitmap(") << bmpcode << wxT(");\n");
116 }
117
118 code << GetWindowParent() << wxT("->Append(") << GetName() << wxT(");\n");
119
120 if((actualKind == wxT("wxITEM_CHECK")) && (PropertyString(PROP_CHECKED) == "1")) {
121 code << GetName() << "->Check();\n";
122 }
123 }
124 return code;
125 }
126
127 void MenuItemWrapper::GetIncludeFile(wxArrayString& headers) const { headers.Add(wxT("#include <wx/menu.h>")); }
128
129 wxString MenuItemWrapper::GetWxClassName() const { return wxT("wxMenuItem"); }
130
131 void MenuItemWrapper::ToXRC(wxString& text, XRC_TYPE type) const
132 {
133 bool isSeparator = (WindowID() == wxT("wxID_SEPARATOR") || PropertyString(PROP_KIND) == ITEM_SEPARATOR);
134
135 if(isSeparator) {
136 text << wxT("<object class=\"separator\" />");
137
138 } else {
139 text << XRCPrefix() << XRCKind() << XRCLabel();
140 if(
141 #if defined(__WXGTK__)
142 (PropertyString(PROP_KIND) ==
143 ITEM_NORMAL) && // gtk asserts if you try to give a bitmap to a check/radio menuitem
144 #endif
145 (!PropertyFile(PROP_BITMAP_PATH).empty())) {
146 text << XRCBitmap();
147 }
148 text << wxT("<accel>") << wxCrafter::CDATA(PropertyString(PROP_ACCELERATOR)) << wxT("</accel>");
149 if((PropertyString(PROP_KIND) == ITEM_CHECK) && (PropertyString(PROP_CHECKED) == "1")) {
150 text << wxT("<checked>") << "1" << wxT("</checked>");
151 }
152 // Add the help string
153 text << "<help>" << wxCrafter::CDATA(PropertyString(PROP_HELP)) << "</help>";
154 text << XRCSuffix();
155 }
156 }
157
158 wxString MenuItemWrapper::XRCKind() const
159 {
160 wxString kind = PropertyString(PROP_KIND);
161 if(kind == ITEM_RADIO)
162 return wxT("<radio>1</radio>");
163
164 else if(kind == ITEM_CHECK)
165 return wxT("<checkable>1</checkable>");
166
167 else
168 return wxT("");
169 }
170
171 void MenuItemWrapper::LoadPropertiesFromXRC(const wxXmlNode* node)
172 {
173 // First call the base-class for the standard things
174 wxcWidget::LoadPropertiesFromXRC(node);
175
176 DoLoadXRCProperties(node);
177 }
178
179 void MenuItemWrapper::LoadPropertiesFromwxSmith(const wxXmlNode* node)
180 {
181 // First call the base-class for the standard things
182 wxcWidget::LoadPropertiesFromwxSmith(node);
183
184 DoLoadXRCProperties(node);
185 }
186
187 void MenuItemWrapper::DoLoadXRCProperties(const wxXmlNode* node)
188 {
189 // Cope with separators. This will be a menuitem, but we must set its type
190 wxString classname = XmlUtils::ReadString(node, wxT("class"));
191 if(classname == "separator") {
192 SetPropertyString(PROP_KIND, ITEM_SEPARATOR);
193 return; // Nothing else needed for a separator
194 }
195
196 wxXmlNode* propertynode = XmlUtils::FindFirstByTagName(node, wxT("checkable"));
197 if(propertynode && propertynode->GetNodeContent() == "1") {
198 SetPropertyString(PROP_KIND, ITEM_CHECK);
199 }
200
201 propertynode = XmlUtils::FindFirstByTagName(node, wxT("checked"));
202 if(propertynode && propertynode->GetNodeContent() == "1") {
203 SetPropertyString(PROP_CHECKED, "1");
204 }
205
206 propertynode = XmlUtils::FindFirstByTagName(node, wxT("radio"));
207 if(propertynode && propertynode->GetNodeContent() == "1") {
208 SetPropertyString(PROP_KIND, ITEM_RADIO);
209 }
210
211 propertynode = XmlUtils::FindFirstByTagName(node, "bitmap");
212 if(propertynode) {
213 ImportFromXrc::ProcessBitmapProperty(propertynode, this, PROP_BITMAP_PATH, "wxART_MENU");
214 }
215
216 propertynode = XmlUtils::FindFirstByTagName(node, "accel");
217 if(propertynode) {
218 SetPropertyString(PROP_ACCELERATOR, propertynode->GetNodeContent());
219 }
220
221 propertynode = XmlUtils::FindFirstByTagName(node, "help");
222 if(propertynode) {
223 SetPropertyString(PROP_HELP, propertynode->GetNodeContent());
224 }
225 }
226
227 void MenuItemWrapper::LoadPropertiesFromwxFB(const wxXmlNode* node)
228 {
229 // First call the base-class for the standard things
230 wxcWidget::LoadPropertiesFromwxFB(node);
231
232 // Next cope with separators. This will be a menuitem, but we must set its type
233 wxString classname = XmlUtils::ReadString(node, wxT("class"));
234 if(classname == "separator") {
235 SetPropertyString(PROP_KIND, ITEM_SEPARATOR);
236 return; // Nothing else needed for a separator
237 }
238
239 wxXmlNode* propertynode = XmlUtils::FindNodeByName(node, "property", "kind");
240 if(propertynode) {
241 wxString value = propertynode->GetNodeContent();
242 if(value == "wxITEM_CHECK") {
243 SetPropertyString(PROP_KIND, ITEM_CHECK);
244 } else if(value == "wxITEM_RADIO") {
245 SetPropertyString(PROP_KIND, ITEM_RADIO);
246 } else {
247 SetPropertyString(PROP_KIND, ITEM_NORMAL);
248 }
249 }
250
251 propertynode = XmlUtils::FindNodeByName(node, "property", "bitmap");
252 if(propertynode) {
253 ImportFromwxFB::ProcessBitmapProperty(propertynode->GetNodeContent(), this, PROP_BITMAP_PATH, "wxART_MENU");
254 }
255
256 propertynode = XmlUtils::FindNodeByName(node, "property", "shortcut");
257 if(propertynode) {
258 SetPropertyString(PROP_ACCELERATOR, propertynode->GetNodeContent());
259 }
260
261 propertynode = XmlUtils::FindNodeByName(node, "property", "help");
262 if(propertynode) {
263 SetPropertyString(PROP_HELP, propertynode->GetNodeContent());
264 }
265
266 propertynode = XmlUtils::FindNodeByName(node, "property", wxT("checked"));
267 if(propertynode && propertynode->GetNodeContent() == "1") {
268 SetPropertyString(PROP_CHECKED, "1");
269 }
270 }