tesseract  4.00.00dev
svmnode.cpp
Go to the documentation of this file.
1 // File: svmnode.cpp
3 // description_: ScrollView Menu Node
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 // A SVMenuNode is an entity which contains the mapping from a menu entry on
21 // the server side to the corresponding associated commands on the client.
22 // It is designed to be a tree structure with a root node, which can then be
23 // used to generate the appropriate messages to the server to display the
24 // menu structure there.
25 // A SVMenuNode can both be used in the context_ of popup menus as well as
26 // menu bars.
27 
28 #include <string.h>
29 #include <iostream>
30 #include <cstring>
31 
32 #include "svmnode.h"
33 
34 // Include automatically generated configuration file if running autoconf.
35 #ifdef HAVE_CONFIG_H
36 #include "config_auto.h"
37 #endif
38 
39 #ifndef GRAPHICS_DISABLED
40 
41 #include "scrollview.h"
42 
43 // Create the empty root menu node. with just a caption. All other nodes should
44 // be added to this or one of the submenus.
46  cmd_event_ = -1;
47  child_ = NULL;
48  next_ = NULL;
49  parent_ = NULL;
50  toggle_value_ = false;
51  is_check_box_entry_ = false;
52 }
53 
55 }
56 
57 // Create a new sub menu node with just a caption. This is used to create
58 // nodes which act as parent nodes to other nodes (e.g. submenus).
59 SVMenuNode* SVMenuNode::AddChild(const char* txt) {
60  SVMenuNode* s = new SVMenuNode(-1, txt, false, false, NULL, NULL);
61  this->AddChild(s);
62  return s;
63 }
64 
65 // Create a "normal" menu node which is associated with a command event.
66 void SVMenuNode::AddChild(const char* txt, int command_event) {
67  this->AddChild(new SVMenuNode(command_event, txt, false, false, NULL, NULL));
68 }
69 
70 // Create a menu node with an associated value (which might be changed
71 // through the gui).
72 void SVMenuNode::AddChild(const char* txt, int command_event,
73  const char* val) {
74  this->AddChild(new SVMenuNode(command_event, txt, false, false, val, NULL));
75 }
76 
77 // Create a menu node with an associated value and description_.
78 void SVMenuNode::AddChild(const char* txt, int command_event, const char* val,
79  const char* desc) {
80  this->AddChild(new SVMenuNode(command_event, txt, false, false, val, desc));
81 }
82 
83 // Create a flag menu node.
84 void SVMenuNode::AddChild(const char* txt, int command_event, int tv) {
85  this->AddChild(new SVMenuNode(command_event, txt, tv, true, NULL, NULL));
86 }
87 
88 // Convenience function called from the different constructors to initialize
89 // the different values of the menu node.
90 SVMenuNode::SVMenuNode(int command_event, const char* txt,
91  int tv, bool check_box_entry, const char* val,
92  const char* desc)
93  : text_(txt), value_(val), description_(desc) {
94  cmd_event_ = command_event;
95 
96  child_ = NULL;
97  next_ = NULL;
98  parent_ = NULL;
99  toggle_value_ = tv != 0;
100  is_check_box_entry_ = check_box_entry;
101 }
102 
103 // Add a child node to this menu node.
104 void SVMenuNode::AddChild(SVMenuNode* svmn) {
105  svmn->parent_ = this;
106  // No children yet.
107  if (child_ == NULL) {
108  child_ = svmn;
109  } else {
110  SVMenuNode* cur = child_;
111  while (cur->next_ != NULL) { cur = cur->next_; }
112  cur->next_ = svmn;
113  }
114 }
115 
116 // Build a menu structure for the server and send the necessary messages.
117 // Should be called on the root node. If menu_bar is true, a menu_bar menu
118 // is built (e.g. on top of the window), if it is false a popup menu is
119 // built which gets shown by right clicking on the window.
120 // Deletes itself afterwards.
121 void SVMenuNode::BuildMenu(ScrollView* sv, bool menu_bar) {
122  if ((parent_ != NULL) && (menu_bar)) {
123  if (is_check_box_entry_) {
124  sv->MenuItem(parent_->text_.string(), text_.string(), cmd_event_,
125  toggle_value_);
126  } else {
127  sv->MenuItem(parent_->text_.string(), text_.string(), cmd_event_); }
128  } else if ((parent_ != NULL) && (!menu_bar)) {
129  if (description_.length() > 0) {
130  sv->PopupItem(parent_->text_.string(), text_.string(), cmd_event_,
131  value_.string(), description_.string());
132  } else {
133  sv->PopupItem(parent_->text_.string(), text_.string());
134  }
135  }
136  if (child_ != NULL) {
137  child_->BuildMenu(sv, menu_bar); delete child_;
138  }
139  if (next_ != NULL) {
140  next_->BuildMenu(sv, menu_bar); delete next_;
141  }
142 }
143 
144 #endif // GRAPHICS_DISABLED
void MenuItem(const char *parent, const char *name)
Definition: scrollview.cpp:686
const char * string() const
Definition: strngs.cpp:198
~SVMenuNode()
Definition: svmnode.cpp:54
inT32 length() const
Definition: strngs.cpp:193
void PopupItem(const char *parent, const char *name)
Definition: scrollview.cpp:692
SVMenuNode()
Definition: svmnode.cpp:45
SVMenuNode * AddChild(const char *txt)
Definition: svmnode.cpp:59
void BuildMenu(ScrollView *sv, bool menu_bar=true)
Definition: svmnode.cpp:121