PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/editors/EditorNotebook.cs

https://github.com/moscrif/ide
C# | 540 lines | 376 code | 86 blank | 78 comment | 98 complexity | cafb5addff8cd45064b4be57575024ff MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Moscrif.IDE.Components;
  4. using Moscrif.IDE.Controls;
  5. using Mono.TextEditor;
  6. using Mono.TextEditor.Highlighting;
  7. using Gtk;
  8. using System.Collections.Generic;
  9. using MessageDialogs = Moscrif.IDE.Controls.MessageDialog;
  10. using Moscrif.IDE.Tool;
  11. using Moscrif.IDE.Iface.Entities;
  12. using System.Threading;
  13. namespace Moscrif.IDE.Editors
  14. {
  15. public delegate void PageIsChangedHandler (string fileName);
  16. public class EditorNotebook : DragNotebook
  17. {
  18. private List<IEditor> listEditor = null;
  19. private SearchPattern searchPatern = null;
  20. public bool OnLoadFinish = false;
  21. public event PageIsChangedHandler PageIsChanged;
  22. public EditorNotebook()
  23. {
  24. listEditor = new List<IEditor>();
  25. this.Scrollable = true;
  26. ButtonPressEvent += new ButtonPressEventHandler(OnButtonPress);
  27. ButtonReleaseEvent += new ButtonReleaseEventHandler(OnButtonRelease);
  28. this.CanFocus = false;
  29. this.SwitchPage += new SwitchPageHandler(OnSwitchPage);
  30. /* filllStartPageThread = new Thread(new ThreadStart(ExecEditorThread));
  31. //filllStartPageThread.Priority = ThreadPriority.Normal;
  32. filllStartPageThread.Name = "ExecEditorThread";
  33. filllStartPageThread.IsBackground = true;
  34. //filllStartPageThread.Start();*/
  35. }
  36. /* private void ExecEditorThread(){
  37. bool play = true;
  38. bool isBussy = false;
  39. try {
  40. //Gtk.Application.Invoke(delegate
  41. //{
  42. while (play){
  43. if ((listEditor != null)&& !isBussy) {
  44. isBussy = true;
  45. //lock (secondTaskList) {
  46. Widget wdt = this.CurrentPageWidget;
  47. IEditor se = listEditor.Find(x => x.Control == wdt);
  48. if(se!=null)
  49. Console.WriteLine(se.FileName);
  50. //}
  51. isBussy = false;
  52. }
  53. Thread.Sleep (2000);
  54. }
  55. //});
  56. }catch(ThreadAbortException tae){
  57. Thread.ResetAbort ();
  58. Tool.Logger.Error("ERROR - Cannot run editor thread.");
  59. Tool.Logger.Error(tae.Message);
  60. }finally{
  61. }
  62. }*/
  63. //[GLib.ConnectBefore]
  64. void OnSwitchPage(object o, SwitchPageArgs args) {
  65. IEditor se = CurentEditor();
  66. if (se != null){
  67. se.ActivateEditor(OnLoadFinish);
  68. if(PageIsChanged != null)
  69. PageIsChanged(se.FileName);
  70. }
  71. }
  72. [GLib.ConnectBefore]
  73. void OnButtonPress(object obj, ButtonPressEventArgs args)
  74. {
  75. int tab = this.FindTabAtPosition(args.Event.XRoot, args.Event.YRoot);
  76. if (tab < 0)
  77. return;
  78. this.CurrentPage = tab;
  79. //if (e.Event.Type == Gdk.EventType.TwoButtonPress){}
  80. }
  81. [GLib.ConnectBefore]
  82. void OnButtonRelease(object obj, ButtonReleaseEventArgs args)
  83. {
  84. int tab = FindTabAtPosition(args.Event.XRoot, args.Event.YRoot);
  85. if (tab < 0)
  86. return;
  87. if (args.Event.Button == 3) {
  88. Menu popupMenu = (Menu)MainClass.MainWindow.ActionUiManager.GetWidget("/netbookPagePopup");
  89. if (popupMenu != null) {
  90. popupMenu.ShowAll();
  91. popupMenu.Popup();
  92. }
  93. }
  94. if (args.Event.Button == 2) {
  95. IEditor ie = FindEditor(tab);
  96. if (ie != null)
  97. this.ClosePage(ie);
  98. }
  99. }
  100. public IEditor Open(string path)
  101. {
  102. path = Tool.FileUtility.GetSystemPath(path);
  103. if (!System.IO.File.Exists(path) && (path != "StartPage") ){
  104. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, MainClass.Languages.Translate("file_not_exist"), path, MessageType.Error,null);
  105. md.ShowDialog();
  106. return null;
  107. }
  108. string ext = System.IO.Path.GetExtension(path);
  109. /*if(ext.ToLower() ==".ttf"){
  110. if (!String.IsNullOrEmpty(path)){
  111. System.Diagnostics.Process.Start(path);
  112. }
  113. return null;
  114. }*/
  115. IEditor se = FindEditor(path);
  116. if (se != null) {
  117. this.CurrentPage = this.PageNum(se.Control);
  118. return null;
  119. }
  120. ExtensionSetting exSet =MainClass.Tools.FindFileTyp(ext);
  121. if(exSet == null){
  122. if (path == "StartPage"){
  123. se = new StartPage(path);
  124. //return null;
  125. } else {// if (!String.IsNullOrEmpty(path)){
  126. System.Diagnostics.Process.Start(path);
  127. return null;
  128. }
  129. }
  130. try {
  131. if(se == null){
  132. switch (exSet.OpenType) {
  133. case ExtensionSetting.OpenTyp.IMAGE:
  134. se = new ImageEditor(path);
  135. break;
  136. case ExtensionSetting.OpenTyp.DATABASE:
  137. se = new DatabaseEditor(path);
  138. break;
  139. case ExtensionSetting.OpenTyp.TEXT:
  140. se = new SourceEditor(path);
  141. break;
  142. case ExtensionSetting.OpenTyp.SYSTEM:{
  143. System.Diagnostics.Process.Start(path);
  144. return null;
  145. }
  146. case ExtensionSetting.OpenTyp.EXTERNAL:{
  147. MainClass.MainWindow.RunProcess(exSet.ExternalProgram, exSet.Parameter+" "+path, "", false,null);
  148. return null;
  149. }
  150. default:
  151. se = new SourceEditor(path);
  152. break;
  153. }
  154. /*switch (extension) {
  155. case ".png":
  156. case ".jpg":
  157. case ".jpeg":
  158. case ".bmp":
  159. case ".gif":
  160. case ".tif":
  161. case ".svg":
  162. se = new ImageEditor(path);
  163. break;
  164. case ".db":
  165. se = new DatabaseEditor(path);
  166. break;
  167. //case ".html":
  168. // se = new WebViewer(path);
  169. // break;
  170. default:
  171. se = new SourceEditor(path);
  172. break;
  173. }*/
  174. }
  175. } catch(Exception ex) {
  176. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.Ok, ex.Message, path, MessageType.Error,null);
  177. md.ShowDialog();
  178. Logger.Error(ex.Message);
  179. Logger.Error(ex.Source);
  180. Logger.Error(ex.StackTrace);
  181. if (ex.InnerException != null){
  182. Logger.Error(ex.InnerException.Message);
  183. Logger.Error(ex.InnerException.Source);
  184. Logger.Error(ex.InnerException.StackTrace);
  185. }
  186. return null;
  187. }
  188. //se.Caption = System.IO.Path.GetFileName(path);
  189. listEditor.Add(se);
  190. NotebookEditorLabel nl = new NotebookEditorLabel(this, se);
  191. int i = this.AppendPage(se.Control, nl);
  192. se.ModifiedChanged += delegate(object sender, ModifiedChangedEventArgs e) {
  193. //IEditor se = IEditor(sender);
  194. //NotebookLabel nl = (NotebookLabel)this.GetTabLabel(se);
  195. if (nl != null) nl.SetSaveState(e.State);
  196. };
  197. se.WriteStatusChange += delegate(object sender, WriteStatusEventArgs e) {
  198. //Console.WriteLine(e.Message);
  199. MainClass.MainWindow.PushText(e.Message);
  200. };
  201. this.CurrentPage = i;
  202. return se;
  203. }
  204. public void GoToFile(string filename, object position){
  205. IEditor se = FindEditor(filename);
  206. if (se != null) {
  207. this.CurrentPage = this.PageNum(se.Control);
  208. se.GoToPosition(position);
  209. return;
  210. } else {
  211. IEditor se2 =Open(filename);
  212. se2.ActivateEditor(true);
  213. GoToCurrentFile(position);
  214. }
  215. }
  216. public void GoToCurrentFile(object position){
  217. IEditor se = CurentEditor();
  218. if (se != null) {
  219. se.GoToPosition(position);
  220. return;
  221. }
  222. }
  223. public void ClosePage(string fileName, bool forceClose){
  224. IEditor se = FindEditor(fileName);//CurentEditor();
  225. if (se != null) {
  226. if (forceClose){
  227. listEditor.Remove(se);
  228. this.Remove(se.Control);
  229. } else {
  230. ClosePage(se);
  231. }
  232. }
  233. }
  234. public bool ClosePage(IEditor se)
  235. {
  236. if (!se.Modified) {
  237. listEditor.Remove(se);
  238. se.Close();
  239. this.Remove(se.Control);
  240. } else {
  241. MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.YesNoCancel, MainClass.Languages.Translate("save_changes_before_close", se.Caption), MainClass.Languages.Translate("changes_will_be_lost"), MessageType.Question);
  242. //MessageDialogs md = new MessageDialogs(MessageDialogs.DialogButtonType.YesNo, MainClass.Languages.Translate("save_changes_before_close", se.Caption), MainClass.Languages.Translate("changes_will_be_lost"), MessageType.Question);
  243. int response = md.ShowDialog();
  244. if (response == (int)Gtk.ResponseType.Cancel) {
  245. return false;
  246. } else if (response == (int)Gtk.ResponseType.No) {
  247. listEditor.Remove(se);
  248. se.Close();
  249. this.Remove(se.Control);
  250. } else if (response == (int)Gtk.ResponseType.Yes) {
  251. if (Save(se)){
  252. listEditor.Remove(se);
  253. se.Close();
  254. this.Remove(se.Control);
  255. }
  256. }
  257. }
  258. return true;
  259. }
  260. public bool CloseAllPage()
  261. {
  262. int count = listEditor.Count;
  263. for (int i = count - 1; i > -1; i--){
  264. // if (listEditor[i].FileName != "StartPage")
  265. if(!ClosePage(listEditor[i])){
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. public void RefreshSetting(){
  272. int count = listEditor.Count;
  273. for (int i = count - 1; i > -1; i--){
  274. // if (listEditor[i].FileName != "StartPage")
  275. listEditor[i].RefreshSettings() ;
  276. }
  277. }
  278. public void CloseCurentPage()
  279. {
  280. IEditor se = CurentEditor();
  281. if (se != null)
  282. ClosePage(se);
  283. }
  284. public void CloseAllButThisPage()
  285. {
  286. IEditor se = CurentEditor();
  287. if (se != null) {
  288. int count = listEditor.Count;
  289. for (int i = count - 1; i > -1; i--)
  290. if (listEditor[i] != se)
  291. ClosePage(listEditor[i]);
  292. }
  293. }
  294. public object GetSelectedObject(){
  295. IEditor se = CurentEditor();
  296. if (se != null){
  297. return se.GetSelected();
  298. }
  299. else return null;
  300. }
  301. public void SetSearchExpresion(SearchPattern expresion)
  302. {
  303. searchPatern = expresion;
  304. }
  305. public void Search(SearchPattern expresion)
  306. {
  307. searchPatern = expresion;
  308. IEditor se = CurentEditor();
  309. if (se != null)
  310. se.SearchExpression(expresion);
  311. }
  312. public void SearchNext()
  313. {
  314. IEditor se = CurentEditor();
  315. if (se != null) {
  316. //Console.WriteLine(searchPatern.Expresion);
  317. se.SearchNext(searchPatern);
  318. }
  319. }
  320. public void SearchPreviu()
  321. {
  322. IEditor se = CurentEditor();
  323. if (se != null)
  324. se.SearchPreviu(searchPatern);
  325. }
  326. public void Replace(SearchPattern expresion)
  327. {
  328. IEditor se = CurentEditor();
  329. if (se != null)
  330. se.Replace(expresion);
  331. }
  332. public void ReplaceAll(SearchPattern expresion)
  333. {
  334. IEditor se = CurentEditor();
  335. if (se != null)
  336. se.ReplaceAll(expresion);
  337. }
  338. public void Undo()
  339. {
  340. IEditor se = CurentEditor();
  341. if (se != null)
  342. se.Undo();
  343. }
  344. public void Redo()
  345. {
  346. IEditor se = CurentEditor();
  347. if (se != null)
  348. se.Redo();
  349. }
  350. public void SaveCurentPage()
  351. {
  352. IEditor se = CurentEditor();
  353. if (se != null)
  354. Save(se);
  355. }
  356. public void SaveAllPage()
  357. {
  358. foreach (IEditor se in listEditor)
  359. Save(se);
  360. }
  361. public void RenameFile(string oldFilePath,string newFilePath){
  362. IEditor se = FindEditor(oldFilePath);
  363. if (se != null) {
  364. se.Rename(newFilePath);
  365. Widget nl = this.GetTabLabel(se.Control);
  366. if( nl!=null && nl.GetType() == typeof(NotebookEditorLabel) )
  367. {
  368. (nl as NotebookEditorLabel).SetNewName(newFilePath);
  369. }
  370. Save(se);
  371. }
  372. }
  373. public void SaveAs()
  374. {
  375. IEditor se = CurentEditor();
  376. SaveAs(se);
  377. }
  378. public Gtk.ActionGroup EditorAction
  379. {
  380. get { return CurentEditor().EditorAction; }
  381. }
  382. public List<string> OpenFiles
  383. {
  384. get { return listEditor.Select(i => i.FileName).ToList(); }
  385. }
  386. public string CurrentFile {
  387. get {
  388. IEditor se = CurentEditor();
  389. if (se!= null)
  390. return se.FileName;
  391. return null;
  392. }
  393. }
  394. public IEditor FindEditor(string filePath)
  395. {
  396. filePath= Tool.FileUtility.GetSystemPath(filePath);
  397. if (MainClass.Platform.IsWindows){
  398. return listEditor.Find(x => x.FileName.ToLower() == filePath.ToLower());
  399. } else{
  400. return listEditor.Find(x => x.FileName == filePath);
  401. }
  402. }
  403. #region private
  404. private IEditor FindEditor(int i)
  405. {
  406. Widget wdt = this.GetNthPage(i);
  407. IEditor se = listEditor.Find(x => x.Control == wdt);
  408. return se;
  409. }
  410. private IEditor CurentEditor()
  411. {
  412. Widget wdt = this.CurrentPageWidget;
  413. IEditor se = listEditor.Find(x => x.Control == wdt);
  414. return se;
  415. }
  416. private bool Save(IEditor se){
  417. bool saveStatus =se.Save();
  418. if (!saveStatus){
  419. saveStatus = SaveAs(se);
  420. }
  421. return saveStatus;
  422. }
  423. private bool SaveAs(IEditor se){
  424. bool saveStatus = false;
  425. string filePath = se.FileName;
  426. var dlg = new Gtk.FileChooserDialog("Save as...",MainClass.MainWindow, FileChooserAction.Save,"Cancel", ResponseType.Cancel, "Save", ResponseType.Accept);
  427. if (filePath != null){
  428. string fileName = System.IO.Path.GetFileName(filePath);
  429. string fileDir = System.IO.Path.GetDirectoryName(filePath);
  430. dlg.CurrentName = fileName;
  431. dlg.SetCurrentFolder(fileDir);
  432. }
  433. if (dlg.Run() == (int)ResponseType.Accept){
  434. saveStatus= se.SaveAs(dlg.Filename);
  435. NotebookEditorLabel nl = (NotebookEditorLabel)this.GetTabLabel(se.Control);
  436. if (nl != null){
  437. nl.SetNewName(se.FileName);
  438. }
  439. }
  440. dlg.Destroy();
  441. return saveStatus;
  442. }
  443. #endregion
  444. /* [GLib.ConnectBefore]
  445. protected override void OnChangeCurrentPage(int offset)
  446. {
  447. base.OnChangeCurrentPage(offset);
  448. Console.WriteLine(offset);
  449. }*/
  450. }
  451. }