MacroManager.patch

Abdelghani Omari, 06/18/2017 07:17 PM

Download (6.97 KB)

View differences:

MacroManager.cpp (working copy)
2 2

  
3 3
#define METHOD_NAME "MacroManagerWindow " << UPP_FUNCTION_NAME << "(): "
4 4

  
5
static const int GLOBAL_MACRO_KEY = 0;
6
static const int LOCAL_MACRO_KEY  = 10;
5
static const int GLOBAL_NODE_KEY = 0;
6
static const bool LOCAL_NODE_KEY  = false;
7
static const double PACKAGE_NODE_KEY  = 0.0;
7 8

  
8
MacroManagerWindow::MacroManagerWindow(Ide& ide)
9
	: ide(ide)
9
MacroManagerWindow::MacroManagerWindow()
10 10
{
11 11
	CtrlLayout(*this, t_("Macro Manager"));
12 12
	Zoomable().Sizeable().MinimizeBox(false);
......
31 31
	InitButtons();
32 32
}
33 33

  
34

  
34 35
void MacroManagerWindow::OnMacroBar(Bar& bar)
35 36
{
36
	bool isGlobalFile = IsString(macrosTree.Get());
37
	
38
	bar.Add(t_("New global macro file.."), [=]{ OnNewMacroFile();});
39
	bar.Add(t_("Delete macro file"),       [=]{ OnDeleteMacroFile();})
40
	    .Enable(isGlobalFile);
37
	bar.Add(t_("New global macro file.."),   [=]{ OnNewMacroFile();});
41 38

  
39
	bar.Add(t_("Delete macro file.."),       [=]{ OnDeleteMacroFile();})
40
	    .Enable(IsAGlobalFile());
41

  
42
	bar.Add(t_("Import macro file.."),       [=]{ OnImport();});
43

  
44
	bar.Add(t_("Export macro file.."),       [=]{ OnExport();})
45
	    .Enable(!IsAMacro());
46

  
47
	bar.Separator();
48

  
49
	bar.Add(t_("Edit"),                      [=]{ OnEditFile();})
50
	    .Enable(IsAFile() || IsAMacro());
42 51
}
43 52

  
44 53
void MacroManagerWindow::InitButtons()
......
71 80
	}
72 81

  
73 82
	Value key = macrosTree.Get();
