/[RomCheater]/trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/MainForm2.cs
ViewVC logotype

Contents of /trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/MainForm2.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 140 - (show annotations) (download)
Fri May 11 11:29:29 2012 UTC (9 years, 1 month ago) by william
File size: 14429 byte(s)


1 using System;
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Reflection;
6 using System.Windows.Forms;
7 using System.IO;
8 using WeifenLuo.WinFormsUI.Docking;
9 using DockSample.Customization;
10
11 namespace DockSample
12 {
13 public partial class MainForm2 : Form
14 {
15 private bool m_bSaveLayout = true;
16 private DeserializeDockContent m_deserializeDockContent;
17 private DummySolutionExplorer m_solutionExplorer = new DummySolutionExplorer();
18 private DummyPropertyWindow m_propertyWindow = new DummyPropertyWindow();
19 private DummyToolbox m_toolbox = new DummyToolbox();
20 private DummyOutputWindow m_outputWindow = new DummyOutputWindow();
21 private DummyTaskList m_taskList = new DummyTaskList();
22
23 public MainForm2()
24 {
25 InitializeComponent();
26 showRightToLeft.Checked = (RightToLeft == RightToLeft.Yes);
27 RightToLeftLayout = showRightToLeft.Checked;
28 m_solutionExplorer = new DummySolutionExplorer();
29 m_solutionExplorer.RightToLeftLayout = RightToLeftLayout;
30 m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
31 }
32
33 private void menuItemExit_Click(object sender, System.EventArgs e)
34 {
35 Close();
36 }
37
38 private void menuItemSolutionExplorer_Click(object sender, System.EventArgs e)
39 {
40 m_solutionExplorer.Show(dockPanel);
41 }
42
43 private void menuItemPropertyWindow_Click(object sender, System.EventArgs e)
44 {
45 m_propertyWindow.Show(dockPanel);
46 }
47
48 private void menuItemToolbox_Click(object sender, System.EventArgs e)
49 {
50 m_toolbox.Show(dockPanel);
51 }
52
53 private void menuItemOutputWindow_Click(object sender, System.EventArgs e)
54 {
55 m_outputWindow.Show(dockPanel);
56 }
57
58 private void menuItemTaskList_Click(object sender, System.EventArgs e)
59 {
60 m_taskList.Show(dockPanel);
61 }
62
63 private void menuItemAbout_Click(object sender, System.EventArgs e)
64 {
65 AboutDialog aboutDialog = new AboutDialog();
66 aboutDialog.ShowDialog(this);
67 }
68
69 private IDockContent FindDocument(string text)
70 {
71 if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
72 {
73 foreach (Form form in MdiChildren)
74 if (form.Text == text)
75 return form as IDockContent;
76
77 return null;
78 }
79 else
80 {
81 foreach (IDockContent content in dockPanel.Documents)
82 if (content.DockHandler.TabText == text)
83 return content;
84
85 return null;
86 }
87 }
88
89 private void menuItemNew_Click(object sender, System.EventArgs e)
90 {
91 DummyDoc dummyDoc = CreateNewDocument();
92 if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
93 {
94 dummyDoc.MdiParent = this;
95 dummyDoc.Show();
96 }
97 else
98 dummyDoc.Show(dockPanel);
99 }
100
101 private DummyDoc CreateNewDocument()
102 {
103 DummyDoc dummyDoc = new DummyDoc();
104
105 int count = 1;
106 //string text = "C:\\MADFDKAJ\\ADAKFJASD\\ADFKDSAKFJASD\\ASDFKASDFJASDF\\ASDFIJADSFJ\\ASDFKDFDA" + count.ToString();
107 string text = "Document" + count.ToString();
108 while (FindDocument(text) != null)
109 {
110 count ++;
111 //text = "C:\\MADFDKAJ\\ADAKFJASD\\ADFKDSAKFJASD\\ASDFKASDFJASDF\\ASDFIJADSFJ\\ASDFKDFDA" + count.ToString();
112 text = "Document" + count.ToString();
113 }
114 dummyDoc.Text = text;
115 return dummyDoc;
116 }
117
118 private DummyDoc CreateNewDocument(string text)
119 {
120 DummyDoc dummyDoc = new DummyDoc();
121 dummyDoc.Text = text;
122 return dummyDoc;
123 }
124
125 private void menuItemOpen_Click(object sender, System.EventArgs e)
126 {
127 OpenFileDialog openFile = new OpenFileDialog();
128
129 openFile.InitialDirectory = Application.ExecutablePath;
130 openFile.Filter = "rtf files (*.rtf)|*.rtf|txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
131 openFile.FilterIndex = 1;
132 openFile.RestoreDirectory = true ;
133
134 if(openFile.ShowDialog() == DialogResult.OK)
135 {
136 string fullName = openFile.FileName;
137 string fileName = Path.GetFileName(fullName);
138
139 if (FindDocument(fileName) != null)
140 {
141 MessageBox.Show("The document: " + fileName + " has already opened!");
142 return;
143 }
144
145 DummyDoc dummyDoc = new DummyDoc();
146 dummyDoc.Text = fileName;
147 dummyDoc.Show(dockPanel);
148 try
149 {
150 dummyDoc.FileName = fullName;
151 }
152 catch (Exception exception)
153 {
154 dummyDoc.Close();
155 MessageBox.Show(exception.Message);
156 }
157
158 }
159 }
160
161 private void menuItemFile_Popup(object sender, System.EventArgs e)
162 {
163 if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
164 {
165 menuItemClose.Enabled = menuItemCloseAll.Enabled = (ActiveMdiChild != null);
166 }
167 else
168 {
169 menuItemClose.Enabled = (dockPanel.ActiveDocument != null);
170 menuItemCloseAll.Enabled = (dockPanel.DocumentsCount > 0);
171 }
172 }
173
174 private void menuItemClose_Click(object sender, System.EventArgs e)
175 {
176 if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
177 ActiveMdiChild.Close();
178 else if (dockPanel.ActiveDocument != null)
179 dockPanel.ActiveDocument.DockHandler.Close();
180 }
181
182 private void menuItemCloseAll_Click(object sender, System.EventArgs e)
183 {
184 CloseAllDocuments();
185 }
186
187 private void CloseAllDocuments()
188 {
189 if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
190 {
191 foreach (Form form in MdiChildren)
192 form.Close();
193 }
194 else
195 {
196 IDockContent[] documents = dockPanel.DocumentsToArray();
197 foreach (IDockContent content in documents)
198 content.DockHandler.Close();
199 }
200 }
201
202 private IDockContent GetContentFromPersistString(string persistString)
203 {
204 if (persistString == typeof(DummySolutionExplorer).ToString())
205 return m_solutionExplorer;
206 else if (persistString == typeof(DummyPropertyWindow).ToString())
207 return m_propertyWindow;
208 else if (persistString == typeof(DummyToolbox).ToString())
209 return m_toolbox;
210 else if (persistString == typeof(DummyOutputWindow).ToString())
211 return m_outputWindow;
212 else if (persistString == typeof(DummyTaskList).ToString())
213 return m_taskList;
214 else
215 {
216 string[] parsedStrings = persistString.Split(new char[] { ',' });
217 if (parsedStrings.Length != 3)
218 return null;
219
220 if (parsedStrings[0] != typeof(DummyDoc).ToString())
221 return null;
222
223 DummyDoc dummyDoc = new DummyDoc();
224 if (parsedStrings[1] != string.Empty)
225 dummyDoc.FileName = parsedStrings[1];
226 if (parsedStrings[2] != string.Empty)
227 dummyDoc.Text = parsedStrings[2];
228
229 return dummyDoc;
230 }
231 }
232
233 private void MainForm_Load(object sender, System.EventArgs e)
234 {
235 string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
236
237 if (File.Exists(configFile))
238 dockPanel.LoadFromXml(configFile, m_deserializeDockContent);
239 }
240
241 private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
242 {
243 string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
244 if (m_bSaveLayout)
245 dockPanel.SaveAsXml(configFile);
246 else if (File.Exists(configFile))
247 File.Delete(configFile);
248 }
249
250 private void menuItemToolBar_Click(object sender, System.EventArgs e)
251 {
252 toolBar.Visible = menuItemToolBar.Checked = !menuItemToolBar.Checked;
253 }
254
255 private void menuItemStatusBar_Click(object sender, System.EventArgs e)
256 {
257 statusBar.Visible = menuItemStatusBar.Checked = !menuItemStatusBar.Checked;
258 }
259
260 private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
261 {
262 if (e.ClickedItem == toolBarButtonNew)
263 menuItemNew_Click(null, null);
264 else if (e.ClickedItem == toolBarButtonOpen)
265 menuItemOpen_Click(null, null);
266 else if (e.ClickedItem == toolBarButtonSolutionExplorer)
267 menuItemSolutionExplorer_Click(null, null);
268 else if (e.ClickedItem == toolBarButtonPropertyWindow)
269 menuItemPropertyWindow_Click(null, null);
270 else if (e.ClickedItem == toolBarButtonToolbox)
271 menuItemToolbox_Click(null, null);
272 else if (e.ClickedItem == toolBarButtonOutputWindow)
273 menuItemOutputWindow_Click(null, null);
274 else if (e.ClickedItem == toolBarButtonTaskList)
275 menuItemTaskList_Click(null, null);
276 else if (e.ClickedItem == toolBarButtonLayoutByCode)
277 menuItemLayoutByCode_Click(null, null);
278 else if (e.ClickedItem == toolBarButtonLayoutByXml)
279 menuItemLayoutByXml_Click(null, null);
280 }
281
282 private void menuItemNewWindow_Click(object sender, System.EventArgs e)
283 {
284 MainForm newWindow = new MainForm();
285 newWindow.Text = newWindow.Text + " - New";
286 newWindow.Show();
287 }
288
289 private void menuItemTools_Popup(object sender, System.EventArgs e)
290 {
291 menuItemLockLayout.Checked = !this.dockPanel.AllowEndUserDocking;
292 }
293
294 private void menuItemLockLayout_Click(object sender, System.EventArgs e)
295 {
296 dockPanel.AllowEndUserDocking = !dockPanel.AllowEndUserDocking;
297 }
298
299 private void menuItemLayoutByCode_Click(object sender, System.EventArgs e)
300 {
301 dockPanel.SuspendLayout(true);
302
303 m_solutionExplorer.Show(dockPanel, DockState.DockRight);
304 m_propertyWindow.Show(m_solutionExplorer.Pane, m_solutionExplorer);
305 m_toolbox.Show(dockPanel, new Rectangle(98, 133, 200, 383));
306 m_outputWindow.Show(m_solutionExplorer.Pane, DockAlignment.Bottom, 0.35);
307 m_taskList.Show(m_toolbox.Pane, DockAlignment.Left, 0.4);
308
309 CloseAllDocuments();
310 DummyDoc doc1 = CreateNewDocument("Document1");
311 DummyDoc doc2 = CreateNewDocument("Document2");
312 DummyDoc doc3 = CreateNewDocument("Document3");
313 DummyDoc doc4 = CreateNewDocument("Document4");
314 doc1.Show(dockPanel, DockState.Document);
315 doc2.Show(doc1.Pane, null);
316 doc3.Show(doc1.Pane, DockAlignment.Bottom, 0.5);
317 doc4.Show(doc3.Pane, DockAlignment.Right, 0.5);
318
319 dockPanel.ResumeLayout(true, true);
320 }
321
322 private void menuItemLayoutByXml_Click(object sender, System.EventArgs e)
323 {
324 dockPanel.SuspendLayout(true);
325
326 // In order to load layout from XML, we need to close all the DockContents
327 CloseAllContents();
328
329 Assembly assembly = Assembly.GetAssembly(typeof(MainForm));
330 Stream xmlStream = assembly.GetManifestResourceStream("DockSample.Resources.DockPanel.xml");
331 dockPanel.LoadFromXml(xmlStream, m_deserializeDockContent);
332 xmlStream.Close();
333
334 dockPanel.ResumeLayout(true, true);
335 }
336
337 private void CloseAllContents()
338 {
339 // we don't want to create another instance of tool window, set DockPanel to null
340 m_solutionExplorer.DockPanel = null;
341 m_propertyWindow.DockPanel = null;
342 m_toolbox.DockPanel = null;
343 m_outputWindow.DockPanel = null;
344 m_taskList.DockPanel = null;
345
346 // Close all other document windows
347 CloseAllDocuments();
348 }
349
350 private void SetSchema(object sender, System.EventArgs e)
351 {
352 CloseAllContents();
353
354 if (sender == menuItemSchemaVS2005)
355 Extender.SetSchema(dockPanel, Extender.Schema.VS2005);
356 else if (sender == menuItemSchemaVS2003)
357 Extender.SetSchema(dockPanel, Extender.Schema.VS2003);
358
359 menuItemSchemaVS2005.Checked = (sender == menuItemSchemaVS2005);
360 menuItemSchemaVS2003.Checked = (sender == menuItemSchemaVS2003);
361 }
362
363 private void SetDocumentStyle(object sender, System.EventArgs e)
364 {
365 DocumentStyle oldStyle = dockPanel.DocumentStyle;
366 DocumentStyle newStyle;
367 if (sender == menuItemDockingMdi)
368 newStyle = DocumentStyle.DockingMdi;
369 else if (sender == menuItemDockingWindow)
370 newStyle = DocumentStyle.DockingWindow;
371 else if (sender == menuItemDockingSdi)
372 newStyle = DocumentStyle.DockingSdi;
373 else
374 newStyle = DocumentStyle.SystemMdi;
375
376 if (oldStyle == newStyle)
377 return;
378
379 if (oldStyle == DocumentStyle.SystemMdi || newStyle == DocumentStyle.SystemMdi)
380 CloseAllDocuments();
381
382 dockPanel.DocumentStyle = newStyle;
383 menuItemDockingMdi.Checked = (newStyle == DocumentStyle.DockingMdi);
384 menuItemDockingWindow.Checked = (newStyle == DocumentStyle.DockingWindow);
385 menuItemDockingSdi.Checked = (newStyle == DocumentStyle.DockingSdi);
386 menuItemSystemMdi.Checked = (newStyle == DocumentStyle.SystemMdi);
387 menuItemLayoutByCode.Enabled = (newStyle != DocumentStyle.SystemMdi);
388 menuItemLayoutByXml.Enabled = (newStyle != DocumentStyle.SystemMdi);
389 toolBarButtonLayoutByCode.Enabled = (newStyle != DocumentStyle.SystemMdi);
390 toolBarButtonLayoutByXml.Enabled = (newStyle != DocumentStyle.SystemMdi);
391 }
392
393 private void menuItemCloseAllButThisOne_Click(object sender, System.EventArgs e)
394 {
395 if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
396 {
397 Form activeMdi = ActiveMdiChild;
398 foreach (Form form in MdiChildren)
399 {
400 if (form != activeMdi)
401 form.Close();
402 }
403 }
404 else
405 {
406 foreach (IDockContent document in dockPanel.DocumentsToArray())
407 {
408 if (!document.DockHandler.IsActivated)
409 document.DockHandler.Close();
410 }
411 }
412 }
413
414 private void menuItemShowDocumentIcon_Click(object sender, System.EventArgs e)
415 {
416 dockPanel.ShowDocumentIcon = menuItemShowDocumentIcon.Checked= !menuItemShowDocumentIcon.Checked;
417 }
418
419 private void showRightToLeft_Click(object sender, EventArgs e)
420 {
421 CloseAllContents();
422 if (showRightToLeft.Checked)
423 {
424 this.RightToLeft = RightToLeft.No;
425 this.RightToLeftLayout = false;
426 }
427 else
428 {
429 this.RightToLeft = RightToLeft.Yes;
430 this.RightToLeftLayout = true;
431 }
432 m_solutionExplorer.RightToLeftLayout = this.RightToLeftLayout;
433 showRightToLeft.Checked = !showRightToLeft.Checked;
434 }
435
436 private void exitWithoutSavingLayout_Click(object sender, EventArgs e)
437 {
438 m_bSaveLayout = false;
439 Close();
440 m_bSaveLayout = true;
441 }
442 }
443 }

  ViewVC Help
Powered by ViewVC 1.1.22