Browse Source

add ztk button

master
Alexandros Theodotou 3 years ago
parent
commit
1f39a9b69e
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 1
      inc/ztoolkit/ztk.h
  2. 180
      inc/ztoolkit/ztk_button.h
  3. 1
      inc/ztoolkit/ztk_widget.h
  4. 1
      src/meson.build
  5. 247
      src/ztk_button.c
  6. 9
      src/ztk_knob.c

1
inc/ztoolkit/ztk.h

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
#include "rsvg.h"
#include "ztk_widget.h"
#include "ztk_app.h"
#include "ztk_button.h"
#include "ztk_color.h"
#include "ztk_combo_box.h"
#include "ztk_control.h"

180
inc/ztoolkit/ztk_button.h

@ -0,0 +1,180 @@ @@ -0,0 +1,180 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __Z_TOOLKIT_ZTK_BUTTON_H__
#define __Z_TOOLKIT_ZTK_BUTTON_H__
#include "ztoolkit/ztk_color.h"
#include "ztoolkit/ztk_widget.h"
typedef struct ZtkButton ZtkButton;
/**
* Button type.
*/
typedef enum ZtkButtonType
{
/** Button is a label. */
ZTK_BTN_LBL,
/** Button has 3 SVGs, one for each state (normal,
* hover, press). */
ZTK_BTN_SVG,
/** Button will be drawn using user callback. */
ZTK_BTN_CUSTOM,
} ZtkButtonType;
/**
* Getter for whether the button is toggled or not.
*/
typedef int (*ZtkButtonToggledGetter) (
ZtkButton * btn,
void * data);
/**
* Button widget.
*/
typedef struct ZtkButton
{
/** Base widget. */
ZtkWidget base;
/** Label, if this is a label button. */
char * lbl;
/** If this button is a toggle or not. */
int is_toggle;
/** Getter to check if the button is toggled or not,
* if toggle button. */
ZtkButtonToggledGetter toggled_getter;
/**
* Activate callback.
*
* This will be called when the button is pressed
* and released (on release).
*/
ZtkWidgetActivateCallback activate_cb;
/** Button type. */
ZtkButtonType type;
/** Normal state SVG. */
ZtkRsvgHandle * normal_svg;
/** Hovered SVG. */
ZtkRsvgHandle * hover_svg;
/** Padding to add when using SVGs to control
* their size. */
int hpadding;
int vpadding;
/**
* Clicked SVG.
*
* This will also be used for when toggled.
*/
ZtkRsvgHandle * clicked_svg;
/** If this is 1, background colors will be drawn,
* regardless if there is a \ref
* ZtkButton.bg_draw_cb. */
int has_bg_colors;
ZtkColor normal_color;
ZtkColor hovered_color;
ZtkColor clicked_color;
/** Set to 1 while pressed, and the activate
* callback will be fired when released. */
int was_pressed;
/** Custom draw callback for backgrounds. */
ZtkWidgetDrawCallback bg_draw_cb;
/** Custom draw callback for completely custom
* buttons. */
ZtkWidgetDrawCallback custom_draw_cb;
} ZtkButton;
/**
* Creates a new ZtkButton.
*
* This must then be set to its type using the setters
* below.
*/
ZtkButton *
ztk_button_new (
ZtkRect * rect,
ZtkWidgetActivateCallback activate_cb,
void * data);
void
ztk_button_make_toggled (
ZtkButton * self,
ZtkButtonToggledGetter toggled_getter);
/**
* Makes a button with a label.
*/
void
ztk_button_make_labeled (
ZtkButton * self,
const char * label);
/**
* Makes a button with SVGs.
*/
void
ztk_button_make_svged (
ZtkButton * self,
int hpadding,
int vpadding,
ZtkRsvgHandle * svg_normal,
ZtkRsvgHandle * svg_hover,
ZtkRsvgHandle * svg_clicked);
/**
* Makes a customly drawn button.
*/
void
ztk_button_make_custom (
ZtkButton * self,
ZtkWidgetDrawCallback draw_cb);
void
ztk_button_set_background_colors (
ZtkButton * self,
ZtkColor * normal,
ZtkColor * hovered,
ZtkColor * clicked);
/**
* Add callback for drawing the background.
*/
void
ztk_button_add_background_callback (
ZtkButton * self,
ZtkWidgetDrawCallback draw_cb);
#endif

