| 1 |
///////////////////////////////////////////////////////////////////////////// |
| 2 |
// Name: src/xrc/xh_ribbon.cpp |
| 3 |
// Purpose: XML resource handler for wxRibbon related classes |
| 4 |
// Author: Armel Asselin |
| 5 |
// Created: 2010-04-23 |
| 6 |
// RCS-ID: $Id$ |
| 7 |
// Copyright: (c) 2010 Armel Asselin |
| 8 |
// Licence: wxWindows licence |
| 9 |
///////////////////////////////////////////////////////////////////////////// |
| 10 |
|
| 11 |
// For compilers that support precompilation, includes "wx.h". |
| 12 |
#include "wx/wxprec.h" |
| 13 |
|
| 14 |
#ifdef __BORLANDC__ |
| 15 |
#pragma hdrstop |
| 16 |
#endif |
| 17 |
|
| 18 |
#include "myxh_ribbon.h" |
| 19 |
#include <wx/ribbon/bar.h> |
| 20 |
#include <wx/ribbon/buttonbar.h> |
| 21 |
#include <wx/ribbon/gallery.h> |
| 22 |
#include <wx/ribbon/toolbar.h> |
| 23 |
#include <wx/xml/xml.h> |
| 24 |
|
| 25 |
#include <wx/scopeguard.h> |
| 26 |
|
| 27 |
#ifndef WX_PRECOMP |
| 28 |
#include <wx/menu.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
// Ribbon bars can contain only pages which are usually panels but may contain |
| 32 |
// any wxWindow. |
| 33 |
// |
| 34 |
// Panels are usually for wxRibbonControls but may as well contain any |
| 35 |
// wxWindow. |
| 36 |
// |
| 37 |
// Galleries are wxRibbonControl and simply contain bitmaps with IDs. |
| 38 |
// |
| 39 |
// Button bars are wxRibbonControl and contain buttons (normal/dropdown/mixed), |
| 40 |
// with id/bitmap/label/short help. |
| 41 |
|
| 42 |
wxIMPLEMENT_DYNAMIC_CLASS(MyWxRibbonXmlHandler, wxXmlResourceHandler); |
| 43 |
|
| 44 |
MyWxRibbonXmlHandler::MyWxRibbonXmlHandler() |
| 45 |
: wxXmlResourceHandler() |
| 46 |
, m_isInside(NULL) |
| 47 |
{ |
| 48 |
XRC_ADD_STYLE(wxRIBBON_BAR_SHOW_PAGE_LABELS); |
| 49 |
XRC_ADD_STYLE(wxRIBBON_BAR_SHOW_PAGE_ICONS); |
| 50 |
XRC_ADD_STYLE(wxRIBBON_BAR_FLOW_HORIZONTAL); |
| 51 |
XRC_ADD_STYLE(wxRIBBON_BAR_FLOW_VERTICAL); |
| 52 |
XRC_ADD_STYLE(wxRIBBON_BAR_SHOW_PANEL_EXT_BUTTONS); |
| 53 |
XRC_ADD_STYLE(wxRIBBON_BAR_SHOW_PANEL_MINIMISE_BUTTONS); |
| 54 |
XRC_ADD_STYLE(wxRIBBON_BAR_ALWAYS_SHOW_TABS); |
| 55 |
XRC_ADD_STYLE(wxRIBBON_BAR_DEFAULT_STYLE); |
| 56 |
XRC_ADD_STYLE(wxRIBBON_BAR_FOLDBAR_STYLE); |
| 57 |
} |
| 58 |
|
| 59 |
wxObject* MyWxRibbonXmlHandler::DoCreateResource() |
| 60 |
{ |
| 61 |
if(m_class == wxT("button")) |
| 62 |
return Handle_button(); |
| 63 |
else if(m_class == wxT("tool")) |
| 64 |
return Handle_tool(); |
| 65 |
else if(m_class == wxT("wxRibbonButtonBar")) |
| 66 |
return Handle_buttonbar(); |
| 67 |
else if(m_class == wxT("item")) |
| 68 |
return Handle_galleryitem(); |
| 69 |
else if(m_class == wxT("wxRibbonGallery")) |
| 70 |
return Handle_gallery(); |
| 71 |
else if(m_class == wxT("wxRibbonPanel") || m_class == wxT("panel")) |
| 72 |
return Handle_panel(); |
| 73 |
else if(m_class == wxT("wxRibbonPage") || m_class == wxT("page")) |
| 74 |
return Handle_page(); |
| 75 |
else if(m_class == wxT("wxRibbonBar")) |
| 76 |
return Handle_bar(); |
| 77 |
else if(m_class == wxT("wxRibbonToolBar")) |
| 78 |
return Handle_toolbar(); |
| 79 |
else |
| 80 |
return Handle_control(); |
| 81 |
} |
| 82 |
|
| 83 |
bool MyWxRibbonXmlHandler::CanHandle(wxXmlNode* node) |
| 84 |
{ |
| 85 |
return IsRibbonControl(node) || |
| 86 |
(m_isInside == &wxRibbonButtonBar::ms_classInfo && IsOfClass(node, wxT("button"))) || |
| 87 |
(m_isInside == &wxRibbonToolBar::ms_classInfo && IsOfClass(node, wxT("tool"))) || |
| 88 |
(m_isInside == &wxRibbonBar::ms_classInfo && IsOfClass(node, wxT("page"))) || |
| 89 |
(m_isInside == &wxRibbonPage::ms_classInfo && IsOfClass(node, wxT("panel"))) || |
| 90 |
(m_isInside == &wxRibbonGallery::ms_classInfo && IsOfClass(node, wxT("item"))); |
| 91 |
} |
| 92 |
|
| 93 |
bool MyWxRibbonXmlHandler::IsRibbonControl(wxXmlNode* node) |
| 94 |
{ |
| 95 |
return IsOfClass(node, wxT("wxRibbonBar")) || IsOfClass(node, wxT("wxRibbonButtonBar")) || |
| 96 |
IsOfClass(node, wxT("wxRibbonToolBar")) || IsOfClass(node, wxT("wxRibbonPage")) || |
| 97 |
IsOfClass(node, wxT("wxRibbonPanel")) || IsOfClass(node, wxT("wxRibbonGallery")) || |
| 98 |
IsOfClass(node, wxT("wxRibbonControl")); |
| 99 |
} |
| 100 |
|
| 101 |
void MyWxRibbonXmlHandler::Handle_RibbonArtProvider(wxRibbonControl* control) |
| 102 |
{ |
| 103 |
wxString provider = GetText("art-provider", false); |
| 104 |
|
| 105 |
if(provider == "default" || provider.IsEmpty()) |
| 106 |
control->SetArtProvider(new wxRibbonDefaultArtProvider); |
| 107 |
else if(provider.CmpNoCase("aui") == 0) |
| 108 |
control->SetArtProvider(new wxRibbonAUIArtProvider); |
| 109 |
else if(provider.CmpNoCase("msw") == 0) |
| 110 |
control->SetArtProvider(new wxRibbonMSWArtProvider); |
| 111 |
else |
| 112 |
ReportError("invalid ribbon art provider"); |
| 113 |
} |
| 114 |
|
| 115 |
wxObject* MyWxRibbonXmlHandler::Handle_buttonbar() |
| 116 |
{ |
| 117 |
XRC_MAKE_INSTANCE(buttonBar, wxRibbonButtonBar); |
| 118 |
|
| 119 |
if(!buttonBar->Create(wxDynamicCast(m_parent, wxWindow), GetID(), GetPosition(), GetSize(), GetStyle())) { |
| 120 |
ReportError("could not create ribbon button bar"); |
| 121 |
} else { |
| 122 |
buttonBar->SetName(GetName()); |
| 123 |
const wxClassInfo* const wasInside = m_isInside; |
| 124 |
wxON_BLOCK_EXIT_SET(m_isInside, wasInside); |
| 125 |
m_isInside = &wxRibbonButtonBar::ms_classInfo; |
| 126 |
|
| 127 |
CreateChildren(buttonBar, true); |
| 128 |
|
| 129 |
buttonBar->Realize(); |
| 130 |
} |
| 131 |
|
| 132 |
return buttonBar; |
| 133 |
} |
| 134 |
|
| 135 |
wxObject* MyWxRibbonXmlHandler::Handle_toolbar() |
| 136 |
{ |
| 137 |
XRC_MAKE_INSTANCE(toolbar, wxRibbonToolBar); |
| 138 |
|
| 139 |
if(!toolbar->Create(wxDynamicCast(m_parent, wxWindow), GetID(), GetPosition(), GetSize(), GetStyle())) { |
| 140 |
ReportError("could not create ribbon toolbar"); |
| 141 |
} else { |
| 142 |
int minrows = GetLong("minrows", 1); |
| 143 |
int maxrows = GetLong("maxrows", -1); |
| 144 |
toolbar->SetRows(minrows, maxrows); |
| 145 |
|
| 146 |
toolbar->SetName(GetName()); |
| 147 |
const wxClassInfo* const wasInside = m_isInside; |
| 148 |
wxON_BLOCK_EXIT_SET(m_isInside, wasInside); |
| 149 |
m_isInside = &wxRibbonToolBar::ms_classInfo; |
| 150 |
|
| 151 |
CreateChildren(toolbar, true); |
| 152 |
|
| 153 |
toolbar->Realize(); |
| 154 |
} |
| 155 |
|
| 156 |
return toolbar; |
| 157 |
} |
| 158 |
|
| 159 |
wxObject* MyWxRibbonXmlHandler::Handle_tool() |
| 160 |
{ |
| 161 |
wxRibbonToolBar* buttonBar = wxStaticCast(m_parent, wxRibbonToolBar); |
| 162 |
wxRibbonButtonKind kind = wxRIBBON_BUTTON_NORMAL; |
| 163 |
|
| 164 |
if(GetBool(wxT("hybrid"))) kind = wxRIBBON_BUTTON_HYBRID; |
| 165 |
|
| 166 |
if(GetBool("separator")) { |
| 167 |
buttonBar->AddSeparator(); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
// FIXME: The code below uses wxXmlNode directly but this can't be done |
| 172 |
// in the ribbon library code as it would force it to always link |
| 173 |
// with the xml library. Disable it for now but the real solution |
| 174 |
// would be to virtualize GetChildren() and GetNext() methods via |
| 175 |
// wxXmlResourceHandler, just as we already do for many others. |
| 176 |
// check whether we have dropdown tag inside |
| 177 |
wxMenu* menu = NULL; // menu for drop down items |
| 178 |
wxXmlNode* const nodeDropdown = GetParamNode("dropdown"); |
| 179 |
if(nodeDropdown) { |
| 180 |
if(kind == wxRIBBON_BUTTON_NORMAL) kind = wxRIBBON_BUTTON_DROPDOWN; |
| 181 |
|
| 182 |
// also check for the menu specified inside dropdown (it is |
| 183 |
// optional and may be absent for e.g. dynamically-created |
| 184 |
// menus) |
| 185 |
wxXmlNode* const nodeMenu = nodeDropdown->GetChildren(); |
| 186 |
if(nodeMenu) { |
| 187 |
wxObject* res = CreateResFromNode(nodeMenu, NULL); |
| 188 |
menu = wxDynamicCast(res, wxMenu); |
| 189 |
if(!menu) { ReportError(nodeMenu, "drop-down tool contents can only be a wxMenu"); } |
| 190 |
|
| 191 |
if(nodeMenu->GetNext()) { |
| 192 |
ReportError(nodeMenu->GetNext(), "unexpected extra contents under drop-down tool"); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if(!buttonBar->AddTool(GetID(), GetBitmap("bitmap"), GetBitmap("disabled-bitmap"), GetText("help"), kind)) { |
| 198 |
ReportError("could not create button"); |
| 199 |
} |
| 200 |
|
| 201 |
if(GetBool(wxT("disabled"))) buttonBar->EnableTool(GetID(), false); |
| 202 |
|
| 203 |
return NULL; // nothing to return |
| 204 |
} |
| 205 |
|
| 206 |
wxObject* MyWxRibbonXmlHandler::Handle_button() |
| 207 |
{ |
| 208 |
wxRibbonButtonBar* buttonBar = wxStaticCast(m_parent, wxRibbonButtonBar); |
| 209 |
|
| 210 |
wxRibbonButtonKind kind = wxRIBBON_BUTTON_NORMAL; |
| 211 |
|
| 212 |
if(GetBool(wxT("hybrid"))) kind = wxRIBBON_BUTTON_HYBRID; |
| 213 |
|
| 214 |
// FIXME: The code below uses wxXmlNode directly but this can't be done |
| 215 |
// in the ribbon library code as it would force it to always link |
| 216 |
// with the xml library. Disable it for now but the real solution |
| 217 |
// would be to virtualize GetChildren() and GetNext() methods via |
| 218 |
// wxXmlResourceHandler, just as we already do for many others. |
| 219 |
// check whether we have dropdown tag inside |
| 220 |
wxMenu* menu = NULL; // menu for drop down items |
| 221 |
wxXmlNode* const nodeDropdown = GetParamNode("dropdown"); |
| 222 |
if(nodeDropdown) { |
| 223 |
if(kind == wxRIBBON_BUTTON_NORMAL) kind = wxRIBBON_BUTTON_DROPDOWN; |
| 224 |
|
| 225 |
// also check for the menu specified inside dropdown (it is |
| 226 |
// optional and may be absent for e.g. dynamically-created |
| 227 |
// menus) |
| 228 |
wxXmlNode* const nodeMenu = nodeDropdown->GetChildren(); |
| 229 |
if(nodeMenu) { |
| 230 |
wxObject* res = CreateResFromNode(nodeMenu, NULL); |
| 231 |
menu = wxDynamicCast(res, wxMenu); |
| 232 |
if(!menu) { ReportError(nodeMenu, "drop-down tool contents can only be a wxMenu"); } |
| 233 |
|
| 234 |
if(nodeMenu->GetNext()) { |
| 235 |
ReportError(nodeMenu->GetNext(), "unexpected extra contents under drop-down tool"); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if(!buttonBar->AddButton(GetID(), GetText("label"), GetBitmap("bitmap"), GetBitmap("small-bitmap"), |
| 241 |
GetBitmap("disabled-bitmap"), GetBitmap("small-disabled-bitmap"), kind, GetText("help"))) { |
| 242 |
ReportError("could not create button"); |
| 243 |
} |
| 244 |
|
| 245 |
if(GetBool(wxT("disabled"))) buttonBar->EnableButton(GetID(), false); |
| 246 |
|
| 247 |
return NULL; // nothing to return |
| 248 |
} |
| 249 |
|
| 250 |
wxObject* MyWxRibbonXmlHandler::Handle_control() |
| 251 |
{ |
| 252 |
wxRibbonControl* control = wxDynamicCast(m_instance, wxRibbonControl); |
| 253 |
|
| 254 |
if(!m_instance) |
| 255 |
ReportError("wxRibbonControl must be subclassed"); |
| 256 |
else if(!control) |
| 257 |
ReportError("controls must derive from wxRibbonControl"); |
| 258 |
|
| 259 |
control->Create(wxDynamicCast(m_parent, wxWindow), GetID(), GetPosition(), GetSize(), GetStyle()); |
| 260 |
|
| 261 |
return m_instance; |
| 262 |
} |
| 263 |
|
| 264 |
wxObject* MyWxRibbonXmlHandler::Handle_page() |
| 265 |
{ |
| 266 |
XRC_MAKE_INSTANCE(ribbonPage, wxRibbonPage); |
| 267 |
|
| 268 |
if(!ribbonPage->Create(wxDynamicCast(m_parent, wxRibbonBar), GetID(), GetText("label"), GetBitmap("icon"), |
| 269 |
GetStyle())) { |
| 270 |
ReportError("could not create ribbon page"); |
| 271 |
} else { |
| 272 |
ribbonPage->SetName(GetName()); |
| 273 |
bool selected = GetBool("selected"); |
| 274 |
if(selected) { |
| 275 |
wxRibbonBar* ribbonbar = wxDynamicCast(m_parent, wxRibbonBar); |
| 276 |
ribbonbar->SetActivePage(ribbonPage); |
| 277 |
} |
| 278 |
|
| 279 |
const wxClassInfo* const wasInside = m_isInside; |
| 280 |
wxON_BLOCK_EXIT_SET(m_isInside, wasInside); |
| 281 |
m_isInside = &wxRibbonPage::ms_classInfo; |
| 282 |
|
| 283 |
CreateChildren(ribbonPage); |
| 284 |
|
| 285 |
ribbonPage->Realize(); |
| 286 |
} |
| 287 |
|
| 288 |
return ribbonPage; |
| 289 |
} |
| 290 |
|
| 291 |
wxObject* MyWxRibbonXmlHandler::Handle_gallery() |
| 292 |
{ |
| 293 |
XRC_MAKE_INSTANCE(ribbonGallery, wxRibbonGallery); |
| 294 |
|
| 295 |
if(!ribbonGallery->Create(wxDynamicCast(m_parent, wxWindow), GetID(), GetPosition(), GetSize(), GetStyle())) { |
| 296 |
ReportError("could not create ribbon gallery"); |
| 297 |
} else { |
| 298 |
ribbonGallery->SetName(GetName()); |
| 299 |
const wxClassInfo* const wasInside = m_isInside; |
| 300 |
wxON_BLOCK_EXIT_SET(m_isInside, wasInside); |
| 301 |
m_isInside = &wxRibbonGallery::ms_classInfo; |
| 302 |
|
| 303 |
CreateChildren(ribbonGallery); |
| 304 |
|
| 305 |
ribbonGallery->Realize(); |
| 306 |
} |
| 307 |
|
| 308 |
return ribbonGallery; |
| 309 |
} |
| 310 |
|
| 311 |
wxObject* MyWxRibbonXmlHandler::Handle_galleryitem() |
| 312 |
{ |
| 313 |
wxRibbonGallery* gallery = wxStaticCast(m_parent, wxRibbonGallery); |
| 314 |
wxCHECK(gallery, NULL); |
| 315 |
|
| 316 |
gallery->Append(GetBitmap(), GetID()); |
| 317 |
|
| 318 |
return NULL; // nothing to return |
| 319 |
} |
| 320 |
|
| 321 |
wxObject* MyWxRibbonXmlHandler::Handle_panel() |
| 322 |
{ |
| 323 |
XRC_MAKE_INSTANCE(ribbonPanel, wxRibbonPanel); |
| 324 |
|
| 325 |
if(!ribbonPanel->Create(wxDynamicCast(m_parent, wxWindow), GetID(), GetText("label"), GetBitmap("icon"), |
| 326 |
GetPosition(), GetSize(), GetStyle("style", wxRIBBON_PANEL_DEFAULT_STYLE))) { |
| 327 |
ReportError("could not create ribbon panel"); |
| 328 |
} else { |
| 329 |
ribbonPanel->SetName(GetName()); |
| 330 |
CreateChildren(ribbonPanel); |
| 331 |
|
| 332 |
ribbonPanel->Realize(); |
| 333 |
} |
| 334 |
|
| 335 |
return ribbonPanel; |
| 336 |
} |
| 337 |
|
| 338 |
wxObject* MyWxRibbonXmlHandler::Handle_bar() |
| 339 |
{ |
| 340 |
XRC_MAKE_INSTANCE(ribbonBar, wxRibbonBar); |
| 341 |
|
| 342 |
Handle_RibbonArtProvider(ribbonBar); |
| 343 |
|
| 344 |
if(!ribbonBar->Create(wxDynamicCast(m_parent, wxWindow), GetID(), GetPosition(), GetSize(), |
| 345 |
GetStyle("style", wxRIBBON_BAR_DEFAULT_STYLE))) { |
| 346 |
ReportError("could not create ribbonbar"); |
| 347 |
} else { |
| 348 |
ribbonBar->SetName(GetName()); |
| 349 |
// Currently the art provider style must be explicitly set to the |
| 350 |
// ribbon style too. |
| 351 |
ribbonBar->GetArtProvider()->SetFlags(GetStyle("style", wxRIBBON_BAR_DEFAULT_STYLE)); |
| 352 |
|
| 353 |
const wxClassInfo* const wasInside = m_isInside; |
| 354 |
wxON_BLOCK_EXIT_SET(m_isInside, wasInside); |
| 355 |
m_isInside = &wxRibbonBar::ms_classInfo; |
| 356 |
|
| 357 |
CreateChildren(ribbonBar, true); |
| 358 |
|
| 359 |
ribbonBar->Realize(); |
| 360 |
} |
| 361 |
|
| 362 |
return ribbonBar; |
| 363 |
} |