diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/button.c | 22 | ||||
| -rw-r--r-- | src/button.h | 26 | ||||
| -rw-r--r-- | src/comet.c | 14 | ||||
| -rw-r--r-- | src/draw.c | 70 | ||||
| -rw-r--r-- | src/draw.h | 4 | ||||
| -rw-r--r-- | src/state.c | 19 | ||||
| -rw-r--r-- | src/state.h | 10 | ||||
| -rw-r--r-- | src/window.h | 2 |
8 files changed, 138 insertions, 29 deletions
diff --git a/src/button.c b/src/button.c new file mode 100644 index 0000000..6e0e263 --- /dev/null +++ b/src/button.c @@ -0,0 +1,22 @@ +#include <glib.h> + +#include "button.h" + +Button *button_create(const char *text, bool fixed_size) +{ + Button *btn = g_malloc0(sizeof(Button)); + btn->text = g_strdup(text); + btn->fixed_size = fixed_size; + return btn; +} + +void button_set_action(Button *btn, ButtonAction action) +{ + btn->action = action; +} + +void button_destroy(Button *btn) +{ + g_free(btn->text); + g_free(btn); +} diff --git a/src/button.h b/src/button.h new file mode 100644 index 0000000..11b6507 --- /dev/null +++ b/src/button.h @@ -0,0 +1,26 @@ +#ifndef COMET_BUTTON_H +#define COMET_BUTTON_H + +#include <stdbool.h> + +// TODO: Generic button shapes/actions + +typedef struct Button Button; + +typedef void (* ButtonAction)(Button *btn); + +struct Button { + bool fixed_size; + ButtonAction action; + char *text; +}; + +Button *button_create(const char *text, bool fixed_size); + +void button_set_action(Button *btn, ButtonAction action); + +void button_destroy(Button *btn); + +#endif + +// vim: ts=4 sw=4 et diff --git a/src/comet.c b/src/comet.c index 9733d4b..cf325bf 100644 --- a/src/comet.c +++ b/src/comet.c @@ -42,9 +42,21 @@ int main(int argc, char **argv) log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding); - Drawable *draw = draw_create("Hack 12", height, x_padding, x_padding, y_padding, 1); + Drawable *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 1); State *state = state_create(win, draw); + + for (int i = 0; i < 9; ++i) { + char text[] = { '1' + i, '\0' }; + Button *btn = button_create(text, true); + state_add_button(state, btn); + } + + Button *btn = button_create("long button", false); + state_add_button(state, btn); + + draw_set_buttons(draw, state->btns); + connect_attach_state(con, state); connect_attach_source(con); @@ -5,6 +5,7 @@ #include <pango/pango-types.h> #include "draw.h" +#include "button.h" #include "log.h" // Idea: Either make a to_draw queue where we put things we schedule to redraw @@ -68,56 +69,85 @@ void draw_paint(Drawable *draw, Window *win) cairo_paint(cr); - cairo_set_line_width(cr, 1 * scale); + int line_w = 1 * scale; + cairo_set_line_width(cr, line_w); - for (int i = 0; i < 9; i++) { + int bx = 0; - PangoLayout *layout = pango_cairo_create_layout (cr); + for (GList *it = draw->btns; it; it = it->next) { + Button *btn = it->data; + + PangoLayout *layout = pango_cairo_create_layout(cr); pango_layout_set_font_description(layout, draw->desc); - int x = (height + cairo_get_line_width(cr) * scale * 2) * i; + int bwidth, bheight = height; + char *btn_text = btn->text; + char tmp[2] = { 0 }; + + if (btn->fixed_size) { + bwidth = height; + // Get only the first char + tmp[0] = btn->text[0]; + btn_text = tmp; + } + + pango_layout_set_text(layout, btn_text, -1); + pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); + + int text_w, text_h; + pango_layout_get_pixel_size(layout, &text_w, &text_h); + text_w = ceil(text_w / scale); + text_h = ceil(text_h / scale); + + if (!btn->fixed_size) { + bwidth = text_w + 2*radius; + } + + int text_x = bx + (bwidth / 2) - (text_w / 2); + int text_y = (bheight / 2) - (text_h / 2); // purple cairo_set_source_rgba(cr, 0.502, 0.168, 0.886, 1); - cairo_arc(cr, x + radius, radius, radius, 0 * degree, 360 * degree); + cairo_new_sub_path(cr); + cairo_arc(cr, bx + radius, radius, radius, 90 * degree, 270 * degree); + cairo_arc(cr, bx + bwidth - radius, radius, radius, 270 * degree, 450 * degree); + cairo_close_path(cr); cairo_fill(cr); cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1); - cairo_arc(cr, x + radius, radius, radius - 1, 0 * degree, 360 * degree); + cairo_new_sub_path(cr); + cairo_arc(cr, bx + radius, radius, radius - line_w , 90 * degree, 270 * degree); + cairo_arc(cr, bx + bwidth - radius, radius, radius - line_w, 270 * degree, 450 * degree); + cairo_close_path(cr); cairo_stroke(cr); cairo_set_source_rgb(cr, 0.8, 0.8, 0.8); - char btn[] = { '1' + i, '\0' }; - pango_layout_set_text(layout, btn, -1); - - int text_w, text_h; - pango_layout_get_pixel_size(layout, &text_w, &text_h); - text_w = ceil(text_w / scale); - text_h = ceil(text_h / scale); - - int text_x = x + radius - (text_w / 2); - int text_y = radius - (text_h / 2); cairo_move_to(cr, text_x, text_y); pango_cairo_update_layout(cr, layout); pango_cairo_show_layout(cr, layout); g_object_unref(layout); + + int sep = 10 * scale; + bx += height + line_w * 2 + sep; } cairo_destroy(cr); - int x = draw->left_pad; - int y = draw->top_pad; - // TODO: Move these somewhere else - window_move(win, x, y); + window_move(win, draw->left_pad, draw->top_pad); window_resize(win, width0, draw->height); window_paint_surface(win, surface, width, height); cairo_surface_destroy(surface); } +void draw_set_buttons(Drawable *draw, GList *btns) +{ + draw->btns = btns; +} + void draw_destroy(Drawable *draw) { pango_font_description_free(draw->desc); @@ -1,6 +1,7 @@ #ifndef COMET_DRAW_H #define COMET_DRAW_H +#include <glib.h> #include <pango/pango-font.h> #include <pango/pango-types.h> @@ -15,10 +16,13 @@ struct Drawable { int right_pad; int top_pad; double alpha; + GList *btns; }; Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha); +void draw_set_buttons(Drawable *draw, GList *btns); + void draw_paint(Drawable *draw, Window *win); void draw_destroy(Drawable *draw); diff --git a/src/state.c b/src/state.c index bb31fa6..c2f2a4a 100644 --- a/src/state.c +++ b/src/state.c @@ -4,18 +4,25 @@ State *state_create(Window *win, Drawable *draw) { - State *state = g_malloc(sizeof(State)); - g_assert_nonnull(state); + State *state = g_malloc(sizeof(State)); + g_assert_nonnull(state); - state->win = win; - state->draw = draw; + state->win = win; + state->draw = draw; + state->btns = NULL; - return state; + return state; +} + +void state_add_button(State *state, Button *btn) +{ + state->btns = g_list_append(state->btns, btn); } void state_destroy(State *state) { - g_free(state); + g_list_free_full(state->btns, (void *)button_destroy); + g_free(state); } // vim: ts=4 sw=4 et diff --git a/src/state.h b/src/state.h index 696b657..38cc63f 100644 --- a/src/state.h +++ b/src/state.h @@ -1,18 +1,24 @@ #ifndef COMET_STATE_H #define COMET_STATE_H +#include <glib.h> + #include "window.h" #include "draw.h" +#include "button.h" typedef struct State State; struct State { - Window *win; - Drawable *draw; + Window *win; + Drawable *draw; + GList *btns; }; State *state_create(Window *win, Drawable *draw); +void state_add_button(State *state, Button *btn); + void state_destroy(State *state); #endif diff --git a/src/window.h b/src/window.h index 77f2d91..ea1209a 100644 --- a/src/window.h +++ b/src/window.h @@ -2,6 +2,8 @@ #define COMET_WINDOW_H #include <cairo.h> +#include <xcb/xcb.h> +#include <xcb/xcb_aux.h> #include "connect.h" |
