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

Annotation of /trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/MainForm.cs

Parent Directory Parent Directory | Revision Log Revision Log


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


1 william 140 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 MainForm : 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 MainForm()
24     {
25     InitializeComponent();
26    
27     showRightToLeft.Checked = (RightToLeft == RightToLeft.Yes);
28     RightToLeftLayout = showRightToLeft.Checked;
29     m_solutionExplorer = new DummySolutionExplorer();
30     m_solutionExplorer.RightToLeftLayout = RightToLeftLayout;
31     m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
32     }
33    
34     #region Methods
35    
36     private IDockContent FindDocument(string text)
37     {
38     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
39     {
40     foreach (Form form in MdiChildren)
41     if (form.Text == text)
42     return form as IDockContent;
43    
44     return null;
45     }
46     else
47     {
48     foreach (IDockContent content in dockPanel.Documents)
49     if (content.DockHandler.TabText == text)
50     return content;
51    
52     return null;
53     }
54     }
55    
56     private DummyDoc CreateNewDocument()
57     {
58     DummyDoc dummyDoc = new DummyDoc();
59    
60     int count = 1;
61     //string text = "C:\\MADFDKAJ\\ADAKFJASD\\ADFKDSAKFJASD\\ASDFKASDFJASDF\\ASDFIJADSFJ\\ASDFKDFDA" + count.ToString();
62     string text = "Document" + count.ToString();
63     while (FindDocument(text) != null)
64     {
65     count++;
66     //text = "C:\\MADFDKAJ\\ADAKFJASD\\ADFKDSAKFJASD\\ASDFKASDFJASDF\\ASDFIJADSFJ\\ASDFKDFDA" + count.ToString();
67     text = "Document" + count.ToString();
68     }
69     dummyDoc.Text = text;
70     return dummyDoc;
71     }
72    
73     private DummyDoc CreateNewDocument(string text)
74     {
75     DummyDoc dummyDoc = new DummyDoc();
76     dummyDoc.Text = text;
77     return dummyDoc;
78     }
79    
80     private void CloseAllDocuments()
81     {
82     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
83     {
84     foreach (Form form in MdiChildren)
85     form.Close();
86     }
87     else
88     {
89     for (int index = dockPanel.Contents.Count - 1; index >= 0; index--)
90     {
91     if (dockPanel.Contents[index] is IDockContent)
92     {
93     IDockContent content = (IDockContent)dockPanel.Contents[index];
94     content.DockHandler.Close();
95     }
96     }
97     }
98     }
99    
100     private IDockContent GetContentFromPersistString(string persistString)
101     {
102     if (persistString == typeof(DummySolutionExplorer).ToString())
103     return m_solutionExplorer;
104     else if (persistString == typeof(DummyPropertyWindow).ToString())
105     return m_propertyWindow;
106     else if (persistString == typeof(DummyToolbox).ToString())
107     return m_toolbox;
108     else if (persistString == typeof(DummyOutputWindow).ToString())
109     return m_outputWindow;
110     else if (persistString == typeof(DummyTaskList).ToString())
111     return m_taskList;
112     else
113     {
114     // DummyDoc overrides GetPersistString to add extra information into persistString.
115     // Any DockContent may override this value to add any needed information for deserialization.
116    
117     string[] parsedStrings = persistString.Split(new char[] { ',' });
118     if (parsedStrings.Length != 3)
119     return null;
120    
121     if (parsedStrings[0] != typeof(DummyDoc).ToString())
122     return null;
123    
124     DummyDoc dummyDoc = new DummyDoc();
125     if (parsedStrings[1] != string.Empty)
126     dummyDoc.FileName = parsedStrings[1];
127     if (parsedStrings[2] != string.Empty)
128     dummyDoc.Text = parsedStrings[2];
129    
130     return dummyDoc;
131     }
132     }
133    
134     private void CloseAllContents()
135     {
136     // we don't want to create another instance of tool window, set DockPanel to null
137     m_solutionExplorer.DockPanel = null;
138     m_propertyWindow.DockPanel = null;
139     m_toolbox.DockPanel = null;
140     m_outputWindow.DockPanel = null;
141     m_taskList.DockPanel = null;
142    
143     // Close all other document windows
144     CloseAllDocuments();
145     }
146    
147     private void SetSchema(object sender, System.EventArgs e)
148     {
149     CloseAllContents();
150    
151     if (sender == menuItemSchemaVS2005)
152     Extender.SetSchema(dockPanel, Extender.Schema.VS2005);
153     else if (sender == menuItemSchemaVS2003)
154     Extender.SetSchema(dockPanel, Extender.Schema.VS2003);
155    
156     menuItemSchemaVS2005.Checked = (sender == menuItemSchemaVS2005);
157     menuItemSchemaVS2003.Checked = (sender == menuItemSchemaVS2003);
158     }
159    
160     private void SetDocumentStyle(object sender, System.EventArgs e)
161     {
162     DocumentStyle oldStyle = dockPanel.DocumentStyle;
163     DocumentStyle newStyle;
164     if (sender == menuItemDockingMdi)
165     newStyle = DocumentStyle.DockingMdi;
166     else if (sender == menuItemDockingWindow)
167     newStyle = DocumentStyle.DockingWindow;
168     else if (sender == menuItemDockingSdi)
169     newStyle = DocumentStyle.DockingSdi;
170     else
171     newStyle = DocumentStyle.SystemMdi;
172    
173     if (oldStyle == newStyle)
174     return;
175    
176     if (oldStyle == DocumentStyle.SystemMdi || newStyle == DocumentStyle.SystemMdi)
177     CloseAllDocuments();
178    
179     dockPanel.DocumentStyle = newStyle;
180     menuItemDockingMdi.Checked = (newStyle == DocumentStyle.DockingMdi);
181     menuItemDockingWindow.Checked = (newStyle == DocumentStyle.DockingWindow);
182     menuItemDockingSdi.Checked = (newStyle == DocumentStyle.DockingSdi);
183     menuItemSystemMdi.Checked = (newStyle == DocumentStyle.SystemMdi);
184     menuItemLayoutByCode.Enabled = (newStyle != DocumentStyle.SystemMdi);
185     menuItemLayoutByXml.Enabled = (newStyle != DocumentStyle.SystemMdi);
186     toolBarButtonLayoutByCode.Enabled = (newStyle != DocumentStyle.SystemMdi);
187     toolBarButtonLayoutByXml.Enabled = (newStyle != DocumentStyle.SystemMdi);
188     }
189    
190     private void SetDockPanelSkinOptions(bool isChecked)
191     {
192     if (isChecked)
193     {
194     // All of these options may be set in the designer.
195     // This is not a complete list of possible options available in the skin.
196    
197     AutoHideStripSkin autoHideSkin = new AutoHideStripSkin();
198     autoHideSkin.DockStripGradient.StartColor = Color.AliceBlue;
199     autoHideSkin.DockStripGradient.EndColor = Color.Blue;
200     autoHideSkin.DockStripGradient.LinearGradientMode = System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal;
201     autoHideSkin.TabGradient.StartColor = SystemColors.Control;
202     autoHideSkin.TabGradient.EndColor = SystemColors.ControlDark;
203     autoHideSkin.TabGradient.TextColor = SystemColors.ControlText;
204     autoHideSkin.TextFont = new Font("Showcard Gothic", 10);
205    
206     dockPanel.Skin.AutoHideStripSkin = autoHideSkin;
207    
208     DockPaneStripSkin dockPaneSkin = new DockPaneStripSkin();
209     dockPaneSkin.DocumentGradient.DockStripGradient.StartColor = Color.Red;
210     dockPaneSkin.DocumentGradient.DockStripGradient.EndColor = Color.Pink;
211    
212     dockPaneSkin.DocumentGradient.ActiveTabGradient.StartColor = Color.Green;
213     dockPaneSkin.DocumentGradient.ActiveTabGradient.EndColor = Color.Green;
214     dockPaneSkin.DocumentGradient.ActiveTabGradient.TextColor = Color.White;
215    
216     dockPaneSkin.DocumentGradient.InactiveTabGradient.StartColor = Color.Gray;
217     dockPaneSkin.DocumentGradient.InactiveTabGradient.EndColor = Color.Gray;
218     dockPaneSkin.DocumentGradient.InactiveTabGradient.TextColor = Color.Black;
219    
220     dockPaneSkin.TextFont = new Font("SketchFlow Print", 10);
221    
222     dockPanel.Skin.DockPaneStripSkin = dockPaneSkin;
223     }
224     else
225     {
226     dockPanel.Skin = new DockPanelSkin();
227     }
228    
229     menuItemLayoutByXml_Click(menuItemLayoutByXml, EventArgs.Empty);
230     }
231    
232     #endregion
233    
234     #region Event Handlers
235    
236     private void menuItemExit_Click(object sender, System.EventArgs e)
237     {
238     Close();
239     }
240    
241     private void menuItemSolutionExplorer_Click(object sender, System.EventArgs e)
242     {
243     m_solutionExplorer.Show(dockPanel);
244     }
245    
246     private void menuItemPropertyWindow_Click(object sender, System.EventArgs e)
247     {
248     m_propertyWindow.Show(dockPanel);
249     }
250    
251     private void menuItemToolbox_Click(object sender, System.EventArgs e)
252     {
253     m_toolbox.Show(dockPanel);
254     }
255    
256     private void menuItemOutputWindow_Click(object sender, System.EventArgs e)
257     {
258     m_outputWindow.Show(dockPanel);
259     }
260    
261     private void menuItemTaskList_Click(object sender, System.EventArgs e)
262     {
263     m_taskList.Show(dockPanel);
264     }
265    
266     private void menuItemAbout_Click(object sender, System.EventArgs e)
267     {
268     AboutDialog aboutDialog = new AboutDialog();
269     aboutDialog.ShowDialog(this);
270     }
271    
272     private void menuItemNew_Click(object sender, System.EventArgs e)
273     {
274     DummyDoc dummyDoc = CreateNewDocument();
275     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
276     {
277     dummyDoc.MdiParent = this;
278     dummyDoc.Show();
279     }
280     else
281     dummyDoc.Show(dockPanel);
282     }
283    
284     private void menuItemOpen_Click(object sender, System.EventArgs e)
285     {
286     OpenFileDialog openFile = new OpenFileDialog();
287    
288     openFile.InitialDirectory = Application.ExecutablePath;
289     openFile.Filter = "rtf files (*.rtf)|*.rtf|txt files (*.txt)|*.txt|All files (*.*)|*.*";
290     openFile.FilterIndex = 1;
291     openFile.RestoreDirectory = true;
292    
293     if (openFile.ShowDialog() == DialogResult.OK)
294     {
295     string fullName = openFile.FileName;
296     string fileName = Path.GetFileName(fullName);
297    
298     if (FindDocument(fileName) != null)
299     {
300     MessageBox.Show("The document: " + fileName + " has already opened!");
301     return;
302     }
303    
304     DummyDoc dummyDoc = new DummyDoc();
305     dummyDoc.Text = fileName;
306     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
307     {
308     dummyDoc.MdiParent = this;
309     dummyDoc.Show();
310     }
311     else
312     dummyDoc.Show(dockPanel);
313     try
314     {
315     dummyDoc.FileName = fullName;
316     }
317     catch (Exception exception)
318     {
319     dummyDoc.Close();
320     MessageBox.Show(exception.Message);
321     }
322    
323     }
324     }
325    
326     private void menuItemFile_Popup(object sender, System.EventArgs e)
327     {
328     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
329     {
330     menuItemClose.Enabled = menuItemCloseAll.Enabled = (ActiveMdiChild != null);
331     }
332     else
333     {
334     menuItemClose.Enabled = (dockPanel.ActiveDocument != null);
335     menuItemCloseAll.Enabled = (dockPanel.DocumentsCount > 0);
336     }
337     }
338    
339     private void menuItemClose_Click(object sender, System.EventArgs e)
340     {
341     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
342     ActiveMdiChild.Close();
343     else if (dockPanel.ActiveDocument != null)
344     dockPanel.ActiveDocument.DockHandler.Close();
345     }
346    
347     private void menuItemCloseAll_Click(object sender, System.EventArgs e)
348     {
349     CloseAllDocuments();
350     }
351    
352     private void MainForm_Load(object sender, System.EventArgs e)
353     {
354     string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
355    
356     if (File.Exists(configFile))
357     dockPanel.LoadFromXml(configFile, m_deserializeDockContent);
358     }
359    
360     private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
361     {
362     string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
363     if (m_bSaveLayout)
364     dockPanel.SaveAsXml(configFile);
365     else if (File.Exists(configFile))
366     File.Delete(configFile);
367     }
368    
369     private void menuItemToolBar_Click(object sender, System.EventArgs e)
370     {
371     toolBar.Visible = menuItemToolBar.Checked = !menuItemToolBar.Checked;
372     }
373    
374     private void menuItemStatusBar_Click(object sender, System.EventArgs e)
375     {
376     statusBar.Visible = menuItemStatusBar.Checked = !menuItemStatusBar.Checked;
377     }
378    
379     private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
380     {
381     if (e.ClickedItem == toolBarButtonNew)
382     menuItemNew_Click(null, null);
383     else if (e.ClickedItem == toolBarButtonOpen)
384     menuItemOpen_Click(null, null);
385     else if (e.ClickedItem == toolBarButtonSolutionExplorer)
386     menuItemSolutionExplorer_Click(null, null);
387     else if (e.ClickedItem == toolBarButtonPropertyWindow)
388     menuItemPropertyWindow_Click(null, null);
389     else if (e.ClickedItem == toolBarButtonToolbox)
390     menuItemToolbox_Click(null, null);
391     else if (e.ClickedItem == toolBarButtonOutputWindow)
392     menuItemOutputWindow_Click(null, null);
393     else if (e.ClickedItem == toolBarButtonTaskList)
394     menuItemTaskList_Click(null, null);
395     else if (e.ClickedItem == toolBarButtonLayoutByCode)
396     menuItemLayoutByCode_Click(null, null);
397     else if (e.ClickedItem == toolBarButtonLayoutByXml)
398     menuItemLayoutByXml_Click(null, null);
399     else if (e.ClickedItem == toolBarButtonDockPanelSkinDemo)
400     SetDockPanelSkinOptions(!toolBarButtonDockPanelSkinDemo.Checked);
401     }
402    
403     private void menuItemNewWindow_Click(object sender, System.EventArgs e)
404     {
405     MainForm newWindow = new MainForm();
406     newWindow.Text = newWindow.Text + " - New";
407     newWindow.Show();
408     }
409    
410     private void menuItemTools_Popup(object sender, System.EventArgs e)
411     {
412     menuItemLockLayout.Checked = !this.dockPanel.AllowEndUserDocking;
413     }
414    
415     private void menuItemLockLayout_Click(object sender, System.EventArgs e)
416     {
417     dockPanel.AllowEndUserDocking = !dockPanel.AllowEndUserDocking;
418     }
419    
420     private void menuItemLayoutByCode_Click(object sender, System.EventArgs e)
421     {
422     dockPanel.SuspendLayout(true);
423    
424     CloseAllDocuments();
425    
426     m_solutionExplorer = new DummySolutionExplorer();
427     m_propertyWindow = new DummyPropertyWindow();
428     m_toolbox = new DummyToolbox();
429     m_outputWindow = new DummyOutputWindow();
430     m_taskList = new DummyTaskList();
431    
432     m_solutionExplorer.Show(dockPanel, DockState.DockRight);
433     m_propertyWindow.Show(m_solutionExplorer.Pane, m_solutionExplorer);
434     m_toolbox.Show(dockPanel, new Rectangle(98, 133, 200, 383));
435     m_outputWindow.Show(m_solutionExplorer.Pane, DockAlignment.Bottom, 0.35);
436     m_taskList.Show(m_toolbox.Pane, DockAlignment.Left, 0.4);
437    
438     DummyDoc doc1 = CreateNewDocument("Document1");
439     DummyDoc doc2 = CreateNewDocument("Document2");
440     DummyDoc doc3 = CreateNewDocument("Document3");
441     DummyDoc doc4 = CreateNewDocument("Document4");
442     doc1.Show(dockPanel, DockState.Document);
443     doc2.Show(doc1.Pane, null);
444     doc3.Show(doc1.Pane, DockAlignment.Bottom, 0.5);
445     doc4.Show(doc3.Pane, DockAlignment.Right, 0.5);
446    
447     dockPanel.ResumeLayout(true, true);
448     }
449    
450     private void menuItemLayoutByXml_Click(object sender, System.EventArgs e)
451     {
452     dockPanel.SuspendLayout(true);
453    
454     // In order to load layout from XML, we need to close all the DockContents
455     CloseAllContents();
456    
457     Assembly assembly = Assembly.GetAssembly(typeof(MainForm));
458     Stream xmlStream = assembly.GetManifestResourceStream("DockSample.Resources.DockPanel.xml");
459     dockPanel.LoadFromXml(xmlStream, m_deserializeDockContent);
460     xmlStream.Close();
461    
462     dockPanel.ResumeLayout(true, true);
463     }
464    
465     private void menuItemCloseAllButThisOne_Click(object sender, System.EventArgs e)
466     {
467     if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
468     {
469     Form activeMdi = ActiveMdiChild;
470     foreach (Form form in MdiChildren)
471     {
472     if (form != activeMdi)
473     form.Close();
474     }
475     }
476     else
477     {
478     foreach (IDockContent document in dockPanel.DocumentsToArray())
479     {
480     if (!document.DockHandler.IsActivated)
481     document.DockHandler.Close();
482     }
483     }
484     }
485    
486     private void menuItemShowDocumentIcon_Click(object sender, System.EventArgs e)
487     {
488     dockPanel.ShowDocumentIcon = menuItemShowDocumentIcon.Checked = !menuItemShowDocumentIcon.Checked;
489     }
490    
491     private void showRightToLeft_Click(object sender, EventArgs e)
492     {
493     CloseAllContents();
494     if (showRightToLeft.Checked)
495     {
496     this.RightToLeft = RightToLeft.No;
497     this.RightToLeftLayout = false;
498     }
499     else
500     {
501     this.RightToLeft = RightToLeft.Yes;
502     this.RightToLeftLayout = true;
503     }
504     m_solutionExplorer.RightToLeftLayout = this.RightToLeftLayout;
505     showRightToLeft.Checked = !showRightToLeft.Checked;
506     }
507    
508     private void exitWithoutSavingLayout_Click(object sender, EventArgs e)
509     {
510     m_bSaveLayout = false;
511     Close();
512     m_bSaveLayout = true;
513     }
514    
515     #endregion
516     }
517     }

  ViewVC Help
Powered by ViewVC 1.1.22