My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "XMLCommand.h"

NAMESPACE_UPP

////////////////////////////////////////////////////////////////////////////////////

// xml support
void XMLCommand::Xmlize(XmlIO xml)
{
// just custom commands get streamed
// only command string gets streamed, other values are just set
if(xml.IsLoading())
{
enabled = true;
custom = true;
control = NULL;
ctrlSize = Size(-1, -1);
callback.Clear();
}
else
{
ASSERT(custom == true);
}
xml("CommandString", commandString);
}

////////////////////////////////////////////////////////////////////////////////////
// adds a custom command
XMLCommands &XMLCommands::Add(String const &id, String const &cmdStr)
{
if(Has(id))
return *this;
XMLCommand *cmd = new XMLCommand;
cmd->control = NULL;
cmd->enabled = true;
cmd->custom = true;
cmd->callback.Clear();
cmd->commandString = cmdStr;
commands.Add(id, cmd);
return *this;
}

// adds a built-in command with given callback
XMLCommands &XMLCommands::Add(String const &id, Callback cb)
{
bool has = Has(id);
XMLCommand *cmd;
if(has)
cmd = &commands.Get(id);
else
cmd = new XMLCommand;
cmd->control = NULL;
cmd->enabled = true;
cmd->custom = false;
cmd->callback = cb;
if(!has)
commands.Add(id, cmd);
return *this;
}

// adds a control
XMLCommands &XMLCommands::Add(String const &id, Ctrl &ctrl, Size const &size)
{
bool has = Has(id);
XMLCommand *cmd;
if(has)
cmd = &commands.Get(id);
else
cmd = new XMLCommand;
cmd->control = &ctrl;
cmd->ctrlSize = size;
cmd->enabled = true;
cmd->custom = false;
cmd->callback.Clear();
if(!has)
commands.Add(id, cmd);
return *this;
}
XMLCommands &XMLCommands::Add(String const &id, Ctrl &ctrl)
{
return Add(id, ctrl, Size(-1, -1));
}

// adds a custom command, allows enable/disable item
XMLCommands &XMLCommands::Add(bool enabled, String const &id, String const &cmdStr)
{
bool has = Has(id);
XMLCommand *cmd;
if(has)
cmd = &commands.Get(id);
else
cmd = new XMLCommand;
cmd->control = NULL;
cmd->enabled = enabled;
cmd->custom = true;
cmd->callback.Clear();
cmd->commandString = cmdStr;
if(!has)
commands.Add(id, cmd);
return *this;
}

// adds a built-in command with given callback, allows enable/disable item
XMLCommands &XMLCommands::Add(bool enabled, String const &id, Callback cb)
{
bool has = Has(id);
XMLCommand *cmd;
if(has)
cmd = &commands.Get(id);
else
cmd = new XMLCommand;
cmd->control = NULL;
cmd->enabled = enabled;
cmd->custom = false;
cmd->callback = cb;
if(!has)
commands.Add(id, cmd);
return *this;
}

// adds a control, allows enable/disable item
XMLCommands &XMLCommands::Add(bool enabled, String const &id, Ctrl &ctrl, Size const &size)
{
bool has = Has(id);
XMLCommand *cmd;
if(has)
cmd = &commands.Get(id);
else
cmd = new XMLCommand;
cmd->control = &ctrl;
cmd->ctrlSize = size;
cmd->enabled = true;
cmd->custom = false;
cmd->callback.Clear();
if(!has)
commands.Add(id, cmd);
return *this;
}
XMLCommands &XMLCommands::Add(bool enabled, String const &id, Ctrl &ctrl)
{
return Add(enabled, id, ctrl, Size(-1, -1));
}

// get all available command IDs
Vector<String> const &XMLCommands::GetIds(void) const
{
return commands.GetKeys();
}

struct XMLCmdLess
{
bool operator()(String const &a, String const &b) const
{ return ToUpper(a) < ToUpper(b); }
};

// sort items - alphabetically, but first built-in commands, then custom ones
XMLCommands &XMLCommands::Sort(void)
{
Array<XMLCommand> builtIn, custom;
Array<String> builtInIdx, customIdx;
for(int i = 0; i < commands.GetCount(); i++)
{
if(commands[i].GetIsCustom())
{
custom.Add(commands[i]);
customIdx.Add(commands.GetKey(i));
}
else
{
builtIn.Add(commands[i]);
builtInIdx.Add(commands.GetKey(i));
}
}
IndexSort(customIdx, custom, XMLCmdLess());
IndexSort(builtInIdx, builtIn, XMLCmdLess());
commands.Clear();
for(int i = 0; i < builtIn.GetCount(); i++)
commands.Add(builtInIdx[i], builtIn[i]);
for(int i = 0; i < custom.GetCount(); i++)
commands.Add(customIdx[i], custom[i]);

return *this;
}

// xml support
void XMLCommands::Xmlize(XmlIO xml)
{
// just stream CUSTOM commands; embedded ones are defined
// by application and can't be edited
if(xml.IsLoading())
{
// wipe all custom commands currently present
for(int i = commands.GetCount() - 1; i >= 0; i--)
if(commands[i].GetIsCustom())
commands.Remove(i);

// stream in new commands
ArrayMap<String, XMLCommand> newCmds;
xml("commands", newCmds);

// appends new commands to current list
for(int i = 0; i < newCmds.GetCount(); i++)
commands.Add(newCmds.GetKey(i), newCmds[i]);
}
else
{
// extract custom commands from current ones
ArrayMap<String, XMLCommand> custCmds;
for(int i = 0; i < commands.GetCount(); i++)
if(commands[i].GetIsCustom())
custCmds.Add(commands.GetKey(i), commands[i]);

// stream out custom commands
xml("commands", custCmds);
}
}

END_UPP_NAMESPACE

Change log

r4247 by micio on Dec 5, 2011   Diff
Bazaar/XMLMenu : removed a couple of Add()
in commands because of overloading
mismatches
Go to: 
Project members, sign in to write a code review

Older revisions

r4242 by micio on Dec 4, 2011   Diff
Bazaar/XMLMenu : allow custom command
string on custom commands
Fixed a couple of bugs in editor
r4210 by micio on Nov 30, 2011   Diff
Bazaar/XMLMenu : avoid adding
duplicated commands
r4070 by micio on Oct 20, 2011   Diff
sandbox/XMLMenu moved to bazaar
All revisions of this file

File info

Size: 5156 bytes, 218 lines
Powered by Google Project Hosting