File: annotator.c

package info (click to toggle)
denemo 2.6.39-0.3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 66,228 kB
  • sloc: ansic: 94,371; lisp: 38,489; xml: 22,630; python: 1,930; sh: 1,239; makefile: 642; yacc: 288; sed: 93
file content (838 lines) | stat: -rw-r--r-- 29,791 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//gcc -Wall annotator.c -o annotator `pkg-config --cflags --libs gtk+-3.0` `pkg-config --cflags --libs evince-view-3.0`
//Tested on debian 3.16.0 and GTK3.14.5
//      annotator.c
//
//      Copyright 2019 Richard Shann
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 3 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include<gtk/gtk.h>
#ifdef USE_ATRIL
#include <atril-view.h>
#else
#include <evince-view.h>
#endif

#define SPOT_SIZE (10) //size of spot for picking out an annotation
typedef struct Location {
     gint  adj;
     gint x, y;
} Location;
typedef struct Annotation {
     gchar *annotation;
     gint page;
     gint x, y;
     double r,g,b,a;
     gchar *font;
} Annotation;



static const gchar *music_home;//the directory where your scores are stored
static GtkWidget *score_window;//the window containing the two halves of the score
static GtkWidget *navigation_window = NULL;//floating window for navigation
static GList *repeat_locations = NULL;// a list of locations for use when a repeat starts in the middle/lower half of a page
static GList *annotations = NULL;// a list of annotations to be displayed on the score
static GtkWidget *view;//the EvView* that shows the pages
static GtkWidget *eventbox;
static gchar *fontdesc=NULL;//current default font description string
static gint num_pages;
static gint current_page; // from 0 - 
static gboolean odd_left = TRUE; //if odd nubered pages are on left
static  gint x=0;//Window size and position on screen: 0 0 is top left corner of screen
static  gint y=0;
static  gint width=0;
static  gint height=0;
static gdouble aspect_ratio=1.414; //A4 page size default
static  EvDocumentModel *model;

static void next_page (void);//turn to the next page
static void previous_page (void);//turn back one page
static void goto_page (gint page);//page number starting at 1

static gchar *markings_file = NULL;//full path to file for storing repeat marks and annotations for currently loaded score
static gboolean markings_unsaved = FALSE; //TRUE when user has created or deleted markings in the current score 
static gint page_on = 'c', page_back = 'a', skip_page = 'b';
static guint64 last_pedal_time = 0;

static gchar *help_text =
"The music annotator allows hands-free turning of pages as you play from a musical score."
"\nand adding annotations to be picked up later by Denemo - anything from reminder accidentals"
" to the spectacles O^O that warn of a tricky passage to take special care with."
" The screen is devoted to two pages with either the odd or even pages on the left."
"\nControl of page turning is via foot pedals which send key press signals just like those of a normal keyboard."
" The right pedal goes on and the left goes back "
" The center pedal can be customised to do various tasks"
"\nTo mark up the score for reminders or proof-reading right click with the mouse at the point where you want the annotation to be placed"
" and choose \"Annotate here\" from the menu. You can drag an annotation if it is misplaced."
"\nThe menu also lets you delete an annotation, navigate the score etc."
"\n Currently Keypresses '%c' to go one page on, '%c' to go back for a repeat and '%c' to skip forward."
"\nTo change the defaults you can pass values on the command line - type pageturner --help to see the command line usage."
;

static void navigate (void);

static void show_help (void)
{
   GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_window_new(GTK_WINDOW_TOPLEVEL)), (GtkDialogFlags) (GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_INFO, GTK_BUTTONS_OK, 
   help_text,
   page_on, page_back, skip_page);
   gtk_widget_show_all (dialog);
   g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
}

static guint64 elapsed_time ()
{
   gint64 thistime = g_get_monotonic_time ();
   guint64 elapsed = 
   (thistime - last_pedal_time) / 1000; //in milliseconds
   last_pedal_time = g_get_monotonic_time ();
   return elapsed;
}

