Date: | 2010-01-23 20:43:28 (14 years 2 months ago) |
---|---|
Author: | Mirko Lindner |
Commit: | 9387c0dd58913fda7211152214fc69e884f7da22 |
Message: | add history and "go to top" functions Signed-off-by: Mirko Lindner <mirko@sharism.cc> |
Files: |
src/main_window.cc (2 diffs) src/vido.cc (16 diffs) src/vido.hh (1 diff) |
Change Details
src/main_window.cc | ||
---|---|---|
38 | 38 | |
39 | 39 | bool main_window::actions(int key) |
40 | 40 | { |
41 | static int key_b = 98; | |
42 | static int key_h = 104; | |
41 | 43 | static int key_q = 113; |
42 | 44 | static int key_r = 114; |
43 | 45 | static int key_s = 115; |
46 | static int key_t = 116; | |
44 | 47 | |
45 | 48 | if (key == key_q) |
46 | 49 | { |
... | ... | |
56 | 59 | { |
57 | 60 | log_debug("search"); |
58 | 61 | search_window(this); |
62 | }else if(key == key_t){ | |
63 | GoToTop(); | |
64 | }else if(key == key_h){ | |
65 | show_history(); | |
66 | }else{ | |
67 | log_debug("key pressed: " << key); | |
59 | 68 | } |
60 | 69 | |
61 | 70 | return true; |
src/vido.cc | ||
---|---|---|
24 | 24 | #include <zim/search.h> |
25 | 25 | #include <iostream> |
26 | 26 | #include <string> |
27 | // #include <fstream> | |
27 | #include <vector> | |
28 | ||
28 | 29 | |
29 | 30 | #include "config.h" |
30 | 31 | #include "message_dialog.hh" |
... | ... | |
40 | 41 | #include "gtkhtml/gtkhtml.h" |
41 | 42 | } |
42 | 43 | |
43 | std::string content; | |
44 | // std::string content; | |
44 | 45 | std::string fileName; |
45 | 46 | |
46 | 47 | main_window *window; |
47 | 48 | Gtk::Widget *html; |
48 | 49 | GtkWidget *html_wg; |
50 | std::vector < std::pair< std::string, std::string > > history; | |
51 | ||
49 | 52 | |
50 | 53 | // // // misc functions |
51 | 54 | // get zimFile, if not already opened, open file |
... | ... | |
57 | 60 | log_debug("file not initialized:" << fileName); |
58 | 61 | zimFile = zim::File(fileName); |
59 | 62 | log_debug("number of articles: " << zimFile.getCountArticles()); |
60 | ||
61 | 63 | } |
62 | 64 | |
63 | 65 | log_debug("returning file."); |
... | ... | |
72 | 74 | |
73 | 75 | // fill gtkhtml widget with new content |
74 | 76 | //TODO prepend "top" anchor on html |
75 | void fill_gtkhtml(std::string& html){ | |
77 | void fill_gtkhtml(std::string& html, std::string url, std::string title){ | |
76 | 78 | log_debug("fill gtkhtml called"); |
79 | std::string ccontent; | |
80 | ccontent = "<a name=\"top\"></a>" + html; | |
77 | 81 | gtk_html_flush(GTK_HTML(html_wg)); |
78 | gtk_html_load_from_string(GTK_HTML(html_wg), html.c_str(), -1); | |
82 | gtk_html_load_from_string(GTK_HTML(html_wg), ccontent.c_str(), -1); | |
83 | ||
84 | if ((url != "") && title != ""){ | |
85 | log_debug("adding " << title << " to history"); | |
86 | history.push_back( std::make_pair( url, title ) ); | |
87 | if (history.size() == 11){ | |
88 | history.erase(history.begin()); | |
89 | } | |
90 | ||
91 | } | |
92 | ||
79 | 93 | } |
80 | 94 | |
81 | //UNUSED | |
82 | // // copy article html from while loop into global variable | |
83 | void set_article(const std::string& txt) | |
95 | void show_history() | |
84 | 96 | { |
85 | content = txt; | |
97 | std::string res, url, title; | |
98 | res += "<ul style=\"list-style-type:none;\">"; | |
99 | ||
100 | for(unsigned i=history.size(); i > 0; --i) | |
101 | { | |
102 | ||
103 | #if HAVE_ZIM_QUNICODE_H | |
104 | res += "<li><a href=" + history[i-1].first + ">" + history[i-1].second + "</a></li>"; | |
105 | #else | |
106 | res += "<li><a href='" + history[i-1].first + "'>" + history[i-1].second + "</a></li>"; | |
107 | #endif | |
108 | ||
109 | } | |
110 | res += "</ul>"; | |
111 | url = ""; | |
112 | title = ""; | |
113 | ||
114 | fill_gtkhtml(res, url, title); | |
115 | ||
86 | 116 | } |
87 | 117 | |
118 | //UNUSED | |
119 | // // copy article html from while loop into global variable | |
120 | // void set_article(const std::string& txt) | |
121 | // { | |
122 | // content = txt; | |
123 | // } | |
124 | ||
88 | 125 | // // // externally called functions |
89 | 126 | |
90 | 127 | // // display random article |
... | ... | |
108 | 145 | |
109 | 146 | std::string res = article.getPage(); |
110 | 147 | |
111 | content = article.getPage(); | |
112 | log_debug("article size=" << content.size()); | |
148 | log_debug("article size=" << res.size()); | |
113 | 149 | log_debug("article title=" << article.getTitle()); |
114 | 150 | log_debug("article namespace=" << article.getNamespace()); |
115 | 151 | |
116 | fill_gtkhtml(res); | |
117 | // gtk_html_flush(GTK_HTML(html_wg)); | |
118 | // gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); | |
119 | //log_debug("html \n" << res.c_str()); | |
152 | fill_gtkhtml(res, article.getUrl(), article.getTitle()); | |
120 | 153 | } |
121 | 154 | |
122 | 155 | // // display search dialog |
... | ... | |
125 | 158 | search_dialog(window_x, " "); |
126 | 159 | } |
127 | 160 | |
161 | // // | |
162 | void GoToTop() | |
163 | { | |
164 | log_debug("go to top called"); | |
165 | std::string term = "top"; | |
166 | bool success = gtk_html_jump_to_anchor(GTK_HTML(html_wg), const_cast<char*>(term.c_str())); | |
167 | } | |
168 | ||
128 | 169 | // // // meta functions |
129 | 170 | |
130 | 171 | // // set displayed html to given url |
... | ... | |
138 | 179 | |
139 | 180 | // // convert url to string |
140 | 181 | std::string term(url); |
141 | ||
182 | std::string content; | |
142 | 183 | // // replace '+' signs with spaces in url |
143 | 184 | std::replace(term.begin(), term.end(), '+', ' '); |
144 | 185 | |
... | ... | |
181 | 222 | { |
182 | 223 | content = article.getPage(); |
183 | 224 | |
184 | fill_gtkhtml(content); | |
225 | fill_gtkhtml(content, article.getUrl(), article.getTitle()); | |
185 | 226 | } |
186 | 227 | else |
187 | 228 | { |
... | ... | |
197 | 238 | |
198 | 239 | |
199 | 240 | // // test functions |
200 | void getArticleFromTitle(const gchar *url) | |
241 | void getArticleFromTitle(const gchar *phrase) | |
201 | 242 | { |
202 | 243 | char ns; |
203 | 244 | ns = 'A'; |
... | ... | |
205 | 246 | |
206 | 247 | zim::Search::Results result; |
207 | 248 | zim::Search search(z); |
208 | std::string term(url); | |
249 | std::string term(phrase); | |
209 | 250 | std::string res; |
251 | std::string url; | |
252 | std::string title; | |
210 | 253 | search.setSearchLimit(25); |
211 | 254 | search.find(result, ns, term); |
212 | 255 | if( result.size() == 0) |
... | ... | |
227 | 270 | }while(article.isRedirect()); |
228 | 271 | |
229 | 272 | res = article.getPage(false, 10); |
230 | ||
273 | url = article.getUrl(); | |
274 | title = article.getTitle(); | |
231 | 275 | } |
232 | 276 | else |
233 | 277 | { |
... | ... | |
240 | 284 | res += "<li><a href='" + result[i].getArticle().getUrl() + "'>" + result[i].getArticle().getUrl() + "</a></li>"; |
241 | 285 | #endif |
242 | 286 | } |
287 | url = ""; | |
288 | title = ""; | |
243 | 289 | |
244 | 290 | } |
245 | 291 | |
246 | fill_gtkhtml(res); | |
292 | fill_gtkhtml(res, url, title); | |
247 | 293 | } |
248 | 294 | } |
249 | 295 | |
... | ... | |
275 | 321 | return true; |
276 | 322 | } |
277 | 323 | |
324 | bool scrolled(GtkHTML *cb_html, GtkOrientation orientation, GtkScrollType scroll_type, gfloat position) | |
325 | { | |
326 | log_debug("scrolled"); | |
327 | // TODO on any scroll adjust cursor, maybe with: | |
328 | //static void gtk_html_adjust_cursor_position (GtkHTML *html) | |
329 | } | |
278 | 330 | |
279 | 331 | // // main function |
280 | 332 | int main(int argc, char **argv) |
... | ... | |
301 | 353 | |
302 | 354 | html = Glib::wrap(html_wg); |
303 | 355 | g_signal_connect( G_OBJECT( html_wg ), "link_clicked", G_CALLBACK( on_link_clicked ), NULL ); |
304 | gtk_html_set_caret_mode(GTK_HTML(html_wg),true); | |
305 | ||
356 | g_signal_connect( G_OBJECT( html_wg ), "scroll", G_CALLBACK( scrolled ), NULL ); | |
357 | gtk_html_set_caret_mode(GTK_HTML(html_wg),false); | |
358 | // gtk_html_adjust_cursor_position(GTK_HTML(html_wg)); | |
306 | 359 | static Gtk::ScrolledWindow scrolled_window; |
307 | 360 | scrolled_window.add(*html); |
308 | 361 | scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
... | ... | |
311 | 364 | window.show_all(); |
312 | 365 | |
313 | 366 | show_random(); |
367 | gtk_html_edit_make_cursor_visible(GTK_HTML(html_wg)); | |
314 | 368 | Gtk::Main::run(window); |
315 | 369 | } |
316 | 370 | else |
... | ... | |
326 | 380 | } |
327 | 381 | |
328 | 382 | } |
383 |
src/vido.hh | ||
---|---|---|
26 | 26 | class main_window; |
27 | 27 | |
28 | 28 | extern void show_random(); |
29 | extern void show_history(); | |
29 | 30 | extern void search_window(main_window *window); |
30 | 31 | extern void getArticleFromUrl(const gchar *url); |
31 | 32 | extern void getArticleFromTitle(const gchar *url); |
33 | extern void GoToTop(); | |
32 | 34 | |
33 | 35 | #endif // VIDO_HH |
Branches:
development
master