aboutsummaryrefslogtreecommitdiff
path: root/src/narrative_window.c
blob: 0f6145d08159539e31bf9a8f79ccf1df99c7e9a1 (plain)
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*
 * narrative_window.c
 *
 * Copyright © 2014-2018 Thomas White <taw@bitwiz.org.uk>
 *
 * This file is part of Colloquium.
 *
 * Colloquium 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, see <http://www.gnu.org/licenses/>.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

#include "colloquium.h"
#include "presentation.h"
#include "narrative_window.h"
#include "sc_editor.h"
#include "sc_parse.h"
#include "render.h"
#include "testcard.h"
#include "pr_clock.h"
#include "print.h"
#include "utils.h"
#include "stylesheet_editor.h"


struct _narrative_window
{
	GtkWidget *window;
	GtkToolItem         *bfirst;
	GtkToolItem         *bprev;
	GtkToolItem         *bnext;
	GtkToolItem         *blast;
	SCEditor *sceditor;
	GApplication *app;
	struct presentation *p;
	SCBlock             *dummy_top;
	SCSlideshow         *show;
	PRClock             *pr_clock;
};


static void show_error(NarrativeWindow *nw, const char *err)
{
	GtkWidget *mw;

	mw = gtk_message_dialog_new(GTK_WINDOW(nw->window),
	                            GTK_DIALOG_DESTROY_WITH_PARENT,
	                            GTK_MESSAGE_ERROR,
	                            GTK_BUTTONS_CLOSE, "%s", err);

	g_signal_connect_swapped(mw, "response",
	                         G_CALLBACK(gtk_widget_destroy), mw);

	gtk_widget_show(mw);
}


static void update_toolbar(NarrativeWindow *nw)
{
	int cur_para;

	cur_para = sc_editor_get_cursor_para(nw->sceditor);
	if ( cur_para == 0 ) {
		gtk_widget_set_sensitive(GTK_WIDGET(nw->bfirst), FALSE);
		gtk_widget_set_sensitive(GTK_WIDGET(nw->bprev), FALSE);
	} else {
		gtk_widget_set_sensitive(GTK_WIDGET(nw->bfirst), TRUE);
		gtk_widget_set_sensitive(GTK_WIDGET(nw->bprev), TRUE);
	}

	if ( cur_para == sc_editor_get_num_paras(nw->sceditor)-1 ) {
		gtk_widget_set_sensitive(GTK_WIDGET(nw->bnext), FALSE);
		gtk_widget_set_sensitive(GTK_WIDGET(nw->blast), FALSE);
	} else {
		gtk_widget_set_sensitive(GTK_WIDGET(nw->bnext), TRUE);
		gtk_widget_set_sensitive(GTK_WIDGET(nw->blast), TRUE);
	}
}


static gint saveas_response_sig(GtkWidget *d, gint response,
                                NarrativeWindow *nw)
{
	if ( response == GTK_RESPONSE_ACCEPT ) {

		GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(d));

		if ( save_presentation(nw->p, file) ) {
			show_error(nw, _("Failed to save presentation"));
		}

		g_object_unref(file);

	}

	gtk_widget_destroy(d);

	return 0;
}


static void saveas_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
{
	GtkWidget *d;
	NarrativeWindow *nw = vp;

	d = gtk_file_chooser_dialog_new(_("Save Presentation"),
	                                GTK_WINDOW(nw->window),
	                                GTK_FILE_CHOOSER_ACTION_SAVE,
	                                _("_Cancel"), GTK_RESPONSE_CANCEL,
	                                _("_Save"), GTK_RESPONSE_ACCEPT,
	                                NULL);
	gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(d),
	                                               TRUE);

	g_signal_connect(G_OBJECT(d), "response",
	                 G_CALLBACK(saveas_response_sig), nw);

	gtk_widget_show_all(d);
}


static void about_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
{
	NarrativeWindow *nw = vp;
	open_about_dialog(nw->window);
}


static void save_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
{
	NarrativeWindow *nw = vp;
	GFile *file;

	if ( nw->p->uri == NULL ) {
		return saveas_sig(NULL, NULL, nw);
	}

	file = g_file_new_for_uri(nw->p->uri);
	save_presentation(nw->p, file);
	g_object_unref(file);
}


