Tesseract 3.01
/data/source/tesseract-ocr/viewer/scrollview.h
Go to the documentation of this file.
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 // Draw a Pix on (x,y).
00205   void Image(struct Pix* image, int x_pos, int y_pos);
00206 
00207 // Flush buffers and update display.
00208   static void Update();
00209 
00210 // Exit the program.
00211   static void Exit();
00212 
00213 // Update the contents of a specific window.
00214   void UpdateWindow();
00215 
00216 // Erase all content from the window, but do not destroy it.
00217   void Clear();
00218 
00219 // Set pen color with an enum.
00220   void Pen(Color color);
00221 
00222 // Set pen color to RGB (0-255).
00223   void Pen(int red, int green, int blue);
00224 
00225 // Set pen color to RGBA (0-255).
00226   void Pen(int red, int green, int blue, int alpha);
00227 
00228 // Set brush color with an enum.
00229   void Brush(Color color);
00230 
00231 // Set brush color to RGB (0-255).
00232   void Brush(int red, int green, int blue);
00233 
00234 // Set brush color to RGBA (0-255).
00235   void Brush(int red, int green, int blue, int alpha);
00236 
00237 // Set attributes for future text, like font name (e.g.
00238 // "Times New Roman"), font size etc..
00239 // Note: The underlined flag is currently not supported
00240   void TextAttributes(const char* font, int pixel_size,
00241                       bool bold, bool italic, bool underlined);
00242 
00243 // Draw line from (x1,y1) to (x2,y2) with the current pencolor.
00244   void Line(int x1, int y1, int x2, int y2);
00245 
00246 // Set the stroke width of the pen.
00247   void Stroke(float width);
00248 
00249 // Draw a rectangle given upper left corner and lower right corner.
00250 // The current pencolor is used as outline, the brushcolor to fill the shape.
00251   void Rectangle(int x1, int y1, int x2, int y2);
00252 
00253 // Draw an ellipse centered on (x,y).
00254 // The current pencolor is used as outline, the brushcolor to fill the shape.
00255   void Ellipse(int x, int y, int width, int height);
00256 
00257 // Draw text with the current pencolor
00258   void Text(int x, int y, const char* mystring);
00259 
00260 // Draw an image from a local filename. This should be faster than createImage.
00261 // WARNING: This only works on a local machine. This also only works image
00262 // types supported by java (like bmp,jpeg,gif,png) since the image is opened by
00263 // the server.
00264   void Image(const char* image, int x_pos, int y_pos);
00265 
00266 // Set the current position to draw from (x,y). In conjunction with...
00267   void SetCursor(int x, int y);
00268 
00269 // ...this function, which draws a line from the current to (x,y) and then
00270 // sets the new position to the new (x,y), this can be used to easily draw
00271 // polygons using vertices
00272   void DrawTo(int x, int y);
00273 
00274 // Set the SVWindow visible/invisible.
00275   void SetVisible(bool visible);
00276 
00277 // Set the SVWindow always on top or not always on top.
00278   void AlwaysOnTop(bool b);
00279 
00280 // Shows a modal dialog with "msg" as question and returns 'y' or 'n'.
00281   int ShowYesNoDialog(const char* msg);
00282 
00283 // Shows a modal dialog with "msg" as question and returns a char* string.
00284 // Constraint: As return, only words (e.g. no whitespaces etc.) are allowed.
00285   char* ShowInputDialog(const char* msg);
00286 
00287 // Adds a messagebox to the SVWindow. This way, it can show the messages...
00288   void AddMessageBox();
00289 
00290 // ...which can be added by this command.
00291 // This is intended as an "debug" output window.
00292   void AddMessage(const char* format, ...);
00293 
00294 // Zoom the window to the rectangle given upper left corner and
00295 // lower right corner.
00296   void ZoomToRectangle(int x1, int y1, int x2, int y2);
00297 
00298 // Custom messages (manipulating java code directly) can be send through this.
00299 // Send a message to the server and attach the Id of the corresponding window.
00300 // Note: This should only be called if you are know what you are doing, since
00301 // you are fiddling with the Java objects on the server directly. Calling
00302 // this just for fun will likely break your application!
00303 // It is public so you can actually take use of the LUA functionalities, but
00304 // be careful!
00305   void SendMsg(const char* msg, ...);
00306 
00307 // Custom messages (manipulating java code directly) can be send through this.
00308 // Send a message to the server without adding the
00309 // window id. Used for global events like Exit().
00310 // Note: This should only be called if you are know what you are doing, since
00311 // you are fiddling with the Java objects on the server directly. Calling
00312 // this just for fun will likely break your application!
00313 // It is public so you can actually take use of the LUA functionalities, but
00314 // be careful!
00315   static void SendRawMessage(const char* msg);
00316 
00317 /*******************************************************************************
00318 * Add new menu entries to parent. If parent is "", the entry gets added to the
00319 * main menubar (toplevel).
00320 *******************************************************************************/
00321 // This adds a new submenu to the menubar.
00322   void MenuItem(const char* parent, const char* name);
00323 
00324 // This adds a new (normal) menu entry with an associated eventID, which should
00325 // be unique among menubar eventIDs.
00326   void MenuItem(const char* parent, const char* name, int cmdEvent);
00327 
00328 // This adds a new checkbox entry, which might initally be flagged.
00329   void MenuItem(const char* parent, const char* name,
00330                 int cmdEvent, bool flagged);
00331 
00332 // This adds a new popup submenu to the popup menu. If parent is "", the entry
00333 // gets added at "toplevel" popupmenu.
00334   void PopupItem(const char* parent, const char* name);
00335 
00336 // This adds a new popup entry with the associated eventID, which should be
00337 // unique among popup eventIDs.
00338 // If value and desc are given, on a click the server will ask you to modify
00339 // the value and return the new value.
00340   void PopupItem(const char* parent, const char* name,
00341                  int cmdEvent, const char* value, const char* desc);
00342 
00343 // Returns the correct Y coordinate for a window, depending on whether it might
00344 // have to be flipped (by ySize).
00345   int TranslateYCoordinate(int y);
00346 
00347  private:
00348 // Transfers a binary Image.
00349   void TransferBinaryImage(struct Pix* image);
00350 // Transfers a gray scale Image.
00351   void TransferGrayImage(struct Pix* image);
00352 // Transfers a 32-Bit Image.
00353   void Transfer32bppImage(struct Pix* image);
00354 
00355 // Sets up ScrollView, depending on the variables from the constructor.
00356   void Initialize(const char* name, int x_pos, int y_pos, int x_size,
00357                   int y_size, int x_canvas_size, int y_canvas_size,
00358                   bool y_axis_reversed, const char* server_name);
00359 
00360 // Send the current buffered polygon (if any) and clear it.
00361   void SendPolygon();
00362 
00363 // Start the message receiving thread.
00364   static void* MessageReceiver(void* a);
00365 
00366 // Place an event into the event_table (synchronized).
00367   void SetEvent(SVEvent* svevent);
00368 
00369 // Wake up the semaphore.
00370   void Signal();
00371 
00372 // Returns the unique, shared network stream.
00373   static SVNetwork* GetStream() { return stream_; }
00374 
00375 // Starts a new event handler. Called whenever a new window is created.
00376   static void* StartEventHandler(void* sv);
00377 
00378 // Escapes the ' character with a \, so it can be processed by LUA.
00379   char* AddEscapeChars(const char* input);
00380 
00381   // The event handler for this window.
00382   SVEventHandler* event_handler_;
00383   // The name of the window.
00384   const char* window_name_;
00385   // The id of the window.
00386   int window_id_;
00387   // The points of the currently under-construction polyline.
00388   SVPolyLineBuffer* points_;
00389   // Whether the axis is reversed.
00390   bool y_axis_is_reversed_;
00391   // Set to true only after the event handler has terminated.
00392   bool event_handler_ended_;
00393   // If the y axis is reversed, flip all y values by ySize.
00394   int y_size_;
00395   // # of created windows (used to assign an id to each ScrollView* for svmap).
00396   static int nr_created_windows_;
00397   // Serial number of sent images to ensure that the viewer knows they
00398   // are distinct.
00399   static int image_index_;
00400 
00401   // The stream through which the c++ client is connected to the server.
00402   static SVNetwork* stream_;
00403 
00404   // Table of all the currently queued events.
00405   SVEvent* event_table_[SVET_COUNT];
00406 
00407   // Mutex to access the event_table_ in a synchronized fashion.
00408   SVMutex* mutex_;
00409 
00410   // Semaphore to the thread belonging to this window.
00411   SVSemaphore* semaphore_;
00412 #endif  // GRAPHICS_DISABLED
00413 };
00414 
00415 #endif  // TESSERACT_VIEWER_SCROLLVIEW_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines