~ubuntu-branches/ubuntu/vivid/birdfont/vivid

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
/*
    Copyright (C) 2012, 2014 Johan Mattsson

    This library is free software; you can redistribute it and/or modify 
    it under the terms of the GNU Lesser General Public License as 
    published by the Free Software Foundation; either version 3 of the 
    License, or (at your option) any later version.

    This library 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 
    Lesser General Public License for more details.
*/

using Cairo;

namespace BirdFont {

public class TabBar : GLib.Object {
	
	int width = 0;
	int height = 0;
	
	public List<Tab> tabs;

	static const int NO_TAB = -1;
	static const int NEXT_TAB = -2;
	static const int PREVIOUS_TAB = -3;
	static const int PROGRESS_WHEEL = -3;

	int first_tab = 0;
	int selected = 0;
	int over = NO_TAB;
	int over_close_tab = NO_TAB;
	
	public signal void signal_tab_selected (Tab selected_tab);
	public signal void redraw (int x, int y, int w, int h);

	Tab? previous_tab = null;
	Tab? current_tab = null;

	ImageSurface? tab1_left = null;
	ImageSurface? tab1_right = null;

	ImageSurface? tab2_left = null;
	ImageSurface? tab2_right = null;

	ImageSurface? tab3_left = null;
	ImageSurface? tab3_right = null;
	
	ImageSurface? progress = null;

	ImageSurface? next_tab;
	ImageSurface? to_previous_tab;
	
	ImageSurface? tab_bar_background = null;
	
	double scale = 1; // scale images in 320 dpi
	
	bool processing = false;
	double wheel_rotation = 0;
	
	public TabBar () {
		tabs = new List<Tab> ();
		
		tab1_left = Icons.get_icon ("tab1_left.png");
		tab1_right = Icons.get_icon ("tab1_right.png");

		tab2_left = Icons.get_icon ("tab2_left.png");
		tab2_right = Icons.get_icon ("tab2_right.png");

		tab3_left = Icons.get_icon ("tab3_left.png");
		tab3_right = Icons.get_icon ("tab3_right.png");
		
		next_tab = Icons.get_icon ("next_tab.png");
		to_previous_tab = Icons.get_icon ("previous_tab.png");
		
		progress = Icons.get_icon ("progress_wheel.png");
		tab_bar_background = Icons.get_icon ("tab_bar_background.png");
	}
	
	public void motion (double x, double y) {
		over_close (x, y, out over, out over_close_tab);
	}

	private void over_close (double x, double y, out int over, out int over_close_tab) {
		int i = 0;
		double offset = 19;
		double edge;
		
		return_if_fail (tab3_right != null);
		
		edge = ((!)tab3_right).get_width () * scale;
		
		if (x < 19 && has_scroll ()) {
			over_close_tab = NO_TAB;
			over = PREVIOUS_TAB;
			return;
		}

		if (has_scroll () && has_progress_wheel ()) {
			if (x > width - 19) {
				over_close_tab = NO_TAB;
				over = PROGRESS_WHEEL;
			} else if (x > width - 2 * 19) {
				over_close_tab = NO_TAB;
				over = NEXT_TAB;
			}
		} else if (!has_scroll () && has_progress_wheel ()) {
			if (x > width - 19) {
				over_close_tab = NO_TAB;
				over = PROGRESS_WHEEL;
			}
		} else if (has_scroll () && !has_progress_wheel ()) {
			if (x > width - 19) {
				over_close_tab = NO_TAB;
				over = NEXT_TAB;
			}
		}
		
		foreach (Tab t in tabs) {
			if (i < first_tab) {
				i++;
				continue;
			}

			if (offset < x < offset + t.get_width () + edge) {
				over = i;
				
				if (height - 18 < y < height - 5 && x > offset + t.get_width () - 19) {
					over_close_tab =  i;
				} else {
					over_close_tab =  NO_TAB;
				}
				
				return;
			}
			
			offset += t.get_width () + ((!)tab1_right).get_width () * scale;
			i++;
		}

		over_close_tab = NO_TAB;		
		over = NO_TAB;
	}	
	
	
	/** Select tab for a glyph by charcode or name.
	 * @return true if the tab was found
	 */
	public bool select_char (string s) {
		int i = 0;
		
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
		
		foreach (Tab t in tabs) {
			if (t.get_display ().get_name () == s) {
				select_tab (i);
				return true;
			}
			i++;
		}
		
		return false;
	}

