libragephoto/examples/ragephoto-gtkviewer/src/main.cpp

106 lines
3.3 KiB
C++

/*****************************************************************************
* libragephoto RAGE Photo Parser
* Copyright (C) 2021 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include "PhotoViewer.h"
#include <gtkmm/application.h>
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/filefilter.h>
#include <gtkmm/label.h>
#include <gtkmm/window.h>
#include <iostream>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "de.syping.ragephoto.gtkviewer", Gio::APPLICATION_HANDLES_OPEN);
Gtk::Window win;
win.set_title("RagePhoto GTK Photo Viewer");
win.set_default_size(400, 0);
win.set_resizable(false);
Gtk::Box vertical_box(Gtk::ORIENTATION_VERTICAL);
Gtk::Label title_label;
Gtk::Label json_label;
PhotoViewer photo_viewer(&title_label, &json_label);
vertical_box.add(photo_viewer);
vertical_box.add(title_label);
vertical_box.add(json_label);
photo_viewer.show();
Gtk::Box horizontal_box(Gtk::ORIENTATION_HORIZONTAL);
vertical_box.add(horizontal_box);
Gtk::Button open_button;
open_button.set_label("Open");
open_button.set_hexpand(true);
open_button.set_can_default(false);
open_button.signal_clicked().connect([&](){
Gtk::FileChooserDialog dialog("Open Photo", Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.set_transient_for(win);
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Open", Gtk::RESPONSE_OK);
Glib::RefPtr<Gtk::FileFilter> filter_photo = Gtk::FileFilter::create();
filter_photo->set_name("GTA V Photo");
filter_photo->add_pattern("PGTA5*");
int result = dialog.run();
switch(result) {
case Gtk::RESPONSE_OK: {
std::string filename = dialog.get_filename();
photo_viewer.open_file(filename.c_str());
break;
}
default:
break;
}
});
horizontal_box.add(open_button);
open_button.show();
Gtk::Button close_button;
close_button.set_label("Close");
close_button.set_hexpand(true);
close_button.set_can_default(false);
close_button.signal_clicked().connect([&](){
win.close();
});
horizontal_box.add(close_button);
close_button.show();
app->signal_open().connect([&](const Gio::Application::type_vec_files &files, const Glib::ustring &hint){
for (const auto &file : files) {
photo_viewer.open_file(file->get_path().c_str());
}
app->add_window(win);
win.show();
});
horizontal_box.show();
vertical_box.show();
win.add(vertical_box);
return app->run(win);
}