tcMenu
BaseDialog.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef TCMENU_BASE_DIALOG_H_
7 #define TCMENU_BASE_DIALOG_H_
8 
18 #define FIRST_DEFAULT_BUTTON 0
19 #define SECOND_DEFAULT_BUTTON 1
20 #define CUSTOM_DIALOG_BUTTON_START 2
21 
25 enum ButtonType: uint8_t {
26  BTNTYPE_OK, BTNTYPE_ACCEPT, BTNTYPE_CANCEL, BTNTYPE_CLOSE, BTNTYPE_NONE,
27  BTNTYPE_CUSTOM0 = 15, BTNTYPE_CUSTOM1, BTNTYPE_CUSTOM2, BTNTYPE_CUSTOM3,
28  BTNTYPE_CUSTOM4, BTNTYPE_CUSTOM5, BTNTYPE_CUSTOM6, BTNTYPE_CUSTOM7 };
29 
30 
31 class BaseDialog;
37 typedef void (*CompletedHandlerFn)(ButtonType buttonPressed, void* yourData);
38 
39 #define DLG_FLAG_SMALLDISPLAY 0
40 #define DLG_FLAG_INUSE 1
41 #define DLG_FLAG_CAN_SEND_REMOTE 2
42 #define DLG_FLAG_MENUITEM_BASED 3
43 #define DLG_FLAG_NEEDS_RENDERING 4
44 #define DLG_FLAG_USING_OO_CONTROLLER 5
45 #define DLG_FLAG_REMOTE_0 8
46 #define DLG_FLAG_REMOTE_1 9
47 #define DLG_FLAG_REMOTE_2 10
48 #define DLG_FLAG_REMOTE_3 11
49 #define DLG_FLAG_REMOTE_4 12
50 
51 #define DLG_FLAG_REMOTE_MASK 0xff00
52 
53 #define DLG_VISIBLE 'S'
54 #define DLG_HIDDEN 'H'
55 #define DLG_ACTION 'A'
56 
57 class TagValueRemoteConnector; // forward reference
58 
66 public:
75  virtual void initialiseAndGetHeader(BaseDialog* dialog, char* buffer, size_t bufferSize)=0;
80  virtual void dialogDismissed(ButtonType buttonType)=0;
81 
87  virtual bool dialogButtonPressed(int buttonNum)=0;
88 
95  virtual void copyCustomButtonText(int buttonNumber, char* buffer, size_t bufferSize)=0;
96 };
97 
105 class BaseDialog {
106 protected:
107  char header[20];
108  const char* headerPgm; // for backwards compatibility with 1.7, not used in 2.0
109  union {
110  BaseDialogController* controller;
111  CompletedHandlerFn completedHandler;
112  };
113  void* userData;
114  ButtonType button1;
115  ButtonType button2;
116  uint8_t lastBtnVal;
117  uint16_t flags;
118  MenuRedrawState needsDrawing;
119 public:
123  BaseDialog();
124  virtual ~BaseDialog() = default;
125 
133  void setButtons(ButtonType btn1, ButtonType btn2, int defVal = 0);
134 
139  void show(const char* headerPgm, bool allowRemote, CompletedHandlerFn completedHandler = NULL);
140 
145  void showRam(const char* headerRam, bool allowRemote, CompletedHandlerFn completedHandler = NULL);
146 
153  void showController(bool allowRemote, BaseDialogController* controller);
154 
159  void setUserData(void *data) { this->userData = data; }
160 
165  virtual void copyIntoBuffer(const char* sz);
166 
168  virtual void internalSetVisible(bool visible);
169 
173  void hide();
174 
176  bool isUsingOOController() { return bitRead(flags, DLG_FLAG_USING_OO_CONTROLLER); }
177 
179  bool isInUse() {return bitRead(flags, DLG_FLAG_INUSE);}
180 
182  bool isCompressedMode() {return bitRead(flags, DLG_FLAG_SMALLDISPLAY);}
183 
190  void dialogRendering(unsigned int currentValue, bool userClicked);
191 
192  bool isMenuItemBased() { return bitRead(flags, DLG_FLAG_MENUITEM_BASED); }
193  bool isRenderNeeded() { return bitRead(flags, DLG_FLAG_NEEDS_RENDERING); }
194  bool isRemoteUpdateNeeded(int remote) { return bitRead(flags, remote + DLG_FLAG_REMOTE_0) && bitRead(flags, DLG_FLAG_CAN_SEND_REMOTE); }
195  void setRemoteUpdateNeeded(int remote, bool b) { bitWrite(flags, remote + DLG_FLAG_REMOTE_0, b); }
196  void setRemoteUpdateNeededAll() { flags |= DLG_FLAG_REMOTE_MASK; }
197  void clearRemoteUpdateNeededAll() { flags &= ~DLG_FLAG_REMOTE_MASK; }
198  void setRemoteAllowed(bool allowed) {bitWrite(flags, DLG_FLAG_CAN_SEND_REMOTE, allowed); }
199 
200  void encodeMessage(TagValueRemoteConnector* remote);
201  void remoteAction(ButtonType type);
202 
211  bool copyButtonText(char* data, int buttonNum, int currentValue, bool isActive);
212 
213  bool copyButtonText(char* data, int buttonNum, int currentValue) {
214  return copyButtonText(data, buttonNum, currentValue, buttonNum == currentValue);
215  }
216 
222  void actionPerformed(int btnNum);
223 protected:
227  void setInUse(bool b) {bitWrite(flags, DLG_FLAG_INUSE, b);}
228 
234  virtual void internalRender(int currentValue)=0;
235 
240  void setNeedsDrawing(bool b) {
241  if(b && needsDrawing == MENUDRAW_COMPLETE_REDRAW) return;
242  needsDrawing = b ? MENUDRAW_EDITOR_CHANGE : MENUDRAW_NO_CHANGE;
243  }
244 
250  ButtonType findActiveBtn(unsigned int currentValue);
251 
252  void internalShow(bool allowRemote);
253 };
254 
260 private:
261  uint8_t buttonNumber;
262 public:
271  LocalDialogButtonMenuItem(RuntimeRenderingFn renderFn, int id, int btnNum, MenuItem* next)
272  : RuntimeMenuItem(MENUTYPE_DIALOG_BUTTON, id, renderFn, 0, 1, next) {
273  setLocalOnly(true);
274  buttonNumber = btnNum;
275  }
276  int getButtonNumber() { return buttonNumber; }
277 };
278 
279 #define FIRST_USER_BUTTON_NUM 2
280 
287 class MenuBasedDialog : public BaseDialog {
288 private:
289  BackMenuItem backItem;
290  TextMenuItem bufferItem;
291  LocalDialogButtonMenuItem btn1Item;
292  LocalDialogButtonMenuItem btn2Item;
293  int addedMenuItems;
294 public:
295  MenuBasedDialog();
296  ~MenuBasedDialog() override = default;
297 
298  void internalSetVisible(bool visible) override;
299  void copyIntoBuffer(const char *sz) override;
300 
301  void insertMenuItem(MenuItem* item);
302 
303  void copyHeader(char *buffer, int bufferSize);
304 
305 protected:
307  void internalRender(int currentValue) override {}
308 
309  void resetDialogFields();
310 };
311 
312 #endif //TCMENU_BASE_DIALOG_H_
void setInUse(bool b)
Definition: BaseDialog.h:227
Definition: MenuItems.h:324
void setNeedsDrawing(bool b)
Definition: BaseDialog.h:240
Definition: BaseDialog.h:259
virtual void dialogDismissed(ButtonType buttonType)=0
MenuRedrawState
Definition: BaseRenderers.h:232
Definition: RuntimeMenuItem.h:110
bool isInUse()
Definition: BaseDialog.h:179
LocalDialogButtonMenuItem(RuntimeRenderingFn renderFn, int id, int btnNum, MenuItem *next)
Definition: BaseDialog.h:271
bool isCompressedMode()
Definition: BaseDialog.h:182
void setUserData(void *data)
Definition: BaseDialog.h:159
void internalRender(int currentValue) override
Definition: BaseDialog.h:307
virtual bool dialogButtonPressed(int buttonNum)=0
Definition: BaseDialog.h:105
ButtonType
Definition: BaseDialog.h:25
virtual void copyCustomButtonText(int buttonNumber, char *buffer, size_t bufferSize)=0
Definition: BaseDialog.h:65
bool isUsingOOController()
Definition: BaseDialog.h:176
Definition: RuntimeMenuItem.h:73
Definition: BaseDialog.h:287
void(* CompletedHandlerFn)(ButtonType buttonPressed, void *yourData)
Definition: BaseDialog.h:37
Definition: RuntimeMenuItem.h:235
Definition: RemoteConnector.h:158
Definition: MenuItems.h:263
virtual void initialiseAndGetHeader(BaseDialog *dialog, char *buffer, size_t bufferSize)=0
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:313