| 1 |
/* gtkmm example Copyright (C) 2016 gtkmm development team |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "exampleapplication.h" |
| 18 |
#include "exampleappwindow.h" |
| 19 |
#include <iostream> |
| 20 |
#include <exception> |
| 21 |
|
| 22 |
ExampleApplication::ExampleApplication() |
| 23 |
: Gtk::Application("org.gtkmm.examples.application", Gio::Application::Flags::HANDLES_OPEN) |
| 24 |
{ |
| 25 |
// Delete a window when it is removed from the application. |
| 26 |
signal_window_removed().connect([](Gtk::Window* window) { delete window; }); |
| 27 |
} |
| 28 |
|
| 29 |
Glib::RefPtr<ExampleApplication> ExampleApplication::create() |
| 30 |
{ |
| 31 |
return Glib::make_refptr_for_instance<ExampleApplication>(new ExampleApplication()); |
| 32 |
} |
| 33 |
|
| 34 |
ExampleAppWindow* ExampleApplication::create_appwindow() |
| 35 |
{ |
| 36 |
auto appwindow = ExampleAppWindow::create(); |
| 37 |
|
| 38 |
// Make sure that the application runs for as long this window is still open. |
| 39 |
add_window(*appwindow); |
| 40 |
|
| 41 |
// A window can be added to an application with Gtk::Application::add_window() |
| 42 |
// or Gtk::Window::set_application(). When all added windows have been removed, |
| 43 |
// the application stops running (Gtk::Application::run() returns), |
| 44 |
// unless Gio::Application::hold() has been called. |
| 45 |
|
| 46 |
return appwindow; |
| 47 |
} |
| 48 |
|
| 49 |
void ExampleApplication::on_activate() |
| 50 |
{ |
| 51 |
try |
| 52 |
{ |
| 53 |
// The application has been started, so let's show a window. |
| 54 |
auto appwindow = create_appwindow(); |
| 55 |
appwindow->present(); |
| 56 |
} |
| 57 |
// If create_appwindow() throws an exception (perhaps from Gtk::Builder), |
| 58 |
// no window has been created, no window has been added to the application, |
| 59 |
// and therefore the application will stop running. |
| 60 |
catch (const Glib::Error& ex) |
| 61 |
{ |
| 62 |
std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl; |
| 63 |
} |
| 64 |
catch (const std::exception& ex) |
| 65 |
{ |
| 66 |
std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void ExampleApplication::on_open(const Gio::Application::type_vec_files& files, |
| 71 |
const Glib::ustring& /* hint */) |
| 72 |
{ |
| 73 |
// The application has been asked to open some files, |
| 74 |
// so let's open a new view for each one. |
| 75 |
ExampleAppWindow* appwindow = nullptr; |
| 76 |
auto windows = get_windows(); |
| 77 |
if (windows.size() > 0) |
| 78 |
appwindow = dynamic_cast<ExampleAppWindow*>(windows[0]); |
| 79 |
|
| 80 |
try |
| 81 |
{ |
| 82 |
if (!appwindow) |
| 83 |
appwindow = create_appwindow(); |
| 84 |
|
| 85 |
for (const auto& file : files) |
| 86 |
appwindow->open_file_view(file); |
| 87 |
|
| 88 |
appwindow->present(); |
| 89 |
} |
| 90 |
catch (const Glib::Error& ex) |
| 91 |
{ |
| 92 |
std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl; |
| 93 |
} |
| 94 |
catch (const std::exception& ex) |
| 95 |
{ |
| 96 |
std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl; |
| 97 |
} |
| 98 |
} |