main_window.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: main_window.cpp
00003  */
00004 
00005 /*
00006  * This file is part of liberscribble.
00007  *
00008  * liberscribble is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * liberscribble is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026  
00027 #include <gdk/gdkkeysyms.h>
00028 #include <sstream>
00029 
00030 #include "main_window.h"
00031 
00032 namespace test
00033 {
00034 
00035 MainWindow::MainWindow()
00036 : main_window(0)
00037 , source(this)
00038 , erscribble_mgr()
00039 , erscribble_gc()
00040 , cur_page_id()
00041 , page_number(0)
00042 , erscribble_status(-1)
00043 , erase_status(-1)
00044 , erscribble_mode(true)
00045 , target_doc_path("test")
00046 {
00047     create();
00048 
00049     std::stringstream str;
00050     str << page_number;
00051     int num = page_number;
00052     erscribble_mgr.generate_page_id(cur_page_id, str.str(), num);
00053     page_number = num;
00054 
00055     add_document_to_db();
00056 
00057     int len1 = sizeof(unsigned long);
00058     int len2 = sizeof(unsigned short);
00059     int len3 = sizeof(int);
00060     int len4 = sizeof(float);
00061 }
00062 
00063 MainWindow::~MainWindow()
00064 {
00065     gdk_gc_unref(erscribble_gc.gc);
00066     destroy();
00067 
00068     erscribble_mgr.close();
00069     gtk_main_quit();
00070 }
00071 
00072 void MainWindow::add_document_to_db()
00073 {
00074     // 1. parse the folder and file name
00075     //size_t idx = target_doc_path.rfind('\\');
00076     //std::string folder = target_doc_path.substr(0, idx);
00077     //std::string file_name = target_doc_path.substr(idx + 1, target_doc_path.length());
00078 
00079     // 2. open the database
00080     /*erMetadb *metadata_db = ermetadb_new();
00081     GString *folder_str = g_string_new(folder.c_str());
00082     GString *file_name_str = g_string_new(file_name.c_str());
00083     ermetadb_open_database(metadata_db, folder_str);
00084 
00085     ermetadb_add_document(metadata_db, file_name_str, 10, 1000);
00086 
00087     ermetadb_close_database(metadata_db);
00088     ermetadb_free(metadata_db);
00089 
00090     g_string_free(folder_str, TRUE);
00091     g_string_free(file_name_str, TRUE);*/
00092 }
00093 
00094 /// Create the main widget. The main widget will be 
00095 /// used as container for all ViewCtrl and the sub views.
00096 void MainWindow::create()
00097 {
00098     // check
00099     if (main_window)
00100     {
00101         printf("Main window has already been created %p!\n", main_window);
00102         return;
00103     }
00104 
00105     // create main window
00106     main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00107     gtk_window_fullscreen(GTK_WINDOW(main_window));
00108 
00109     // initialize the display
00110     //Display * display = gdk_x11_display_get_xdisplay(gdk_display_get_default());
00111     //GlobalComponents::instance().set_display(display);
00112 
00113     printf("View window %p create done!\n", main_window);
00114 
00115     // install event handlers
00116     connect_event_handlers(main_window);
00117 
00118     // add document into database if it does not exist
00119     add_document_to_db();
00120     // initialize the scribble manager
00121     // scribble manager cannot be used when open fails.
00122     erscribble_mgr.open(target_doc_path.c_str());
00123 
00124 }
00125 
00126 void MainWindow::show(bool show)
00127 {
00128     if (show)
00129     {
00130         gtk_widget_show(main_window);
00131     }
00132     else
00133     {
00134         gtk_widget_hide(main_window);
00135     }
00136 }
00137 
00138 /// Destroy the main widget and related resouce.
00139 void MainWindow::destroy()
00140 {
00141     g_object_unref(main_window);
00142     main_window = 0;
00143 }
00144 
00145     /// @brief Enable main window to handle events.
00146 void MainWindow::connect_event_handlers(GtkWidget *widget)
00147 {
00148     /// Install event handlers by using member functions.
00149     source.connect_event_handlers(widget);
00150     source.set_event_handler(EventSource::EVENT_EXPOSE,
00151                              &MainWindow::on_expose);
00152     source.set_event_handler(EventSource::EVENT_CONFIG,
00153                              &MainWindow::on_configure);
00154     source.set_event_handler(EventSource::EVENT_BUTTON_PRESS,
00155                              &MainWindow::on_button_press);
00156     source.set_event_handler(EventSource::EVENT_BUTTON_RELEASE,
00157                              &MainWindow::on_button_release);
00158     source.set_event_handler(EventSource::EVENT_MOTION_NOTIFY,
00159                              &MainWindow::on_motion_notify);
00160     source.set_event_handler(EventSource::EVENT_KEY_PRESS,
00161                              &MainWindow::on_key_press);
00162     source.set_event_handler(EventSource::EVENT_KEY_RELEASE,
00163                              &MainWindow::on_key_release);
00164     source.set_event_handler(EventSource::EVENT_DELETE,
00165                              &MainWindow::on_delete);
00166 }
00167 
00168 void MainWindow::update()
00169 {
00170     // clear background
00171     GdkColor white = {0, 0xffff, 0xffff, 0xffff};
00172     gdk_gc_set_rgb_fg_color(erscribble_gc.gc, &white);
00173     gdk_draw_rectangle(main_window->window,
00174                        erscribble_gc.gc,
00175                        TRUE,
00176                        0, 0, 
00177                        main_window->allocation.width,
00178                        main_window->allocation.height);
00179     draw_scribble_page();
00180 }
00181 
00182 gboolean MainWindow::on_expose(GtkWidget *widget, 
00183                                GdkEvent *event) 
00184 {
00185     update();
00186     return TRUE;
00187 }
00188 
00189 gboolean MainWindow::on_configure(GtkWidget *widget, 
00190                                GdkEvent *event) 
00191 {
00192     // initialize the scribble gc
00193     if (erscribble_gc.gc)
00194     {
00195         gdk_gc_unref(erscribble_gc.gc);
00196     }
00197 
00198     erscribble_gc.widget = main_window;
00199     erscribble_gc.gc = gdk_gc_new(main_window->window);
00200     erscribble_gc.realZoom = 100.0f;
00201     erscribble_gc.rotation = 0;
00202     return TRUE;
00203 }
00204 
00205 gboolean MainWindow::on_button_press(GtkWidget *widget, 
00206                                      GdkEvent *event)
00207 {
00208     // Record the position inside the widget.
00209     GdkEventButton * button_event = reinterpret_cast<GdkEventButton *>(event);
00210 
00211     // Usually, on the device, only the left button.
00212     switch (button_event->button)
00213     {
00214     case 1:
00215         {
00216             ScbPoint pos;
00217             pos.x = static_cast<int>(((GdkEventButton*)event)->x);
00218             pos.y = static_cast<int>(((GdkEventButton*)event)->y);
00219 
00220             if (erscribble_mode)
00221             {
00222                 if (erscribble_status < 0)
00223                 {
00224                     erscribble_status = 0;
00225                     scribble(pos, erscribble_status);
00226                 }
00227             }
00228             else
00229             {
00230                 if (erase_status < 0)
00231                 {
00232                     erase_status = 0;
00233                     erase(pos, erase_status);
00234                 }
00235             }
00236         }
00237         break;
00238     default:
00239         break;
00240     }
00241 
00242     return TRUE;
00243 }
00244 
00245 gboolean MainWindow::on_button_release(GtkWidget *widget, 
00246                                        GdkEvent *event) 
00247 {
00248     // Record the position inside the widget.
00249     GdkEventButton * button_event = reinterpret_cast<GdkEventButton *>(event);
00250 
00251     // Usually, on the device, only the left button.
00252     switch (button_event->button)
00253     {
00254     case 1:
00255         {
00256             ScbPoint pos;
00257             pos.x = static_cast<int>(((GdkEventButton*)event)->x);
00258             pos.y = static_cast<int>(((GdkEventButton*)event)->y);
00259 
00260             if (erscribble_mode)
00261             {
00262                 if (erscribble_status >= 0)
00263                 {
00264                     erscribble_status = 2;
00265                     scribble(pos, erscribble_status);
00266 
00267                     // reset the status
00268                     erscribble_status = -1;
00269                 }
00270             }
00271             else
00272             {
00273                 if (erase_status >= 0)
00274                 {
00275                     erase_status = 2;
00276                     erase(pos, erase_status);
00277 
00278                     // reset the status
00279                     erase_status = -1;
00280                 }
00281             }
00282         }
00283         break;
00284     default:
00285         break;
00286     }
00287 
00288     return TRUE;
00289 }
00290 
00291 gboolean MainWindow::on_motion_notify(GtkWidget *widget, 
00292                                       GdkEvent *event)
00293 {
00294     ScbPoint pos;
00295     pos.x = static_cast<int>(((GdkEventButton*)event)->x);
00296     pos.y = static_cast<int>(((GdkEventButton*)event)->y);
00297 
00298     if (erscribble_mode)
00299     {
00300         if (erscribble_status == 0)
00301         {
00302             erscribble_status = 1;
00303         }
00304 
00305         if (erscribble_status == 1)
00306         {
00307             scribble(pos, erscribble_status);
00308         }
00309     }
00310     else
00311     {
00312         if (erase_status == 0)
00313         {
00314             erase_status = 1;
00315         }
00316 
00317         if (erase_status == 1)
00318         {
00319             erase(pos, erase_status);
00320         }
00321     }
00322     return TRUE;
00323 }
00324 
00325 gboolean MainWindow::on_key_press(GtkWidget *widget,
00326                                 GdkEvent *event)
00327 {
00328     return FALSE;
00329 }
00330 
00331 void MainWindow::goto_prev_page()
00332 {
00333     erscribble_mgr.save();
00334 
00335     page_number --;
00336     int num = page_number;
00337     std::stringstream str;
00338     str << page_number;
00339     erscribble_mgr.generate_page_id(cur_page_id, str.str(), num);
00340     page_number = num;
00341     update();
00342     show(0);
00343     show(1);
00344 }
00345 
00346 void MainWindow::goto_next_page()
00347 {
00348     erscribble_mgr.save();
00349 
00350     page_number ++;
00351     int num = page_number;
00352     std::stringstream str;
00353     str << page_number;
00354     erscribble_mgr.generate_page_id(cur_page_id, str.str(), num);
00355     page_number = num;
00356     update();
00357     show(0);
00358     show(1);
00359 }
00360 
00361 gboolean MainWindow::on_key_release(GtkWidget *widget,
00362                                   GdkEvent *event)
00363 {
00364     guint key_code = ((GdkEventKey*)event)->keyval;
00365 
00366     switch(key_code)
00367     {
00368     case GDK_Up:
00369         
00370         //Turn to the previous page
00371         goto_prev_page();
00372 
00373         break;
00374     case GDK_Down:
00375         
00376         //Turn to the next page
00377         goto_next_page();
00378 
00379         break;
00380     
00381     case GDK_Left:
00382     case GDK_Right:
00383 
00384         erscribble_mode = !erscribble_mode;
00385 
00386         break;
00387 
00388     case GDK_Return:
00389         erscribble_mgr.clear_page(cur_page_id);
00390         show(0);
00391         show(1);
00392         break;
00393 
00394     default:
00395         break;
00396     }
00397     return FALSE;
00398 }
00399 
00400 gboolean MainWindow::on_delete(GtkWidget *widget,
00401                               GdkEvent *event)
00402 {
00403     return FALSE;
00404 }
00405 
00406 void MainWindow::draw_scribble_page()
00407 {
00408     erscribble_mgr.draw_scribble_page(cur_page_id, erscribble_gc, 0, 0);
00409 }
00410 
00411 void MainWindow::scribble(const ScbPoint &pos, int status)
00412 {
00413     
00414     switch(status)
00415     {
00416     case 0:
00417         erscribble_mgr.on_scribble_begin(cur_page_id
00418             , erscribble_gc
00419             , pos.x
00420             , pos.y
00421             , pos.x
00422             , pos.y
00423             , 0
00424             , 0);
00425 
00426         break;
00427     case 1:
00428         erscribble_mgr.on_scribble_move(cur_page_id
00429             , erscribble_gc
00430             , pos.x
00431             , pos.y
00432             , pos.x
00433             , pos.y
00434             , 0
00435             , 0);
00436 
00437         break;
00438     case 2:
00439         erscribble_mgr.on_scribble_end(cur_page_id
00440             , erscribble_gc
00441             , pos.x
00442             , pos.y
00443             , pos.x
00444             , pos.y
00445             , 0
00446             , 0);
00447 
00448         break;
00449     default:
00450         break;
00451     }
00452 }
00453 
00454 void MainWindow::erase(const ScbPoint &pos, int status)
00455 {
00456     switch(status)
00457     {
00458     case 0:
00459         erscribble_mgr.on_erase_begin(cur_page_id
00460             , erscribble_gc
00461             , pos.x
00462             , pos.y
00463             , pos.x
00464             , pos.y
00465             , 0
00466             , 0);
00467 
00468         break;
00469     case 1:
00470         erscribble_mgr.on_erase_move(cur_page_id
00471             , erscribble_gc
00472             , pos.x
00473             , pos.y
00474             , pos.x
00475             , pos.y
00476             , 0
00477             , 0);
00478 
00479         break;
00480     case 2:
00481         erscribble_mgr.on_erase_end(cur_page_id
00482             , erscribble_gc
00483             , pos.x
00484             , pos.y
00485             , pos.x
00486             , pos.y
00487             , 0
00488             , 0);
00489 
00490         break;
00491     default:
00492         break;
00493     }
00494 }
00495 
00496 
00497 } //namespace uds
Generated by  doxygen 1.6.2-20100208