]> git.cworth.org Git - scherzo/blob - scherzo.c
c49584c2465e2c153f3f6690fa2df3045e829751
[scherzo] / scherzo.c
1 /* scherzo - Music notation training
2  *
3  * Copyright © 2010 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  */
18
19 #include <gtk/gtk.h>
20
21 #include "score.h"
22
23 #define unused(foo) foo __attribute__((unused))
24
25 typedef struct scherzo
26 {
27     score_t *score;
28     int staff_height;
29 } scherzo_t;
30
31 static int
32 on_delete_event_quit (unused (GtkWidget *widget),
33                       unused (GdkEvent *event),
34                       unused (gpointer user_data))
35 {
36     gtk_main_quit ();
37
38     /* Returning FALSE allows the default handler for delete-event
39      * to proceed to cleanup the widget. */
40     return FALSE;
41 }
42
43 static int
44 on_expose_event_draw (GtkWidget *widget,
45                       unused (GdkEventExpose *event),
46                       void * user_data)
47 {
48     scherzo_t *scherzo = user_data;
49     score_t *score = scherzo->score;
50     cairo_t *cr;
51     GtkAllocation allocation;
52     static const int pad = 10;
53     int widget_width;
54
55     gtk_widget_get_allocation (widget, &allocation);
56     widget_width = allocation.width;
57
58     cr = gdk_cairo_create (widget->window);
59
60     /* White background */
61     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
62     cairo_paint (cr);
63
64     /* Add some padding on the left/right */
65     cairo_translate (cr, pad, pad);
66     score_set_width (score, widget_width - 2 * pad);
67
68     score_draw (score, cr);
69  
70     return TRUE;
71 }
72
73 int
74 main (int argc, char *argv[])
75 {
76     GtkWidget *window;
77     GtkWidget *drawing_area;
78     scherzo_t scherzo;
79
80     gtk_init (&argc, &argv);
81
82     scherzo.score = score_create (NULL);
83     scherzo.staff_height = 20;
84     score_set_staff_height (scherzo.score, scherzo.staff_height);
85
86     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
87
88     gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
89
90     g_signal_connect (window, "delete-event",
91                       G_CALLBACK (on_delete_event_quit), NULL);
92
93     drawing_area = gtk_drawing_area_new ();
94
95     gtk_container_add (GTK_CONTAINER (window), drawing_area);
96
97     g_signal_connect (drawing_area, "expose-event",  
98                       G_CALLBACK (on_expose_event_draw),
99                       &scherzo);
100     
101     gtk_widget_show_all (window);
102     
103     gtk_main ();
104
105     return 0;
106 }