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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include "XMLMenuEditor.h"

#include <plugin/bmp/bmp.h>

NAMESPACE_UPP

// colored Display
class ColDisp : public Display
{
Color color;
public :
ColDisp(Color const &c) { color = c; }
virtual void Paint(Draw& w, const Rect& r, const Value& q, Color ink, Color paper, dword style) const
{ Display::Paint(w, r, q, color, paper, style); }
};
static ColDisp RedDisp(Red());

////////////////////////////////////////////////////////////////////////////////
// constructor
XMLBarEditor::XMLBarEditor()
{
bar = NULL;
curIcon = Null;
itemSize = itemPane.GetLayoutSize();
CtrlLayout(itemPane);
Add(vertSplitter);
vertSplitter.Vert(barTree, itemPane);

// drag'n drop
barTree.WhenDropInsert = THISBACK(dropInsertCb);
barTree.WhenDrag = THISBACK(dragCb);
barTree.WhenSel = THISBACK(itemSelCb);
barTree.WhenBar = THISBACK(treeContextCb);

itemPane.cmdId <<= THISBACK(fieldsModCb);
itemPane.label <<= THISBACK(fieldsModCb);
itemPane.tooltip <<= THISBACK(fieldsModCb);

itemPane.icon <<= THISBACK(imageSelCb);

itemPane.Disable();
}

// gets minimum size of bar editor
Size XMLBarEditor::GetMinSize(void)
{
return Size(itemSize.cx, itemSize.cy * 2);
}

// layouts control
void XMLBarEditor::Layout(void)
{
int h = GetSize().cy;
if(h)
vertSplitter.SetPos(10000 * (h - itemSize.cy) / h);
}

// refresh current bar
void XMLBarEditor::RefreshBar(int treeRoot, XMLToolBar *subBar)
{
if(!subBar)
subBar = bar;
if(!subBar)
return;
subBar->items.Clear();
for(int iChild = 0; iChild < barTree.GetChildCount(treeRoot); iChild++)
{
int iNode = barTree.GetChild(treeRoot, iChild);
XMLToolBarItem const &item = ValueTo<XMLToolBarItem>(barTree.Get(iNode));
subBar->items.Add(new XMLToolBarItem(item, 0));
if(barTree.GetChildCount(iNode))
{
subBar->items.Top().subMenu = new XMLToolBar;
RefreshBar(iNode, ~subBar->items.Top().subMenu);
}
else
subBar->items.Top().subMenu.Clear();
}
}

// sets bar being edited
void XMLBarEditor::SetBar(XMLToolBar *_bar)
{
// if changing bar, update current one if not null
// before changing it
if(bar && bar != _bar)
RefreshBar();

bar = _bar;
barTree.Clear();
if(!bar)
return;
buildTree(0, bar);
barTree.SetRoot(Null, bar->GetName());
barTree.OpenDeep(0);
}

// builds bar tree
void XMLBarEditor::buildTree(int root, XMLToolBar const *bar)
{
Array<XMLToolBarItem> const &items = bar->GetItems();
for(int iItem = 0; iItem < items.GetCount(); iItem++)
{
XMLToolBarItem const &item = items[iItem];
TreeCtrl::Node node(item.GetIcon(), RawDeepToValue(item), item.GetLabel());
int iNode = barTree.Add(root, node);
if(items[iItem].IsSubMenu())
buildTree(iNode, &items[iItem].GetSubMenu());
}
}

// dragging element
void XMLBarEditor::dragCb(void)
{
if(barTree.DoDragAndDrop(InternalClip(barTree, "mytreedrag"), barTree.GetDragSample()) == DND_MOVE)
barTree.RemoveSelection();
}

// dropping between elements (inserts between)
void XMLBarEditor::dropInsertCb(int parent, int ii, PasteClip& d)
{
barTree.AdjustAction(parent, d);
if(AcceptInternal<TreeCtrl>(d, "mytreedrag"))
{
barTree.InsertDrop(parent, ii, d);
barTree.SetFocus();
return;
}
if(AcceptText(d))
{
barTree.SetCursor(barTree.Insert(parent, ii, Image(), GetString(d)));
barTree.SetFocus();
return;
}
}