	public bool select_tab_name (string s) {
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
		
		return select_char (s);
	}

	public void select_overview () {
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
		
		select_tab_name ("Overview");
	}

	private void select_previous_tab () {
		Tab t;
		bool open;
		
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
		
		if (previous_tab == null) {
			return;
		}
		
		t = (!) previous_tab;
		open = selected_open_tab (t);
		
		if (!open) {
			select_tab ((int) tabs.length () - 1);
		}
	}
		
	public void close_display (FontDisplay f) {
		int i = -1;
		
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
		
		if (tabs.length () > 1) {
			foreach (var t in tabs) {
				++i;
				
				if (t.get_display () == f) {
					close_tab (i) ;
					return;
				}
			}	
		}
		
		return_if_fail (i != -1);
	} 

	public void close_all_tabs () {
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
		
		for (int i = 0; i < get_length (); i++) {
			if (close_tab (i, false, true)) {
				close_all_tabs ();
			}
		}
	}

	public bool close_tab (int index, bool background_tab = false, bool select_new_tab = true) {	
		unowned List<Tab?>? lt;
		Tab t;
		EmptyTab empty_tab_canvas;
		Tab empty_tab;
	
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
				
		if (!(0 <= index < tabs.length ())) {
			return false;
		}
		
		if (tabs.length () == 1) {
			empty_tab_canvas = new EmptyTab ("", "");
			MainWindow.get_glyph_canvas ().set_current_glyph (empty_tab_canvas);
			empty_tab = new Tab (empty_tab_canvas, 0, false);
			signal_tab_selected (empty_tab);
		}
		
		lt = tabs.nth(index);
		
		if (lt == null || ((!) lt).data == null) {
			return false;
		}

		if (first_tab > 0) {
			first_tab--;
		}

		t = (!) ((!) lt).data;

		if (t.has_close_button ()) {
			tabs.delete_link (tabs.nth(index));
			
			if (!background_tab && select_new_tab) {
				select_previous_tab ();
			}
			
			return true;
		}
		
		if (select_new_tab) {
			select_tab (index);
		}
		
		return false;
	}
	
	public bool close_by_name (string name, bool background_tab = false) {
		int i = 0;

		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
				
		foreach (var t in tabs) {
			if (t.get_display ().get_name () == name) {
				return close_tab (i, background_tab);
			}
			
			i++;
		}
		
		return false;
	}
	
	public void close_background_tab_by_name (string name) {
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
		
		close_by_name (name, true);
	}
	
	/** Select a tab and return true if it is open. */
	public bool selected_open_tab (Tab t) {
		int i = 0;

		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
		
		foreach (var n in tabs) {
			if (n == t) {
				select_tab (i);
				return true;
			}
			
			i++;
		}
		
		return false;
	}

	public Tab? get_nth (int i) {
		if (!(0 <= i < get_length ())) {
			return null;
		}
		
		return tabs.nth (i).data;
	}

	public Tab? get_tab (string name) {
		foreach (var n in tabs) {
			if (n.get_display ().get_name () == name) {
				return n;
			}
		}
		
		return null;
	}

	public bool selected_open_tab_by_name (string t) {
		int i = 0;
	
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
		
		foreach (var n in tabs) {
			if (n.get_display ().get_name () == t) {
				select_tab (i);
				return true;
			}
			
			i++;
		}
		
		return false;
	}
	