static void free_annotation (Annotation *a)
{
   g_free (a->annotation);
   g_free (a->font);
   g_free (a);
}
//load repeat marks and annotations from the file associated with the opened pdf score
static void load_markings (gchar *pdfname)
{
   if (markings_file) 
      g_free (markings_file);
   markings_file = g_strdup_printf ("%s%s", pdfname, ".marks");
   FILE *fp = fopen (markings_file, "r");
   if (fp)
      {
         gint window_width, window_height;
         if (2==fscanf (fp, "%d%d", &window_width, &window_height))
            {
               if ((width != window_width) || (height != window_height))
                  g_warning ("Height and Width of markings file does not match current window - expect misplaced marks %d %d %d %d\n", width, height, window_width, window_height);
               gint page, x, y;  
               gchar text1[100], text2[100];
               *text1 = *text2 = 0;
               double r,g,b,a;   
               while ( 7 == fscanf (fp, "%d%d%d%lf%lf%lf%lf\n", &page, &x, &y, &r, &g, &b, &a))
                                 {
                                    Annotation *ann = (Annotation*)g_malloc (sizeof (Annotation));
                                    ann->x = x;
                                    ann->y = y;
                              
                                    ann->page = page;
                                    ann->r = r;
                                    ann->g = g;
                                    ann->b = b;
                                    ann->a = a;
                                    if (fgets (text1, 100, fp))
                                       ann->font = g_strdup (text1);
                                    else
                                       ann->font = g_strdup ("Times 12\n");  
                                    
                                    if (fgets (text2, 100, fp))
                                       ann->annotation = g_strdup (text2);
                                    else
                                       ann->annotation = g_strdup ("???\n"); 
                                    *(ann->font + strlen (ann->font) - 1) = 0;   
                                    *(ann->annotation + strlen (ann->annotation) - 1) = 0;   
                                          
                                    annotations = g_list_append (annotations, ann);
                                 } 
            }
         else  g_warning ("Corrupt markings file");
        fclose (fp);
      }
   markings_unsaved = FALSE;
}
static void
load_score (gchar *pdfname, GError ** err)
{
  GFile *file;
  file = g_file_new_for_commandline_arg (pdfname);
  gchar *uri = g_file_get_uri (file);
  EvDocument *doc = ev_document_factory_get_document (uri, err);
  if (*err)
    {
      g_warning ("Trying to read the pdf file %s gave an error: %s", uri, (*err)->message);
      gtk_widget_queue_draw (view);
    }
  else
   {
     if (model == NULL)
       {
         model = ev_document_model_new_with_document (doc);
         ev_view_set_model ((EvView *) view, model);
      }
     else
      {
         ev_document_model_set_document (model, doc);
       }
G_GNUC_BEGIN_IGNORE_DEPRECATIONS    
      ev_document_model_set_dual_page (model, FALSE);
G_GNUC_END_IGNORE_DEPRECATIONS   
      ev_document_model_set_page (model, 0);
      for (num_pages=1;;num_pages++)
         {
            if (!ev_view_next_page ((EvView*)view))
               break;
         }
      g_print ("Number of pages %d\n", num_pages);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS    
      ev_document_model_set_dual_page (model, TRUE);
G_GNUC_END_IGNORE_DEPRECATIONS   
      ev_document_model_set_page (model, 0);
      ev_document_model_set_dual_page_odd_pages_left (model, odd_left);
      current_page = odd_left?0:-1;  g_print ("current page  %d \n", current_page);
       
      g_list_free_full (repeat_locations, g_free);
      repeat_locations = NULL;
      g_list_free_full (annotations, (GDestroyNotify)free_annotation);
      annotations = NULL;
      load_markings (pdfname);
      goto_page (1);
      if (navigation_window) 
         {
            gtk_widget_destroy (navigation_window);
            navigation_window = NULL;
            navigate ();
         }
   }
  return;
}

static void set_page (gint num)
{
   if ((num >= num_pages) || (num <0))
      {  static gboolean once = FALSE;
         if (once)
            {
               if(num>0)
                  goto_page (1);
               else
                  goto_page (num_pages - 1);
               once = FALSE;
            }
         else
            {
               g_warning ("Attempt to go off page %d\n", num);// two presses of forward wraps round
               once = TRUE;
            }
         return;
      }

  ev_document_model_set_page (model, num);
  current_page = num; 
  if (current_page == 0)
   current_page = odd_left?0:-1;
  g_print ("current page  %d (set page) \n", current_page);
  gtk_widget_queue_draw (view);
}
static void next_page (void)
{
   set_page (current_page + 2);
}


