Add Draw Area for image show

This commit is contained in:
daleclack 2021-12-26 00:05:54 +08:00
parent bd11f73c9b
commit 0b48e73017
5 changed files with 76 additions and 5 deletions

View File

@ -47,9 +47,9 @@ if(WIN32)
set_property(SOURCE ../icon.rc APPEND PROPERTY set_property(SOURCE ../icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico 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() 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) endif(WIN32)

View File

@ -0,0 +1,33 @@
#include "MyImage.hh"
MyImage::MyImage(){
}
MyImage::~MyImage(){
}
bool MyImage::on_draw(const Cairo::RefPtr<Cairo::Context> &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<Gdk::Pixbuf> &pixbuf)
{
image = pixbuf;
}

View File

@ -0,0 +1,13 @@
#pragma once
#include <gtkmm.h>
class MyImage : public Gtk::DrawingArea{
public:
MyImage();
virtual ~MyImage();
void set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf> &pixbuf);
protected:
bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr) override;
Glib::RefPtr<Gdk::Pixbuf> image;
};

View File

@ -2,7 +2,8 @@
MyWin::MyWin() MyWin::MyWin()
:main_box(Gtk::ORIENTATION_VERTICAL,5), :main_box(Gtk::ORIENTATION_VERTICAL,5),
btnbox(Gtk::ORIENTATION_VERTICAL,5) btnbox(Gtk::ORIENTATION_HORIZONTAL,5),
btnopen("Open Image")
{ {
//Add Widgets //Add Widgets
set_default_size(800,450); set_default_size(800,450);
@ -13,8 +14,25 @@ btnbox(Gtk::ORIENTATION_VERTICAL,5)
sw.add(image_area); sw.add(image_area);
main_box.pack_start(sw); 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); main_box.pack_start(btnbox,Gtk::PACK_SHRINK);
btnopen.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::btnopen_clicked));
add(main_box); add(main_box);
show_all_children(); show_all_children();
} }
void MyWin::btnopen_clicked(){
}
void MyWin::dialog_response(int response_id){
}

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <gtkmm.h> #include <gtkmm.h>
#include "MyImage.hh"
class MyWin : public Gtk::Window{ class MyWin : public Gtk::Window{
public: public:
@ -8,10 +9,16 @@ class MyWin : public Gtk::Window{
private: private:
//Child widgets //Child widgets
Gtk::ScrolledWindow sw; Gtk::ScrolledWindow sw;
Gtk::DrawingArea image_area; MyImage image_area;
Gtk::Box main_box,btnbox; Gtk::Box main_box,btnbox;
Gtk::Button btnopen;
Gtk::Scale scale;
Glib::RefPtr<Gtk::Adjustment> m_adjustment;
Glib::RefPtr<Gtk::FileChooserNative> dialog;
//Gesture control //Gesture control
//Signal Handlers //Signal Handlers
void btnopen_clicked();
void dialog_response(int response_id);
}; };