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 <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
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 }
00093
00094
00095
00096 void MainWindow::create()
00097 {
00098
00099 if (main_window)
00100 {
00101 printf("Main window has already been created %p!\n", main_window);
00102 return;
00103 }
00104
00105
00106 main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00107 gtk_window_fullscreen(GTK_WINDOW(main_window));
00108
00109
00110
00111
00112
00113 printf("View window %p create done!\n", main_window);
00114
00115
00116 connect_event_handlers(main_window);
00117
00118
00119 add_document_to_db();
00120
00121
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
00139 void MainWindow::destroy()
00140 {
00141 g_object_unref(main_window);
00142 main_window = 0;
00143 }
00144
00145
00146 void MainWindow::connect_event_handlers(GtkWidget *widget)
00147 {
00148
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
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
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
00209 GdkEventButton * button_event = reinterpret_cast<GdkEventButton *>(event);
00210
00211
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
00249 GdkEventButton * button_event = reinterpret_cast<GdkEventButton *>(event);
00250
00251
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
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
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
00371 goto_prev_page();
00372
00373 break;
00374 case GDK_Down:
00375
00376
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 }