7
7
class BookWormWindow : Window
9
9
private Granite.Widgets.Welcome welcome;
10
private WebKit.WebView text_view;
11
private ScrolledWindow scrolled_window;
12
private Button nextButton;
13
private Button backButton;
14
private int pageno = 0;
10
private Button next_button;
11
private Button back_button;
15
12
private Grid controls;
17
private Gee.HashMap<string, string> contents;
18
private List<string> toc;
19
private int pageCount = 0;
20
private string booktitle;
22
private List<int> pageCountSection;
23
private int totalPageCount;
24
private int totalPageNumber;
14
private EBookWidget ebook_widget;
26
16
private Gtk.Label page_counter_label;
48
40
white_bg.parse("rgb(255,255,255)");
49
41
override_background_color (StateFlags.NORMAL, white_bg);
51
nextButton = new Button ();
43
next_button = new Button ();
52
44
Gtk.Image next_img = new Gtk.Image.from_icon_name ("go-next-symbolic",
53
45
Gtk.IconSize.BUTTON);
54
nextButton.set_image(next_img);
46
next_button.set_image(next_img);
56
backButton = new Button();
48
back_button = new Button();
57
49
Gtk.Image back_img = new Gtk.Image.from_icon_name ("go-previous-symbolic",
58
50
Gtk.IconSize.BUTTON);
59
backButton.set_image(back_img);
51
back_button.set_image(back_img);
61
53
set_title ("BookWorm");
62
54
set_size_request (640, 480);
63
text_view = new WebKit.WebView ();
64
text_view.expand = true;
65
scrolled_window = new ScrolledWindow (null, null);
66
//scrolled_window.set_policy (PolicyType.NEVER, PolicyType.NEVER);
67
scrolled_window.add (this.text_view);
68
scrolled_window.expand = true;
69
//h_scrollBar = (Scrollbar) scrolled_window.get_hscrollbar();
70
text_view.notify["load-status"].connect (webview_status_changed);
56
controls = new Grid ();
72
58
controls.hexpand = true;
73
59
content_grid.attach (welcome, 0, 1, 1, 1);
74
content_grid.attach (scrolled_window, 1, 1, 1, 1);
77
controls = new Grid ();
78
controls.attach (backButton, 0, 0, 1, 1);
60
content_grid.attach (ebook_widget, 1, 1, 1, 1);
64
controls.attach (back_button, 0, 0, 1, 1);
79
65
var spacer1 = new Gtk.Grid();
80
66
spacer1.hexpand = true;
81
67
controls.attach (spacer1, 1, 0, 1, 1);
88
74
controls.attach (spacer2, 3, 0, 1, 1);
91
controls.attach (nextButton, 4, 0, 1, 1);
77
controls.attach (next_button, 4, 0, 1, 1);
93
79
content_grid.attach (controls, 0, 2, 2, 1);
95
81
content_grid.show_all ();
96
scrolled_window.show_all ();
97
scrolled_window.hide ();
82
ebook_widget.show_all ();
99
85
welcome.append( Gtk.Stock.OPEN, "Open Book", "Open a Book");
100
86
welcome.set_sensitive (true);
101
87
welcome.show_all ();
103
89
welcome.activated.connect (on_welcome_activated);
104
nextButton.clicked.connect (on_next_clicked);
105
backButton.clicked.connect (on_back_clicked);
90
next_button.clicked.connect (on_next_clicked);
91
back_button.clicked.connect (on_back_clicked);
92
back_button.sensitive = false;
93
update_page_counter_label ();
119
private void on_next_clicked() {
122
if((pageCount - 1) < pageno) {
125
text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
131
private void on_back_clicked() {
137
text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
143
bool twoPages = false;
145
private void update_page() {
147
set_title(booktitle + " " + (totalPageNumber + 1).to_string() + " of " + totalPageCount.to_string());
149
string height = (text_view.get_allocated_height() - 10).to_string();
150
string width = text_view.get_allocated_width().to_string();
152
int offset = text_view.get_allocated_width() * pageno;
155
text_view.execute_script(@"
157
totalHeight = content.offsetHeight;
158
pageCount = Math.floor(totalHeight/$height) + 1;
159
pageCount = pageCount * 2;
160
content.style.width = $width * pageCount / 2;
161
content.style.height = $height;
162
content.style.right = $offset;
163
content.style.WebkitColumnCount = pageCount;
166
text_view.execute_script(@"
168
totalHeight = content.offsetHeight;
169
pageCount = Math.floor(totalHeight/$height) + 1;
170
pageCount = pageCount;
171
content.style.width = $width * pageCount;
172
content.style.height = $height;
173
content.style.right = $offset;
174
content.style.WebkitColumnCount = pageCount;
178
page_counter_label.set_label (pageno.to_string () +
179
"/" + totalPageCount.to_string ());
183
private void webview_status_changed (Object? source, ParamSpec pspec) {
184
int height = (text_view.get_allocated_height() - 10);
185
string strHeight = height.to_string();
186
string width = text_view.get_allocated_width().to_string();
190
text_view.get_preferred_height(out min, out natural);
191
string totalHeight = min.to_string();
193
pageCount = (int) Math.floor(min/height) + 1;
194
string strPageCount = pageCount.to_string();
195
switch (text_view.load_status) {
196
case WebKit.LoadStatus.FINISHED:
198
double animationSpeed = 0.5;
199
if(calculatingTotalPageCount) {
203
var desiredHeight = $height;
204
var desiredWidth = $width;
206
var bodyID = document.getElementsByTagName('body')[0];
208
org_html = bodyID.innerHTML;
209
new_html = \"<div id = 'container'><div id='content'>\" +
210
org_html + '</div></div>';
211
bodyID.innerHTML = new_html;
213
var container = document.getElementsByTagName('div')[0];
214
var content = document.getElementsByTagName('div')[1];
216
totalHeight = $totalHeight;
217
pageCount = Math.floor(totalHeight/desiredHeight) + 1;
218
content.style.padding = 0;
219
content.style.width = desiredWidth * $strPageCount;
220
content.style.height = $strHeight;
221
content.style.WebkitColumnCount = $strPageCount;
222
content.style.right = 0;
223
content.style.position = 'relative';
225
content.style['-webkit-transition-duration'] = '$animationSpeed s';
226
content.style.overflow = 'visible';
229
container.style.height = '100%';
230
container.style.width = '100%';
232
container.style.overflow = 'hidden';";
234
text_view.execute_script(js);
236
if(calculatingTotalPageCount) {
238
pageCountSection.append(pageCount);
239
totalPageCount += pageCount;
241
if(calcProgress < toc.length()) {
242
text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
244
calculatingTotalPageCount = false;
245
text_view.load_html_string(contents[toc.nth_data(0)], "file://");
105
private void on_next_clicked () {
106
ebook_widget.next_page ();
107
check_nav_buttons ();
108
update_page_counter_label ();
111
private void on_back_clicked () {
112
ebook_widget.previous_page ();
113
check_nav_buttons ();
114
update_page_counter_label ();
117
private void check_nav_buttons () {
118
if(ebook_widget.page_counter_available ()) {
119
back_button.sensitive = (ebook_widget.get_current_page () != 0);
120
next_button.sensitive = (ebook_widget.get_current_page ()
121
!= ebook_widget.get_total_pages ());
125
private void update_page_counter_label () {
126
if(ebook_widget.page_counter_available ())
127
page_counter_label.set_label (
128
(ebook_widget.get_current_page ()+1).to_string () +
129
"/" + ebook_widget.get_total_pages ().to_string ());
257
133
* Set and call the file chooser dialog.
270
146
filechooser.destroy ();
272
148
if (filename != "") {
273
open_epub (filename);
279
bool calculatingTotalPageCount = false;
280
int calcProgress = 0;
282
private void calculateTotalPageCount() {
284
calculatingTotalPageCount = true;
286
text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
290
private void open_epub(string filename) {
292
EPub.Context ebook = new EPub.Context(filename, 3);
294
booktitle = ebook.get_metadata(EPub.Metadata.TITLE)[0];
295
set_title(booktitle);
298
this.scrolled_window.show ();
299
this.welcome.hide ();
303
//get table of contents from epub
304
toc = new List<string> ();
306
EPub.TIterator it = new TIterator (ebook, EPub.TIteratorType.NAVMAP, 3);
310
it = new TIterator(ebook, EPub.TIteratorType.GUIDE, 3);
313
while (it != null && it.next () == 1) {
315
toc.append (it.get_curr_link ());
319
//if table of contents turns out to be empty, we generate one
320
bool generatetoc = (toc.length () == 0 );
323
//get contents of epub
324
EPub.Iterator I = new Iterator (ebook, EPub.IteratorType.SPINE, 3);
325
contents = new Gee.HashMap<string, string> ();
331
toc.append (I.get_curr_url());
334
contents.set (I.get_curr_url (), next);
335
next = I.get_next ();
338
while (next != null);
340
//print out table of contents for debugging purposes
341
foreach (string key in toc) {
342
stdout.printf ("keys %s\n", key);
346
calculateTotalPageCount();
348
//load the first set of pages from table of contents
349
text_view.load_html_string (contents[toc.nth_data(0)], "file://");
149
ebook_widget.open_book (filename);
150
ebook_widget.show_all();
153
update_page_counter_label ();