tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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 class ScrollView;
00040 class SVNetwork;
00041 class SVMutex;
00042 class SVSemaphore;
00043 struct SVPolyLineBuffer;
00044 
00045 enum SVEventType {
00046   SVET_DESTROY,    // Window has been destroyed by user.
00047   SVET_EXIT,       // User has destroyed the last window by clicking on the 'X'.
00048   SVET_CLICK,      // Left button pressed.
00049   SVET_SELECTION,  // Left button selection.
00050   SVET_INPUT,      // There is some input (single key or a whole string).
00051   SVET_MOUSE,      // The mouse has moved with a button pressed.
00052   SVET_MOTION,     // The mouse has moved with no button pressed.
00053   SVET_HOVER,      // The mouse has stayed still for a second.
00054   SVET_POPUP,      // A command selected through a popup menu.
00055   SVET_MENU,       // A command selected through the menubar.
00056   SVET_ANY,        // Any of the above.
00057 
00058   SVET_COUNT       // Array sizing.
00059 };
00060 
00061 struct SVEvent {
00062   ~SVEvent() { delete [] parameter; }
00063   SVEvent* copy();
00064   SVEventType type;    // What kind of event.
00065   ScrollView* window;  // Window event relates to.
00066   int x;               // Coords of click or selection.
00067   int y;
00068   int x_size;          // Size of selection.
00069   int y_size;
00070   int command_id;      // The ID of the possibly associated event (e.g. MENU)
00071   char* parameter;     // Any string that might have been passed as argument.
00072   int counter;         // Used to detect which kind of event to process next.
00073 
00074   SVEvent() {
00075     window = NULL;
00076     parameter = NULL;
00077   }
00078 
00079   SVEvent(const SVEvent&);
00080   SVEvent& operator=(const SVEvent&);
00081 };
00082 
00083 // The SVEventHandler class is used for Event handling: If you register your
00084 // class as SVEventHandler to a ScrollView Window, the SVEventHandler will be
00085 // called whenever an appropriate event occurs.
00086 class SVEventHandler {
00087   public:
00088     virtual ~SVEventHandler() {}
00089 
00090 // Gets called by the SV Window. Does nothing on default, overwrite this
00091 // to implement the desired behaviour
00092     virtual void Notify(const SVEvent* sve) { }
00093 };
00094 
00095 // The ScrollView class provides the expernal API to the scrollviewer process.
00096 // The scrollviewer process manages windows and displays images, graphics and
00097 // text while allowing the user to zoom and scroll the windows arbitrarily.
00098 // Each ScrollView class instance represents one window, and stuff is drawn in
00099 // the window through method calls on the class. The constructor is used to
00100 // create the class instance (and the window).
00101 
00102 class ScrollView {
00103  public:
00104 // Color enum for pens and brushes.
00105   enum Color {
00106     NONE,
00107     BLACK,
00108     WHITE,
00109     RED,
00110     YELLOW,
00111     GREEN,
00112     CYAN,
00113     BLUE,
00114     MAGENTA,
00115     AQUAMARINE,
00116     DARK_SLATE_BLUE,
00117     LIGHT_BLUE,
00118     MEDIUM_BLUE,
00119     MIDNIGHT_BLUE,
00120     NAVY_BLUE,
00121     SKY_BLUE,
00122     SLATE_BLUE,
00123     STEEL_BLUE,
00124     CORAL,
00125     BROWN,
00126     SANDY_BROWN,
00127     GOLD,
00128     GOLDENROD,
00129     DARK_GREEN,
00130     DARK_OLIVE_GREEN,
00131     FOREST_GREEN,
00132     LIME_GREEN,
00133     PALE_GREEN,
00134     YELLOW_GREEN,
00135     LIGHT_GREY,
00136     DARK_SLATE_GREY,
00137     DIM_GREY,
00138     GREY,
00139     KHAKI,
00140     MAROON,
00141     ORANGE,
00142     ORCHID,
00143     PINK,
00144     PLUM,
00145     INDIAN_RED,
00146     ORANGE_RED,
00147     VIOLET_RED,
00148     SALMON,
00149     TAN,
00150     TURQUOISE,
00151     DARK_TURQUOISE,
00152     VIOLET,
00153     WHEAT,
00154     GREEN_YELLOW  // Make sure this one is last.
00155 };
00156 
00157   ~ScrollView();
00158 
00159 #ifndef GRAPHICS_DISABLED
00160 
00161 // Create a window. The pixel size of the window may be 0,0, in which case
00162 // a default size is selected based on the size of your canvas.
00163 // The canvas may not be 0,0 in size!
00164   ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
00165              int x_canvas_size, int y_canvas_size);
00166 // With a flag whether the x axis is reversed.
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, bool y_axis_reversed);
00169 // Connect to a server other than localhost.
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              const char* server_name);
00173 /*******************************************************************************
00174 * Event handling
00175 * To register as listener, the class has to derive from the SVEventHandler
00176 * class, which consists of a notifyMe(SVEvent*) function that should be
00177 * overwritten to process the event the way you want.
00178 *******************************************************************************/
00179 
00180 // Add an Event Listener to this ScrollView Window.
00181   void AddEventHandler(SVEventHandler* listener);
00182 
00183 // Block until an event of the given type is received.
00184   SVEvent* AwaitEvent(SVEventType type);
00185 
00186 // Block until any event on any window is received.
00187   SVEvent* AwaitEventAnyWindow();
00188 
00189 /*******************************************************************************
00190 * Getters and Setters
00191 *******************************************************************************/
00192 
00193 // Returns the title of the window.
00194   const char* GetName() { return window_name_; }
00195 
00196 // Returns the unique ID of the window.
00197   int GetId() { return window_id_; }
00198 
00199 /*******************************************************************************
00200 * API functions for LUA calls
00201 * the implementations for these can be found in svapi.cc
00202 * (keep in mind that the window is actually created through the ScrollView
00203 * constructor, so this is not listed here)
00204 *******************************************************************************/
00205 
00206 // Draw a Pix on (x,y).
00207   void Image(struct Pix* image, int x_pos, int y_pos);
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 // Transfers a binary Image.
00351   void TransferBinaryImage(struct Pix* image);
00352 // Transfers a gray scale Image.
00353   void TransferGrayImage(struct Pix* image);
00354 // Transfers a 32-Bit Image.
00355   void Transfer32bppImage(struct Pix* image);
00356 
00357 // Sets up ScrollView, depending on the variables from the constructor.
00358   void Initialize(const char* name, int x_pos, int y_pos, int x_size,
00359                   int y_size, int x_canvas_size, int y_canvas_size,
00360                   bool y_axis_reversed, const char* server_name);
00361 
00362 // Send the current buffered polygon (if any) and clear it.
00363   void SendPolygon();
00364 
00365 // Start the message receiving thread.
00366   static void* MessageReceiver(void* a);
00367 
00368 // Place an event into the event_table (synchronized).
00369   void SetEvent(SVEvent* svevent);
00370 
00371 // Wake up the semaphore.
00372   void Signal();
00373 
00374 // Returns the unique, shared network stream.
00375   static SVNetwork* GetStream() { return stream_; }
00376 
00377 // Starts a new event handler. Called whenever a new window is created.
00378   static void* StartEventHandler(void* sv);
00379 
00380 // Escapes the ' character with a \, so it can be processed by LUA.
00381   char* AddEscapeChars(const char* input);
00382 
00383   // The event handler for this window.
00384   SVEventHandler* event_handler_;
00385   // The name of the window.
00386   const char* window_name_;
00387   // The id of the window.
00388   int window_id_;
00389   // The points of the currently under-construction polyline.
00390   SVPolyLineBuffer* points_;
00391   // Whether the axis is reversed.
00392   bool y_axis_is_reversed_;
00393   // Set to true only after the event handler has terminated.
00394   bool event_handler_ended_;
00395   // If the y axis is reversed, flip all y values by ySize.
00396   int y_size_;
00397   // # of created windows (used to assign an id to each ScrollView* for svmap).
00398   static int nr_created_windows_;
00399   // Serial number of sent images to ensure that the viewer knows they
00400   // are distinct.
00401   static int image_index_;
00402 
00403   // The stream through which the c++ client is connected to the server.
00404   static SVNetwork* stream_;
00405 
00406   // Table of all the currently queued events.
00407   SVEvent* event_table_[SVET_COUNT];
00408 
00409   // Mutex to access the event_table_ in a synchronized fashion.
00410   SVMutex* mutex_;
00411 
00412   // Semaphore to the thread belonging to this window.
00413   SVSemaphore* semaphore_;
00414 #endif  // GRAPHICS_DISABLED
00415 };
00416 
00417 #endif  // OCR_SCROLLVIEW_H__
00418 #endif  // TESSERACT_VIEWER_SCROLLVIEW_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines