|
10 | 10 | import processing.app.syntax.*; |
11 | 11 | import processing.app.ui.*; |
12 | 12 | import java.util.List; |
| 13 | +import java.util.prefs.Preferences; |
13 | 14 |
|
14 | 15 |
|
15 | 16 | public class CppEditor extends Editor { |
@@ -51,13 +52,69 @@ public void dispose() { |
51 | 52 | private static final String BUG_REPORT_URL = |
52 | 53 | "https://github.com/processing-cpp/processing.cpp/issues"; |
53 | 54 |
|
| 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 | + |
54 | 59 | @Override |
55 | 60 | public EditorFooter createFooter() { |
56 | 61 | EditorFooter ef = super.createFooter(); |
57 | | - SwingUtilities.invokeLater(() -> addBugReportLink(ef)); |
| 62 | + SwingUtilities.invokeLater(() -> { |
| 63 | + addBugReportLink(ef); |
| 64 | + addStdDropdown(ef); |
| 65 | + }); |
58 | 66 | return ef; |
59 | 67 | } |
60 | 68 |
|
| 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 | + |
61 | 118 | private void addBugReportLink(EditorFooter ef) { |
62 | 119 | JLabel prefix = new JLabel("Found a bug? Report it: "); |
63 | 120 | JLabel link = new JLabel("<html><u>here</u></html>"); |
|
0 commit comments