+ (show-all (window go))))
+
+
+(define (gnome-main book name)
+ (let* ((paper (ly:paper-book-paper book))
+ (paper-width (ly:output-def-lookup paper 'paper-width))
+ (paper-height (ly:output-def-lookup paper 'paper-height))
+ (page-width (inexact->exact (ceiling (* OUTPUT-SCALE paper-width))))
+ (page-height (inexact->exact (ceiling (* OUTPUT-SCALE paper-height))))
+ ;;(page-width (inexact->exact (ceiling paper-width)))
+ ;;(page-height (inexact->exact (ceiling paper-height)))
+
+ (screen-width (gdk-screen-width))
+ (screen-height (gdk-screen-height))
+ (desktop-height (- screen-height PANELS-HEIGHT))
+
+ (go (make <gnome-outputter>
+ #:name name
+ #:page-stencils (list->vector (map page-stencil (ly:paper-book-pages book)))
+ #:canvas-width page-width
+ #:canvas-height page-height
+ #:window-width
+ ;; huh, *2 -- pixels-per-unit?
+ (min (+ SCROLLBAR-SIZE (* page-width 2)) screen-width)
+ #:window-height
+ (min (+ BUTTON-HEIGHT SCROLLBAR-SIZE (* page-height 2))
+ desktop-height))))
+
+ ;; ugh. The GOOPS doc promises this is called automagically.
+ ;; possibly a goops 1.6.4 problem
+ (initialize go)
+
+ (dump-page go 0)
+
+ (gtk-main)))
+
+(define (dump-page go number)
+ (if (or (not (page-stencils go))
+ (< number 0)
+ (>= number (vector-length (page-stencils go))))
+ (stderr "No such page: ~S\n" (1+ number))
+
+ (let ((old-canvas (canvas go)))
+ (new-canvas go)
+ (set! (page-number go) number)
+
+ ;; no destroy method for gnome-canvas-text yet.
+ ;;(map destroy (gtk-container-get-children main-canvas))
+ ;;(map destroy text-items)
+
+ (set! (text-items go) '())
+ (debugf "page-stencil ~S: ~S\n"
+ (page-number go)
+ (vector-ref (page-stencils go) (page-number go)))
+
+ (ly:interpret-stencil-expression
+ ;; ;;(vector-ref (page-stencils go) (page-number go))
+ (ly:stencil-expr (vector-ref (page-stencils go) (page-number go)))
+ gnome-output-expression go '(0 . 0))
+
+ (if old-canvas (destroy old-canvas))
+ (add (scrolled go) (canvas go))
+ (show (canvas go)))))
+
+(define ifs #f)
+(define (get-ifs)
+ (if (not ifs)
+ (set! ifs (getenv "IFS")))
+ (if (not ifs)
+ (set! ifs " "))
+ ifs)
+
+(define (spawn-editor location)
+ (let* ((file-name (car location))
+ (line (cadr location))
+ (char (caddr location))
+ (column (cadddr location))
+ (command (get-editor-command file line char column)))
+ (debugf "spawning: ~s\n" command)
+ (if (= (primitive-fork) 0)
+ (let ((command-list (string-split command #\ )));; (get-ifs))))
+ (apply execlp command-list)
+ (primitive-exit)))))
+
+(define location-callback spawn-editor)
+
+(define (get-location grob)
+ (and-let* ((p (procedure? point-and-click))
+ (g grob)
+ (cause (ly:grob-property grob 'cause))
+ (music-origin (if (ly:event? cause)
+ (ly:event-property cause 'origin)
+ ;; How come #<unspecified> [and '()]
+ ;; are #t? :-(
+ #f)))
+ (if (ly:input-location? music-origin)
+ (ly:input-location music-origin)
+ #f)))
+
+;; todo: how to integrate nicely?
+;(define-public (tweak-grob-property grob sym val)
+; (set! (ly:grob-property grob sym) val))
+
+
+(define-method (tweak (go <gnome-outputter>) item offset)
+ (let* ((grob (hashq-ref (item-grobs go) item #f))
+ (extra-offset (ly:grob-property grob 'extra-offset))
+ (origin (if (null? extra-offset) '(0 . 0)
+ (offset-flip-y extra-offset))))
+
+ (if grob
+ (ly:grob-replace-tweak
+ grob (list tweak-grob-property
+ 'extra-offset
+ (offset-flip-y (offset-add origin offset)))))))
+
+(define-method (save-tweaks (go <gnome-outputter>))
+ (let* ((dumper (ly:make-dumper))
+ (tweaks (ly:all-tweaks))
+ (serialized-tweaks
+ (map
+ (lambda (tweak) (append
+ (list (ly:dumper-key-serial dumper (car tweak))
+ (list 'unquote (procedure-name (cadr tweak))))
+ (cddr tweak)))
+ tweaks)))
+
+ (if (not (null? serialized-tweaks))
+ (let ((file (open-file (string-append (name go) ".twy") "w")))
+ (format file
+ ";;;tweaks. Generated file. Do not edit.
+(ly:tweak-clear-registry)
+(ly:tweak-define-keys `~S)
+(ly:tweak-define-tweaks `~S)"
+ (ly:dumper-definitions dumper)
+ serialized-tweaks)))))
+
+;;;(define (item-event go grob item event)
+(define (item-event go item event)
+ ;;(stderr "EVENT: ~S\n" event)
+ ;;(stderr "TYPE: ~S\n" (gdk-event:type event))
+ (case (gdk-event:type event)
+ ((enter-notify) (gobject-set-property item 'fill-color "red"))
+ ((leave-notify) (gobject-set-property item 'fill-color "black"))
+ ((motion-notify) (if (ly:grob? (dragging go))
+ (let ((x (gdk-event-motion:x event))
+ (y (gdk-event-motion:y event))
+ (s output-scale)
+ (r (drag-location go)))
+ ;;(stderr "MOVED AT: ~S ~S\n" x y)
+ (move item (/ (- x (car r)) s) (/ (- y (cdr r)) s))
+ (set! (drag-location go) (cons x y)))))
+ ((button-release) (if (ly:grob? (dragging go))
+ (let ((x (gdk-event-button:x event))
+ (y (gdk-event-button:y event))
+ (s output-scale)
+ (o (drag-origin go))
+ (r (drag-location go)))
+ (move item (/ (- x (car r)) s) (/ (- y (cdr r)) s))
+ (set! (drag-location go) #f)
+ (set! (drag-origin go) #f)
+ (stderr "RELEASE at: ~S ~S\n" x y)
+ (set! (dragging go) #f)
+ (tweak go item (cons (/ (- x (car o)) s)
+ (/ (- y (cdr o)) s))))))
+ ((button-press)
+ (let ((button (gdk-event-button:button event)))
+ (cond
+ ((= button 1)
+ (if (null? (gdk-event-button:modifiers event))
+ (let ((x (gdk-event-button:x event))
+ (y (gdk-event-button:y event)))
+ (stderr "CLICK at: ~S ~S\n" x y)
+ (set! (dragging go) (hashq-ref (item-grobs go) item #f))
+ (set! (drag-origin go) (cons x y))
+ (set! (drag-location go) (cons x y)))
+ (begin
+ (stderr "CLICK WITH MODIFIERS: ~S\n"
+ (gdk-event-button:modifiers event))
+
+ ;; some modifier, do jump to source
+ (and-let* ((grob (hashq-ref (item-grobs go) item #f))
+ (location (get-location grob)))
+ (location-callback location)))))
+ ((= button 2)
+ (and-let* ((grob (hashq-ref (item-grobs go) item #f)))
+
+ (let ((properties (ly:grob-properties grob))
+ (basic-properties (ly:grob-basic-properties grob))
+ (x (inexact->exact (gdk-event-button:x-root event)))
+ (y (inexact->exact (gdk-event-button:y-root event))))
+
+ (debugf "GROB: ~S\n" grob)
+ (debugf "PROPERTIES: ~S\n" properties)
+ (debugf "BASIC PROPERTIES: ~S\n" basic-properties)
+
+ ;; FIXME: dialog iso window?
+ ;; http://www.gtk.org/tutorial/sec-textentries.html
+ (let ((window (make <gtk-window>))
+ (vbox (make <gtk-vbox>))
+ (ok (make <gtk-button> #:label "Ok")))
+
+ (add window vbox)
+ (connect ok 'clicked (lambda (b) (destroy window)))
+
+ (for-each
+ (lambda (x)
+ (let ((label (make <gtk-label>
+ ;;#:label (symbol->string (car x))))
+ #:label (format #f "~S" (car x))))
+ ;;(symbol->string (car x))))
+ (entry (make <gtk-entry>
+ #:text (format #f "~S" (cdr x))))
+ (hbox (make <gtk-hbox>)))
+ (add hbox label)
+ (add hbox entry)
+ (set-size-request label 150 BUTTON-HEIGHT)
+ (add vbox hbox)))
+ (append properties basic-properties))
+ (add vbox ok)
+
+ (show-all window)
+ (move window x y))))))))