tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
scrollview.h
1 // File: scrollview.h
3 // Description: ScrollView
4 // Author: Joern Wanke
5 // Created: Thu Nov 29 2007
6 //
7 // (C) Copyright 2007, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 //
20 // ScrollView is designed as an UI which can be run remotely. This is the
21 // client code for it, the server part is written in java. The client consists
22 // mainly of 2 parts:
23 // The "core" ScrollView which sets up the remote connection,
24 // takes care of event handling etc.
25 // The other part of ScrollView consists of predefined API calls through LUA,
26 // which can basically be used to get a zoomable canvas in which it is possible
27 // to draw lines, text etc.
28 // Technically, thanks to LUA, its even possible to bypass the here defined LUA
29 // API calls at all and generate a java user interface from scratch (or
30 // basically generate any kind of java program, possibly even dangerous ones).
31 
32 #ifndef TESSERACT_VIEWER_SCROLLVIEW_H_
33 #define TESSERACT_VIEWER_SCROLLVIEW_H_
34 // TODO(rays) Move ScrollView into the tesseract namespace.
35 #ifndef OCR_SCROLLVIEW_H__
36 
37 #include <cstdio>
38 
39 class ScrollView;
40 class SVNetwork;
41 class SVMutex;
42 class SVSemaphore;
43 struct SVPolyLineBuffer;
44 
45 enum SVEventType {
46  SVET_DESTROY, // Window has been destroyed by user.
47  SVET_EXIT, // User has destroyed the last window by clicking on the 'X'.
48  SVET_CLICK, // Left button pressed.
49  SVET_SELECTION, // Left button selection.
50  SVET_INPUT, // There is some input (single key or a whole string).
51  SVET_MOUSE, // The mouse has moved with a button pressed.
52  SVET_MOTION, // The mouse has moved with no button pressed.
53  SVET_HOVER, // The mouse has stayed still for a second.
54  SVET_POPUP, // A command selected through a popup menu.
55  SVET_MENU, // A command selected through the menubar.
56  SVET_ANY, // Any of the above.
57 
58  SVET_COUNT // Array sizing.
59 };
60 
61 struct SVEvent {
62  ~SVEvent() { delete [] parameter; }
63  SVEvent* copy();
64  SVEventType type; // What kind of event.
65  ScrollView* window; // Window event relates to.
66  int x; // Coords of click or selection.
67  int y;
68  int x_size; // Size of selection.
69  int y_size;
70  int command_id; // The ID of the possibly associated event (e.g. MENU)
71  char* parameter; // Any string that might have been passed as argument.
72  int counter; // Used to detect which kind of event to process next.
73 
74  SVEvent() {
75  window = nullptr;
76  parameter = nullptr;
77  }
78 
79  SVEvent(const SVEvent&);
80  SVEvent& operator=(const SVEvent&);
81 };
82 
83 // The SVEventHandler class is used for Event handling: If you register your
84 // class as SVEventHandler to a ScrollView Window, the SVEventHandler will be
85 // called whenever an appropriate event occurs.
87  public:
88  virtual ~SVEventHandler();
89 
90 // Gets called by the SV Window. Does nothing on default, overwrite this
91 // to implement the desired behaviour
92  virtual void Notify(const SVEvent* sve) { (void)sve; }
93 };
94 
95 // The ScrollView class provides the expernal API to the scrollviewer process.
96 // The scrollviewer process manages windows and displays images, graphics and
97 // text while allowing the user to zoom and scroll the windows arbitrarily.
98 // Each ScrollView class instance represents one window, and stuff is drawn in
99 // the window through method calls on the class. The constructor is used to
100 // create the class instance (and the window).
101 
102 class ScrollView {
103  public:
104 // Color enum for pens and brushes.
105  enum Color {
154  GREEN_YELLOW // Make sure this one is last.
155 };
156 
157  ~ScrollView();
158 
159 #ifndef GRAPHICS_DISABLED
160 
161 // Create a window. The pixel size of the window may be 0,0, in which case
162 // a default size is selected based on the size of your canvas.
163 // The canvas may not be 0,0 in size!
164  ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
165  int x_canvas_size, int y_canvas_size);
166 // With a flag whether the x axis is reversed.
167  ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
168  int x_canvas_size, int y_canvas_size, bool y_axis_reversed);
169 // Connect to a server other than localhost.
170  ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
171  int x_canvas_size, int y_canvas_size, bool y_axis_reversed,
172  const char* server_name);
173 /*******************************************************************************
174 * Event handling
175 * To register as listener, the class has to derive from the SVEventHandler
176 * class, which consists of a notifyMe(SVEvent*) function that should be
177 * overwritten to process the event the way you want.
178 *******************************************************************************/
179 
180 // Add an Event Listener to this ScrollView Window.
181  void AddEventHandler(SVEventHandler* listener);
182 
183 // Block until an event of the given type is received.
184  SVEvent* AwaitEvent(SVEventType type);
185 
186 // Block until any event on any window is received.
187  SVEvent* AwaitEventAnyWindow();
188 
189 /*******************************************************************************
190 * Getters and Setters
191 *******************************************************************************/
192 
193 // Returns the title of the window.
194  const char* GetName() { return window_name_; }
195 
196 // Returns the unique ID of the window.
197  int GetId() { return window_id_; }
198 
199 /*******************************************************************************
200 * API functions for LUA calls
201 * the implementations for these can be found in svapi.cc
202 * (keep in mind that the window is actually created through the ScrollView
203 * constructor, so this is not listed here)
204 *******************************************************************************/
205 
206 // Draw a Pix on (x,y).
207  void Image(struct Pix* image, int x_pos, int y_pos);
208 
209 // Flush buffers and update display.
210  static void Update();
211 
212 // Exit the program.
213  static void Exit();
214 
215 // Update the contents of a specific window.
216  void UpdateWindow();
217 
218 // Erase all content from the window, but do not destroy it.
219  void Clear();
220 
221 // Set pen color with an enum.
222  void Pen(Color color);
223 
224 // Set pen color to RGB (0-255).
225  void Pen(int red, int green, int blue);
226 
227 // Set pen color to RGBA (0-255).
228  void Pen(int red, int green, int blue, int alpha);
229 
230 // Set brush color with an enum.
231  void Brush(Color color);
232 
233 // Set brush color to RGB (0-255).
234  void Brush(int red, int green, int blue);
235 
236 // Set brush color to RGBA (0-255).
237  void Brush(int red, int green, int blue, int alpha);
238 
239 // Set attributes for future text, like font name (e.g.
240 // "Times New Roman"), font size etc..
241 // Note: The underlined flag is currently not supported
242  void TextAttributes(const char* font, int pixel_size,
243  bool bold, bool italic, bool underlined);
244 
245 // Draw line from (x1,y1) to (x2,y2) with the current pencolor.
246  void Line(int x1, int y1, int x2, int y2);
247 
248 // Set the stroke width of the pen.
249  void Stroke(float width);
250 
251 // Draw a rectangle given upper left corner and lower right corner.
252 // The current pencolor is used as outline, the brushcolor to fill the shape.
253  void Rectangle(int x1, int y1, int x2, int y2);
254 
255 // Draw an ellipse centered on (x,y).
256 // The current pencolor is used as outline, the brushcolor to fill the shape.
257  void Ellipse(int x, int y, int width, int height);
258 
259 // Draw text with the current pencolor
260  void Text(int x, int y, const char* mystring);
261 
262 // Draw an image from a local filename. This should be faster than createImage.
263 // WARNING: This only works on a local machine. This also only works image
264 // types supported by java (like bmp,jpeg,gif,png) since the image is opened by
265 // the server.
266  void Image(const char* image, int x_pos, int y_pos);
267 
268 // Set the current position to draw from (x,y). In conjunction with...
269  void SetCursor(int x, int y);
270 
271 // ...this function, which draws a line from the current to (x,y) and then
272 // sets the new position to the new (x,y), this can be used to easily draw
273 // polygons using vertices
274  void DrawTo(int x, int y);
275 
276 // Set the SVWindow visible/invisible.
277  void SetVisible(bool visible);
278 
279 // Set the SVWindow always on top or not always on top.
280  void AlwaysOnTop(bool b);
281 
282 // Shows a modal dialog with "msg" as question and returns 'y' or 'n'.
283  int ShowYesNoDialog(const char* msg);
284 
285 // Shows a modal dialog with "msg" as question and returns a char* string.
286 // Constraint: As return, only words (e.g. no whitespaces etc.) are allowed.
287  char* ShowInputDialog(const char* msg);
288 
289 // Adds a messagebox to the SVWindow. This way, it can show the messages...
290  void AddMessageBox();
291 
292 // ...which can be added by this command.
293 // This is intended as an "debug" output window.
294  void AddMessage(const char* format, ...);
295 
296 // Zoom the window to the rectangle given upper left corner and
297 // lower right corner.
298  void ZoomToRectangle(int x1, int y1, int x2, int y2);
299 
300 // Custom messages (manipulating java code directly) can be send through this.
301 // Send a message to the server and attach the Id of the corresponding window.
302 // Note: This should only be called if you are know what you are doing, since
303 // you are fiddling with the Java objects on the server directly. Calling
304 // this just for fun will likely break your application!
305 // It is public so you can actually take use of the LUA functionalities, but
306 // be careful!
307  void SendMsg(const char* msg, ...);
308 
309 // Custom messages (manipulating java code directly) can be send through this.
310 // Send a message to the server without adding the
311 // window id. Used for global events like Exit().
312 // Note: This should only be called if you are know what you are doing, since
313 // you are fiddling with the Java objects on the server directly. Calling
314 // this just for fun will likely break your application!
315 // It is public so you can actually take use of the LUA functionalities, but
316 // be careful!
317  static void SendRawMessage(const char* msg);
318 
319 /*******************************************************************************
320 * Add new menu entries to parent. If parent is "", the entry gets added to the
321 * main menubar (toplevel).
322 *******************************************************************************/
323 // This adds a new submenu to the menubar.
324  void MenuItem(const char* parent, const char* name);
325 
326 // This adds a new (normal) menu entry with an associated eventID, which should
327 // be unique among menubar eventIDs.
328  void MenuItem(const char* parent, const char* name, int cmdEvent);
329 
330  // This adds a new checkbox entry, which might initially be flagged.
331  void MenuItem(const char* parent, const char* name,
332  int cmdEvent, bool flagged);
333 
334 // This adds a new popup submenu to the popup menu. If parent is "", the entry
335 // gets added at "toplevel" popupmenu.
336  void PopupItem(const char* parent, const char* name);
337 
338 // This adds a new popup entry with the associated eventID, which should be
339 // unique among popup eventIDs.
340 // If value and desc are given, on a click the server will ask you to modify
341 // the value and return the new value.
342  void PopupItem(const char* parent, const char* name,
343  int cmdEvent, const char* value, const char* desc);
344 
345 // Returns the correct Y coordinate for a window, depending on whether it might
346 // have to be flipped (by ySize).
347  int TranslateYCoordinate(int y);
348 
349  private:
350 // Transfers a binary Image.
351  void TransferBinaryImage(struct Pix* image);
352 // Transfers a gray scale Image.
353  void TransferGrayImage(struct Pix* image);
354 // Transfers a 32-Bit Image.
355  void Transfer32bppImage(struct Pix* image);
356 
357 // Sets up ScrollView, depending on the variables from the constructor.
358  void Initialize(const char* name, int x_pos, int y_pos, int x_size,
359  int y_size, int x_canvas_size, int y_canvas_size,
360  bool y_axis_reversed, const char* server_name);
361 
362 // Send the current buffered polygon (if any) and clear it.
363  void SendPolygon();
364 
365 // Start the message receiving thread.
366  static void* MessageReceiver(void* a);
367 
368 // Place an event into the event_table (synchronized).
369  void SetEvent(SVEvent* svevent);
370 
371 // Wake up the semaphore.
372  void Signal();
373 
374 // Returns the unique, shared network stream.
375  static SVNetwork* GetStream() { return stream_; }
376 
377 // Starts a new event handler. Called whenever a new window is created.
378  static void* StartEventHandler(void* sv);
379 
380 // Escapes the ' character with a \, so it can be processed by LUA.
381  char* AddEscapeChars(const char* input);
382 
383  // The event handler for this window.
385  // The name of the window.
386  const char* window_name_;
387  // The id of the window.
389  // The points of the currently under-construction polyline.
391  // Whether the axis is reversed.
393  // Set to true only after the event handler has terminated.
395  // If the y axis is reversed, flip all y values by ySize.
396  int y_size_;
397  // # of created windows (used to assign an id to each ScrollView* for svmap).
399  // Serial number of sent images to ensure that the viewer knows they
400  // are distinct.
401  static int image_index_;
402 
403  // The stream through which the c++ client is connected to the server.
405 
406  // Table of all the currently queued events.
407  SVEvent* event_table_[SVET_COUNT];
408 
409  // Mutex to access the event_table_ in a synchronized fashion.
411 
412  // Semaphore to the thread belonging to this window.
414 #endif // GRAPHICS_DISABLED
415 };
416 
417 #endif // OCR_SCROLLVIEW_H__
418 #endif // TESSERACT_VIEWER_SCROLLVIEW_H_
Definition: scrollview.h:136
Definition: scrollview.h:130
Definition: scrollview.h:108
bool event_handler_ended_
Definition: scrollview.h:394
int command_id
Definition: scrollview.h:70
Definition: scrollview.h:147
Definition: scrollview.h:144
Definition: scrollview.h:61
Definition: scrollview.h:151
Definition: scrollview.h:148
Definition: scrollview.h:145
Definition: scrollview.h:142
Definition: scrollview.h:115
Definition: scrollview.h:116
SVEventType type
Definition: scrollview.h:64
Definition: scrollview.h:128
Definition: svutil.h:58
const char * window_name_
Definition: scrollview.h:386
Color
Definition: scrollview.h:105
Definition: scrollview.h:117
Definition: scrollview.h:123
Definition: scrollview.h:143
int window_id_
Definition: scrollview.h:388
Definition: scrollview.h:134
Definition: scrollview.h:110
int y_size
Definition: scrollview.h:69
int counter
Definition: scrollview.h:72
SVEvent()
Definition: scrollview.h:74
int x
Definition: scrollview.h:66
Definition: scrollview.h:126
SVMutex * mutex_
Definition: scrollview.h:410
Definition: scrollview.h:106
int x_size
Definition: scrollview.h:68
Definition: scrollview.h:120
Definition: scrollview.h:138
Definition: scrollview.h:129
SVEvent * copy()
Definition: scrollview.cpp:61
static SVNetwork * stream_
Definition: scrollview.h:404
Definition: scrollview.h:119
~SVEvent()
Definition: scrollview.h:62
virtual void Notify(const SVEvent *sve)
Definition: scrollview.h:92
Definition: scrollview.h:139
Definition: scrollview.h:149
Definition: scrollview.h:153
Definition: scrollview.h:135
Definition: scrollview.h:102
SVEvent & operator=(const SVEvent &)
Definition: scrollview.h:146
Definition: scrollview.h:127
Definition: scrollview.h:131
Definition: svutil.h:78
Definition: svutil.h:109
Definition: scrollview.h:109
Definition: scrollview.h:125
Definition: scrollview.h:113
Definition: scrollview.h:132
Definition: scrollview.h:152
int y_size_
Definition: scrollview.h:396
static int nr_created_windows_
Definition: scrollview.h:398
static SVNetwork * GetStream()
Definition: scrollview.h:375
bool y_axis_is_reversed_
Definition: scrollview.h:392
SVEventHandler * event_handler_
Definition: scrollview.h:384
Definition: scrollview.h:118
Definition: scrollview.h:112
Definition: scrollview.h:150
ScrollView * window
Definition: scrollview.h:65
int GetId()
Definition: scrollview.h:197
Definition: scrollview.h:124
Definition: scrollview.h:86
Definition: scrollview.h:122
static int image_index_
Definition: scrollview.h:401
Definition: scrollview.h:137
Definition: scrollview.h:121
SVSemaphore * semaphore_
Definition: scrollview.h:413
char * parameter
Definition: scrollview.h:71
Definition: scrollview.h:133
Definition: scrollview.cpp:47
const char * GetName()
Definition: scrollview.h:194
Definition: scrollview.h:107
SVPolyLineBuffer * points_
Definition: scrollview.h:390
Definition: scrollview.h:141
Definition: scrollview.h:111
Definition: scrollview.h:114
Definition: scrollview.h:140
int y
Definition: scrollview.h:67