summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2020-01-18 23:30:07 +0100
committerThomas White <taw@physics.org>2020-01-18 23:32:51 +0100
commitae5112ce573d88be392248978de623200330a143 (patch)
treea0b8704fe63ff4418bcc44ad43b5edafd4dad38f
parentfff66ac9857dc2b1a6d71e76591168787e81be0e (diff)
Only redraw the required area
...and make it a bit easier to set the size and position of the clock
-rw-r--r--glitchyclock.c52
1 files changed, 30 insertions, 22 deletions
diff --git a/glitchyclock.c b/glitchyclock.c
index de34a30..0e15787 100644
--- a/glitchyclock.c
+++ b/glitchyclock.c
@@ -36,6 +36,11 @@ struct glitchyclock
int base_minutes;
int base_seconds;
int flag;
+
+ double clock_x;
+ double clock_y;
+ double clock_w;
+ double clock_h;
};
@@ -90,6 +95,9 @@ struct glitch_cue cues[] = {
{ EOL, 0, 0, 0, 0 }
};
+const double screen_w_frac = 0.2; /* Fraction of screen width */
+const double screen_y_offset = 0.5; /* Fraction of screen height */
+const double clock_aspect = 0.305; /* Clock height divided by width */
static double get_monotonic_seconds()
{
@@ -116,11 +124,9 @@ static void run_cue(struct glitchyclock *gc)
static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct glitchyclock *gc)
{
- int w, h;
- int lw, lh;
- double sf;
+ double lw, lh;
+ int ilw, ilh;
PangoLayout *layout;
- const double screen_w_frac = 0.2;
char timestr[64];
double seconds_elapsed, seconds_rounded;
double real_seconds_elapsed, real_seconds_rounded;
@@ -130,13 +136,12 @@ static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct glitchyclock *gc
double duty;
const char *frozenstr = "vl:68";
- w = gtk_widget_get_allocated_width(widget);
- h = gtk_widget_get_allocated_height(widget);
-
/* Overall background */
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_paint(cr);
+ cairo_translate(cr, gc->clock_x, gc->clock_y);
+
layout = pango_layout_new(gtk_widget_get_pango_context(widget));
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
pango_layout_set_font_description(layout, gc->fontdesc);
@@ -223,20 +228,13 @@ static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct glitchyclock *gc
}
pango_layout_set_text(layout, "88:88", -1);
-
pango_cairo_update_layout(cr, layout);
- pango_layout_get_size(layout, &lw, &lh);
+ pango_layout_get_size(layout, &ilw, &ilh);
+ lw = (double)ilw / PANGO_SCALE;
+ lh = (double)ilh / PANGO_SCALE;
+ cairo_scale(cr, gc->clock_w/lw, gc->clock_h/lh);
- sf = (double)PANGO_SCALE*screen_w_frac*w/lw;
- cairo_scale(cr, sf, sf);
pango_cairo_update_layout(cr, layout);
-
- pango_layout_get_size(layout, &lw, &lh);
- lw /= PANGO_SCALE; lh /= PANGO_SCALE;
- w /= sf; h /= sf;
- cairo_translate(cr, (w-lw)/2.0, h-lh-5*lh);
- pango_cairo_update_layout(cr, layout);
-
cairo_set_source_rgb(cr, gc->brightness*0.10, 0.0, 0.0);
pango_cairo_show_layout(cr, layout);
@@ -248,13 +246,22 @@ static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct glitchyclock *gc
}
+static gboolean configure_sig(GtkWidget *da, GdkEventConfigure *event, struct glitchyclock *gc)
+{
+ gc->clock_w = screen_w_frac * event->width;
+ gc->clock_h = clock_aspect * gc->clock_w;
+ gc->clock_x = (event->width - gc->clock_w)/2.0;
+ gc->clock_y = event->height * screen_y_offset;
+
+ return FALSE;
+}
+
+
static gboolean redraw_cb(gpointer data)
{
- gint w, h;
struct glitchyclock *gc = data;
- w = gtk_widget_get_allocated_width(GTK_WIDGET(gc->da));
- h = gtk_widget_get_allocated_height(GTK_WIDGET(gc->da));
- gtk_widget_queue_draw_area(GTK_WIDGET(gc->da), 0, 0, w, h);
+ gtk_widget_queue_draw_area(GTK_WIDGET(gc->da), gc->clock_x, gc->clock_y,
+ gc->clock_w, gc->clock_h);
return G_SOURCE_CONTINUE;
}
@@ -317,6 +324,7 @@ int main(int argc, char *argv[])
gtk_widget_add_events(gc.da, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
g_signal_connect(G_OBJECT(gc.da), "draw", G_CALLBACK(draw_sig), &gc);
g_signal_connect(G_OBJECT(gc.da), "realize", G_CALLBACK(realise_sig), &gc);
+ g_signal_connect(G_OBJECT(gc.da), "configure-event", G_CALLBACK(configure_sig), &gc);
gtk_widget_grab_focus(GTK_WIDGET(gc.da));
gtk_widget_show_all(mainwindow);