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 //
18 ///////////////////////////////////////////////////////////////////////
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 #include "svmnode.h"
28
29 #ifndef GRAPHICS_DISABLED
30
31 #include <string.h>
32 #include <iostream>
33 #include <cstring>
34
35 #include "scrollview.h"
36
37 // Create the empty root menu node. with just a caption. All other nodes should
38 // be added to this or one of the submenus.
SVMenuNode()39 SVMenuNode::SVMenuNode() {
40 cmd_event_ = -1;
41 text_ = NULL;
42 child_ = NULL;
43 next_ = NULL;
44 parent_ = NULL;
45 toggle_value_ = false;
46 is_check_box_entry_ = false;
47 value_ = NULL;
48 description_ = NULL;
49 }
50
~SVMenuNode()51 SVMenuNode::~SVMenuNode() {
52 delete[] text_;
53 // delete[] description_;
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).
AddChild(const char * txt)58 SVMenuNode* SVMenuNode::AddChild(const char* txt) {
59 SVMenuNode* s = new SVMenuNode(-1, txt, false, false, NULL, NULL);
60 this->AddChild(s);
61 return s;
62 }
63
64 // Create a "normal" menu node which is associated with a command event.
AddChild(const char * txt,int command_event)65 void SVMenuNode::AddChild(const char* txt, int command_event) {
66 this->AddChild(new SVMenuNode(command_event, txt, false, false, NULL, NULL));
67 }
68
69 // Create a menu node with an associated value (which might be changed
70 // through the gui).
AddChild(const char * txt,int command_event,const char * val)71 void SVMenuNode::AddChild(const char* txt, int command_event,
72 const char* val) {
73 this->AddChild(new SVMenuNode(command_event, txt, false, false, val, NULL));
74 }
75
76 // Create a menu node with an associated value and description_.
AddChild(const char * txt,int command_event,const char * val,const char * desc)77 void 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.
AddChild(const char * txt,int command_event,int tv)83 void SVMenuNode::AddChild(const char* txt, int command_event, int tv) {
84 this->AddChild(new SVMenuNode(command_event, txt, tv, true, NULL, NULL));
85 }
86
87 // Convenience function called from the different constructors to initialize
88 // the different values of the menu node.
SVMenuNode(int command_event,const char * txt,int tv,bool check_box_entry,const char * val,const char * desc)89 SVMenuNode::SVMenuNode(int command_event, const char* txt,
90 int tv, bool check_box_entry, const char* val,
91 const char* desc) {
92 cmd_event_ = command_event;
93
94 text_ = new char[strlen(txt) + 1];
95 strncpy(text_, txt, strlen(txt));
96 text_[strlen(txt)] = '\0';
97
98 value_ = val;
99 description_ = desc;
100
101 child_ = NULL;
102 next_ = NULL;
103 parent_ = NULL;
104 toggle_value_ = tv != 0;
105 is_check_box_entry_ = check_box_entry;
106 }
107
108 // Add a child node to this menu node.
AddChild(SVMenuNode * svmn)109 void SVMenuNode::AddChild(SVMenuNode* svmn) {
110 svmn->parent_ = this;
111 // No children yet.
112 if (child_ == NULL) {
113 child_ = svmn;
114 } else {
115 SVMenuNode* cur = child_;
116 while (cur->next_ != NULL) { cur = cur->next_; }
117 cur->next_ = svmn;
118 }
119 }
120
121 // Build a menu structure for the server and send the necessary messages.
122 // Should be called on the root node. If menu_bar is true, a menu_bar menu
123 // is built (e.g. on top of the window), if it is false a popup menu is
124 // built which gets shown by right clicking on the window.
125 // Deletes itself afterwards.
BuildMenu(ScrollView * sv,bool menu_bar)126 void SVMenuNode::BuildMenu(ScrollView* sv, bool menu_bar) {
127 if ((parent_ != NULL) && (menu_bar)) {
128 if (is_check_box_entry_) {
129 sv->MenuItem(parent_->text_, text_, cmd_event_, toggle_value_);
130 } else { sv->MenuItem(parent_->text_, text_, cmd_event_); }
131 } else if ((parent_ != NULL) && (!menu_bar)) {
132 if (description_ != NULL) { sv->PopupItem(parent_->text_, text_,
133 cmd_event_, value_, description_);
134 } else { sv->PopupItem(parent_->text_, text_); }
135 }
136 if (child_ != NULL) { child_->BuildMenu(sv, menu_bar); delete child_; }
137 if (next_ != NULL) { next_->BuildMenu(sv, menu_bar); delete next_; }
138 }
139
140 #endif // GRAPHICS_DISABLED
141