From 9387c0dd58913fda7211152214fc69e884f7da22 Mon Sep 17 00:00:00 2001 From: Mirko Lindner Date: Sat, 23 Jan 2010 20:43:28 +0100 Subject: [PATCH] add history and "go to top" functions Signed-off-by: Mirko Lindner --- src/main_window.cc | 9 ++++ src/vido.cc | 101 ++++++++++++++++++++++++++++++++++----------- src/vido.hh | 2 + 3 files changed, 89 insertions(+), 23 deletions(-) diff --git a/src/main_window.cc b/src/main_window.cc index 2131a2b..4d77dda 100644 --- a/src/main_window.cc +++ b/src/main_window.cc @@ -38,9 +38,12 @@ main_window::main_window() bool main_window::actions(int key) { + static int key_b = 98; + static int key_h = 104; static int key_q = 113; static int key_r = 114; static int key_s = 115; + static int key_t = 116; if (key == key_q) { @@ -56,6 +59,12 @@ bool main_window::actions(int key) { log_debug("search"); search_window(this); + }else if(key == key_t){ + GoToTop(); + }else if(key == key_h){ + show_history(); + }else{ + log_debug("key pressed: " << key); } return true; diff --git a/src/vido.cc b/src/vido.cc index e6f8299..6ff2e1a 100644 --- a/src/vido.cc +++ b/src/vido.cc @@ -24,7 +24,8 @@ #include #include #include -// #include +#include + #include "config.h" #include "message_dialog.hh" @@ -40,12 +41,14 @@ extern "C" { #include "gtkhtml/gtkhtml.h" } -std::string content; +// std::string content; std::string fileName; main_window *window; Gtk::Widget *html; GtkWidget *html_wg; +std::vector < std::pair< std::string, std::string > > history; + // // // misc functions // get zimFile, if not already opened, open file @@ -57,7 +60,6 @@ const zim::File& get_file() log_debug("file not initialized:" << fileName); zimFile = zim::File(fileName); log_debug("number of articles: " << zimFile.getCountArticles()); - } log_debug("returning file."); @@ -72,18 +74,53 @@ void show_message(std::string title, std::string txt) // fill gtkhtml widget with new content //TODO prepend "top" anchor on html -void fill_gtkhtml(std::string& html){ +void fill_gtkhtml(std::string& html, std::string url, std::string title){ log_debug("fill gtkhtml called"); + std::string ccontent; + ccontent = "" + html; gtk_html_flush(GTK_HTML(html_wg)); - gtk_html_load_from_string(GTK_HTML(html_wg), html.c_str(), -1); + gtk_html_load_from_string(GTK_HTML(html_wg), ccontent.c_str(), -1); + + if ((url != "") && title != ""){ + log_debug("adding " << title << " to history"); + history.push_back( std::make_pair( url, title ) ); + if (history.size() == 11){ + history.erase(history.begin()); + } + + } + +} + +void show_history() +{ + std::string res, url, title; + res += ""; + url = ""; + title = ""; + + fill_gtkhtml(res, url, title); + } //UNUSED // // copy article html from while loop into global variable -void set_article(const std::string& txt) -{ - content = txt; -} +// void set_article(const std::string& txt) +// { +// content = txt; +// } // // // externally called functions @@ -108,15 +145,11 @@ void show_random() std::string res = article.getPage(); - content = article.getPage(); - log_debug("article size=" << content.size()); + log_debug("article size=" << res.size()); log_debug("article title=" << article.getTitle()); log_debug("article namespace=" << article.getNamespace()); - fill_gtkhtml(res); -// gtk_html_flush(GTK_HTML(html_wg)); -// gtk_html_load_from_string(GTK_HTML(html_wg), res.c_str(), -1); - //log_debug("html \n" << res.c_str()); + fill_gtkhtml(res, article.getUrl(), article.getTitle()); } // // display search dialog @@ -125,6 +158,14 @@ void search_window(main_window *window_x) search_dialog(window_x, " "); } +// // +void GoToTop() +{ + log_debug("go to top called"); + std::string term = "top"; + bool success = gtk_html_jump_to_anchor(GTK_HTML(html_wg), const_cast(term.c_str())); +} + // // // meta functions // // set displayed html to given url @@ -138,7 +179,7 @@ void getArticleFromUrl(const gchar *url) // // convert url to string std::string term(url); - + std::string content; // // replace '+' signs with spaces in url std::replace(term.begin(), term.end(), '+', ' '); @@ -181,7 +222,7 @@ void getArticleFromUrl(const gchar *url) { content = article.getPage(); - fill_gtkhtml(content); + fill_gtkhtml(content, article.getUrl(), article.getTitle()); } else { @@ -197,7 +238,7 @@ void getArticleFromUrl(const gchar *url) // // test functions -void getArticleFromTitle(const gchar *url) +void getArticleFromTitle(const gchar *phrase) { char ns; ns = 'A'; @@ -205,8 +246,10 @@ void getArticleFromTitle(const gchar *url) zim::Search::Results result; zim::Search search(z); - std::string term(url); + std::string term(phrase); std::string res; + std::string url; + std::string title; search.setSearchLimit(25); search.find(result, ns, term); if( result.size() == 0) @@ -227,7 +270,8 @@ void getArticleFromTitle(const gchar *url) }while(article.isRedirect()); res = article.getPage(false, 10); - + url = article.getUrl(); + title = article.getTitle(); } else { @@ -240,10 +284,12 @@ void getArticleFromTitle(const gchar *url) res += "
  • " + result[i].getArticle().getUrl() + "
  • "; #endif } + url = ""; + title = ""; } - fill_gtkhtml(res); + fill_gtkhtml(res, url, title); } } @@ -275,6 +321,12 @@ bool on_link_clicked(GtkHTML *html, const gchar *url) return true; } +bool scrolled(GtkHTML *cb_html, GtkOrientation orientation, GtkScrollType scroll_type, gfloat position) +{ + log_debug("scrolled"); + // TODO on any scroll adjust cursor, maybe with: + //static void gtk_html_adjust_cursor_position (GtkHTML *html) +} // // main function int main(int argc, char **argv) @@ -301,8 +353,9 @@ int main(int argc, char **argv) html = Glib::wrap(html_wg); g_signal_connect( G_OBJECT( html_wg ), "link_clicked", G_CALLBACK( on_link_clicked ), NULL ); - gtk_html_set_caret_mode(GTK_HTML(html_wg),true); - + g_signal_connect( G_OBJECT( html_wg ), "scroll", G_CALLBACK( scrolled ), NULL ); + gtk_html_set_caret_mode(GTK_HTML(html_wg),false); +// gtk_html_adjust_cursor_position(GTK_HTML(html_wg)); static Gtk::ScrolledWindow scrolled_window; scrolled_window.add(*html); scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); @@ -311,6 +364,7 @@ int main(int argc, char **argv) window.show_all(); show_random(); + gtk_html_edit_make_cursor_visible(GTK_HTML(html_wg)); Gtk::Main::run(window); } else @@ -326,3 +380,4 @@ int main(int argc, char **argv) } } + diff --git a/src/vido.hh b/src/vido.hh index 3c2cca7..33adfe3 100644 --- a/src/vido.hh +++ b/src/vido.hh @@ -26,8 +26,10 @@ class main_window; extern void show_random(); +extern void show_history(); extern void search_window(main_window *window); extern void getArticleFromUrl(const gchar *url); extern void getArticleFromTitle(const gchar *url); +extern void GoToTop(); #endif // VIDO_HH