	public Tab get_selected_tab () {
		int s = get_selected ();
		if (0 <= s < tabs.length ()) {
			return tabs.nth (get_selected ()).data;
		}
		
		warning ("No tab selected.");
		return new Tab (new EmptyTab ("Error", "Error"), 30, false);
	}
	
	public uint get_length () {
		return tabs.length ();
	}

	public int get_selected () {
		return selected;
	}
	
	public void select_tab (int index, bool signal_selected = true) {
		Tab t;

		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
			
		// always close any pending text input if the user switches tab
		MainWindow.native_window.hide_text_input ();

		if (index == NEXT_TAB) {
			selected++;
			
			if (selected >=  tabs.length ()) {
				selected = (int) tabs.length () - 1;
			}
			
			scroll_to_tab (selected);
			return;
		}
		
		if (index == PREVIOUS_TAB) {

			if (selected > 0) {
				selected--;
			}
			
			scroll_to_tab (selected);
			return;
		}
		
		if (!(0 <= index < tabs.length ())) {
			return;
		}

		selected = index;
		
		unowned List<Tab?>? lt = tabs.nth(index);
		
		return_if_fail(lt != null);
		t = (!) ((!) lt).data;
		
		previous_tab = current_tab;
		current_tab = t;

		scroll_to_tab (selected, signal_selected);
	}
	
	private bool has_scroll () {
		int i = 0;
		double offset = 19;
		double end = (has_progress_wheel ()) ? width - 28 : width - 19;
		
		return_if_fail (tab1_right != null);
		
		if (first_tab > 0) {
			return true;
		}
		
		foreach (Tab t in tabs) {	
			if (i < first_tab) {
				i++;
				continue;
			}
			
			if (offset + t.get_width () + 3 > end) {
				return true;
			}

			offset += t.get_width () + ((!)tab1_right).get_width () * scale;
			i++;
		}
		
		return false;		
	}
	
	private void signal_selected (int index) {
		unowned List<Tab?>? lt;
		Tab t;
		
		lt = tabs.nth (index);
		return_if_fail (lt != null);		
		t = (!) ((!) lt).data;
		
		signal_tab_selected (t);		
	}
	
	private void scroll_to_tab (int index, bool send_signal_selected = true) {
		double offset = 19;
		int i = 0;
		double end = (has_progress_wheel ()) ? width - 28 : width - 19;
		
		return_if_fail (tab1_right != null);
		
		if (index < first_tab) {
			first_tab = index;
			
			if (send_signal_selected) {
				signal_selected (index);
			}
			return;
		}
		
		foreach (Tab t in tabs) {
			
			if (i < first_tab) {
				i++;
				continue;
			}
			
			// out of view
			if (offset + t.get_width () + 3 > end) {
				first_tab++;
				scroll_to_tab (index);
				return;
			}

			// in view
			if (i == index) {
				
				if (send_signal_selected) {				
					signal_selected (index);
				}
				
				return;
			}

			offset += t.get_width () + ((!)tab1_right).get_width () * scale;
			i++;
		}
		
		warning ("");
	}
	
	public void select_tab_click (double x, double y, int width, int height) {
		int over, close;
		
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
		
		this.width = width;
		this.height = height;
		this.scale = height / 117.0;
		
		over_close (x, y, out over, out close);

		if (over_close_tab >= 0 && over == selected) {
			close_tab (over_close_tab);
		} else {
			select_tab (over);
		}
	}
	
	public void add_tab (FontDisplay display_item, bool signal_selected = true) {
		double tab_width = -1;
		bool always_open = false;
		int s = (tabs.length () == 0) ? 0 : selected + 1;

		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return;
		}
				
		if (tab_width < 0) {
			tab_width = 9 * display_item.get_label ().char_count ();
			tab_width += 36;
		}
				
		tabs.insert (new Tab (display_item, tab_width, always_open), s);
		select_tab (s, signal_selected);
	}
	
