Skip to content

Commit 6e11aad

Browse files
committed
Add C++ standard dropdown to footer, persisted via Preferences, wired into build
1 parent ffbaa58 commit 6e11aad

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

mode/CppMode.jar

2.03 KB
Binary file not shown.

src/java/CppBuild.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ public File compile(RunnerListener listener) throws Exception {
508508
File binary = new File(buildDir, sketch.getName() + ext);
509509
String gpp = findGpp(isWindows);
510510

511-
List<String> cmd = buildCommand(gpp, sketchSrc, binary, isWindows, isMac);
511+
String stdOverride = (listener instanceof CppEditor ce) ? ce.getSelectedCppStd() : null;
512+
List<String> cmd = buildCommand(gpp, sketchSrc, binary, isWindows, isMac, stdOverride);
512513
listener.statusNotice("$ " + String.join(" ", cmd));
513514
// Only print the full compile command when debug mode is actually on
514515
// (i.e. the DEBUG file already caused buildCommand() to add
@@ -3021,9 +3022,12 @@ static String bestCppStd(String gpp) {
30213022
}
30223023

30233024
private List<String> buildCommand(String gpp, File src, File bin, boolean win, boolean mac) {
3025+
return buildCommand(gpp, src, bin, win, mac, null);
3026+
}
3027+
private List<String> buildCommand(String gpp, File src, File bin, boolean win, boolean mac, String overrideStd) {
30243028
List<String> cmd = new ArrayList<>();
30253029
cmd.add(gpp);
3026-
String cppStd = bestCppStd(gpp);
3030+
String cppStd = (overrideStd != null) ? overrideStd : bestCppStd(gpp);
30273031
cmd.add("-std=" + cppStd);
30283032
cmd.add("-O2");
30293033
// -march=native isn't reliably supported by Apple's clang (which is

src/java/CppEditor.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import processing.app.syntax.*;
1111
import processing.app.ui.*;
1212
import java.util.List;
13+
import java.util.prefs.Preferences;
1314

1415

1516
public class CppEditor extends Editor {
@@ -51,13 +52,69 @@ public void dispose() {
5152
private static final String BUG_REPORT_URL =
5253
"https://github.com/processing-cpp/processing.cpp/issues";
5354

55+
private static final String[] CPP_STDS = {"c++17", "c++20", "c++23", "c++2c"};
56+
private static final String PREF_KEY = "cppmode.std";
57+
private JComboBox<String> stdCombo;
58+
5459
@Override
5560
public EditorFooter createFooter() {
5661
EditorFooter ef = super.createFooter();
57-
SwingUtilities.invokeLater(() -> addBugReportLink(ef));
62+
SwingUtilities.invokeLater(() -> {
63+
addBugReportLink(ef);
64+
addStdDropdown(ef);
65+
});
5866
return ef;
5967
}
6068

69+
private void addStdDropdown(EditorFooter ef) {
70+
JLabel versionLabel = findVersionLabel(ef);
71+
Container tabBar = (versionLabel != null) ? versionLabel.getParent() : null;
72+
if (tabBar == null) return;
73+
74+
// Load saved preference or detect from g++
75+
Preferences prefs = Preferences.userNodeForPackage(CppEditor.class);
76+
String savedStd = prefs.get(PREF_KEY, null);
77+
String currentStd = (savedStd != null) ? savedStd
78+
: processing.mode.cpp.CppBuild.bestCppStd("g++");
79+
80+
stdCombo = new JComboBox<>(CPP_STDS);
81+
stdCombo.setSelectedItem(currentStd);
82+
stdCombo.setFont(versionLabel.getFont());
83+
stdCombo.setFocusable(false);
84+
stdCombo.setToolTipText("C++ standard version");
85+
stdCombo.addActionListener(e -> {
86+
String selected = (String) stdCombo.getSelectedItem();
87+
prefs.put(PREF_KEY, selected);
88+
});
89+
90+
// Position just left of the version label
91+
tabBar.setLayout(null);
92+
tabBar.add(stdCombo);
93+
tabBar.addComponentListener(new java.awt.event.ComponentAdapter() {
94+
@Override public void componentResized(java.awt.event.ComponentEvent e) {
95+
positionStdCombo(tabBar, versionLabel);
96+
}
97+
});
98+
SwingUtilities.invokeLater(() -> positionStdCombo(tabBar, versionLabel));
99+
}
100+
101+
private void positionStdCombo(Container tabBar, JLabel versionLabel) {
102+
if (stdCombo == null || !versionLabel.isShowing()) return;
103+
Point vpos = SwingUtilities.convertPoint(versionLabel, 0, 0, tabBar);
104+
Dimension pref = stdCombo.getPreferredSize();
105+
int h = tabBar.getHeight();
106+
int y = (h - pref.height) / 2;
107+
int x = vpos.x - pref.width - 8;
108+
if (x > 0) stdCombo.setBounds(x, y, pref.width, pref.height);
109+
}
110+
111+
/** Returns the selected C++ standard for use in CppBuild. */
112+
public String getSelectedCppStd() {
113+
if (stdCombo != null) return (String) stdCombo.getSelectedItem();
114+
Preferences prefs = Preferences.userNodeForPackage(CppEditor.class);
115+
return prefs.get(PREF_KEY, processing.mode.cpp.CppBuild.bestCppStd("g++"));
116+
}
117+
61118
private void addBugReportLink(EditorFooter ef) {
62119
JLabel prefix = new JLabel("Found a bug? Report it: ");
63120
JLabel link = new JLabel("<html><u>here</u></html>");

0 commit comments

Comments
 (0)