1
inc/ztoolkit/ztk_widget.h

@ -68,6 +68,7 @@ typedef enum ZtkWidgetType @@ -68,6 +68,7 @@ typedef enum ZtkWidgetType
{
ZTK_WIDGET_TYPE_NONE,
ZTK_WIDGET_TYPE_LABEL,
ZTK_WIDGET_TYPE_BUTTON,
ZTK_WIDGET_TYPE_KNOB,
ZTK_WIDGET_TYPE_KNOB_WITH_LABEL,
ZTK_WIDGET_TYPE_DRAWING_AREA,

1
src/meson.build

@ -19,6 +19,7 @@ ztoolkit_srcs = files([ @@ -19,6 +19,7 @@ ztoolkit_srcs = files([
'log.c',
'rsvg.c',
'ztk_app.c',
'ztk_button.c',
'ztk_color.c',
'ztk_combo_box.c',
'ztk_control.c',

247
src/ztk_button.c

@ -0,0 +1,247 @@ @@ -0,0 +1,247 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <ztoolkit/ztk.h>
static void
button_draw_cb (
ZtkWidget * widget,
cairo_t * cr,
void * data)
{
ZtkButton * self = (ZtkButton *) widget;
ZtkWidgetState state = widget->state;
if (self->has_bg_colors)
{
if (state & ZTK_WIDGET_STATE_PRESSED)
{
ztk_color_set_for_cairo (
&self->clicked_color, cr);
}
else if (state & ZTK_WIDGET_STATE_HOVERED)
{
ztk_color_set_for_cairo (
&self->hovered_color, cr);
}
else
{
ztk_color_set_for_cairo (
&self->normal_color, cr);
}
cairo_rectangle (
cr, widget->rect.x, widget->rect.y,
widget->rect.width, widget->rect.height);
cairo_fill (cr);
}
else if (self->bg_draw_cb)
{
self->bg_draw_cb (widget, cr, data);
}
#define DRAW_SVG(svg) \
ZtkRect rect = { \
widget->rect.x + self->hpadding, \
widget->rect.y + self->vpadding, \
widget->rect.width - self->hpadding * 2, \
widget->rect.height - self->vpadding * 2 }; \
ztk_rsvg_draw ( \
self->svg, cr, &rect)
switch (self->type)
{
case ZTK_BTN_LBL:
/* TODO draw label */
break;
case ZTK_BTN_SVG:
if (state & ZTK_WIDGET_STATE_PRESSED)
{
DRAW_SVG (clicked_svg);
}
else if (state & ZTK_WIDGET_STATE_HOVERED)
{
DRAW_SVG (hover_svg);
}
else
{
DRAW_SVG (normal_svg);
}
break;
case ZTK_BTN_CUSTOM:
self->custom_draw_cb (widget, cr, data);
break;
}
#undef DRAW_SVG
}
static void
update_cb (
ZtkWidget * w,
void * data)
{
}
static int
on_button_event (
ZtkWidget * w,
const PuglEventButton * btn,
void * data)
{
ZtkButton * self = (ZtkButton *) w;
ZtkWidgetState state = w->state;
if (state & ZTK_WIDGET_STATE_PRESSED)
{
if (ztk_widget_is_hit (w, btn->x, btn->y))
{
self->was_pressed = 1;
}
}
else
{
if (self->was_pressed)
{
self->activate_cb (w, data);
}
self->was_pressed = 0;
}
return 1;
}
static void
free_cb (
ZtkWidget * widget,
void * data)
{
ZtkButton * self = (ZtkButton *) widget;
free (self);
}
/**
* Creates a new ZtkButton.
*
* This must then be set to its type using the setters
* below.
*/
ZtkButton *
ztk_button_new (
ZtkRect * rect,
ZtkWidgetActivateCallback activate_cb,
void * user_data)
{
ZtkButton * self = calloc (1, sizeof (ZtkButton));
ZtkWidget * widget = (ZtkWidget *) self;
ztk_widget_init (
(ZtkWidget *) self, ZTK_WIDGET_TYPE_BUTTON, rect,
update_cb, button_draw_cb, free_cb);
self->activate_cb = activate_cb;
widget->button_event_cb = on_button_event;
widget->user_data = user_data;
return self;
}
void
ztk_button_make_toggled (
ZtkButton * self,
ZtkButtonToggledGetter toggled_getter)
{
self->is_toggle = 1;
self->toggled_getter = toggled_getter;
}
/**
* Makes a button with a label.
*/
void
ztk_button_make_labeled (
ZtkButton * self,
const char * label)
{
self->type = ZTK_BTN_LBL;
strcpy (self->lbl, label);
}
/**
* Makes a button with SVGs.
*/
void
ztk_button_make_svged (
ZtkButton * self,
int hpadding,
int vpadding,
ZtkRsvgHandle * svg_normal,
ZtkRsvgHandle * svg_hover,
ZtkRsvgHandle * svg_clicked)
{
self->type = ZTK_BTN_SVG;
self->hpadding = hpadding;
self->vpadding = vpadding;
self->normal_svg = svg_normal;
self->hover_svg = svg_hover;
self->clicked_svg = svg_clicked;
}
/**
* Makes a customly drawn button.
*/
void
ztk_button_make_custom (
ZtkButton * self,
ZtkWidgetDrawCallback draw_cb)
{
self->type = ZTK_BTN_CUSTOM;
self->custom_draw_cb = draw_cb;
}
void
ztk_button_set_background_colors (
ZtkButton * self,
ZtkColor * normal,
ZtkColor * hovered,
ZtkColor * clicked)
{
self->has_bg_colors = 1;
self->normal_color = * normal;
self->hovered_color = * hovered;
self->clicked_color = * clicked;
}
/**
* Add callback for drawing the background.
*/
void
ztk_button_add_background_callback (
ZtkButton * self,
ZtkWidgetDrawCallback draw_cb)
{
self->bg_draw_cb = draw_cb;
}

9
src/ztk_knob.c

@ -70,7 +70,7 @@ @@ -70,7 +70,7 @@
((*self->setter)(self->object, (float) real))
static void
ztk_knob_draw_cb (
draw_cb (
ZtkWidget * widget,
cairo_t * cr,
void * data)
@ -223,7 +223,7 @@ ztk_knob_draw_cb ( @@ -223,7 +223,7 @@ ztk_knob_draw_cb (
}
static void
ztk_knob_update_cb (
update_cb (
ZtkWidget * w,
void * data)
{
@ -245,7 +245,7 @@ ztk_knob_update_cb ( @@ -245,7 +245,7 @@ ztk_knob_update_cb (
}
static void
ztk_knob_free (
knob_free (
ZtkWidget * widget,
void * data)
{
@ -276,8 +276,7 @@ ztk_knob_new ( @@ -276,8 +276,7 @@ ztk_knob_new (
ZtkKnob * self = calloc (1, sizeof (ZtkKnob));
ztk_widget_init (
(ZtkWidget *) self, ZTK_WIDGET_TYPE_KNOB, rect,
ztk_knob_update_cb, ztk_knob_draw_cb,
ztk_knob_free);
update_cb, draw_cb, knob_free);
self->getter = get_val;
self->setter = set_val;

Loading…
Cancel
Save