]> mj.ucw.cz Git - teatimer.git/commitdiff
Add font resize feature
authorJiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Sun, 26 Feb 2023 19:56:41 +0000 (20:56 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 26 Feb 2023 20:32:12 +0000 (21:32 +0100)
Useful for displaying remaining time on full screen.

teatimer.c

index 78e72ff59142336eb954a6ec33f698be1b37fd74..b0e3b5c6c3f12530527c36f043d9d40c86542c64 100644 (file)
@@ -10,6 +10,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <time.h>
 #include <getopt.h>
 
@@ -21,6 +22,9 @@
 static guint second_timer;
 static char old_text[16];
 static GtkWidget *win, *hbox1, *vbox1, *timebox, *namebox, *togglebutton1;
+static PangoFontDescription *timebox_font;
+static bool autoresize_timebox_font = false;
+static int timebox_font_size;
 static time_t alarm_time;
 static char *run_notify;
 static char *default_name = "Tea";
@@ -230,6 +234,44 @@ on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpoin
   quit();
 }
 
+static void
+set_timebox_font_size(int new_size)
+{
+  if (new_size == timebox_font_size)
+    return;
+  timebox_font_size = new_size;
+
+  pango_font_description_set_size(timebox_font, timebox_font_size*PANGO_SCALE);
+  gtk_widget_modify_font(timebox, timebox_font);
+}
+
+static void
+on_window_resized(GtkWidget *widget UNUSED, GdkRectangle *rect, gpointer user_data UNUSED)
+{
+  int window_width = rect->width;
+  int window_height = rect->height;
+
+  if (autoresize_timebox_font)
+  {
+    // Binary search for optimal font size
+    int beg = 12, end = 1024;
+    while (beg < end)
+      {
+       int mid = (beg + end) / 2;
+       pango_font_description_set_size(timebox_font, mid*PANGO_SCALE);
+       PangoFontMetrics *metric = pango_context_get_metrics(gtk_widget_get_pango_context(timebox), timebox_font, NULL);
+       int width = pango_font_metrics_get_approximate_digit_width(metric) * 8.3 / PANGO_SCALE;
+       int height = pango_font_metrics_get_height(metric) / PANGO_SCALE;
+       if (width > window_width - 50 || height > window_height - 45)
+         end = mid;
+       else
+         beg = mid + 1;
+      }
+
+    set_timebox_font_size(beg);
+  }
+}
+
 static void
 open_window(void)
 {
@@ -254,12 +296,15 @@ open_window(void)
   gtk_widget_show(timebox);
   gtk_box_pack_start(GTK_BOX(vbox1), timebox, TRUE, TRUE, 0);
   gtk_entry_set_text(GTK_ENTRY(timebox), "00:00");
+  timebox_font = pango_font_description_from_string("Monospace");
+  set_timebox_font_size(12);
 
   togglebutton1 = gtk_toggle_button_new_with_label("Run");
   gtk_widget_show(togglebutton1);
   gtk_box_pack_start(GTK_BOX(hbox1), togglebutton1, FALSE, FALSE, 0);
 
   gtk_signal_connect(GTK_OBJECT(win), "remove", GTK_SIGNAL_FUNC(on_window_remove), NULL);
+  gtk_signal_connect(GTK_OBJECT(win), "size-allocate", GTK_SIGNAL_FUNC(on_window_resized), NULL);
   gtk_signal_connect(GTK_OBJECT(namebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
   gtk_signal_connect(GTK_OBJECT(timebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
   gtk_signal_connect(GTK_OBJECT(togglebutton1), "toggled", GTK_SIGNAL_FUNC(on_togglebutton1_toggled), NULL);
@@ -274,12 +319,13 @@ open_window(void)
   gtk_widget_show(win);
 }
 
-static const char short_opts[] = "r:n:k:";
+static const char short_opts[] = "r:n:k:a";
 
 static const struct option long_opts[] = {
   { "run",             required_argument,      NULL,   'r' },
   { "kill",            required_argument,      NULL,   'k' },
   { "timer-name",      required_argument,      NULL,   'n' },
+  { "auto-resize",     no_argument,    NULL,   'a' },
   { NULL,              0,                      NULL,   0   },
 };
 
@@ -293,6 +339,7 @@ Options:\n\
 \t\t\t\t%%%% will be expanded to %%\n\
 -k, --kill=<int>\tKill run program by a given signal when the timer is stopped\n\
 -n, --timer-name=<str>\tFill name box with <str>\n\
+-a, --auto-resize\tAutomatically resize font to fit the box\n\
 ");
   exit(1);
 }
@@ -328,6 +375,9 @@ main(int argc, char **argv)
       case 'n':
        default_name = optarg;
        break;
+      case 'a':
+       autoresize_timebox_font = true;
+       break;
       default:
        usage();
       }