testing-repository/Gtkmm3/gtk97_drawing/src/main.cc

130 lines
3.7 KiB
C++
Raw Normal View History

2021-07-22 10:57:01 +08:00
#include <gtkmm.h>
class MyWin : public Gtk::Window{
//Child Widgets
Gtk::DrawingArea draw_area;
Gtk::ColorButton color_btn;
Gtk::Label main_label;
Gtk::Box main_box,btn_box;
2021-07-23 08:39:38 +08:00
Gtk::Button btn_clear;
2021-07-22 10:57:01 +08:00
//Color Setting
Gdk::RGBA m_color;
Cairo::RefPtr<Cairo::ImageSurface> surface;
//Gesture to draw
Glib::RefPtr<Gtk::GestureDrag> drag;
double start_x,start_y;
//Signal Handlers
bool draw_event(const Cairo::RefPtr<Cairo::Context> &context){
//Show context
context->set_source(surface,0,0);
context->paint();
return true;
}
void draw_brush(double x,double y){
//Create Draw Brush
auto cr=Cairo::Context::create(surface);
cr->arc(x, y, 3, 0, 2 * G_PI);
//Set Color
cr->set_source_rgba(m_color.get_red(),m_color.get_green(),
m_color.get_blue(),m_color.get_alpha());
//Fill Color and Delete the brush
cr->fill();
cr.clear();
draw_area.queue_draw();
}
2021-07-23 08:39:38 +08:00
void button_press(){
2021-07-22 10:57:01 +08:00
if(surface){
//Clear the content in draw area
auto cr=Cairo::Context::create(surface);
cr->set_source_rgb(1,1,1);
cr->paint();
cr.clear();
draw_area.queue_draw();
}
}
void drag_begin(double x,double y){
//The Begin
start_x=x;start_y=y;
draw_brush(x,y);
}
void drag_progress(double x,double y){
//Progress and end
draw_brush(x+start_x,y+start_y);
}
void color_set(){
m_color=color_btn.get_rgba();
}
public:
MyWin()
:main_label("Select a color"),
main_box(Gtk::ORIENTATION_HORIZONTAL,5),
2021-07-23 08:39:38 +08:00
btn_box(Gtk::ORIENTATION_VERTICAL,5),
btn_clear("Clear Board")
2021-07-22 10:57:01 +08:00
{
//Ininalize window
set_icon_name("org.gtk.daleclack");
set_default_size(640,480);
//Color set panel
btn_box.pack_start(main_label,Gtk::PACK_SHRINK);
btn_box.pack_start(color_btn,Gtk::PACK_SHRINK);
2021-07-23 08:39:38 +08:00
btn_box.pack_start(btn_clear,Gtk::PACK_SHRINK);
2021-07-22 10:57:01 +08:00
btn_box.set_halign(Gtk::ALIGN_CENTER);
btn_box.set_valign(Gtk::ALIGN_CENTER);
//Add Gesture
2021-07-23 08:39:38 +08:00
btn_clear.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::button_press));
2021-07-22 10:57:01 +08:00
drag=Gtk::GestureDrag::create(draw_area);
drag->set_button(GDK_BUTTON_PRIMARY);
drag->signal_drag_begin().connect(sigc::mem_fun(*this,&MyWin::drag_begin));
drag->signal_drag_update().connect(sigc::mem_fun(*this,&MyWin::drag_progress));
drag->signal_drag_end().connect(sigc::mem_fun(*this,&MyWin::drag_progress));
//Create a Surface
surface=Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32,600,480);
//Set Default Color
m_color.set_blue(0);
m_color.set_green(0);
m_color.set_red(0);
m_color.set_alpha(1);
color_btn.set_rgba(m_color);
color_btn.signal_color_set().connect(sigc::mem_fun(*this,&MyWin::color_set));
2021-07-23 08:39:38 +08:00
//Initalial draw
auto cr=Cairo::Context::create(surface);
cr->set_source_rgb(1,1,1);
cr->paint();
cr.clear();
draw_area.queue_draw();
2021-07-22 10:57:01 +08:00
//Initalize main widget
draw_area.set_size_request(600,480);
draw_area.signal_draw().connect(sigc::mem_fun(*this,&MyWin::draw_event));
main_box.pack_start(draw_area);
main_box.pack_start(btn_box,Gtk::PACK_SHRINK);
main_box.set_border_width(10);
add(main_box);
show_all_children();
}
};
int main(int argc,char **argv){
auto app=Gtk::Application::create(argc,argv,"org.gtk.daleclack");
MyWin window;
return app->run(window);
}