Initial commit

parents
build/
cmake-build-debug/
.idea/
cmake_minimum_required(VERSION 3.14)
project(wlclock)
set(CMAKE_CXX_STANDARD 14)
include(FindPkgConfig)
if(NOT PKG_CONFIG_FOUND)
message(FATAL_ERROR "pkg-config not found!" )
endif()
pkg_check_modules(gtkmm-3.0 REQUIRED IMPORTED_TARGET gtkmm-3.0)
add_executable(wlclock
clock.cc
clock.h
main.cc colors.h)
target_link_libraries(wlclock PkgConfig::gtkmm-3.0)
\ No newline at end of file
# Wayland Clock
## Building
Requires Gtkmm, CMake, pkg-config.
```
mkdir build
cd build
cmake ..
make
```
#include <ctime>
#include <cmath>
#include <cairomm/context.h>
#include <glibmm/main.h>
#include "clock.h"
#include "colors.h"
Clock::Clock()
: m_radius(0.42), m_line_width(0.05)
{
Glib::signal_timeout().connect( sigc::mem_fun(*this, &Clock::on_timeout), 1000 );
}
Clock::~Clock()
{
}
bool Clock::on_draw(const Cairo::RefPtr<Cairo::Context>& 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(WINDOW_BG_COLOR);
cr->paint();
cr->restore();
cr->set_source_rgba(CLOCK_BORDER_COLOR);
cr->arc(0, 0, m_radius, 0, 2 * M_PI);
cr->save();
cr->set_source_rgba(CLOCK_BG_COLOR);
cr->fill_preserve();
cr->restore();
cr->stroke_preserve();
cr->clip();
//clock ticks
for (int i = 0; i < 12; i++)
{
double inset = 0.05;
cr->set_source_rgba(CLOCK_TICKS_COLOR);
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 */
}
// store the current time
time_t rawtime;
time(&rawtime);
struct tm * timeinfo = localtime (&rawtime);
// compute the angles of the indicators of our clock
double minutes = timeinfo->tm_min * M_PI / 30;
double hours = timeinfo->tm_hour * M_PI / 6;
double seconds= timeinfo->tm_sec * M_PI / 30;
cr->save();
cr->set_line_cap(Cairo::LINE_CAP_ROUND);
// draw the seconds hand
cr->save();
cr->set_line_width(m_line_width / 3);
cr->set_source_rgba(SEC_HAND_COLOR); // 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(MIN_HAND_COLOR); // 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(HOUR_HAND_COLOR); // 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->set_source_rgba(MIDDLE_DOT_COLOR);
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;
}
#ifndef GTKMM_EXAMPLE_CLOCK_H
#define GTKMM_EXAMPLE_CLOCK_H
#include <gtkmm/drawingarea.h>
class Clock : public Gtk::DrawingArea
{
public:
Clock();
virtual ~Clock();
protected:
//Override default signal handler:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
bool on_timeout();
double m_radius;
double m_line_width;
};
#endif // GTKMM_EXAMPLE_CLOCK_H
//
// Created by depau on 7/30/19.
//
#ifndef WLCLOCK_COLORS_H
#define WLCLOCK_COLORS_H
#define WINDOW_BG_COLOR 0, 0, 0, 1
#define CLOCK_TICKS_COLOR 0.1607843137254902, 0.17254901960784313, 0.19215686274509805, 1
#define CLOCK_BG_COLOR 0.0627450980392156, 0.06274509803921569, 0.06274509803921569, 1
#define CLOCK_BORDER_COLOR 0.1607843137254902, 0.17254901960784313, 0.19215686274509805, 1
#define SEC_HAND_COLOR 0.7, 0.65, 0.5, 0.3
#define MIN_HAND_COLOR 1, 0.7725490196078432, 0, 0.3
#define HOUR_HAND_COLOR 1, 0.7725490196078432, 0, 1
#define MIDDLE_DOT_COLOR 0.1607843137254902, 0.17254901960784313, 0.19215686274509805, 1
#endif //WLCLOCK_COLORS_H
#include "clock.h"
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#define APP_ID "org.poul.wlclock"
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv, APP_ID);
Gtk::Window win;
win.set_title("Wayland Clock");
win.set_default_size(400, 400);
win.set_startup_id(APP_ID);
win.set_wmclass(APP_ID, APP_ID);
Clock c;
win.add(c);
c.show();
return app->run(win);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment