From 7bc3cabc896145ec807b50a1b326c9f878fe9053 Mon Sep 17 00:00:00 2001
From: Syping <schiedelrafael@keppe.org>
Date: Sat, 30 Oct 2021 15:31:29 +0200
Subject: [PATCH] improve examples

---
 examples/ragephoto-gtkviewer/CMakeLists.txt   |  7 +-
 .../ragephoto-gtkviewer/src/PhotoViewer.cpp   | 87 -------------------
 .../ragephoto-gtkviewer/src/PhotoViewer.h     | 38 --------
 examples/ragephoto-gtkviewer/src/main.cpp     | 58 ++++++++++---
 examples/ragephoto-qtviewer/src/main.cpp      |  2 +-
 5 files changed, 49 insertions(+), 143 deletions(-)
 delete mode 100644 examples/ragephoto-gtkviewer/src/PhotoViewer.cpp
 delete mode 100644 examples/ragephoto-gtkviewer/src/PhotoViewer.h

diff --git a/examples/ragephoto-gtkviewer/CMakeLists.txt b/examples/ragephoto-gtkviewer/CMakeLists.txt
index be78730..ab79430 100644
--- a/examples/ragephoto-gtkviewer/CMakeLists.txt
+++ b/examples/ragephoto-gtkviewer/CMakeLists.txt
@@ -27,13 +27,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
 find_package(PkgConfig REQUIRED)
 pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)
 
-set(GTKVIEWER_HEADERS
-    src/PhotoViewer.h
-)
-
 set(GTKVIEWER_SOURCES
     src/main.cpp
-    src/PhotoViewer.cpp
 )
 
 if (TARGET ragephoto)
@@ -43,7 +38,7 @@ else()
     pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)
 endif()
 
-add_executable(ragephoto-gtkviewer WIN32 ${GTKVIEWER_HEADERS} ${GTKVIEWER_SOURCES})
+add_executable(ragephoto-gtkviewer WIN32 ${GTKVIEWER_SOURCES})
 target_compile_options(ragephoto-gtkviewer PRIVATE ${GTKMM_CFLAGS} ${RAGEPHOTO_CFLAGS})
 target_link_libraries(ragephoto-gtkviewer PRIVATE ${GTKMM_LIBRARIES} ${RAGEPHOTO_LIBRARIES})
 if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.13.0")
diff --git a/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp b/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp
deleted file mode 100644
index 3a31752..0000000
--- a/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp
+++ /dev/null
@@ -1,87 +0,0 @@
-/*****************************************************************************
-* 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 <cairomm/context.h>
-#include <gdkmm/general.h>
-#include <gtkmm/messagedialog.h>
-#include <RagePhoto.h>
-#include <fstream>
-#include <iostream>
-#include <iterator>
-
-PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win)
-{
-}
-
-void PhotoViewer::open_file(const std::string &filename)
-{
-    if (p_image)
-        p_image.clear();
-
-    RagePhoto ragePhoto;
-
-    // Read file
-    std::ifstream ifs(filename, std::ios::in | std::ios::binary);
-    if (!ifs.is_open()) {
-        Gtk::MessageDialog msg(*p_win, "Failed to open file: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
-        msg.set_title("Open Photo");
-        msg.run();
-        return;
-    }
-    std::string data(std::istreambuf_iterator<char>{ifs}, {});
-    ifs.close();
-
-    // Load Photo
-    const bool loaded = ragePhoto.load(data);
-    if (!loaded) {
-        const RagePhoto::Error error = ragePhoto.error();
-        if (error <= RagePhoto::Error::PhotoReadError) {
-            Gtk::MessageDialog msg(*p_win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
-            msg.set_title("Open Photo");
-            msg.run();
-            return;
-        }
-    }
-
-    GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
-    gdk_pixbuf_loader_write(loader, reinterpret_cast<const guchar*>(ragePhoto.photoData()), ragePhoto.photoSize(), NULL);
-    GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
-    gdk_pixbuf_loader_close(loader, NULL);
-    p_image = Glib::wrap(c_pixbuf);
-
-    p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());
-
-    if (p_image)
-        set_size_request(p_image->get_width(), p_image->get_height());
-}
-
-bool PhotoViewer::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
-{
-    if (!p_image)
-        return false;
-
-    Gtk::Allocation allocation = get_allocation();
-    const int width = allocation.get_width();
-    const int height = allocation.get_height();
-
-    Gdk::Cairo::set_source_pixbuf(cr, p_image, (width - p_image->get_width()) / 2, (height - p_image->get_height()) / 2);
-    cr->paint();
-
-    return true;
-}
diff --git a/examples/ragephoto-gtkviewer/src/PhotoViewer.h b/examples/ragephoto-gtkviewer/src/PhotoViewer.h
deleted file mode 100644
index 1b1f5ab..0000000
--- a/examples/ragephoto-gtkviewer/src/PhotoViewer.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*****************************************************************************
-* 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.
-*****************************************************************************/
-
-#ifndef PHOTOVIEWER_H
-#define PHOTOVIEWER_H
-
-#include <gdkmm/pixbuf.h>
-#include <gtkmm/drawingarea.h>
-#include <gtkmm/window.h>
-
-class PhotoViewer : public Gtk::DrawingArea
-{
-public:
-    PhotoViewer(Gtk::Window *win);
-    void open_file(const std::string &filename);
-
-protected:
-    bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
-    Glib::RefPtr<Gdk::Pixbuf> p_image;
-    Gtk::Window *p_win;
-};
-
-#endif // PHOTOVIEWER_H
diff --git a/examples/ragephoto-gtkviewer/src/main.cpp b/examples/ragephoto-gtkviewer/src/main.cpp
index a1e4e56..555cdf8 100644
--- a/examples/ragephoto-gtkviewer/src/main.cpp
+++ b/examples/ragephoto-gtkviewer/src/main.cpp
@@ -16,16 +16,52 @@
 * responsible for anything with use of the software, you are self responsible.
 *****************************************************************************/
 
