private bool fileOperation(string cmd, string filename) { if (cmd != null) { SaveFileDialog dialog; #region add if (cmd == "add") { if (m_app != null && m_app.ChangeApp) { switch (MessageBox.Show(" Do you want to save changes made? ".Translate(), "Confirm".Translate(), MessageBoxButtons.YesNoCancel)) { case DialogResult.Yes: if (!fileOperation("save", "")) return false; break; case DialogResult.Cancel: return false; } } dialog = new SaveFileDialog(); dialog.Filter = "HMI File|*.HMI|All File|*.*".Translate(); Utility.SetInitialPath(dialog, "file"); if (dialog.ShowDialog() != DialogResult.OK) return false; closeHMI(); m_app = new HmiApplication(); m_app.ChangeApplication = new HmiApplication.AppChangEvent(m_app_ChangeEvent); m_app.FileSave = new HmiApplication.AppFileSave(m_app_FileSaveEvent); m_app.ChangeApplication(false); m_app.AddPage(); if (new DeviceParameters(m_app).ShowDialog() != DialogResult.OK) return false; runScreen.RunStop(); m_openFile = dialog.FileName; Utility.SavePath(dialog, "file"); m_app.SaveFile(m_openFile, false, null); closeHMI(); m_app = new HmiApplication(); m_app.ChangeApplication = new HmiApplication.AppChangEvent(m_app_ChangeEvent); m_app.FileSave = new HmiApplication.AppFileSave(m_app_FileSaveEvent); m_app.ChangeApplication(false); if (!m_app.Open(m_openFile)) return false; if (!Utility.DeleteFileWait(HmiOptions.RunFilePath)) return false; File.Copy(m_openFile, HmiOptions.RunFilePath); runScreen.GuiInit(HmiOptions.RunFilePath, m_app, true); runScreen.Visible = true; pageAdmin.SetAppInfo(m_app); picAdmin.SetAppInfo(m_app); fontAdmin.SetAppInfo(m_app); pageAdmin.RefreshObject(0); picAdmin.RefreshPictures(); fontAdmin.RefreshFonts(); mi_Compile.Enabled = true; mi_AddComponent.Enabled = true; mi_DeleteComponent.Enabled = true; mi_Resolution.Enabled = true; mi_ID.Enabled = true; mi_XY.Enabled = true; } #endregion #region open else if (cmd == "open") { if (m_app != null && m_app.ChangeApp) { switch (MessageBox.Show( " Do you want to save changes made? ".Translate(), "Confirm".Translate(), MessageBoxButtons.YesNoCancel )) { case DialogResult.Yes: if (!fileOperation("save", "")) return false; break; case DialogResult.Cancel: return false; } } OpenFileDialog op = new OpenFileDialog(); if (filename == "") { op.Filter = "HMI File|*.HMI|All File|*.*".Translate(); Utility.SetInitialPath(op, "file"); if (op.ShowDialog() != DialogResult.OK) return false; Utility.SavePath(op, "file"); m_openFile = op.FileName; } else m_openFile = filename; closeHMI(); m_app = new HmiApplication(); m_app.ChangeApplication = new HmiApplication.AppChangEvent(m_app_ChangeEvent); m_app.FileSave = new HmiApplication.AppFileSave(m_app_FileSaveEvent); m_app.ChangeApplication(false); if (!m_app.Open(m_openFile)) return false; if (!Utility.DeleteFileWait(HmiOptions.RunFilePath)) return false; File.Copy(m_openFile, HmiOptions.RunFilePath); runScreen.GuiInit(HmiOptions.RunFilePath, m_app, true); runScreen.Visible = true; pageAdmin.SetAppInfo(m_app); picAdmin.SetAppInfo(m_app); fontAdmin.SetAppInfo(m_app); pageAdmin.RefreshObject(0); picAdmin.RefreshPictures(); fontAdmin.RefreshFonts(); mi_Compile.Enabled = true; mi_AddComponent.Enabled = true; mi_DeleteComponent.Enabled = true; mi_Resolution.Enabled = true; mi_ID.Enabled = true; mi_XY.Enabled = true; } #endregion #region save / savexml else if (cmd == "save" || cmd == "savexml") { if (m_app == null || m_openFile == null || HmiOptions.RunFilePath == null ) return false; runScreen.PauseScreen(); if (cmd == "save") { m_compiler.SaveCodes(); m_binpath = ""; if (!m_app.SaveFile(HmiOptions.RunFilePath, false, null)) { runScreen.StartFile(); return false; } if (!Utility.DeleteFileWait(m_openFile)) { runScreen.StartFile(); return false; } File.Copy(HmiOptions.RunFilePath, m_openFile); } else { dialog = new SaveFileDialog(); dialog.Filter = "XML File|*.xml|All File|*.*".Translate(); dialog.FileName = Path.GetFileNameWithoutExtension(m_openFile) + ".xml"; Utility.SetInitialPath(dialog, "file"); if (dialog.ShowDialog() != DialogResult.OK) return false; try { if (File.Exists(dialog.FileName)) File.Delete(dialog.FileName); using (TextWriter writer = new StreamWriter(dialog.FileName, false, Encoding.UTF8)) { XmlSerializer xs = new XmlSerializer(runScreen.GuiApp.GetType()); xs.Serialize(writer, runScreen.GuiApp); writer.Close(); } // xdoc.Save(dialog.FileName); } catch(Exception ex) { MessageBox.Show("Export XML:".Translate() + "\n" + ex.Message); } } runScreen.StartFile(); m_app.ChangeApplication(false); } #endregion #region compile else if (cmd == "compile") { if (m_app == null || m_openFile == null) return false; if (m_app.ChangeApp) { runScreen.PauseScreen(); m_compiler.SaveCodes(); if (!m_app.SaveFile(HmiOptions.RunFilePath, false, null)) { runScreen.StartFile(); return false; } if (!Utility.DeleteFileWait(m_openFile)) { runScreen.StartFile(); return false; } File.Copy(HmiOptions.RunFilePath, m_openFile); runScreen.StartFile(); m_app.ChangeApplication(false); } if (!Directory.Exists(HmiOptions.AppDataBinPath)) Directory.CreateDirectory(HmiOptions.AppDataBinPath); m_binpath = Path.Combine(HmiOptions.AppDataBinPath, Path.GetFileNameWithoutExtension(m_openFile) + ".tft"); label2.ForeColor = Color.Black; if (m_app.SaveFile(m_binpath, true, tbCompilerOutput)) m_app.ChangeApplication(false); else { m_binpath = ""; label2.ForeColor = Color.Red; return false; } } #endregion #region saveAs else if (cmd == "saveAs" && m_app != null) { dialog = new SaveFileDialog { Filter = "HMI File|*.HMI|All File|*.*".Translate() }; Utility.SetInitialPath(dialog, "file"); if (dialog.ShowDialog() != DialogResult.OK) return false; m_openFile = dialog.FileName; Utility.SavePath(dialog, "file"); runScreen.PauseScreen(); if (m_app.SaveFile(HmiOptions.RunFilePath, false, null)) { if (!Utility.DeleteFileWait(m_openFile)) { runScreen.StartFile(); return false; } File.Copy(HmiOptions.RunFilePath, m_openFile); } runScreen.StartFile(); refreshPage(); m_app.ChangeApplication(false); return true; } #endregion } if (m_app != null) showUsageSpace(); panelView.Refresh(); return true; }
private void closeHMI() { m_app = null; m_page = null; m_compiler.RefreshObject(null, null, null); m_attributeEdit.RefreshObject(null, null, null); pageAdmin.SetAppInfo(m_app); fontAdmin.SetAppInfo(m_app); picAdmin.SetAppInfo(m_app); runScreen.RunStop(); runScreen.Visible = false; m_obj = null; m_objEdit = null; m_binpath = ""; mi_Save.Enabled = false; mi_Copy.Enabled = false; mi_Paste.Enabled = false; mi_DeleteComponent.Enabled = false; mi_Resolution.Enabled = false; mi_ID.Enabled = false; mi_XY.Enabled = false; mi_Compile.Enabled = false; mi_AddComponent.Enabled = false; if (m_app != null) m_app.ChangeApplication(false); refreshTimerVar(); }