forked from external/yambar
exposable: add a "common" constructor and default destructor
This commit is contained in:
parent
acda1a4b21
commit
e8d8bf70d3
6 changed files with 59 additions and 33 deletions
24
particle.c
24
particle.c
|
@ -1,5 +1,7 @@
|
||||||
#include "particle.h"
|
#include "particle.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
void
|
void
|
||||||
particle_default_destroy(struct particle *particle)
|
particle_default_destroy(struct particle *particle)
|
||||||
|
@ -18,3 +20,25 @@ particle_common_new(int left_margin, int right_margin)
|
||||||
p->deco = NULL;
|
p->deco = NULL;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct exposable *
|
||||||
|
exposable_common_new(const struct particle *particle, const char *on_click)
|
||||||
|
{
|
||||||
|
struct exposable *exposable = malloc(sizeof(*exposable));
|
||||||
|
exposable->particle = particle;
|
||||||
|
exposable->private = NULL;
|
||||||
|
exposable->width = 0;
|
||||||
|
exposable->on_click = on_click != NULL ? strdup(on_click) : NULL;
|
||||||
|
exposable->destroy = &exposable_default_destroy;
|
||||||
|
exposable->on_mouse = &exposable_default_on_mouse;
|
||||||
|
exposable->begin_expose = NULL;
|
||||||
|
exposable->expose = NULL;
|
||||||
|
return exposable;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
exposable_default_destroy(struct exposable *exposable)
|
||||||
|
{
|
||||||
|
free(exposable->on_click);
|
||||||
|
free(exposable);
|
||||||
|
}
|
||||||
|
|
|
@ -35,3 +35,10 @@ struct exposable {
|
||||||
|
|
||||||
struct particle *particle_common_new(int left_margin, int right_margin);
|
struct particle *particle_common_new(int left_margin, int right_margin);
|
||||||
void particle_default_destroy(struct particle *particle);
|
void particle_default_destroy(struct particle *particle);
|
||||||
|
|
||||||
|
struct exposable *exposable_common_new(
|
||||||
|
const struct particle *particle, const char *on_click);
|
||||||
|
void exposable_default_destroy(struct exposable *exposable);
|
||||||
|
void exposable_default_on_mouse(
|
||||||
|
struct exposable *exposable, struct bar *bar,
|
||||||
|
enum mouse_event event, int x, int y);
|
||||||
|
|
|
@ -2,12 +2,6 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static void
|
|
||||||
exposable_destroy(struct exposable *exposable)
|
|
||||||
{
|
|
||||||
free(exposable);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
begin_expose(struct exposable *exposable, cairo_t *cr)
|
begin_expose(struct exposable *exposable, cairo_t *cr)
|
||||||
{
|
{
|
||||||
|
@ -27,9 +21,7 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
|
||||||
static struct exposable *
|
static struct exposable *
|
||||||
instantiate(const struct particle *particle, const struct tag_set *tags)
|
instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
{
|
{
|
||||||
struct exposable *exposable = malloc(sizeof(*exposable));
|
struct exposable *exposable = exposable_common_new(particle, NULL);
|
||||||
exposable->particle = particle;
|
|
||||||
exposable->destroy = &exposable_destroy;
|
|
||||||
exposable->begin_expose = &begin_expose;
|
exposable->begin_expose = &begin_expose;
|
||||||
exposable->expose = &expose;
|
exposable->expose = &expose;
|
||||||
return exposable;
|
return exposable;
|
||||||
|
|
|
@ -14,6 +14,20 @@ struct exposable_private {
|
||||||
int left_spacing, right_spacing;
|
int left_spacing, right_spacing;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
exposable_destroy(struct exposable *exposable)
|
||||||
|
{
|
||||||
|
struct exposable_private *e = exposable->private;
|
||||||
|
for (size_t i = 0; i < e->count; i++)
|
||||||
|
e->exposables[i]->destroy(e->exposables[i]);
|
||||||
|
|
||||||
|
free(e->exposables);
|
||||||
|
free(e->widths);
|
||||||
|
free(e);
|
||||||
|
exposable_default_destroy(exposable);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
begin_expose(struct exposable *exposable, cairo_t *cr)
|
begin_expose(struct exposable *exposable, cairo_t *cr)
|
||||||
{
|
{
|
||||||
|
@ -55,16 +69,8 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
exposable_destroy(struct exposable *exposable)
|
|
||||||
{
|
{
|
||||||
struct exposable_private *e = exposable->private;
|
|
||||||
for (size_t i = 0; i < e->count; i++)
|
|
||||||
e->exposables[i]->destroy(e->exposables[i]);
|
|
||||||
|
|
||||||
free(e->exposables);
|
|
||||||
free(e->widths);
|
|
||||||
free(e);
|
|
||||||
free(exposable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct exposable *
|
static struct exposable *
|
||||||
|
@ -84,9 +90,8 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
e->exposables[i] = pp->instantiate(pp, tags);
|
e->exposables[i] = pp->instantiate(pp, tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct exposable *exposable = malloc(sizeof(*exposable));
|
struct exposable *exposable = exposable_common_new(particle, NULL);
|
||||||
exposable->private = e;
|
exposable->private = e;
|
||||||
exposable->particle = particle;
|
|
||||||
exposable->destroy = &exposable_destroy;
|
exposable->destroy = &exposable_destroy;
|
||||||
exposable->begin_expose = &begin_expose;
|
exposable->begin_expose = &begin_expose;
|
||||||
exposable->expose = &expose;
|
exposable->expose = &expose;
|
||||||
|
|
|
@ -48,7 +48,7 @@ exposable_destroy(struct exposable *exposable)
|
||||||
e->exposables[i]->destroy(e->exposables[i]);
|
e->exposables[i]->destroy(e->exposables[i]);
|
||||||
free(e->exposables);
|
free(e->exposables);
|
||||||
free(e);
|
free(e);
|
||||||
free(exposable);
|
exposable_default_destroy(exposable);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -120,8 +120,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
|
|
||||||
assert(idx == epriv->count);
|
assert(idx == epriv->count);
|
||||||
|
|
||||||
struct exposable *exposable = malloc(sizeof(*exposable));
|
struct exposable *exposable = exposable_common_new(particle, NULL);
|
||||||
exposable->particle = particle;
|
|
||||||
exposable->private = epriv;
|
exposable->private = epriv;
|
||||||
exposable->destroy = &exposable_destroy;
|
exposable->destroy = &exposable_destroy;
|
||||||
exposable->begin_expose = &begin_expose;
|
exposable->begin_expose = &begin_expose;
|
||||||
|
|
|
@ -11,6 +11,15 @@ struct private {
|
||||||
struct rgba foreground;
|
struct rgba foreground;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
exposable_destroy(struct exposable *exposable)
|
||||||
|
{
|
||||||
|
struct private *e = exposable->private;
|
||||||
|
free(e->text);
|
||||||
|
free(e);
|
||||||
|
exposable_default_destroy(exposable);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
begin_expose(struct exposable *exposable, cairo_t *cr)
|
begin_expose(struct exposable *exposable, cairo_t *cr)
|
||||||
{
|
{
|
||||||
|
@ -68,15 +77,6 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
exposable_destroy(struct exposable *exposable)
|
|
||||||
{
|
|
||||||
struct private *e = exposable->private;
|
|
||||||
free(e->text);
|
|
||||||
free(e);
|
|
||||||
free(exposable);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sbuf {
|
struct sbuf {
|
||||||
char *s;
|
char *s;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
@ -164,8 +164,7 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
e->font = p->font;
|
e->font = p->font;
|
||||||
e->foreground = p->foreground;
|
e->foreground = p->foreground;
|
||||||
|
|
||||||
struct exposable *exposable = malloc(sizeof(*exposable));
|
struct exposable *exposable = exposable_common_new(particle, NULL);
|
||||||
exposable->particle = particle;
|
|
||||||
exposable->private = e;
|
exposable->private = e;
|
||||||
exposable->destroy = &exposable_destroy;
|
exposable->destroy = &exposable_destroy;
|
||||||
exposable->begin_expose = &begin_expose;
|
exposable->begin_expose = &begin_expose;
|
||||||
|
|
Loading…
Add table
Reference in a new issue