.
1 /*
2 Copyright (C) 2013 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14 using Cairo;
15 using Math;
16
17 namespace BirdFont {
18
19 public class LanguageSelectionTab : FontDisplay {
20
21 int scroll = 0;
22 int visible_rows = 0;
23 WidgetAllocation allocation;
24
25 public LanguageSelectionTab () {
26 allocation = new WidgetAllocation ();
27 }
28
29 void select_language (int row) {
30 string iso_code;
31 TabBar tb = MainWindow.get_tab_bar ();
32
33 return_if_fail (0 <= row < DefaultLanguages.codes.size);
34
35 iso_code = DefaultLanguages.codes.get (row);
36 Preferences.set ("language", iso_code);
37 tb.close_display (this);
38 Toolbox.select_tool_by_name ("custom_character_set");
39 }
40
41 public override void button_release (int button, double ex, double ey) {
42 int r = (int) rint ((ey - 17) / 18.0);
43 if (button == 1 && 0 <= r < DefaultLanguages.codes.size) {
44 select_language (r + scroll);
45 }
46 }
47
48 public override void draw (WidgetAllocation allocation, Context cr) {
49 int y = 20;
50 int s = 0;
51 bool color = (scroll % 2) == 0;
52
53 this.allocation = allocation;
54
55 visible_rows = (int) (allocation.height / 18.0);
56
57 cr.save ();
58 Theme.color (cr, "Background 1");
59 cr.rectangle (0, 0, allocation.width, allocation.height);
60 cr.fill ();
61 cr.restore ();
62
63 cr.save ();
64 Theme.color (cr, "Background 5");
65 cr.set_font_size (12);
66
67 foreach (string language in DefaultLanguages.names) {
68 if (s++ >= scroll) {
69 draw_row (allocation, cr, language, color, y);
70 y += 18;
71 color = !color;
72 }
73 }
74 cr.restore ();
75 }
76
77 private static void draw_row (WidgetAllocation allocation, Context cr, string language, bool color, double y) {
78
79 if (color) {
80 cr.save ();
81 Theme.color (cr, "Background 6");
82 cr.rectangle (0, y - 14, allocation.width, 18);
83 cr.fill ();
84 cr.restore ();
85 }
86
87 cr.move_to (30, y);
88 cr.show_text (language);
89 }
90
91 public override string get_label () {
92 return t_("Character Set");
93 }
94
95 public override string get_name () {
96 return "Character Set";
97 }
98
99 public override bool has_scrollbar () {
100 return true;
101 }
102
103 public override void scroll_wheel_down (double x, double y) {
104 uint rows = DefaultLanguages.names.size;
105 scroll += 3;
106
107 if (scroll + visible_rows > rows) {
108 scroll = (int) (rows - visible_rows);
109 }
110
111 if (scroll < 0) {
112 scroll = 0;
113 }
114
115 update_scrollbar ();
116 redraw_area (0, 0, allocation.width, allocation.height);
117 }
118
119 public override void scroll_wheel_up (double x, double y) {
120 scroll -= 3;
121
122 if (scroll < 0) {
123 scroll = 0;
124 }
125
126 update_scrollbar ();
127 redraw_area (0, 0, allocation.width, allocation.height);
128 }
129
130 public override void selected_canvas () {
131 update_scrollbar ();
132 redraw_area (0, 0, allocation.width, allocation.height);
133 }
134
135 public override void update_scrollbar () {
136 uint rows = DefaultLanguages.names.size;
137
138 if (rows == 0 || visible_rows == 0) {
139 MainWindow.set_scrollbar_size (0);
140 MainWindow.set_scrollbar_position (0);
141 } else {
142 MainWindow.set_scrollbar_size ((double) visible_rows / rows);
143 MainWindow.set_scrollbar_position ((double) scroll / rows);
144 }
145 }
146
147 public override void scroll_to (double percent) {
148 uint rows = DefaultLanguages.names.size;
149 scroll = (int) (percent * rows);
150
151 if (scroll > rows - visible_rows) {
152 scroll = (int) (rows - visible_rows);
153 }
154
155 redraw_area (0, 0, allocation.width, allocation.height);
156 }
157 }
158
159 }
160