Newer
Older
Yogi
committed
------------------------------------------------------------------
Yogi
committed
This file is part of the Open Ephys GUI
Copyright (C) 2014 Open Ephys
Yogi
committed
------------------------------------------------------------------
Yogi
committed
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Yogi
committed
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Yogi
committed
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Yogi
committed
*/
#include "UI/UIComponent.h"
#include "UI/EditorViewport.h"
//-----------------------------------------------------------------------
Christopher Stawarz
committed
static inline File getSavedStateDirectory() {
#if defined(__APPLE__)
File dir = File::getSpecialLocation(File::userApplicationDataDirectory).getChildFile("Application Support/open-ephys");
if (!dir.isDirectory()) {
dir.createDirectory();
}
return std::move(dir);
#else
return File::getSpecialLocation(File::currentExecutableFile).getParentDirectory();
#endif
}
Yogi
committed
MainWindow::MainWindow()
: DocumentWindow(JUCEApplication::getInstance()->getApplicationName(),
Colour(Colours::black),
DocumentWindow::allButtons)
Yogi
committed
setResizable(true, // isResizable
false); // useBottomCornerRisizer -- doesn't work very well
Yogi
committed
shouldReloadOnStartup = false;
Yogi
committed
// Create ProcessorGraph and AudioComponent, and connect them.
// Callbacks will be set by the play button in the control panel
Yogi
committed
processorGraph = new ProcessorGraph();
std::cout << std::endl;
std::cout << "Created processor graph." << std::endl;
std::cout << std::endl;
Yogi
committed
audioComponent = new AudioComponent();
std::cout << "Created audio component." << std::endl;
Yogi
committed
audioComponent->connectToProcessorGraph(processorGraph);
Yogi
committed
setContentOwned(new UIComponent(this, processorGraph, audioComponent), true);
Yogi
committed
UIComponent* ui = (UIComponent*) getContentComponent();
Yogi
committed
commandManager.registerAllCommandsForTarget(ui);
commandManager.registerAllCommandsForTarget(JUCEApplication::getInstance());
Yogi
committed
ui->setApplicationCommandManagerToWatch(&commandManager);
Yogi
committed
addKeyListener(commandManager.getKeyMappings());
Yogi
committed
loadWindowBounds();
setUsingNativeTitleBar(true);
Component::addToDesktop(getDesktopWindowStyleFlags()); // prevents the maximize
// button from randomly disappearing
setVisible(true);
Yogi
committed
// Constraining the window's size doesn't seem to work:
setResizeLimits(300, 200, 10000, 10000);
Yogi
committed
if (shouldReloadOnStartup)
{
Christopher Stawarz
committed
File file = getSavedStateDirectory().getChildFile("lastConfig.xml");
Yogi
committed
ui->getEditorViewport()->loadState(file);
}
Yogi
committed
if (audioComponent->callbacksAreActive())
{
audioComponent->endCallbacks();
processorGraph->disableProcessors();
}
Yogi
committed
saveWindowBounds();
Yogi
committed
audioComponent->disconnectProcessorGraph();
UIComponent* ui = (UIComponent*) getContentComponent();
ui->disableDataViewport();
Christopher Stawarz
committed
File file = getSavedStateDirectory().getChildFile("lastConfig.xml");
Yogi
committed
ui->getEditorViewport()->saveState(file);
Yogi
committed
setMenuBar(0);
Yogi
committed
MenuBarModel::setMacMainMenu(0);
}
void MainWindow::closeButtonPressed()
Yogi
committed
if (audioComponent->callbacksAreActive())
{
audioComponent->endCallbacks();
}
Yogi
committed
processorGraph->disableProcessors();
Yogi
committed
JUCEApplication::getInstance()->systemRequestedQuit();
}
void MainWindow::saveWindowBounds()
{
Yogi
committed
std::cout << std::endl;
std::cout << "Saving window bounds." << std::endl;
std::cout << std::endl;
Christopher Stawarz
committed
File file = getSavedStateDirectory().getChildFile("windowState.xml");
Yogi
committed
XmlElement* xml = new XmlElement("MAINWINDOW");
Yogi
committed
xml->setAttribute("version", JUCEApplication::getInstance()->getApplicationVersion());
xml->setAttribute("shouldReloadOnStartup", shouldReloadOnStartup);
Yogi
committed
XmlElement* bounds = new XmlElement("BOUNDS");
bounds->setAttribute("x",getScreenX());
bounds->setAttribute("y",getScreenY());
bounds->setAttribute("w",getContentComponent()->getWidth());
bounds->setAttribute("h",getContentComponent()->getHeight());
bounds->setAttribute("fullscreen", isFullScreen());
Yogi
committed
xml->addChildElement(bounds);
Yogi
committed
XmlElement* recentDirectories = new XmlElement("RECENTDIRECTORYNAMES");
Yogi
committed
UIComponent* ui = (UIComponent*) getContentComponent();
Yogi
committed
StringArray dirs = ui->getRecentlyUsedFilenames();
Yogi
committed
for (int i = 0; i < dirs.size(); i++)
{
XmlElement* directory = new XmlElement("DIRECTORY");
directory->setAttribute("name", dirs[i]);
recentDirectories->addChildElement(directory);
}
Yogi
committed
xml->addChildElement(recentDirectories);
Yogi
committed
String error;
Yogi
committed
if (! xml->writeToFile(file, String::empty))
error = "Couldn't write to file";
Yogi
committed
delete xml;
}
void MainWindow::loadWindowBounds()
{
Yogi
committed
std::cout << std::endl;
std::cout << "Loading window bounds." << std::endl;
std::cout << std::endl;
Christopher Stawarz
committed
File file = getSavedStateDirectory().getChildFile("windowState.xml");
Yogi
committed
XmlDocument doc(file);
XmlElement* xml = doc.getDocumentElement();
Yogi
committed
if (xml == 0 || ! xml->hasTagName("MAINWINDOW"))
{
Yogi
committed
std::cout << "File not found." << std::endl;
delete xml;
centreWithSize(800, 600);
Yogi
committed
}
else
{
Yogi
committed
String description;
Yogi
committed
shouldReloadOnStartup = xml->getBoolAttribute("shouldReloadOnStartup", false);
Yogi
committed
forEachXmlChildElement(*xml, e)
{
Yogi
committed
if (e->hasTagName("BOUNDS"))
{
Yogi
committed
int x = e->getIntAttribute("x");
int y = e->getIntAttribute("y");
int w = e->getIntAttribute("w");
int h = e->getIntAttribute("h");
Yogi
committed
// bool fs = e->getBoolAttribute("fullscreen");
Yogi
committed
// without the correction, you get drift over time
Yogi
committed
setTopLeftPosition(x,y); //Windows doesn't need correction
Yogi
committed
setTopLeftPosition(x,y-27);
Yogi
committed
getContentComponent()->setBounds(0,0,w-10,h-33);
//setFullScreen(fs);
}
else if (e->hasTagName("RECENTDIRECTORYNAMES"))
{
Yogi
committed
StringArray filenames;
Yogi
committed
forEachXmlChildElement(*e, directory)
{
Yogi
committed
if (directory->hasTagName("DIRECTORY"))
{
filenames.add(directory->getStringAttribute("name"));
}
}
Yogi
committed
UIComponent* ui = (UIComponent*) getContentComponent();
ui->setRecentlyUsedFilenames(filenames);
Yogi
committed
}
Yogi
committed
}
Yogi
committed
delete xml;
}
// return "Everything went ok.";