tesseract 4.1.1
Loading...
Searching...
No Matches
svmnode.cpp
Go to the documentation of this file.
1
2// 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 <cstring>
29#include <iostream>
30
31#include "svmnode.h"
32
33// Include automatically generated configuration file if running autoconf.
34#ifdef HAVE_CONFIG_H
35#include "config_auto.h"
36#endif
37
38#ifndef GRAPHICS_DISABLED
39
40#include "scrollview.h"
41
42// Create the empty root menu node. with just a caption. All other nodes should
43// be added to this or one of the submenus.
45 cmd_event_ = -1;
46 child_ = nullptr;
47 next_ = nullptr;
48 parent_ = nullptr;
49 toggle_value_ = false;
50 is_check_box_entry_ = false;
51}
52
54}
55
56// Create a new sub menu node with just a caption. This is used to create
57// nodes which act as parent nodes to other nodes (e.g. submenus).
59 auto* s = new SVMenuNode(-1, txt, false, false, nullptr, nullptr);
60 this->AddChild(s);
61 return s;
62}
63
64// Create a "normal" menu node which is associated with a command event.
65void SVMenuNode::AddChild(const char* txt, int command_event) {
66 this->AddChild(new SVMenuNode(command_event, txt, false, false, nullptr, nullptr));
67}
68
69// Create a menu node with an associated value (which might be changed
70// through the gui).
71void SVMenuNode::AddChild(const char* txt, int command_event,
72 const char* val) {
73 this->AddChild(new SVMenuNode(command_event, txt, false, false, val, nullptr));
74}
75
76// Create a menu node with an associated value and description_.
77void SVMenuNode::AddChild(const char* txt, int command_event, const char* val,
78 const char* desc) {
79 this->AddChild(new SVMenuNode(command_event, txt, false, false, val, desc));
80}
81
82// Create a flag menu node.
83void SVMenuNode::AddChild(const char* txt, int command_event, int tv) {
84 this->AddChild(new SVMenuNode(command_event, txt, tv, true, nullptr, nullptr));
85}
86
87// Convenience function called from the different constructors to initialize
88// the different values of the menu node.
89SVMenuNode::SVMenuNode(int command_event, const char* txt,
90 int tv, bool check_box_entry, const char* val,
91 const char* desc)
92 : text_(txt), value_(val), description_(desc) {
93 cmd_event_ = command_event;
94
95 child_ = nullptr;
96 next_ = nullptr;
97 parent_ = nullptr;
98 toggle_value_ = tv != 0;
99 is_check_box_entry_ = check_box_entry;
100}
101
102// Add a child node to this menu node.
104 svmn->parent_ = this;
105 // No children yet.
106 if (child_ == nullptr) {
107 child_ = svmn;
108 } else {
109 SVMenuNode* cur = child_;
110 while (cur->next_ != nullptr) { cur = cur->next_; }
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.
120void SVMenuNode::BuildMenu(ScrollView* sv, bool menu_bar) {
121 if ((parent_ != nullptr) && (menu_bar)) {
122 if (is_check_box_entry_) {
123 sv->MenuItem(parent_->text_.string(), text_.string(), cmd_event_,
124 toggle_value_);
125 } else {
126 sv->MenuItem(parent_->text_.string(), text_.string(), cmd_event_); }
127 } else if ((parent_ != nullptr) && (!menu_bar)) {
128 if (description_.length() > 0) {
129 sv->PopupItem(parent_->text_.string(), text_.string(), cmd_event_,
130 value_.string(), description_.string());
131 } else {
132 sv->PopupItem(parent_->text_.string(), text_.string());
133 }
134 }
135 if (child_ != nullptr) {
136 child_->BuildMenu(sv, menu_bar); delete child_;
137 }
138 if (next_ != nullptr) {
139 next_->BuildMenu(sv, menu_bar); delete next_;
140 }
141}
142
143#endif // GRAPHICS_DISABLED
int32_t length() const
Definition: strngs.cpp:189
const char * string() const
Definition: strngs.cpp:194
void MenuItem(const char *parent, const char *name)
Definition: scrollview.cpp:680
void PopupItem(const char *parent, const char *name)
Definition: scrollview.cpp:686
~SVMenuNode()
Definition: svmnode.cpp:53
SVMenuNode * AddChild(const char *txt)
Definition: svmnode.cpp:58
void BuildMenu(ScrollView *sv, bool menu_bar=true)
Definition: svmnode.cpp:120
SVMenuNode()
Definition: svmnode.cpp:44