Mercurial > hg > touchkeys
comparison Source/GUI/MainWindow.cpp @ 0:3580ffe87dc8
First commit of TouchKeys public pre-release.
author | Andrew McPherson <andrewm@eecs.qmul.ac.uk> |
---|---|
date | Mon, 11 Nov 2013 18:19:35 +0000 |
parents | |
children | 73703cb77094 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3580ffe87dc8 |
---|---|
1 /* | |
2 TouchKeys: multi-touch musical keyboard control software | |
3 Copyright (c) 2013 Andrew McPherson | |
4 | |
5 This program is free software: you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation, either version 3 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 ===================================================================== | |
19 | |
20 MainWindow.cpp: the control window, plus menu bar and Juce application methods | |
21 */ | |
22 | |
23 #include "../../JuceLibraryCode/JuceHeader.h" | |
24 #include "MainWindow.h" | |
25 | |
26 //============================================================================== | |
27 MainWindow::MainWindow(MainApplicationController& controller) | |
28 : DocumentWindow ("TouchKeys", Colours::lightgrey, DocumentWindow::allButtons), | |
29 controller_(controller) | |
30 { | |
31 commandManager_.registerAllCommandsForTarget(this); | |
32 commandManager_.registerAllCommandsForTarget(JUCEApplication::getInstance()); | |
33 | |
34 // This lets the command manager use keypresses that arrive in our window to send | |
35 // out commands | |
36 addKeyListener(commandManager_.getKeyMappings()); | |
37 | |
38 // Create the menu bar, either in the window or at the top of the screen | |
39 #if JUCE_MAC | |
40 MenuBarModel::setMacMainMenu(this); | |
41 #else | |
42 // Menu bar goes at the top of the window | |
43 setMenuBar(this); | |
44 | |
45 // Window height should change to accommodate it | |
46 Component *actualMenuBar = getMenuBarComponent(); | |
47 if(actualMenuBar != 0) { | |
48 Rectangle<int> r = actualMenuBar->getBounds(); | |
49 setSize(getWidth(), getHeight() + r.getHeight()); | |
50 } | |
51 #endif /* JUCE_MAC */ | |
52 | |
53 // Tells our menu bar model that it should watch this command manager for | |
54 // changes, and send change messages accordingly. | |
55 setApplicationCommandManagerToWatch(&commandManager_); | |
56 | |
57 mainComponent_.setMainApplicationController(&controller_); | |
58 setContentNonOwned(&mainComponent_, true); | |
59 | |
60 commandManager_.setFirstCommandTarget(this); | |
61 | |
62 // Start a timer that will keep the interface in sync with the application | |
63 startTimer(50); | |
64 | |
65 centreWithSize (getWidth(), getHeight()); | |
66 setUsingNativeTitleBar(true); | |
67 setVisible (true); | |
68 } | |
69 | |
70 MainWindow::~MainWindow() { | |
71 // Delete the menu bar before exiting | |
72 #if JUCE_MAC | |
73 MenuBarModel::setMacMainMenu(nullptr); | |
74 #else | |
75 setMenuBar(nullptr); | |
76 #endif /* JUCE_MAC */ | |
77 } | |
78 | |
79 // Handle periodic GUI updates, keeping the GUI in sync with the underlying system | |
80 void MainWindow::timerCallback() { | |
81 mainComponent_.synchronize(); | |
82 } | |
83 | |
84 // ***** Juce menu bar methods **** | |
85 StringArray MainWindow::getMenuBarNames() { | |
86 const char* const names[] = { "File", "Edit", "Control", "Window", nullptr }; | |
87 | |
88 return StringArray (names); | |
89 } | |
90 | |
91 PopupMenu MainWindow::getMenuForIndex(int menuIndex, const String& menuName) { | |
92 PopupMenu menu; | |
93 | |
94 if(menuIndex == 0) { // File | |
95 menu.addCommandItem(&commandManager_, kCommandNewPreset); | |
96 menu.addSeparator(); | |
97 menu.addCommandItem(&commandManager_, kCommandOpenPreset); | |
98 menu.addSeparator(); | |
99 menu.addCommandItem(&commandManager_, kCommandSavePreset); | |
100 menu.addCommandItem(&commandManager_, kCommandSavePresetAs); | |
101 #ifndef JUCE_MAC | |
102 menu.addSeparator(); | |
103 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::quit); | |
104 #endif | |
105 } | |
106 else if(menuIndex == 1) { // Edit | |
107 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::undo); | |
108 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::redo); | |
109 menu.addSeparator(); | |
110 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::cut); | |
111 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::copy); | |
112 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::paste); | |
113 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::del); | |
114 menu.addSeparator(); | |
115 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::selectAll); | |
116 } | |
117 else if(menuIndex == 2) { // Control | |
118 menu.addCommandItem(&commandManager_, kCommandRescanDevices); | |
119 menu.addSeparator(); | |
120 menu.addCommandItem(&commandManager_, kCommandEnableExperimentalMappings); | |
121 } | |
122 else if(menuIndex == 3) { // Window | |
123 menu.addCommandItem(&commandManager_, kCommandShowControlWindow); | |
124 menu.addCommandItem(&commandManager_, kCommandShowKeyboardWindow); | |
125 } | |
126 | |
127 return menu; | |
128 } | |
129 | |
130 void MainWindow::menuItemSelected(int menuItemID, int topLevelMenuIndex) { | |
131 // TODO | |
132 } | |
133 | |
134 // **** Command Manager methods ***** | |
135 | |
136 ApplicationCommandTarget* MainWindow::getNextCommandTarget() { | |
137 return findFirstTargetParentComponent(); | |
138 } | |
139 | |
140 // this returns the set of all commands that this target can perform.. | |
141 void MainWindow::getAllCommands(Array <CommandID>& commands) { | |
142 // this returns the set of all commands that this target can perform.. | |
143 const CommandID ids[] = { | |
144 // File | |
145 kCommandNewPreset, kCommandOpenPreset, kCommandSavePreset, | |
146 kCommandSavePresetAs, | |
147 // Edit | |
148 StandardApplicationCommandIDs::undo, | |
149 StandardApplicationCommandIDs::redo, | |
150 StandardApplicationCommandIDs::cut, | |
151 StandardApplicationCommandIDs::copy, | |
152 StandardApplicationCommandIDs::paste, | |
153 StandardApplicationCommandIDs::del, | |
154 StandardApplicationCommandIDs::selectAll, | |
155 // Control | |
156 kCommandRescanDevices, | |
157 kCommandEnableExperimentalMappings, | |
158 // Window | |
159 kCommandShowControlWindow, | |
160 kCommandShowKeyboardWindow | |
161 }; | |
162 | |
163 commands.addArray (ids, numElementsInArray(ids)); | |
164 } | |
165 | |
166 // This method is used when something needs to find out the details about one of the commands | |
167 // that this object can perform.. | |
168 void MainWindow::getCommandInfo(CommandID commandID, ApplicationCommandInfo& result) { | |
169 const String presetsCategory("Presets"); | |
170 const String editsCategory("Editing"); | |
171 const String controlCategory("Control"); | |
172 const String windowCategory("Window"); | |
173 | |
174 switch (commandID) { | |
175 | |
176 // *** File Menu *** | |
177 case kCommandNewPreset: | |
178 result.setInfo("New Preset", "Clears the current settings", presetsCategory, 0); | |
179 result.setTicked(false); | |
180 result.setActive(false); | |
181 result.addDefaultKeypress ('N', ModifierKeys::commandModifier); | |
182 break; | |
183 case kCommandOpenPreset: | |
184 result.setInfo("Open Preset...", "Opens an existing preset", presetsCategory, 0); | |
185 result.setTicked(false); | |
186 result.setActive(false); | |
187 result.addDefaultKeypress ('O', ModifierKeys::commandModifier); | |
188 break; | |
189 case kCommandSavePreset: | |
190 result.setInfo("Save Preset", "Saves the current preset", presetsCategory, 0); | |
191 result.setTicked(false); | |
192 result.setActive(false); | |
193 result.addDefaultKeypress ('S', ModifierKeys::commandModifier); | |
194 break; | |
195 case kCommandSavePresetAs: | |
196 result.setInfo("Save Preset As...", "Saves the current preset with a new name", presetsCategory, 0); | |
197 result.setTicked (false); | |
198 result.setActive(false); | |
199 break; | |
200 // Quit command is handled by JuceApplication | |
201 | |
202 // *** Edit Menu *** | |
203 case StandardApplicationCommandIDs::undo: | |
204 result.setInfo("Undo", "Undo", editsCategory, 0); | |
205 result.setTicked(false); | |
206 result.setActive(false); | |
207 result.addDefaultKeypress ('Z', ModifierKeys::commandModifier); | |
208 break; | |
209 case StandardApplicationCommandIDs::redo: | |
210 result.setInfo("Redo", "Redo", editsCategory, 0); | |
211 result.setTicked(false); | |
212 result.setActive(false); | |
213 result.addDefaultKeypress ('Z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier); | |
214 break; | |
215 case StandardApplicationCommandIDs::cut: | |
216 result.setInfo("Cut", "Cut", editsCategory, 0); | |
217 result.setTicked(false); | |
218 result.setActive(false); | |
219 result.addDefaultKeypress ('X', ModifierKeys::commandModifier); | |
220 break; | |
221 case StandardApplicationCommandIDs::copy: | |
222 result.setInfo("Copy", "Copy", editsCategory, 0); | |
223 result.setTicked(false); | |
224 result.setActive(false); | |
225 result.addDefaultKeypress ('C', ModifierKeys::commandModifier); | |
226 break; | |
227 case StandardApplicationCommandIDs::paste: | |
228 result.setInfo("Paste", "Paste", editsCategory, 0); | |
229 result.setTicked(false); | |
230 result.setActive(false); | |
231 result.addDefaultKeypress ('V', ModifierKeys::commandModifier); | |
232 break; | |
233 case StandardApplicationCommandIDs::del: | |
234 result.setInfo("Delete", "Delete", editsCategory, 0); | |
235 result.setTicked(false); | |
236 result.setActive(false); | |
237 break; | |
238 case StandardApplicationCommandIDs::selectAll: | |
239 result.setInfo("Select All", "Select All", editsCategory, 0); | |
240 result.setTicked(false); | |
241 result.setActive(false); | |
242 result.addDefaultKeypress ('A', ModifierKeys::commandModifier); | |
243 break; | |
244 | |
245 // *** Control Menu *** | |
246 case kCommandRescanDevices: | |
247 result.setInfo("Rescan Devices", "Rescans available TouchKeys and MIDI devices", controlCategory, 0); | |
248 result.setTicked(false); | |
249 result.setActive(false); | |
250 result.addDefaultKeypress ('R', ModifierKeys::commandModifier); | |
251 break; | |
252 case kCommandEnableExperimentalMappings: | |
253 result.setInfo("Enable Experimental Mappings", "Enables mappings which are still experimental", controlCategory, 0); | |
254 result.setTicked(controller_.experimentalMappingsEnabled()); | |
255 break; | |
256 | |
257 // *** Window Menu *** | |
258 case kCommandShowControlWindow: | |
259 result.setInfo("TouchKeys Controls", "Show control and mapping window", windowCategory, 0); | |
260 result.setTicked(false); | |
261 break; | |
262 case kCommandShowKeyboardWindow: | |
263 result.setInfo("Keyboard Display", "Show keyboard display", windowCategory, 0); | |
264 result.setTicked(false); | |
265 break; | |
266 | |
267 default: | |
268 break; | |
269 }; | |
270 } | |
271 | |
272 bool MainWindow::perform(const InvocationInfo& info) { | |
273 switch (info.commandID) | |
274 { | |
275 case kCommandNewPreset: | |
276 break; | |
277 case kCommandRescanDevices: | |
278 std::cout << "Rescan\n"; | |
279 break; | |
280 case kCommandEnableExperimentalMappings: | |
281 controller_.setExperimentalMappingsEnabled(!controller_.experimentalMappingsEnabled()); | |
282 break; | |
283 case kCommandShowControlWindow: | |
284 toFront(true); | |
285 break; | |
286 case kCommandShowKeyboardWindow: | |
287 controller_.showKeyboardDisplayWindow(); | |
288 break; | |
289 | |
290 default: | |
291 return false; | |
292 }; | |
293 | |
294 return true; | |
295 } |