	/** Returns true if the new item was added to the bar. */
	public bool add_unique_tab (FontDisplay display_item, bool signal_selected = true) {
		bool i;
		
		if (MenuTab.suppress_event) {
			warn_if_test ("Event supressed");
			return false;
		}
				
		i = select_tab_name (display_item.get_name ());

		if (!i) {
			add_tab (display_item, signal_selected);
			return true;
		}
		
		return false;
	}
	
	public void draw (Context cr, int width, int height) {
		double next_tab_x;
		double w, h;
		double progress_y;
		
		this.width = width;
		this.height = height;
		this.scale = height / 117.0;
				
		return_if_fail (to_previous_tab != null);
		return_if_fail (next_tab != null);
		return_if_fail (progress != null);
		return_if_fail (tab_bar_background != null);
		
		cr.save ();
		cr.rectangle (0, 0, width, height);
		cr.set_line_width (0);
		cr.set_source_rgba (230/255.0, 229/255.0, 228/255.0, 1);
		cr.fill_preserve ();
		cr.restore ();

		cr.save ();
		cr.scale (scale, scale);
		
		w = width / scale;
		h = height / scale;

		for (double i = 0; i < w; i += ((!)tab_bar_background).get_width ()) {
			cr.set_source_surface ((!) tab_bar_background, i, h - ((!) tab_bar_background).get_height ());
			cr.paint ();
		}

		if (has_scroll ()) {
			// left arrow
			cr.set_source_surface ((!) to_previous_tab, 2 / scale, h - ((!) to_previous_tab).get_height () - 5 / scale);
			cr.paint ();

			// right arrow
			next_tab_x = (has_progress_wheel ()) ? w - (2 * 19 + 3) / scale : w - 19 / scale;
			cr.set_source_surface ((!) next_tab, next_tab_x, h - ((!) next_tab).get_height () - 5 / scale);
			cr.paint ();
		}
		
		// progress wheel
		if (has_progress_wheel ()) {
			ImageSurface p = (!) progress;
			Surface s = new Surface.similar (p, p.get_content (), p.get_width (), p.get_height ());
			Context c = new Context (s);
			
			c.save ();
			c.rectangle (0, 0, p.get_width (), p.get_height ());
			c.set_line_width (0);
			c.set_source_rgba (230/255.0, 229/255.0, 228/255.0, 1);
			c.fill_preserve ();	
			
			c.translate (p.get_width () / 2.0, p.get_height () / 2.0);
			c.rotate (wheel_rotation);
			c.translate (-p.get_width () / 2.0, -p.get_height () / 2.0);
			
			c.set_source_surface ((!) progress, 0 ,0);
			
			c.translate (p.get_width () / 2.0, p.get_height () / 2.0);
			c.paint ();
			c.restore ();

			progress_y = (has_scroll ()) ? h - ((!) progress).get_height () - 5 / scale : (h - ((!) progress).get_height ()) / 2;
			cr.set_source_surface (c.get_target (), w - 19 / scale, progress_y);
			cr.paint ();
			
		}
		
		draw_tabs (cr);
		cr.restore ();
	}
	
