]> git.cworth.org Git - scherzo/commitdiff
Add some scaling controls with plus/minus keybindings.
authorCarl Worth <cworth@cworth.org>
Sat, 17 Sep 2011 16:17:42 +0000 (09:17 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 17 Sep 2011 16:17:42 +0000 (09:17 -0700)
Total cheese in that the symbols (braces and clefs) don't change size yet,
but at least the line widths snap to nice integers.

scherzo.c
score.c

index c49584c2465e2c153f3f6690fa2df3045e829751..f6669edc3fda5194149cd72d88883c963b5cc076 100644 (file)
--- a/scherzo.c
+++ b/scherzo.c
@@ -17,6 +17,7 @@
  */
 
 #include <gtk/gtk.h>
  */
 
 #include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
 
 #include "score.h"
 
 
 #include "score.h"
 
@@ -41,8 +42,8 @@ on_delete_event_quit (unused (GtkWidget *widget),
 }
 
 static int
 }
 
 static int
-on_expose_event_draw (GtkWidget        *widget,
-                     unused (GdkEventExpose *event),
+on_expose_event_draw (GtkWidget *widget,
+                     unused (GdkEventExpose *expose),
                      void * user_data)
 {
     scherzo_t *scherzo = user_data;
                      void * user_data)
 {
     scherzo_t *scherzo = user_data;
@@ -63,6 +64,7 @@ on_expose_event_draw (GtkWidget       *widget,
 
     /* Add some padding on the left/right */
     cairo_translate (cr, pad, pad);
 
     /* Add some padding on the left/right */
     cairo_translate (cr, pad, pad);
+    score_set_staff_height (score, scherzo->staff_height);
     score_set_width (score, widget_width - 2 * pad);
 
     score_draw (score, cr);
     score_set_width (score, widget_width - 2 * pad);
 
     score_draw (score, cr);
@@ -70,6 +72,39 @@ on_expose_event_draw (GtkWidget      *widget,
     return TRUE;
 }
 
     return TRUE;
 }
 
+static int
+on_key_press_event (GtkWidget *widget,
+                   GdkEventKey *key,
+                   void *user_data)
+{
+    scherzo_t *scherzo = user_data;
+
+    switch (key->keyval) {
+    case GDK_KEY_plus:
+    case GDK_KEY_KP_Add:
+    case GDK_KEY_equal:
+    case GDK_KEY_KP_Equal:
+       scherzo->staff_height += 4;
+       gtk_widget_queue_draw (widget);
+       return TRUE;
+       break;
+    case GDK_KEY_minus:
+    case GDK_KEY_KP_Subtract:
+       scherzo->staff_height -= 4;
+       gtk_widget_queue_draw (widget);
+       return TRUE;
+       break;
+    case GDK_KEY_q:
+    case GDK_KEY_Q:
+    case GDK_KEY_Escape:
+       gtk_main_quit ();
+       return FALSE;
+    }
+
+    /* Allow the event to propagate to other handlers. */
+    return FALSE;
+}
+
 int
 main (int argc, char *argv[])
 {
 int
 main (int argc, char *argv[])
 {
@@ -80,7 +115,7 @@ main (int argc, char *argv[])
     gtk_init (&argc, &argv);
 
     scherzo.score = score_create (NULL);
     gtk_init (&argc, &argv);
 
     scherzo.score = score_create (NULL);
-    scherzo.staff_height = 20;
+    scherzo.staff_height = 24;
     score_set_staff_height (scherzo.score, scherzo.staff_height);
 
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     score_set_staff_height (scherzo.score, scherzo.staff_height);
 
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
@@ -97,6 +132,10 @@ main (int argc, char *argv[])
     g_signal_connect (drawing_area, "expose-event",  
                      G_CALLBACK (on_expose_event_draw),
                      &scherzo);
     g_signal_connect (drawing_area, "expose-event",  
                      G_CALLBACK (on_expose_event_draw),
                      &scherzo);
+
+    g_signal_connect (window, "key-press-event",  
+                     G_CALLBACK (on_key_press_event),
+                     &scherzo);
     
     gtk_widget_show_all (window);
     
     
     gtk_widget_show_all (window);
     
diff --git a/score.c b/score.c
index 598c298752d13d42634b487176dd90267419169c..5bb79096c394a45741810d3cb3b22e6ebeeeb552 100644 (file)
--- a/score.c
+++ b/score.c
@@ -28,6 +28,9 @@ struct score
     /* Height of one space within a staff */
     int space_height;
 
     /* Height of one space within a staff */
     int space_height;
 
+    /* Minimal line width for staff lines */
+    int line_width;
+
     /* Full width of staff */
     int width;
 };
     /* Full width of staff */
     int width;
 };
@@ -48,6 +51,11 @@ score_set_staff_height (score_t *score, int height)
 {
     score->space_height = (int) height / 4;
     score->staff_height = score->space_height * 4;
 {
     score->space_height = (int) height / 4;
     score->staff_height = score->space_height * 4;
+
+    score->line_width = score->space_height / 15;
+    if (score->line_width == 0)
+       score->line_width = 1;
+
     return score->staff_height;
 }
 
     return score->staff_height;
 }
 
@@ -96,15 +104,17 @@ _draw_staff (score_t *score, cairo_t *cr, score_clef_t clef)
     cairo_show_glyphs (cr, &glyph, 1);
 
     cairo_rectangle (cr,
     cairo_show_glyphs (cr, &glyph, 1);
 
     cairo_rectangle (cr,
-                    0.5, 0.5,
-                    score->width - 1.0, score->space_height * 4);
+                    score->line_width / 2.0,
+                    score->line_width / 2.0,
+                    score->width - score->line_width,
+                    score->space_height * 4);
     
     for (i = 1; i < 4; i++) {
     
     for (i = 1; i < 4; i++) {
-       cairo_move_to (cr, 0, i * score->space_height + 0.5);
+       cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
        cairo_rel_line_to (cr, score->width, 0);
     }
 
        cairo_rel_line_to (cr, score->width, 0);
     }
 
-    cairo_set_line_width (cr, 1.0);
+    cairo_set_line_width (cr, score->line_width);
 
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
     cairo_stroke (cr);
 
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
     cairo_stroke (cr);
@@ -148,11 +158,12 @@ _draw_grand_staff (score_t *score, cairo_t *cr)
 
     /* Vertical lines at each end */
     cairo_rectangle (cr,
 
     /* Vertical lines at each end */
     cairo_rectangle (cr,
-                    0.5, 0.5,
-                    score->width - 1.0,
+                    score->line_width / 2.0,
+                    score->line_width / 2.0,
+                    score->width - score->line_width,
                     score->staff_height * 3);
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
                     score->staff_height * 3);
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
-    cairo_set_line_width (cr, 1.0);
+    cairo_set_line_width (cr, score->line_width);
     cairo_stroke (cr);
 
     /* Top staff */
     cairo_stroke (cr);
 
     /* Top staff */