Pebble Foundation Classes  0.2.0
C++ for Pebble
pebble_menu_layer.hpp
1 #pragma once
2 
3 #include "pebble.hpp"
4 
5 namespace Pebble {
6 
7 // Pebble callback handlers for menu items
8 // ---------------------------------------
9 template<class T, uint16_t (T::*Method)(void)>
10 static uint16_t pebble_callback_handler(struct ::MenuLayer *menu_layer, void *callback_context)
11 {
12  return (((T*)callback_context)->*Method)();
13 }
14 
15 template<class T, uint16_t (T::*Method)(uint16_t)>
16 static uint16_t pebble_callback_handler(struct ::MenuLayer *menu_layer, uint16_t section_index, void *callback_context)
17 {
18  uint16_t result = (((T*)callback_context)->*Method)(section_index);
19  return result;
20 }
21 
22 template<class T, int16_t (T::*Method)(const MenuIndex&)>
23 static int16_t pebble_callback_handler(::MenuLayer *menu_layer, ::MenuIndex *cell_index, void *callback_context)
24 {
25  return (((T*)callback_context)->*Method)(*cell_index);
26 }
27 
28 template<class T, int16_t (T::*Method)(uint16_t)>
29 static int16_t pebble_callback_handler(struct ::MenuLayer *menu_layer, uint16_t section_index, void *callback_context)
30 {
31  return (((T*)callback_context)->*Method)(section_index);
32 }
33 
34 template<class T, void (T::*Method)(const GContext&, const Layer&, const MenuIndex&)>
35 static void pebble_callback_handler(::GContext* ctx, const ::Layer *cell_layer, ::MenuIndex *cell_index, void *callback_context)
36 {
37  typedef ::Layer* LayerPtr;
38  (((T*)callback_context)->*Method)(ctx, Layer(const_cast<LayerPtr>(cell_layer)), *cell_index);
39 }
40 
41 template<class T, void (T::*Method)(const GContext&, const Layer&, uint16_t)>
42 static void pebble_callback_handler(::GContext* ctx, const ::Layer *cell_layer, uint16_t section_index, void *callback_context)
43 {
44  typedef ::Layer* LayerPtr;
45  (((T*)callback_context)->*Method)(ctx, Layer(const_cast<LayerPtr>(cell_layer)), section_index);
46 }
47 
48 template<class T, void (T::*Method)(const MenuIndex&)>
49 static void pebble_callback_handler(::MenuLayer *menu_layer, ::MenuIndex *cell_index, void *callback_context)
50 {
51  (((T*)callback_context)->*Method)(*cell_index);
52 }
53 
54 template<class T, void (T::*Method)(const MenuIndex&, const MenuIndex&)>
55 static void pebble_callback_handler(::MenuLayer *menu_layer, ::MenuIndex new_index, ::MenuIndex old_index, void *callback_context)
56 {
57  (((T*)callback_context)->*Method)(new_index, old_index);
58 }
59 
60 template<class T, void (T::*Method)(MenuIndex&, const MenuIndex&)>
61 static void pebble_callback_handler(::MenuLayer *menu_layer, ::MenuIndex *new_index, ::MenuIndex old_index, void *callback_context)
62 {
63  (((T*)callback_context)->*Method)(*new_index, old_index);
64 }
65 
66 template<class T, void (T::*Method)(const GContext&, const Layer&, bool)>
67 static void pebble_callback_handler(::GContext* ctx, const ::Layer *bg_layer, bool highlight, void *callback_context)
68 {
69  typedef ::Layer* LayerPtr;
70  (((T*)callback_context)->*Method)(ctx, Layer(const_cast<LayerPtr>(bg_layer)), highlight);
71 }
72 // ---------------------------------------
73 
74 
75 class MenuLayer : public Layer
76 {
77 
78 public:
79  MenuLayer(GRect frame) : Layer((menu_layer_ = menu_layer_create(frame), menu_layer_get_layer(menu_layer_))) { }
80 
81  MenuLayer() : MenuLayer(GRect(0, 0, 0, 0)) {}
82 
84  virtual ~MenuLayer() {
85  menu_layer_destroy(menu_layer_);
86  }
87 
89  inline operator ::MenuLayer*() { return menu_layer_; }
90 
91  inline void SetClickConfigOntoWindow(Window& owner_window) { menu_layer_set_click_config_onto_window(menu_layer_, owner_window); }
92 
93  inline void SetSelectedNext(MenuRowAlign scroll_align, bool animated) { menu_layer_set_selected_next(menu_layer_, false, scroll_align, animated); }
94 
95  inline void SetSelectedPrevious(MenuRowAlign scroll_align, bool animated) { menu_layer_set_selected_next(menu_layer_, true, scroll_align, animated); }
96 
97  inline void SetSelectedIndex(MenuIndex index, MenuRowAlign scroll_align, bool animated) { menu_layer_set_selected_index(menu_layer_, index, scroll_align, animated); }
98 
99  inline void SetSelectedIndex(uint16_t section, uint16_t row, MenuRowAlign scroll_align, bool animated)
100  {
101  SetSelectedIndex({ .section = section, .row = row }, scroll_align, animated);
102  }
103 
104  inline MenuIndex GetSelectedIndex() const { return menu_layer_get_selected_index(menu_layer_); }
105 
106  inline void ReloadData() { menu_layer_reload_data(menu_layer_); }
107 
108  inline void SetNormalColors(GColor background, GColor foreground) { menu_layer_set_normal_colors(menu_layer_, background, foreground); }
109 
110  inline void SetHighlightColors(GColor background, GColor foreground) { menu_layer_set_highlight_colors(menu_layer_, background, foreground); }
111 
112  inline void PadBottom(bool enable) { menu_layer_pad_bottom_enable(menu_layer_, enable); }
113 
114  inline bool GetCenterFocused() const { return menu_layer_get_center_focused(menu_layer_); }
115 
116  inline void SetCenterFocused(bool center_focused) { menu_layer_set_center_focused(menu_layer_, center_focused); }
117 
118  inline bool IsIndexSelected(const MenuIndex& index) const { return menu_layer_is_index_selected(menu_layer_, const_cast<MenuIndex*>(&index)); }
119 
120  inline bool IsIndexSelected(uint16_t section, uint16_t row) const
121  {
122  return IsIndexSelected({ .section = section, .row = row });
123  }
124 
125  template<class T>
126  inline void SetCallbacks(T& owner, MenuLayerCallbacks callbacks)
127  {
128  menu_layer_set_callbacks(menu_layer_, &owner, callbacks);
129  }
130 
131  static bool IsHighlighted(const Layer& cell_layer) { return menu_cell_layer_is_highlighted(cell_layer); }
132 
133  static int16_t IndexCompare(const MenuIndex& a, const MenuIndex& b) { return menu_index_compare(&a, &b); }
134 
135 protected:
136  // The layer pointer
137  ::MenuLayer* menu_layer_;
138 };
139 
140 
142 {
143 protected:
146  void EnableMenuEvents(MenuLayer& menuLayer, bool hasHeaders, bool hasSeparators, bool longClicks, bool customBackground)
147  {
148  menuLayer.SetCallbacks(*this, (MenuLayerCallbacks){
149  .get_num_sections = CALLBACK(OnGetNumSections),
150  .get_num_rows = CALLBACK(OnGetNumRows),
151  .get_cell_height = CALLBACK(OnGetCellHeight),
152  .get_header_height = hasHeaders ? CALLBACK(OnGetHeaderHeight) : nullptr,
153  .draw_row = CALLBACK(OnDrawRow),
154  .draw_header = hasHeaders ? CALLBACK(OnDrawHeader) : nullptr,
155  .select_click = CALLBACK(OnSelectClick),
156  .select_long_click = longClicks ? CALLBACK(OnSelectLongClick) : nullptr,
157  .selection_changed = CALLBACK(OnSelectionChanged),
158  .get_separator_height = hasSeparators ? CALLBACK(OnGetSeparatorHeight) : nullptr,
159  .draw_separator = hasSeparators ? CALLBACK(OnDrawSeparator) : nullptr,
160  .selection_will_change = CALLBACK(OnSelectionWillChange),
161  .draw_background = customBackground ? CALLBACK(OnDrawBackground) : nullptr
162  });
163  }
164 
169  virtual uint16_t OnGetNumSections() { return 1; }
170 
177  virtual uint16_t OnGetNumRows(uint16_t section_index) = 0;
178 
187  virtual int16_t OnGetCellHeight(const MenuIndex& cell_index) {
188  return 44; // MENU_CELL_BASIC_CELL_HEIGHT
189  }
190 
197  virtual int16_t OnGetHeaderHeight(uint16_t section_index)
198  {
199  return 0;
200  }
201 
210  virtual int16_t OnGetSeparatorHeight(const MenuIndex& cell_index)
211  {
212  return 0;
213  }
214 
226  virtual void OnDrawRow(const GContext& ctx, const Layer& cell_layer, const MenuIndex& cell_index) = 0;
227 
240  virtual void OnDrawHeader(const GContext& ctx, const Layer& cell_layer, uint16_t section_index) { }
241 
254  virtual void OnDrawSeparator(const GContext& ctx, const Layer& cell_layer, const MenuIndex& cell_index) { }
255 
261  virtual void OnSelectClick(const MenuIndex& cell_index) { }
262 
268  virtual void OnSelectLongClick(const MenuIndex& cell_index) { }
269 
276  virtual void OnSelectionChanged(const MenuIndex& new_index, const MenuIndex& old_index) { }
277 
287  virtual void OnSelectionWillChange(MenuIndex& new_index, const MenuIndex& old_index) { }
288 
296  virtual void OnDrawBackground(const GContext& ctx, const Layer& bg_layer, bool highlight) { }
297 };
298 
299 }
Pebble::Layer
Definition: pebble_layer.hpp:34
menu_layer_set_normal_colors
void menu_layer_set_normal_colors(MenuLayer *menu_layer, GColor background, GColor foreground)
Pebble::Window
Wraps and existing Pebble window but does not take ownership of it.
Definition: pebble_window.hpp:26
Pebble::Layer::Layer
Layer(::Layer *layer)
Wraps an existing TextLayer.
Definition: pebble_layer.hpp:38
menu_layer_create
MenuLayer * menu_layer_create(GRect frame)
MenuRowAlign
MenuRowAlign
Definition: pebble.h:7204
MenuLayerCallbacks
Data structure containing all the callbacks of a MenuLayer.
Definition: pebble.h:7065
menu_layer_pad_bottom_enable
void menu_layer_pad_bottom_enable(MenuLayer *menu_layer, bool enable)
Pebble::MenuEvents::OnGetSeparatorHeight
virtual int16_t OnGetSeparatorHeight(const MenuIndex &cell_index)
Definition: pebble_menu_layer.hpp:210
Pebble::MenuLayer::~MenuLayer
virtual ~MenuLayer()
Destroys the underlying TextLayer.
Definition: pebble_menu_layer.hpp:84
menu_index_compare
int16_t menu_index_compare(const MenuIndex *a, const MenuIndex *b)
menu_layer_set_selected_next
void menu_layer_set_selected_next(MenuLayer *menu_layer, bool up, MenuRowAlign scroll_align, bool animated)
Pebble::MenuEvents::OnGetNumSections
virtual uint16_t OnGetNumSections()
Definition: pebble_menu_layer.hpp:169
Pebble::MenuEvents::OnSelectionChanged
virtual void OnSelectionChanged(const MenuIndex &new_index, const MenuIndex &old_index)
Definition: pebble_menu_layer.hpp:276
Pebble::MenuEvents
Definition: pebble_menu_layer.hpp:141
menu_layer_set_click_config_onto_window
void menu_layer_set_click_config_onto_window(MenuLayer *menu_layer, struct Window *window)
Pebble::MenuLayer
Definition: pebble_menu_layer.hpp:75
menu_layer_is_index_selected
bool menu_layer_is_index_selected(const MenuLayer *menu_layer, MenuIndex *index)
menu_layer_get_center_focused
bool menu_layer_get_center_focused(MenuLayer *menu_layer)
Pebble::MenuEvents::OnDrawBackground
virtual void OnDrawBackground(const GContext &ctx, const Layer &bg_layer, bool highlight)
Definition: pebble_menu_layer.hpp:296
MenuIndex
Definition: pebble.h:6875
Pebble::MenuEvents::OnSelectLongClick
virtual void OnSelectLongClick(const MenuIndex &cell_index)
Definition: pebble_menu_layer.hpp:268
menu_layer_set_highlight_colors
void menu_layer_set_highlight_colors(MenuLayer *menu_layer, GColor background, GColor foreground)
Pebble::MenuEvents::OnGetNumRows
virtual uint16_t OnGetNumRows(uint16_t section_index)=0
Pebble::MenuEvents::OnDrawSeparator
virtual void OnDrawSeparator(const GContext &ctx, const Layer &cell_layer, const MenuIndex &cell_index)
Definition: pebble_menu_layer.hpp:254
Pebble::MenuEvents::EnableMenuEvents
void EnableMenuEvents(MenuLayer &menuLayer, bool hasHeaders, bool hasSeparators, bool longClicks, bool customBackground)
Definition: pebble_menu_layer.hpp:146
menu_layer_set_selected_index
void menu_layer_set_selected_index(MenuLayer *menu_layer, MenuIndex index, MenuRowAlign scroll_align, bool animated)
Pebble::MenuEvents::OnGetHeaderHeight
virtual int16_t OnGetHeaderHeight(uint16_t section_index)
Definition: pebble_menu_layer.hpp:197
Pebble::MenuEvents::OnSelectClick
virtual void OnSelectClick(const MenuIndex &cell_index)
Definition: pebble_menu_layer.hpp:261
Pebble::GRect
Definition: pebble_graphics_types.hpp:71
Pebble::GContext
Definition: pebble_graphics_context.hpp:73
Pebble::MenuEvents::OnDrawRow
virtual void OnDrawRow(const GContext &ctx, const Layer &cell_layer, const MenuIndex &cell_index)=0
menu_layer_get_selected_index
MenuIndex menu_layer_get_selected_index(const MenuLayer *menu_layer)
GRect
struct GRect GRect
menu_layer_set_center_focused
void menu_layer_set_center_focused(MenuLayer *menu_layer, bool center_focused)
menu_layer_destroy
void menu_layer_destroy(MenuLayer *menu_layer)
Destroys a MenuLayer previously created by menu_layer_create.
menu_layer_set_callbacks
void menu_layer_set_callbacks(MenuLayer *menu_layer, void *callback_context, MenuLayerCallbacks callbacks)
menu_cell_layer_is_highlighted
bool menu_cell_layer_is_highlighted(const Layer *cell_layer)
menu_layer_get_layer
Layer * menu_layer_get_layer(const MenuLayer *menu_layer)
Pebble::MenuEvents::OnSelectionWillChange
virtual void OnSelectionWillChange(MenuIndex &new_index, const MenuIndex &old_index)
Definition: pebble_menu_layer.hpp:287
menu_layer_reload_data
void menu_layer_reload_data(MenuLayer *menu_layer)
GColor8
Definition: pebble.h:3268
Pebble::MenuEvents::OnDrawHeader
virtual void OnDrawHeader(const GContext &ctx, const Layer &cell_layer, uint16_t section_index)
Definition: pebble_menu_layer.hpp:240
Pebble::MenuEvents::OnGetCellHeight
virtual int16_t OnGetCellHeight(const MenuIndex &cell_index)
Definition: pebble_menu_layer.hpp:187