1 // $Id: setmenuitem.h,v 1.30 2011/03/07 06:08:51 bobgian Exp $
2 
3 /*
4   Copyright 2002  Peter Beerli, Mary Kuhner, Jon Yamato and Joseph Felsenstein
5 
6   This software is distributed free of charge for non-commercial use
7   and is copyrighted.  Of course, we do not guarantee that the software
8   works, and are not responsible for any damage you may cause or have.
9 */
10 
11 // Menuitem.h
12 // is line in the menu and consists of a (key, text, handlerobject)
13 //
14 // Peter Beerli
15 
16 #ifndef SETMENUITEM_H
17 #define SETMENUITEM_H
18 
19 #include <string>
20 #include "dialogrepeat.h"
21 #include "errhandling.h"
22 #include "menuitem.h"
23 #include "menuinteraction.h"
24 #include "stringx.h"
25 #include "ui_interface.h"
26 #include "ui_constants.h"
27 #include "vectorx.h"
28 #include "menutypedefs.h"
29 
30 class SetDialog : public DialogRepeat
31 {
32   private:
33     SetDialog(); //undefined
34   protected:
35     UIInterface & ui;
36     std::string menuKey;
37     const UIId id;
38     //
39     bool haveError;
40     std::string currError;
41     //
getText()42     std::string getText()
43     { return ui.doGetDescription(menuKey,id);};
stuffValIntoUI(std::string val)44     virtual void stuffValIntoUI(std::string val)
45     { ui.doSet(menuKey,val,id);};
46   public:
SetDialog(UIInterface & myui,std::string myMenuKey,UIId myId)47     SetDialog(UIInterface & myui, std::string myMenuKey, UIId myId)
48         : ui(myui) , menuKey(myMenuKey), id(myId) {};
~SetDialog()49     virtual ~SetDialog() {};
maxTries()50     virtual long maxTries()
51     { return 3;};
beforeLoopOutputString()52     virtual std::string beforeLoopOutputString()
53     { return "";};
54     virtual std::string inLoopOutputString();
inLoopFailureOutputString()55     virtual std::string inLoopFailureOutputString()
56     {
57         if (haveError)
58         {
59             return currError;
60         }
61         else
62         {
63             implementation_error e("Unknown menu error");
64             throw e;
65         }
66     };
afterLoopSuccessOutputString()67     virtual std::string afterLoopSuccessOutputString()
68     { return "";};
afterLoopFailureOutputString()69     virtual std::string afterLoopFailureOutputString()
70     { return string("Unable to accept your input\n")+
71             "Leaving \""+getText()+"\" unchanged\n";};
handleInput(std::string input)72     virtual bool handleInput(std::string input)
73     {   haveError = false;
74         try
75         {
76             stuffValIntoUI(input);
77         }
78         catch (data_error& e)
79         {
80             currError = e.what();
81             haveError = true;
82             return false;
83         }
84         return true;
85     };
doFailure()86     virtual void doFailure()
87     {};
88 
89   private:
getMin()90     std::string getMin() { return ui.doGetMin(menuKey,id);};
getMax()91     std::string getMax() { return ui.doGetMax(menuKey,id);};
92 };
93 
94 class SetMenuItem : public KeyedMenuItem
95 {
96   private:
97     SetMenuItem(); //undefined
98   protected:
99     UIInterface & ui;
100     std::string menuKey;
101     virtual UIId GetId() = 0;
102   public:
103     SetMenuItem(std::string myKey, UIInterface & myui, std::string mk );
104     SetMenuItem(std::string myKey, UIInterface & myui, std::string mk, UIId myId);
105     virtual ~SetMenuItem();
106     // ---- MenuItem methods
107     virtual std::string GetText();
108     virtual std::string GetVariableText();
109     virtual MenuInteraction_ptr GetHandler(std::string input);
110     virtual bool IsVisible();
111     virtual bool IsConsistent();
112 };
113 
114 class SetMenuItemNoId : public SetMenuItem
115 {
116   private:
117     SetMenuItemNoId(); //undefined
118   protected:
119     virtual UIId GetId();
120   public:
121     SetMenuItemNoId(std::string myKey, UIInterface & myui, std::string myMenuKey);
122     virtual ~SetMenuItemNoId();
123 };
124 
125 class SetMenuItemId : public SetMenuItem
126 {
127   private:
128     SetMenuItemId(); //undefined
129   protected:
130     UIId id;
131     virtual UIId GetId();
132   public:
133     SetMenuItemId(std::string myKey, UIInterface & myui, std::string myMenuKey, UIId myId);
134     virtual ~SetMenuItemId();
135 };
136 
137 class NewToggleHandler : public MenuInteraction
138 {
139   private:
140     NewToggleHandler(); //undefined
141   protected:
142     UIInterface & ui;
143     std::string menuKey;
144     UIId id;
145   public:
NewToggleHandler(UIInterface & myui,std::string myMenuKey,UIId myId)146     NewToggleHandler(UIInterface & myui, std::string myMenuKey, UIId myId)
147         : ui(myui),menuKey(myMenuKey),id(myId) {};
~NewToggleHandler()148     ~NewToggleHandler() {};
InvokeMe(Display & display)149     virtual menu_return_type InvokeMe(Display & display)
150     { ui.doToggle(menuKey,id); return menu_REDISPLAY;};
151 };
152 
153 class ToggleMenuItemNoId : public SetMenuItemNoId
154 {
155   private:
156     ToggleMenuItemNoId(); //undefined
157   public:
ToggleMenuItemNoId(std::string myKey,UIInterface & myui,std::string myMenuKey)158     ToggleMenuItemNoId(std::string myKey, UIInterface & myui, std::string myMenuKey )
159         : SetMenuItemNoId(myKey,myui,myMenuKey) {};
~ToggleMenuItemNoId()160     ~ToggleMenuItemNoId() {};
GetHandler(std::string input)161     virtual MenuInteraction_ptr GetHandler(std::string input)
162     { return MenuInteraction_ptr(new NewToggleHandler(ui,menuKey,GetId()));};
163 };
164 
165 class ToggleMenuItemId : public SetMenuItemId
166 {
167   public:
ToggleMenuItemId(std::string myKey,UIInterface & myui,std::string myMenuKey,UIId myId)168     ToggleMenuItemId(std::string myKey, UIInterface & myui, std::string myMenuKey, UIId myId)
169         : SetMenuItemId(myKey,myui,myMenuKey,myId) {};
~ToggleMenuItemId()170     ~ToggleMenuItemId() {};
GetHandler(std::string input)171     virtual MenuInteraction_ptr GetHandler(std::string input)
172     { return MenuInteraction_ptr(new NewToggleHandler(ui,menuKey,GetId()));};
173 };
174 
175 class MenuDisplayGroupBaseImplementation : public MenuDisplayGroup
176 {
177   private:
178     MenuDisplayGroupBaseImplementation(); //undefined
179   protected:
180     UIInterface & ui;
181     std::string menuKey;
182     virtual MenuInteraction_ptr MakeOneHandler(UIId id) = 0;
183   public:
184     MenuDisplayGroupBaseImplementation(UIInterface & myui, std::string myMenuKey);
185     virtual ~MenuDisplayGroupBaseImplementation();
186     virtual std::string GetKey(UIId id);
187     virtual std::string GetText(UIId id);
188     virtual std::string GetVariableText(UIId id);
189     virtual bool Handles(std::string input);
190     virtual MenuInteraction_ptr GetHandler(std::string input);
191 };
192 
193 class SetMenuItemGroup : public MenuDisplayGroupBaseImplementation
194 {
195   private:
196     SetMenuItemGroup(); //undefined
197   protected:
198     virtual MenuInteraction_ptr MakeOneHandler(UIId id);
199   public:
200     SetMenuItemGroup(UIInterface & myui, std::string myMenuKey);
201     virtual ~SetMenuItemGroup();
202 };
203 
204 class ToggleMenuItemGroup : public MenuDisplayGroupBaseImplementation
205 {
206   private:
207     ToggleMenuItemGroup(); //undefined
208   protected:
209     virtual MenuInteraction_ptr MakeOneHandler(UIId id);
210   public:
211     ToggleMenuItemGroup(UIInterface & myui, std::string myMenuKey);
212     virtual ~ToggleMenuItemGroup();
213 };
214 
215 #endif  // SETMENUITEM_H
216 
217 //____________________________________________________________________________________
218