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 
15 #define FIRST_DEFAULT_BUTTON 0
16 #define SECOND_DEFAULT_BUTTON 1
17 #define CUSTOM_DIALOG_BUTTON_START 2
18 
22 enum ButtonType: uint8_t {
23  BTNTYPE_OK, BTNTYPE_ACCEPT, BTNTYPE_CANCEL, BTNTYPE_CLOSE, BTNTYPE_NONE,
24  BTNTYPE_CUSTOM0 = 15, BTNTYPE_CUSTOM1, BTNTYPE_CUSTOM2, BTNTYPE_CUSTOM3,
25  BTNTYPE_CUSTOM4, BTNTYPE_CUSTOM5, BTNTYPE_CUSTOM6, BTNTYPE_CUSTOM7 };
26 
27 
28 class BaseDialog;
34 typedef void (*CompletedHandlerFn)(ButtonType buttonPressed, void* yourData);
35 
36 #define DLG_FLAG_SMALLDISPLAY 0
37 #define DLG_FLAG_INUSE 1
38 #define DLG_FLAG_CAN_SEND_REMOTE 2
39 #define DLG_FLAG_MENUITEM_BASED 3
40 #define DLG_FLAG_NEEDS_RENDERING 4
41 #define DLG_FLAG_USING_OO_CONTROLLER 5
42 #define DLG_FLAG_REMOTE_0 8
43 #define DLG_FLAG_REMOTE_1 9
44 #define DLG_FLAG_REMOTE_2 10
45 #define DLG_FLAG_REMOTE_3 11
46 #define DLG_FLAG_REMOTE_4 12
47 
48 #define DLG_FLAG_REMOTE_MASK 0xff00
49 
50 #define DLG_VISIBLE 'S'
51 #define DLG_HIDDEN 'H'
52 #define DLG_ACTION 'A'
53 
54 class TagValueRemoteConnector; // forward reference
55 
63 public:
72  virtual void initialiseAndGetHeader(BaseDialog* dialog, char* buffer, size_t bufferSize)=0;
77  virtual void dialogDismissed(ButtonType buttonType)=0;
78 
84  virtual bool dialogButtonPressed(int buttonNum)=0;
85 
92  virtual void copyCustomButtonText(int buttonNumber, char* buffer, size_t bufferSize)=0;
93 };
94 
102 class BaseDialog {
103 protected:
104  char header[20];
105  const char* headerPgm; // for backwards compatibility with 1.7, not used in 2.0
106  union {
107  BaseDialogController* controller;
108  CompletedHandlerFn completedHandler;
109  };
110  void* userData;
111  ButtonType button1;
112  ButtonType button2;
113  uint8_t lastBtnVal;
114  uint16_t flags;
115  MenuRedrawState needsDrawing;
116 public:
120  BaseDialog();
121  virtual ~BaseDialog() = default;
122 
130  void setButtons(ButtonType btn1, ButtonType btn2, int defVal = 0);
131 
136  void show(const char* headerPgm, bool allowRemote, CompletedHandlerFn completedHandler = NULL);
137 
142  void showRam(const char* headerRam, bool allowRemote, CompletedHandlerFn completedHandler = NULL);
143 
150  void showController(bool allowRemote, BaseDialogController* controller);
151 
156  void setUserData(void *data) { this->userData = data; }
157 
162  virtual void copyIntoBuffer(const char* sz);
163 
165  virtual void internalSetVisible(bool visible);
166 
167  virtual char* getBufferData();
168 
172  void hide();
173 
175  bool isUsingOOController() { return bitRead(flags, DLG_FLAG_USING_OO_CONTROLLER); }
176 
178  bool isInUse() {return bitRead(flags, DLG_FLAG_INUSE);}
179 
181  bool isCompressedMode() {return bitRead(flags, DLG_FLAG_SMALLDISPLAY);}
182 
189  void dialogRendering(unsigned int currentValue, bool userClicked);
190 
191  bool isMenuItemBased() { return bitRead(flags, DLG_FLAG_MENUITEM_BASED); }
192  bool isRenderNeeded() { return bitRead(flags, DLG_FLAG_NEEDS_RENDERING); }
193  bool isRemoteUpdateNeeded(int remote) { return bitRead(flags, remote + DLG_FLAG_REMOTE_0) && bitRead(flags, DLG_FLAG_CAN_SEND_REMOTE); }
194  void setRemoteUpdateNeeded(int remote, bool b) { bitWrite(flags, remote + DLG_FLAG_REMOTE_0, b); }
195  void setRemoteUpdateNeededAll() { flags |= DLG_FLAG_REMOTE_MASK; }
196  void clearRemoteUpdateNeededAll() { flags &= ~DLG_FLAG_REMOTE_MASK; }
197  void setRemoteAllowed(bool allowed) {bitWrite(flags, DLG_FLAG_CAN_SEND_REMOTE, allowed); }
198 
199  void encodeMessage(TagValueRemoteConnector* remote);
200  void remoteAction(ButtonType type);
201 
210  bool copyButtonText(char* data, int buttonNum, int currentValue, bool isActive);
211 
212  bool copyButtonText(char* data, int buttonNum, int currentValue) {
213  return copyButtonText(data, buttonNum, currentValue, buttonNum == currentValue);
214  }
215 
221  void actionPerformed(int btnNum);
222 protected:
226  void setInUse(bool b) {bitWrite(flags, DLG_FLAG_INUSE, b);}
227 
233  virtual void internalRender(int currentValue)=0;
234 
239  void setNeedsDrawing(bool b) {
240  if(b && needsDrawing == MENUDRAW_COMPLETE_REDRAW) return;
241  needsDrawing = b ? MENUDRAW_EDITOR_CHANGE : MENUDRAW_NO_CHANGE;
242  }
243 
249  ButtonType findActiveBtn(unsigned int currentValue);
250 
251  void internalShow(bool allowRemote);
252 };
253 
262 int dialogButtonRenderFn(RuntimeMenuItem* item, uint8_t /*row*/, RenderFnMode mode, char* buffer, int bufferSize);
263 
269 private:
270  uint8_t buttonNumber;
271 public:
280  LocalDialogButtonMenuItem(RuntimeRenderingFn renderFn, menuid_t id, int btnNum, MenuItem* next)
281  : RuntimeMenuItem(MENUTYPE_DIALOG_BUTTON, id, renderFn, 0, 1, next), buttonNumber(btnNum) {
282  setLocalOnly(true);
283  }
284  int getButtonNumber() { return buttonNumber; }
285 };
286 
287 #define FIRST_USER_BUTTON_NUM 2
288 
295 class MenuBasedDialog : public BaseDialog {
296 private:
297  BackMenuItem backItem;
298  TextMenuItem bufferItem;
299  LocalDialogButtonMenuItem btn1Item;
300  LocalDialogButtonMenuItem btn2Item;
301  int addedMenuItems;
302 public:
303  MenuBasedDialog();
304  ~MenuBasedDialog() override = default;
305 
306  uint16_t getBackMenuItemId() { return backItem.getId(); }
307 
308  void internalSetVisible(bool visible) override;
309  void copyIntoBuffer(const char *sz) override;
310 
311  void insertMenuItem(MenuItem* item);
312 
313  void copyHeader(char *buffer, int bufferSize);
314 
315  char* getBufferData() override { return const_cast<char *>(bufferItem.getTextValue()); }
316 
317  TextMenuItem* getBufferMenuItem() { return &bufferItem; }
318 
319 protected:
321  void internalRender(int currentValue) override {}
322 
323  void resetDialogFields();
324 };
325 
328 
336 
337 #endif //TCMENU_BASE_DIALOG_H_
void(* DialogInitialiser)(MenuBasedDialog *)
Definition: BaseDialog.h:327
void withMenuDialogIfAvailable(DialogInitialiser dlgFn)
Definition: BaseDialog.cpp:324
void(* CompletedHandlerFn)(ButtonType buttonPressed, void *yourData)
Definition: BaseDialog.h:34
ButtonType
Definition: BaseDialog.h:22
int dialogButtonRenderFn(RuntimeMenuItem *item, uint8_t, RenderFnMode mode, char *buffer, int bufferSize)
Definition: BaseDialog.cpp:238
MenuRedrawState
Definition: BaseRenderers.h:254
RenderFnMode
Definition: MenuItems.h:283
int(* RuntimeRenderingFn)(RuntimeMenuItem *item, uint8_t row, RenderFnMode mode, char *buffer, int bufferSize)
Definition: MenuItems.h:318
@ MENUTYPE_DIALOG_BUTTON
Definition: MenuItems.h:264
Definition: RuntimeMenuItem.h:116
Definition: BaseDialog.h:62
virtual void initialiseAndGetHeader(BaseDialog *dialog, char *buffer, size_t bufferSize)=0
virtual bool dialogButtonPressed(int buttonNum)=0
virtual void dialogDismissed(ButtonType buttonType)=0
virtual void copyCustomButtonText(int buttonNumber, char *buffer, size_t bufferSize)=0
Definition: BaseDialog.h:102
bool isInUse()
Definition: BaseDialog.h:178
void setButtons(ButtonType btn1, ButtonType btn2, int defVal=0)
Definition: BaseDialog.cpp:192
void actionPerformed(int btnNum)
Definition: BaseDialog.cpp:103
virtual void internalRender(int currentValue)=0
bool isCompressedMode()
Definition: BaseDialog.h:181
void setUserData(void *data)
Definition: BaseDialog.h:156
BaseDialog()
Definition: BaseDialog.cpp:23
void dialogRendering(unsigned int currentValue, bool userClicked)
Definition: BaseDialog.cpp:122
void showRam(const char *headerRam, bool allowRemote, CompletedHandlerFn completedHandler=NULL)
Definition: BaseDialog.cpp:37
bool copyButtonText(char *data, int buttonNum, int currentValue, bool isActive)
Definition: BaseDialog.cpp:140
void setInUse(bool b)
Definition: BaseDialog.h:226
void show(const char *headerPgm, bool allowRemote, CompletedHandlerFn completedHandler=NULL)
Definition: BaseDialog.cpp:29
void hide()
Definition: BaseDialog.cpp:80
ButtonType findActiveBtn(unsigned int currentValue)
Definition: BaseDialog.cpp:94
virtual void internalSetVisible(bool visible)
Definition: BaseDialog.cpp:68
void setNeedsDrawing(bool b)
Definition: BaseDialog.h:239
void showController(bool allowRemote, BaseDialogController *controller)
Definition: BaseDialog.cpp:46
virtual void copyIntoBuffer(const char *sz)
Definition: BaseDialog.cpp:172
bool isUsingOOController()
Definition: BaseDialog.h:175
Definition: BaseDialog.h:268
LocalDialogButtonMenuItem(RuntimeRenderingFn renderFn, menuid_t id, int btnNum, MenuItem *next)
Definition: BaseDialog.h:280
Definition: BaseDialog.h:295
void internalSetVisible(bool visible) override
Definition: BaseDialog.cpp:275
void copyIntoBuffer(const char *sz) override
Definition: BaseDialog.cpp:271
void internalRender(int currentValue) override
Definition: BaseDialog.h:321
Definition: MenuItems.h:329
void setLocalOnly(bool localOnly)
Definition: MenuItems.h:389
menuid_t getId() const
Definition: MenuItems.cpp:81
Definition: RuntimeMenuItem.h:75
Definition: RemoteConnector.h:187
Definition: RuntimeMenuItem.h:268
const char * getTextValue() const
Definition: RuntimeMenuItem.h:337