[gtk+/resolution-independence] RI: gtk-demo: calculate monitor DPI from reported dimensions



commit b89a059967900cc5be7ce9b00aeff1e833c14434
Author: Davyd Madeley <davyd madeley id au>
Date:   Thu May 7 12:38:36 2009 +0800

    RI: gtk-demo: calculate monitor DPI from reported dimensions
    
    Add a button to set this as the DPI for the demo.
---
 demos/gtk-demo/resolution_independence.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/demos/gtk-demo/resolution_independence.c b/demos/gtk-demo/resolution_independence.c
index fca946d..ccd8647 100644
--- a/demos/gtk-demo/resolution_independence.c
+++ b/demos/gtk-demo/resolution_independence.c
@@ -13,6 +13,10 @@ static GtkWidget *window = NULL;
 static GtkWindowGroup *window_group = NULL;
 static GtkWidget *hscale;
 static GtkWidget *label;
+static GtkWidget *button;
+static gdouble button_dpi;
+
+#define INCHES_TO_MM 25.4
 
 #define MIN_DPI 24.0
 #define MAX_DPI 480.0
@@ -58,6 +62,7 @@ update (void)
 
   s = g_strdup_printf ("Monitor %d (%s) @ %dx%d+%d+%d\n"
                        "%d mm x %d mm\n"
+		       "DPI: %.1f x %.1f\n"
                        "Font \"%s\"\n"
                        "1 em -> %g pixels\n"
                        "1 mm -> %g pixels",
@@ -65,12 +70,20 @@ update (void)
                        plug_name != NULL ? plug_name : "unknown name",
                        geometry.width, geometry.height, geometry.x, geometry.y,
                        width_mm, height_mm,
+		       INCHES_TO_MM * geometry.width / width_mm,
+		       INCHES_TO_MM * geometry.height / height_mm,
                        font_name,
                        gtk_size_to_pixel_double (screen, monitor_num, gtk_size_em (1.0)),
                        gtk_size_to_pixel_double (screen, monitor_num, gtk_size_mm (1.0)));
   gtk_label_set_text (GTK_LABEL (label), s);
   g_free (s);
 
+  button_dpi = MIN (INCHES_TO_MM * geometry.width / width_mm,
+                    INCHES_TO_MM * geometry.height / height_mm);
+  s = g_strdup_printf ("Set DPI to %.1f", button_dpi);
+  gtk_button_set_label (GTK_BUTTON (button), s);
+  g_free (s);
+
   dpi = -1;
   if (screen != NULL)
     {
@@ -152,6 +165,12 @@ hscale_format_value (GtkScale *scale, gdouble value, gpointer user_data)
   return g_strdup_printf ("%g DPI", value);
 }
 
+static void
+dpi_button_clicked (GtkButton *button, gpointer user_data)
+{
+  gtk_range_set_value (GTK_RANGE (hscale), button_dpi);
+}
+
 GtkWidget *
 do_resolution_independence (GtkWidget *do_widget)
 {
@@ -192,6 +211,10 @@ do_resolution_independence (GtkWidget *do_widget)
   gtk_widget_set_size_request (window, GTK_SIZE_ONE_TWELFTH_EM (500), -1);
   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
 
+  button = gtk_button_new ();
+  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (dpi_button_clicked), NULL);
+  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
+
   /* avoid dialogs (e.g. printing) grabbing focus from us */
   if (window_group == NULL)
     window_group = gtk_window_group_new ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]