annotate Source/GUI/MainWindow.cpp @ 50:114427cb39f0

Changes to allow software to be run from the command line (still needs testing)
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Mon, 13 Apr 2015 20:51:17 -0700
parents 85577160a0d4
children ff5d65c69e73
rev   line source
andrewm@0 1 /*
andrewm@0 2 TouchKeys: multi-touch musical keyboard control software
andrewm@0 3 Copyright (c) 2013 Andrew McPherson
andrewm@0 4
andrewm@0 5 This program is free software: you can redistribute it and/or modify
andrewm@0 6 it under the terms of the GNU General Public License as published by
andrewm@0 7 the Free Software Foundation, either version 3 of the License, or
andrewm@0 8 (at your option) any later version.
andrewm@0 9
andrewm@0 10 This program is distributed in the hope that it will be useful,
andrewm@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0 13 GNU General Public License for more details.
andrewm@0 14
andrewm@0 15 You should have received a copy of the GNU General Public License
andrewm@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
andrewm@0 17
andrewm@0 18 =====================================================================
andrewm@0 19
andrewm@0 20 MainWindow.cpp: the control window, plus menu bar and Juce application methods
andrewm@0 21 */
andrewm@0 22
andrewm@50 23 #ifndef TOUCHKEYS_NO_GUI
andrewm@50 24
andrewm@0 25 #include "../../JuceLibraryCode/JuceHeader.h"
andrewm@0 26 #include "MainWindow.h"
andrewm@0 27
andrewm@0 28 //==============================================================================
andrewm@0 29 MainWindow::MainWindow(MainApplicationController& controller)
andrewm@0 30 : DocumentWindow ("TouchKeys", Colours::lightgrey, DocumentWindow::allButtons),
andrewm@0 31 controller_(controller)
andrewm@0 32 {
andrewm@0 33 commandManager_.registerAllCommandsForTarget(this);
andrewm@0 34 commandManager_.registerAllCommandsForTarget(JUCEApplication::getInstance());
andrewm@0 35
andrewm@0 36 // This lets the command manager use keypresses that arrive in our window to send
andrewm@0 37 // out commands
andrewm@0 38 addKeyListener(commandManager_.getKeyMappings());
andrewm@0 39
andrewm@0 40 // Create the menu bar, either in the window or at the top of the screen
andrewm@0 41 #if JUCE_MAC
andrewm@0 42 MenuBarModel::setMacMainMenu(this);
andrewm@0 43 #else
andrewm@0 44 // Menu bar goes at the top of the window
andrewm@0 45 setMenuBar(this);
andrewm@0 46
andrewm@0 47 // Window height should change to accommodate it
andrewm@0 48 Component *actualMenuBar = getMenuBarComponent();
andrewm@0 49 if(actualMenuBar != 0) {
andrewm@20 50 juce::Rectangle<int> r = actualMenuBar->getBounds();
andrewm@0 51 setSize(getWidth(), getHeight() + r.getHeight());
andrewm@0 52 }
andrewm@0 53 #endif /* JUCE_MAC */
andrewm@0 54
andrewm@0 55 // Tells our menu bar model that it should watch this command manager for
andrewm@0 56 // changes, and send change messages accordingly.
andrewm@0 57 setApplicationCommandManagerToWatch(&commandManager_);
andrewm@0 58
andrewm@0 59 mainComponent_.setMainApplicationController(&controller_);
andrewm@0 60 setContentNonOwned(&mainComponent_, true);
andrewm@0 61
andrewm@0 62 commandManager_.setFirstCommandTarget(this);
andrewm@0 63
andrewm@0 64 // Start a timer that will keep the interface in sync with the application
andrewm@0 65 startTimer(50);
andrewm@0 66
andrewm@0 67 centreWithSize (getWidth(), getHeight());
andrewm@0 68 setUsingNativeTitleBar(true);
andrewm@31 69
andrewm@31 70 // Allow resizing to be larger vertically
andrewm@31 71 setResizable(true, true);
andrewm@31 72 setResizeLimits(getWidth(), getHeight(), getWidth(), 2048);
andrewm@31 73
andrewm@0 74 setVisible (true);
andrewm@0 75 }
andrewm@0 76
andrewm@0 77 MainWindow::~MainWindow() {
andrewm@0 78 // Delete the menu bar before exiting
andrewm@0 79 #if JUCE_MAC
andrewm@0 80 MenuBarModel::setMacMainMenu(nullptr);
andrewm@0 81 #else
andrewm@0 82 setMenuBar(nullptr);
andrewm@0 83 #endif /* JUCE_MAC */
andrewm@0 84 }
andrewm@0 85
andrewm@0 86 // Handle periodic GUI updates, keeping the GUI in sync with the underlying system
andrewm@0 87 void MainWindow::timerCallback() {
andrewm@0 88 mainComponent_.synchronize();
andrewm@0 89 }
andrewm@0 90
andrewm@0 91 // ***** Juce menu bar methods ****
andrewm@0 92 StringArray MainWindow::getMenuBarNames() {
andrewm@0 93 const char* const names[] = { "File", "Edit", "Control", "Window", nullptr };
andrewm@0 94
andrewm@0 95 return StringArray (names);
andrewm@0 96 }
andrewm@0 97
andrewm@0 98 PopupMenu MainWindow::getMenuForIndex(int menuIndex, const String& menuName) {
andrewm@0 99 PopupMenu menu;
andrewm@0 100
andrewm@0 101 if(menuIndex == 0) { // File
andrewm@33 102 menu.addCommandItem(&commandManager_, kCommandClearPreset);
andrewm@0 103 menu.addSeparator();
andrewm@0 104 menu.addCommandItem(&commandManager_, kCommandOpenPreset);
andrewm@0 105 menu.addCommandItem(&commandManager_, kCommandSavePreset);
andrewm@0 106 #ifndef JUCE_MAC
andrewm@0 107 menu.addSeparator();
andrewm@0 108 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::quit);
andrewm@0 109 #endif
andrewm@0 110 }
andrewm@0 111 else if(menuIndex == 1) { // Edit
andrewm@0 112 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::undo);
andrewm@0 113 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::redo);
andrewm@0 114 menu.addSeparator();
andrewm@0 115 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::cut);
andrewm@0 116 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::copy);
andrewm@0 117 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::paste);
andrewm@0 118 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::del);
andrewm@0 119 menu.addSeparator();
andrewm@0 120 menu.addCommandItem(&commandManager_, StandardApplicationCommandIDs::selectAll);
andrewm@0 121 }
andrewm@0 122 else if(menuIndex == 2) { // Control
andrewm@0 123 menu.addCommandItem(&commandManager_, kCommandRescanDevices);
andrewm@0 124 menu.addSeparator();
andrewm@31 125 menu.addCommandItem(&commandManager_, kCommandLoggingStartStop);
andrewm@31 126 menu.addCommandItem(&commandManager_, kCommandLoggingPlay);
andrewm@31 127 menu.addSeparator();
andrewm@0 128 menu.addCommandItem(&commandManager_, kCommandEnableExperimentalMappings);
andrewm@17 129 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 130 menu.addCommandItem(&commandManager_, kCommandTestTouchkeySensors);
andrewm@17 131 #endif
andrewm@41 132 menu.addSeparator();
andrewm@41 133 menu.addCommandItem(&commandManager_, kCommandPreferences);
andrewm@0 134 }
andrewm@0 135 else if(menuIndex == 3) { // Window
andrewm@0 136 menu.addCommandItem(&commandManager_, kCommandShowControlWindow);
andrewm@0 137 menu.addCommandItem(&commandManager_, kCommandShowKeyboardWindow);
andrewm@0 138 }
andrewm@0 139
andrewm@0 140 return menu;
andrewm@0 141 }
andrewm@0 142
andrewm@0 143 void MainWindow::menuItemSelected(int menuItemID, int topLevelMenuIndex) {
andrewm@0 144 // TODO
andrewm@0 145 }
andrewm@0 146
andrewm@0 147 // **** Command Manager methods *****
andrewm@0 148
andrewm@0 149 ApplicationCommandTarget* MainWindow::getNextCommandTarget() {
andrewm@0 150 return findFirstTargetParentComponent();
andrewm@0 151 }
andrewm@0 152
andrewm@0 153 // this returns the set of all commands that this target can perform..
andrewm@0 154 void MainWindow::getAllCommands(Array <CommandID>& commands) {
andrewm@0 155 // this returns the set of all commands that this target can perform..
andrewm@0 156 const CommandID ids[] = {
andrewm@0 157 // File
andrewm@33 158 kCommandClearPreset,
andrewm@33 159 kCommandOpenPreset,
andrewm@33 160 kCommandSavePreset,
andrewm@0 161 // Edit
andrewm@0 162 StandardApplicationCommandIDs::undo,
andrewm@0 163 StandardApplicationCommandIDs::redo,
andrewm@0 164 StandardApplicationCommandIDs::cut,
andrewm@0 165 StandardApplicationCommandIDs::copy,
andrewm@0 166 StandardApplicationCommandIDs::paste,
andrewm@0 167 StandardApplicationCommandIDs::del,
andrewm@0 168 StandardApplicationCommandIDs::selectAll,
andrewm@0 169 // Control
andrewm@0 170 kCommandRescanDevices,
andrewm@31 171 kCommandLoggingStartStop,
andrewm@31 172 kCommandLoggingPlay,
andrewm@0 173 kCommandEnableExperimentalMappings,
andrewm@17 174 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 175 kCommandTestTouchkeySensors,
andrewm@17 176 #endif
andrewm@41 177 kCommandPreferences,
andrewm@41 178
andrewm@0 179 // Window
andrewm@0 180 kCommandShowControlWindow,
andrewm@0 181 kCommandShowKeyboardWindow
andrewm@0 182 };
andrewm@0 183
andrewm@0 184 commands.addArray (ids, numElementsInArray(ids));
andrewm@0 185 }
andrewm@0 186
andrewm@0 187 // This method is used when something needs to find out the details about one of the commands
andrewm@0 188 // that this object can perform..
andrewm@0 189 void MainWindow::getCommandInfo(CommandID commandID, ApplicationCommandInfo& result) {
andrewm@0 190 const String presetsCategory("Presets");
andrewm@0 191 const String editsCategory("Editing");
andrewm@0 192 const String controlCategory("Control");
andrewm@0 193 const String windowCategory("Window");
andrewm@0 194
andrewm@0 195 switch (commandID) {
andrewm@0 196
andrewm@0 197 // *** File Menu ***
andrewm@33 198 case kCommandClearPreset:
andrewm@33 199 result.setInfo("Clear Settings", "Clears the current settings", presetsCategory, 0);
andrewm@0 200 result.setTicked(false);
andrewm@37 201 result.setActive(true);
andrewm@0 202 break;
andrewm@0 203 case kCommandOpenPreset:
andrewm@0 204 result.setInfo("Open Preset...", "Opens an existing preset", presetsCategory, 0);
andrewm@0 205 result.setTicked(false);
andrewm@33 206 result.setActive(true);
andrewm@0 207 result.addDefaultKeypress ('O', ModifierKeys::commandModifier);
andrewm@0 208 break;
andrewm@0 209 case kCommandSavePreset:
andrewm@33 210 result.setInfo("Save Preset...", "Saves the current preset", presetsCategory, 0);
andrewm@0 211 result.setTicked(false);
andrewm@33 212 result.setActive(true);
andrewm@0 213 result.addDefaultKeypress ('S', ModifierKeys::commandModifier);
andrewm@0 214 break;
andrewm@0 215 // Quit command is handled by JuceApplication
andrewm@0 216
andrewm@0 217 // *** Edit Menu ***
andrewm@0 218 case StandardApplicationCommandIDs::undo:
andrewm@0 219 result.setInfo("Undo", "Undo", editsCategory, 0);
andrewm@0 220 result.setTicked(false);
andrewm@0 221 result.setActive(false);
andrewm@0 222 result.addDefaultKeypress ('Z', ModifierKeys::commandModifier);
andrewm@0 223 break;
andrewm@0 224 case StandardApplicationCommandIDs::redo:
andrewm@0 225 result.setInfo("Redo", "Redo", editsCategory, 0);
andrewm@0 226 result.setTicked(false);
andrewm@0 227 result.setActive(false);
andrewm@0 228 result.addDefaultKeypress ('Z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier);
andrewm@0 229 break;
andrewm@0 230 case StandardApplicationCommandIDs::cut:
andrewm@0 231 result.setInfo("Cut", "Cut", editsCategory, 0);
andrewm@0 232 result.setTicked(false);
andrewm@0 233 result.setActive(false);
andrewm@0 234 result.addDefaultKeypress ('X', ModifierKeys::commandModifier);
andrewm@0 235 break;
andrewm@0 236 case StandardApplicationCommandIDs::copy:
andrewm@0 237 result.setInfo("Copy", "Copy", editsCategory, 0);
andrewm@0 238 result.setTicked(false);
andrewm@0 239 result.setActive(false);
andrewm@0 240 result.addDefaultKeypress ('C', ModifierKeys::commandModifier);
andrewm@0 241 break;
andrewm@0 242 case StandardApplicationCommandIDs::paste:
andrewm@0 243 result.setInfo("Paste", "Paste", editsCategory, 0);
andrewm@0 244 result.setTicked(false);
andrewm@0 245 result.setActive(false);
andrewm@0 246 result.addDefaultKeypress ('V', ModifierKeys::commandModifier);
andrewm@0 247 break;
andrewm@0 248 case StandardApplicationCommandIDs::del:
andrewm@0 249 result.setInfo("Delete", "Delete", editsCategory, 0);
andrewm@0 250 result.setTicked(false);
andrewm@0 251 result.setActive(false);
andrewm@0 252 break;
andrewm@0 253 case StandardApplicationCommandIDs::selectAll:
andrewm@0 254 result.setInfo("Select All", "Select All", editsCategory, 0);
andrewm@0 255 result.setTicked(false);
andrewm@0 256 result.setActive(false);
andrewm@0 257 result.addDefaultKeypress ('A', ModifierKeys::commandModifier);
andrewm@0 258 break;
andrewm@0 259
andrewm@0 260 // *** Control Menu ***
andrewm@0 261 case kCommandRescanDevices:
andrewm@0 262 result.setInfo("Rescan Devices", "Rescans available TouchKeys and MIDI devices", controlCategory, 0);
andrewm@0 263 result.setTicked(false);
andrewm@28 264 result.setActive(true);
andrewm@0 265 result.addDefaultKeypress ('R', ModifierKeys::commandModifier);
andrewm@0 266 break;
andrewm@31 267 case kCommandLoggingStartStop:
andrewm@31 268 result.setInfo("Record Log File", "Records TouchKeys and MIDI data to file", controlCategory, 0);
andrewm@31 269 result.setTicked(controller_.isLogging());
andrewm@31 270 result.setActive(true);
andrewm@31 271 break;
andrewm@31 272 case kCommandLoggingPlay:
andrewm@31 273 result.setInfo("Play Log...", "Plays TouchKeys and MIDI from file", controlCategory, 0);
andrewm@31 274 result.setTicked(false);
andrewm@31 275 result.setActive(false);
andrewm@31 276 break;
andrewm@0 277 case kCommandEnableExperimentalMappings:
andrewm@0 278 result.setInfo("Enable Experimental Mappings", "Enables mappings which are still experimental", controlCategory, 0);
andrewm@0 279 result.setTicked(controller_.experimentalMappingsEnabled());
andrewm@0 280 break;
andrewm@17 281 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 282 case kCommandTestTouchkeySensors:
andrewm@17 283 result.setInfo("Test TouchKeys Sensors", "Enables a test of individual TouchKeys sensors", controlCategory, 0);
andrewm@17 284 result.setActive(controller_.availableTouchkeyDevices().size() > 0);
andrewm@17 285 result.setTicked(controller_.touchkeySensorTestIsRunning());
andrewm@17 286 break;
andrewm@17 287 #endif
andrewm@41 288 case kCommandPreferences:
andrewm@41 289 result.setInfo("Preferences...", "General application preferences", controlCategory, 0);
andrewm@41 290 result.setTicked(false);
andrewm@41 291 result.setActive(true);
andrewm@41 292 break;
andrewm@0 293
andrewm@0 294 // *** Window Menu ***
andrewm@0 295 case kCommandShowControlWindow:
andrewm@0 296 result.setInfo("TouchKeys Controls", "Show control and mapping window", windowCategory, 0);
andrewm@0 297 result.setTicked(false);
andrewm@12 298 result.addDefaultKeypress ('1', ModifierKeys::commandModifier);
andrewm@0 299 break;
andrewm@0 300 case kCommandShowKeyboardWindow:
andrewm@0 301 result.setInfo("Keyboard Display", "Show keyboard display", windowCategory, 0);
andrewm@0 302 result.setTicked(false);
andrewm@12 303 result.addDefaultKeypress ('2', ModifierKeys::commandModifier);
andrewm@0 304 break;
andrewm@0 305
andrewm@0 306 default:
andrewm@0 307 break;
andrewm@0 308 };
andrewm@0 309 }
andrewm@0 310
andrewm@0 311 bool MainWindow::perform(const InvocationInfo& info) {
andrewm@0 312 switch (info.commandID)
andrewm@0 313 {
andrewm@33 314 case kCommandClearPreset:
andrewm@37 315 // Display an alert to confirm the user wants to delete this mapping
andrewm@37 316 AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
andrewm@37 317 "Clear settings",
andrewm@37 318 "Are you sure you want to clear all zones and mappings?",
andrewm@37 319 String::empty,
andrewm@37 320 String::empty,
andrewm@37 321 0,
andrewm@37 322 ModalCallbackFunction::forComponent(alertBoxResultChosen, this));
andrewm@33 323 break;
andrewm@33 324 case kCommandOpenPreset:
andrewm@33 325 controller_.loadPresetWithDialog();
andrewm@33 326 break;
andrewm@33 327 case kCommandSavePreset:
andrewm@33 328 controller_.savePresetWithDialog();
andrewm@0 329 break;
andrewm@0 330 case kCommandRescanDevices:
andrewm@28 331 controller_.tellDevicesToUpdate();
andrewm@0 332 break;
andrewm@31 333 case kCommandLoggingStartStop:
andrewm@31 334 if(controller_.isLogging())
andrewm@31 335 controller_.stopLogging();
andrewm@31 336 else
andrewm@31 337 controller_.startLogging();
andrewm@31 338 break;
andrewm@31 339 case kCommandLoggingPlay:
andrewm@31 340 // TODO
andrewm@31 341 break;
andrewm@0 342 case kCommandEnableExperimentalMappings:
andrewm@0 343 controller_.setExperimentalMappingsEnabled(!controller_.experimentalMappingsEnabled());
andrewm@0 344 break;
andrewm@17 345 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 346 case kCommandTestTouchkeySensors:
andrewm@17 347 if(!controller_.touchkeySensorTestIsRunning())
andrewm@17 348 controller_.touchkeySensorTestStart(mainComponent_.currentTouchkeysSelectedPath().toUTF8(), controller_.touchkeyDeviceLowestMidiNote());
andrewm@17 349 else
andrewm@17 350 controller_.touchkeySensorTestStop();
andrewm@17 351 break;
andrewm@17 352 #endif
andrewm@41 353 case kCommandPreferences:
andrewm@41 354 controller_.showPreferencesWindow();
andrewm@41 355 break;
andrewm@0 356 case kCommandShowControlWindow:
andrewm@0 357 toFront(true);
andrewm@0 358 break;
andrewm@0 359 case kCommandShowKeyboardWindow:
andrewm@0 360 controller_.showKeyboardDisplayWindow();
andrewm@0 361 break;
andrewm@0 362
andrewm@0 363 default:
andrewm@0 364 return false;
andrewm@0 365 };
andrewm@0 366
andrewm@0 367 return true;
andrewm@0 368 }
andrewm@37 369
andrewm@37 370 // Called when user clicks a result in the alert box to confirm deletion
andrewm@37 371 void MainWindow::alertBoxResultChosen(int result, MainWindow *item) {
andrewm@37 372 if(result != 0) {
andrewm@37 373 item->clearPreset();
andrewm@37 374 }
andrewm@37 375 }
andrewm@50 376
andrewm@50 377 #endif // TOUCHKEYS_NO_GUI