tesseract 3.04.01

viewer/svmnode.cpp

Go to the documentation of this file.
00001 
00002 // File:        svmnode.cpp
00003 // description_: ScrollView Menu Node
00004 // Author:      Joern Wanke
00005 // Created:     Thu Nov 29 2007
00006 //
00007 // (C) Copyright 2007, Google Inc.
00008 // Licensed under the Apache License, Version 2.0 (the "License");
00009 // you may not use this file except in compliance with the License.
00010 // You may obtain a copy of the License at
00011 // http://www.apache.org/licenses/LICENSE-2.0
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 //
00019 //
00020 // A SVMenuNode is an entity which contains the mapping from a menu entry on
00021 // the server side to the corresponding associated commands on the client.
00022 // It is designed to be a tree structure with a root node, which can then be
00023 // used to generate the appropriate messages to the server to display the
00024 // menu structure there.
00025 // A SVMenuNode can both be used in the context_ of popup menus as well as
00026 // menu bars.
00027 
00028 #include <string.h>
00029 #include <iostream>
00030 #include <cstring>
00031 
00032 #include "svmnode.h"
00033 
00034 // Include automatically generated configuration file if running autoconf.
00035 #ifdef HAVE_CONFIG_H
00036 #include "config_auto.h"
00037 #endif
00038 
00039 #ifndef GRAPHICS_DISABLED
00040 
00041 #include "scrollview.h"
00042 
00043 // Create the empty root menu node. with just a caption. All other nodes should
00044 // be added to this or one of the submenus.
00045 SVMenuNode::SVMenuNode() {
00046   cmd_event_ = -1;
00047   child_ = NULL;
00048   next_ = NULL;
00049   parent_ = NULL;
00050   toggle_value_ = false;
00051   is_check_box_entry_ = false;
00052 }
00053 
00054 SVMenuNode::~SVMenuNode() {
00055 }
00056 
00057 // Create a new sub menu node with just a caption.  This is used to create
00058 // nodes which act as parent nodes to other nodes (e.g. submenus).
00059 SVMenuNode* SVMenuNode::AddChild(const char* txt) {
00060   SVMenuNode* s = new SVMenuNode(-1, txt, false, false, NULL, NULL);
00061   this->AddChild(s);
00062   return s;
00063 }
00064 
00065 // Create a "normal" menu node which is associated with a command event.
00066 void SVMenuNode::AddChild(const char* txt, int command_event) {
00067   this->AddChild(new SVMenuNode(command_event, txt, false, false, NULL, NULL));
00068 }
00069 
00070 // Create a menu node with an associated value (which might be changed
00071 // through the gui).
00072 void SVMenuNode::AddChild(const char* txt, int command_event,
00073                           const char* val) {
00074   this->AddChild(new SVMenuNode(command_event, txt, false, false, val, NULL));
00075 }
00076 
00077 // Create a menu node with an associated value and description_.
00078 void SVMenuNode::AddChild(const char* txt, int command_event, const char* val,
00079                           const char* desc) {
00080   this->AddChild(new SVMenuNode(command_event, txt, false, false, val, desc));
00081 }
00082 
00083 // Create a flag menu node.
00084 void SVMenuNode::AddChild(const char* txt, int command_event, int tv) {
00085   this->AddChild(new SVMenuNode(command_event, txt, tv, true, NULL, NULL));
00086 }
00087 
00088 // Convenience function called from the different constructors to initialize
00089 // the different values of the menu node.
00090 SVMenuNode::SVMenuNode(int command_event, const char* txt,
00091                        int tv, bool check_box_entry, const char* val,
00092                        const char* desc)
00093   : text_(txt), value_(val), description_(desc) {
00094   cmd_event_ = command_event;
00095 
00096   child_ = NULL;
00097   next_ = NULL;
00098   parent_ = NULL;
00099   toggle_value_ = tv != 0;
00100   is_check_box_entry_ = check_box_entry;
00101 }
00102 
00103 // Add a child node to this menu node.
00104 void SVMenuNode::AddChild(SVMenuNode* svmn) {
00105   svmn->parent_ = this;
00106   // No children yet.
00107   if (child_ == NULL) {
00108     child_ = svmn;
00109   } else {
00110     SVMenuNode* cur = child_;
00111     while (cur->next_ != NULL) { cur = cur->next_; }
00112     cur->next_ = svmn;
00113   }
00114 }
00115 
00116 // Build a menu structure for the server and send the necessary messages.
00117 // Should be called on the root node. If menu_bar is true, a menu_bar menu
00118 // is built (e.g. on top of the window), if it is false a popup menu is
00119 // built which gets shown by right clicking on the window.
00120 // Deletes itself afterwards.
00121 void SVMenuNode::BuildMenu(ScrollView* sv, bool menu_bar) {
00122   if ((parent_ != NULL) && (menu_bar)) {
00123     if (is_check_box_entry_) {
00124       sv->MenuItem(parent_->text_.string(), text_.string(), cmd_event_,
00125                    toggle_value_);
00126     } else {
00127       sv->MenuItem(parent_->text_.string(), text_.string(), cmd_event_); }
00128   } else if ((parent_ != NULL) && (!menu_bar)) {
00129     if (description_.length() > 0) {
00130       sv->PopupItem(parent_->text_.string(), text_.string(), cmd_event_,
00131                     value_.string(), description_.string());
00132       } else {
00133       sv->PopupItem(parent_->text_.string(), text_.string());
00134     }
00135   }
00136   if (child_ != NULL) {
00137     child_->BuildMenu(sv, menu_bar); delete child_;
00138   }
00139   if (next_ != NULL) {
00140     next_->BuildMenu(sv, menu_bar); delete next_;
00141   }
00142 }
00143 
00144 #endif  // GRAPHICS_DISABLED
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines