1
///////////////////////////////////////////////////////////////////////
3
// Description: ScrollView
5
// Created: Thu Nov 29 2007
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.
18
///////////////////////////////////////////////////////////////////////
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
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).
32
#ifndef TESSERACT_VIEWER_SCROLLVIEW_H__
33
#define TESSERACT_VIEWER_SCROLLVIEW_H__
41
struct SVPolyLineBuffer;
44
SVET_DESTROY, // Window has been destroyed by user.
45
SVET_EXIT, // User has destroyed the last window by clicking on the 'X'.
46
SVET_CLICK, // Left button pressed.
47
SVET_SELECTION, // Left button selection.
48
SVET_INPUT, // There is some input (single key or a whole string).
49
SVET_MOUSE, // The mouse has moved with a button pressed.
50
SVET_MOTION, // The mouse has moved with no button pressed.
51
SVET_HOVER, // The mouse has stayed still for a second.
52
SVET_POPUP, // A command selected through a popup menu.
53
SVET_MENU, // A command selected through the menubar.
54
SVET_ANY, // Any of the above.
56
SVET_COUNT // Array sizing.
60
~SVEvent() { delete [] parameter; }
62
SVEventType type; // What kind of event.
63
ScrollView* window; // Window event relates to.
64
int x; // Coords of click or selection.
66
int x_size; // Size of selection.
68
int command_id; // The ID of the possibly associated event (e.g. MENU)
69
char* parameter; // Any string that might have been passed as argument.
70
int counter; // Used to detect which kind of event to process next.
77
SVEvent(const SVEvent&);
78
SVEvent& operator=(const SVEvent&);
81
// The SVEventHandler class is used for Event handling: If you register your
82
// class as SVEventHandler to a ScrollView Window, the SVEventHandler will be
83
// called whenever an appropriate event occurs.
84
class SVEventHandler {
86
virtual ~SVEventHandler() {}
88
// Gets called by the SV Window. Does nothing on default, overwrite this
89
// to implement the desired behaviour
90
virtual void Notify(const SVEvent* sve) { }
93
// The ScrollView class provides the expernal API to the scrollviewer process.
94
// The scrollviewer process manages windows and displays images, graphics and
95
// text while allowing the user to zoom and scroll the windows arbitrarily.
96
// Each ScrollView class instance represents one window, and stuff is drawn in
97
// the window through method calls on the class. The constructor is used to
98
// create the class instance (and the window).
102
// Color enum for pens and brushes.
152
GREEN_YELLOW // Make sure this one is last.
155
#ifndef GRAPHICS_DISABLED
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);
171
/*******************************************************************************
173
* To register as listener, the class has to derive from the SVEventHandler
174
* class, which consists of a notifyMe(SVEvent*) function that should be
175
* overwritten to process the event the way you want.
176
*******************************************************************************/
178
// Add an Event Listener to this ScrollView Window.
179
void AddEventHandler(SVEventHandler* listener);
181
// Block until an event of the given type is received.
182
SVEvent* AwaitEvent(SVEventType type);
184
// Block until any event on any window is received.
185
SVEvent* AwaitEventAnyWindow();
187
/*******************************************************************************
188
* Getters and Setters
189
*******************************************************************************/
191
// Returns the title of the window.
192
const char* GetName() { return window_name_; }
194
// Returns the unique ID of the window.
195
int GetId() { return window_id_; }
197
/*******************************************************************************
198
* API functions for LUA calls
199
* the implementations for these can be found in svapi.cc
200
* (keep in mind that the window is actually created through the ScrollView
201
* constructor, so this is not listed here)
202
*******************************************************************************/
204
// Draw a Pix on (x,y).
205
void Image(struct Pix* image, int x_pos, int y_pos);
207
// Flush buffers and update display.
208
static void Update();
213
// Update the contents of a specific window.
216
// Erase all content from the window, but do not destroy it.
219
// Set pen color with an enum.
220
void Pen(Color color);
222
// Set pen color to RGB (0-255).
223
void Pen(int red, int green, int blue);
225
// Set pen color to RGBA (0-255).
226
void Pen(int red, int green, int blue, int alpha);
228
// Set brush color with an enum.
229
void Brush(Color color);
231
// Set brush color to RGB (0-255).
232
void Brush(int red, int green, int blue);
234
// Set brush color to RGBA (0-255).
235
void Brush(int red, int green, int blue, int alpha);
237
// Set attributes for future text, like font name (e.g.
238
// "Times New Roman"), font size etc..
239
// Note: The underlined flag is currently not supported
240
void TextAttributes(const char* font, int pixel_size,
241
bool bold, bool italic, bool underlined);
243
// Draw line from (x1,y1) to (x2,y2) with the current pencolor.
244
void Line(int x1, int y1, int x2, int y2);
246
// Set the stroke width of the pen.
247
void Stroke(float width);
249
// Draw a rectangle given upper left corner and lower right corner.
250
// The current pencolor is used as outline, the brushcolor to fill the shape.
251
void Rectangle(int x1, int y1, int x2, int y2);
253
// Draw an ellipse centered on (x,y).
254
// The current pencolor is used as outline, the brushcolor to fill the shape.
255
void Ellipse(int x, int y, int width, int height);
257
// Draw text with the current pencolor
258
void Text(int x, int y, const char* mystring);
260
// Draw an image from a local filename. This should be faster than createImage.
261
// WARNING: This only works on a local machine. This also only works image
262
// types supported by java (like bmp,jpeg,gif,png) since the image is opened by
264
void Image(const char* image, int x_pos, int y_pos);
266
// Set the current position to draw from (x,y). In conjunction with...
267
void SetCursor(int x, int y);
269
// ...this function, which draws a line from the current to (x,y) and then
270
// sets the new position to the new (x,y), this can be used to easily draw
271
// polygons using vertices
272
void DrawTo(int x, int y);
274
// Set the SVWindow visible/invisible.
275
void SetVisible(bool visible);
277
// Set the SVWindow always on top or not always on top.
278
void AlwaysOnTop(bool b);
280
// Shows a modal dialog with "msg" as question and returns 'y' or 'n'.
281
int ShowYesNoDialog(const char* msg);
283
// Shows a modal dialog with "msg" as question and returns a char* string.
284
// Constraint: As return, only words (e.g. no whitespaces etc.) are allowed.
285
char* ShowInputDialog(const char* msg);
287
// Adds a messagebox to the SVWindow. This way, it can show the messages...
288
void AddMessageBox();
290
// ...which can be added by this command.
291
// This is intended as an "debug" output window.
292
void AddMessage(const char* format, ...);
294
// Zoom the window to the rectangle given upper left corner and
295
// lower right corner.
296
void ZoomToRectangle(int x1, int y1, int x2, int y2);
298
// Custom messages (manipulating java code directly) can be send through this.
299
// Send a message to the server and attach the Id of the corresponding window.
300
// Note: This should only be called if you are know what you are doing, since
301
// you are fiddling with the Java objects on the server directly. Calling
302
// this just for fun will likely break your application!
303
// It is public so you can actually take use of the LUA functionalities, but
305
void SendMsg(const char* msg, ...);
307
// Custom messages (manipulating java code directly) can be send through this.
308
// Send a message to the server without adding the
309
// window id. Used for global events like Exit().
310
// Note: This should only be called if you are know what you are doing, since
311
// you are fiddling with the Java objects on the server directly. Calling
312
// this just for fun will likely break your application!
313
// It is public so you can actually take use of the LUA functionalities, but
315
static void SendRawMessage(const char* msg);
317
/*******************************************************************************
318
* Add new menu entries to parent. If parent is "", the entry gets added to the
319
* main menubar (toplevel).
320
*******************************************************************************/
321
// This adds a new submenu to the menubar.
322
void MenuItem(const char* parent, const char* name);
324
// This adds a new (normal) menu entry with an associated eventID, which should
325
// be unique among menubar eventIDs.
326
void MenuItem(const char* parent, const char* name, int cmdEvent);
328
// This adds a new checkbox entry, which might initally be flagged.
329
void MenuItem(const char* parent, const char* name,
330
int cmdEvent, bool flagged);
332
// This adds a new popup submenu to the popup menu. If parent is "", the entry
333
// gets added at "toplevel" popupmenu.
334
void PopupItem(const char* parent, const char* name);
336
// This adds a new popup entry with the associated eventID, which should be
337
// unique among popup eventIDs.
338
// If value and desc are given, on a click the server will ask you to modify
339
// the value and return the new value.
340
void PopupItem(const char* parent, const char* name,
341
int cmdEvent, const char* value, const char* desc);
343
// Returns the correct Y coordinate for a window, depending on whether it might
344
// have to be flipped (by ySize).
345
int TranslateYCoordinate(int y);
348
// Transfers a binary Image.
349
void TransferBinaryImage(struct Pix* image);
350
// Transfers a gray scale Image.
351
void TransferGrayImage(struct Pix* image);
352
// Transfers a 32-Bit Image.
353
void Transfer32bppImage(struct Pix* image);
355
// Sets up ScrollView, depending on the variables from the constructor.
356
void Initialize(const char* name, int x_pos, int y_pos, int x_size,
357
int y_size, int x_canvas_size, int y_canvas_size,
358
bool y_axis_reversed, const char* server_name);
360
// Send the current buffered polygon (if any) and clear it.
363
// Start the message receiving thread.
364
static void* MessageReceiver(void* a);
366
// Place an event into the event_table (synchronized).
367
void SetEvent(SVEvent* svevent);
369
// Wake up the semaphore.
372
// Returns the unique, shared network stream.
373
static SVNetwork* GetStream() { return stream_; }
375
// Starts a new event handler. Called whenever a new window is created.
376
static void* StartEventHandler(void* sv);
378
// Escapes the ' character with a \, so it can be processed by LUA.
379
char* AddEscapeChars(const char* input);
381
// The event handler for this window.
382
SVEventHandler* event_handler_;
383
// The name of the window.
384
const char* window_name_;
385
// The id of the window.
387
// The points of the currently under-construction polyline.
388
SVPolyLineBuffer* points_;
389
// Whether the axis is reversed.
390
bool y_axis_is_reversed_;
391
// If the y axis is reversed, flip all y values by ySize.
393
// # of created windows (used to assign an id to each ScrollView* for svmap).
394
static int nr_created_windows_;
396
// The stream through which the c++ client is connected to the server.
397
static SVNetwork* stream_;
399
// Table of all the currently queued events.
400
SVEvent* event_table_[SVET_COUNT];
402
// Mutex to access the event_table_ in a synchronized fashion.
405
// Semaphore to the thread belonging to this window.
406
SVSemaphore* semaphore_;
407
#endif // GRAPHICS_DISABLED
410
#endif // TESSERACT_VIEWER_SCROLLVIEW_H__