static void previous_page (void)
{
   if (current_page>1)
      set_page (current_page - 2);
   else
     set_page (0);
}

static void do_skip (void)
{

         g_warning ("Nothing implemented");
      
}
static void goto_page (gint page)
{
   ev_document_model_set_page (model, page - 1);
   current_page = page - 1; 
   if (current_page == 0)
      current_page = odd_left?0:-1;
   g_print ("current page  %d (set page) \n", current_page);
}
static void change_page (GtkSpinButton * widget)
{
   goto_page ((gint)gtk_spin_button_get_value (widget));
}

static gboolean keypress (GtkWidget *eventbox, GdkEventKey * event)
{
  guint keyval = event->keyval; 
  
 if (elapsed_time()>100)
      {
      if (keyval==page_on)
       {
         next_page();
       }

      if (keyval==skip_page)
       {
         do_skip ();
       }
      if (keyval==page_back)
       {
         previous_page();
       }
      }
 
  return FALSE;
}

static gboolean choose (gchar *filename)
{
   GtkWidget *top_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

   GtkWidget *chooser = gtk_file_chooser_dialog_new ("Choose Score",
                        GTK_WINDOW (top_window),
                        GTK_FILE_CHOOSER_ACTION_OPEN,
                        "Ok", GTK_RESPONSE_ACCEPT,
                         NULL);

   GtkFileFilter *filter = gtk_file_filter_new ();
   gtk_file_filter_set_name (filter, "PDF files");
   gtk_file_filter_add_pattern (filter, "*.pdf");
   gtk_file_filter_add_pattern (filter, "*.PDF");
   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

   if (filename && g_file_test(filename, G_FILE_TEST_IS_DIR))
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), filename);
   if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
       {
         gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
         GError *err = NULL;
         load_score (filename, &err);
      }
   gtk_widget_destroy (chooser);
  return FALSE; //remove this callback
}

static void back_to_main_window ()
{
   gtk_widget_grab_focus (score_window);
   gtk_window_present (GTK_WINDOW (score_window)); 
}

static void navigate (void)
{
  
   if (navigation_window) gtk_widget_destroy (navigation_window);

   navigation_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   g_signal_connect (navigation_window, "delete-event", G_CALLBACK(gtk_widget_hide), NULL);
   GtkWidget *label = gtk_label_new ("Set Page Number:");
   GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   gtk_container_add (GTK_CONTAINER (navigation_window), box);
   gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);
   GtkWidget *spinner_adj = (GtkWidget *) gtk_adjustment_new (1.0, 1.0, num_pages, 1.0, 1.0, 1.0);
   GtkWidget *spinner = (GtkWidget *) gtk_spin_button_new (GTK_ADJUSTMENT(spinner_adj), 100.0, 0);
   gtk_box_pack_start (GTK_BOX (box), spinner, FALSE, TRUE, 0);
   g_signal_connect (G_OBJECT (spinner), "value-changed", G_CALLBACK (change_page), NULL);
   g_signal_connect_after (G_OBJECT (spinner),  "value-changed", G_CALLBACK (back_to_main_window), NULL);

   gtk_widget_show_all (navigation_window);
}

static void odd_even (void)
{
   odd_left = !odd_left;
   ev_document_model_set_dual_page_odd_pages_left (model, odd_left);
   goto_page (1);
   gtk_widget_queue_draw (view);
}

// text and font must be malloc'd memory so the annotation can be freed
static Annotation *create_annotation (gchar *text, gint page, gdouble x, gdouble y, gchar *font, GdkRGBA *color)
{
    Annotation *ann;
    markings_unsaved = TRUE;
    ann = g_malloc (sizeof (Annotation));
    if (x>width/2)
      {
         x -= width/2;
         page++;
      }
    ann->annotation = text;
    ann->page = page;
    ann->x = x;
    ann->y = y;
    ann->r = color->red;
    ann->g = color->green;
    ann->b = color->blue;
    ann->a = color->alpha;
    ann->font = font;
    return ann;
}

