Add gtk97
This commit is contained in:
parent
6e7a7eb099
commit
7bad57be10
|
@ -0,0 +1,30 @@
|
|||
#A Simple Project Test
|
||||
project('gtk97', 'cpp',
|
||||
default_options : ['c_std=c17', 'cpp_std=c++17'])
|
||||
|
||||
#Initalize variants
|
||||
# gnome=import('gnome')
|
||||
|
||||
#Compile Resource
|
||||
# gresources = gnome.compile_resources(
|
||||
# 'resources', 'res/gtk91.resource.xml',
|
||||
# source_dir: 'res',
|
||||
# c_name: 'resources'
|
||||
# )
|
||||
|
||||
#The Gtkmm Library as a dependency
|
||||
gtkdep = dependency('gtkmm-3.0')
|
||||
|
||||
#Additional include dirs
|
||||
dir_include = include_directories('..')
|
||||
|
||||
#Use Different Build Opinions in windows and Linux
|
||||
if host_machine.system() == 'windows'
|
||||
win=import('windows')
|
||||
icon_res=win.compile_resources('../icon.rc')
|
||||
executable('gtk97', icon_res, 'src/main.cc', dependencies : gtkdep,
|
||||
win_subsystem : 'windows', include_directories : dir_include)
|
||||
else
|
||||
executable('gtk97', 'src/main.cc', dependencies : gtkdep,
|
||||
include_directories : dir_include)
|
||||
endif
|
|
@ -0,0 +1,123 @@
|
|||
#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;
|
||||
|
||||
//Color Setting
|
||||
Gdk::RGBA m_color;
|
||||
Cairo::RefPtr<Cairo::ImageSurface> surface;
|
||||
|
||||
//Gesture to draw
|
||||
Glib::RefPtr<Gtk::GestureMultiPress> gesture;
|
||||
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();
|
||||
}
|
||||
|
||||
void button_press(int n_press, double x, double y){
|
||||
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),
|
||||
btn_box(Gtk::ORIENTATION_VERTICAL,5)
|
||||
{
|
||||
//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);
|
||||
btn_box.set_halign(Gtk::ALIGN_CENTER);
|
||||
btn_box.set_valign(Gtk::ALIGN_CENTER);
|
||||
|
||||
//Add Gesture
|
||||
gesture=Gtk::GestureMultiPress::create(draw_area);
|
||||
gesture->set_button(GDK_BUTTON_SECONDARY);
|
||||
gesture->signal_pressed().connect(sigc::mem_fun(*this,&MyWin::button_press));
|
||||
|
||||
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));
|
||||
|
||||
|
||||
//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);
|
||||
}
|
Loading…
Reference in New Issue