// item selection callback
void XMLBarEditor::itemSelCb()
{
// get selected node
int i = barTree.GetCursor();

// if none, just disable input fields and return
if(i < 0)
{
itemPane.Disable();
return;
}

// enable input pane
itemPane.Enable();

// enable label and tooltip fields
itemPane.label.Enable();

// disable command field, it'll be filled
// by command bar anyways
itemPane.cmdId.Disable();

// if not the root, treat as an item
// otherwise allow just to edit bar name
if(i > 0)
{
itemPane.icon.Enable();
itemPane.tooltip.Enable();

// fetch node data
XMLToolBarItem const &item = ValueTo<XMLToolBarItem>(barTree.Get(i));

bool hasChilds = barTree.GetChildCount(i);

// fill fields
if(hasChilds)
itemPane.cmdId.Clear();
else
itemPane.cmdId = item.GetId();
itemPane.labelName = t_("Label :");
itemPane.label = item.GetLabel();
itemPane.tooltip = item.GetTooltip();
itemPane.icon.SetImage(item.GetIcon());
curIcon = item.GetIcon();
}
else
{
itemPane.icon.Disable();
itemPane.tooltip.Disable();
itemPane.cmdId.Clear();
itemPane.labelName = t_("Bar name :");
itemPane.label = String(barTree.GetValue(0));
itemPane.tooltip.Clear();
itemPane.icon.SetImage(Null);
curIcon = Null;
}
}

// fields modified callback
void XMLBarEditor::fieldsModCb(void)
{
// get selected node
int i = barTree.GetCursor();
if( i < 0 || bar == 0)
return;
if(i == 0)
{
bar->SetName(itemPane.label);
barTree.SetRoot(Null, itemPane.label);
return;
}

XMLToolBarItem item(ValueTo<XMLToolBarItem>(barTree.Get(i)), 0);
item.label = itemPane.label;
item.commandId = itemPane.cmdId;
item.tooltip = itemPane.tooltip;
item.icon = curIcon;
TreeCtrl::Node node(item.GetIcon(), RawDeepToValue(item), item.GetLabel());
barTree.SetNode(i, node);
}

// image selection callback
void XMLBarEditor::imageSelCb(void)
{
static String lastPath = "";

// opens a file selector, allows selection of some
// kind of image formats
FileSel fs;
fs.ActiveDir(lastPath);
fs.Type(t_("Image files"), "*.png,*.jpg,*.ico,*.bmp");
if(!fs.ExecuteOpen(t_("Select icon file")))
return;
String path = fs.Get();
lastPath = GetFileFolder(path);
String ext = ToUpper(GetFileExt(path));
Image img;
if(ext != ".ICO")
img = StreamRaster::LoadFileAny(path);
else
{
String data = LoadFile(path);
Vector<Image> imgVec;
try
{
imgVec = ReadIcon(data);
}
catch(...)
{
}
if(imgVec.GetCount())
img = imgVec[0];
else
img = Null;
}
curIcon = img;
itemPane.icon.SetImage(img);
fieldsModCb();
}

// barTree context menu
void XMLBarEditor::treeContextCb(Bar &b)
{
// if no toolbar selected in, no menu
if(!bar)
return;

int i = barTree.GetCursor();
if(i < 0)
return;
if(i > 0)
{
b.Add(t_("Insert new item before current"), THISBACK1(treeContextAddCb, 1));
b.Add(t_("Insert new item after current"), THISBACK1(treeContextAddCb, 2));
}
if(barTree.GetChildCount(i))
{
b.Add(t_("Insert new child item at top"), THISBACK1(treeContextAddCb, 3));
b.Add(t_("Append new child item at bottom"), THISBACK1(treeContextAddCb, 4));
}
else
b.Add(t_("Insert new child item"), THISBACK1(treeContextAddCb, 4));
if(i > 0)
b.Add(t_("Remove item"), THISBACK(treeContextRemoveCb));
}