static void delete_slide_sig(GSimpleAction *action, GVariant *parameter,
                              gpointer vp)
{
	SCBlock *ns;
	NarrativeWindow *nw = vp;

	/* Get the SCBlock corresponding to the slide */
	ns = sc_editor_get_cursor_bvp(nw->sceditor);
	if ( ns == NULL ) {
		fprintf(stderr, _("Not a slide!\n"));
		return;
	}

	sc_block_delete(&nw->dummy_top, ns);

	/* Full rerender */
	sc_editor_set_scblock(nw->sceditor, nw->dummy_top);
	nw->p->saved = 0;
	update_titlebar(nw);
}


static struct template_id *get_templates(SCBlock *ss, int *n)
{
	struct template_id *list;
	SCInterpreter *scin;

	scin = sc_interp_new(NULL, NULL, NULL, NULL);
	sc_interp_run_stylesheet(scin, ss);  /* ss == NULL is OK */
	list = sc_interp_get_templates(scin, n);
	sc_interp_destroy(scin);
	return list;
}


static void update_template_menus(NarrativeWindow *nw)
{
	struct template_id *templates;
	int i, n_templates;

	templates = get_templates(nw->p->stylesheet, &n_templates);

	for ( i=0; i<n_templates; i++ ) {
		printf("%2i: %s %s\n", i, templates[i].name,
		       templates[i].friendlyname);
		free(templates[i].name);
		free(templates[i].friendlyname);
		sc_block_free(templates[i].scblock);
	}

	free(templates);
}


static SCBlock *get_slide_template(SCBlock *ss)
{
	struct template_id *templates;
	int i, n_templates;
	SCBlock *ret = NULL;

	templates = get_templates(ss, &n_templates);

	for ( i=0; i<n_templates; i++ ) {
		if ( strcmp(templates[i].name, "slide") == 0 ) {
			ret = templates[i].scblock;
		} else {
			sc_block_free(templates[i].scblock);
		}
		free(templates[i].name);
		free(templates[i].friendlyname);
	}
	free(templates);

        /* No template? */
        if ( ret == NULL ) {
		ret = sc_parse("\\slide{}");
	}

	return ret;  /* NB this is a copy of the one owned by the interpreter */
}


static gint load_ss_response_sig(GtkWidget *d, gint response,
                                 NarrativeWindow *nw)
{
	if ( response == GTK_RESPONSE_ACCEPT ) {

		char *filename;
		char *stext;

		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(d));
		printf("Loading %s\n",filename);

		stext = load_everything(filename);
		if ( stext != NULL ) {

			SCBlock *bl;
			SCBlock *ss;
			bl = sc_parse(stext);
			free(stext);
			ss = find_stylesheet(bl);

			if ( ss != NULL ) {

				/* Substitute the style sheet in
				 * presentation Storycode */
				replace_stylesheet(nw->p, ss);

				sc_editor_set_stylesheet(nw->sceditor, ss);

				/* Full rerender, first block may have
				 * changed */
				sc_editor_set_scblock(nw->sceditor,
				                      nw->dummy_top);

			} else {
				fprintf(stderr, _("Not a style sheet\n"));
			}
		} else {
			fprintf(stderr, _("Failed to load\n"));
		}

		g_free(filename);

	}

	gtk_widget_destroy(d);

	return 0;
}


static void stylesheet_changed_sig(GtkWidget *da, NarrativeWindow *nw)
{
	/* It might have changed (been created) since last time */
	sc_editor_set_stylesheet(nw->sceditor, nw->p->stylesheet);

	/* Full rerender, first block may have changed */
	sc_editor_set_scblock(nw->sceditor, nw->dummy_top);
}


static void edit_ss_sig(GSimpleAction *action, GVariant *parameter,
                        gpointer vp)
{
	NarrativeWindow *nw = vp;
	StylesheetEditor *se;

	se = stylesheet_editor_new(nw->p);
	gtk_window_set_transient_for(GTK_WINDOW(se), GTK_WINDOW(nw->window));
	g_signal_connect(G_OBJECT(se), "changed",
	                 G_CALLBACK(stylesheet_changed_sig), nw);
	gtk_widget_show_all(GTK_WIDGET(se));
}


