1 ;;;; framework-gnome.scm --
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;; See output-gnome.scm for usage information.
10 (define-module (scm framework-gnome))
25 (define-public (output-framework basename book scopes fields )
26 (gnome-main book basename))
28 (define SCROLLBAR-SIZE 20)
29 (define BUTTON-HEIGHT 25)
30 (define PANELS-HEIGHT 80)
32 (define PIXELS-PER-UNIT 2)
34 (define OUTPUT-SCALE (* 2.5 PIXELS-PER-UNIT))
35 (define-public output-scale OUTPUT-SCALE)
37 (define (debugf string . rest)
39 (apply stderr (cons string rest))))
41 (define-class <gnome-outputter> ()
42 (name #:init-value "untitled" #:init-keyword #:name #:accessor name)
45 (dragging #:init-value #f #:accessor dragging)
46 (drag-origin #:init-value #f #:accessor drag-origin)
47 (drag-location #:init-value #f #:accessor drag-location)
49 (page-stencils ;;#:init-value '#()
50 #:init-keyword #:page-stencils #:accessor page-stencils)
51 (window #:init-value (make <gtk-window> #:type 'toplevel) #:accessor window)
52 (scrolled #:init-value (make <gtk-scrolled-window>) #:accessor scrolled)
53 (canvas #:init-value #f #:accessor canvas)
54 (page-number #:init-value 0 #:accessor page-number)
55 (pixels-per-unit #:init-value PIXELS-PER-UNIT #:accessor pixels-per-unit)
56 (text-items #:init-value '() #:accessor text-items)
57 (grob #:init-value #f #:accessor grob)
58 (item-grobs #:init-value (make-hash-table 31) #:accessor item-grobs)
59 (window-width #:init-keyword #:window-width #:accessor window-width)
60 (window-height #:init-keyword #:window-height #:accessor window-height)
61 (canvas-width #:init-keyword #:canvas-width #:accessor canvas-width)
62 (canvas-height #:init-keyword #:canvas-height #:accessor canvas-height))
64 (define-method (initialize (go <gnome-outputter>))
65 (let* ((save (make <gtk-button> #:label "Save"))
66 (exit (make <gtk-button> #:label "Exit"))
67 (next (make <gtk-button> #:label "Next"))
68 (prev (make <gtk-button> #:label "Previous"))
69 (vbox (make <gtk-vbox> #:homogeneous #f))
70 (hbox (make <gtk-hbox> #:homogeneous #f)))
72 (set-size-request (window go) (window-width go) (window-height go))
74 (set-size-request (scrolled go) (window-width go) (- (window-height go)
80 (add (window go) vbox)
81 (add vbox (scrolled go))
83 (add (scrolled go) (canvas go))
87 (set-size-request hbox (window-width go) BUTTON-HEIGHT)
89 ;; hmm? These are broken when using <gnome-outputter>.
90 ;;(set-child-packing vbox hbox #f #f 0 'end)
91 ;;(set-child-packing hbox button #f #f 0 'end)
93 (set-size-request exit (quotient (window-width go) 2) BUTTON-HEIGHT)
102 (connect exit 'clicked (lambda (b) (gtk-main-quit)))
103 (connect save 'clicked (lambda (b) (save-tweaks go)))
104 (connect next 'clicked (lambda (b) (dump-page go (1+ (page-number go)))))
105 (connect prev 'clicked (lambda (b) (dump-page go (1- (page-number go)))))
106 (connect (window go) 'key-press-event
107 (lambda (w e) (key-press-event go w e)))
109 (show-all (window go))))
112 (define (gnome-main book name)
113 (let* ((paper (ly:paper-book-paper book))
114 (paper-width (ly:output-def-lookup paper 'paper-width))
115 (paper-height (ly:output-def-lookup paper 'paper-height))
116 (page-width (inexact->exact (ceiling (* OUTPUT-SCALE paper-width))))
117 (page-height (inexact->exact (ceiling (* OUTPUT-SCALE paper-height))))
118 ;;(page-width (inexact->exact (ceiling paper-width)))
119 ;;(page-height (inexact->exact (ceiling paper-height)))
121 (screen-width (gdk-screen-width))
122 (screen-height (gdk-screen-height))
123 (desktop-height (- screen-height PANELS-HEIGHT))
125 (go (make <gnome-outputter>
127 #:page-stencils (list->vector (map page-stencil (ly:paper-book-pages book)))
128 #:canvas-width page-width
129 #:canvas-height page-height
131 ;; huh, *2 -- pixels-per-unit?
132 (min (+ SCROLLBAR-SIZE (* page-width 2)) screen-width)
134 (min (+ BUTTON-HEIGHT SCROLLBAR-SIZE (* page-height 2))
137 ;; ugh. The GOOPS doc promises this is called automagically.
138 ;; possibly a goops 1.6.4 problem
145 (define (dump-page go number)
146 (if (or (not (page-stencils go))
148 (>= number (vector-length (page-stencils go))))
149 (stderr "No such page: ~S\n" (1+ number))
151 (let ((old-canvas (canvas go)))
153 (set! (page-number go) number)
155 ;; no destroy method for gnome-canvas-text yet.
156 ;;(map destroy (gtk-container-get-children main-canvas))
157 ;;(map destroy text-items)
159 (set! (text-items go) '())
160 (debugf "page-stencil ~S: ~S\n"
162 (vector-ref (page-stencils go) (page-number go)))
164 (ly:interpret-stencil-expression
165 ;; ;;(vector-ref (page-stencils go) (page-number go))
166 (ly:stencil-expr (vector-ref (page-stencils go) (page-number go)))
167 gnome-output-expression go '(0 . 0))
169 (if old-canvas (destroy old-canvas))
170 (add (scrolled go) (canvas go))
171 (show (canvas go)))))
176 (set! ifs (getenv "IFS")))
181 (define (spawn-editor location)
182 (let* ((file-name (car location))
183 (line (cadr location))
184 (char (caddr location))
185 (column (cadddr location))
186 (command (get-editor-command file line char column)))
187 (debugf "spawning: ~s\n" command)
188 (if (= (primitive-fork) 0)
189 (let ((command-list (string-split command #\ )));; (get-ifs))))
190 (apply execlp command-list)
193 (define location-callback spawn-editor)
195 (define (get-location grob)
196 (and-let* ((p (procedure? point-and-click))
198 (cause (ly:grob-property grob 'cause))
199 (music-origin (if (ly:event? cause)
200 (ly:event-property cause 'origin)
201 ;; How come #<unspecified> [and '()]
204 (if (ly:input-location? music-origin)
205 (ly:input-location music-origin)
208 ;; todo: how to integrate nicely?
209 ;(define-public (tweak-grob-property grob sym val)
210 ; (set! (ly:grob-property grob sym) val))
213 (define-method (tweak (go <gnome-outputter>) item offset)
214 (let* ((grob (hashq-ref (item-grobs go) item #f))
215 (extra-offset (ly:grob-property grob 'extra-offset))
216 (origin (if (null? extra-offset) '(0 . 0)
217 (offset-flip-y extra-offset))))
220 (ly:grob-replace-tweak
221 grob (list tweak-grob-property
223 (offset-flip-y (offset-add origin offset)))))))
225 (define-method (save-tweaks (go <gnome-outputter>))
226 (let* ((dumper (ly:make-dumper))
227 (tweaks (ly:all-tweaks))
230 (lambda (tweak) (append
231 (list (ly:dumper-key-serial dumper (car tweak))
232 (list 'unquote (procedure-name (cadr tweak))))
236 (if (not (null? serialized-tweaks))
237 (let ((file (open-file (string-append (name go) ".twy") "w")))
239 ";;;tweaks. Generated file. Do not edit.
240 (ly:tweak-clear-registry)
241 (ly:tweak-define-keys `~S)
242 (ly:tweak-define-tweaks `~S)"
243 (ly:dumper-definitions dumper)
244 serialized-tweaks)))))
246 ;;;(define (item-event go grob item event)
247 (define (item-event go item event)
248 ;;(stderr "EVENT: ~S\n" event)
249 ;;(stderr "TYPE: ~S\n" (gdk-event:type event))
250 (case (gdk-event:type event)
251 ((enter-notify) (gobject-set-property item 'fill-color "red"))
252 ((leave-notify) (gobject-set-property item 'fill-color "black"))
253 ((motion-notify) (if (ly:grob? (dragging go))
254 (let ((x (gdk-event-motion:x event))
255 (y (gdk-event-motion:y event))
257 (r (drag-location go)))
258 ;;(stderr "MOVED AT: ~S ~S\n" x y)
259 (move item (/ (- x (car r)) s) (/ (- y (cdr r)) s))
260 (set! (drag-location go) (cons x y)))))
261 ((button-release) (if (ly:grob? (dragging go))
262 (let ((x (gdk-event-button:x event))
263 (y (gdk-event-button:y event))
266 (r (drag-location go)))
267 (move item (/ (- x (car r)) s) (/ (- y (cdr r)) s))
268 (set! (drag-location go) #f)
269 (set! (drag-origin go) #f)
270 (stderr "RELEASE at: ~S ~S\n" x y)
271 (set! (dragging go) #f)
272 (tweak go item (cons (/ (- x (car o)) s)
273 (/ (- y (cdr o)) s))))))
275 (let ((button (gdk-event-button:button event)))
278 (if (null? (gdk-event-button:modifiers event))
279 (let ((x (gdk-event-button:x event))
280 (y (gdk-event-button:y event)))
281 (stderr "CLICK at: ~S ~S\n" x y)
282 (set! (dragging go) (hashq-ref (item-grobs go) item #f))
283 (set! (drag-origin go) (cons x y))
284 (set! (drag-location go) (cons x y)))
286 (stderr "CLICK WITH MODIFIERS: ~S\n"
287 (gdk-event-button:modifiers event))
289 ;; some modifier, do jump to source
290 (and-let* ((grob (hashq-ref (item-grobs go) item #f))
291 (location (get-location grob)))
292 (location-callback location)))))
294 (and-let* ((grob (hashq-ref (item-grobs go) item #f)))
296 (let ((properties (ly:grob-properties grob))
297 (basic-properties (ly:grob-basic-properties grob))
298 (x (inexact->exact (gdk-event-button:x-root event)))
299 (y (inexact->exact (gdk-event-button:y-root event))))
301 (debugf "GROB: ~S\n" grob)
302 (debugf "PROPERTIES: ~S\n" properties)
303 (debugf "BASIC PROPERTIES: ~S\n" basic-properties)
305 ;; FIXME: dialog iso window?
306 ;; http://www.gtk.org/tutorial/sec-textentries.html
307 (let ((window (make <gtk-window>))
308 (vbox (make <gtk-vbox>))
309 (ok (make <gtk-button> #:label "Ok")))
312 (connect ok 'clicked (lambda (b) (destroy window)))
316 (let ((label (make <gtk-label>
317 ;;#:label (symbol->string (car x))))
318 #:label (format #f "~S" (car x))))
319 ;;(symbol->string (car x))))
320 (entry (make <gtk-entry>
321 #:text (format #f "~S" (cdr x))))
322 (hbox (make <gtk-hbox>)))
325 (set-size-request label 150 BUTTON-HEIGHT)
327 (append properties basic-properties))
331 (move window x y))))))))
333 ((2button-press) (gobject-set-property item 'fill-color "green"))
335 (let ((keyval (gdk-event-key:keyval event))
336 (mods (gdk-event-key:modifiers event))
337 (step (quotient (pixels-per-unit go) 2)))
338 (cond ((and (null? mods)
340 (tweak go item (cons 0 (- 0 step))))
342 (eq? keyval gdk:Down))
343 (tweak go item (cons 0 step)))
345 (eq? keyval gdk:Left))
346 (tweak go item (cons (- 0 step) 0)))
348 (eq? keyval gdk:Right))
349 (tweak go item (cons step 0)))))))
352 (define (scale-canvas go factor)
353 (set! (pixels-per-unit go) (* (pixels-per-unit go) factor))
354 (set-pixels-per-unit (canvas go) (pixels-per-unit go))
357 (let ((scale (gobject-get-property x 'scale))
358 (points (gobject-get-property x 'size-points)))
359 ;;(gobject-set-property x 'scale pixels-per-unit)
360 (gobject-set-property x 'size-points (* points factor))))
363 (define (key-press-event go item event)
364 (let ((keyval (gdk-event-key:keyval event))
365 (mods (gdk-event-key:modifiers event)))
366 (cond ((and (or (eq? keyval gdk:q)
368 (equal? mods '(control-mask modifier-mask)))
370 ((and (eq? keyval gdk:s)
371 (equal? mods '(control-mask modifier-mask)))
373 ((and #t ;;(null? mods)
374 (eq? keyval gdk:plus))
376 ((and #t ;; (null? mods)
377 (eq? keyval gdk:minus))
378 (scale-canvas go 0.5))
379 ((or (eq? keyval gdk:Page-Up)
380 (eq? keyval gdk:BackSpace))
381 (dump-page go (1- (page-number go))))
382 ((or (eq? keyval gdk:Page-Down)
383 (eq? keyval gdk:space))
384 (dump-page go (1+ (page-number go)))))
387 (define (new-canvas go)
388 (set! (canvas go) (make <gnome-canvas>))
389 (set-size-request (canvas go) (window-width go) (window-height go))
390 (set-scroll-region (canvas go) 0 0 (canvas-width go) (canvas-height go))
391 (set-pixels-per-unit (canvas go) (pixels-per-unit go))
392 (make <gnome-canvas-rect>
393 #:parent (root (canvas go))
394 #:x2 (canvas-width go) #:y2 (canvas-height go)
395 #:fill-color "white"))
397 (define output-gnome-module #f)
398 (define (get-output-gnome-module go)
399 (if (not output-gnome-module)
400 (let ((m (resolve-module '(scm output-gnome))))
401 (module-define! m 'canvas-root (lambda () (root (canvas go))))
402 (module-define! m 'output-scale output-scale)
403 (set! output-gnome-module m)))
406 (define-public (gnome-output-expression go expr)
407 (let* ((m (get-output-gnome-module go))
408 (result (eval expr m)))
410 ((ly:grob? result) (set! (grob go) result))
411 ((is-a? result <gnome-canvas-item>)
413 ;; AAARGH; grobs happen after stencils
414 ;; (connect result 'event (lambda (w e) (item-event go (grob go) w e)))
415 (connect result 'event (lambda (w e) (item-event go w e)))
417 (hashq-set! (item-grobs go) result (grob go)))
420 (if (is-a? result <gnome-canvas-text>)
421 (set! (text-items go) (cons result (text-items go))))))))