Skip to content

Commit

Permalink
Add Drawing App
Browse files Browse the repository at this point in the history
  • Loading branch information
daleclack committed Oct 10, 2021
1 parent d386972 commit 4ccae57
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Gtkmm3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ if(WIN32)
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/icon.ico
)
add_executable(My_GtkUI WIN32 ${app_WINRC} src/main.cc src/MyWin.cc src/MyStack.cc src/Runner.cc
src/Game.cc src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/TextEditor.cc
src/Game.cc src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/TextEditor.cc src/drawing.cc
${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
else()
add_executable(My_GtkUI src/main.cc src/MyWin.cc src/MyStack.cc src/Game.cc src/Runner.cc
src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/TextEditor.cc
src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/TextEditor.cc src/drawing.cc
${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})

endif(WIN32)
Expand Down
Binary file added Gtkmm3/res/icons/16x16/actions/drawing_app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gtkmm3/res/icons/48x48/actions/drawing_app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 51 additions & 11 deletions Gtkmm3/res/leftpanel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
<property name="icon-name">gedit</property>
<property name="icon_size">6</property>
</object>
<object class="GtkImage" id="image11">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">drawing_app</property>
</object>
<object class="GtkImage" id="image12">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">drawing_app</property>
<property name="icon_size">6</property>
</object>
<object class="GtkImage" id="image2">
<property name="visible">True</property>
<property name="can-focus">False</property>
Expand Down Expand Up @@ -203,6 +214,22 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="action-name">win.about</property>
<property name="image">image3</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btngame">
<property name="label" translatable="yes">Guess Game</property>
Expand All @@ -216,22 +243,22 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btneditor">
<property name="label" translatable="yes">Simple Text Editor</property>
<object class="GtkButton" id="btndraw">
<property name="label" translatable="yes">Drawing</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image8</property>
<property name="image">image11</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
<child>
Expand All @@ -246,23 +273,22 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnabout">
<property name="label" translatable="yes">About</property>
<object class="GtkButton" id="btneditor">
<property name="label" translatable="yes">Simple Text Editor</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="action-name">win.about</property>
<property name="image">image3</property>
<property name="image">image8</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
<property name="position">4</property>
</packing>
</child>
<child>
Expand Down Expand Up @@ -355,5 +381,19 @@
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="panel_draw">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image12</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
</interface>
3 changes: 3 additions & 0 deletions Gtkmm3/res/my_gtkui.resource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
<file>icons/16x16/actions/game.png</file>
<file>icons/16x16/actions/window-close.png</file>
<file>icons/16x16/actions/window-minimize.png</file>
<file>icons/16x16/actions/drawing_app.png</file>
<file>icons/48x48/actions/game.png</file>
<file>icons/48x48/actions/game_running.png</file>
<file>icons/48x48/actions/My_GtkUI.png</file>
<file>icons/48x48/actions/user_home.png</file>
<file>icons/48x48/actions/my_user.png</file>
<file>icons/48x48/actions/gedit.png</file>
<file>icons/48x48/actions/gedit_running.png</file>
<file>icons/48x48/actions/drawing_app.png</file>
<file>icons/48x48/actions/drawing_app_running.png</file>
<file>icons/scalable/status/graphics.svg</file>
<file>icons/scalable/status/log_out.svg</file>
<file>icons/scalable/status/system.svg</file>
Expand Down
33 changes: 33 additions & 0 deletions Gtkmm3/src/LeftPanel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ LeftPanel::LeftPanel(){
panel_builder->get_widget("btnrun",btnrun);
panel_builder->get_widget("btneditor",btneditor);
panel_builder->get_widget("panel_editor",panel_editor);
panel_builder->get_widget("btndraw",btndraw);
panel_builder->get_widget("panel_draw",panel_draw);

//Add timer
paneltimer=Glib::signal_timeout().connect(sigc::mem_fun(*this,&LeftPanel::on_timeout),100);
Expand All @@ -32,6 +34,8 @@ LeftPanel::LeftPanel(){
btnrun->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btnrun_clicked));
btneditor->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btnedit_clicked));
panel_editor->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btnedit_clicked));
btndraw->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btndraw_clicked));
panel_draw->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btndraw_clicked));
}

void LeftPanel::add_panel(Gtk::Window *parent1,Gtk::Overlay &overlay){
Expand All @@ -43,6 +47,7 @@ void LeftPanel::add_panel(Gtk::Window *parent1,Gtk::Overlay &overlay){
parent=parent1;
runner1.set_transient_for(*parent1);
editor1.set_transient_for(*parent1);
drawing_app.set_transient_for(*parent1);
}

void LeftPanel::btnaud_clicked(){
Expand Down Expand Up @@ -109,6 +114,11 @@ bool LeftPanel::on_timeout(){
}else{
panel_editor->set_image_from_icon_name("gedit",Gtk::ICON_SIZE_DIALOG);
}
if(drawing_app.running){
panel_draw->set_image_from_icon_name("drawing_app_running",Gtk::ICON_SIZE_DIALOG);
}else{
panel_draw->set_image_from_icon_name("drawing_app",Gtk::ICON_SIZE_DIALOG);
}
return true;
}

Expand Down Expand Up @@ -139,3 +149,26 @@ void LeftPanel::btnedit_clicked(){
}
popover->popdown();
}

