1// Aseprite
2// Copyright (C) 2001-2017 David Capello
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifndef APP_WIDGET_LOADER_H_INCLUDED
8#define APP_WIDGET_LOADER_H_INCLUDED
9#pragma once
10
11#include "app/i18n/xml_translator.h"
12#include "app/widget_type_mismatch.h"
13
14#include <map>
15#include <string>
16
17class TiXmlElement;
18
19namespace ui {
20 class Widget;
21 class TooltipManager;
22}
23
24namespace app {
25
26 class WidgetLoader {
27 public:
28 // Interface used to create customized widgets.
29 class IWidgetTypeCreator {
30 public:
31 virtual ~IWidgetTypeCreator() { }
32 virtual void dispose() = 0;
33 virtual ui::Widget* createWidgetFromXml(const TiXmlElement* xmlElem) = 0;
34 };
35
36 WidgetLoader();
37 ~WidgetLoader();
38
39 // Adds a new widget type that can be referenced in the .xml file
40 // with an XML element. The "tagName" is the same name as in the
41 // .xml should appear as <tagName>...</tagName>
42 //
43 // The "creator" will not be deleted automatically at the
44 // WidgetLoader dtor.
45 void addWidgetType(const char* tagName, IWidgetTypeCreator* creator);
46
47 // Loads the specified widget from an .xml file.
48 ui::Widget* loadWidget(const char* fileName, const char* widgetId, ui::Widget* widget = NULL);
49
50 template<class T>
51 T* loadWidgetT(const char* fileName, const char* widgetId, T* widget = NULL) {
52 T* specificWidget = dynamic_cast<T*>(loadWidget(fileName, widgetId, widget));
53 if (!specificWidget)
54 throw WidgetTypeMismatch(widgetId);
55
56 return specificWidget;
57 }
58
59 private:
60 ui::Widget* loadWidgetFromXmlFile(
61 const std::string& xmlFilename,
62 const std::string& widgetId,
63 ui::Widget* widget);
64
65 ui::Widget* convertXmlElementToWidget(const TiXmlElement* elem, ui::Widget* root, ui::Widget* parent, ui::Widget* widget);
66 void fillWidgetWithXmlElementAttributes(const TiXmlElement* elem, ui::Widget* root, ui::Widget* widget);
67 void fillWidgetWithXmlElementAttributesWithChildren(const TiXmlElement* elem, ui::Widget* root, ui::Widget* widget);
68
69 typedef std::map<std::string, IWidgetTypeCreator*> TypeCreatorsMap;
70
71 TypeCreatorsMap m_typeCreators;
72 ui::TooltipManager* m_tooltipManager;
73 XmlTranslator m_xmlTranslator;
74 };
75
76} // namespace app
77
78#endif
79