tesseract 4.1.1
Loading...
Searching...
No Matches
scrollview.h
Go to the documentation of this file.
1
2// 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
39class ScrollView;
40class SVNetwork;
41class SVMutex;
42class SVSemaphore;
43struct SVPolyLineBuffer;
44
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
61struct SVEvent {
62 ~SVEvent() { delete [] parameter; }
63 SVEvent* copy();
64 SVEventType type = SVET_DESTROY; // What kind of event.
65 ScrollView* window = nullptr; // Window event relates to.
66 char* parameter = nullptr; // Any string that might have been passed as argument.
67 int x = 0; // Coords of click or selection.
68 int y = 0;
69 int x_size = 0; // Size of selection.
70 int y_size = 0;
71 int command_id = 0; // The ID of the possibly associated event (e.g. MENU)
72 int counter = 0; // Used to detect which kind of event to process next.
73
74 SVEvent() = default;
77};
78
79// The SVEventHandler class is used for Event handling: If you register your
80// class as SVEventHandler to a ScrollView Window, the SVEventHandler will be
81// called whenever an appropriate event occurs.
83 public:
84 virtual ~SVEventHandler();
85
86// Gets called by the SV Window. Does nothing on default, overwrite this
87// to implement the desired behaviour
88 virtual void Notify(const SVEvent* sve) { (void)sve; }
89};
90
91// The ScrollView class provides the expernal API to the scrollviewer process.
92// The scrollviewer process manages windows and displays images, graphics and
93// text while allowing the user to zoom and scroll the windows arbitrarily.
94// Each ScrollView class instance represents one window, and stuff is drawn in
95// the window through method calls on the class. The constructor is used to
96// create the class instance (and the window).
97
99 public:
100// Color enum for pens and brushes.
101 enum Color {
150 GREEN_YELLOW // Make sure this one is last.
152
153 ~ScrollView();
154
155#ifndef GRAPHICS_DISABLED
156
157// Create a window. The pixel size of the window may be 0,0, in which case
158// a default size is selected based on the size of your canvas.
159// The canvas may not be 0,0 in size!
160 ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
161 int x_canvas_size, int y_canvas_size);
162// With a flag whether the x axis is reversed.
163 ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
164 int x_canvas_size, int y_canvas_size, bool y_axis_reversed);
165// Connect to a server other than localhost.
166 ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
167 int x_canvas_size, int y_canvas_size, bool y_axis_reversed,
168 const char* server_name);
169/*******************************************************************************
170* Event handling
171* To register as listener, the class has to derive from the SVEventHandler
172* class, which consists of a notifyMe(SVEvent*) function that should be
173* overwritten to process the event the way you want.
174*******************************************************************************/
175
176// Add an Event Listener to this ScrollView Window.
177 void AddEventHandler(SVEventHandler* listener);
178
179// Block until an event of the given type is received.
181
182// Block until any event on any window is received.
184
185/*******************************************************************************
186* Getters and Setters
187*******************************************************************************/
188
189// Returns the title of the window.
190 const char* GetName() { return window_name_; }
191
192// Returns the unique ID of the window.
193 int GetId() { return window_id_; }
194
195/*******************************************************************************
196* API functions for LUA calls
197* the implementations for these can be found in svapi.cc
198* (keep in mind that the window is actually created through the ScrollView
199* constructor, so this is not listed here)
200*******************************************************************************/
201
202// Draw a Pix on (x,y).
203 void Image(struct Pix* image, int x_pos, int y_pos);
204
205// Flush buffers and update display.
206 static void Update();
207
208// Exit the program.
209 static void Exit();
210
211// Update the contents of a specific window.
212 void UpdateWindow();
213
214// Erase all content from the window, but do not destroy it.
215 void Clear();
216
217// Set pen color with an enum.
218 void Pen(Color color);
219
220// Set pen color to RGB (0-255).
221 void Pen(int red, int green, int blue);
222
223// Set pen color to RGBA (0-255).
224 void Pen(int red, int green, int blue, int alpha);
225
226// Set brush color with an enum.
227 void Brush(Color color);
228
229// Set brush color to RGB (0-255).
230 void Brush(int red, int green, int blue);
231
232// Set brush color to RGBA (0-255).
233 void Brush(int red, int green, int blue, int alpha);
234
235// Set attributes for future text, like font name (e.g.
236// "Times New Roman"), font size etc..
237// Note: The underlined flag is currently not supported
238 void TextAttributes(const char* font, int pixel_size,
239 bool bold, bool italic, bool underlined);
240
241// Draw line from (x1,y1) to (x2,y2) with the current pencolor.
242 void Line(int x1, int y1, int x2, int y2);
243
244// Set the stroke width of the pen.
245 void Stroke(float width);
246
247// Draw a rectangle given upper left corner and lower right corner.
248// The current pencolor is used as outline, the brushcolor to fill the shape.
249 void Rectangle(int x1, int y1, int x2, int y2);
250
251// Draw an ellipse centered on (x,y).
252// The current pencolor is used as outline, the brushcolor to fill the shape.
253 void Ellipse(int x, int y, int width, int height);
254
255// Draw text with the current pencolor
256 void Text(int x, int y, const char* mystring);
257
258// Draw an image from a local filename. This should be faster than createImage.
259// WARNING: This only works on a local machine. This also only works image
260// types supported by java (like bmp,jpeg,gif,png) since the image is opened by
261// the server.
262 void Image(const char* image, int x_pos, int y_pos);
263
264// Set the current position to draw from (x,y). In conjunction with...
265 void SetCursor(int x, int y);
266
267// ...this function, which draws a line from the current to (x,y) and then
268// sets the new position to the new (x,y), this can be used to easily draw
269// polygons using vertices
270 void DrawTo(int x, int y);
271
272// Set the SVWindow visible/invisible.
273 void SetVisible(bool visible);
274
275// Set the SVWindow always on top or not always on top.
276 void AlwaysOnTop(bool b);
277
278// Shows a modal dialog with "msg" as question and returns 'y' or 'n'.
279 int ShowYesNoDialog(const char* msg);
280
281// Shows a modal dialog with "msg" as question and returns a char* string.
282// Constraint: As return, only words (e.g. no whitespaces etc.) are allowed.
283 char* ShowInputDialog(const char* msg);
284
285// Adds a messagebox to the SVWindow. This way, it can show the messages...
286 void AddMessageBox();
287
288// ...which can be added by this command.
289// This is intended as an "debug" output window.
290 void AddMessage(const char* format, ...);
291
292// Zoom the window to the rectangle given upper left corner and
293// lower right corner.
294 void ZoomToRectangle(int x1, int y1, int x2, int y2);
295
296// Custom messages (manipulating java code directly) can be send through this.
297// Send a message to the server and attach the Id of the corresponding window.
298// Note: This should only be called if you are know what you are doing, since
299// you are fiddling with the Java objects on the server directly. Calling
300// this just for fun will likely break your application!
301// It is public so you can actually take use of the LUA functionalities, but
302// be careful!
303 void SendMsg(const char* msg, ...);
304
305// Custom messages (manipulating java code directly) can be send through this.
306// Send a message to the server without adding the
307// window id. Used for global events like Exit().
308// Note: This should only be called if you are know what you are doing, since
309// you are fiddling with the Java objects on the server directly. Calling
310// this just for fun will likely break your application!
311// It is public so you can actually take use of the LUA functionalities, but
312// be careful!
313 static void SendRawMessage(const char* msg);
314
315/*******************************************************************************
316* Add new menu entries to parent. If parent is "", the entry gets added to the
317* main menubar (toplevel).
318*******************************************************************************/
319// This adds a new submenu to the menubar.
320 void MenuItem(const char* parent, const char* name);
321
322// This adds a new (normal) menu entry with an associated eventID, which should
323// be unique among menubar eventIDs.
324 void MenuItem(const char* parent, const char* name, int cmdEvent);
325
326 // This adds a new checkbox entry, which might initially be flagged.
327 void MenuItem(const char* parent, const char* name,
328 int cmdEvent, bool flagged);
329
330// This adds a new popup submenu to the popup menu. If parent is "", the entry
331// gets added at "toplevel" popupmenu.
332 void PopupItem(const char* parent, const char* name);
333
334// This adds a new popup entry with the associated eventID, which should be
335// unique among popup eventIDs.
336// If value and desc are given, on a click the server will ask you to modify
337// the value and return the new value.
338 void PopupItem(const char* parent, const char* name,
339 int cmdEvent, const char* value, const char* desc);
340
341// Returns the correct Y coordinate for a window, depending on whether it might
342// have to be flipped (by ySize).
343 int TranslateYCoordinate(int y);
344
345 private:
346// Transfers a binary Image.
347 void TransferBinaryImage(struct Pix* image);
348// Transfers a gray scale Image.
349 void TransferGrayImage(struct Pix* image);
350// Transfers a 32-Bit Image.
351 void Transfer32bppImage(struct Pix* image);
352
353// Sets up ScrollView, depending on the variables from the constructor.
354 void Initialize(const char* name, int x_pos, int y_pos, int x_size,
355 int y_size, int x_canvas_size, int y_canvas_size,
356 bool y_axis_reversed, const char* server_name);
357
358// Send the current buffered polygon (if any) and clear it.
359 void SendPolygon();
360
361// Start the message receiving thread.
362 static void* MessageReceiver(void* a);
363
364// Place an event into the event_table (synchronized).
365 void SetEvent(SVEvent* svevent);
366
367// Wake up the semaphore.
368 void Signal();
369
370// Returns the unique, shared network stream.
371 static SVNetwork* GetStream() { return stream_; }
372
373// Starts a new event handler. Called whenever a new window is created.
374 static void* StartEventHandler(void* sv);
375
376// Escapes the ' character with a \, so it can be processed by LUA.
377 char* AddEscapeChars(const char* input);
378
379 // The event handler for this window.
380 SVEventHandler* event_handler_;
381 // The name of the window.
382 const char* window_name_;
383 // The id of the window.
384 int window_id_;
385 // The points of the currently under-construction polyline.
386 SVPolyLineBuffer* points_;
387 // Whether the axis is reversed.
388 bool y_axis_is_reversed_;
389 // Set to true only after the event handler has terminated.
390 bool event_handler_ended_;
391 // If the y axis is reversed, flip all y values by ySize.
392 int y_size_;
393 // # of created windows (used to assign an id to each ScrollView* for svmap).
394 static int nr_created_windows_;
395 // Serial number of sent images to ensure that the viewer knows they
396 // are distinct.
397 static int image_index_;
398
399 // The stream through which the c++ client is connected to the server.
400 static SVNetwork* stream_;
401
402 // Table of all the currently queued events.
403 SVEvent* event_table_[SVET_COUNT];
404
405 // Mutex to access the event_table_ in a synchronized fashion.
406 SVMutex* mutex_;
407
408 // Semaphore to the thread belonging to this window.
409 SVSemaphore* semaphore_;
410#endif // GRAPHICS_DISABLED
411};
412
413#endif // OCR_SCROLLVIEW_H__
414#endif // TESSERACT_VIEWER_SCROLLVIEW_H_
SVEventType
Definition: scrollview.h:45
@ SVET_HOVER
Definition: scrollview.h:53
@ SVET_MOTION
Definition: scrollview.h:52
@ SVET_CLICK
Definition: scrollview.h:48
@ SVET_EXIT
Definition: scrollview.h:47
@ SVET_POPUP
Definition: scrollview.h:54
@ SVET_ANY
Definition: scrollview.h:56
@ SVET_SELECTION
Definition: scrollview.h:49
@ SVET_MOUSE
Definition: scrollview.h:51
@ SVET_MENU
Definition: scrollview.h:55
@ SVET_DESTROY
Definition: scrollview.h:46
@ SVET_COUNT
Definition: scrollview.h:58
@ SVET_INPUT
Definition: scrollview.h:50
~SVEvent()
Definition: scrollview.h:62
int x
Definition: scrollview.h:67
SVEvent * copy()
Definition: scrollview.cpp:59
SVEvent & operator=(const SVEvent &)
SVEventType type
Definition: scrollview.h:64
int x_size
Definition: scrollview.h:69
char * parameter
Definition: scrollview.h:66
int counter
Definition: scrollview.h:72
int y
Definition: scrollview.h:68
SVEvent(const SVEvent &)
int y_size
Definition: scrollview.h:70
int command_id
Definition: scrollview.h:71
ScrollView * window
Definition: scrollview.h:65
SVEvent()=default
virtual void Notify(const SVEvent *sve)
Definition: scrollview.h:88
virtual ~SVEventHandler()
static void Exit()
Definition: scrollview.cpp:583
static void Update()
Definition: scrollview.cpp:709
void MenuItem(const char *parent, const char *name)
Definition: scrollview.cpp:680
SVEvent * AwaitEvent(SVEventType type)
Definition: scrollview.cpp:443
@ DARK_SLATE_GREY
Definition: scrollview.h:132
@ DARK_SLATE_BLUE
Definition: scrollview.h:112
@ DARK_TURQUOISE
Definition: scrollview.h:147
@ DARK_OLIVE_GREEN
Definition: scrollview.h:126
void DrawTo(int x, int y)
Definition: scrollview.cpp:525
int GetId()
Definition: scrollview.h:193
void AddMessageBox()
Definition: scrollview.cpp:578
void PopupItem(const char *parent, const char *name)
Definition: scrollview.cpp:686
void Line(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:532
void Ellipse(int x, int y, int width, int height)
Definition: scrollview.cpp:609
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:635
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:765
SVEvent * AwaitEventAnyWindow()
Definition: scrollview.cpp:464
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:652
void Clear()
Definition: scrollview.cpp:589
void AddEventHandler(SVEventHandler *listener)
Add an Event Listener to this ScrollView Window.
Definition: scrollview.cpp:414
void ZoomToRectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:757
void SendMsg(const char *msg,...)
Send a message to the server, attaching the window id.
Definition: scrollview.cpp:391
void UpdateWindow()
Definition: scrollview.cpp:704
static void SendRawMessage(const char *msg)
Definition: scrollview.cpp:409
void SetCursor(int x, int y)
Definition: scrollview.cpp:519
void Pen(Color color)
Definition: scrollview.cpp:719
void AlwaysOnTop(bool b)
Definition: scrollview.cpp:555
int TranslateYCoordinate(int y)
Definition: scrollview.cpp:827
const char * GetName()
Definition: scrollview.h:190
void SetVisible(bool visible)
Definition: scrollview.cpp:549
int ShowYesNoDialog(const char *msg)
Definition: scrollview.cpp:745
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:600
char * ShowInputDialog(const char *msg)
Definition: scrollview.cpp:733
void Brush(Color color)
Definition: scrollview.cpp:725
void Stroke(float width)
Definition: scrollview.cpp:594
void AddMessage(const char *format,...)
Definition: scrollview.cpp:561
Definition: svutil.h:68