00001 00002 // File: scrollview.h 00003 // Description: ScrollView 00004 // Author: Joern Wanke 00005 // Created: Thu Nov 29 2007 00006 // 00007 // (C) Copyright 2007, Google Inc. 00008 // Licensed under the Apache License, Version 2.0 (the "License"); 00009 // you may not use this file except in compliance with the License. 00010 // You may obtain a copy of the License at 00011 // http://www.apache.org/licenses/LICENSE-2.0 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // 00019 // 00020 // ScrollView is designed as an UI which can be run remotely. This is the 00021 // client code for it, the server part is written in java. The client consists 00022 // mainly of 2 parts: 00023 // The "core" ScrollView which sets up the remote connection, 00024 // takes care of event handling etc. 00025 // The other part of ScrollView consists of predefined API calls through LUA, 00026 // which can basically be used to get a zoomable canvas in which it is possible 00027 // to draw lines, text etc. 00028 // Technically, thanks to LUA, its even possible to bypass the here defined LUA 00029 // API calls at all and generate a java user interface from scratch (or 00030 // basically generate any kind of java program, possibly even dangerous ones). 00031 00032 #ifndef TESSERACT_VIEWER_SCROLLVIEW_H__ 00033 #define TESSERACT_VIEWER_SCROLLVIEW_H__ 00034 00035 #include <stdio.h> 00036 00037 class ScrollView; 00038 class SVNetwork; 00039 class SVMutex; 00040 class SVSemaphore; 00041 struct SVPolyLineBuffer; 00042 00043 enum SVEventType { 00044 SVET_DESTROY, // Window has been destroyed by user. 00045 SVET_EXIT, // User has destroyed the last window by clicking on the 'X'. 00046 SVET_CLICK, // Left button pressed. 00047 SVET_SELECTION, // Left button selection. 00048 SVET_INPUT, // There is some input (single key or a whole string). 00049 SVET_MOUSE, // The mouse has moved with a button pressed. 00050 SVET_MOTION, // The mouse has moved with no button pressed. 00051 SVET_HOVER, // The mouse has stayed still for a second. 00052 SVET_POPUP, // A command selected through a popup menu. 00053 SVET_MENU, // A command selected through the menubar. 00054 SVET_ANY, // Any of the above. 00055 00056 SVET_COUNT // Array sizing. 00057 }; 00058 00059 struct SVEvent { 00060 ~SVEvent() { delete [] parameter; } 00061 SVEvent* copy(); 00062 SVEventType type; // What kind of event. 00063 ScrollView* window; // Window event relates to. 00064 int x; // Coords of click or selection. 00065 int y; 00066 int x_size; // Size of selection. 00067 int y_size; 00068 int command_id; // The ID of the possibly associated event (e.g. MENU) 00069 char* parameter; // Any string that might have been passed as argument. 00070 int counter; // Used to detect which kind of event to process next. 00071 00072 SVEvent() { 00073 window = NULL; 00074 parameter = NULL; 00075 } 00076 00077 SVEvent(const SVEvent&); 00078 SVEvent& operator=(const SVEvent&); 00079 }; 00080 00081 // The SVEventHandler class is used for Event handling: If you register your 00082 // class as SVEventHandler to a ScrollView Window, the SVEventHandler will be 00083 // called whenever an appropriate event occurs. 00084 class SVEventHandler { 00085 public: 00086 virtual ~SVEventHandler() {} 00087 00088 // Gets called by the SV Window. Does nothing on default, overwrite this 00089 // to implement the desired behaviour 00090 virtual void Notify(const SVEvent* sve) { } 00091 }; 00092 00093 // The ScrollView class provides the expernal API to the scrollviewer process. 00094 // The scrollviewer process manages windows and displays images, graphics and 00095 // text while allowing the user to zoom and scroll the windows arbitrarily. 00096 // Each ScrollView class instance represents one window, and stuff is drawn in 00097 // the window through method calls on the class. The constructor is used to 00098 // create the class instance (and the window). 00099 00100 class ScrollView { 00101 public: 00102 // Color enum for pens and brushes. 00103 enum Color { 00104 NONE, 00105 BLACK, 00106 WHITE, 00107 RED, 00108 YELLOW, 00109 GREEN, 00110 CYAN, 00111 BLUE, 00112 MAGENTA, 00113 AQUAMARINE, 00114 DARK_SLATE_BLUE, 00115 LIGHT_BLUE, 00116 MEDIUM_BLUE, 00117 MIDNIGHT_BLUE, 00118 NAVY_BLUE, 00119 SKY_BLUE, 00120 SLATE_BLUE, 00121 STEEL_BLUE, 00122 CORAL, 00123 BROWN, 00124 SANDY_BROWN, 00125 GOLD, 00126 GOLDENROD, 00127 DARK_GREEN, 00128 DARK_OLIVE_GREEN, 00129 FOREST_GREEN, 00130 LIME_GREEN, 00131 PALE_GREEN, 00132 YELLOW_GREEN, 00133 LIGHT_GREY, 00134 DARK_SLATE_GREY, 00135 DIM_GREY, 00136 GREY, 00137 KHAKI, 00138 MAROON, 00139 ORANGE, 00140 ORCHID, 00141 PINK, 00142 PLUM, 00143 INDIAN_RED, 00144 ORANGE_RED, 00145 VIOLET_RED, 00146 SALMON, 00147 TAN, 00148 TURQUOISE, 00149 DARK_TURQUOISE, 00150 VIOLET, 00151 WHEAT, 00152 GREEN_YELLOW // Make sure this one is last. 00153 }; 00154 00155 #ifndef GRAPHICS_DISABLED 00156 00157 // Create a window. The pixel size of the window may be 0,0, in which case 00158 // a default size is selected based on the size of your canvas. 00159 // The canvas may not be 0,0 in size! 00160 ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size, 00161 int x_canvas_size, int y_canvas_size); 00162 // With a flag whether the x axis is reversed. 00163 ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size, 00164 int x_canvas_size, int y_canvas_size, bool y_axis_reversed); 00165 // Connect to a server other than localhost. 00166 ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size, 00167 int x_canvas_size, int y_canvas_size, bool y_axis_reversed, 00168 const char* server_name); 00169 ~ScrollView(); 00170 00171 /******************************************************************************* 00172 * Event handling 00173 * To register as listener, the class has to derive from the SVEventHandler 00174 * class, which consists of a notifyMe(SVEvent*) function that should be 00175 * overwritten to process the event the way you want. 00176 *******************************************************************************/ 00177 00178 // Add an Event Listener to this ScrollView Window. 00179 void AddEventHandler(SVEventHandler* listener); 00180 00181 // Block until an event of the given type is received. 00182 SVEvent* AwaitEvent(SVEventType type); 00183 00184 // Block until any event on any window is received. 00185 SVEvent* AwaitEventAnyWindow(); 00186 00187 /******************************************************************************* 00188 * Getters and Setters 00189 *******************************************************************************/ 00190 00191 // Returns the title of the window. 00192 const char* GetName() { return window_name_; } 00193 00194 // Returns the unique ID of the window. 00195 int GetId() { return window_id_; } 00196 00197 /******************************************************************************* 00198 * API functions for LUA calls 00199 * the implementations for these can be found in svapi.cc 00200 * (keep in mind that the window is actually created through the ScrollView 00201 * constructor, so this is not listed here) 00202 *******************************************************************************/ 00203 00204 #ifdef HAVE_LIBLEPT 00205 // Draw a Pix on (x,y). 00206 void Image(struct Pix* image, int x_pos, int y_pos); 00207 #endif 00208 00209 // Flush buffers and update display. 00210 static void Update(); 00211 00212 // Exit the program. 00213 static void Exit(); 00214 00215 // Update the contents of a specific window. 00216 void UpdateWindow(); 00217 00218 // Erase all content from the window, but do not destroy it. 00219 void Clear(); 00220 00221 // Set pen color with an enum. 00222 void Pen(Color color); 00223 00224 // Set pen color to RGB (0-255). 00225 void Pen(int red, int green, int blue); 00226 00227 // Set pen color to RGBA (0-255). 00228 void Pen(int red, int green, int blue, int alpha); 00229 00230 // Set brush color with an enum. 00231 void Brush(Color color); 00232 00233 // Set brush color to RGB (0-255). 00234 void Brush(int red, int green, int blue); 00235 00236 // Set brush color to RGBA (0-255). 00237 void Brush(int red, int green, int blue, int alpha); 00238 00239 // Set attributes for future text, like font name (e.g. 00240 // "Times New Roman"), font size etc.. 00241 // Note: The underlined flag is currently not supported 00242 void TextAttributes(const char* font, int pixel_size, 00243 bool bold, bool italic, bool underlined); 00244 00245 // Draw line from (x1,y1) to (x2,y2) with the current pencolor. 00246 void Line(int x1, int y1, int x2, int y2); 00247 00248 // Set the stroke width of the pen. 00249 void Stroke(float width); 00250 00251 // Draw a rectangle given upper left corner and lower right corner. 00252 // The current pencolor is used as outline, the brushcolor to fill the shape. 00253 void Rectangle(int x1, int y1, int x2, int y2); 00254 00255 // Draw an ellipse centered on (x,y). 00256 // The current pencolor is used as outline, the brushcolor to fill the shape. 00257 void Ellipse(int x, int y, int width, int height); 00258 00259 // Draw text with the current pencolor 00260 void Text(int x, int y, const char* mystring); 00261 00262 // Draw an image from a local filename. This should be faster than createImage. 00263 // WARNING: This only works on a local machine. This also only works image 00264 // types supported by java (like bmp,jpeg,gif,png) since the image is opened by 00265 // the server. 00266 void Image(const char* image, int x_pos, int y_pos); 00267 00268 // Set the current position to draw from (x,y). In conjunction with... 00269 void SetCursor(int x, int y); 00270 00271 // ...this function, which draws a line from the current to (x,y) and then 00272 // sets the new position to the new (x,y), this can be used to easily draw 00273 // polygons using vertices 00274 void DrawTo(int x, int y); 00275 00276 // Set the SVWindow visible/invisible. 00277 void SetVisible(bool visible); 00278 00279 // Set the SVWindow always on top or not always on top. 00280 void AlwaysOnTop(bool b); 00281 00282 // Shows a modal dialog with "msg" as question and returns 'y' or 'n'. 00283 int ShowYesNoDialog(const char* msg); 00284 00285 // Shows a modal dialog with "msg" as question and returns a char* string. 00286 // Constraint: As return, only words (e.g. no whitespaces etc.) are allowed. 00287 char* ShowInputDialog(const char* msg); 00288 00289 // Adds a messagebox to the SVWindow. This way, it can show the messages... 00290 void AddMessageBox(); 00291 00292 // ...which can be added by this command. 00293 // This is intended as an "debug" output window. 00294 void AddMessage(const char* format, ...); 00295 00296 // Zoom the window to the rectangle given upper left corner and 00297 // lower right corner. 00298 void ZoomToRectangle(int x1, int y1, int x2, int y2); 00299 00300 // Custom messages (manipulating java code directly) can be send through this. 00301 // Send a message to the server and attach the Id of the corresponding window. 00302 // Note: This should only be called if you are know what you are doing, since 00303 // you are fiddling with the Java objects on the server directly. Calling 00304 // this just for fun will likely break your application! 00305 // It is public so you can actually take use of the LUA functionalities, but 00306 // be careful! 00307 void SendMsg(const char* msg, ...); 00308 00309 // Custom messages (manipulating java code directly) can be send through this. 00310 // Send a message to the server without adding the 00311 // window id. Used for global events like Exit(). 00312 // Note: This should only be called if you are know what you are doing, since 00313 // you are fiddling with the Java objects on the server directly. Calling 00314 // this just for fun will likely break your application! 00315 // It is public so you can actually take use of the LUA functionalities, but 00316 // be careful! 00317 static void SendRawMessage(const char* msg); 00318 00319 /******************************************************************************* 00320 * Add new menu entries to parent. If parent is "", the entry gets added to the 00321 * main menubar (toplevel). 00322 *******************************************************************************/ 00323 // This adds a new submenu to the menubar. 00324 void MenuItem(const char* parent, const char* name); 00325 00326 // This adds a new (normal) menu entry with an associated eventID, which should 00327 // be unique among menubar eventIDs. 00328 void MenuItem(const char* parent, const char* name, int cmdEvent); 00329 00330 // This adds a new checkbox entry, which might initally be flagged. 00331 void MenuItem(const char* parent, const char* name, 00332 int cmdEvent, bool flagged); 00333 00334 // This adds a new popup submenu to the popup menu. If parent is "", the entry 00335 // gets added at "toplevel" popupmenu. 00336 void PopupItem(const char* parent, const char* name); 00337 00338 // This adds a new popup entry with the associated eventID, which should be 00339 // unique among popup eventIDs. 00340 // If value and desc are given, on a click the server will ask you to modify 00341 // the value and return the new value. 00342 void PopupItem(const char* parent, const char* name, 00343 int cmdEvent, const char* value, const char* desc); 00344 00345 // Returns the correct Y coordinate for a window, depending on whether it might 00346 // have to be flipped (by ySize). 00347 int TranslateYCoordinate(int y); 00348 00349 private: 00350 #ifdef HAVE_LIBLEPT 00351 // Transfers a binary Image. 00352 void TransferBinaryImage(struct Pix* image); 00353 // Transfers a gray scale Image. 00354 void TransferGrayImage(struct Pix* image); 00355 // Transfers a 32-Bit Image. 00356 void Transfer32bppImage(struct Pix* image); 00357 #endif 00358 00359 // Sets up ScrollView, depending on the variables from the constructor. 00360 void Initialize(const char* name, int x_pos, int y_pos, int x_size, 00361 int y_size, int x_canvas_size, int y_canvas_size, 00362 bool y_axis_reversed, const char* server_name); 00363 00364 // Send the current buffered polygon (if any) and clear it. 00365 void SendPolygon(); 00366 00367 // Start the message receiving thread. 00368 static void* MessageReceiver(void* a); 00369 00370 // Place an event into the event_table (synchronized). 00371 void SetEvent(SVEvent* svevent); 00372 00373 // Wake up the semaphore. 00374 void Signal(); 00375 00376 // Returns the unique, shared network stream. 00377 static SVNetwork* GetStream() { return stream_; } 00378 00379 // Starts a new event handler. Called whenever a new window is created. 00380 static void* StartEventHandler(void* sv); 00381 00382 // Escapes the ' character with a \, so it can be processed by LUA. 00383 char* AddEscapeChars(const char* input); 00384 00385 // The event handler for this window. 00386 SVEventHandler* event_handler_; 00387 // The name of the window. 00388 const char* window_name_; 00389 // The id of the window. 00390 int window_id_; 00391 // The points of the currently under-construction polyline. 00392 SVPolyLineBuffer* points_; 00393 // Whether the axis is reversed. 00394 bool y_axis_is_reversed_; 00395 // Set to true only after the event handler has terminated. 00396 bool event_handler_ended_; 00397 // If the y axis is reversed, flip all y values by ySize. 00398 int y_size_; 00399 // # of created windows (used to assign an id to each ScrollView* for svmap). 00400 static int nr_created_windows_; 00401 00402 // The stream through which the c++ client is connected to the server. 00403 static SVNetwork* stream_; 00404 00405 // Table of all the currently queued events. 00406 SVEvent* event_table_[SVET_COUNT]; 00407 00408 // Mutex to access the event_table_ in a synchronized fashion. 00409 SVMutex* mutex_; 00410 00411 // Semaphore to the thread belonging to this window. 00412 SVSemaphore* semaphore_; 00413 #endif // GRAPHICS_DISABLED 00414 }; 00415 00416 #endif // TESSERACT_VIEWER_SCROLLVIEW_H__