tesseract  5.0.0
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 automatically generated configuration file if running autoconf.
29 #ifdef HAVE_CONFIG_H
30 # include "config_auto.h"
31 #endif
32 
33 #ifndef GRAPHICS_DISABLED
34 
35 #include "svmnode.h"
36 
37 #include <cstring>
38 #include <iostream>
39 
40 #include "scrollview.h"
41 
42 namespace tesseract {
43 
44 // Create the empty root menu node. with just a caption. All other nodes should
45 // be added to this or one of the submenus.
47  cmd_event_ = -1;
48  child_ = nullptr;
49  next_ = nullptr;
50  parent_ = nullptr;
51  toggle_value_ = false;
52  is_check_box_entry_ = false;
53 }
54 
55 SVMenuNode::~SVMenuNode() = default;
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  auto *s = new SVMenuNode(-1, txt, false, false);
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));
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, const char *val) {
73  this->AddChild(new SVMenuNode(command_event, txt, false, false, val));
74 }
75 
76 // Create a menu node with an associated value and description_.
77 void SVMenuNode::AddChild(const char *txt, int command_event, const char *val, const char *desc) {
78  this->AddChild(new SVMenuNode(command_event, txt, false, false, val, desc));
79 }
80 
81 // Create a flag menu node.
82 void SVMenuNode::AddChild(const char *txt, int command_event, int tv) {
83  this->AddChild(new SVMenuNode(command_event, txt, tv, true));
84 }
85 
86 // Convenience function called from the different constructors to initialize
87 // the different values of the menu node.
88 SVMenuNode::SVMenuNode(int command_event, const char *txt, int tv, bool check_box_entry,
89  const char *val, const char *desc)
90  : text_(txt), value_(val), description_(desc) {
91  cmd_event_ = command_event;
92 
93  child_ = nullptr;
94  next_ = nullptr;
95  parent_ = nullptr;
96  toggle_value_ = tv != 0;
97  is_check_box_entry_ = check_box_entry;
98 }
99 
100 // Add a child node to this menu node.
101 void SVMenuNode::AddChild(SVMenuNode *svmn) {
102  svmn->parent_ = this;
103  // No children yet.
104  if (child_ == nullptr) {
105  child_ = svmn;
106  } else {
107  SVMenuNode *cur = child_;
108  while (cur->next_ != nullptr) {
109  cur = cur->next_;
110  }
111  cur->next_ = svmn;
112  }
113 }
114 
115 // Build a menu structure for the server and send the necessary messages.
116 // Should be called on the root node. If menu_bar is true, a menu_bar menu
117 // is built (e.g. on top of the window), if it is false a popup menu is
118 // built which gets shown by right clicking on the window.
119 // Deletes itself afterwards.
120 void SVMenuNode::BuildMenu(ScrollView *sv, bool menu_bar) {
121  if ((parent_ != nullptr) && (menu_bar)) {
122  if (is_check_box_entry_) {
123  sv->MenuItem(parent_->text_.c_str(), text_.c_str(), cmd_event_, toggle_value_);
124  } else {
125  sv->MenuItem(parent_->text_.c_str(), text_.c_str(), cmd_event_);
126  }
127  } else if ((parent_ != nullptr) && (!menu_bar)) {
128  if (description_.length() > 0) {
129  sv->PopupItem(parent_->text_.c_str(), text_.c_str(), cmd_event_, value_.c_str(),
130  description_.c_str());
131  } else {
132  sv->PopupItem(parent_->text_.c_str(), text_.c_str());
133  }
134  }
135  if (child_ != nullptr) {
136  child_->BuildMenu(sv, menu_bar);
137  delete child_;
138  }
139  if (next_ != nullptr) {
140  next_->BuildMenu(sv, menu_bar);
141  delete next_;
142  }
143 }
144 
145 } // namespace tesseract
146 
147 #endif // !GRAPHICS_DISABLED
void PopupItem(const char *parent, const char *name)
Definition: scrollview.cpp:687
void MenuItem(const char *parent, const char *name)
Definition: scrollview.cpp:679
SVMenuNode * AddChild(const char *txt)
Definition: svmnode.cpp:59
void BuildMenu(ScrollView *sv, bool menu_bar=true)
Definition: svmnode.cpp:120