static void load_ss_sig(GSimpleAction *action, GVariant *parameter,
                        gpointer vp)
{
	//SCBlock *nsblock;
	//SCBlock *templ;
	NarrativeWindow *nw = vp;
	GtkWidget *d;

	d = gtk_file_chooser_dialog_new(_("Load stylesheet"),
	                                GTK_WINDOW(nw->window),
	                                GTK_FILE_CHOOSER_ACTION_OPEN,
	                                _("_Cancel"), GTK_RESPONSE_CANCEL,
	                                _("_Open"), GTK_RESPONSE_ACCEPT,
	                                NULL);

	g_signal_connect(G_OBJECT(d), "response",
	                 G_CALLBACK(load_ss_response_sig), nw);

	gtk_widget_show_all(d);
}


static void add_slide_sig(GSimpleAction *action, GVariant *parameter,
                          gpointer vp)
{
	SCBlock *nsblock;
	SCBlock *templ;
	NarrativeWindow *nw = vp;

	sc_editor_ensure_cursor(nw->sceditor);

	/* Split the current paragraph */
	nsblock = split_paragraph_at_cursor(nw->sceditor);

	/* Get the template */
	templ = get_slide_template(nw->p->stylesheet); /* our copy */
	show_sc_blocks(templ);

	/* Link the new SCBlock in */
	if ( nsblock != NULL ) {
		sc_block_append_p(nsblock, templ);
	} else {
		fprintf(stderr, _("Failed to split paragraph\n"));
	}

	sc_editor_set_scblock(nw->sceditor, nw->dummy_top);
	nw->p->saved = 0;
	update_titlebar(nw);
}


static void first_para_sig(GSimpleAction *action, GVariant *parameter,
                           gpointer vp)
{
	NarrativeWindow *nw = vp;
	sc_editor_set_cursor_para(nw->sceditor, 0);
	pr_clock_set_pos(nw->pr_clock, sc_editor_get_cursor_para(nw->sceditor),
	                               sc_editor_get_num_paras(nw->sceditor));
	update_toolbar(nw);
}


static void ss_prev_para(SCSlideshow *ss, void *vp)
{
	NarrativeWindow *nw = vp;
	if ( sc_editor_get_cursor_para(nw->sceditor) == 0 ) return;
	sc_editor_set_cursor_para(nw->sceditor,
	                          sc_editor_get_cursor_para(nw->sceditor)-1);
	pr_clock_set_pos(nw->pr_clock, sc_editor_get_cursor_para(nw->sceditor),
	                               sc_editor_get_num_paras(nw->sceditor));
	update_toolbar(nw);
}


static void prev_para_sig(GSimpleAction *action, GVariant *parameter,
                          gpointer vp)
{
	NarrativeWindow *nw = vp;
	ss_prev_para(nw->show, nw);
}


static void ss_next_para(SCSlideshow *ss, void *vp)
{
	NarrativeWindow *nw = vp;
	SCBlock *ns;

	sc_editor_set_cursor_para(nw->sceditor,
	                          sc_editor_get_cursor_para(nw->sceditor)+1);

	/* If we only have one monitor, don't try to do paragraph counting */
	if ( ss->single_monitor ) {
		int i, max;
		max = sc_editor_get_num_paras(nw->sceditor);
		for ( i=sc_editor_get_cursor_para(nw->sceditor); i<max; i++ ) {
			SCBlock *ns;
			sc_editor_set_cursor_para(nw->sceditor, i);
			ns = sc_editor_get_cursor_bvp(nw->sceditor);
			if ( ns != NULL ) break;
		}
	}

	pr_clock_set_pos(nw->pr_clock, sc_editor_get_cursor_para(nw->sceditor),
	                               sc_editor_get_num_paras(nw->sceditor));
	ns = sc_editor_get_cursor_bvp(nw->sceditor);
	if ( ns != NULL ) {
		sc_slideshow_set_slide(nw->show, ns);
	}
	update_toolbar(nw);
}


