Debian Codegraph @

wxcrafter/xrc_handlers/myxh_auitoolb.cpp

raw
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xh_toolb.cpp
3 // Purpose: XRC resource for wxAuiToolBar
4 // Author: Vaclav Slavik
5 // Created: 2000/08/11
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "myxh_auitoolb.h"
13 #include "wx/aui/auibar.h"
14 #include "wx/frame.h"
15 #include "wx/log.h"
16 #include "wx/menu.h"
17 #include "wx/pen.h"
18 #include "wx/sizer.h"
19 #include "wx/wxprec.h"
20 #include <wx/xml/xml.h>
21
22 MyWxAuiToolBarXmlHandler::MyWxAuiToolBarXmlHandler()
23 : wxXmlResourceHandler()
24 , m_isInside(false)
25 , m_toolbar(NULL)
26 {
27 XRC_ADD_STYLE(wxAUI_TB_TEXT);
28 XRC_ADD_STYLE(wxAUI_TB_NO_TOOLTIPS);
29 XRC_ADD_STYLE(wxAUI_TB_NO_AUTORESIZE);
30 XRC_ADD_STYLE(wxAUI_TB_GRIPPER);
31 XRC_ADD_STYLE(wxAUI_TB_OVERFLOW);
32 XRC_ADD_STYLE(wxAUI_TB_VERTICAL);
33 XRC_ADD_STYLE(wxAUI_TB_HORZ_LAYOUT);
34 XRC_ADD_STYLE(wxAUI_TB_HORZ_TEXT);
35 XRC_ADD_STYLE(wxAUI_TB_DEFAULT_STYLE);
36 XRC_ADD_STYLE(wxAUI_TB_PLAIN_BACKGROUND);
37 AddWindowStyles();
38 }
39
40 wxObject* MyWxAuiToolBarXmlHandler::DoCreateResource()
41 {
42 if(m_class == wxT("tool")) {
43 if(!m_toolbar) { return NULL; }
44
45 wxItemKind kind = wxITEM_NORMAL;
46 if(GetBool(wxT("radio"))) kind = wxITEM_RADIO;
47
48 if(GetBool(wxT("toggle"))) { kind = wxITEM_CHECK; }
49
50 m_toolbar->AddTool(GetID(), GetText(wxT("label")), GetBitmap(wxT("bitmap"), wxART_TOOLBAR, m_toolSize),
51 GetText(wxT("longhelp")), kind);
52
53 if(GetBool(wxT("disabled"))) m_toolbar->EnableTool(GetID(), false);
54
55 if(GetBool(wxS("checked"))) { m_toolbar->ToggleTool(GetID(), true); }
56
57 wxAuiToolBarItem* tool = m_toolbar->FindTool(GetID());
58 wxXmlNode* const nodeDropdown = GetParamNode("dropdown"); // The new official wx xh_auitoolb.cpp uses "dropdown"
59 if(tool && (GetBool("hasdropdown") || (nodeDropdown != NULL))) {
60 // From the official wx3.1 handler
61 // also check for the menu specified inside dropdown (it is
62 // optional and may be absent for e.g. dynamically-created menus)
63 wxMenu* menu = NULL; // menu for drop down items
64 wxXmlNode* const nodeMenu = nodeDropdown->GetChildren();
65 if(nodeMenu) {
66 wxObject* res = CreateResFromNode(nodeMenu, NULL);
67 menu = wxDynamicCast(res, wxMenu);
68 }
69 tool->SetHasDropDown(true);
70 if(menu) { tool->SetUserData(m_menuHandler.RegisterMenu(m_toolbar, GetID(), menu)); }
71 }
72 return m_toolbar; // must return non-NULL
73 }
74
75 else if(m_class == wxS("label")) {
76 if(m_toolbar) { m_toolbar->AddLabel(GetID(), GetText(wxS("label")), GetLong(wxS("width"), -1l)); }
77
78 return m_toolbar;
79 }
80
81 else if(m_class == wxS("space")) {
82 if(m_toolbar) {
83 // This may be a stretch spacer (the default) or a non-stretch one which will have the attribute 'width'
84 if(HasParam("width")) {
85 m_toolbar->AddSpacer(GetLong("width"));
86 } else {
87 m_toolbar->AddStretchSpacer(GetLong(wxS("proportion"), 1l));
88 }
89 }
90
91 return m_toolbar;
92 }
93
94 else if(m_class == wxT("separator")) {
95 if(m_toolbar) { m_toolbar->AddSeparator(); }
96
97 return m_toolbar;
98 }
99
100 else { /*<object class="wxAuiToolBar">*/
101 int style = GetStyle(wxT("style"), wxAUI_TB_DEFAULT_STYLE);
102 wxAuiToolBar* toolbar = new wxAuiToolBar(m_parentAsWindow, GetID(), GetPosition(), GetSize(), style);
103 toolbar->SetName(GetName());
104 SetupWindow(toolbar);
105
106 m_toolSize = GetSize(wxT("bitmapsize"));
107 if(!(m_toolSize == wxDefaultSize)) toolbar->SetToolBitmapSize(m_toolSize);
108 wxSize margins = GetSize(wxT("margins"));
109 if(!(margins == wxDefaultSize)) toolbar->SetMargins(margins.x, margins.y);
110 long packing = GetLong(wxT("packing"), -1);
111 if(packing != -1) toolbar->SetToolPacking(packing);
112 long separation = GetLong(wxT("separation"), -1);
113 if(separation != -1) toolbar->SetToolSeparation(separation);
114
115 wxXmlNode* children_node = GetParamNode(wxT("object"));
116 if(!children_node) children_node = GetParamNode(wxT("object_ref"));
117
118 if(children_node == NULL) return toolbar;
119
120 m_isInside = true;
121 m_toolbar = toolbar;
122
123 wxXmlNode* n = children_node;
124
125 while(n) {
126 if((n->GetType() == wxXML_ELEMENT_NODE) &&
127 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref"))) {
128 wxObject* created = CreateResFromNode(n, toolbar, NULL);
129 wxControl* control = wxDynamicCast(created, wxControl);
130 if(!IsOfClass(n, wxT("tool")) && !IsOfClass(n, wxT("label")) && !IsOfClass(n, wxT("separator")) &&
131 !IsOfClass(n, wxT("space")) && // == AddStretchSpacer()
132 control != NULL)
133 toolbar->AddControl(control);
134 }
135 n = n->GetNext();
136 }
137
138 m_isInside = false;
139 m_toolbar = NULL;
140
141 toolbar->Realize();
142 return toolbar;
143 }
144 }
145
146 bool MyWxAuiToolBarXmlHandler::CanHandle(wxXmlNode* node)
147 {
148 return ((!m_isInside && IsOfClass(node, wxT("wxAuiToolBar"))) || (m_isInside && IsOfClass(node, wxT("tool"))) ||
149 (m_isInside && IsOfClass(node, wxT("label"))) || (m_isInside && IsOfClass(node, wxT("space"))) ||
150 (m_isInside && IsOfClass(node, wxT("separator"))));
151 }
152
153 // -------------------------- From the official wx3.1 handler ----------------------
154 void MyWxAuiToolBarXmlHandler::MenuHandler::OnDropDown(wxAuiToolBarEvent& event)
155 {
156 if(event.IsDropDownClicked()) {
157 wxAuiToolBar* toobar = wxDynamicCast(event.GetEventObject(), wxAuiToolBar);
158 if(toobar != NULL) {
159 wxAuiToolBarItem* item = toobar->FindTool(event.GetId());
160 if(item != NULL) {
161 wxMenu* const menu = m_menus[item->GetUserData()];
162 if(menu != NULL) {
163 wxPoint pt = event.GetItemRect().GetBottomLeft();
164 pt.y++;
165 toobar->PopupMenu(menu, pt);
166 }
167 }
168 }
169 } else {
170 event.Skip();
171 }
172 }
173
174 unsigned MyWxAuiToolBarXmlHandler::MenuHandler::RegisterMenu(wxAuiToolBar* toolbar, int id, wxMenu* menu)
175 {
176 m_menus.push_back(menu);
177 toolbar->Bind(wxEVT_COMMAND_AUITOOLBAR_TOOL_DROPDOWN, &MyWxAuiToolBarXmlHandler::MenuHandler::OnDropDown, this, id);
178
179 return m_menus.size() - 1;
180 }