void LeftPanel::btndraw_clicked(){
//Simulate the panel by Gdk::Window Proprties
auto draw_win=drawing_app.get_window();
if(draw_win){
auto state=draw_win->get_state();
switch(state){
case Gdk::WINDOW_STATE_ICONIFIED:
drawing_app.deiconify();
break;
case Gdk::WINDOW_STATE_WITHDRAWN:
drawing_app.show();
drawing_app.running=true;
break;
default:
drawing_app.iconify();
}
}else{
drawing_app.show();
drawing_app.running=true;
}
popover->popdown();
}
6 changes: 5 additions & 1 deletion Gtkmm3/src/LeftPanel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Game.hh"
#include "Runner.hh"
#include "TextEditor.hh"
#include "drawing.hh"

class LeftPanel{
public:
Expand All @@ -16,7 +17,7 @@ private:
Gtk::Box *btnbox;
Gtk::Popover *popover;
Gtk::Button *btnaud,*btngedit,*btnvlc,*btnnote,*btnvlc_win32,*btngame,
*panelgame,*btnrun,*btneditor,*panel_editor;
*panelgame,*btnrun,*btneditor,*panel_editor,*btndraw,*panel_draw;
//Parent Window
Gtk::Window *parent;
Game game1;
Expand All @@ -27,6 +28,8 @@ private:
GtkRunner runner1;
//Text Editor
TextEditor editor1;
//Drawing App
Drawing drawing_app;
//Signal Handlers for application start
void btnaud_clicked();
void btngedit_clicked();
Expand All @@ -36,4 +39,5 @@ private:
void btngame_clicked();
void btnrun_clicked();
void btnedit_clicked();
void btndraw_clicked();
};
119 changes: 119 additions & 0 deletions Gtkmm3/src/drawing.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "drawing.hh"

bool Drawing::draw_event(const Cairo::RefPtr<Cairo::Context> &context){
//Show context
context->set_source(surface,0,0);
context->paint();
return true;
}

void Drawing::draw_brush(double x,double y){
//Create Draw Brush with specificed size
double size=scale.get_value();
auto cr=Cairo::Context::create(surface);
cr->arc(x, y, size, 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 Drawing::button_press(){
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 Drawing::drag_begin(double x,double y){
//The Begin
start_x=x;start_y=y;
draw_brush(x,y);
}

void Drawing::drag_progress(double x,double y){
//Progress and end
draw_brush(x+start_x,y+start_y);
}

void Drawing::color_set(){
m_color=color_btn.get_rgba();
}

Drawing::Drawing()
:main_label("Select a color"),
size_label("Pen Size"),
main_box(Gtk::ORIENTATION_HORIZONTAL,5),
btn_box(Gtk::ORIENTATION_VERTICAL,5),
btn_clear("Clear Board"),
btn_exit("Exit")
{
//Ininalize window
set_icon_name("org.gtk.daleclack");
set_title("Drawing");
set_default_size(640,480);

//Color set panel
size_adj=Gtk::Adjustment::create(3.0,1.0,20.0);
scale.set_adjustment(size_adj);
scale.set_value_pos(Gtk::POS_BOTTOM);
btn_box.pack_start(main_label,Gtk::PACK_SHRINK);
btn_box.pack_start(color_btn,Gtk::PACK_SHRINK);
btn_box.pack_start(size_label,Gtk::PACK_SHRINK);
btn_box.pack_start(scale,Gtk::PACK_SHRINK);
btn_box.pack_start(btn_clear,Gtk::PACK_SHRINK);
btn_box.pack_start(btn_exit,Gtk::PACK_SHRINK);
btn_box.set_halign(Gtk::ALIGN_CENTER);
btn_box.set_valign(Gtk::ALIGN_CENTER);

//Add Gesture
btn_clear.signal_clicked().connect(sigc::mem_fun(*this,&Drawing::button_press));
btn_exit.signal_clicked().connect(sigc::mem_fun(*this,&Drawing::hide));

drag=Gtk::GestureDrag::create(draw_area);
drag->set_button(GDK_BUTTON_PRIMARY);
drag->signal_drag_begin().connect(sigc::mem_fun(*this,&Drawing::drag_begin));
drag->signal_drag_update().connect(sigc::mem_fun(*this,&Drawing::drag_progress));
drag->signal_drag_end().connect(sigc::mem_fun(*this,&Drawing::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,&Drawing::color_set));

//Initalial draw
auto cr=Cairo::Context::create(surface);
cr->set_source_rgb(1,1,1);
cr->paint();
cr.clear();
draw_area.queue_draw();

//Initalize main widget
draw_area.set_size_request(600,480);
draw_area.signal_draw().connect(sigc::mem_fun(*this,&Drawing::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();

//Default Mode
running=false;
}
Loading

0 comments on commit 4ccae57

Please sign in to comment.