1 #ifndef _XMLCommand_h_
2 #define _XMLCommand_h_
3 
4 #include <CtrlCore/CtrlCore.h>
5 
6 NAMESPACE_UPP
7 
8 ////////////////////////////////////////////////////////////////////////////////////
9 // a single command, i.e. available entry for menu and bars
10 class XMLToolBar;
11 class XMLCommand : DeepCopyOption<XMLCommand>
12 {
13 	friend class XMLCommands;
14 	private:
15 		// embedded control, if any
16 		Ptr<Ctrl> control;
17 		Size ctrlSize;
18 
19 		// associated callback, if any
20 		Function<void()> callback;
21 
22 		// generated menu, if any
23 		Function<void(XMLToolBar &)> menuCallback;
24 
25 		// a plece to store the generated menu toolbar
26 		// it MUJST be persistent, as system require it after generation
27 		One<XMLToolBar> menuTb;
28 
29 		// enabled flag
30 		bool enabled;
31 
32 		// custom command flag
33 		bool custom;
34 
35 		// custom command string to be sent
36 		// to custom command handler
37 		String commandString;
38 
39 	public:
40 		// constructor
XMLCommand()41 		XMLCommand() {}
42 
43 		// copy constructor
44 		XMLCommand(XMLCommand rval_ c);
45 		XMLCommand(XMLCommand const &c, int);
46 
GetCtrl(void)47 		Ctrl *GetCtrl(void) const										{ return control;		}
GetCtrlSize(void)48 		Size const &GetCtrlSize(void) const								{ return ctrlSize;		}
GetCallback(void)49 		Function<void()> const &GetCallback(void) const					{ return callback;		}
GetMenuCallback(void)50 		Function<void(XMLToolBar &)> const &GetMenuCallback(void) const	{ return menuCallback;	}
51 		XMLToolBar const &GetMenuTb(void);
52 
GetIsEnabled(void)53 		bool GetIsEnabled(void) const									{ return enabled;		}
GetIsCustom(void)54 		bool GetIsCustom(void) const									{ return custom;		}
55 
GetCommandString(void)56 		String const &GetCommandString(void) const						{ return commandString; }
SetCommandString(String const & s)57 		XMLCommand &SetCommandString(String const &s)					{ commandString = s; return *this; }
58 
59 		bool operator==(XMLCommand &other) const;
60 
61 		// xml support
62 		void Xmlize(XmlIO xml);
63 };
64 
65 ////////////////////////////////////////////////////////////////////////////////////
66 // an array of available commands
67 class XMLCommands : DeepCopyOption<XMLCommands>
68 {
69 
70 	private:
71 		ArrayMap<String, XMLCommand> commands;
72 
73 	public:
74 		rval_default(XMLCommands);
75 
76 		// default constructor
XMLCommands()77 		XMLCommands() {}
78 
79 		// copy constructor
XMLCommands(XMLCommands const & cmds,int dummy)80 		XMLCommands(XMLCommands const &cmds, int dummy) : commands(cmds.commands, 0) {}
81 
82 		// adds a custom command
83 		XMLCommands &Add(String const &id, String const &cmd);
Add(const char * id,const char * cmd)84 		XMLCommands &Add(const char *id, const char *cmd)
85 			{ return Add(String(id), String(cmd)); }
86 
87 		// adds a built-in command with given callback
88 		XMLCommands &Add(String const &id, Function<void()> cb);
Add(const char * id,Function<void ()> cb)89 		XMLCommands &Add(const char *id, Function<void()> cb)
90 			{ return Add(String(id), cb); }
91 
92 		// adds a generated submenu "command"
93 		XMLCommands &Add(String const &id, Function<void(XMLToolBar &)> mc);
Add(const char * id,Function<void (XMLToolBar &)> mc)94 		XMLCommands &Add(const char *id, Function<void(XMLToolBar &)> mc)
95 			{ return Add(String(id), mc); }
96 
97 		// adds a control
98 		XMLCommands &Add(String const &id, Ctrl &ctrl);
Add(const char * id,Ctrl & ctrl)99 		XMLCommands &Add(const char *id, Ctrl &ctrl)
100 			{ return Add(String(id), ctrl); }
101 		XMLCommands &Add(String const &id, Ctrl &ctrl, Size const &size);
Add(const char * id,Ctrl & ctrl,Size const & size)102 		XMLCommands &Add(const char *id, Ctrl &ctrl, Size const &size)
103 			{ return Add(String(id), ctrl, size); }
104 
105 		// adds a custom command, allows enable/disable item
106 		XMLCommands &Add(bool enabled, String const &id, String const &cmd);
Add(bool enabled,const char * id,String const & cmd)107 		XMLCommands &Add(bool enabled, const char *id, String const &cmd)
108 			{ return Add(enabled, String(id), cmd); }
Add(bool enabled,String const & id,const char * cmd)109 		XMLCommands &Add(bool enabled, String const &id, const char *cmd)
110 			{ return Add(enabled, id, String(cmd)); }
111 
112 		// adds a built-in command with given callback, allows enable/disable item
113 		XMLCommands &Add(bool enabled, String const &id, Function<void()> cb);
Add(bool enabled,const char * id,Function<void ()> cb)114 		XMLCommands &Add(bool enabled, const char *id, Function<void()> cb)
115 			{ return Add(enabled, String(id), cb); }
116 
117 		// adds a generated submenu "command", allows enable/disable item
118 		XMLCommands &Add(bool enabled, String const &id, Function<void(XMLToolBar &)> mc);
Add(bool enabled,const char * id,Function<void (XMLToolBar &)> mc)119 		XMLCommands &Add(bool enabled, const char *id, Function<void(XMLToolBar &)> mc)
120 			{ return Add(enabled, String(id), mc); }
121 
122 		// adds a control, allows enable/disable item
123 		XMLCommands &Add(bool enabled, String const &id, Ctrl &ctrl);
Add(bool enabled,const char * id,Ctrl & ctrl)124 		XMLCommands &Add(bool enabled, const char *id, Ctrl &ctrl)
125 			{ return Add(enabled, String(id), ctrl); }
126 
127 		XMLCommands &Add(bool enabled, String const &id, Ctrl &ctrl, Size const &size);
Add(bool enabled,const char * id,Ctrl & ctrl,Size const & size)128 		XMLCommands &Add(bool enabled, const char *id, Ctrl &ctrl, Size const &size)
129 			{ return Add(enabled, String(id), ctrl, size); }
130 
131 		// removes a command
Remove(int idx)132 		XMLCommands &Remove(int idx) { commands.Remove(idx); return *this; }
133 
134 		// get all available command IDs
135 		Vector<String> const &GetIds(void) const;
136 
137 		// get a command for a given id
Get(String const & id)138 		XMLCommand const &Get(String const &id) const { return commands.Get(ToUpper(id)); }
Get(String const & id)139 		XMLCommand &Get(String const &id) { return commands.Get(ToUpper(id)); }
140 
141 		// find a command given its name
Find(String const & id)142 		int Find(String const &id) const { return commands.Find(ToUpper(id)); }
143 
144 		// sets the commands by a callback
145 		void Set(Function<void(XMLCommands &)> commands);
146 
147 		// checks wether a command is present given its id
Has(String const & id)148 		bool Has(String const &id) const { return commands.Find(ToUpper(id)) >= 0; }
149 
150 		// array access
151 		XMLCommand const &operator[](int idx) const { return commands[idx]; }
152 		XMLCommand &operator[](int idx) { return commands[idx]; }
153 
154 		// sort items - alphabetically, but first built-in commands, then custom ones
155 		XMLCommands &Sort(void);
156 
157 		// xml support
158 		void Xmlize(XmlIO xml);
159 };
160 
161 END_UPP_NAMESPACE
162 
163 #endif
164