-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppEditor.java
More file actions
447 lines (399 loc) · 18 KB
/
Copy pathCppEditor.java
File metadata and controls
447 lines (399 loc) · 18 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package processing.mode.cpp;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import processing.app.*;
import processing.app.syntax.*;
import processing.app.ui.*;
public class CppEditor extends Editor {
private CppRunner currentRunner;
private final Set<Integer> errorLines = new HashSet<>();
private ErrorOverlay overlay;
public CppEditor(Base base, String path,
EditorState state, CppMode mode) throws EditorException {
super(base, path, state, mode);
}
// ── Bug report link (placed in the footer's tab bar, just left of the
// version number / "Updates" indicator) ──────────────────────────────
private static final String BUG_REPORT_URL =
"https://github.com/processing-cpp/processing.cpp/issues";
@Override
public EditorFooter createFooter() {
EditorFooter ef = super.createFooter();
SwingUtilities.invokeLater(() -> addBugReportLink(ef));
return ef;
}
private void addBugReportLink(EditorFooter ef) {
JLabel prefix = new JLabel("Found a bug? Report it: ");
JLabel link = new JLabel("<html><u>here</u></html>");
link.setForeground(new Color(0x88, 0x66, 0xDD));
link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
link.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
openBugReportUrl();
}
});
JPanel row = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 0));
row.setOpaque(false);
row.add(prefix);
row.add(link);
// "Console" and "Updates" are both painted directly onto a single
// canvas component (drawString calls), not separate Swing children —
// there's no way to insert a real component "between" them through
// the normal layout, and adding a child directly into the tab bar
// would fight its active BoxLayout. Instead we float our link on the
// window's layered pane, positioned over the tab bar at the x-offset
// where the "Console" tab text ends (computed the same way the
// footer itself lays out tabs: LEFT_GUTTER + margin + text width +
// margin), and keep it synced to the tab bar's on-screen position.
JLabel versionLabel = findVersionLabel(ef);
Container tabBar = (versionLabel != null) ? versionLabel.getParent() : null;
if (tabBar == null) {
// Fallback: append to the footer itself so it's at least visible.
ef.add(row);
ef.revalidate();
ef.repaint();
return;
}
Font tabFont = versionLabel.getFont();
FontMetrics fm = tabBar.getFontMetrics(tabFont);
final int LEFT_GUTTER = Editor.LEFT_GUTTER;
final int MARGIN = 8; // matches EditorFooter's own tab margin
int consoleTextWidth = fm.stringWidth("Console");
int anchorX = LEFT_GUTTER + MARGIN + consoleTextWidth + MARGIN + 35;
JRootPane rootPane = getRootPane();
JLayeredPane layered = rootPane.getLayeredPane();
Dimension pref = row.getPreferredSize();
layered.add(row, JLayeredPane.PALETTE_LAYER);
row.setVisible(false); // hidden until we confirm there's room
final int SAFETY_GAP = 16; // minimum breathing room before Updates/version
Runnable reposition = () -> {
if (!tabBar.isShowing() || !versionLabel.isShowing()) {
row.setVisible(false);
return;
}
Point tabBarPos = SwingUtilities.convertPoint(tabBar, 0, 0, layered);
Point versionPos = SwingUtilities.convertPoint(versionLabel, 0, 0, tabBar);
// Only show the link if it fits entirely before the version label
// (which also means before "Updates", since that's painted right up
// against the version label's left edge) with some breathing room.
boolean fits = (anchorX + pref.width + SAFETY_GAP) <= versionPos.x;
row.setVisible(fits);
if (!fits) return;
int y = tabBarPos.y + (tabBar.getHeight() - pref.height) / 2;
row.setBounds(tabBarPos.x + anchorX, Math.max(0, y), pref.width, pref.height);
};
reposition.run();
java.awt.event.ComponentAdapter syncListener = new java.awt.event.ComponentAdapter() {
@Override public void componentResized(java.awt.event.ComponentEvent e) { reposition.run(); }
@Override public void componentMoved(java.awt.event.ComponentEvent e) { reposition.run(); }
};
tabBar.addComponentListener(syncListener);
rootPane.addComponentListener(syncListener);
// Safety net: if the window wasn't fully shown yet on the first pass,
// retry briefly until it is (covers the case where no resize/move ever
// happens after this runs, so the link would otherwise stay at 0,0).
Timer retry = new Timer(150, null);
int[] attempts = { 0 };
retry.addActionListener(e -> {
reposition.run();
attempts[0]++;
if (tabBar.isShowing() || attempts[0] > 10) {
retry.stop();
}
});
retry.start();
layered.revalidate();
layered.repaint();
}
private boolean looksLikeVersionLabel(Component c) {
if (!(c instanceof JLabel)) return false;
String text = ((JLabel) c).getText();
return text != null && text.matches("\\d+(\\.\\d+)+\\w*");
}
private JLabel findVersionLabel(Container parent) {
for (Component c : parent.getComponents()) {
if (looksLikeVersionLabel(c)) return (JLabel) c;
if (c instanceof Container) {
JLabel found = findVersionLabel((Container) c);
if (found != null) return found;
}
}
return null;
}
private void openBugReportUrl() {
// Try AWT Desktop first.
try {
if (java.awt.Desktop.isDesktopSupported()
&& java.awt.Desktop.getDesktop().isSupported(java.awt.Desktop.Action.BROWSE)) {
java.awt.Desktop.getDesktop().browse(new java.net.URI(BUG_REPORT_URL));
return;
}
} catch (Exception ignored) {
// fall through to OS-specific commands below
}
// Linux fallback: try common openers directly.
String[] candidates = { "xdg-open", "gio", "gnome-open", "kde-open" };
for (String cmd : candidates) {
try {
ProcessBuilder pb = (cmd.equals("gio"))
? new ProcessBuilder(cmd, "open", BUG_REPORT_URL)
: new ProcessBuilder(cmd, BUG_REPORT_URL);
pb.start();
return;
} catch (Exception ignored) {
// try next candidate
}
}
// Nothing worked — let the user copy the link themselves.
JOptionPane.showMessageDialog(this,
"Couldn't open the browser automatically.\n\nPlease visit:\n" + BUG_REPORT_URL,
"Open Link", JOptionPane.INFORMATION_MESSAGE);
}
@Override
protected JEditTextArea createTextArea() {
PdeTextArea ta = new PdeTextArea(new PdeTextAreaDefaults(),
new CppInputHandler(this), this);
InputHandler ih = ta.getInputHandler();
// NOTE: ENTER (auto-indent), TAB/S+TAB (indent/outdent/expand), and
// C+SLASH (comment toggle) are now handled by CppInputHandler's
// handlePressed(), mirroring JavaInputHandler, so they're no longer
// bound here individually.
// ── Home / End ─────────────────────────────────────────────────────────
ih.addKeyBinding("HOME", InputHandler.HOME);
ih.addKeyBinding("END", InputHandler.END);
ih.addKeyBinding("S+HOME", InputHandler.SELECT_HOME);
ih.addKeyBinding("S+END", InputHandler.SELECT_END);
ih.addKeyBinding("C+HOME", InputHandler.DOCUMENT_HOME);
ih.addKeyBinding("C+END", InputHandler.DOCUMENT_END);
ih.addKeyBinding("CS+HOME", InputHandler.SELECT_DOC_HOME);
ih.addKeyBinding("CS+END", InputHandler.SELECT_DOC_END);
// ── Arrow keys with selection ──────────────────────────────────────────
ih.addKeyBinding("S+LEFT", InputHandler.SELECT_PREV_CHAR);
ih.addKeyBinding("S+RIGHT", InputHandler.SELECT_NEXT_CHAR);
ih.addKeyBinding("S+UP", InputHandler.SELECT_PREV_LINE);
ih.addKeyBinding("S+DOWN", InputHandler.SELECT_NEXT_LINE);
// ── Word navigation ────────────────────────────────────────────────────
ih.addKeyBinding("C+LEFT", InputHandler.PREV_WORD);
ih.addKeyBinding("C+RIGHT", InputHandler.NEXT_WORD);
ih.addKeyBinding("CS+LEFT", InputHandler.SELECT_PREV_WORD);
ih.addKeyBinding("CS+RIGHT",InputHandler.SELECT_NEXT_WORD);
// ── Delete ────────────────────────────────────────────────────────────
ih.addKeyBinding("DELETE", InputHandler.DELETE);
ih.addKeyBinding("BACK_SPACE",InputHandler.BACKSPACE);
ih.addKeyBinding("C+BACK_SPACE", InputHandler.BACKSPACE_WORD);
ih.addKeyBinding("C+DELETE", InputHandler.DELETE_WORD);
// ── Page up/down ──────────────────────────────────────────────────────
ih.addKeyBinding("PAGE_UP", InputHandler.PREV_PAGE);
ih.addKeyBinding("PAGE_DOWN", InputHandler.NEXT_PAGE);
ih.addKeyBinding("S+PAGE_UP", InputHandler.SELECT_PREV_PAGE);
ih.addKeyBinding("S+PAGE_DOWN",InputHandler.SELECT_NEXT_PAGE);
// ── Error overlay ─────────────────────────────────────────────────────
overlay = new ErrorOverlay(ta);
ta.getPainter().setLayout(null);
ta.getPainter().add(overlay);
ta.getPainter().addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent e) {
overlay.setBounds(0, 0,
ta.getPainter().getWidth(), ta.getPainter().getHeight());
}
});
overlay.setBounds(0, 0, 600, 400);
return ta;
}
// ── Error overlay ─────────────────────────────────────────────────────────
class ErrorOverlay extends JComponent {
private final JEditTextArea ta;
ErrorOverlay(JEditTextArea ta) { this.ta = ta; setOpaque(false); }
@Override
protected void paintComponent(Graphics g) {
if (errorLines.isEmpty()) return;
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(0xFF, 0x40, 0x40, 200));
g2.setStroke(new BasicStroke(1.5f));
int lineH = ta.getPainter().getFontMetrics().getHeight();
int firstVisible = ta.getFirstLine();
for (int docLine : errorLines) {
int visLine = docLine - firstVisible;
if (visLine < 0) continue;
int y = visLine * lineH + lineH - 3;
int w = ta.getPainter().getWidth();
for (int x = 0; x < w - 6; x += 6) {
g2.drawLine(x, y, x+3, y - 2);
g2.drawLine(x+3, y - 2, x+6, y);
}
}
g2.dispose();
}
}
// ── Error highlighting ────────────────────────────────────────────────────
public void highlightErrorLine(int line) {
if (line <= 0) return;
int docLine = line - 1;
if (!(textarea instanceof PdeTextArea)) return;
PdeTextArea pta = (PdeTextArea) textarea;
try {
if (docLine >= pta.getLineCount()) return;
errorLines.clear();
errorLines.add(docLine);
pta.clearGutterText();
pta.setGutterText(docLine, "▶");
pta.scrollTo(docLine, 0);
pta.select(pta.getLineStartOffset(docLine),
pta.getLineStopOffset(docLine) - 1);
if (overlay != null) overlay.repaint();
} catch (Exception ignored) {}
}
public void clearErrorHighlight() {
errorLines.clear();
if (textarea instanceof PdeTextArea)
((PdeTextArea) textarea).clearGutterText();
if (overlay != null) overlay.repaint();
}
// ── Comment toggle ────────────────────────────────────────────────────────
void toggleComment(JEditTextArea ta) {
int selStart = ta.getSelectionStart();
int selEnd = ta.getSelectionStop();
int startLine = ta.getLineOfOffset(selStart);
int endLine = ta.getLineOfOffset(selEnd);
if (endLine > startLine && ta.getLineStartOffset(endLine) == selEnd)
endLine--;
boolean allCommented = true;
for (int i = startLine; i <= endLine; i++) {
if (!getLineText(ta, i).stripLeading().startsWith("//")) {
allCommented = false; break;
}
}
for (int i = endLine; i >= startLine; i--) {
String raw = getLineText(ta, i);
if (allCommented)
replaceLineText(ta, i, raw.replaceFirst("(\\s*)//\\s?", "$1"));
else {
int indent = 0;
while (indent < raw.length() &&
(raw.charAt(indent)==' ' || raw.charAt(indent)=='\t')) indent++;
replaceLineText(ta, i,
raw.substring(0, indent) + "// " + raw.substring(indent));
}
}
}
private String getLineText(JEditTextArea ta, int line) {
try {
int start = ta.getLineStartOffset(line);
int end = ta.getLineStopOffset(line) - 1;
return ta.getDocument().getText(start, Math.max(0, end - start));
} catch (BadLocationException e) { return ""; }
}
private void replaceLineText(JEditTextArea ta, int line, String text) {
try {
int start = ta.getLineStartOffset(line);
int end = ta.getLineStopOffset(line) - 1;
ta.getDocument().remove(start, Math.max(0, end - start));
ta.getDocument().insertString(start, text, null);
} catch (BadLocationException e) {}
}
@Override public EditorToolbar createToolbar() { return new CppToolbar(this); }
@Override public Formatter createFormatter() { return null; }
@Override public JMenu buildFileMenu() {
javax.swing.JMenuItem exportApp = new javax.swing.JMenuItem("Export Application...");
exportApp.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_E,
java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()
| java.awt.event.InputEvent.SHIFT_DOWN_MASK));
exportApp.addActionListener(e -> handleExportApplication());
return buildFileMenu(new javax.swing.JMenuItem[]{ exportApp });
}
@Override public JMenu buildHelpMenu() { return new JMenu("Help"); }
@Override public void handleImportLibrary(String name) {}
@Override public String getCommentPrefix() { return "//"; }
@Override public void startIndeterminate() { }
@Override public void stopIndeterminate() { }
@Override public void statusHalt() { }
@Override public boolean isHalted() { return currentRunner == null; }
@Override public void deactivateRun() { toolbar.deactivateRun(); }
@Override
public void internalCloseRunner() {
if (currentRunner != null) { currentRunner.stop(); currentRunner = null; }
}
@Override
public JMenu buildSketchMenu() {
JMenuItem run = processing.app.ui.Toolkit.newJMenuItem("Run", 'R');
JMenuItem stop = new JMenuItem("Stop");
run.addActionListener(e -> handleRun(false, null, null));
stop.addActionListener(e -> handleStop());
return buildSketchMenu(new JMenuItem[] { run, stop });
}
public void handleRun(boolean present, Runnable stopCb, Runnable startCb) {
clearErrorHighlight();
prepareRun();
statusNotice("Compiling...");
new Thread(() -> {
try {
CppMode mode = (CppMode) getMode();
currentRunner = mode.handleLaunch(sketch, this);
if (currentRunner != null) {
statusNotice("Running.");
if (startCb != null) startCb.run();
} else {
statusError("Build failed — see console for errors.");
deactivateRun();
}
} catch (InstallWizard.CancelledByUser e) {
statusNotice("Cancelled.");
deactivateRun();
} catch (Exception e) {
statusError(e.getMessage());
deactivateRun();
}
}).start();
}
public void handleStop() {
internalCloseRunner();
deactivateRun();
statusNotice("Stopped.");
}
public void handleExportApplication() {
// Block export only for truly unsaved sketches (untitled, never saved)
// Check if the sketch folder is inside a temp directory
java.io.File sketchFolder = sketch.getFolder();
boolean isTemp = sketchFolder == null
|| !sketchFolder.exists()
|| sketch.isUntitled();
if (isTemp) {
javax.swing.JOptionPane.showMessageDialog(null,
"<html><b>Please save the sketch first.</b><br><br>" +
"Use <b>File → Save As...</b> to give it a name,<br>" +
"then try Export Application again.</html>",
"Save Required", javax.swing.JOptionPane.WARNING_MESSAGE);
return;
}
// Save if modified
if (sketch.isModified()) {
int choice = javax.swing.JOptionPane.showOptionDialog(null,
"<html><b>Sketch has unsaved changes.</b><br><br>" +
"Save before exporting?</html>",
"Unsaved Changes",
javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
javax.swing.JOptionPane.WARNING_MESSAGE,
null,
new String[]{"Save & Export", "Export Anyway", "Cancel"},
"Save & Export");
if (choice == 2 || choice < 0) return;
if (choice == 0) {
try { sketch.save(); } catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(null,
"Could not save: " + e.getMessage(),
"Save Failed", javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
}
}
CppBuild build = new CppBuild(sketch, (CppMode) getMode());
build.showExportDialog(sketch, this);
}
}