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