private void OpenFile(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); openFileDialog.Filter = "LadderApp files (*.xml;*.a43)|*.xml;*.a43|XML files (*.xml)|*.xml|MSP430 Executable files (*.a43)|*.a43"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { string fileName = openFileDialog.FileName; switch (Path.GetExtension(fileName).ToLower()) { case ".xml": try { XmlReader fileReader = new XmlTextReader(new FileStream(fileName, FileMode.Open)); XmlSerializer xmlSerializer = new XmlSerializer(typeof(LadderProgram)); if (xmlSerializer.CanDeserialize(fileReader)) { LadderProgram ladderProgram = (LadderProgram)xmlSerializer.Deserialize(fileReader); projectForm = new ProjectForm(ladderProgram, AddressingServices.Instance); projectForm.Status = ProjectForm.ProgramStatus.Open; projectForm.PathFile = fileName; projectForm.Program.Name = Path.GetFileNameWithoutExtension(fileName); projectForm.MdiParent = this; projectForm.Show(); projectForm.SetText(); } fileReader.Close(); } catch (Exception ex) { MessageBox.Show("Error reading file! " + ex.InnerException.Message, "Open files ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; case ".a43": try { MicIntegrationServices p = new MicIntegrationServices(); String readContent = p.ConvertHex2String(fileName); if (CheckPassword(readContent)) { ReadExecutable(readContent, fileName.Substring(fileName.LastIndexOf(@"\") + 1, fileName.Length - fileName.LastIndexOf(@"\") - 1)); } } catch { MessageBox.Show("Unknown file format!", "Open files ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; default: MessageBox.Show("Unknown file format!", "Open files ...", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } } }
private void ReadExecutable(string content, string projectName) { try { ExecutableReaderService executableReaderService = new ExecutableReaderService(); LadderProgram program = executableReaderService.ReadExecutable(content, projectName); projectForm = new ProjectForm(program, AddressingServices.Instance); projectForm.MdiParent = this; projectForm.Show(); projectForm.SetText(); } catch (Exception ex) { MessageBox.Show(ex.Message, "ReadExecutable ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ShowNewForm(object sender, EventArgs e) { if (MdiChildren.Length == 0) { projectForm = new ProjectForm(new LadderProgram(), addressingServices); projectForm.MdiParent = this; projectForm.Show(); projectForm.SetText(); } else { DialogResult result = MessageBox.Show(VisualResources.SaveTheProjectQuestion.Replace("%%", projectForm.Text.Trim()).Trim(), "LadderApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { projectForm.Close(); } } }
void VisualInstruction_Click(object sender, MouseEventArgs e) { if (!e.Button.Equals(MouseButtons.Right)) { return; } VisualInstructionUserControl visualInstruction = (VisualInstructionUserControl)sender; OperationCode opCode = visualInstruction.OpCode; if (opCode != OperationCode.LineBegin) { ladderForm.mnuInsertLadderLine.Enabled = false; ladderForm.mnuToggleBit.Enabled = false; if (opCode == OperationCode.ParallelBranchBegin || opCode == OperationCode.ParallelBranchEnd || opCode == OperationCode.ParallelBranchNext) { ladderForm.mnuAddressing.Enabled = false; ladderForm.mnuAddressing.Visible = false; ladderForm.mnuExtendParallelBranchAbove.Enabled = true; ladderForm.mnuExtendParallelBranchAbove.Visible = true; ladderForm.mnuExtendParallelBranchBelow.Enabled = true; ladderForm.mnuExtendParallelBranchBelow.Visible = true; } else { ladderForm.mnuAddressing.Enabled = true; ladderForm.mnuAddressing.Visible = true; if (visualInstruction.IsAllOperandsOk()) { ladderForm.mnuToggleBit.Enabled = true; } else { ladderForm.mnuToggleBit.Enabled = false; } ProjectForm projectForm = ladderForm.projectForm; TreeNode addressingNode = projectForm.tvnProjectTree.Nodes["tvnProjectNode"].Nodes["tvnAddressingNode"]; foreach (TreeNode eachAddressTypeNode in addressingNode.Nodes) { ToolStripMenuItem menu = null; switch (eachAddressTypeNode.Text) { case "Memories": menu = ladderForm.mnuMemory; break; case "Timer": menu = ladderForm.mnuTimer; break; case "Counter": menu = ladderForm.mnuCounter; break; case "Input": menu = ladderForm.mnuInput; break; case "Output": menu = ladderForm.mnuOutput; break; } Address address = null; if (visualInstruction.IsAllOperandsOk()) { Object obj = visualInstruction.GetOperand(0); if (obj is Address) { address = (Address)obj; } } menu.DropDownItems.Clear(); foreach (TreeNode eachAddressNode in eachAddressTypeNode.Nodes) { menu.DropDownItems.Add(eachAddressNode.Text); if (address != null) { if (address.GetName() == eachAddressNode.Text) { menu.DropDownItems[menu.DropDownItems.Count - 1].Select(); } } menu.DropDownItems[menu.DropDownItems.Count - 1].Name = eachAddressNode.Text; menu.DropDownItems[menu.DropDownItems.Count - 1].Tag = eachAddressNode.Tag; menu.DropDownItems[menu.DropDownItems.Count - 1].Click += new EventHandler(MenuContextAddress_Click); } } } } ladderForm.mnuContextAtInstruction.Show(visualInstruction.PointToScreen(e.Location)); }