tesseract  4.00.00dev
svpaint.cpp
Go to the documentation of this file.
1 // Copyright 2007 Google Inc. All Rights Reserved.
2 //
3 // Author: Joern Wanke
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 //
14 // Simple drawing program to illustrate ScrollView capabilities.
15 //
16 // Functionality:
17 // - The menubar is used to select from different sample styles of input.
18 // - With the RMB it is possible to change the RGB values in different
19 // popup menus.
20 // - A LMB click either draws point-to-point, point or text.
21 // - A LMB dragging either draws a line, a rectangle or ellipse.
22 
23 // Include automatically generated configuration file if running autoconf.
24 #ifdef HAVE_CONFIG_H
25 #include "config_auto.h"
26 #endif
27 
28 #ifndef GRAPHICS_DISABLED
29 #include "scrollview.h"
30 #include "svmnode.h"
31 #include <stdlib.h>
32 #include <iostream>
33 
34 // The current color values we use, initially white (== ScrollView::WHITE).
35 int rgb[3] = { 255, 255, 255 };
36 
37 class SVPaint : public SVEventHandler {
38  public:
39  explicit SVPaint(const char* server_name);
40 // This is the main event handling function that we need to overwrite, defined
41 // in SVEventHandler.
42  void Notify(const SVEvent* sv_event);
43  private:
44 // The Handler take care of the SVET_POPUP, SVET_MENU, SVET_CLICK and
45 // SVET_SELECTION events.
46  void PopupHandler(const SVEvent* sv_event);
47  void MenuBarHandler(const SVEvent* sv_event);
48  void ClickHandler(const SVEvent* sv_event);
49  void SelectionHandler(const SVEvent* sv_event);
50 
51 // Convenience functions to build little menus.
52  SVMenuNode* BuildPopupMenu();
53  SVMenuNode* BuildMenuBar();
54 
55 // Our window.
56  ScrollView* window_;
57 
58 // The mode we are in when an SVET_CLICK or an SVET_SELECTION event occurs.
59  int click_mode_;
60  int drag_mode_;
61 
62 // In the point-to-point drawing mode, we need to set a start-point the first
63 // time we call it (e.g. call SetCursor).
64  bool has_start_point_;
65 };
66 
67 // Build a sample popup menu.
68 SVMenuNode* SVPaint::BuildPopupMenu() {
69  SVMenuNode* root = new SVMenuNode(); // Empty root node
70  // Initial color is white, so we all values to 255.
71  root->AddChild("R", // Shown caption.
72  1, // assoc. command_id.
73  "255", // initial value.
74  "Red Color Value?"); // Shown description.
75  root->AddChild("G", 2, "255", "Green Color Value?");
76  root->AddChild("B", 3, "255", "Blue Color Value?");
77  return root;
78 }
79 
80 // Build a sample menu bar.
81 SVMenuNode* SVPaint::BuildMenuBar() {
82  SVMenuNode* root = new SVMenuNode(); // Empty root node
83 
84  // Create some submenus and add them to the root.
85  SVMenuNode* click = root->AddChild("Clicking");
86  SVMenuNode* drag = root->AddChild("Dragging");
87 
88  // Put some nodes into the submenus.
89  click->AddChild("Point to Point Drawing", // Caption.
90  1); // command_id.
91  click->AddChild("Point Drawing", 2);
92  click->AddChild("Text Drawing", 3);
93  drag->AddChild("Line Drawing", 4);
94  drag->AddChild("Rectangle Drawing", 5);
95  drag->AddChild("Ellipse Drawing", 6);
96  return root;
97 }
98 
99 // Takes care of the SVET_POPUP events.
100 // In our case, SVET_POPUP is used to set RGB values.
101 void SVPaint::PopupHandler(const SVEvent* sv_event) {
102  // Since we only have the RGB values as popup items,
103  // we take a shortcut to not bloat up code:
104  rgb[sv_event->command_id - 1] = atoi(sv_event->parameter);
105  window_->Pen(rgb[0], rgb[1], rgb[2]);
106 }
107 
108 // Takes care of the SVET_MENU events.
109 // In our case, we change either the click_mode_ (commands 1-3)
110 // or the drag_mode_ (commands 4-6).
111 void SVPaint::MenuBarHandler(const SVEvent* sv_event) {
112  if ((sv_event->command_id > 0) && (sv_event->command_id < 4)) {
113  click_mode_ = sv_event->command_id;
114  has_start_point_ = false;
115  } else { drag_mode_ = sv_event->command_id; }
116 }
117 
118 // Takes care of the SVET_CLICK events.
119 // Depending on the click_mode_ we are in, either do Point-to-Point drawing,
120 // point drawing, or draw text.
121 void SVPaint::ClickHandler(const SVEvent* sv_event) {
122  switch (click_mode_) {
123  case 1: //Point to Point
124  if (has_start_point_) { window_->DrawTo(sv_event->x, sv_event->y);
125  } else {
126  has_start_point_ = true;
127  window_->SetCursor(sv_event->x, sv_event->y);
128  }
129  break;
130  case 2: //Point Drawing..simulated by drawing a 1 pixel line.
131  window_->Line(sv_event->x, sv_event->y, sv_event->x, sv_event->y);
132  break;
133  case 3: //Text
134  // We show a modal input dialog on our window, then draw the input and
135  // finally delete the input pointer.
136  char* p = window_->ShowInputDialog("Text:");
137  window_->Text(sv_event->x, sv_event->y, p);
138  delete [] p;
139  break;
140  }
141 }
142 
143 // Takes care of the SVET_SELECTION events.
144 // Depending on the drag_mode_ we are in, either draw a line, a rectangle or
145 // an ellipse.
146 void SVPaint::SelectionHandler(const SVEvent* sv_event) {
147  switch (drag_mode_) {
148  //FIXME inversed x_size, y_size
149  case 4: //Line
150  window_->Line(sv_event->x, sv_event->y,
151  sv_event->x - sv_event->x_size,
152  sv_event->y - sv_event->y_size);
153  break;
154  case 5: //Rectangle
155  window_->Rectangle(sv_event->x, sv_event->y,
156  sv_event->x - sv_event->x_size,
157  sv_event->y - sv_event->y_size);
158  break;
159  case 6: //Ellipse
160  window_->Ellipse(sv_event->x - sv_event->x_size,
161  sv_event->y - sv_event->y_size,
162  sv_event->x_size, sv_event->y_size);
163  break;
164  }
165 }
166 
167 // The event handling function from ScrollView which we have to overwrite.
168 // We handle CLICK, SELECTION, MENU and POPUP and throw away all other events.
169 void SVPaint::Notify(const SVEvent* sv_event) {
170  if (sv_event->type == SVET_CLICK) { ClickHandler(sv_event); }
171  else if (sv_event->type == SVET_SELECTION) { SelectionHandler(sv_event); }
172  else if (sv_event->type == SVET_MENU) { MenuBarHandler(sv_event); }
173  else if (sv_event->type == SVET_POPUP) { PopupHandler(sv_event); }
174  else {} //throw other events away
175 }
176 
177 // Builds a new window, initializes the variables and event handler and builds
178 // the menu.
179 SVPaint::SVPaint(const char *server_name) {
180  window_ = new ScrollView("ScrollView Paint Example", // window caption
181  0, 0, // x,y window position
182  500, 500, // window size
183  500, 500, // canvas size
184  false, // whether the Y axis is inversed.
185  // this is included due to legacy
186  // reasons for tesseract and enables
187  // us to have (0,0) as the LOWER left
188  // of the coordinate system.
189  server_name); // the server address.
190 
191  // Set the start modes to point-to-point and line drawing.
192  click_mode_ = 1;
193  drag_mode_ = 4;
194  has_start_point_ = false;
195 
196  // Bild our menus and add them to the window. The flag illustrates whether
197  // this is a menu bar.
198  SVMenuNode* popup_menu = BuildPopupMenu();
199  popup_menu->BuildMenu(window_,false);
200 
201  SVMenuNode* bar_menu = BuildMenuBar();
202  bar_menu->BuildMenu(window_,true);
203 
204  // Set the initial color values to White (could also be done by
205  // passing (rgb[0], rgb[1], rgb[2]).
206  window_->Pen(ScrollView::WHITE);
207  window_->Brush(ScrollView::WHITE);
208 
209  // Adds the event handler to the window. This actually ensures that Notify
210  // gets called when events occur.
211  window_->AddEventHandler(this);
212 
213  // Set the window visible (calling this is important to actually render
214  // everything. Without this call, the window would also be drawn, but the
215  // menu bars would be missing.
216  window_->SetVisible(true);
217 
218  // Rest this thread until its window is destroyed.
219  // Note that a special eventhandling thread was created when constructing
220  // the window. Due to this, the application will not deadlock here.
221  window_->AwaitEvent(SVET_DESTROY);
222  // We now have 3 Threads running:
223  // (1) The MessageReceiver thread which fetches messages and distributes them
224  // (2) The EventHandler thread which handles all events for window_
225  // (3) The main thread which waits on window_ for a DESTROY event (blocked)
226 }
227 
228 // If a parameter is given, we try to connect to the given server.
229 // This enables us to test the remote capabilites of ScrollView.
230 int main(int argc, char** argv) {
231  const char* server_name;
232  if (argc > 1) { server_name = argv[1]; } else { server_name = "localhost"; }
233  SVPaint svp(server_name);
234 }
235 #endif // GRAPHICS_DISABLED
void Line(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:538
char * parameter
Definition: scrollview.h:71
SVEventType type
Definition: scrollview.h:64
int rgb[3]
Definition: svpaint.cpp:35
void Brush(Color color)
Definition: scrollview.cpp:732
void Notify(const SVEvent *sv_event)
Definition: svpaint.cpp:169
SVPaint(const char *server_name)
Definition: svpaint.cpp:179
char * ShowInputDialog(const char *msg)
Definition: scrollview.cpp:740
void SetCursor(int x, int y)
Definition: scrollview.cpp:525
int command_id
Definition: scrollview.h:70
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:606
int y_size
Definition: scrollview.h:69
int y
Definition: scrollview.h:67
void AddEventHandler(SVEventHandler *listener)
Add an Event Listener to this ScrollView Window.
Definition: scrollview.cpp:418
int x
Definition: scrollview.h:66
void Ellipse(int x, int y, int width, int height)
Definition: scrollview.cpp:615
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:658
SVMenuNode * AddChild(const char *txt)
Definition: svmnode.cpp:59
int x_size
Definition: scrollview.h:68
void SetVisible(bool visible)
Definition: scrollview.cpp:555
int main(int argc, char **argv)
Definition: svpaint.cpp:230
void BuildMenu(ScrollView *sv, bool menu_bar=true)
Definition: svmnode.cpp:121
void Pen(Color color)
Definition: scrollview.cpp:726
void DrawTo(int x, int y)
Definition: scrollview.cpp:531
SVEvent * AwaitEvent(SVEventType type)
Definition: scrollview.cpp:449