Remove all global variables
This commit is contained in:
116
src/windows/action/action_menu_layer.c
Normal file
116
src/windows/action/action_menu_layer.c
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <pebble.h>
|
||||
#include "action_menu_layer.h"
|
||||
#include "style.h"
|
||||
|
||||
const char *ACTIONS[] = {
|
||||
" Check",
|
||||
" View stats"
|
||||
};
|
||||
|
||||
// callback: MenuLayerSelectCallback
|
||||
static void on_click(struct MenuLayer *menu_layer,
|
||||
MenuIndex *cell_index,
|
||||
void *callback_context)
|
||||
{
|
||||
struct ActionMenuLayer *action_menu = callback_context;
|
||||
if(!action_menu->callbacks.on_select) return;
|
||||
action_menu->callbacks.on_select(action_menu->callback_context);
|
||||
}
|
||||
|
||||
// MenuLayerGetNumberOfRowsInSectionsCallback
|
||||
static uint16_t on_get_num_rows(MenuLayer *menu_layer,
|
||||
uint16_t section_index,
|
||||
void *context)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
// MenuLayerDrawRowCallback
|
||||
static void on_draw_row(GContext *ctx,
|
||||
const Layer *cell_layer,
|
||||
MenuIndex *cell_index,
|
||||
void *extra)
|
||||
{
|
||||
int n = cell_index->row;
|
||||
menu_cell_basic_draw(ctx, cell_layer, ACTIONS[n], NULL, NULL);
|
||||
}
|
||||
|
||||
// MenuLayerGetCellHeightCallback
|
||||
static int16_t on_get_cell_height(struct MenuLayer *menu_layer,
|
||||
MenuIndex *cell_index,
|
||||
void *context)
|
||||
{
|
||||
return CELL_HEIGHT;
|
||||
}
|
||||
|
||||
static void set_menu_layer_callbacks(struct ActionMenuLayer *action_menu,
|
||||
struct MenuLayer *menu)
|
||||
{
|
||||
MenuLayerCallbacks callbacks = {
|
||||
.get_num_rows = on_get_num_rows,
|
||||
.draw_row = on_draw_row,
|
||||
.get_cell_height = on_get_cell_height,
|
||||
.select_click = on_click,
|
||||
};
|
||||
|
||||
menu_layer_set_callbacks(menu, action_menu, callbacks);
|
||||
menu_layer_set_highlight_colors(menu, HIGHLIGHT_BACKGROUND_COLOR,
|
||||
HIGHLIGHT_FOREGROUND_COLOR);
|
||||
menu_layer_set_normal_colors(menu, NORMAL_BACKGROUND_COLOR,
|
||||
NORMAL_FOREGROUND_COLOR);
|
||||
}
|
||||
|
||||
void ACTION_MENU_LAYER_add_to_layer(struct ActionMenuLayer *action_menu,
|
||||
struct Layer *root_layer)
|
||||
{
|
||||
struct Layer *raw_layer = menu_layer_get_layer(action_menu->menu_layer);
|
||||
layer_add_child(root_layer, raw_layer);
|
||||
}
|
||||
|
||||
void ACTION_MENU_LAYER_attach_to_window(struct ActionMenuLayer *action_menu,
|
||||
struct Window *window)
|
||||
{
|
||||
struct MenuLayer *menu = action_menu->menu_layer;
|
||||
menu_layer_set_click_config_onto_window(menu, window);
|
||||
};
|
||||
|
||||
struct ActionMenuLayer* ACTION_MENU_LAYER_create(struct GRect bounds)
|
||||
{
|
||||
struct ActionMenuLayer *action_menu = 0;
|
||||
action_menu = (struct ActionMenuLayer*) malloc(sizeof(struct ActionMenuLayer));
|
||||
if(!action_menu) return NULL;
|
||||
|
||||
struct MenuLayer *menu = menu_layer_create(bounds);
|
||||
if(!menu) return NULL;
|
||||
|
||||
set_menu_layer_callbacks(action_menu, menu);
|
||||
action_menu->menu_layer = menu;
|
||||
|
||||
return action_menu;
|
||||
}
|
||||
|
||||
void ACTION_MENU_LAYER_destroy(struct ActionMenuLayer *action_menu)
|
||||
{
|
||||
if(!action_menu) return;
|
||||
if(action_menu->menu_layer) menu_layer_destroy(action_menu->menu_layer);
|
||||
free(action_menu);
|
||||
}
|
||||
43
src/windows/action/action_menu_layer.h
Normal file
43
src/windows/action/action_menu_layer.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
struct ActionMenuLayerCallbacks
|
||||
{
|
||||
void (*on_select)(void *callback_context);
|
||||
};
|
||||
|
||||
struct ActionMenuLayer
|
||||
{
|
||||
struct MenuLayer *menu_layer;
|
||||
|
||||
void *callback_context;
|
||||
struct ActionMenuLayerCallbacks callbacks;
|
||||
};
|
||||
|
||||
void ACTION_MENU_LAYER_attach_to_window(struct ActionMenuLayer *action_menu,
|
||||
struct Window *window);
|
||||
|
||||
void ACTION_MENU_LAYER_add_to_layer(struct ActionMenuLayer *action_menu,
|
||||
struct Layer *root_layer);
|
||||
|
||||
void ACTION_MENU_LAYER_destroy(struct ActionMenuLayer *action_menu);
|
||||
|
||||
struct ActionMenuLayer* ACTION_MENU_LAYER_create(struct GRect bounds);
|
||||
@@ -19,128 +19,108 @@
|
||||
|
||||
#include <pebble.h>
|
||||
#include "action_window.h"
|
||||
#include "style.h"
|
||||
#include "../../layers/border_layer.h"
|
||||
#include "../../util.h"
|
||||
#include "action_menu_layer.h"
|
||||
#include "style.h"
|
||||
|
||||
static struct Window *WINDOW = 0;
|
||||
static struct MenuLayer *MENU_LAYER = 0;
|
||||
static struct BorderLayer *BORDER_LAYER = 0;
|
||||
|
||||
char *ACTIONS[] = { " Check", " View stats"};
|
||||
|
||||
// DRAWING
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void draw_left_border(GContext *ctx, const Layer *cell_layer)
|
||||
static void on_select(void *callback_context)
|
||||
{
|
||||
GRect bounds = layer_get_bounds(cell_layer);
|
||||
GRect rect = GRect(bounds.origin.x, bounds.origin.y, 5, bounds.size.h);
|
||||
graphics_context_set_fill_color(ctx, GColorFolly);
|
||||
graphics_fill_rect(ctx, rect, 0, GCornerNone);
|
||||
window_stack_pop(true);
|
||||
}
|
||||
|
||||
// MENU LAYER CALLBACKS
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
static uint16_t get_num_rows(MenuLayer *menu_layer,
|
||||
uint16_t section_index,
|
||||
void *context)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void draw_row(GContext *ctx,
|
||||
const Layer *cell_layer,
|
||||
MenuIndex *cell_index,
|
||||
void *extra)
|
||||
{
|
||||
int n = cell_index->row;
|
||||
menu_cell_basic_draw(ctx, cell_layer, ACTIONS[n], NULL, NULL);
|
||||
draw_left_border(ctx, cell_layer);
|
||||
}
|
||||
|
||||
static int16_t get_cell_height(struct MenuLayer *menu_layer,
|
||||
MenuIndex *cell_index,
|
||||
void *context)
|
||||
{
|
||||
return CELL_HEIGHT;
|
||||
}
|
||||
|
||||
static void select_click(MenuLayer *menu_layer, MenuIndex *index, void *context)
|
||||
{
|
||||
}
|
||||
|
||||
// WINDOWS HANDLERS
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void add_menu_layer(Window *window,
|
||||
Layer *root_layer,
|
||||
GRect bounds)
|
||||
{
|
||||
MenuLayerCallbacks callbacks = {
|
||||
.get_num_rows = get_num_rows,
|
||||
.draw_row = draw_row,
|
||||
.get_cell_height = get_cell_height,
|
||||
.select_click = select_click,
|
||||
};
|
||||
|
||||
MENU_LAYER = menu_layer_create(bounds);
|
||||
menu_layer_set_click_config_onto_window(MENU_LAYER, window);
|
||||
menu_layer_set_callbacks(MENU_LAYER, NULL, callbacks);
|
||||
menu_layer_set_highlight_colors(MENU_LAYER, HIGHLIGHT_BACKGROUND_COLOR,
|
||||
HIGHLIGHT_FOREGROUND_COLOR);
|
||||
menu_layer_set_normal_colors(MENU_LAYER, NORMAL_BACKGROUND_COLOR,
|
||||
NORMAL_FOREGROUND_COLOR);
|
||||
|
||||
layer_add_child(root_layer, menu_layer_get_layer(MENU_LAYER));
|
||||
}
|
||||
|
||||
static int add_border_layer(Window *window, Layer *root_layer, GRect frame)
|
||||
static int add_menu_layer(struct ActionWindow *window,
|
||||
struct Layer *root_layer,
|
||||
struct GRect bounds)
|
||||
{
|
||||
int rval = 0;
|
||||
|
||||
BORDER_LAYER = (struct BorderLayer*) malloc(sizeof(struct BorderLayer));
|
||||
abort_if(!BORDER_LAYER, "could not allocate BORDER_LAYER");
|
||||
struct ActionMenuLayer *menu_layer = 0;
|
||||
menu_layer = ACTION_MENU_LAYER_create(bounds);
|
||||
abort_if(!menu_layer, "ACTION_MENU_LAYER_create failed");
|
||||
|
||||
BORDER_LAYER_init(BORDER_LAYER, frame);
|
||||
BORDER_LAYER->color = GColorFolly;
|
||||
window->menu_layer = menu_layer;
|
||||
ACTION_MENU_LAYER_attach_to_window(menu_layer, window->raw_window);
|
||||
ACTION_MENU_LAYER_add_to_layer(menu_layer, root_layer);
|
||||
|
||||
layer_add_child(root_layer, BORDER_LAYER->raw_layer);
|
||||
menu_layer->callback_context = window;
|
||||
menu_layer->callbacks = (struct ActionMenuLayerCallbacks) {
|
||||
.on_select = on_select
|
||||
};
|
||||
|
||||
CLEANUP:
|
||||
return rval;
|
||||
}
|
||||
|
||||
static void on_load(Window *window)
|
||||
static int add_border_layer(struct ActionWindow *window,
|
||||
struct Layer *root_layer,
|
||||
const struct GRect frame)
|
||||
{
|
||||
Layer *root_layer = window_get_root_layer(window);
|
||||
int rval = 0;
|
||||
|
||||
struct BorderLayer *border_layer = 0;
|
||||
|
||||
border_layer = BORDER_LAYER_create(frame);
|
||||
abort_if(!border_layer, "BORDER_LAYER_create failed");
|
||||
|
||||
border_layer->color = BORDER_COLOR;
|
||||
border_layer->width = BORDER_WIDTH;
|
||||
|
||||
window->border_layer = border_layer;
|
||||
BORDER_LAYER_add_to_layer(border_layer, root_layer);
|
||||
|
||||
CLEANUP:
|
||||
return rval;
|
||||
}
|
||||
|
||||
// callback: WindowHandler
|
||||
static void on_load(Window *raw_window)
|
||||
{
|
||||
struct ActionWindow *window = window_get_user_data(raw_window);
|
||||
Layer *root_layer = window_get_root_layer(raw_window);
|
||||
GRect bounds = layer_get_bounds(root_layer);
|
||||
add_menu_layer(window, root_layer, bounds);
|
||||
add_border_layer(window, root_layer, bounds);
|
||||
}
|
||||
|
||||
static void on_unload(Window *window)
|
||||
// callback: WindowHandler
|
||||
static void on_unload(Window *raw_window)
|
||||
{
|
||||
menu_layer_destroy(MENU_LAYER);
|
||||
free(BORDER_LAYER);
|
||||
struct ActionWindow *window = window_get_user_data(raw_window);
|
||||
ACTION_MENU_LAYER_destroy(window->menu_layer);
|
||||
BORDER_LAYER_destroy(window->border_layer);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
void ACTION_WINDOW_push()
|
||||
static void set_raw_window_handlers(Window *raw_window)
|
||||
{
|
||||
WindowHandlers handlers = {
|
||||
.load = on_load,
|
||||
.unload = on_unload
|
||||
.load = on_load,
|
||||
.unload = on_unload
|
||||
};
|
||||
|
||||
WINDOW = window_create();
|
||||
window_set_window_handlers(WINDOW, handlers);
|
||||
window_stack_push(WINDOW, true);
|
||||
window_set_window_handlers(raw_window, handlers);
|
||||
}
|
||||
|
||||
void ACTION_WINDOW_destroy()
|
||||
struct ActionWindow *ACTION_WINDOW_create()
|
||||
{
|
||||
window_destroy(WINDOW);
|
||||
struct ActionWindow *window = 0;
|
||||
|
||||
window = (struct ActionWindow*) malloc(sizeof(struct ActionWindow));
|
||||
if(!window) return NULL;
|
||||
|
||||
Window *raw_window = window_create();
|
||||
if(!raw_window) return NULL;
|
||||
|
||||
window->raw_window = raw_window;
|
||||
window_set_user_data(raw_window, window);
|
||||
set_raw_window_handlers(raw_window);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
void ACTION_WINDOW_destroy(struct ActionWindow *window)
|
||||
{
|
||||
if(!window) return;
|
||||
window_destroy(window->raw_window);
|
||||
free(window);
|
||||
}
|
||||
@@ -19,6 +19,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
void ACTION_WINDOW_push();
|
||||
struct ActionWindow
|
||||
{
|
||||
struct Window *raw_window;
|
||||
|
||||
void ACTION_WINDOW_destroy();
|
||||
struct ActionMenuLayer *menu_layer;
|
||||
|
||||
struct BorderLayer *border_layer;
|
||||
};
|
||||
|
||||
struct ActionWindow* ACTION_WINDOW_create();
|
||||
|
||||
void ACTION_WINDOW_destroy(struct ActionWindow *window);
|
||||
@@ -29,4 +29,8 @@
|
||||
|
||||
#define NORMAL_FOREGROUND_COLOR GColorDarkGray
|
||||
|
||||
#define BORDER_COLOR GColorFolly
|
||||
|
||||
#define BORDER_WIDTH 10
|
||||
|
||||
#define CELL_HEIGHT 36
|
||||
Reference in New Issue
Block a user