Repository navigation
Expand file tree
/
Copy pathUserMenu.cs
More file actions
210 lines (180 loc) · 6.38 KB
/
Copy pathUserMenu.cs
File metadata and controls
210 lines (180 loc) · 6.38 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace netCommander
{
public class UserMenu
{
public event QueryPanelInfoEventHandler QueryCurrentPanel;
public event QueryPanelInfoEventHandler QueryOtherPanel;
//first menu is 'Edit menu'
//second menu is divider
public UserMenu(MenuItem parent_menu)
{
ParentMenuItem = parent_menu;
MenuItem edit_menu = ParentMenuItem.MenuItems.Add(Options.GetLiteral(Options.LANG_EDIT));
edit_menu.Click += new EventHandler(edit_menu_Click);
ParentMenuItem.MenuItems.Add("-");
Options.ReadUserMenu(this);
}
void edit_menu_Click(object sender, EventArgs e)
{
UserMenuEditDialog dialog = new UserMenuEditDialog();
dialog.SetMenu(this);
dialog.ShowDialog();
Options.WriteUserMenu(this);
}
private void OnQueryCurrentPanel(QueryPanelInfoEventArgs e)
{
if (QueryCurrentPanel != null)
{
QueryCurrentPanel(this,e);
}
}
private void OnQueryOtherPanel(QueryPanelInfoEventArgs e)
{
if (QueryOtherPanel != null)
{
QueryOtherPanel(this,e);
}
}
public UserMenuEntry this[int index]
{
get
{
return (UserMenuEntry)ParentMenuItem.MenuItems[index + 2];
}
}
public MenuItem ParentMenuItem
{
get;
private set;
}
public void Clear()
{
for (int i = 2; i < ParentMenuItem.MenuItems.Count; i++)
{
ParentMenuItem.MenuItems.RemoveAt(i);
}
}
public void Add(UserMenuEntry entry)
{
ParentMenuItem.MenuItems.Add(entry);
entry.Click += new EventHandler(MenuItem_Click);
}
public void RemoveAt(int index)
{
ParentMenuItem.MenuItems.RemoveAt(index + 2);
}
public int Count
{
get
{
return ParentMenuItem.MenuItems.Count - 2;
}
}
void MenuItem_Click(object sender, EventArgs e)
{
execute((UserMenuEntry)sender);
}
private void execute(UserMenuEntry entry)
{
string command_expanded = entry.CommandText;
try
{
QueryPanelInfoEventArgs e_current = new QueryPanelInfoEventArgs();
QueryPanelInfoEventArgs e_other = new QueryPanelInfoEventArgs();
OnQueryCurrentPanel(e_current);
OnQueryOtherPanel(e_other);
string repl_text = string.Empty;
//process %1
if (command_expanded.Contains("%1"))
{
repl_text = e_current.ItemCollection.GetCommandlineTextShort(e_current.FocusedIndex);
if (repl_text.Contains(" "))
{
repl_text = '"' + repl_text + '"';
}
command_expanded = command_expanded.Replace("%1", repl_text);
}
//process %2
if (command_expanded.Contains("%2"))
{
repl_text = e_current.ItemCollection.GetCommandlineTextLong(e_current.FocusedIndex);
if (repl_text.Contains(" "))
{
repl_text = '"' + repl_text + '"';
}
command_expanded= command_expanded.Replace("%2", repl_text);
}
//process %3
if (command_expanded.Contains("%3"))
{
DirectoryList dl = (DirectoryList)e_other.ItemCollection;
repl_text = dl.DirectoryPath;
if (repl_text.Contains(" "))
{
repl_text = '"' + repl_text + '"';
}
command_expanded = command_expanded.Replace("%3", repl_text);
}
string reg_pattern = Options.REGEX_PARSE_COMMAND;
Regex rex = new Regex(reg_pattern);
string[] splitted = rex.Split(command_expanded, 2);
if (splitted.Length < 2)
{
throw new ApplicationException(Options.GetLiteral(Options.LANG_CANNOT_PARSE_COMMAND_LINE));
}
string command_name = splitted[1];
string command_args = splitted.Length > 2 ? splitted[2] : string.Empty;
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = command_args;
psi.CreateNoWindow = (entry.Options & ProcessStartFlags.NoWindow) == ProcessStartFlags.NoWindow;
psi.ErrorDialog = false;
psi.FileName = command_name;
psi.UseShellExecute = (entry.Options & ProcessStartFlags.UseShellexecute) == ProcessStartFlags.UseShellexecute;
psi.WorkingDirectory = Directory.GetCurrentDirectory();
Process p = new Process();
p.StartInfo = psi;
p.Start();
}
catch (Exception ex)
{
Messages.ShowException
(ex,
string.Format
(Options.GetLiteral(Options.LANG_CANNOT_EXECUTE_0),
command_expanded));
}
}
}
public class UserMenuEntry : MenuItem
{
public UserMenuEntry(string command_text, string menu_text)
: base(menu_text)
{
CommandText = command_text;
}
public string CommandText
{
get;
set;
}
public ProcessStartFlags Options
{
get;
set;
}
}
[Flags()]
public enum ProcessStartFlags
{
None = 0,
NoWindow = 0x1,
UseShellexecute = 0x2
}
}