test_main_window.cpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #include <gdk/gdkkeysyms.h>
00028 #include "plugin_type.h"
00029 
00030 #include "string_impl.h"
00031 #include "test_main_window.h"
00032 
00033 namespace test
00034 {
00035 
00036 MainWindow::MainWindow()
00037 : main_window(0)
00038 , source(this)
00039 , gc(0)
00040 , pixmap(0)
00041 , output_dev()
00042 , current_page_data(0)
00043 , search_results()
00044 {
00045     create();
00046 }
00047 
00048 MainWindow::~MainWindow()
00049 {
00050 }
00051 
00052 void MainWindow::create()
00053 {
00054     
00055     if (main_window)
00056     {
00057         return;
00058     }
00059 
00060     
00061     main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00062 
00063     gtk_widget_set_size_request(main_window, 1024, 768);
00064 
00065     
00066     connect_event_handlers(main_window);
00067 
00068     gtk_widget_show(main_window);
00069 
00070     update_window();
00071 
00072     open_document("G:\\std.pdf");
00073 }
00074 
00075 void MainWindow::destory()
00076 {
00077     g_object_unref(main_window);
00078     main_window = 0;
00079 }
00080 
00081 void MainWindow::connect_event_handlers(GtkWidget *widget)
00082 {
00083 
00084     source.connect_event_handlers(widget);
00085     
00086     source.set_event_handler(EventSource::EVENT_EXPOSE,
00087                              &MainWindow::on_expose);
00088 
00089     source.set_event_handler(EventSource::EVENT_CONFIG,
00090                              &MainWindow::on_configure);
00091     
00092     source.set_event_handler(EventSource::EVENT_BUTTON_PRESS,
00093                              &MainWindow::on_button_press);
00094 
00095     source.set_event_handler(EventSource::EVENT_BUTTON_RELEASE,
00096                              &MainWindow::on_button_release);
00097 
00098     source.set_event_handler(EventSource::EVENT_MOTION_NOTIFY,
00099                              &MainWindow::on_motion_notify);
00100     
00101     source.set_event_handler(EventSource::EVENT_KEY_PRESS,
00102                              &MainWindow::on_key_press);
00103     
00104     source.set_event_handler(EventSource::EVENT_KEY_RELEASE,
00105                              &MainWindow::on_key_release);
00106     
00107     source.set_event_handler(EventSource::EVENT_DELETE,
00108                              &MainWindow::on_delete);
00109 
00110 }
00111 
00112 gboolean MainWindow::on_expose(GtkWidget *widget, 
00113                                GdkEvent *event) 
00114 {
00115     draw();
00116 
00117     return TRUE;
00118 }
00119 
00120 void MainWindow::update_output_device()
00121 {
00122 
00123     OutputDeviceContext output_dev_ctx;
00124 
00125     output_dev_ctx.widget = main_window;
00126     output_dev_ctx.gc = gc;
00127 
00128     
00129     output_dev.map(output_dev_ctx);
00130 }
00131 
00132 void MainWindow::update_window()
00133 {
00134     if (pixmap)
00135     {
00136         g_object_unref(pixmap);
00137     }
00138 
00139     if (gc)
00140     {
00141         g_object_unref(gc);
00142     }
00143 
00144     
00145     pixmap = gdk_pixmap_new(main_window->window,
00146                             main_window->allocation.width,
00147                             main_window->allocation.height, -1);
00148 
00149     
00150     gc = gdk_gc_new(pixmap);
00151 
00152     
00153     update_output_device();
00154 }
00155 
00156 gboolean MainWindow::on_configure(GtkWidget *widget, 
00157                                GdkEvent *event) 
00158 {
00159     update_window();
00160 
00161     return TRUE;
00162 }
00163 
00164 void MainWindow::open_document(const std::string &filename)
00165 {
00166     
00167 
00168     document.open(filename);
00169 
00170     current_page = document.get_cur_page_num();
00171 
00172     document.sig_page_ready.add_slot(this, &MainWindow::handle_page_ready);
00173 
00174     document.sig_search_results_ready.add_slot(this
00175         , &MainWindow::handle_searching_done);
00176     
00177     document.get_cur_page_data();
00178     
00179 }
00180 
00181 void MainWindow::search_next(bool forward)
00182 {
00183     if (search_results.b_searching)
00184     {
00185         return;
00186     }
00187 
00188     std::string dst_str = "from where you found the exception. In this regard, exception handling is completely different from signal handling. Exception objects are ordinary objects that are described in ordinary classes or ordinary fundamental types. Thus, you can use ints, strings, or template classes that";
00189     PDFSearchCriteria criteria;
00190     criteria.text = dst_str;
00191     criteria.forward = forward;
00192     criteria.match_whole_word = false;
00193     criteria.case_sensitive = true;
00194 
00195     static unsigned int id = 0;
00196 
00197     search_results.ref_id = id;
00198     search_results.b_forward = forward;
00199 
00200     if (search_results.cur_pos.empty())
00201     {
00202         document.get_anchor_of_page(current_page, search_results.cur_pos);
00203     }
00204     
00205     if (document.search_next(criteria, search_results.cur_pos, id++))
00206     {
00207         search_results.b_searching = true;
00208     }
00209 
00210 }
00211 
00212 void MainWindow::search_all()
00213 {
00214 }
00215 
00216 void MainWindow::handle_searching_done(SearchResult ret, PDFSearchCollection* results
00217         , unsigned int ref_id)
00218 {
00219     if (ref_id != search_results.ref_id)
00220     {
00221         delete results;
00222         return;
00223     }
00224 
00225     
00226     search_results.b_searching = false;
00227 
00228     
00229     if (ret != RES_OK)
00230     {
00231         return;
00232     }
00233 
00234     
00235     delete search_results.data;
00236     search_results.data = results;
00237 
00238     
00239     PluginRange *first_result = results->front();
00240 
00241     if (search_results.b_forward)
00242     {
00243         search_results.cur_pos = 
00244             static_cast<StringImpl*>(first_result->end_anchor)->stl_string();
00245     }
00246     else
00247     {
00248         search_results.cur_pos = 
00249             static_cast<StringImpl*>(first_result->start_anchor)->stl_string();
00250     }
00251     
00252     
00253     
00254     
00255     document.get_dst_page_data(search_results.cur_pos);
00256 }
00257 
00258 void MainWindow::draw_page_bitmap(const PagePtr page)
00259 {
00260     
00261     output_dev.clear_background();
00262 
00263     output_dev.set_color_depth(RGB_COLOR_DEPTH);
00264     
00265     output_dev.draw_image(page->get_bitmap()->getDataPtr()
00266         , page->get_bitmap()->getWidth()
00267         , page->get_bitmap()->getHeight()
00268         , page->get_bitmap()->getRowSize());
00269 
00270     
00271     page->unlock();
00272 }
00273 
00274 void MainWindow::draw_search_results()
00275 {
00276     
00277     
00278     PDFSearchCollection *cur_search_doc = search_results.data;
00279     if (!cur_search_doc)
00280     {
00281         return;
00282     }
00283 
00284     
00285     
00286     
00287     
00288     
00289     
00290     
00291     
00292     
00293     
00294     
00295 
00296     
00297     
00298     
00299     
00300     
00301     
00302     
00303 
00304     
00305     
00306     
00307     
00308     
00309     
00310     
00311     
00312     
00313     
00314     
00315     
00316     
00317     
00318     
00319     
00320     
00321     
00322     
00323 }
00324 
00325 void MainWindow::handle_page_ready(PagePtr cur_page, unsigned int ref_id)
00326 {
00327     current_page = cur_page->get_page_num();
00328 
00329     current_page_data = cur_page;
00330 
00331     draw_page_bitmap(current_page_data);
00332 
00333     draw_search_results();
00334 }
00335 
00336 void MainWindow::draw()
00337 {
00338     if (current_page_data)
00339     {
00340         draw_page_bitmap(current_page_data);
00341     }
00342 
00343     draw_search_results();
00344 }
00345 
00346 gboolean MainWindow::on_button_press(GtkWidget *widget, 
00347                                      GdkEvent *event)
00348 {
00349     return FALSE;
00350 }
00351 
00352 gboolean MainWindow::on_button_release(GtkWidget *widget, 
00353                                        GdkEvent *event) 
00354 {
00355     int dst_num = current_page + 1;
00356     document.get_dst_page_data(dst_num);
00357     return TRUE;
00358 }
00359 
00360 gboolean MainWindow::on_motion_notify(GtkWidget *widget, 
00361                                       GdkEvent *event)
00362 {
00363     return FALSE;
00364 }
00365 
00366 gboolean MainWindow::on_key_press(GtkWidget *widget,
00367                                 GdkEvent *event)
00368 {
00369     return FALSE;
00370 }
00371 
00372 gboolean MainWindow::on_key_release(GtkWidget *widget,
00373                                   GdkEvent *event)
00374 {
00375     guint key_code = ((GdkEventKey*)event)->keyval;
00376 
00377     switch(key_code)
00378     {
00379     
00380     case GDK_F1:
00381         search_next();
00382 
00383         break;
00384     case GDK_F2:
00385         search_next(false);
00386 
00387         break;
00388     default:
00389 
00390         break;
00391     }
00392 
00393     return TRUE;
00394 }
00395 
00396 gboolean MainWindow::on_delete(GtkWidget *widget,
00397                               GdkEvent *event)
00398 {
00399     return FALSE;
00400 }
00401 
00402 } 
00403