Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <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  text_ = NULL;
48  child_ = NULL;
49  next_ = NULL;
50  parent_ = NULL;
51  toggle_value_ = false;
52  is_check_box_entry_ = false;
53  value_ = NULL;
54  description_ = NULL;
55 }
56 
58  delete[] text_;
59 // delete[] description_;
60 }
61 
62 // Create a new sub menu node with just a caption. This is used to create
63 // nodes which act as parent nodes to other nodes (e.g. submenus).
64 SVMenuNode* SVMenuNode::AddChild(const char* txt) {
65  SVMenuNode* s = new SVMenuNode(-1, txt, false, false, NULL, NULL);
66  this->AddChild(s);
67  return s;
68 }
69 
70 // Create a "normal" menu node which is associated with a command event.
71 void SVMenuNode::AddChild(const char* txt, int command_event) {
72  this->AddChild(new SVMenuNode(command_event, txt, false, false, NULL, NULL));
73 }
74 
75 // Create a menu node with an associated value (which might be changed
76 // through the gui).
77 void SVMenuNode::AddChild(const char* txt, int command_event,
78  const char* val) {
79  this->AddChild(new SVMenuNode(command_event, txt, false, false, val, NULL));
80 }
81 
82 // Create a menu node with an associated value and description_.
83 void SVMenuNode::AddChild(const char* txt, int command_event, const char* val,
84  const char* desc) {
85  this->AddChild(new SVMenuNode(command_event, txt, false, false, val, desc));
86 }
87 
88 // Create a flag menu node.
89 void SVMenuNode::AddChild(const char* txt, int command_event, int tv) {
90  this->AddChild(new SVMenuNode(command_event, txt, tv, true, NULL, NULL));
91 }
92 
93 // Convenience function called from the different constructors to initialize
94 // the different values of the menu node.
95 SVMenuNode::SVMenuNode(int command_event, const char* txt,
96  int tv, bool check_box_entry, const char* val,
97  const char* desc) {
98  cmd_event_ = command_event;
99 
100  text_ = new char[strlen(txt) + 1];
101  strncpy(text_, txt, strlen(txt));
102  text_[strlen(txt)] = '\0';
103 
104  value_ = val;
105  description_ = desc;
106 
107  child_ = NULL;
108  next_ = NULL;
109  parent_ = NULL;
110  toggle_value_ = tv != 0;
111  is_check_box_entry_ = check_box_entry;
112 }
113 
114 // Add a child node to this menu node.
115 void SVMenuNode::AddChild(SVMenuNode* svmn) {
116  svmn->parent_ = this;
117  // No children yet.
118  if (child_ == NULL) {
119  child_ = svmn;
120  } else {
121  SVMenuNode* cur = child_;
122  while (cur->next_ != NULL) { cur = cur->next_; }
123  cur->next_ = svmn;
124  }
125 }
126 
127 // Build a menu structure for the server and send the necessary messages.
128 // Should be called on the root node. If menu_bar is true, a menu_bar menu
129 // is built (e.g. on top of the window), if it is false a popup menu is
130 // built which gets shown by right clicking on the window.
131 // Deletes itself afterwards.
132 void SVMenuNode::BuildMenu(ScrollView* sv, bool menu_bar) {
133  if ((parent_ != NULL) && (menu_bar)) {
134  if (is_check_box_entry_) {
135  sv->MenuItem(parent_->text_, text_, cmd_event_, toggle_value_);
136  } else { sv->MenuItem(parent_->text_, text_, cmd_event_); }
137  } else if ((parent_ != NULL) && (!menu_bar)) {
138  if (description_ != NULL) { sv->PopupItem(parent_->text_, text_,
139  cmd_event_, value_, description_);
140  } else { sv->PopupItem(parent_->text_, text_); }
141  }
142  if (child_ != NULL) { child_->BuildMenu(sv, menu_bar); delete child_; }
143  if (next_ != NULL) { next_->BuildMenu(sv, menu_bar); delete next_; }
144 }
145 
146 #endif // GRAPHICS_DISABLED