void XMLBarEditor::treeContextAddCb(int mode)
{
int id = barTree.GetCursor();
int parentId = 0;
int childIdx;
int newId = 0;
Value v;
CreateRawValue<XMLToolBarItem>(v);
switch(mode)
{
case 1:
parentId = barTree.GetParent(id);
childIdx = barTree.GetChildIndex(parentId, id);
newId = barTree.Insert(parentId, childIdx, Null, v, "");
break;

case 2:
parentId = barTree.GetParent(id);
childIdx = barTree.GetChildIndex(parentId, id);
newId = barTree.Insert(parentId, childIdx + 1, Null, v, "");
break;

case 3:
newId = barTree.Insert(id, 0, Null, v, "");
break;

case 4:
newId = barTree.Add(id, Null, v, "");
break;

default:
NEVER();
}
RefreshBar();
barTree.SetCursor(newId);
itemPane.label.SetFocus();
}

void XMLBarEditor::treeContextRemoveCb(void)
{
int id = barTree.GetCursor();
if(id > 0)
{
int line = barTree.GetCursorLine();
barTree.Remove(id);
RefreshBar();
barTree.SetCursorLine(line);
itemPane.label.SetFocus();
}
}

////////////////////////////////////////////////////////////////////////////////
// constructor
XMLBarsEditor::XMLBarsEditor()
{
// get toolbar/menu selector pane sizes, it's width will be fixed too
selectorSize = barListPane.GetLayoutSize();

CtrlLayout(barListPane);

Add(horzSplitter.SizePos());
horzSplitter.Horz(barListPane, barEditor);

barListPane.barList.WhenSel << THISBACK(barSelCb);
barListPane.barList.WhenBar << THISBACK(barContextCb);
}

// gets minimum size of bar editor
Size XMLBarsEditor::GetMinSize(void)
{
return Size(
selectorSize.cx + barEditor.GetMinSize().cx,
max(selectorSize.cy, barEditor.GetMinSize().cy)
);
}

// adjust layout on win changes
void XMLBarsEditor::Layout(void)
{
if(!GetSize().cx)
return;
horzSplitter.SetPos(10000 * selectorSize.cx / GetSize().cx);
}

// set title
void XMLBarsEditor::SetTitle(const char *s)
{
barListPane.title = s;
}

// sets the local copy of toolbars
void XMLBarsEditor::SetToolBars(XMLToolBars const &tb)
{
toolBars <<= tb;
barListPane.barList.Clear();
for(int iBar = 0; iBar < toolBars.GetCount(); iBar++)
barListPane.barList.Add(toolBars[iBar].GetName());

// select first item
if(barListPane.barList.GetCount())
barListPane.barList.SetCursor(0);
}

// gets the local copy of toolbars
XMLToolBars &XMLBarsEditor::GetToolBars(void)
{
barEditor.RefreshBar();
for(int i = 0; i < toolBars.GetCount(); i++)
toolBars.SetKey(i, toolBars[i].GetName());
return toolBars;
}

// bar selection callback
void XMLBarsEditor::barSelCb(void)
{

int idx = barListPane.barList.GetCursor();
if(idx < 0)
barEditor.SetBar(NULL);
else
barEditor.SetBar(&toolBars[idx]);
}

// query if a command is in use by a toolbar
bool XMLBarsEditor::IsUsingCommand(String const &cmdId) const
{
for(int iBar = 0; iBar < toolBars.GetCount(); iBar++)
{
Array<XMLToolBarItem> const &items = toolBars[iBar].GetItems();
for(int iItem = 0; iItem < items.GetCount(); iItem++)
if(items[iItem].GetId() == cmdId)
return true;
}
return false;
}

// bar list context menu
void XMLBarsEditor::barContextCb(Bar &bar)
{
// get current selection
bar.Add(t_("Add new bar"), THISBACK(barContextAddCb));
int i = barListPane.barList.GetCursor();
if(i < 0)
return;
String barName = barListPane.barList.Get(i);
bar.Add(Format(t_("Remove bar '%s'"), barName), THISBACK(barContextRemoveCb));
}