	private void draw_tabs (Context cr) {
		double close_opacity;
		double offset = 19 / scale;
		double tab_width;
		int i = 0;
		double tabs_end = width / scale;
		double h = height / scale;
		double tab_height;
		double tab_y;
		
		if (has_progress_wheel ()) {
			tabs_end -= 19 / scale;
		}
		
		if (has_scroll ()) {
			tabs_end -= 19 / scale;
		}		
		
		return_if_fail (tab3_right != null);
		return_if_fail (tab2_right != null);
		return_if_fail (tab1_right != null);
		return_if_fail (tab3_left != null);
		return_if_fail (tab3_right != null);
		return_if_fail (tab2_left != null);
		return_if_fail (tab1_left != null);
		return_if_fail (tab1_right != null);
		return_if_fail (next_tab != null);
		
		tab_height = ((!)tab3_right).get_height ();
		tab_y = h - tab_height;
		
		foreach (Tab t in tabs) {
			
			if (i < first_tab) {
				i++;
				continue;
			}

			cr.save ();
			cr.translate (offset, 0);
			
			tab_width = t.get_width () / scale;
			
			if (offset + tab_width + ((!)next_tab).get_width () + 3 / scale > tabs_end) {
				cr.restore ();
				break;
			}
		
			// background
			if (i == selected) {
				for (int j = ((!)tab3_left).get_width (); j < tab_width; j++) {
					cr.set_source_surface ((!) tab3_right, j, tab_y);
					cr.paint ();
				}							
			} else if (i == over) {
				for (int j = ((!)tab2_left).get_width (); j < tab_width; j++) {
					cr.set_source_surface ((!) tab2_right, j, tab_y);
					cr.paint ();
				}				
			} else {
				for (int j = ((!)tab1_left).get_width (); j < tab_width; j++) {
					cr.set_source_surface ((!) tab1_right, j, tab_y);
					cr.paint ();
				}
			}
			
			// close (x)
			if (t.has_close_button ()) {
				cr.save ();
				cr.new_path ();
				cr.set_line_width (1 / scale);
				
				close_opacity = (over_close_tab == i) ? 1 : 0.2; 
				cr.set_source_rgba (0, 0, 0, close_opacity);
				
				cr.move_to (tab_width - 5 / scale, h - 14 / scale);
				cr.line_to (tab_width - 10 / scale, h - 9 / scale);

				cr.move_to (tab_width - 10 / scale, h - 14 / scale);
				cr.line_to (tab_width - 5 / scale, h - 9 / scale);
				
				cr.stroke ();
				cr.restore ();
			}

			cr.set_source_rgba (0, 0, 0, 1);
			cr.set_font_size (14 / scale);
			cr.move_to (8 / scale, h - 6 / scale);
			cr.show_text (t.get_display ().get_label ());
			cr.stroke ();
			
			// edges
			if (i == selected) {
				cr.set_source_surface ((!) tab3_left, 0, tab_y);
				cr.paint ();
				
				cr.set_source_surface ((!) tab3_right, tab_width, tab_y);
				cr.paint ();
			} else if (i == over) {
				cr.set_source_surface ((!) tab2_left, 0, tab_y);
				cr.paint ();

				cr.set_source_surface ((!) tab2_right, tab_width, tab_y);
				cr.paint ();
			} else {
				cr.set_source_surface ((!) tab1_left, 0, tab_y);
				cr.paint ();

				cr.set_source_surface ((!) tab1_right, tab_width, tab_y);
				cr.paint ();
			}

			cr.restore ();
			
			offset += tab_width + ((!)tab1_right).get_width ();
			i++;
		}
	}
	
	public void add_empty_tab (string name, string label) {
		add_tab (new EmptyTab (name, label));
	}
	
	bool has_progress_wheel () {
		return processing;
	}

	public void set_progress (bool running) {
		TimeoutSource timer;
		
		if (unlikely (processing == running)) {
			warning (@"Progress is already set to $running");
			return;
		}
		
		processing = running;
		
		if (processing) {
			timer = new TimeoutSource (50);
			timer.set_callback (() => {
				wheel_rotation += 0.008 * 2 * Math.PI;
				
				if (wheel_rotation > 2 * Math.PI) {
					wheel_rotation -= 2 * Math.PI;
				}
				redraw (width - 19, 0, 19, height);
				return processing;
			});
			timer.attach (null);
		}
	}
	
	public static void start_wheel () {
		if (!is_null (MainWindow.get_tab_bar ())) {
			MainWindow.get_tab_bar ().set_progress (true);
			Tool.yield ();
		}
	}

	public static void stop_wheel () {
		if (!is_null (MainWindow.get_tab_bar ())) {
			MainWindow.get_tab_bar ().set_progress (false);
			Tool.yield ();
		}
	}
}

}