static void delete_annotation (GList *annlink)
{
   
             markings_unsaved = TRUE;
             free_annotation (annlink->data);
             annotations = g_list_delete_link (annotations, annlink);
             gtk_widget_queue_draw (view);
}

static void font_chosen (GtkWidget *fontchooser)
{
   fontdesc = gtk_font_chooser_get_font (GTK_FONT_CHOOSER(fontchooser));
}

static GdkRGBA colordesc = {1.0,0.0,0.0,1.0};
#if 1 //!((GTK_MAJOR_VERSION>=3) && (GTK_MINOR_VERSION>=4)) bizarrely gtk_color_button_get_rgba is declared deprecated but gtk_color_chooser_get_color is not available.
static void color_chosen (GtkWidget *colorchooser)
   {
   G_GNUC_BEGIN_IGNORE_DEPRECATIONS    
      gtk_color_button_get_rgba (GTK_COLOR_BUTTON(colorchooser), &colordesc);
   G_GNUC_END_IGNORE_DEPRECATIONS
   }                    
#else
static void color_chosen (GtkWidget *colorchooser)
   {
      gtk_color_chooser_get_color (GTK_COLOR_CHOOSER(colorchooser), &colordesc);
   }
#endif

static void insert_button_text (GtkWidget *button, GtkWidget *entry)
{
    gchar *text = g_strdup_printf ("%s%s", gtk_entry_get_text (GTK_ENTRY (entry)), gtk_button_get_label(GTK_BUTTON(button)));
    gtk_entry_set_text (GTK_ENTRY (entry), text);
    g_free (text);
}

static void create_button (GtkWidget *hbox, GtkWidget *entry, gchar *text)
{
  GtkWidget *widget = gtk_button_new_with_label (text);
  g_signal_connect (G_OBJECT (widget), "clicked", G_CALLBACK (insert_button_text), entry);
  gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE,2); 

}
Annotation *get_annotation_from_user (gint page, gint x, gint y)
{
  GtkWidget *dialog;
  GtkWidget *entry;
  GtkWidget *label;
  gchar *entry_string = NULL;
  entry = gtk_entry_new ();

  dialog = gtk_dialog_new_with_buttons ("Annotation", GTK_WINDOW (gtk_window_new(GTK_WINDOW_TOPLEVEL)), (GtkDialogFlags) (GTK_DIALOG_DESTROY_WITH_PARENT), "OK", GTK_RESPONSE_ACCEPT, "Cancel", GTK_RESPONSE_REJECT, NULL);
  label = gtk_label_new ("Give annotation");
  GtkWidget *content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
  gtk_container_add (GTK_CONTAINER (content_area), label);
  GtkWidget *widget = gtk_font_button_new_with_font (fontdesc?fontdesc:"Sans 16");
  g_signal_connect (G_OBJECT (widget), "font-set", G_CALLBACK (font_chosen), NULL);
  gtk_container_add (GTK_CONTAINER (content_area), widget);
  widget = gtk_color_button_new ();
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 gtk_color_button_set_rgba (GTK_COLOR_BUTTON(widget), &colordesc); 
 G_GNUC_END_IGNORE_DEPRECATIONS

  g_signal_connect (G_OBJECT (widget), "color-set", G_CALLBACK (color_chosen), NULL);
  gtk_container_add (GTK_CONTAINER (content_area), widget); 
  GtkWidget *hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_container_add (GTK_CONTAINER (content_area), hbox); 
  
  create_button (hbox, entry, "O^O");
  create_button (hbox, entry, "♯");
  create_button (hbox, entry, "♭");
  create_button (hbox, entry, "♮");
  create_button (hbox, entry, "𝄪");
  create_button (hbox, entry, "𝄫");
  create_button (hbox, entry, "FIX -->");
  create_button (hbox, entry, "<---FIX");
   
    
  gtk_entry_set_text (GTK_ENTRY (entry), "");
  gtk_container_add (GTK_CONTAINER (content_area), entry);

  gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
  gtk_widget_show_all (dialog);

  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
        {
          gchar *string = NULL;
          
          if (GTK_DIALOG (dialog))
            {
               entry_string = (gchar *) gtk_entry_get_text (GTK_ENTRY (entry));
               string = g_strdup (entry_string);
               gtk_widget_destroy (dialog);
               Annotation *ann = create_annotation (string, page, x, y, g_strdup (fontdesc?fontdesc:"Sans 16"), &colordesc);
               return ann;
           }
       }
  else
   {
     gtk_widget_destroy (dialog);
   }
   return NULL;
}


  
static void mark_annotation (Annotation *p)
{
   Annotation *ann = get_annotation_from_user (p->page, p->x, p->y);
   if (ann)
      {
         annotations = g_list_append (annotations, ann);
         gtk_widget_queue_draw (view);
      }
}

