diff --git a/Gtk3/CMakeLists.txt b/Gtk3/CMakeLists.txt index fd31ed7..80f0e80 100644 --- a/Gtk3/CMakeLists.txt +++ b/Gtk3/CMakeLists.txt @@ -47,11 +47,11 @@ if(WIN32) OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/icon.ico ) add_executable(My_GtkUi WIN32 ${app_WINRC} src/main.cpp src/background.cpp src/game.cpp - src/TextEditor.cpp src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp + src/TextEditor.cpp src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/drawing.cpp ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}) else() add_executable(My_GtkUi src/main.cpp src/background.cpp src/game.cpp src/TextEditor.cpp - src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp + src/panel1.cpp src/panel2.cpp src/win1.cpp src/winconf.cpp src/drawing.cpp ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}) endif(WIN32) diff --git a/Gtk3/res/leftpanel.ui b/Gtk3/res/leftpanel.ui index 6b62904..ad8079a 100644 --- a/Gtk3/res/leftpanel.ui +++ b/Gtk3/res/leftpanel.ui @@ -10,11 +10,6 @@ True False - - True - False - gtk-about - 300 400 @@ -188,6 +183,20 @@ 1 + + + Drawing App + True + True + True + none + + + False + True + 2 + + About @@ -200,15 +209,12 @@ False True - 2 + 3 - - - 2 @@ -265,4 +271,9 @@ + + True + False + gtk-about + diff --git a/Gtk3/res/leftpanel.ui~ b/Gtk3/res/leftpanel.ui~ index 7a6c132..ad8079a 100644 --- a/Gtk3/res/leftpanel.ui~ +++ b/Gtk3/res/leftpanel.ui~ @@ -10,16 +10,10 @@ True False - - True - False - gtk-about - 300 400 False - False True @@ -189,6 +183,20 @@ 1 + + + Drawing App + True + True + True + none + + + False + True + 2 + + About @@ -201,15 +209,12 @@ False True - 2 + 3 - - - 2 @@ -266,4 +271,9 @@ + + True + False + gtk-about + diff --git a/Gtk3/src/drawing.cpp b/Gtk3/src/drawing.cpp new file mode 100644 index 0000000..777a04b --- /dev/null +++ b/Gtk3/src/drawing.cpp @@ -0,0 +1,149 @@ +#include "drawing.h" +#include + +typedef struct { + GtkWidget *draw_area1; + double x; + double y; +}MousePos; + +static cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,640,360); + +//static double start_x,start_y; + +static void clear_surface (void) +{ + cairo_t *cr; + + cr = cairo_create (surface); + + cairo_set_source_rgb (cr, 1, 1, 1); + cairo_paint (cr); + + cairo_destroy (cr); +} + +/* +static void resize_cb(GtkWidget *widget,int width,int height,gpointer data){ + cairo_surface_destroy (surface); + cairo_surface_create_similar(surface,CAIRO_CONTENT_COLOR,width,height); +}*/ + +static gboolean draw_cb(GtkWidget *widget,cairo_t *cr,gpointer data){ + cairo_set_source_surface(cr,surface,0,0); + cairo_paint(cr); + //Set Color + /*guint width, height; + width = gtk_widget_get_allocated_width (widget); + height = gtk_widget_get_allocated_height (widget); + cairo_arc (cr, + width / 2.0, height / 2.0, + MIN (width, height) / 2.0, + 0, 2 * G_PI); + cairo_fill(cr);*/ + return FALSE; +} + +static void draw_brush(GtkWidget *widget,double x,double y){ + cairo_t *cr; + //Draw on the surface + cr=cairo_create(surface); + //Some bugs in color set + GdkRGBA color; + //Draw and fill in color + cairo_arc(cr,x,y,3,0,2*G_PI); + + //gdk_rgba_parse(&color,"66CCFF"); + //gdk_cairo_set_source_rgba(cr,&color); + cairo_fill(cr); + + cairo_destroy(cr); + + gtk_widget_queue_draw(widget); + +} + +static void drag_begin(GtkGesture *gesture,double x,double y,MousePos *pos){ + pos->x=x; + pos->y=y; + draw_brush(pos->draw_area1,x,y); + /* + freopen("drag.txt","a",stdout); + g_print("%lf %lf\n",x,y); + fclose(stdout); + */ +} + +static void drag_update(GtkGesture *gesture,double x,double y,MousePos *pos){ + draw_brush(pos->draw_area1,x+pos->x,y+pos->y); + /* + freopen("drag.txt","a",stdout); + g_print("%lf %lf\n",x+pos->x,y+pos->y); + fclose(stdout); + */ +} + +static void drag_end(GtkGesture *gesture,double x,double y,MousePos *pos){ + draw_brush(pos->draw_area1,x+pos->x,y+pos->y); + /* + freopen("drag.txt","a",stdout); + g_print("%lf %lf\n",x+pos->x,y+pos->y); + fclose(stdout); + */ +} + +static void pressed(GtkGestureMultiPress *gesture,int n_press,double x,double y,GtkWidget *widget){ + clear_surface(); + gtk_widget_queue_draw(widget); + /* + g_print("%d\n",n_press); + freopen("drag.txt","w",stdout); + printf("\0"); + fclose(stdout); + */ +} + +static void close_window(void){ + if(surface){ + cairo_surface_destroy(surface); + } +} + +void drawing_main(GtkWidget *widget,GtkWindow *parent){ + GtkWidget *window,*draw_area; + GtkGesture *press,*drag; + //Create a window + window=gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_transient_for(GTK_WINDOW(window),parent); + gtk_window_set_title(GTK_WINDOW(window),"Drawing Test"); + gtk_window_set_default_size(GTK_WINDOW(window),640,360); + gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack"); + + g_signal_connect(window,"destroy",G_CALLBACK(close_window),NULL); + + //Create a draw area + draw_area=gtk_drawing_area_new(); + g_signal_connect(draw_area,"draw",G_CALLBACK(draw_cb),NULL); + //g_signal_connect_after(draw_area,"resize",G_CALLBACK(resize_cb),NULL); + //Create gestures + + static MousePos pos; + pos.draw_area1=draw_area; + //"Drag" gesture + drag=gtk_gesture_drag_new(draw_area); + + gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(drag),GDK_BUTTON_PRIMARY); + g_signal_connect(drag,"drag-begin",G_CALLBACK(drag_begin),&pos); + g_signal_connect(drag,"drag-update",G_CALLBACK(drag_update),&pos); + g_signal_connect(drag,"drag-end",G_CALLBACK(drag_end),&pos); + + //"Press" gesture + + press=gtk_gesture_multi_press_new(draw_area); + + gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(press),GDK_BUTTON_SECONDARY); + g_signal_connect(press,"pressed",G_CALLBACK(pressed),draw_area); + + gtk_container_add(GTK_CONTAINER(window),draw_area); + gtk_widget_show_all(window); +} diff --git a/Gtk3/src/drawing.h b/Gtk3/src/drawing.h new file mode 100644 index 0000000..9f33f95 --- /dev/null +++ b/Gtk3/src/drawing.h @@ -0,0 +1,8 @@ +#ifndef __DRAWING_H_ +#define __DRAWING_H_ + +#include + +void drawing_main(GtkWidget *widget,GtkWindow *parent); + +#endif diff --git a/Gtk3/src/panel2.cpp b/Gtk3/src/panel2.cpp index 80e9068..36835d1 100644 --- a/Gtk3/src/panel2.cpp +++ b/Gtk3/src/panel2.cpp @@ -4,6 +4,7 @@ #include "win1.h" #include "game.h" #include "TextEditor.h" +#include "drawing.h" static void btnvlc_clicked(GtkWidget *widget,gpointer data){ std::thread first(system,"vlc"); @@ -82,6 +83,10 @@ void add_leftpanel(GtkBuilder *builder,GtkFixed *fixed){ GObject *btnedit=gtk_builder_get_object(panel2,"btneditor"); g_signal_connect(btnedit,"clicked",G_CALLBACK(text_editor),window); g_signal_connect_swapped(btnedit,"clicked",G_CALLBACK(gtk_widget_hide),popover); + //Drawing application + GObject *btndraw=gtk_builder_get_object(panel2,"btndraw"); + g_signal_connect(btndraw,"clicked",G_CALLBACK(drawing_main),window); + g_signal_connect_swapped(btndraw,"clicked",G_CALLBACK(gtk_widget_hide),popover); //About window GObject *btn_about=gtk_builder_get_object(panel2,"btnabout"); g_signal_connect(btn_about,"clicked",G_CALLBACK(win1_init),window);