annotate Source/GUI/MainWindow.cpp @ 56:b4a2d2ae43cf tip

merge
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Fri, 23 Nov 2018 15:48:14 +0000
parents ff5d65c69e73
children
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@53 132 #ifdef ENABLE_TOUCHKEYS_FIRMWARE_UPDATE
andrewm@53 133 menu.addCommandItem(&commandManager_, kCommandJumpToBootloader);
andrewm@53 134 #endif
andrewm@41 135 menu.addSeparator();
andrewm@41 136 menu.addCommandItem(&commandManager_, kCommandPreferences);
andrewm@0 137 }
andrewm@0 138 else if(menuIndex == 3) { // Window
andrewm@0 139 menu.addCommandItem(&commandManager_, kCommandShowControlWindow);
andrewm@0 140 menu.addCommandItem(&commandManager_, kCommandShowKeyboardWindow);
andrewm@0 141 }
andrewm@0 142
andrewm@0 143 return menu;
andrewm@0 144 }
andrewm@0 145
andrewm@0 146 void MainWindow::menuItemSelected(int menuItemID, int topLevelMenuIndex) {
andrewm@0 147 // TODO
andrewm@0 148 }
andrewm@0 149
andrewm@0 150 // **** Command Manager methods *****
andrewm@0 151
andrewm@0 152 ApplicationCommandTarget* MainWindow::getNextCommandTarget() {
andrewm@0 153 return findFirstTargetParentComponent();
andrewm@0 154 }
andrewm@0 155
andrewm@0 156 // this returns the set of all commands that this target can perform..
andrewm@0 157 void MainWindow::getAllCommands(Array <CommandID>& commands) {
andrewm@0 158 // this returns the set of all commands that this target can perform..
andrewm@0 159 const CommandID ids[] = {
andrewm@0 160 // File
andrewm@33 161 kCommandClearPreset,
andrewm@33 162 kCommandOpenPreset,
andrewm@33 163 kCommandSavePreset,
andrewm@0 164 // Edit
andrewm@0 165 StandardApplicationCommandIDs::undo,
andrewm@0 166 StandardApplicationCommandIDs::redo,
andrewm@0 167 StandardApplicationCommandIDs::cut,
andrewm@0 168 StandardApplicationCommandIDs::copy,
andrewm@0 169 StandardApplicationCommandIDs::paste,
andrewm@0 170 StandardApplicationCommandIDs::del,
andrewm@0 171 StandardApplicationCommandIDs::selectAll,
andrewm@0 172 // Control
andrewm@0 173 kCommandRescanDevices,
andrewm@31 174 kCommandLoggingStartStop,
andrewm@31 175 kCommandLoggingPlay,
andrewm@0 176 kCommandEnableExperimentalMappings,
andrewm@17 177 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 178 kCommandTestTouchkeySensors,
andrewm@17 179 #endif
andrewm@53 180 #ifdef ENABLE_TOUCHKEYS_FIRMWARE_UPDATE
andrewm@53 181 kCommandJumpToBootloader,
andrewm@53 182 #endif
andrewm@41 183 kCommandPreferences,
andrewm@41 184
andrewm@0 185 // Window
andrewm@0 186 kCommandShowControlWindow,
andrewm@0 187 kCommandShowKeyboardWindow
andrewm@0 188 };
andrewm@0 189
andrewm@0 190 commands.addArray (ids, numElementsInArray(ids));
andrewm@0 191 }
andrewm@0 192
andrewm@0 193 // This method is used when something needs to find out the details about one of the commands
andrewm@0 194 // that this object can perform..
andrewm@0 195 void MainWindow::getCommandInfo(CommandID commandID, ApplicationCommandInfo& result) {
andrewm@0 196 const String presetsCategory("Presets");
andrewm@0 197 const String editsCategory("Editing");
andrewm@0 198 const String controlCategory("Control");
andrewm@0 199 const String windowCategory("Window");
andrewm@0 200
andrewm@0 201 switch (commandID) {
andrewm@0 202
andrewm@0 203 // *** File Menu ***
andrewm@33 204 case kCommandClearPreset:
andrewm@33 205 result.setInfo("Clear Settings", "Clears the current settings", presetsCategory, 0);
andrewm@0 206 result.setTicked(false);
andrewm@37 207 result.setActive(true);
andrewm@0 208 break;
andrewm@0 209 case kCommandOpenPreset:
andrewm@0 210 result.setInfo("Open Preset...", "Opens an existing preset", presetsCategory, 0);
andrewm@0 211 result.setTicked(false);
andrewm@33 212 result.setActive(true);
andrewm@0 213 result.addDefaultKeypress ('O', ModifierKeys::commandModifier);
andrewm@0 214 break;
andrewm@0 215 case kCommandSavePreset:
andrewm@33 216 result.setInfo("Save Preset...", "Saves the current preset", presetsCategory, 0);
andrewm@0 217 result.setTicked(false);
andrewm@33 218 result.setActive(true);
andrewm@0 219 result.addDefaultKeypress ('S', ModifierKeys::commandModifier);
andrewm@0 220 break;
andrewm@0 221 // Quit command is handled by JuceApplication
andrewm@0 222
andrewm@0 223 // *** Edit Menu ***
andrewm@0 224 case StandardApplicationCommandIDs::undo:
andrewm@0 225 result.setInfo("Undo", "Undo", editsCategory, 0);
andrewm@0 226 result.setTicked(false);
andrewm@0 227 result.setActive(false);
andrewm@0 228 result.addDefaultKeypress ('Z', ModifierKeys::commandModifier);
andrewm@0 229 break;
andrewm@0 230 case StandardApplicationCommandIDs::redo:
andrewm@0 231 result.setInfo("Redo", "Redo", editsCategory, 0);
andrewm@0 232 result.setTicked(false);
andrewm@0 233 result.setActive(false);
andrewm@0 234 result.addDefaultKeypress ('Z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier);
andrewm@0 235 break;
andrewm@0 236 case StandardApplicationCommandIDs::cut:
andrewm@0 237 result.setInfo("Cut", "Cut", editsCategory, 0);
andrewm@0 238 result.setTicked(false);
andrewm@0 239 result.setActive(false);
andrewm@0 240 result.addDefaultKeypress ('X', ModifierKeys::commandModifier);
andrewm@0 241 break;
andrewm@0 242 case StandardApplicationCommandIDs::copy:
andrewm@0 243 result.setInfo("Copy", "Copy", editsCategory, 0);
andrewm@0 244 result.setTicked(false);
andrewm@0 245 result.setActive(false);
andrewm@0 246 result.addDefaultKeypress ('C', ModifierKeys::commandModifier);
andrewm@0 247 break;
andrewm@0 248 case StandardApplicationCommandIDs::paste:
andrewm@0 249 result.setInfo("Paste", "Paste", editsCategory, 0);
andrewm@0 250 result.setTicked(false);
andrewm@0 251 result.setActive(false);
andrewm@0 252 result.addDefaultKeypress ('V', ModifierKeys::commandModifier);
andrewm@0 253 break;
andrewm@0 254 case StandardApplicationCommandIDs::del:
andrewm@0 255 result.setInfo("Delete", "Delete", editsCategory, 0);
andrewm@0 256 result.setTicked(false);
andrewm@0 257 result.setActive(false);
andrewm@0 258 break;
andrewm@0 259 case StandardApplicationCommandIDs::selectAll:
andrewm@0 260 result.setInfo("Select All", "Select All", editsCategory, 0);
andrewm@0 261 result.setTicked(false);
andrewm@0 262 result.setActive(false);
andrewm@0 263 result.addDefaultKeypress ('A', ModifierKeys::commandModifier);
andrewm@0 264 break;
andrewm@0 265
andrewm@0 266 // *** Control Menu ***
andrewm@0 267 case kCommandRescanDevices:
andrewm@0 268 result.setInfo("Rescan Devices", "Rescans available TouchKeys and MIDI devices", controlCategory, 0);
andrewm@0 269 result.setTicked(false);
andrewm@28 270 result.setActive(true);
andrewm@0 271 result.addDefaultKeypress ('R', ModifierKeys::commandModifier);
andrewm@0 272 break;
andrewm@31 273 case kCommandLoggingStartStop:
andrewm@31 274 result.setInfo("Record Log File", "Records TouchKeys and MIDI data to file", controlCategory, 0);
andrewm@31 275 result.setTicked(controller_.isLogging());
andrewm@53 276 result.setActive(!controller_.isPlayingLog());
andrewm@31 277 break;
andrewm@31 278 case kCommandLoggingPlay:
andrewm@31 279 result.setInfo("Play Log...", "Plays TouchKeys and MIDI from file", controlCategory, 0);
andrewm@53 280 result.setTicked(controller_.isPlayingLog());
andrewm@53 281 result.setActive(!controller_.isLogging());
andrewm@31 282 break;
andrewm@0 283 case kCommandEnableExperimentalMappings:
andrewm@0 284 result.setInfo("Enable Experimental Mappings", "Enables mappings which are still experimental", controlCategory, 0);
andrewm@0 285 result.setTicked(controller_.experimentalMappingsEnabled());
andrewm@0 286 break;
andrewm@17 287 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 288 case kCommandTestTouchkeySensors:
andrewm@17 289 result.setInfo("Test TouchKeys Sensors", "Enables a test of individual TouchKeys sensors", controlCategory, 0);
andrewm@17 290 result.setActive(controller_.availableTouchkeyDevices().size() > 0);
andrewm@17 291 result.setTicked(controller_.touchkeySensorTestIsRunning());
andrewm@17 292 break;
andrewm@17 293 #endif
andrewm@53 294 #ifdef ENABLE_TOUCHKEYS_FIRMWARE_UPDATE
andrewm@53 295 case kCommandJumpToBootloader:
andrewm@53 296 result.setInfo("Go to Firmware Update Mode", "Puts the TouchKeys in firmware update mode", controlCategory, 0);
andrewm@53 297 result.setActive(controller_.availableTouchkeyDevices().size() > 0);
andrewm@53 298 result.setTicked(false);
andrewm@53 299 break;
andrewm@53 300 #endif
andrewm@41 301 case kCommandPreferences:
andrewm@41 302 result.setInfo("Preferences...", "General application preferences", controlCategory, 0);
andrewm@41 303 result.setTicked(false);
andrewm@41 304 result.setActive(true);
andrewm@41 305 break;
andrewm@0 306
andrewm@0 307 // *** Window Menu ***
andrewm@0 308 case kCommandShowControlWindow:
andrewm@0 309 result.setInfo("TouchKeys Controls", "Show control and mapping window", windowCategory, 0);
andrewm@0 310 result.setTicked(false);
andrewm@12 311 result.addDefaultKeypress ('1', ModifierKeys::commandModifier);
andrewm@0 312 break;
andrewm@0 313 case kCommandShowKeyboardWindow:
andrewm@0 314 result.setInfo("Keyboard Display", "Show keyboard display", windowCategory, 0);
andrewm@0 315 result.setTicked(false);
andrewm@12 316 result.addDefaultKeypress ('2', ModifierKeys::commandModifier);
andrewm@0 317 break;
andrewm@0 318
andrewm@0 319 default:
andrewm@0 320 break;
andrewm@0 321 };
andrewm@0 322 }
andrewm@0 323
andrewm@0 324 bool MainWindow::perform(const InvocationInfo& info) {
andrewm@0 325 switch (info.commandID)
andrewm@0 326 {
andrewm@33 327 case kCommandClearPreset:
andrewm@37 328 // Display an alert to confirm the user wants to delete this mapping
andrewm@37 329 AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
andrewm@37 330 "Clear settings",
andrewm@37 331 "Are you sure you want to clear all zones and mappings?",
andrewm@37 332 String::empty,
andrewm@37 333 String::empty,
andrewm@37 334 0,
andrewm@37 335 ModalCallbackFunction::forComponent(alertBoxResultChosen, this));
andrewm@33 336 break;
andrewm@33 337 case kCommandOpenPreset:
andrewm@33 338 controller_.loadPresetWithDialog();
andrewm@33 339 break;
andrewm@33 340 case kCommandSavePreset:
andrewm@33 341 controller_.savePresetWithDialog();
andrewm@0 342 break;
andrewm@0 343 case kCommandRescanDevices:
andrewm@28 344 controller_.tellDevicesToUpdate();
andrewm@0 345 break;
andrewm@31 346 case kCommandLoggingStartStop:
andrewm@31 347 if(controller_.isLogging())
andrewm@31 348 controller_.stopLogging();
andrewm@31 349 else
andrewm@31 350 controller_.startLogging();
andrewm@31 351 break;
andrewm@31 352 case kCommandLoggingPlay:
andrewm@53 353 if(controller_.isPlayingLog())
andrewm@53 354 controller_.stopPlayingLog();
andrewm@53 355 else
andrewm@53 356 controller_.playLogWithDialog();
andrewm@31 357 break;
andrewm@0 358 case kCommandEnableExperimentalMappings:
andrewm@0 359 controller_.setExperimentalMappingsEnabled(!controller_.experimentalMappingsEnabled());
andrewm@0 360 break;
andrewm@17 361 #ifdef ENABLE_TOUCHKEYS_SENSOR_TEST
andrewm@17 362 case kCommandTestTouchkeySensors:
andrewm@17 363 if(!controller_.touchkeySensorTestIsRunning())
andrewm@17 364 controller_.touchkeySensorTestStart(mainComponent_.currentTouchkeysSelectedPath().toUTF8(), controller_.touchkeyDeviceLowestMidiNote());
andrewm@17 365 else
andrewm@17 366 controller_.touchkeySensorTestStop();
andrewm@17 367 break;
andrewm@17 368 #endif
andrewm@53 369 #ifdef ENABLE_TOUCHKEYS_FIRMWARE_UPDATE
andrewm@53 370 case kCommandJumpToBootloader:
andrewm@53 371 controller_.touchkeyJumpToBootloader(mainComponent_.currentTouchkeysSelectedPath().toUTF8());
andrewm@53 372 break;
andrewm@53 373 #endif
andrewm@41 374 case kCommandPreferences:
andrewm@41 375 controller_.showPreferencesWindow();
andrewm@41 376 break;
andrewm@0 377 case kCommandShowControlWindow:
andrewm@0 378 toFront(true);
andrewm@0 379 break;
andrewm@0 380 case kCommandShowKeyboardWindow:
andrewm@0 381 controller_.showKeyboardDisplayWindow();
andrewm@0 382 break;
andrewm@0 383
andrewm@0 384 default:
andrewm@0 385 return false;
andrewm@0 386 };
andrewm@0 387
andrewm@0 388 return true;
andrewm@0 389 }
andrewm@37 390
andrewm@37 391 // Called when user clicks a result in the alert box to confirm deletion
andrewm@37 392 void MainWindow::alertBoxResultChosen(int result, MainWindow *item) {
andrewm@37 393 if(result != 0) {
andrewm@37 394 item->clearPreset();
andrewm@37 395 }
andrewm@37 396 }
andrewm@50 397
andrewm@50 398 #endif // TOUCHKEYS_NO_GUI