7
class EPubWidget : EBookWidget {
9
private WebKit.WebView text_view;
10
private ScrolledWindow scrolled_window;
11
private Gee.HashMap<string, string> contents;
12
private List<string> toc;
13
private int pageCount = 0;
14
private string booktitle;
15
private int pageno = 0;
17
private int indexNum = 0;
18
private List<int> pageCountSection;
19
private int totalPageCount;
20
private int totalPageNumber;
22
private bool twoPages = false;
23
private bool calculatingTotalPageCount = false;
24
private int calcProgress = 0;
28
text_view = new WebKit.WebView ();
29
text_view.expand = true;
30
scrolled_window = new ScrolledWindow (null, null);
31
//scrolled_window.set_policy (PolicyType.NEVER, PolicyType.NEVER);
32
scrolled_window.add (this.text_view);
33
scrolled_window.expand = true;
34
//h_scrollBar = (Scrollbar) scrolled_window.get_hscrollbar();
35
text_view.notify["load-status"].connect (webview_status_changed);
36
attach(scrolled_window,0,0,1,1);
39
public override int get_current_page () {
43
public override int get_total_pages () {
44
return totalPageCount;
47
public override bool page_counter_available () {
51
public override void next_page () {
54
if((pageCount - 1) < pageno) {
57
text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
63
public override void previous_page () {
69
text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
75
private void update_page() {
77
//set_title(booktitle + " " + (totalPageNumber + 1).to_string() + " of " + totalPageCount.to_string());
79
string height = (text_view.get_allocated_height() - 10).to_string();
80
string width = text_view.get_allocated_width().to_string();
82
int offset = text_view.get_allocated_width() * pageno;
85
text_view.execute_script(@"
87
totalHeight = content.offsetHeight;
88
pageCount = Math.floor(totalHeight/$height) + 1;
89
pageCount = pageCount * 2;
90
content.style.width = $width * pageCount / 2;
91
content.style.height = $height;
92
content.style.right = $offset;
93
content.style.WebkitColumnCount = pageCount;
96
text_view.execute_script(@"
98
totalHeight = content.offsetHeight;
99
pageCount = Math.floor(totalHeight/$height) + 1;
100
pageCount = pageCount;
101
content.style.width = $width * pageCount;
102
content.style.height = $height;
103
content.style.right = $offset;
104
content.style.WebkitColumnCount = pageCount;
110
private void webview_status_changed (Object? source, ParamSpec pspec) {
111
int height = (text_view.get_allocated_height() - 10);
112
string strHeight = height.to_string();
113
string width = text_view.get_allocated_width().to_string();
117
text_view.get_preferred_height(out min, out natural);
118
string totalHeight = min.to_string();
120
pageCount = (int) Math.floor(min/height) + 1;
121
string strPageCount = pageCount.to_string();
122
switch (text_view.load_status) {
123
case WebKit.LoadStatus.FINISHED:
125
double animationSpeed = 0.5;
126
if(calculatingTotalPageCount) {
130
var desiredHeight = $height;
131
var desiredWidth = $width;
133
var bodyID = document.getElementsByTagName('body')[0];
135
org_html = bodyID.innerHTML;
136
new_html = \"<div id = 'container'><div id='content'>\" +
137
org_html + '</div></div>';
138
bodyID.innerHTML = new_html;
140
var container = document.getElementsByTagName('div')[0];
141
var content = document.getElementsByTagName('div')[1];
143
totalHeight = $totalHeight;
144
pageCount = Math.floor(totalHeight/desiredHeight) + 1;
145
content.style.padding = 0;
146
content.style.width = desiredWidth * $strPageCount;
147
content.style.height = $strHeight;
148
content.style.WebkitColumnCount = $strPageCount;
149
content.style.right = 0;
150
content.style.position = 'relative';
152
content.style['-webkit-transition-duration'] = '$animationSpeed s';
153
content.style.overflow = 'visible';
156
container.style.height = '100%';
157
container.style.width = '100%';
159
container.style.overflow = 'hidden';";
161
text_view.execute_script(js);
163
if(calculatingTotalPageCount) {
165
pageCountSection.append(pageCount);
166
totalPageCount += pageCount;
168
if(calcProgress < toc.length()) {
169
text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
171
calculatingTotalPageCount = false;
172
text_view.load_html_string(contents[toc.nth_data(0)], "file://");
181
private void calculateTotalPageCount() {
183
calculatingTotalPageCount = true;
185
text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
189
public override void open_book(string filename) {
190
EPub.Context ebook = new EPub.Context(filename, 3);
192
booktitle = ebook.get_metadata(EPub.Metadata.TITLE)[0];
193
//set_title(booktitle);
196
this.scrolled_window.show ();
199
//get table of contents from epub
200
toc = new List<string> ();
202
EPub.TIterator it = new TIterator (ebook, EPub.TIteratorType.NAVMAP, 3);
206
it = new TIterator(ebook, EPub.TIteratorType.GUIDE, 3);
209
while (it != null && it.next () == 1) {
211
toc.append (it.get_curr_link ());
215
//if table of contents turns out to be empty, we generate one
216
bool generatetoc = (toc.length () == 0 );
219
//get contents of epub
220
EPub.Iterator I = new Iterator (ebook, EPub.IteratorType.SPINE, 3);
221
contents = new Gee.HashMap<string, string> ();
226
toc.append (I.get_curr_url());
229
contents.set (I.get_curr_url (), next);
230
next = I.get_next ();
232
} while (next != null);
234
//print out table of contents for debugging purposes
235
foreach (string key in toc) {
236
stdout.printf ("keys %s\n", key);
239
calculateTotalPageCount();
241
//load the first set of pages from table of contents
242
text_view.load_html_string (contents[toc.nth_data(0)], "file://");
245
public override string get_title () {