static void mark_spot (Annotation *p)
{
   GdkRGBA color = { 0.98, 0.91, 0.3, 0.8 };
   Annotation *ann = create_annotation  (g_strdup("●"), p->page, p->x, p->y, g_strdup("denemo medium 40"), &color);
   if (ann)
      {
         annotations = g_list_append (annotations, ann);
         gtk_widget_queue_draw (view);
      }
}

static void save_annotations (void)
{
  if (markings_file)
      {
         FILE *fp = fopen (markings_file, "w");
         if (fp)
            {
               GList *g;
               fprintf (fp, "%d %d\n", width/2, height); g_print ("Saved %d %d\n", width/2, height);
             
               for (g=annotations;g;g=g->next)
                  {
                   Annotation *ann = (Annotation *)g->data;
                   fprintf (fp, "%d %d %d %f %f %f %f\n%s\n%s\n", ann->page, (int)ann->x, (int)ann->y, ann->r, ann->g, ann->b, ann->a, ann->font, ann->annotation);
                  }  
               fclose (fp);
            }
         else
            g_warning ("Could not write %s for markings\n", markings_file);
      }
   else
      g_warning ("No file for markings");
}

   
static GList *nearby_annotation (gint page, gint x, gint y)
{
   GList *g;
   for (g = annotations; g; g=g->next)
      {
        Annotation *ann = g->data;
         if ((ann->page == current_page) && abs(ann->x-x+15)<SPOT_SIZE && abs(ann->y-y+15)<SPOT_SIZE)
            {
              return g;
            }
         else 
           if ((ann->page == current_page + 1) && abs(width/2 + ann->x-x+15)<SPOT_SIZE && abs(ann->y-y+15)<SPOT_SIZE)
            {
              return g;
            }
      }
   return NULL;
}


void
hot_cursor (void)
{
  static GdkCursor *circ = NULL;
  if (!circ)
    circ = gdk_cursor_new_for_display (gdk_display_get_default (), GDK_TARGET);
  gdk_window_set_cursor (gtk_widget_get_window (score_window), circ);
}

void
normal_cursor (void)
{
  static GdkCursor *arrowcursor = NULL;
  if (!arrowcursor)
    arrowcursor = gdk_cursor_new_for_display (gdk_display_get_default (), GDK_LEFT_PTR);
  gdk_window_set_cursor (gtk_widget_get_window (score_window), arrowcursor);
}


static GList *dragging_annlink = NULL;
static gboolean button_press (GtkWidget * view, GdkEventButton * event)
{
  if (event->button == 1)
   { 
      Annotation current_position;//just as a page, x, y , not a real annotation
      current_position.page = current_page;
      current_position.x = (int)event->x;
      current_position.y = (int)event->y;
      dragging_annlink = nearby_annotation (current_position.page, current_position.x, current_position.y);
    //  if (dragging_annlink)
     //    hot_cursor();
   }
   return TRUE;
  return FALSE;                 //propagate further 
}

