Date:2010-01-23 20:43:28 (10 years 6 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
3838
3939bool main_window::actions(int key)
4040{
41    static int key_b = 98;
42    static int key_h = 104;
4143    static int key_q = 113;
4244    static int key_r = 114;
4345    static int key_s = 115;
46    static int key_t = 116;
4447
4548    if (key == key_q)
4649    {
...... 
5659    {
5760        log_debug("search");
5861        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);
5968    }
6069
6170    return true;
src/vido.cc
2424#include <zim/search.h>
2525#include <iostream>
2626#include <string>
27// #include <fstream>
27#include <vector>
28
2829
2930#include "config.h"
3031#include "message_dialog.hh"
...... 
4041   #include "gtkhtml/gtkhtml.h"
4142 }
4243
43std::string content;
44// std::string content;
4445std::string fileName;
4546
4647main_window *window;
4748Gtk::Widget *html;
4849GtkWidget *html_wg;
50std::vector < std::pair< std::string, std::string > > history;
51
4952
5053// // // misc functions
5154// get zimFile, if not already opened, open file
...... 
5760    log_debug("file not initialized:" << fileName);
5861    zimFile = zim::File(fileName);
5962    log_debug("number of articles: " << zimFile.getCountArticles());
60
6163  }
6264
6365  log_debug("returning file.");
...... 
7274
7375// fill gtkhtml widget with new content
7476//TODO prepend "top" anchor on html
75void fill_gtkhtml(std::string& html){
77void fill_gtkhtml(std::string& html, std::string url, std::string title){
7678    log_debug("fill gtkhtml called");
79    std::string ccontent;
80    ccontent = "<a name=\"top\"></a>" + html;
7781    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
7993}
8094
81//UNUSED
82// // copy article html from while loop into global variable
83void set_article(const std::string& txt)
95void show_history()
8496{
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
86116}
87117
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
88125// // // externally called functions
89126
90127// // display random article
...... 
108145
109146    std::string res = article.getPage();
110147
111    content = article.getPage();
112    log_debug("article size=" << content.size());
148    log_debug("article size=" << res.size());
113149    log_debug("article title=" << article.getTitle());
114150    log_debug("article namespace=" << article.getNamespace());
115151
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());
120153}
121154
122155// // display search dialog
...... 
125158    search_dialog(window_x, " ");
126159}
127160
161// //
162void 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
128169// // // meta functions
129170
130171// // set displayed html to given url
...... 
138179
139180// // convert url to string
140181    std::string term(url);
141
182    std::string content;
142183// // replace '+' signs with spaces in url
143184    std::replace(term.begin(), term.end(), '+', ' ');
144185
...... 
181222        {
182223            content = article.getPage();
183224
184            fill_gtkhtml(content);
225            fill_gtkhtml(content, article.getUrl(), article.getTitle());
185226        }
186227        else
187228        {
...... 
197238
198239
199240// // test functions
200void getArticleFromTitle(const gchar *url)
241void getArticleFromTitle(const gchar *phrase)
201242{
202243    char ns;
203244    ns = 'A';
...... 
205246
206247    zim::Search::Results result;
207248    zim::Search search(z);
208    std::string term(url);
249    std::string term(phrase);
209250    std::string res;
251    std::string url;
252    std::string title;
210253    search.setSearchLimit(25);
211254    search.find(result, ns, term);
212255    if( result.size() == 0)
...... 
227270        }while(article.isRedirect());
228271
229272        res = article.getPage(false, 10);
230
273        url = article.getUrl();
274        title = article.getTitle();
231275      }
232276      else
233277      {
...... 
240284          res += "<li><a href='" + result[i].getArticle().getUrl() + "'>" + result[i].getArticle().getUrl() + "</a></li>";
241285#endif
242286        }
287        url = "";
288        title = "";
243289
244290      }
245291
246      fill_gtkhtml(res);
292      fill_gtkhtml(res, url, title);
247293    }
248294}
249295
...... 
275321    return true;
276322}
277323
324bool 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}
278330
279331// // main function
280332int main(int argc, char **argv)
...... 
301353
302354      html = Glib::wrap(html_wg);
303355      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));
306359      static Gtk::ScrolledWindow scrolled_window;
307360      scrolled_window.add(*html);
308361      scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
...... 
311364      window.show_all();
312365
313366      show_random();
367      gtk_html_edit_make_cursor_visible(GTK_HTML(html_wg));
314368      Gtk::Main::run(window);
315369    }
316370    else
...... 
326380  }
327381
328382}
383
src/vido.hh
2626class main_window;
2727
2828extern void show_random();
29extern void show_history();
2930extern void search_window(main_window *window);
3031extern void getArticleFromUrl(const gchar *url);
3132extern void getArticleFromTitle(const gchar *url);
33extern void GoToTop();
3234
3335#endif // VIDO_HH

Archive Download the corresponding diff file

Branches:
development
master



interactive