void XMLBarsEditor::barContextAddCb(void)
{
XMLBarAdd add(toolBars);
int res = add.RunAppModal();
add.Close();
if(res == IDOK)
{
toolBars.Add(~add.barName, XMLToolBar());
barListPane.barList.Add(~add.barName);
barListPane.barList.SetCursor(barListPane.barList.GetCount() - 1);
}
}

void XMLBarsEditor::barContextRemoveCb(void)
{
int i = barListPane.barList.GetCursor();
if(i < 0)
return;

// the double SetBar(NULL) is needed because, when deleting
// row from barList, the WhenSel event is triggered and app
// try to refresh a deleted bar, otherwise
barEditor.SetBar(NULL);
barListPane.barList.Remove(i);
barEditor.SetBar(NULL);
toolBars.Remove(i);

if(barListPane.barList.GetCount() > i)
barListPane.barList.SetCursor(i);
else if(barListPane.barList.GetCount())
barListPane.barList.SetCursor(barListPane.barList.GetCount()-1);
}

////////////////////////////////////////////////////////////////////////////////
void XMLMenuEditor::cancelCb(void)
{
Break(IDCANCEL);
Close();
}

void XMLMenuEditor::okCb(void)
{
// updates edited toolbars
if(iFace)
{
iFace->SetCommands(commands);
iFace->SetMenuBars(menusEditor.GetToolBars());
iFace->SetToolBars(barsEditor.GetToolBars());
}
Break(IDOK);
Close();
}

XMLMenuEditor::XMLMenuEditor(XMLMenuInterface *_iFace) : iFace(_iFace)
{
CtrlLayout(*this);

// setup ok and cancel handlers
okButton.Ok() <<= THISBACK(okCb);
cancelButton.Cancel() <<= THISBACK(cancelCb);

// get command pane size, that one will be fixed in width
cmdSize = cmdPane.GetLayoutSize();

// gets menu/bar editor minimum sizes
editorSize = menusEditor.GetMinSize();

// now calculate tabctrl minimum sizes
int tx = editorSize.cx;
int ty = cmdSize.cy;
tabCtrlSize = Size(tx, ty);

// gets button vertical neede size
buttonVertSize = GetLayoutSize().cy - horzSplitter.GetSize().cy;

// adds items to horz splitter
horzSplitter.Horz(cmdPane, tabCtrl);

// adds items to tabctrl
tabCtrl.Add(menusEditor.SizePos(), t_("Menus"));
tabCtrl.Add(barsEditor.SizePos(), t_("ToolBars"));

// adds layout to all ctrls
CtrlLayout(cmdPane);

// set title for selector panes
menusEditor.SetTitle(t_("Available menus"));
barsEditor.SetTitle(t_("Available toolbars"));

// adjust window width to accomodate at least itemPane and cmdPane
minWidth = cmdSize.cx + tabCtrlSize.cx;

// adjust window height to accomodate cmdPane
minHeight = cmdSize.cy;

SetMinSize(Size(minWidth, minHeight));

int w = max(GetSize().cx, minWidth);
int h = max(GetSize().cy - buttonVertSize, minHeight);
int wr = w;
int hr = h + buttonVertSize;
SetRect(GetRect().left, GetRect().top, wr, hr);

Sizeable();

// reads the commands and put them into list
commands <<= iFace->GetCommands();
FillCmdList();

// sets toolbars in bars editors
menusEditor.SetToolBars(iFace->GetMenuBars());
barsEditor.SetToolBars(iFace->GetToolBars());

// setup handlers for command pane
cmdPane.commandList.WhenLeftDouble << THISBACK(cmdDblClickCb);
cmdPane.commandList.WhenBar << THISBACK(cmdContextCb);
}

XMLMenuEditor::~XMLMenuEditor()
{
}

