This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
CustomUIContent.cs
365 lines (296 loc) · 9.93 KB
/
CustomUIContent.cs
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
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Diagnostics;
using UndoRedo.Control;
namespace CustomUIEditor
{
/// <summary>
/// Code related to custom UI Xml
/// </summary>
public partial class MainForm
{
#region Custom UI RichTextbox
private void customUI_TextChanged(object sender, EventArgs e)
{
this.colorTimer.Stop();
this.colorTimer.Start();
}
private void customUI_SelectionChanged(object sender, EventArgs e)
{
RichTextBox customUI = sender as RichTextBox;
Debug.Assert(customUI != null);
int end = customUI.SelectionStart + customUI.SelectionLength;
int line = customUI.GetLineFromCharIndex(end);
int col = end - customUI.GetFirstCharIndexFromLine(line);
this.statusBar.SuspendLayout();
this.lbLine.Text = StringsResource.idsLineStatus + (line + 1);
this.lbColumn.Text = StringsResource.idsColumnStatus + (col + 1);
this.statusBar.ResumeLayout();
//UndoRedo
_commands.NewCommand(rtbCustomUI.UndoActionName, rtbCustomUI.Rtf, rtbCustomUI.Text, rtbCustomUI.SelectionStart);
}
private void colorTimer_Tick(object sender, EventArgs e)
{
this.colorTimer.Stop();
int selectionStart = this.rtbCustomUI.SelectionStart;
int selectionLength = this.rtbCustomUI.SelectionLength;
bool modified = this.rtbCustomUI.Modified;
System.Drawing.Point pVisiblePoint = this.rtbCustomUI.GetPositionFromCharIndex(selectionStart);
this.rtbCustomUI.SuspendLayout();
this.rtbCustomUI.Rtf = XmlColorizer.Colorize(this.rtbCustomUI.Text);
this.rtbCustomUI.SelectionStart = selectionStart;
this.rtbCustomUI.SelectionLength = selectionLength;
this.rtbCustomUI.Modified = modified;
this.rtbCustomUI.ResumeLayout();
this.colorTimer.Stop();
}
#endregion
#region Custom Images
private void insertIcons_Click(object sender, EventArgs e)
{
if (tvDocument.SelectedNode == null || tvDocument.SelectedNode.Tag == null) return;
if (tvDocument.SelectedNode.Tag is XMLParts)
{
ShowInsertIconsDialog((XMLParts)tvDocument.SelectedNode.Tag);
}
}
private void insertIconsToolStripMenuItem_Click(object sender, EventArgs e)
{
XMLParts partType = (XMLParts)((TreeNode)tvDocument.Tag).Tag;
ShowInsertIconsDialog(partType);
}
private void ShowInsertIconsDialog(XMLParts partType)
{
Debug.Assert(this.package != null);
Debug.Assert(!this.package.ReadOnly);
if (this.package == null || this.package.ReadOnly) return;
OpenFileDialog insertIconsDialog = new OpenFileDialog();
insertIconsDialog.Tag = partType;
Debug.Assert(insertIconsDialog != null);
#region Initializing Insert Icons Dialog
insertIconsDialog.Title = StringsResource.idsInsertIconsDialogTitle;
insertIconsDialog.Multiselect = true;
insertIconsDialog.Filter = StringsResource.idsFilterAllSupportedImages + "|" + StringsResource.idsFilterAllFiles;
insertIconsDialog.FilterIndex = 0;
insertIconsDialog.RestoreDirectory = false;
#endregion
insertIconsDialog.FileOk += new CancelEventHandler(this.insertIcons_FileOk);
insertIconsDialog.ShowDialog(this);
insertIconsDialog.Dispose();
}
private void insertIcons_FileOk(object sender, CancelEventArgs e)
{
XMLParts partType = (XMLParts)(sender as OpenFileDialog).Tag;
OfficePart part = this.package.RetrieveCustomPart(partType);
Debug.Assert(part != null);
TreeNode partNode = null;
foreach (TreeNode node in tvDocument.Nodes[0].Nodes)
{
if (node.Text == part.Name)
{
partNode = node;
break;
}
}
Debug.Assert(partNode != null);
tvDocument.SuspendLayout();
foreach (string fileName in (sender as OpenFileDialog).FileNames)
{
try
{
string id = XmlConvert.EncodeName(Path.GetFileNameWithoutExtension(fileName));
Stream imageStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream, true, true);
// The file is a valid image at this point.
id = part.AddImage(fileName, id);
Debug.Assert(id != null, "Cannot create image part.");
if (id == null) continue;
imageStream.Close();
TreeNode imageNode = new TreeNode(id);
imageNode.ImageKey = "_" + id;
imageNode.SelectedImageKey = imageNode.ImageKey;
imageNode.ContextMenuStrip = imageContextMenu;
imageNode.Tag = partType;
tvDocument.ImageList.Images.Add(imageNode.ImageKey, image);
partNode.Nodes.Add(imageNode);
this.package.IsDirty = true;
}
catch (Exception ex)
{
ShowError(ex.Message);
continue;
}
}
tvDocument.ResumeLayout();
}
private void removeImage_Click(object sender, EventArgs e)
{
TreeNode currentNode = this.tvDocument.Tag as TreeNode;
Debug.Assert(currentNode != null);
if (currentNode == null) return;
OfficePart part = this.package.RetrieveCustomPart((XMLParts)currentNode.Tag);
Debug.Assert(part != null);
part.RemoveImage(currentNode.Text);
currentNode.Remove();
this.package.IsDirty = true;
}
private void editImageId_Click(object sender, EventArgs e)
{
TreeNode currentNode = this.tvDocument.Tag as TreeNode;
Debug.Assert(currentNode != null);
if (currentNode == null) return;
currentNode.BeginEdit();
}
private void imageContextMenu_Opening(object sender, CancelEventArgs e)
{
this.changeToolStripMenuItem.Enabled = !this.package.ReadOnly;
this.removeToolStripMenuItem.Enabled = !this.package.ReadOnly;
e.Cancel = false;
}
#endregion
#region Edit Context Menu
private void editContextMenu_Opening(object sender, CancelEventArgs e)
{
ContextMenuStrip contextMenu = sender as ContextMenuStrip;
contextMenu.SourceControl.Focus();
if (contextMenu.Items.Count > 0) return;
contextMenu.Width = 150;
contextMenu.Items.AddRange(this.CreateEditMenu());
e.Cancel = false;
}
private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
ToolStripMenuItem editMenu = sender as ToolStripMenuItem;
if (editMenu.DropDownItems.Count > 0) return;
editMenu.DropDownItems.AddRange(this.CreateEditMenu());
}
private ToolStripItem[] CreateEditMenu()
{
ToolStripItem[] items = new ToolStripItem[this.EditMenuItemText.Length];
for (int i = 0; i < items.Length; i++)
{
if (this.EditMenuItemText[i] == null)
{
items[i] = new ToolStripSeparator();
continue;
}
items[i] = new ToolStripMenuItem();
items[i].Size = new Size(150, 22);
items[i].Text = this.EditMenuItemText[i];
items[i].Image = this.EditMenuItemImages[i];
(items[i] as ToolStripMenuItem).ShortcutKeys = this.EditMenuItemKeys[i];
items[i].Tag = this.EditMenuItemTags[i];
}
return items;
}
private void edit_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
System.Reflection.MethodInfo methodInfo = e.ClickedItem.Tag as System.Reflection.MethodInfo;
if (methodInfo == null) return;
Control control = null;
if (sender is ContextMenuStrip)
{
control = (sender as ContextMenuStrip).SourceControl;
}
if (control == null)
{
if (this.rtbCustomUI.Focused)
{
control = this.rtbCustomUI;
}
else if (this.rtbCallbacks != null && this.rtbCallbacks.Focused)
{
control = this.rtbCallbacks;
}
}
if (control != null && control is RichTextBox)
{
//RedoUndo
if (methodInfo.Name.Equals("Undo"))
{
RichTextBox rtb = control as RichTextBox;
int index;
string rtf = _commands.RemoveCommand(out index, true);
while (rtf.Equals(rtb.Rtf))
{
rtf = _commands.RemoveCommand(out index, false);
if (rtf == null) break;
}
if (rtf != null) rtb.Rtf = rtf;
rtb.SelectionStart = index;
}
else if (methodInfo.Name.Equals("Redo"))
{
RichTextBox rtb = control as RichTextBox;
int index;
string rtf = _commands.RedoCommand(out index);
if (rtf != null)
{
while (rtf.Equals(rtb.Rtf))
{
rtf = _commands.RedoCommand(out index);
if (rtf == null) break;
}
rtb.Rtf = rtf;
rtb.SelectionStart = index;
}
}
else
{
methodInfo.Invoke(control, null);
}
return;
}
Debug.Assert(false, "Fail to invoke " + methodInfo.Name);
}
#region Edit Menu Items Information
private Keys[] EditMenuItemKeys = new Keys[] {
Keys.Control | Keys.Z,
Keys.Control | Keys.Y,
Keys.Control,
Keys.Control | Keys.X,
Keys.Control | Keys.C,
Keys.Control | Keys.V,
Keys.Control,
Keys.Control | Keys.A
};
private string[] EditMenuItemText = new String[] {
StringsResource.idsUndo,
StringsResource.idsRedo,
null,
StringsResource.idsCut,
StringsResource.idsCopy,
StringsResource.idsPaste,
null,
StringsResource.idsSellectAll
};
private System.Reflection.MethodInfo[] EditMenuItemTags = new System.Reflection.MethodInfo[] {
typeof(RichTextBox).GetMethod("Undo"),
typeof(RichTextBox).GetMethod("Redo"),
null,
typeof(RichTextBox).GetMethod("Cut"),
typeof(RichTextBox).GetMethod("Copy"),
typeof(RichTextBox).GetMethod("Paste", new Type[0]),
null,
typeof(RichTextBox).GetMethod("SelectAll")
};
private Image[] EditMenuItemImages = new Image[] {
ImagesResource.undo,
ImagesResource.redo,
null,
ImagesResource.cut,
ImagesResource.copy,
ImagesResource.paste,
null,
null
};
#endregion
#endregion
}
}