static void next_para_sig(GSimpleAction *action, GVariant *parameter,
                          gpointer vp)
{
	NarrativeWindow *nw = vp;
	ss_next_para(nw->show, nw);
}


static void last_para_sig(GSimpleAction *action, GVariant *parameter,
                          gpointer vp)
{
	NarrativeWindow *nw = vp;
	sc_editor_set_cursor_para(nw->sceditor, -1);
	pr_clock_set_pos(nw->pr_clock, sc_editor_get_cursor_para(nw->sceditor),
	                               sc_editor_get_num_paras(nw->sceditor));
	update_toolbar(nw);
}


static void open_clock_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
{
	NarrativeWindow *nw = vp;
	nw->pr_clock = pr_clock_new();
}


static void testcard_sig(GSimpleAction *action, GVariant *parameter,
                         gpointer vp)
{
	NarrativeWindow *nw = vp;
	show_testcard(nw->p);
}


static gint export_pdf_response_sig(GtkWidget *d, gint response,
                                    struct presentation *p)
{
       if ( response == GTK_RESPONSE_ACCEPT ) {
               char *filename;
               filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(d));
               export_pdf(p, filename);
               g_free(filename);
       }

       gtk_widget_destroy(d);

       return 0;
}


static void print_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
{
	NarrativeWindow *nw = vp;
	run_printing(nw->p, nw->window);
}


static void exportpdf_sig(GSimpleAction *action, GVariant *parameter,
                          gpointer vp)
{
	NarrativeWindow *nw = vp;
       GtkWidget *d;

       d = gtk_file_chooser_dialog_new(_("Export PDF"),
                                       NULL,
                                       GTK_FILE_CHOOSER_ACTION_SAVE,
                                       _("_Cancel"), GTK_RESPONSE_CANCEL,
                                       _("_Export"), GTK_RESPONSE_ACCEPT,
                                       NULL);
       gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(d),
                                                      TRUE);

       g_signal_connect(G_OBJECT(d), "response",
                        G_CALLBACK(export_pdf_response_sig), nw->p);

       gtk_widget_show_all(d);
}



static gboolean nw_button_press_sig(GtkWidget *da, GdkEventButton *event,
                                    NarrativeWindow *nw)
{
	return 0;
}


static void changed_sig(GtkWidget *da, NarrativeWindow *nw)
{
	nw->p->saved = 0;
	update_titlebar(nw);
}


static void scroll_down(NarrativeWindow *nw)
{
	gdouble inc, val;
	GtkAdjustment *vadj;

	vadj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(nw->sceditor));
	inc = gtk_adjustment_get_step_increment(GTK_ADJUSTMENT(vadj));
	val = gtk_adjustment_get_value(GTK_ADJUSTMENT(vadj));
	gtk_adjustment_set_value(GTK_ADJUSTMENT(vadj), inc+val);
}


static gboolean nw_destroy_sig(GtkWidget *da, NarrativeWindow *nw)
{
	g_application_release(nw->app);
	return FALSE;
}


static gboolean nw_key_press_sig(GtkWidget *da, GdkEventKey *event,
                                 NarrativeWindow *nw)
{
	switch ( event->keyval ) {

		case GDK_KEY_B :
		case GDK_KEY_b :
		if ( nw->show != NULL ) {
			scroll_down(nw);
			return TRUE;
		}
		break;

		case GDK_KEY_Page_Up :
		if ( nw->show != NULL ) {
			ss_prev_para(nw->show, nw);
			return TRUE;
		}
		break;

		case GDK_KEY_Page_Down :
		if ( nw->show != NULL) {
			ss_next_para(nw->show, nw);
			return TRUE;
		}
		break;

		case GDK_KEY_Escape :
		if ( nw->show != NULL ) {
			gtk_widget_destroy(GTK_WIDGET(nw->show));
			return TRUE;
		}
		break;

		case GDK_KEY_F5 :
		if ( nw->show != NULL ) {
			/* Trap F5 so that full rerender does NOT happen */
			return TRUE;
		}

	}

	return FALSE;
}