static gboolean motion_notify (GtkWidget * view, GdkEventMotion * event)
{
   if (dragging_annlink)
      { 
         Annotation *ann = dragging_annlink->data;
         if (ann->page == current_page)
           {
               markings_unsaved = TRUE;
               ann->x = event->x, ann->y = event->y;
               gtk_widget_queue_draw (view);
               return TRUE;
            }
         else
         if (ann->page == current_page +1)
           {
               markings_unsaved = TRUE;
               ann->x = event->x - width/2, ann->y = event->y;
               gtk_widget_queue_draw (view);
               return TRUE;
            }         
         
         
      }
      Annotation current_position;//just as a page, x, y , not a real annotation
      current_position.page = current_page;
      current_position.x = (int)event->x;
      current_position.y = (int)event->y;
      if (nearby_annotation (current_position.page, current_position.x, current_position.y))
         hot_cursor ();
      else
         normal_cursor ();
 
   return FALSE;                 //propagate further
}

static gboolean button_release (GtkWidget * view, GdkEventButton * event)
{
  if (dragging_annlink)
      {
      normal_cursor ();
      dragging_annlink = NULL;
      return TRUE;
      }
  if (event->button != 1)
   { 
      static Annotation current_position;//just as a page, x, y , not a real annotation
      current_position.page = current_page;
      current_position.x = (int)event->x;
      current_position.y = (int)event->y;
      GtkWidget *menu = gtk_menu_new ();
      GtkWidget *item;



      item = gtk_menu_item_new_with_label ("Annotate here");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (mark_annotation), &current_position);
      item = gtk_menu_item_new_with_label ("Spot here");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (mark_spot), &current_position);
      
      
      
      GList *annlink = nearby_annotation (current_position.page, current_position.x, current_position.y);
      item = gtk_menu_item_new_with_label ("Delete this annotation");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (delete_annotation), annlink);
      gtk_widget_set_sensitive (item,(annlink != NULL));
      
      item = gtk_menu_item_new_with_label ("Save annotations");
      gtk_widget_set_sensitive (item,markings_unsaved);
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (save_annotations), NULL);
        
      item = gtk_menu_item_new_with_label ("Navigate");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (navigate), NULL);

      item = gtk_menu_item_new_with_label ("Choose score");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (choose), NULL);
      
      item = gtk_menu_item_new_with_label ("Switch Odd/Even pages on left");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (odd_even), NULL);
          
       
      item = gtk_menu_item_new_with_label ("Help");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (show_help), NULL); 
      item = gtk_menu_item_new_with_label ("Quit");
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      g_signal_connect_swapped (G_OBJECT (item), "activate", G_CALLBACK (gtk_main_quit), NULL);     

      gtk_widget_show_all (menu);
#if ((GTK_MAJOR_VERSION==3) && (GTK_MINOR_VERSION>=22))
      gtk_menu_popup_at_pointer (GTK_MENU (menu), NULL);
#else
// FIXME something for gtk2
#endif   
      return TRUE;
   }
 return FALSE;
}


static void draw_text (cairo_t * cr, const char *font, const char *text, double x, double y, gboolean invert)
{
  //y -= size;
  //size *= 0.75;
  PangoLayout *layout;
  PangoFontDescription *desc;
  /* Create a PangoLayout, set the font and text */
  layout = pango_cairo_create_layout (cr);

  pango_layout_set_text (layout, text, -1);
  desc = pango_font_description_from_string (font);
 // pango_font_description_set_size (desc, size * PANGO_SCALE);
  pango_layout_set_font_description (layout, desc);
  pango_font_description_free (desc);
  pango_cairo_update_layout (cr, layout);


  cairo_move_to (cr, x, y);
  if (invert)
    cairo_scale (cr, 1, -1);
  pango_cairo_show_layout (cr, layout);
  /* free the layout object */
  g_object_unref (layout);
}



      
static gboolean overdraw (GtkWidget* view, cairo_t * cr)
{
   GList *g;
   gint pnum = current_page;
   for (g = annotations; g; g=g->next)
      {
         Annotation *ann = (Annotation*)g->data;
         gdouble x = ann->x, y = ann->y; 
         //g_print ("annotation %s page %d for current page %d?  x %d x+ halfwidth %d\n", ann->annotation, ann->page, pnum, (int)x, (int)(x+width/2));
         if (ann->page == pnum)
            {

               cairo_set_source_rgba (cr, ann->r, ann->g, ann->b, ann->a);
              
                  draw_text (cr, ann->font, ann->annotation, x, y, FALSE);
                  
            }
         else
         if (ann->page == pnum + 1)
            {
               cairo_set_source_rgba (cr, ann->r, ann->g, ann->b, ann->a);
               draw_text (cr, ann->font, ann->annotation, x + width/2, y, FALSE);
            }
      }
  return FALSE;
}


