tcMenu
BaseDialog.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
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 
170  virtual char* getBufferData();
171 
175  void hide();
176 
178  bool isUsingOOController() { return bitRead(flags, DLG_FLAG_USING_OO_CONTROLLER); }
179 
181  bool isInUse() {return bitRead(flags, DLG_FLAG_INUSE);}
182 
184  bool isCompressedMode() {return bitRead(flags, DLG_FLAG_SMALLDISPLAY);}
185 
192  void dialogRendering(unsigned int currentValue, bool userClicked);
193 
194  bool isMenuItemBased() { return bitRead(flags, DLG_FLAG_MENUITEM_BASED); }
195  bool isRenderNeeded() { return bitRead(flags, DLG_FLAG_NEEDS_RENDERING); }
196  bool isRemoteUpdateNeeded(int remote) { return bitRead(flags, remote + DLG_FLAG_REMOTE_0) && bitRead(flags, DLG_FLAG_CAN_SEND_REMOTE); }
197  void setRemoteUpdateNeeded(int remote, bool b) { bitWrite(flags, remote + DLG_FLAG_REMOTE_0, b); }
198  void setRemoteUpdateNeededAll() { flags |= DLG_FLAG_REMOTE_MASK; }
199  void clearRemoteUpdateNeededAll() { flags &= ~DLG_FLAG_REMOTE_MASK; }
200  void setRemoteAllowed(bool allowed) {bitWrite(flags, DLG_FLAG_CAN_SEND_REMOTE, allowed); }
201 
202  void encodeMessage(TagValueRemoteConnector* remote);
203  void remoteAction(ButtonType type);
204 
213  bool copyButtonText(char* data, int buttonNum, int currentValue, bool isActive);
214 
215  bool copyButtonText(char* data, int buttonNum, int currentValue) {
216  return copyButtonText(data, buttonNum, currentValue, buttonNum == currentValue);
217  }
218 
224  void actionPerformed(int btnNum);
225 protected:
229  void setInUse(bool b) {bitWrite(flags, DLG_FLAG_INUSE, b);}
230 
236  virtual void internalRender(int currentValue)=0;
237 
242  void setNeedsDrawing(bool b) {
243  if(b && needsDrawing == MENUDRAW_COMPLETE_REDRAW) return;
244  needsDrawing = b ? MENUDRAW_EDITOR_CHANGE : MENUDRAW_NO_CHANGE;
245  }
246 
252  ButtonType findActiveBtn(unsigned int currentValue);
253 
254  void internalShow(bool allowRemote);
255 };
256 
265 int dialogButtonRenderFn(RuntimeMenuItem* item, uint8_t /*row*/, RenderFnMode mode, char* buffer, int bufferSize);
266 
272 private:
273  uint8_t buttonNumber;
274 public:
283  LocalDialogButtonMenuItem(RuntimeRenderingFn renderFn, menuid_t id, int btnNum, MenuItem* next)
284  : RuntimeMenuItem(MENUTYPE_DIALOG_BUTTON, id, renderFn, 0, 1, next), buttonNumber(btnNum) {
285  setLocalOnly(true);
286  }
287  int getButtonNumber() { return buttonNumber; }
288 };
289 
290 #define FIRST_USER_BUTTON_NUM 2
291 
298 class MenuBasedDialog : public BaseDialog {
299 private:
300  BackMenuItem backItem;
301  TextMenuItem bufferItem;
302  LocalDialogButtonMenuItem btn1Item;
303  LocalDialogButtonMenuItem btn2Item;
304  int addedMenuItems;
305 public:
306  MenuBasedDialog();
307  ~MenuBasedDialog() override = default;
308 
309  uint16_t getBackMenuItemId() { return backItem.getId(); }
310 
311  void internalSetVisible(bool visible) override;
312  void copyIntoBuffer(const char *sz) override;
313 
314  void insertMenuItem(MenuItem* item);
315 
316  void copyHeader(char *buffer, int bufferSize);
317 
318  char* getBufferData() override { return const_cast<char *>(bufferItem.getTextValue()); }
319 
320  TextMenuItem* getBufferMenuItem() { return &bufferItem; }
321 
322 protected:
324  void internalRender(int currentValue) override {}
325 
326  void resetDialogFields();
327 };
328 
331 
339 
340 #endif //TCMENU_BASE_DIALOG_H_
void withMenuDialogIfAvailable(DialogInitialiser dlgFn)
Definition: BaseDialog.cpp:326
RenderFnMode
Definition: MenuItems.h:294
void setInUse(bool b)
Definition: BaseDialog.h:229
Definition: MenuItems.h:336
void setNeedsDrawing(bool b)
Definition: BaseDialog.h:242
void(* DialogInitialiser)(MenuBasedDialog *)
Definition: BaseDialog.h:330
Definition: BaseDialog.h:271
virtual void dialogDismissed(ButtonType buttonType)=0
MenuRedrawState
Definition: BaseRenderers.h:235
Definition: RuntimeMenuItem.h:111
bool isInUse()
Definition: BaseDialog.h:181
menuid_t getId() const
Definition: MenuItems.cpp:75
const char * getTextValue() const
Definition: RuntimeMenuItem.h:264
bool isCompressedMode()
Definition: BaseDialog.h:184
void setUserData(void *data)
Definition: BaseDialog.h:159
void internalRender(int currentValue) override
Definition: BaseDialog.h:324
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:178
int dialogButtonRenderFn(RuntimeMenuItem *item, uint8_t, RenderFnMode mode, char *buffer, int bufferSize)
Definition: BaseDialog.cpp:237
Definition: RuntimeMenuItem.h:73
Definition: BaseDialog.h:298
LocalDialogButtonMenuItem(RuntimeRenderingFn renderFn, menuid_t id, int btnNum, MenuItem *next)
Definition: BaseDialog.h:283
void(* CompletedHandlerFn)(ButtonType buttonPressed, void *yourData)
Definition: BaseDialog.h:37
Definition: RuntimeMenuItem.h:236
Definition: RemoteConnector.h:182
Definition: MenuItems.h:275
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:325