static gboolean ss_destroy_sig(GtkWidget *da, NarrativeWindow *nw)
{
	nw->show = NULL;
	sc_editor_set_para_highlight(nw->sceditor, 0);

	gtk_widget_set_sensitive(GTK_WIDGET(nw->bfirst), FALSE);
	gtk_widget_set_sensitive(GTK_WIDGET(nw->bprev), FALSE);
	gtk_widget_set_sensitive(GTK_WIDGET(nw->bnext), FALSE);
	gtk_widget_set_sensitive(GTK_WIDGET(nw->blast), FALSE);

	return FALSE;
}


static void start_slideshow_here_sig(GSimpleAction *action, GVariant *parameter,
                                     gpointer vp)
{
	NarrativeWindow *nw = vp;
	void *bvp;

	if ( num_slides(nw->p) == 0 ) return;

	bvp = sc_editor_get_cursor_bvp(nw->sceditor);
	if ( bvp == NULL ) return;

	nw->show = sc_slideshow_new(nw->p, GTK_APPLICATION(nw->app));
	if ( nw->show == NULL ) return;

	g_signal_connect(G_OBJECT(nw->show), "key-press-event",
		 G_CALLBACK(nw_key_press_sig), nw);
	g_signal_connect(G_OBJECT(nw->show), "destroy",
		 G_CALLBACK(ss_destroy_sig), nw);
	sc_slideshow_set_slide(nw->show, bvp);
	sc_editor_set_para_highlight(nw->sceditor, 1);
	gtk_widget_show_all(GTK_WIDGET(nw->show));
	update_toolbar(nw);
}


static void start_slideshow_noslides_sig(GSimpleAction *action, GVariant *parameter,
                                         gpointer vp)
{
	NarrativeWindow *nw = vp;

	if ( num_slides(nw->p) == 0 ) return;

	nw->show = sc_slideshow_new(nw->p, GTK_APPLICATION(nw->app));
	if ( nw->show == NULL ) return;

	g_signal_connect(G_OBJECT(nw->show), "key-press-event",
		 G_CALLBACK(nw_key_press_sig), nw);
	g_signal_connect(G_OBJECT(nw->show), "destroy",
		 G_CALLBACK(ss_destroy_sig), nw);
	sc_slideshow_set_slide(nw->show, first_slide(nw->p));
	sc_editor_set_para_highlight(nw->sceditor, 1);
	sc_editor_set_cursor_para(nw->sceditor, 0);
	update_toolbar(nw);
}


static void start_slideshow_sig(GSimpleAction *action, GVariant *parameter,
                                gpointer vp)
{
	NarrativeWindow *nw = vp;

	if ( num_slides(nw->p) == 0 ) return;

	nw->show = sc_slideshow_new(nw->p, GTK_APPLICATION(nw->app));
	if ( nw->show == NULL ) return;

	g_signal_connect(G_OBJECT(nw->show), "key-press-event",
		 G_CALLBACK(nw_key_press_sig), nw);
	g_signal_connect(G_OBJECT(nw->show), "destroy",
		 G_CALLBACK(ss_destroy_sig), nw);
	sc_slideshow_set_slide(nw->show, first_slide(nw->p));
	sc_editor_set_para_highlight(nw->sceditor, 1);
	sc_editor_set_cursor_para(nw->sceditor, 0);
	gtk_widget_show_all(GTK_WIDGET(nw->show));
	update_toolbar(nw);
}


static void nw_update_titlebar(NarrativeWindow *nw)
{
	char *tb = get_titlebar_string(nw->p);

	if ( nw->p->slidewindow != NULL ) {

		char *title;

		title = malloc(strlen(tb)+14);
		sprintf(title, "%s - Colloquium", tb);
		gtk_window_set_title(GTK_WINDOW(nw->window), title);
		free(title);

       }

}


static int create_thumbnail(SCInterpreter *scin, SCBlock *bl,
                            double *w, double *h, void **bvp, void *vp)
{
	struct presentation *p = vp;

	*w = 270.0*(p->slide_width / p->slide_height);
	*h = 270.0;
	*bvp = bl;

	return 1;
}