int main(int argc, char **argv)
 {
   gtk_init(&argc, &argv);
   GdkRectangle r;
#if ((GTK_MAJOR_VERSION==3) && (GTK_MINOR_VERSION>=22))
   gdk_monitor_get_workarea (gdk_display_get_primary_monitor (gdk_display_get_default ()), &r);
#else
// FIXME something for gtk2
#endif   
   width = r.width;
   height = 0;
 
   if (argc>1 && (1 != sscanf (argv[1], "%d", (int*)&page_on)))
    goto error;      
   if (argc>2 && (1 != sscanf (argv[2], "%d", (int*)&page_back)))
    goto error;      
   if (argc>3 && (1 != sscanf (argv[3], "%d", (int*)&skip_page)))
    goto error;      

      
   if (argc>4 && (1 != sscanf (argv[4], "%lf", &aspect_ratio)))
    goto error;
   if (argc>5 && (1 != sscanf (argv[5], "%d", &width)))
    goto error;
   if (argc>6 && (1 != sscanf (argv[6], "%d", &height)))
    goto error;
   if (height == 0)
         {
            if (aspect_ratio<0.001)
               aspect_ratio = 1.414;
            height = width * aspect_ratio;
            if (height > r.height)
               height = r.height;
            width = height/aspect_ratio;   
            x = (r.width-width)/2;
         }
   width *= 2; //duplex view see set_dual above
   g_print ("Using height %d width %d, aspect ratio %f at %d horizontally\n", height, width, aspect_ratio, x);
   music_home = g_get_home_dir();
   ev_init ();
   score_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_decorated (GTK_WINDOW (score_window), FALSE);
   g_signal_connect (score_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

   gtk_window_move (GTK_WINDOW (score_window), x, y);
   gtk_window_set_default_size (GTK_WINDOW (score_window), width, height);
   GtkWidget *main_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   gtk_container_add (GTK_CONTAINER(score_window), main_hbox);
   
   eventbox = gtk_event_box_new ();
   
   gtk_widget_add_events (eventbox, (GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK ));
   gtk_box_pack_start (GTK_BOX(main_hbox), eventbox, TRUE, TRUE, 0);
   
   GtkWidget *scroll = gtk_scrolled_window_new (NULL, NULL);
   gtk_container_add (GTK_CONTAINER(eventbox), scroll); 
   g_signal_connect (G_OBJECT(eventbox), "key-press-event", G_CALLBACK (keypress), NULL);
   view = (GtkWidget *) ev_view_new ();
   g_signal_connect (G_OBJECT(view), "button-release-event", G_CALLBACK (button_release), NULL);
   g_signal_connect (G_OBJECT(view), "button-press-event", G_CALLBACK (button_press), NULL);

   g_signal_connect (G_OBJECT (view), "motion_notify_event", G_CALLBACK (motion_notify), NULL);

   g_signal_connect_after (G_OBJECT (view), "draw", G_CALLBACK (overdraw), NULL);
   gtk_container_add (GTK_CONTAINER (scroll), view);

   gtk_widget_show_all (score_window);
   gtk_widget_hide (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW(scroll)));
  
   if (argc==1)
      show_help();
   g_idle_add ((GSourceFunc) choose, (gpointer) music_home);
   gtk_main();

   return 0;  
   error:
        g_print ("Usage: pageswitcher [page onward, default %d (ie '%c')] [lh page back, default  %d (ie '%c')]  [rh page forward, default  %d (ie '%c')]  [aspect ratio, default 1.414, ie A4] [width in pixels, default maximum][height in pixels - overrides monitor size and aspect ratio]\n",
               page_on, page_on, page_back, page_back, skip_page, skip_page);
         return -1;
 }