// fills (or updates) command list
void XMLMenuEditor::FillCmdList(void)
{
commands.Sort();
Vector<String> const &ids = commands.GetIds();
cmdPane.commandList.Clear();
for(int i = 0; i < ids.GetCount(); i++)
{
// built in commands (un-modifiable) are in red color
// custom ones in normal color
XMLCommand const &cmd = commands[i];
if(cmd.GetIsCustom())
cmdPane.commandList.Add(ids[i]);
else
cmdPane.commandList.Add(ids[i], RedDisp);
}
}

// adjust layout on win changes
void XMLMenuEditor::Layout(void)
{
// adjust splitter sizes
horzSplitter.SetPos(10000 * cmdSize.cx / GetSize().cx);
}

// check wether a command is used in a menu
bool XMLMenuEditor::cmdInUse(String const &cmdId) const
{
if(menusEditor.IsUsingCommand(cmdId))
return true;
if(barsEditor.IsUsingCommand(cmdId))
return true;
return false;
}

// commandlist context menu
void XMLMenuEditor::cmdContextCb(Bar &bar)
{
// get current selection
int i = cmdPane.commandList.GetCursor();
if(i < 0)
return;
String cmdId = cmdPane.commandList.Get(i);
XMLCommand const &cmd = commands[i];
bar.Add(t_("Add custom command"), THISBACK(cmdContextAddCb));
if(cmd.GetIsCustom())
bar.Add(t_("Edit custom command"), THISBACK(cmdContextEditCb));
if(!cmd.GetIsCustom())
bar.Add(false, t_("Can't remove built-in command"), THISBACK(cmdContextRemoveCb));
else if(cmdInUse(cmdId))
bar.Add(false, t_("Can't remove - command in use"), THISBACK(cmdContextRemoveCb));
else
bar.Add(t_("Remove custom command"), THISBACK(cmdContextRemoveCb));
}

void XMLMenuEditor::cmdContextAddCb(void)
{
XMLCmdEdit edit(commands, true);
int res = edit.RunAppModal();
edit.Close();
if(res == IDOK)
{
commands.Add(edit.cmdId, edit.cmdStr);
FillCmdList();
cmdPane.commandList.SetCursor(cmdPane.commandList.Find(~edit.cmdId));
}
}

void XMLMenuEditor::cmdContextEditCb(void)
{
XMLCmdEdit edit(commands, false);
edit.cmdId <<= cmdPane.commandList.GetData();
edit.cmdStr <<= commands.Get(cmdPane.commandList.GetData()).GetCommandString();
int res = edit.RunAppModal();
edit.Close();
if(res == IDOK)
{
int idx = commands.Find(~edit.cmdId);
commands[idx].SetCommandString(edit.cmdStr);
FillCmdList();
cmdPane.commandList.SetCursor(cmdPane.commandList.Find(~edit.cmdId));
}
}

void XMLMenuEditor::cmdContextRemoveCb(void)
{
int i = cmdPane.commandList.GetCursor();
if(i < 0)
return;
cmdPane.commandList.Remove(i);
commands.Remove(i);
FillCmdList();
}

// commandlist double-click handler
void XMLMenuEditor::cmdDblClickCb(void)
{
int iCmd = cmdPane.commandList.GetCursor();
if(iCmd < 0)
return;
String cmdId = cmdPane.commandList.Get(iCmd);
int iTab = tabCtrl.Get();
if(iTab == 0)
menusEditor.SetCommandId(cmdId);
else
barsEditor.SetCommandId(cmdId);
}

END_UPP_NAMESPACE

Change log

r4242 by micio on Dec 4, 2011   Diff
Bazaar/XMLMenu : allow custom command
string on custom commands
Fixed a couple of bugs in editor
Go to: 
Project members, sign in to write a code review

Older revisions

r4072 by micio on Oct 20, 2011   Diff
Bazaar/XMLMenu : replaced FileSelector
with FileSel
r4070 by micio on Oct 20, 2011   Diff
sandbox/XMLMenu moved to bazaar
r4057 by micio on Oct 19, 2011   Diff
sandbox/XMLMenu : fixed template bug
and divide by zero one
All revisions of this file

File info

Size: 16484 bytes, 662 lines
Powered by Google Project Hosting