1
0
mirror of git://projects.qi-hardware.com/vido.git synced 2025-01-03 06:40:15 +02:00

add history and "go to top" functions

Signed-off-by: Mirko Lindner <mirko@sharism.cc>
This commit is contained in:
Mirko Lindner 2010-01-23 20:43:28 +01:00
parent 556e12ff0d
commit 9387c0dd58
3 changed files with 89 additions and 23 deletions

View File

@ -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;

View File

@ -24,7 +24,8 @@
#include <zim/search.h>
#include <iostream>
#include <string>
// #include <fstream>
#include <vector>
#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 = "<a name=\"top\"></a>" + 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 += "<ul style=\"list-style-type:none;\">";
for(unsigned i=history.size(); i > 0; --i)
{
#if HAVE_ZIM_QUNICODE_H
res += "<li><a href=" + history[i-1].first + ">" + history[i-1].second + "</a></li>";
#else
res += "<li><a href='" + history[i-1].first + "'>" + history[i-1].second + "</a></li>";
#endif
}
res += "</ul>";
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<char*>(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 += "<li><a href='" + result[i].getArticle().getUrl() + "'>" + result[i].getArticle().getUrl() + "</a></li>";
#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)
}
}

View File

@ -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