Tesseract  3.02
tesseract-ocr/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   text_ = NULL;
00048   child_ = NULL;
00049   next_ = NULL;
00050   parent_ = NULL;
00051   toggle_value_ = false;
00052   is_check_box_entry_ = false;
00053   value_ = NULL;
00054   description_ = NULL;
00055 }
00056 
00057 SVMenuNode::~SVMenuNode() {
00058   delete[] text_;
00059 //  delete[] description_;
00060 }
00061 
00062 // Create a new sub menu node with just a caption.  This is used to create
00063 // nodes which act as parent nodes to other nodes (e.g. submenus).
00064 SVMenuNode* SVMenuNode::AddChild(const char* txt) {
00065   SVMenuNode* s = new SVMenuNode(-1, txt, false, false, NULL, NULL);
00066   this->AddChild(s);
00067   return s;
00068 }
00069 
00070 // Create a "normal" menu node which is associated with a command event.
00071 void SVMenuNode::AddChild(const char* txt, int command_event) {
00072   this->AddChild(new SVMenuNode(command_event, txt, false, false, NULL, NULL));
00073 }
00074 
00075 // Create a menu node with an associated value (which might be changed
00076 // through the gui).
00077 void SVMenuNode::AddChild(const char* txt, int command_event,
00078                           const char* val) {
00079   this->AddChild(new SVMenuNode(command_event, txt, false, false, val, NULL));
00080 }
00081 
00082 // Create a menu node with an associated value and description_.
00083 void SVMenuNode::AddChild(const char* txt, int command_event, const char* val,
00084                           const char* desc) {
00085   this->AddChild(new SVMenuNode(command_event, txt, false, false, val, desc));
00086 }
00087 
00088 // Create a flag menu node.
00089 void SVMenuNode::AddChild(const char* txt, int command_event, int tv) {
00090   this->AddChild(new SVMenuNode(command_event, txt, tv, true, NULL, NULL));
00091 }
00092 
00093 // Convenience function called from the different constructors to initialize
00094 // the different values of the menu node.
00095 SVMenuNode::SVMenuNode(int command_event, const char* txt,
00096                        int tv, bool check_box_entry, const char* val,
00097                        const char* desc) {
00098   cmd_event_ = command_event;
00099 
00100   text_ = new char[strlen(txt) + 1];
00101   strncpy(text_, txt, strlen(txt));
00102   text_[strlen(txt)] = '\0';
00103 
00104   value_ = val;
00105   description_ = desc;
00106 
00107   child_ = NULL;
00108   next_ = NULL;
00109   parent_ = NULL;
00110   toggle_value_ = tv != 0;
00111   is_check_box_entry_ = check_box_entry;
00112 }
00113 
00114 // Add a child node to this menu node.
00115 void SVMenuNode::AddChild(SVMenuNode* svmn) {
00116   svmn->parent_ = this;
00117   // No children yet.
00118   if (child_ == NULL) {
00119     child_ = svmn;
00120   } else {
00121     SVMenuNode* cur = child_;
00122     while (cur->next_ != NULL) { cur = cur->next_; }
00123     cur->next_ = svmn;
00124   }
00125 }
00126 
00127 // Build a menu structure for the server and send the necessary messages.
00128 // Should be called on the root node. If menu_bar is true, a menu_bar menu
00129 // is built (e.g. on top of the window), if it is false a popup menu is
00130 // built which gets shown by right clicking on the window.
00131 // Deletes itself afterwards.
00132 void SVMenuNode::BuildMenu(ScrollView* sv, bool menu_bar) {
00133   if ((parent_ != NULL) && (menu_bar)) {
00134     if (is_check_box_entry_) {
00135       sv->MenuItem(parent_->text_, text_, cmd_event_, toggle_value_);
00136     } else { sv->MenuItem(parent_->text_, text_, cmd_event_); }
00137   } else if ((parent_ != NULL) && (!menu_bar)) {
00138     if (description_ != NULL) { sv->PopupItem(parent_->text_, text_,
00139                                              cmd_event_, value_, description_);
00140       } else { sv->PopupItem(parent_->text_, text_); }
00141   }
00142   if (child_ != NULL) { child_->BuildMenu(sv, menu_bar); delete child_; }
00143   if (next_ != NULL) { next_->BuildMenu(sv, menu_bar); delete next_; }
00144 }
00145 
00146 #endif  // GRAPHICS_DISABLED