multi_thread_test.cpp File Reference

#include <stdio.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "text_config.h"
#include "text_model.h"
#include "text_view.h"
Include dependency graph for multi_thread_test.cpp:

Go to the source code of this file.

Data Structures

struct  RenderData
struct  SearchResult
struct  Event
class  EventQueue
class  EventHandler

Functions

void fill_pix_buf (const unsigned char *bmp, const int width, const int height)
void handle_event (const Event &ev)
static gboolean widget_event_handler (GtkWidget *widget, GdkEvent *event, gpointer user_data)
gboolean timeout_handler (gpointer data)
static gboolean my_expose (GtkWidget *da, GdkEventExpose *event, gpointer data)
static void destroy (GtkWidget *widget, gpointer data)
int main (int argc, char *argv[])

Variables

GdkPixbuf * pix_buf = NULL
GtkWidget * top_level_window = NULL
static int page_index = 0
static int total_pages = 0

Function Documentation

static void destroy ( GtkWidget *  widget,
gpointer  data 
) [static]

Definition at line 315 of file multi_thread_test.cpp.

Referenced by main().

00316 {
00317     gtk_main_quit ();
00318 }

Here is the caller graph for this function:

void fill_pix_buf ( const unsigned char *  bmp,
const int  width,
const int  height 
)

Definition at line 166 of file multi_thread_test.cpp.

References pix_buf.

Referenced by handle_event(), main(), and widget_event_handler().

00167 {
00168     if (pix_buf != NULL)
00169     {
00170         g_object_unref(pix_buf);
00171         pix_buf = NULL;
00172     }
00173 
00174     pix_buf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
00175         FALSE,
00176         8,
00177         width,
00178         height);
00179 
00180     guchar *pixels = gdk_pixbuf_get_pixels(pix_buf);
00181     int rowstride = gdk_pixbuf_get_rowstride(pix_buf);
00182     int channels = gdk_pixbuf_get_n_channels(pix_buf);
00183 
00184     guchar *p = NULL;
00185     for (int y=0; y<height; y++)
00186     {
00187         for (int x=0; x<width; x++)
00188         {
00189             p = pixels + y*rowstride + x*channels;
00190             p[0] = p[1] = p[2] = bmp[y*width+x];
00191         }
00192     }
00193 }

Here is the caller graph for this function:

void handle_event ( const Event ev  ) 

Definition at line 195 of file multi_thread_test.cpp.

References RenderData::ba, bmp, PluginBitmapAttributes::data, Event::data, text::Range::end, Event::event, fill_pix_buf(), PluginBitmapAttributes::height, SearchResult::matches, text::Position::offset, text::Position::paragraph, RenderData::pos, SearchResult::ranges, RENDER_DONE, text::Range::start, top_level_window, total_pages, and PluginBitmapAttributes::width.

Referenced by timeout_handler().

00196 {
00197     const unsigned char *bmp = 0;
00198     RenderData *rd = 0;
00199     Position pos;
00200     SearchResult *sr = 0;
00201 
00202     switch (ev.event)
00203     {
00204     case PAGINATE_STARTED:
00205         printf("Pagination starts...\n");
00206         break;
00207 
00208     case PAGINATE_ENDED:
00209         total_pages = (unsigned int)ev.data;
00210         printf("Pagination end, total_pages = %d.\n", total_pages);
00211         break;
00212 
00213     case RENDER_DONE:
00214         rd = (RenderData *)ev.data;
00215         pos = rd->pos;
00216         bmp = rd->ba.data;
00217         fill_pix_buf(bmp, rd->ba.width, rd->ba.height);
00218         delete[] bmp;
00219         delete rd;
00220         rd = 0;
00221         printf("Render done for pos (%d, %d).\n", pos.paragraph, pos.offset);
00222         gdk_window_invalidate_rect(top_level_window->window, NULL, TRUE);
00223         break;
00224 
00225     case SEARCH_DONE:
00226         printf("Search done.\n");
00227         sr = (SearchResult *)ev.data;
00228         for (int i=0; i<sr->matches; i++)
00229         {
00230             printf("Match %3d: (%d, %d)~(%d, %d)\n",
00231                 i+1,
00232                 sr->ranges[i].start.paragraph,
00233                 sr->ranges[i].start.offset,
00234                 sr->ranges[i].end.paragraph,
00235                 sr->ranges[i].end.offset);
00236         }
00237         delete[] sr->ranges;
00238         delete sr;
00239         break;
00240 
00241     default:
00242         break;
00243     }
00244 }

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 320 of file multi_thread_test.cpp.

References utils::Signal< A1, A2, A3, A4, A5 >::add_slot(), DEFAULT_SURFACE_HEIGHT, DEFAULT_SURFACE_WIDTH, destroy(), EventHandler::handle_paginate_end(), EventHandler::handle_paginate_start(), EventHandler::handle_render_done(), EventHandler::handle_search_done(), my_expose(), text::TextModel::open(), page_index, text::TextView::pagination_end_signal, text::TextView::pagination_start_signal, pix_buf, text::TextController::render(), text::TextView::render_done_signal, text::TextController::search(), text::SEARCH_ALL, text::TextModel::search_done_signal, timeout_handler(), top_level_window, and widget_event_handler().

00321 {
00322     gtk_init(&argc, &argv);
00323 
00324     if (argc != 2)
00325     {
00326         fprintf(stderr, "Usage: %s <file_name>.\n", argv[0]);
00327         return -1;
00328     }
00329 
00330     // Read content from disk file.
00331     TextModel model(argv[1]);
00332     model.open();
00333 
00334     // Create view and controller.
00335     TextView view(&model);
00336     g_thread_init(NULL);
00337     TextController ctrl(&model, &view);
00338 
00339     // Add signal handler.
00340     EventQueue eq;
00341     EventHandler event_handler(&eq);
00342     model.search_done_signal.add_slot<EventHandler>(&event_handler, &EventHandler::handle_search_done);
00343     view.pagination_start_signal.add_slot<EventHandler>(&event_handler, &EventHandler::handle_paginate_start);
00344     view.pagination_end_signal.add_slot<EventHandler>(&event_handler, &EventHandler::handle_paginate_end);
00345     view.render_done_signal.add_slot<EventHandler>(&event_handler, &EventHandler::handle_render_done);
00346 
00347     // GTK stuff
00348     top_level_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00349     gtk_widget_set_size_request(top_level_window, DEFAULT_SURFACE_WIDTH, DEFAULT_SURFACE_HEIGHT);
00350 
00351     // Register signal handler
00352     g_signal_connect(G_OBJECT(top_level_window), "destroy", G_CALLBACK(destroy), NULL);
00353     g_signal_connect(G_OBJECT(top_level_window), "expose_event", G_CALLBACK(my_expose), &pix_buf);
00354     gtk_signal_connect(GTK_OBJECT(top_level_window), "key_press_event", GTK_SIGNAL_FUNC(widget_event_handler), &ctrl);
00355     g_timeout_add(10, timeout_handler, &eq);
00356 
00357     // Show all widget
00358     gtk_widget_show_all(top_level_window);
00359 
00360     // Render page 0
00361     ctrl.render(page_index);
00362     ctrl.search(SearchContext(SEARCH_ALL, Position(0, 0), "render", true, true, false));
00363     gtk_main();
00364 
00365     g_object_unref(pix_buf);
00366     return 0;
00367 }

Here is the call graph for this function:

static gboolean my_expose ( GtkWidget *  da,
GdkEventExpose *  event,
gpointer  data 
) [static]

Definition at line 292 of file multi_thread_test.cpp.

References gc, and pix_buf.

Referenced by main().

00293 {
00294     GdkGC *gc = gdk_gc_new(da->window);
00295     GdkPixbuf** pix_buf = (GdkPixbuf**)data;
00296     if (*pix_buf != NULL)
00297     {
00298         gdk_draw_pixbuf(da->window,
00299             gc,
00300             *pix_buf,
00301             0,
00302             0,
00303             0,
00304             0,
00305             -1,
00306             -1,
00307             GDK_RGB_DITHER_NORMAL,
00308             0,
00309             0);
00310     }
00311 
00312     return TRUE;
00313 }

Here is the caller graph for this function:

gboolean timeout_handler ( gpointer  data  ) 

Definition at line 280 of file multi_thread_test.cpp.

References EventQueue::get_event(), and handle_event().

Referenced by main().

00281 {
00282     Event ev;
00283     EventQueue *eq = (EventQueue *)data;
00284     while (eq->get_event(ev))
00285     {
00286         handle_event(ev);
00287     }
00288     
00289     return TRUE;
00290 }

Here is the call graph for this function:

Here is the caller graph for this function:

static gboolean widget_event_handler ( GtkWidget *  widget,
GdkEvent *  event,
gpointer  user_data 
) [static]

Definition at line 246 of file multi_thread_test.cpp.

References page_index, text::TextController::render(), and total_pages.

Referenced by main().

00247 {
00248     guint key_code;
00249     TextController *ctrl = (TextController *)user_data;
00250 
00251     switch (event->type)
00252     {
00253         case GDK_KEY_PRESS:
00254             key_code = ((GdkEventKey*)event)->keyval;
00255             if (key_code == GDK_Page_Up)
00256             {
00257                 if (page_index == 0)
00258                 {
00259                     return FALSE;
00260                 }
00261                 --page_index;
00262             }
00263             else if (key_code == GDK_Page_Down)
00264             {
00265                 if (page_index == total_pages-1)
00266                 {
00267                     return FALSE;
00268                 }
00269                 ++page_index;
00270             }
00271             ctrl->render(page_index);
00272             break;
00273 
00274         default:
00275             break;
00276     }
00277 
00278     return TRUE;
00279 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int page_index = 0 [static]

Definition at line 42 of file multi_thread_test.cpp.

Referenced by main(), and widget_event_handler().

GdkPixbuf* pix_buf = NULL

Definition at line 39 of file multi_thread_test.cpp.

Referenced by fill_pix_buf(), main(), and my_expose().

GtkWidget* top_level_window = NULL

Definition at line 40 of file multi_thread_test.cpp.

Referenced by handle_event(), main(), and widget_event_handler().

int total_pages = 0 [static]

Definition at line 43 of file multi_thread_test.cpp.

Referenced by handle_event(), and widget_event_handler().

Generated by  doxygen 1.6.2-20100208