Repository navigation
Expand file tree
/
Copy pathForm1.Python.cs
More file actions
135 lines (122 loc) · 5.51 KB
/
Copy pathForm1.Python.cs
File metadata and controls
135 lines (122 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FarsiLibrary.Win;
using Dm42.Editor;
namespace dm42editor
{
/// <summary>
/// Menu « Python » : conversion d'un Python simplifié en RPN HP-42S / DM42 (bibliothèque Py42.Core).
/// Le RPN généré arrive dans un onglet normal de l'éditeur, prêt pour « Encoder et enregistrer en .raw ».
/// </summary>
public partial class Form1
{
FrmPython pythonWindow;
private void InitPythonMenu()
{
var menu = new ToolStripMenuItem("Python");
var nouveau = new ToolStripMenuItem("Nouveau programme Python", null, (s, e) => ShowPython(null))
{ ShortcutKeys = Keys.Control | Keys.Shift | Keys.N };
var ouvrir = new ToolStripMenuItem("Ouvrir un fichier Python…", null, (s, e) => OpenPythonFile())
{ ShortcutKeys = Keys.Control | Keys.Shift | Keys.O };
var fenetre = new ToolStripMenuItem("Afficher l'éditeur Python", null, (s, e) => ShowPython(null, keepText: true))
{ ShortcutKeys = Keys.F12 };
var exemples = new ToolStripMenuItem("Exemples DM42");
for (int i = 0; i < Py42.Core.Examples.All.Count; i++)
{
int idx = i;
exemples.DropDownItems.Add(Py42.Core.Tr.T(Py42.Core.Examples.All[i].Title), null, (s, e) => ShowPython(null).LoadExample(idx));
}
var exemplesC47 = new ToolStripMenuItem("Exemples C47 / R47");
for (int i = 0; i < Py42.Core.ExamplesC47.All.Count; i++)
{
int idx = i;
exemplesC47.DropDownItems.Add(Py42.Core.ExamplesC47.All[i].Title, null, (s, e) => ShowPython(null).LoadC47Example(idx));
}
var programmesC47 = new ToolStripMenuItem("Programmes de la distribution C47");
for (int i = 0; i < Py42.Core.ExamplesC47.Programs.Count; i++)
{
int idx = i;
programmesC47.DropDownItems.Add(Py42.Core.ExamplesC47.Programs[i].Title, null, (s, e) => ShowPython(null).LoadC47Program(idx));
}
exemplesC47.DropDownItems.Add(new ToolStripSeparator());
exemplesC47.DropDownItems.Add(programmesC47);
var aide = new ToolStripMenuItem("Aide Python → RPN DM42", null, (s, e) => FrmPython.ShowHelp(this));
var aideC47 = new ToolStripMenuItem("Aide Python → RPN C47 / R47", null, (s, e) => FrmPython.ShowHelp(this, c47: true));
menu.DropDownItems.AddRange(new ToolStripItem[]
{
nouveau, ouvrir, fenetre, new ToolStripSeparator(), exemples, exemplesC47, new ToolStripSeparator(), aide, aideC47
});
// avant le menu Help
int at = menuStrip1.Items.IndexOf(helpToolStripMenuItem);
menuStrip1.Items.Insert(at < 0 ? menuStrip1.Items.Count : at, menu);
}
private FrmPython ShowPython(string file, bool keepText = false)
{
if (pythonWindow == null || pythonWindow.IsDisposed)
{
pythonWindow = new FrmPython(SendRpnToTab);
pythonWindow.Show(this);
}
else if (!keepText)
{
pythonWindow.NewProgram();
}
if (file != null) pythonWindow.OpenFile(file);
pythonWindow.WindowState = pythonWindow.WindowState == FormWindowState.Minimized ? FormWindowState.Normal : pythonWindow.WindowState;
pythonWindow.Activate();
return pythonWindow;
}
private void OpenPythonFile()
{
using (var dlg = new OpenFileDialog { Filter = "Python (*.py)|*.py|Tous les fichiers (*.*)|*.*", Title = "Ouvrir un programme Python" })
{
if (dlg.ShowDialog(this) == DialogResult.OK) ShowPython(dlg.FileName, keepText: true);
}
}
/// <summary>
/// Reçoit le RPN de la fenêtre Python et l'ouvre dans un onglet (réutilise l'onglet du même nom).
/// path = chemin du .free42 à côté du .py, ou null si le .py n'est pas enregistré.
/// </summary>
private void SendRpnToTab(string rpn, string title, string path)
{
FATabStripItem tab = null;
foreach (FATabStripItem t in tsFiles.Items)
{
if (t.Title == title && t.Controls.Count > 0 && t.Controls[0] is CodeEditor)
{
tab = t;
break;
}
}
if (tab == null && title.EndsWith(".p47u", StringComparison.OrdinalIgnoreCase))
{
// cible C47 de la fenêtre Python : onglet C47
InitNewDataTab(rpn, null, c47: true);
tab = tsFiles.SelectedItem;
tab.Tag = path;
tab.Title = title;
EditorOf(tab).IsChanged = true;
Activate();
return;
}
if (tab == null)
{
var tb = InitNewTab(Color.Black);
if (tb == null) return;
tab = new FATabStripItem(title, tb);
tab.Tag = path; // null : « Save » demandera un nom
tab.Title = title;
tsFiles.AddTab(tab);
}
var box = (CodeEditor)tab.Controls[0];
box.Text = rpn;
box.IsChanged = true;
tsFiles.SelectedItem = tab;
Activate();
}
}
}