From b12c1f037a44954417d05128df3ddce706dfecc8 Mon Sep 17 00:00:00 2001 From: daleclack Date: Tue, 27 Jul 2021 09:44:40 +0800 Subject: [PATCH] Add gtk99 --- Gtkmm3/gtk99_clock/meson.build | 30 +++++++++ Gtkmm3/gtk99_clock/src/Clock.cc | 112 ++++++++++++++++++++++++++++++++ Gtkmm3/gtk99_clock/src/Clock.hh | 17 +++++ Gtkmm3/gtk99_clock/src/main.cc | 17 +++++ 4 files changed, 176 insertions(+) create mode 100644 Gtkmm3/gtk99_clock/meson.build create mode 100644 Gtkmm3/gtk99_clock/src/Clock.cc create mode 100644 Gtkmm3/gtk99_clock/src/Clock.hh create mode 100644 Gtkmm3/gtk99_clock/src/main.cc diff --git a/Gtkmm3/gtk99_clock/meson.build b/Gtkmm3/gtk99_clock/meson.build new file mode 100644 index 0000000..a25ff46 --- /dev/null +++ b/Gtkmm3/gtk99_clock/meson.build @@ -0,0 +1,30 @@ +#A Simple Project Test +project('gtk99', '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('gtk99', icon_res, 'src/main.cc', 'src/Clock.cc', dependencies : gtkdep, + win_subsystem : 'windows', include_directories : dir_include) +else + executable('gtk99', 'src/main.cc', 'src/Clock.cc', dependencies : gtkdep, + include_directories : dir_include) +endif diff --git a/Gtkmm3/gtk99_clock/src/Clock.cc b/Gtkmm3/gtk99_clock/src/Clock.cc new file mode 100644 index 0000000..2ade66b --- /dev/null +++ b/Gtkmm3/gtk99_clock/src/Clock.cc @@ -0,0 +1,112 @@ +#include "Clock.hh" +#include +#include + +Clock::Clock() +:m_radius(0.42),m_line_width(0.05) +{ + Glib::signal_timeout().connect(sigc::mem_fun(*this,&Clock::on_timeout),1000); +} + +bool Clock::on_draw(const Cairo::RefPtr &cr){ + Gtk::Allocation allocation=get_allocation(); + const int width=allocation.get_width(); + const int height=allocation.get_height(); + + // scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e. + // the center of the window + cr->scale(width,height); + cr->translate(0.5,0.5); + cr->set_line_width(m_line_width); + + cr->save(); + cr->set_source_rgba(0.337, 0.612, 0.117, 0.9); //green + cr->paint(); + cr->restore(); + cr->arc(0, 0, m_radius, 0, 2 * M_PI); + cr->save(); + cr->set_source_rgba(1.0, 1.0, 1.0, 0.8); + cr->fill_preserve(); + cr->restore(); + cr->stroke_preserve(); + cr->clip(); + + //Clock Ticks + for(int i=0;i<12;i++){ + double inset = 0.05; + + cr->save(); + cr->set_line_cap(Cairo::LINE_CAP_ROUND); + + if(i % 3 != 0) + { + inset *= 0.8; + cr->set_line_width(0.03); + } + + cr->move_to( + (m_radius - inset) * cos (i * M_PI / 6), + (m_radius - inset) * sin (i * M_PI / 6)); + cr->line_to ( + m_radius * cos (i * M_PI / 6), + m_radius * sin (i * M_PI / 6)); + cr->stroke(); + cr->restore(); /* stack-pen-size */ + } + + //Get Current time + time_t t; + time(&t); + struct tm *local=localtime(&t); + + //Convert time to angles + double minutes=local->tm_min * M_PI / 30; + double hours=local->tm_hour * M_PI / 6; + double seconds=local->tm_sec * M_PI /30; + + cr->save(); + cr->set_line_cap(Cairo::LINE_CAP_ROUND); + + //Seconds tick + cr->save(); + cr->set_line_width(m_line_width / 3); + cr->set_source_rgba(0.7, 0.7, 0.7, 0.8); // gray + cr->move_to(0,0); + cr->line_to(sin(seconds) * (m_radius * 0.9), + -cos(seconds) * (m_radius * 0.9)); + cr->stroke(); + cr->restore(); + + // draw the minutes hand + cr->set_source_rgba(0.117, 0.337, 0.612, 0.9); // blue + cr->move_to(0, 0); + cr->line_to(sin(minutes + seconds / 60) * (m_radius * 0.8), + -cos(minutes + seconds / 60) * (m_radius * 0.8)); + cr->stroke(); + + // draw the hours hand + cr->set_source_rgba(0.337, 0.612, 0.117, 0.9); // green + cr->move_to(0, 0); + cr->line_to(sin(hours + minutes / 12.0) * (m_radius * 0.5), + -cos(hours + minutes / 12.0) * (m_radius * 0.5)); + cr->stroke(); + cr->restore(); + + // draw a little dot in the middle + cr->arc(0, 0, m_line_width / 3.0, 0, 2 * M_PI); + cr->fill(); + + return true; +} + +bool Clock::on_timeout(){ + // force our program to redraw the entire clock. + auto win = get_window(); + if (win) + { + Gdk::Rectangle r(0, 0, get_allocation().get_width(), + get_allocation().get_height()); + win->invalidate_rect(r, false); + } + return true; +} diff --git a/Gtkmm3/gtk99_clock/src/Clock.hh b/Gtkmm3/gtk99_clock/src/Clock.hh new file mode 100644 index 0000000..3810ef6 --- /dev/null +++ b/Gtkmm3/gtk99_clock/src/Clock.hh @@ -0,0 +1,17 @@ +#pragma once + +#include + +class Clock : public Gtk::DrawingArea{ +public: + Clock(); +protected: + //Default Signals + bool on_draw(const Cairo::RefPtr &cr) override; + + bool on_timeout(); + + //Radius for the circle + double m_radius; + double m_line_width; +}; diff --git a/Gtkmm3/gtk99_clock/src/main.cc b/Gtkmm3/gtk99_clock/src/main.cc new file mode 100644 index 0000000..082e399 --- /dev/null +++ b/Gtkmm3/gtk99_clock/src/main.cc @@ -0,0 +1,17 @@ +#include +#include +#include "Clock.hh" + +int main(int argc,char **argv){ + auto app=Gtk::Application::create(argc,argv,"org.gtk.daleclack"); + + //Initalize window and a clock + Gtk::Window window; + window.set_icon_name("org.gtk.daleclack"); + window.set_title("Clock"); + Clock clock1; + window.add(clock1); + window.show_all(); + + return app->run(window); +}