74
	if(IsNumber(key)) {
83
	if(IsARootOrAPackage()) {
75 84
		editor.Hide();
76 85
		editButton.Disable();
77 86
		
78 87
		int node = macrosTree.Find(key);
79
		exportButton.Enable(macrosTree.GetChildCount(node) > 0);
88
		exportButton.Enable(IsTheGlobalRoot() || IsAGlobalFile());
80 89
		
81 90
		return;
82 91
	}
83 92

  
84
	if(IsString(key)) {
93
	if(IsAFile()) {
85 94
		editButton.Enable();
86 95
		exportButton.Disable();
87 96
		editor.Show();
88
		editor.Set(LoadFile(AppendFileName(GetLocalDir(),  (String)key)));
97
		editor.Set(LoadFile((String)key));
89 98
		
90 99
		return;
91 100
	}
......
146 155

  
147 156
void MacroManagerWindow::FindNodeFiles(int id, Index<String>& list)
148 157
{
149
	Value key = macrosTree.Get(id);
150
	if(IsNumber(key) || IsString(key)) {
158
	if(IsAFile(id)) {
159
		list.FindAdd((String)macrosTree.Get(id));
160
	}
161
	else {
151 162
		for(int i = 0; i < macrosTree.GetChildCount(id); i++) {
152 163
			int node = macrosTree.GetChild(id, i);
153 164
			FindNodeFiles(node, list);
154 165
		}
155 166
	}
156
	else {
157
		MacroElement element = ValueTo<MacroElement>(key);
158
		list.FindAdd(element.fileName);
159
	}
160 167
}
161 168

  
162 169
void MacroManagerWindow::OnExport()
......
168 175
	if(dir.IsEmpty())
169 176
		return;
170 177

  
171
	Value key = macrosTree.Get();
172
	if(IsNumber(key) || IsString(key)) {
178
	if(!IsAMacro()) {
173 179
		int id = macrosTree.GetCursor();
174 180
		Index<String> list;
175 181
		FindNodeFiles(id, list);
......
182 188
	if(!macrosTree.IsCursor())
183 189
		return;
184 190

  
185
	Value key = macrosTree.Get();
186
	if(IsNumber(key))
187
		return;
188
	
189
	// TODO: Move this logic outside class - we serious don't want ide dependency here.
190
	if(IsString(key)) {
191
		ide.EditFile(AppendFileName(GetLocalDir(), (String)key));
192
		ide.editor.SetCursor(0);
193
		ide.editor.CenterCursor();
194
	} else {
195
		MacroElement element = ValueTo<MacroElement>(key);
196
	
197
		ide.EditFile(element.fileName);
198
		ide.editor.SetCursor(ide.editor.GetPos(element.line - 1));
199
		ide.editor.CenterCursor();
191
	if(IsAMacro()) {
192
		MacroElement element = ValueTo<MacroElement>(macrosTree.Get());
193
		WhenEdit(element.fileName, element.line - 1);
194
		Break();
200 195
	}
201
	
202
	Break();
196
	else if(IsAFile()) {
197
		WhenEdit( (String)macrosTree.Get(), 1);
198
		Break();
199
	}
203 200
}
204 201

  
205 202
void MacroManagerWindow::OnNewMacroFile()
......
246 243
		if(!ff.GetPath().EndsWith(String() << "UppLocal" << DIR_SEPS << ff.GetName()))
247 244
			fileTitle = "../" + fileTitle;
248 245
		
249
		int fileNode = macrosTree.Add(globalNode, Image(), fileTitle, fileTitle);
246
		int fileNode = macrosTree.Add(globalNode, Image(), ff.GetPath(), fileTitle);
250 247
		
251 248
		auto list = UscFileParser(ff.GetPath()).Parse();
252 249
		for(const auto& element : list) {
......
265 262
{
266 263
	int globalNode = macrosTree.Find(0);
267 264
	if(globalNode < 0) {
268
		globalNode = macrosTree.Add(0, Image(), GLOBAL_MACRO_KEY, t_("Global macros"));
265
		globalNode = macrosTree.Add(0, Image(), GLOBAL_NODE_KEY, t_("Global macros"));
269 266
	}
270 267
	else {
271 268
		macrosTree.RemoveChildren(globalNode);
......
279 276

  
280 277
void MacroManagerWindow::ReloadLocalMacros()
281 278
{
282
	int localNode = macrosTree.Add(0, Image(), LOCAL_MACRO_KEY, t_("Local macros"));
279
	int localNode = macrosTree.Add(0, Image(), LOCAL_NODE_KEY, t_("Local macros"));
283 280

  
284
	const Workspace& wspc = ide.IdeWorkspace();
281
	const Workspace& wspc = GetIdeWorkspace();
285 282

  
286 283
	for(int i = 0; i < wspc.GetCount(); i++) {
287 284
		const Package& package = wspc.GetPackage(i);
......
297 294
				continue;
298 295
			
299 296
			if(packageNode == -1)
300
				packageNode = macrosTree.Add(localNode, Image(), LOCAL_MACRO_KEY + 1, wspc[i]);
297
				packageNode = macrosTree.Add(localNode, Image(), PACKAGE_NODE_KEY, wspc[i]);
301 298
					
302
			int fileNode = macrosTree.Add(packageNode, Image(), LOCAL_MACRO_KEY + 2, file);
299
			int fileNode = macrosTree.Add(packageNode, Image(), filePath.ToWString(), file);
303 300
			for(int j = 0; j < list.GetCount(); j++) {
304 301
				MacroElement& element = list[j];
305 302
				macrosTree.Add(fileNode, element.GetImage(), RawToValue(element), element.name);
......
310 307

  
311 308
void Ide::DoMacroManager()
312 309
{
313
	MacroManagerWindow(*this).Execute();
310
	MacroManagerWindow dlg;
311
	
312
	dlg.WhenEdit = [&](String fileName, int line) {
313
		EditFile(fileName);
314
		editor.SetCursor(editor.GetPos(line));
315
		editor.CenterCursor();
316
	};
317
	
318
	dlg.Execute();
314 319
}
MacroManager.h (working copy)
52 52
	using MacroStore = ArrayMap<String, Array<MacroElement>>;
53 53
	
54 54
public:
55
	MacroManagerWindow(Ide& ide);
55
	MacroManagerWindow();
56 56

  
57 57
	void Layout() override;
58
	
59
	Event<String, int> WhenEdit;
58 60

  
59 61
private:
60 62
	void InitButtons();
......
77 79

  
78 80
private:
79 81
	static String GenFileOverrideMessage(const String& fileName);
82

  
83
	bool IsAMacro()				{ return macrosTree.Get().Is<MacroElement>();}
84
	bool IsAMacro(int id)		{ return macrosTree.Get(id).Is<MacroElement>();}
80 85
	
86
	bool IsTheGlobalRoot()		{ return macrosTree.Get().Is<int>(); }
87
	bool IsTheLocalRoot()		{ return macrosTree.Get().Is<bool>(); }
88
	bool IsAPackage()			{ return macrosTree.Get().Is<double>(); }
89
	bool IsARootOrAPackage()	{ return IsNumber(macrosTree.Get()); }
90
	
91
	bool IsAGlobalFile()		{ return macrosTree.Get().Is<String>(); }
92
	bool IsALocalFile()			{ return macrosTree.Get().Is<WString>(); }
93
	
94
	bool IsAFile()				{ return macrosTree.Get().Is<String>() || macrosTree.Get().Is<WString>(); }
95
	bool IsAFile(int id)		{ return macrosTree.Get(id).Is<String>() || macrosTree.Get(id).Is<WString>(); }
96
	
81 97
private:
82
	// TODO: MacroManager shold not depend upon Ide instance.
83
	// The edit logic should be outside class the same as load macros.
84
	Ide&          ide;
85 98
	
86 99
	TreeCtrl      macrosTree;
87 100
	SplitterFrame splitter;