From 0b48e7301751301afbf96520ef39d8bb719eb780 Mon Sep 17 00:00:00 2001 From: daleclack Date: Sun, 26 Dec 2021 00:05:54 +0800 Subject: [PATCH] Add Draw Area for image show --- Gtkmm3/gtk119_imageviewer2/CMakeLists.txt | 4 +-- Gtkmm3/gtk119_imageviewer2/src/MyImage.cc | 33 +++++++++++++++++++++++ Gtkmm3/gtk119_imageviewer2/src/MyImage.hh | 13 +++++++++ Gtkmm3/gtk119_imageviewer2/src/MyWin.cc | 22 +++++++++++++-- Gtkmm3/gtk119_imageviewer2/src/MyWin.hh | 9 ++++++- 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 Gtkmm3/gtk119_imageviewer2/src/MyImage.cc create mode 100644 Gtkmm3/gtk119_imageviewer2/src/MyImage.hh diff --git a/Gtkmm3/gtk119_imageviewer2/CMakeLists.txt b/Gtkmm3/gtk119_imageviewer2/CMakeLists.txt index f99f4ef..e9bda3d 100644 --- a/Gtkmm3/gtk119_imageviewer2/CMakeLists.txt +++ b/Gtkmm3/gtk119_imageviewer2/CMakeLists.txt @@ -47,9 +47,9 @@ if(WIN32) set_property(SOURCE ../icon.rc APPEND PROPERTY OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico ) - add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} src/main.cc src/MyWin.cc) + add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} src/main.cc src/MyWin.cc src/MyImage.cc) else() - add_executable(${PROJECT_NAME} src/main.cc src/MyWin.cc) + add_executable(${PROJECT_NAME} src/main.cc src/MyWin.cc src/MyImage.cc) endif(WIN32) diff --git a/Gtkmm3/gtk119_imageviewer2/src/MyImage.cc b/Gtkmm3/gtk119_imageviewer2/src/MyImage.cc new file mode 100644 index 0000000..512160e --- /dev/null +++ b/Gtkmm3/gtk119_imageviewer2/src/MyImage.cc @@ -0,0 +1,33 @@ +#include "MyImage.hh" + +MyImage::MyImage(){ + +} + +MyImage::~MyImage(){ + +} + +bool MyImage::on_draw(const Cairo::RefPtr &cr){ + if(!image){ + return false; + } + + int width = image->get_width(); + int height = image->get_height(); + + set_size_request(width,height); + + // Draw the image in the middle of the drawing area, or (if the image is + // larger than the drawing area) draw the middle part of the image. + Gdk::Cairo::set_source_pixbuf(cr, image, + (width - image->get_width())/2, (height - image->get_height())/2); + cr->paint(); + + return true; +} + +void MyImage::set_pixbuf(const Glib::RefPtr &pixbuf) +{ + image = pixbuf; +} diff --git a/Gtkmm3/gtk119_imageviewer2/src/MyImage.hh b/Gtkmm3/gtk119_imageviewer2/src/MyImage.hh new file mode 100644 index 0000000..21cd2e2 --- /dev/null +++ b/Gtkmm3/gtk119_imageviewer2/src/MyImage.hh @@ -0,0 +1,13 @@ +#pragma once + +#include + +class MyImage : public Gtk::DrawingArea{ + public: + MyImage(); + virtual ~MyImage(); + void set_pixbuf(const Glib::RefPtr &pixbuf); + protected: + bool on_draw(const Cairo::RefPtr &cr) override; + Glib::RefPtr image; +}; diff --git a/Gtkmm3/gtk119_imageviewer2/src/MyWin.cc b/Gtkmm3/gtk119_imageviewer2/src/MyWin.cc index f3a547a..33f92d0 100644 --- a/Gtkmm3/gtk119_imageviewer2/src/MyWin.cc +++ b/Gtkmm3/gtk119_imageviewer2/src/MyWin.cc @@ -2,7 +2,8 @@ MyWin::MyWin() :main_box(Gtk::ORIENTATION_VERTICAL,5), -btnbox(Gtk::ORIENTATION_VERTICAL,5) +btnbox(Gtk::ORIENTATION_HORIZONTAL,5), +btnopen("Open Image") { //Add Widgets set_default_size(800,450); @@ -13,8 +14,25 @@ btnbox(Gtk::ORIENTATION_VERTICAL,5) sw.add(image_area); main_box.pack_start(sw); + //Initalize Scale + m_adjustment = Gtk::Adjustment::create(1.0,0.1,10.0,0.1,0.1); + scale.set_default_direction(Gtk::TEXT_DIR_LTR); + scale.set_adjustment(m_adjustment); + + //Add control widgets + btnbox.pack_start(scale); + btnbox.pack_start(btnopen,Gtk::PACK_SHRINK); main_box.pack_start(btnbox,Gtk::PACK_SHRINK); + btnopen.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::btnopen_clicked)); add(main_box); show_all_children(); -} \ No newline at end of file +} + +void MyWin::btnopen_clicked(){ + +} + +void MyWin::dialog_response(int response_id){ + +} diff --git a/Gtkmm3/gtk119_imageviewer2/src/MyWin.hh b/Gtkmm3/gtk119_imageviewer2/src/MyWin.hh index 75f57e6..3f8d2fc 100644 --- a/Gtkmm3/gtk119_imageviewer2/src/MyWin.hh +++ b/Gtkmm3/gtk119_imageviewer2/src/MyWin.hh @@ -1,6 +1,7 @@ #pragma once #include +#include "MyImage.hh" class MyWin : public Gtk::Window{ public: @@ -8,10 +9,16 @@ class MyWin : public Gtk::Window{ private: //Child widgets Gtk::ScrolledWindow sw; - Gtk::DrawingArea image_area; + MyImage image_area; Gtk::Box main_box,btnbox; + Gtk::Button btnopen; + Gtk::Scale scale; + Glib::RefPtr m_adjustment; + Glib::RefPtr dialog; //Gesture control //Signal Handlers + void btnopen_clicked(); + void dialog_response(int response_id); };