-#include "PhotoViewer.h"
+#include <RagePhoto.h>
 #include <gtkmm/application.h>
 #include <gtkmm/box.h>
 #include <gtkmm/button.h>
+#include <gtkmm/image.h>
 #include <gtkmm/filechooserdialog.h>
 #include <gtkmm/filefilter.h>
 #include <gtkmm/label.h>
 #include <gtkmm/messagedialog.h>
 #include <gtkmm/window.h>
+#include <fstream>
 #include <iostream>
+#include <iterator>
+
+bool readPhotoFile(const std::string &filename, Gtk::Window *win, Gtk::Image *image)
+{
+    std::ifstream ifs(filename, std::ios::in | std::ios::binary);
+    if (ifs.is_open()) {
+        std::string data(std::istreambuf_iterator<char>{ifs}, {});
+        ifs.close();
+        RagePhoto ragePhoto;
+        const bool loaded = ragePhoto.load(data);
+        if (!loaded) {
+            const RagePhoto::Error error = ragePhoto.error();
+            if (error <= RagePhoto::Error::PhotoReadError) {
+                Gtk::MessageDialog msg(*win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
+                msg.set_title("Open Photo");
+                msg.run();
+                return false;
+            }
+        }
+        GdkPixbufLoader *pixbuf_loader = gdk_pixbuf_loader_new();
+        gdk_pixbuf_loader_write(pixbuf_loader, reinterpret_cast<const guchar*>(ragePhoto.photoData()), ragePhoto.photoSize(), NULL);
+        GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(pixbuf_loader);
+        gdk_pixbuf_loader_close(pixbuf_loader, NULL);
+        image->set(Glib::wrap(pixbuf));
+        win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());
+        return true;
+    }
+    else {
+        Gtk::MessageDialog msg(*win, "Failed to open file: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
+        msg.set_title("Open Photo");
+        msg.run();
+    }
+    return false;
+}
 
 int main(int argc, char *argv[])
 {
@@ -40,9 +76,9 @@ int main(int argc, char *argv[])
     vertical_box.set_margin_bottom(6);
     vertical_box.set_spacing(6);
 
-    PhotoViewer photo_viewer(&win);
-    vertical_box.add(photo_viewer);
-    photo_viewer.show();
+    Gtk::Image image;
+    vertical_box.add(image);
+    image.show();
 
     Gtk::Box horizontal_box(Gtk::ORIENTATION_HORIZONTAL);
     horizontal_box.set_margin_left(6);
@@ -62,17 +98,17 @@ int main(int argc, char *argv[])
         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*");
-        dialog.add_filter(filter_photo);
+        Glib::RefPtr<Gtk::FileFilter> ragephoto_filter = Gtk::FileFilter::create();
+        ragephoto_filter->set_name("RagePhoto compatible");
+        ragephoto_filter->add_pattern("PGTA5*");
+        ragephoto_filter->add_pattern("PRDR3*");
+        dialog.add_filter(ragephoto_filter);
 
         int result = dialog.run();
 
         switch(result) {
         case Gtk::RESPONSE_OK: {
-            std::string filename = dialog.get_filename();
-            photo_viewer.open_file(filename.c_str());
+            readPhotoFile(dialog.get_filename(), &win, &image);
             break;
         }
         default:
@@ -96,7 +132,7 @@ int main(int argc, char *argv[])
     app->signal_open().connect([&](const Gio::Application::type_vec_files &files, const Glib::ustring &hint) {
         if (files.size() == 1) {
             for (const auto &file : files) {
-                photo_viewer.open_file(file->get_path());
+                readPhotoFile(file->get_path(), &win, &image);
             }
             app->add_window(win);
             win.present();
diff --git a/examples/ragephoto-qtviewer/src/main.cpp b/examples/ragephoto-qtviewer/src/main.cpp
index 5bf58a6..44f71e0 100644
--- a/examples/ragephoto-qtviewer/src/main.cpp
+++ b/examples/ragephoto-qtviewer/src/main.cpp
@@ -89,7 +89,7 @@ int main(int argc, char *argv[])
     if (QIcon::hasThemeIcon("document-open"))
         openButton.setIcon(QIcon::fromTheme("document-open"));
     QObject::connect(&openButton, &QPushButton::clicked, &mainWindow, [&](){
-        const QString filename = QFileDialog::getOpenFileName(&mainWindow, "Open Photo...", QString(), "GTA V Photo (PGTA5*)");
+        const QString filename = QFileDialog::getOpenFileName(&mainWindow, "Open Photo...", QString(), "RagePhoto compatible (PGTA5* PRDR3*)");
         if (filename.isEmpty())
             return;
         if (readPhotoFile(filename, &mainWindow, &photoLabel)) {