Pebble Foundation Classes  0.2.0
C++ for Pebble
pebble_window.hpp
1 #pragma once
2 
3 namespace Pebble {
4 
5 // Window callback handler
6 template<class T, void (T::*Method)(void)>
7 static void pebble_callback_handler(::Window* window);
8 
9 // Click conflig callback handler
10 template<class T, void (T::*Method)(const ClickConfig&)>
11 static void pebble_callback_handler(void* context);
12 
13 // Click callback handler
14 template<class T, void (T::*Method)(const ClickRecognizer&)>
15 static void pebble_callback_handler(::ClickRecognizerRef recognizer, void* context);
16 
17 // Numberwindow callback handler
18 template<class T, void (T::*Method)(void)>
19 static void pebble_callback_handler(::NumberWindow* window, void* context);
20 
21 
24 
26 class Window : public Layer
27 {
28 public:
31  Window(::Window* window) : Layer(window_get_root_layer(window)), window_(window)
32  {
33  }
34 
36  virtual ~Window()
37  {
38  // Does not destroy the Pebble window has this base class does not take ownership.
39  }
40 
42  operator ::Window*() { return window_; }
43 
48  inline void StackPush(bool animated) { window_stack_push(window_, animated); }
49 
52  inline bool StackContainsWindow() const { return window_stack_contains_window(window_); }
53 
56  inline bool WindowIsTop() { return window_ == window_stack_get_top_window(); }
57 
68  inline bool StackRemove(bool animated) { return window_stack_remove(window_, animated); }
69 
75  inline bool IsLoaded() const { return window_is_loaded(window_); }
76 
79 
84  inline void SetBackgroundColor(GColor background_color) { window_set_background_color(window_, background_color); }
85 
86 protected:
87  // Private instance of window pebble Window pointer
88  ::Window *window_;
89 
90 }; // End Class
91 
92 
94 class AppWindow : public Window
95 {
96 public:
100  {
101  window_set_user_data(window_, this);
102  window_set_window_handlers(window_, {
103  .load = CALLBACK(OnLoad),
104  .appear = CALLBACK(OnAppear),
105  .disappear = CALLBACK(OnDisappear),
106  .unload = CALLBACK(OnUnload)
107  });
108  }
109 
111  virtual ~AppWindow()
112  {
113  // The assignment to a static variable makes the virtual methods work for subclasses
114  // I have no idea why!
115  dummy() = 0;
116  window_destroy(window_);
117  }
118 
120  template<class T>
121  static inline T& Get(::Window* window) { return *(T*)(window_get_user_data(window)); }
122 
123 protected:
126  virtual void OnLoad() { };
127 
130  virtual void OnUnload() { };
131 
136  virtual void OnAppear() { };
137 
140  virtual void OnDisappear() { };
141 
143  inline void SetClickConfigProvider(::ClickConfigProvider click_config_provider)
144  {
145  window_set_click_config_provider_with_context(window_, click_config_provider, this);
146  }
147 
148 private:
149  // Dummy static variable needed for virutal method pointers to work (see destructor)
150  inline static int& dummy() { static int dummy = 0; return dummy; }
151 
152 }; // End Class
153 
154 
155 class NumberWindow : public Window
156 {
157 public:
160  NumberWindow(const char* label) : Window((::Window*)number_window_create(
161  label,
162  {
163  .incremented = CALLBACK(OnIncremented),
164  .decremented = CALLBACK(OnDecremented),
165  .selected = CALLBACK(OnSelected),
166  },
167  this
168  ))
169  {
170  }
171 
173  virtual ~NumberWindow() {
174  number_window_destroy((::NumberWindow*)window_);
175  }
176 
177  operator ::NumberWindow*() { return (::NumberWindow*)window_; }
178 
179  inline int32_t GetValue() const { return number_window_get_value((::NumberWindow*)window_); }
180 
181  inline void SetValue(int32_t value) { number_window_set_value((::NumberWindow*)window_, value); }
182 
183  inline void SetLabel(const char* label) { number_window_set_label((::NumberWindow*)window_, label); }
184 
185  inline void SetMin(int32_t min) { number_window_set_min((::NumberWindow*)window_, min); }
186 
187  inline void SetMax(int32_t max) { number_window_set_max((::NumberWindow*)window_, max); }
188 
189  inline void SetStepSize(int32_t step) { number_window_set_step_size((::NumberWindow*)window_, step); }
190 
191 protected:
192  virtual void OnIncremented() { };
193 
194  virtual void OnDecremented() { };
195 
196  virtual void OnSelected() { };
197 
198 }; // End Class
199 
200 // Window callback handler implementation
201 template<class T, void (T::*Method)(void)>
202 static void pebble_callback_handler(::Window* window)
203 {
204  (AppWindow::Get<T>(window).*Method)();
205 }
206 
207 // Click configuration handler implementation
208 template<class T, void (T::*Method)(const ClickConfig&)>
209 static void pebble_callback_handler(void* context)
210 {
211  (((T*)context)->*Method)(ClickConfig());
212 }
213 
214 // Click handler implementation
215 template<class T, void (T::*Method)(const ClickRecognizer&)>
216 static void pebble_callback_handler(::ClickRecognizerRef recognizer, void* context)
217 {
218  (((T*)context)->*Method)(ClickRecognizer(recognizer));
219 }
220 
221 // Number window handler implementation
222 template<class T, void (T::*Method)(void)>
223 static void pebble_callback_handler(::NumberWindow* window, void* context)
224 {
225  (((T*)context)->*Method)();
226 }
227 
228 
230 
231 }
Pebble::Layer
Definition: pebble_layer.hpp:34
window_get_user_data
void * window_get_user_data(const Window *window)
Pebble::Window::~Window
virtual ~Window()
Window destructor
Definition: pebble_window.hpp:36
Pebble::Window
Wraps and existing Pebble window but does not take ownership of it.
Definition: pebble_window.hpp:26
Pebble::Window::IsLoaded
bool IsLoaded() const
Definition: pebble_window.hpp:75
number_window_destroy
void number_window_destroy(NumberWindow *number_window)
Destroys a NumberWindow previously created by number_window_create.
number_window_get_value
int32_t number_window_get_value(const NumberWindow *numberwindow)
window_stack_get_top_window
Window * window_stack_get_top_window(void)
number_window_set_max
void number_window_set_max(NumberWindow *numberwindow, int32_t max)
Pebble::NumberWindow::NumberWindow
NumberWindow(const char *label)
Definition: pebble_window.hpp:160
number_window_create
NumberWindow * number_window_create(const char *label, NumberWindowCallbacks callbacks, void *callback_context)
Pebble::Window::StackContainsWindow
bool StackContainsWindow() const
Definition: pebble_window.hpp:52
Pebble::AppWindow::~AppWindow
virtual ~AppWindow()
Destroys the underlying Pebble window.
Definition: pebble_window.hpp:111
window_set_background_color
void window_set_background_color(Window *window, GColor background_color)
ClickConfigProvider
void(* ClickConfigProvider)(void *context)
Definition: pebble.h:4990
Pebble::AppWindow::OnAppear
virtual void OnAppear()
Definition: pebble_window.hpp:136
window_set_user_data
void window_set_user_data(Window *window, void *data)
Pebble::Window::Window
Window(::Window *window)
Definition: pebble_window.hpp:31
window_stack_push
void window_stack_push(Window *window, bool animated)
Pebble::AppWindow::AppWindow
AppWindow()
Definition: pebble_window.hpp:99
window_set_window_handlers
void window_set_window_handlers(Window *window, WindowHandlers handlers)
Pebble::Window::SetBackgroundColor
void SetBackgroundColor(GColor background_color)
Definition: pebble_window.hpp:84
window_stack_contains_window
bool window_stack_contains_window(Window *window)
window_stack_remove
bool window_stack_remove(Window *window, bool animated)
window_destroy
void window_destroy(Window *window)
Destroys a Window previously created by window_create.
Pebble::NumberWindow::~NumberWindow
virtual ~NumberWindow()
Destroys the underlying Pebble window.
Definition: pebble_window.hpp:173
number_window_set_step_size
void number_window_set_step_size(NumberWindow *numberwindow, int32_t step)
window_is_loaded
bool window_is_loaded(Window *window)
window_create
Window * window_create(void)
Pebble::AppWindow::OnDisappear
virtual void OnDisappear()
Definition: pebble_window.hpp:140
Pebble::Window::WindowIsTop
bool WindowIsTop()
Definition: pebble_window.hpp:56
window_get_click_config_provider
ClickConfigProvider window_get_click_config_provider(const Window *window)
Pebble::NumberWindow
Definition: pebble_window.hpp:155
window_set_click_config_provider_with_context
void window_set_click_config_provider_with_context(Window *window, ClickConfigProvider click_config_provider, void *context)
Pebble::AppWindow::Get
static T & Get(::Window *window)
Returns an object of the template class type from the specified window's user data.
Definition: pebble_window.hpp:121
number_window_set_label
void number_window_set_label(NumberWindow *numberwindow, const char *label)
number_window_set_value
void number_window_set_value(NumberWindow *numberwindow, int32_t value)
window_get_root_layer
struct Layer * window_get_root_layer(const Window *window)
ClickRecognizerRef
void * ClickRecognizerRef
Definition: pebble.h:4970
Pebble::AppWindow::OnLoad
virtual void OnLoad()
Definition: pebble_window.hpp:126
Pebble::AppWindow::OnUnload
virtual void OnUnload()
Definition: pebble_window.hpp:130
Pebble::Window::StackPush
void StackPush(bool animated)
Definition: pebble_window.hpp:48
Pebble::Window::StackRemove
bool StackRemove(bool animated)
Definition: pebble_window.hpp:68
Pebble::Window::GetClickConfigProvider
inline ::ClickConfigProvider GetClickConfigProvider() const
Gets the current click configuration provider of the window.
Definition: pebble_window.hpp:78
GColor8
Definition: pebble.h:3268
Pebble::AppWindow
Base class for application windows.
Definition: pebble_window.hpp:94
Pebble::AppWindow::SetClickConfigProvider
void SetClickConfigProvider(::ClickConfigProvider click_config_provider)
Sets the click config provider of this window to a local method.
Definition: pebble_window.hpp:143
number_window_set_min
void number_window_set_min(NumberWindow *numberwindow, int32_t min)