static cairo_surface_t *render_thumbnail(int w, int h, void *bvp, void *vp)
{
	struct presentation *p = vp;
	SCBlock *scblocks = bvp;
	cairo_surface_t *surf;
	struct frame *top;
	int sn = slide_number(p, scblocks);

	/* FIXME: Cache like crazy here */
	surf = render_sc(scblocks, w, h, p->slide_width, p->slide_height,
	                 p->stylesheet, NULL, p->is, sn, &top, p->lang);
	frame_free(top);

	return surf;
}


static int click_thumbnail(double x, double y, void *bvp, void *vp)
{
	struct presentation *p = vp;
	SCBlock *scblocks = bvp;

	if ( p->narrative_window->show != NULL ) {
		sc_slideshow_set_slide(p->narrative_window->show, scblocks);
	} else {
		slide_window_open(p, scblocks, p->narrative_window->app);
	}

	return 0;
}


GActionEntry nw_entries[] = {

	{ "about", about_sig, NULL, NULL, NULL },
	{ "save", save_sig, NULL, NULL, NULL },
	{ "saveas", saveas_sig, NULL, NULL, NULL },
	{ "deleteslide", delete_slide_sig, NULL, NULL, NULL },
	{ "slide", add_slide_sig, NULL, NULL, NULL },
	{ "loadstylesheet", load_ss_sig, NULL, NULL, NULL },
	{ "stylesheet", edit_ss_sig, NULL, NULL, NULL },
	{ "startslideshow", start_slideshow_sig, NULL, NULL, NULL },
	{ "startslideshowhere", start_slideshow_here_sig, NULL, NULL, NULL },
	{ "startslideshownoslides", start_slideshow_noslides_sig, NULL, NULL, NULL },
	{ "clock", open_clock_sig, NULL, NULL, NULL },
	{ "testcard", testcard_sig, NULL, NULL, NULL },
	{ "first", first_para_sig, NULL, NULL, NULL },
	{ "prev", prev_para_sig, NULL, NULL, NULL },
	{ "next", next_para_sig, NULL, NULL, NULL },
	{ "last", last_para_sig, NULL, NULL, NULL },
	{ "print", print_sig, NULL, NULL, NULL  },
	{ "exportpdf", exportpdf_sig, NULL, NULL, NULL  },
};


void update_titlebar(NarrativeWindow *nw)
{
	char *title;

	title = get_titlebar_string(nw->p);
	title = realloc(title, strlen(title)+16);
	if ( title == NULL ) return;

	strcat(title, " - Colloquium");
	if ( !nw->p->saved ) {
		strcat(title, " *");
	}
	gtk_window_set_title(GTK_WINDOW(nw->window), title);
	free(title);
}


NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *papp)
{
	NarrativeWindow *nw;
	GtkWidget *vbox;
	GtkWidget *scroll;
	GtkWidget *toolbar;
	GtkToolItem *button;
	SCCallbackList *cbl;
	GtkWidget *image;
	Colloquium *app = COLLOQUIUM(papp);

	if ( p->narrative_window != NULL ) {
		fprintf(stderr, _("Narrative window is already open!\n"));
		return NULL;
	}

	nw = calloc(1, sizeof(NarrativeWindow));
	if ( nw == NULL ) return NULL;

	nw->app = papp;
	nw->p = p;

	nw->window = gtk_application_window_new(GTK_APPLICATION(app));
	p->narrative_window = nw;
	update_titlebar(nw);

	g_action_map_add_action_entries(G_ACTION_MAP(nw->window), nw_entries,
	                                G_N_ELEMENTS(nw_entries), nw);

	nw_update_titlebar(nw);

	vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
	gtk_container_add(GTK_CONTAINER(nw->window), vbox);

	/* If the presentation is completely empty, give ourselves at least
	 * something to work with */
	if ( nw->p->scblocks == NULL ) {
		nw->p->scblocks = sc_parse("");
	}

	/* Put everything we have inside \presentation{}.
	 * SCEditor will start processing one level down */
	nw->dummy_top = sc_block_new_parent(nw->p->scblocks, "presentation");

	nw->sceditor = sc_editor_new(nw->dummy_top, p->stylesheet, p->lang,
	                             colloquium_get_imagestore(app));
	cbl = sc_callback_list_new();
	sc_callback_list_add_callback(cbl, "slide", create_thumbnail,
	                              render_thumbnail, click_thumbnail, p);
	sc_editor_set_callbacks(nw->sceditor, cbl);
	sc_editor_set_imagestore(nw->sceditor, p->is);

	toolbar = gtk_toolbar_new();
	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(toolbar), FALSE, FALSE, 0);

	/* Fullscreen */
	image = gtk_image_new_from_icon_name("view-fullscreen",
	                                     GTK_ICON_SIZE_LARGE_TOOLBAR);
	button = gtk_tool_button_new(image, _("Start slideshow"));
	gtk_actionable_set_action_name(GTK_ACTIONABLE(button),
	                               "win.startslideshow");
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(button));

	button = gtk_separator_tool_item_new();
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(button));

	/* Add slide */
	image = gtk_image_new_from_icon_name("list-add",
	                                     GTK_ICON_SIZE_LARGE_TOOLBAR);
	button = gtk_tool_button_new(image, _("Add slide"));
	gtk_actionable_set_action_name(GTK_ACTIONABLE(button),
	                               "win.slide");
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(button));

	button = gtk_separator_tool_item_new();
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(button));

	image = gtk_image_new_from_icon_name("go-top",
	                                     GTK_ICON_SIZE_LARGE_TOOLBAR);
	nw->bfirst = gtk_tool_button_new(image, _("First slide"));
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(nw->bfirst));
	gtk_actionable_set_action_name(GTK_ACTIONABLE(nw->bfirst),
	                               "win.first");

	image = gtk_image_new_from_icon_name("go-up",
	                                     GTK_ICON_SIZE_LARGE_TOOLBAR);
	nw->bprev = gtk_tool_button_new(image, _("Previous slide"));
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(nw->bprev));
	gtk_actionable_set_action_name(GTK_ACTIONABLE(nw->bprev),
	                               "win.prev");

	image = gtk_image_new_from_icon_name("go-down",
	                                     GTK_ICON_SIZE_LARGE_TOOLBAR);
	nw->bnext = gtk_tool_button_new(image, _("Next slide"));
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(nw->bnext));
	gtk_actionable_set_action_name(GTK_ACTIONABLE(nw->bnext),
	                               "win.next");

	image = gtk_image_new_from_icon_name("go-bottom",
	                                     GTK_ICON_SIZE_LARGE_TOOLBAR);
	nw->blast = gtk_tool_button_new(image, _("Last slide"));
	gtk_container_add(GTK_CONTAINER(toolbar), GTK_WIDGET(nw->blast));
	gtk_actionable_set_action_name(GTK_ACTIONABLE(nw->blast),
	                               "win.last");

	update_toolbar(nw);
	update_template_menus(nw);

	scroll = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
	                               GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(nw->sceditor));

	sc_editor_set_flow(nw->sceditor, 1);
	sc_editor_set_background(nw->sceditor, 0.9, 0.9, 0.9);
	sc_editor_set_min_border(nw->sceditor, 0.0);
	sc_editor_set_top_frame_editable(nw->sceditor, 1);

	g_signal_connect(G_OBJECT(nw->sceditor), "button-press-event",
	                 G_CALLBACK(nw_button_press_sig), nw);
	g_signal_connect(G_OBJECT(nw->sceditor), "changed",
	                 G_CALLBACK(changed_sig), nw);
	g_signal_connect(G_OBJECT(nw->sceditor), "key-press-event",
			 G_CALLBACK(nw_key_press_sig), nw);
	g_signal_connect(G_OBJECT(nw->window), "destroy",
			 G_CALLBACK(nw_destroy_sig), nw);

	gtk_window_set_default_size(GTK_WINDOW(nw->window), 768, 768);
	gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
	gtk_container_set_focus_child(GTK_CONTAINER(nw->window),
	                              GTK_WIDGET(nw->sceditor));

	gtk_widget_show_all(nw->window);
	g_application_hold(papp);

	return nw;
}