⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 C#实现DHTML编辑器功能!
💻 CS
📖 第 1 页 / 共 4 页
字号:
			{
#if DEBUG
				System.Diagnostics.Debug.Assert(tp != null, "XML_editor.MainForms.MainForm.cs :: GetTabPageByFileName() => tp == null");
#endif
				if (tp.TextAreaControl.FileName == fullFileName)
				{
					return tp;
				}
			}
			return null;
		}

		#region Otvaranie, Ukladanie a Zatvaranie suborov

		// -------------------------------------------------------------------------
		/// <summary>
		/// Ulozi obsah dokumentu, pod novym menom.
		/// </summary>
		/// <param name="tab"><see cref="XML_editor.TabPages.TextAreaTabPage"/>, ktory sa ma ulozit</param>
		/// <returns><c>true</c> = ak bol subor ulozeny ; <c>false</c> = ak nebol ulozeny (uzivatel dal <c>Cancel</c>)</returns>
		public bool FileSaveAs(XML_editor.TabPages.TextAreaTabPage tab) 
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Title = "Save '" + tab.TitleToShow + "' As";
			dialog.Filter = IconProvider.FileOpenFilter; //"XML files (*.xml)|*.xml|XSLT files (*.xsl)|*.xsl|All files (*.*)|*.*"  ;
			dialog.RestoreDirectory = true ;
 
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				System.IO.FileInfo fileInfo = new System.IO.FileInfo(dialog.FileName);
				//			TODO: DLTextEditor.LineTerminatorStyle.Windows ---
				tab.TextAreaControl.SaveFile(fileInfo.FullName, DLTextEditor.LineTerminatorStyle.Windows);
				return true;
			} 
			else 
			{
				return false;
			}
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Ulozi obsah dokumentu, pod
		/// rovnakym menom ake uz ma.
		/// </summary>
		/// <param name="tab"><see cref="XML_editor.TabPages.TextAreaTabPage"/>, ktory sa ma ulozit</param>
		/// <returns><c>true</c> = ak bol subor ulozeny ; <c>false</c> = ak nebol ulozeny (uzivatel dal <c>Cancel</c>)</returns>
		public bool FileSave(XML_editor.TabPages.TextAreaTabPage tab) 
		{
			if (tab.TextAreaHasChanges == false) 
			{
				return true;
			}
			System.IO.FileInfo fileInfo = new System.IO.FileInfo(tab.TextAreaControl.FileName);
			if (fileInfo.Exists == false) 
			{
				return this.FileSaveAs(tab);
			}
			//			TODO: DLTextEditor.LineTerminatorStyle.Windows ---
			tab.TextAreaControl.SaveFile(tab.TextAreaControl.FileName, DLTextEditor.LineTerminatorStyle.Windows);
			return true;
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Ulozi obsah vsetkych dokumentov.
		/// </summary>
		/// <returns><c>true</c> = ak boli vsetky subory ulozenene ; <c>false</c> = ak nebol aspon jeden dokument ulozeny (uzivatel dal <c>Cancel</c>)</returns>
		public bool FileSaveAll() 
		{
			bool result = true;
			foreach (XML_editor.TabPages.TextAreaTabPage tab in this.tabControl.TabPages) 
			{
				if (this.FileSave(tab) == false) 
				{
					result = false;
				}
			}
			return result;
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Zavrie aktualne zvoleny editor.
		/// </summary>
		/// <param name="tab"><see cref="XML_editor.TabPages.TextAreaTabPage"/>, ktory sa ma zatvorit</param>
		/// <returns><c>true</c> = ak bol subor zatvoreny ; <c>false</c> = ak nebol zatvoreny (uzivatel dal <c>Cancel</c>)</returns>
		public bool FileClose(XML_editor.TabPages.TextAreaTabPage tab) 
		{
			if (tab.TextAreaHasChanges == true) 
			{
				DialogResult dr = MessageBox.Show("File " + tab.TitleToShow + " is modified. Save changes ?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

				switch (dr) 
				{
					case DialogResult.Yes:
						if (this.FileSave(tab) == true) 
						{
							this.tabControl.TabPages.Remove(tab);
#if DEBUG
							System.Diagnostics.Debug.Assert(tab != null);
#endif
							this.OnTextAreaControlClosed(tab);
							return true;
						}
						return false;

					case DialogResult.No:
						this.tabControl.TabPages.Remove(tab);
#if DEBUG
						System.Diagnostics.Debug.Assert(tab != null);
#endif
						this.OnTextAreaControlClosed(tab);
            return true;

					case DialogResult.Cancel:
						return false;

					default:
#if DEBUG
						throw new Exception("XML_editor.MyForms.MainForm.cs : FileClose() => Unknown DialogResult");
#else
						return false;
#endif
				}
			}
			else 
			{
				this.tabControl.TabPages.Remove(tab);
#if DEBUG
				System.Diagnostics.Debug.Assert(tab != null);
#endif
				this.OnTextAreaControlClosed(tab);
				return true;
			}
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Zavrie vsetky subory v aplikacii.
		/// </summary>
		/// <returns><c>true</c> = ak boli zatvorene vsetky subory ; <c>false</c> = ak neboli zatvorene vsetky subory (uzivatel dal <c>Cancel</c>)</returns>
		public bool FileCloseAll() 
		{
			// fsetky - skusim savenut
			foreach (XML_editor.TabPages.TextAreaTabPage tab in this.tabControl.TabPages)
			{
				if (tab.TextAreaHasChanges == true) 
				{
					DialogResult dr = MessageBox.Show("File " + tab.TitleToShow + " is modified. Save changes ?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

					switch (dr) 
					{
						case DialogResult.Yes:
							this.FileSave(tab);
							break;

						case DialogResult.No:
							break;

						case DialogResult.Cancel:
							return false;

						default:
#if DEBUG
							throw new Exception("XML_editor.MyForms.MainForm.cs : FileCloseAll() => Unknown DialogResult");
#else
						break;
#endif
					}
				}
			} // foreach

			// Podarilo sa vsetko ulozit
			this.tabControl.TabPages.Clear();
			this.OnTextAreaControlClosed(null);
			return true;
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Vytvori novy editor
		/// </summary>
		public void FileNew() 
		{
			const string fileNameBase = "edit";
			bool end = false;
			int count = 1;
			do
			{
				end = false;
				foreach (Crownwood.Magic.Controls.TabPage tp in this.tabControl.TabPages)
				{
					DLTextEditor.TextAreaControl tac = tp.Control as DLTextEditor.TextAreaControl;
#if DEBUG
					System.Diagnostics.Debug.Assert(tac != null, "XML_editor.MainForms.MainForm.cs :: Clicked_FileNew() => tac == null");
#endif
					if (tac.FileName.ToLower() == (fileNameBase + count.ToString()))
					{
						count++;
						end = true;
					}
				}
			} while (end == true);

			XML_editor.TabPages.TextAreaTabPage t = new XML_editor.TabPages.TextAreaTabPage((fileNameBase + count.ToString()), IconProvider.GetIconForFileName(this, String.Empty));
			t.IsNewFile = true;
			Crownwood.Magic.Controls.TabPage tabPage = t;
			tabPage.Selected = true;
			this.tabControl.TabPages.Add(tabPage);
			this.actualTextAreaControl = this.SelectedTabPage.TextAreaControl;
			this.actualTextAreaControl.Document.Caret.OffsetChanged += new DLTextEditor.Document.Caret.CaretEventHandler(CaretChanged);
			this.actualTextAreaControl.Document.Caret.CaretModeChanged += new DLTextEditor.Document.Caret.CaretEventHandler(CaretChanged);
			this.actualTextAreaControl.ContextMenuToShow += new DLTextEditor.ContextMenuToShowEventHandler(OnContextMenuToShow);
#if DEBUG			
			if (DLTextEditor.Document.HightlightingStrategy.HighlightingStrategyFactory.CreateHighlightingStrategy("XML") == null) 
			{
				System.Diagnostics.Debug.WriteLine("HightlightingStrategy NOT FOUND");
			}
#endif
			this.actualTextAreaControl.Document.HighlightingStrategy = DLTextEditor.Document.HightlightingStrategy.HighlightingStrategyFactory.CreateHighlightingStrategy("XML");
		}


		// -------------------------------------------------------------------------
		/// <summary>
		/// Otvori subor do editor-a
		/// </summary>
		public void FileOpen()
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.Filter = IconProvider.FileOpenFilter;// "XML files (*.xml)|*.xml|XSLT files (*.xsl)|*.xsl|Scalable Vector Graphics files (*.svg)|*.svg|All files (*.*)|*.*";
			dialog.Title = "Open File";
			
			if (dialog.ShowDialog() == DialogResult.OK) 
			{
				System.IO.FileInfo fileInfo = new System.IO.FileInfo(dialog.FileName);
				if (fileInfo.Exists == false) 
				{
					return;
				}
				
				foreach (XML_editor.TabPages.TextAreaTabPage tab in this.tabControl.TabPages)
				{
					if (tab.TextAreaControl.FileName == fileInfo.FullName) 
					{
						//this.tabControl.MakePageVisible(1);
						tab.Selected = true;
						return;
					}
				}

				DLTextEditor.TextAreaControl txt = new DLTextEditor.TextAreaControl();
				txt.LoadFile(fileInfo.FullName);
				XML_editor.TabPages.TextAreaTabPage t = new XML_editor.TabPages.TextAreaTabPage(fileInfo.Name, IconProvider.GetIconForFileName(this, fileInfo.FullName), txt);
				t.IsNewFile = false;
				Crownwood.Magic.Controls.TabPage tabPage = t;
				tabPage.Selected = true;
				this.tabControl.TabPages.Add(tabPage);
				this.ActualTextAreaControl = txt;
				txt.Document.Caret.OffsetChanged += new DLTextEditor.Document.Caret.CaretEventHandler(CaretChanged);
				txt.Document.Caret.CaretModeChanged += new DLTextEditor.Document.Caret.CaretEventHandler(CaretChanged);
				txt.ContextMenuToShow += new DLTextEditor.ContextMenuToShowEventHandler(OnContextMenuToShow);

#if DEBUG			
				if (DLTextEditor.Document.HightlightingStrategy.HighlightingStrategyFactory.CreateHighlightingStrategy("XML") == null) 
				{
					System.Diagnostics.Debug.WriteLine("HightlightingStrategy NOT FOUND");
				}
#endif
				txt.Document.HighlightingStrategy = DLTextEditor.Document.HightlightingStrategy.HighlightingStrategyFactory.CreateHighlightingStrategy("XML");
			}
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Otvori (zvoleny) subor do editor-a. Ak je otvoreny tak ho reloadne (bez savenutia)
		/// </summary>
		/// <param name="fullFileName">Subor (aj s plnou cestou), ktory sa ma otvorit</param>
		public void FileOpen(string fullFileName)
		{
			System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullFileName);
			if (fileInfo.Exists == false) 
			{
				return;
			}
				
			foreach (XML_editor.TabPages.TextAreaTabPage tab in this.tabControl.TabPages)
			{
				if (tab.TextAreaControl.FileName == fileInfo.FullName) 
				{
					//this.tabControl.MakePageVisible(1);
					tab.Selected = true;
					tab.TextAreaControl.LoadFile(tab.TextAreaControl.FileName);
					return;
				}
			}

			DLTextEditor.TextAreaControl txt = new DLTextEditor.TextAreaControl();
			txt.LoadFile(fileInfo.FullName);
			XML_editor.TabPages.TextAreaTabPage t = new XML_editor.TabPages.TextAreaTabPage(fileInfo.Name, IconProvider.GetIconForFileName(this, fileInfo.FullName), txt);
			t.IsNewFile = false;
			Crownwood.Magic.Controls.TabPage tabPage = t;
			tabPage.Selected = true;
			this.tabControl.TabPages.Add(tabPage);
			this.ActualTextAreaControl = txt;
			txt.Document.Caret.OffsetChanged += new DLTextEditor.Document.Caret.CaretEventHandler(CaretChanged);
			txt.Document.Caret.CaretModeChanged += new DLTextEditor.Document.Caret.CaretEventHandler(CaretChanged);
			txt.ContextMenuToShow += new DLTextEditor.ContextMenuToShowEventHandler(OnContextMenuToShow);

#if DEBUG			
			if (DLTextEditor.Document.HightlightingStrategy.HighlightingStrategyFactory.CreateHighlightingStrategy("XML") == null) 
			{
				System.Diagnostics.Debug.WriteLine("HightlightingStrategy NOT FOUND");
			}
#endif
			txt.Document.HighlightingStrategy = DLTextEditor.Document.HightlightingStrategy.HighlightingStrategyFactory.CreateHighlightingStrategy("XML");
		}

		#endregion

		#region Eventy ktore 'vyhadzuje' tato trieda

		// -------------------------------------------------------------------------
		/// <summary>
		/// Vyvola udalost <see cref="ActualTextAreaControlChanged"/>. Metoda sa
		/// vola PO zmene aktualneho <see cref="ActualTextAreaControl"/>
		/// - teda ked uzivatel prepne na inu zalozku.
		/// <seealso cref="ActualTextAreaControlChanged"/>
		/// <seealso cref="ActualTextAreaControl"/>
		/// </summary>
		/// <remarks>
		/// Tato metoda vyvola udalost volanim 'event handleru' cez delegata.
		/// Viac informacii, pozri <c>Raising an Event</c> v .NET manuali.
		/// </remarks>
		protected virtual void OnActualTextAreaControlChanged()
		{
			if (this.ActualTextAreaControlChanged != null) 
			{
				this.ActualTextAreaControlChanged(this, null);
			}
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Vyvola sa, PO zmene aktualneho <see cref="ActualTextAreaControl"/>
		/// - teda ked uzivatel prepne na inu zalozku.
		/// <seealso cref="OnActualTextAreaControlChanged"/>
 		/// <seealso cref="ActualTextAreaControl"/>
		/// </summary>
		public event EventHandler ActualTextAreaControlChanged;

		// -------------------------------------------------------------------------
		/// <summary>
		/// Vyvola udalost <see cref="TextAreaControlClosed"/>. Metoda sa
		/// vola PO zatvoreni <see cref="DLTextEditor.TextAreaControl"/>
		/// <seealso cref="TextAreaControlClosed"/>
		/// </summary>
		/// <param name="tab"><see cref="XML_editor.TabPages.TextAreaTabPage"/>, ktora sa zatvara</param>
		/// <remarks>
		/// Tato metoda vyvola udalost volanim 'event handleru' cez delegata.
		/// Viac informacii, pozri <c>Raising an Event</c> v .NET manuali.
		/// </remarks>
		protected virtual void OnTextAreaControlClosed(XML_editor.TabPages.TextAreaTabPage tab)
		{
			System.Diagnostics.Debug.WriteLine("CLOSED");
			if (this.TextAreaControlClosed != null) 
			{
				this.TextAreaControlClosed(tab);
			}
		}

		// -------------------------------------------------------------------------
		/// <summary>
		/// Vyvola sa, PO zatvoreni editore - <see cref="DLTextEditor.TextAreaControl"/>
		/// <seealso cref="TextAreaControlClosedHandler"/>
		/// <seealso cref="OnTextAreaControlClosed"/>
		/// </summary>
		public event TextAreaControlClosedHandler TextAreaControlClosed;
		

		#endregion


	} // public class MainForm : ...
} // namespace XML_editor.MyForms

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -