Browse Source

only redraw widgets hit by the expose rect

master
Alexandros Theodotou 3 years ago
parent
commit
6baf47a37f
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 6
      inc/ztoolkit/ztk_app.h
  2. 9
      inc/ztoolkit/ztk_widget.h
  3. 44
      src/ztk_app.c
  4. 29
      src/ztk_widget.c

6
inc/ztoolkit/ztk_app.h

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
#include <pugl/pugl.h>
typedef struct ZtkWidget ZtkWidget;
typedef struct ZtkRect ZtkRect;
typedef struct ZtkApp
{
@ -96,12 +97,13 @@ ztk_app_contains_widget ( @@ -96,12 +97,13 @@ ztk_app_contains_widget (
ZtkWidget * widget);
/**
* Draws each widget.
* Draws each hit widget.
*/
void
ztk_app_draw (
ZtkApp * self,
cairo_t * cr);
cairo_t * cr,
ZtkRect * rect);
void
ztk_app_idle (

9
inc/ztoolkit/ztk_widget.h

@ -271,6 +271,15 @@ ztk_widget_is_hit ( @@ -271,6 +271,15 @@ ztk_widget_is_hit (
double x,
double y);
/**
* Returns if the widget is hit by the given
* ZtkRect.
*/
int
ztk_widget_is_hit_by_rect (
ZtkWidget * self,
ZtkRect * rect);
/**
* Sets the user data.
*/

44
src/ztk_app.c

@ -53,38 +53,11 @@ on_expose ( @@ -53,38 +53,11 @@ on_expose (
cairo_t* cr = (cairo_t*)puglGetContext(view);
#if 0
const PuglRect frame = puglGetFrame(view);
const double width = frame.width;
const double height = frame.height;
#if 0
// Draw background
ztk_color_set_for_cairo (
&self->theme.bg_dark_variant2, cr);
cairo_rectangle(cr, 0, 0, width, height);
cairo_fill(cr);
/* draw border */
cairo_set_source_rgba (cr, 1,1,1, 0.4 );
const double padding = 4.0;
ztk_cairo_rounded_rectangle (
cr, padding, padding, width - padding * 2,
height - padding * 2, 1.0, 6.0);
cairo_stroke (cr);
// Draw label
const char * lbl = "Z Series";
cairo_text_extents_t extents;
cairo_set_font_size(cr, 16.0);
cairo_text_extents(cr, lbl, &extents);
cairo_move_to(
cr,
(width - extents.width) - 24.0,
padding * 2.3 + extents.height);
cairo_set_source_rgba(cr, 1, 1, 1, 1);
cairo_show_text(cr, lbl);
#endif
// Scale to view size
const double scaleX =
(width - (self->width / width)) / self->width;
@ -93,8 +66,13 @@ on_expose ( @@ -93,8 +66,13 @@ on_expose (
self->height;
cairo_scale(cr, scaleX, scaleY);
cairo_stroke (cr);
#endif
ztk_app_draw (self, cr);
ZtkRect rect = {
expose->x, expose->y, expose->width,
expose->height };
ztk_app_draw (
self, cr, &rect);
}
static int
@ -537,17 +515,19 @@ ztk_app_contains_widget ( @@ -537,17 +515,19 @@ ztk_app_contains_widget (
}
/**
* Draws each widget.
* Draws each hit widget.
*/
void
ztk_app_draw (
ZtkApp * self,
cairo_t * cr)
cairo_t * cr,
ZtkRect * rect)
{
for (int i = 0; i < self->num_widgets; i++)
{
ZtkWidget * widget = self->widgets[i];
if (!widget->visible)
if (!widget->visible ||
!ztk_widget_is_hit_by_rect (widget, rect))
continue;
widget->draw_cb (

29
src/ztk_widget.c

@ -59,6 +59,35 @@ ztk_widget_is_hit ( @@ -59,6 +59,35 @@ ztk_widget_is_hit (
y <= self->rect.y + self->rect.height;
}
/**
* Returns if the widget is hit by the given
* ZtkRect.
*/
int
ztk_widget_is_hit_by_rect (
ZtkWidget * self,
ZtkRect * rect)
{
ZtkRect * this = &self->rect;
int x_hit = 0, y_hit = 0;
if ((this->x >= rect->x &&
this->x <= rect->x + rect->width) ||
(this->x + this->width >= rect->x &&
this->x + this->width <=
rect->x + rect->width))
x_hit = 1;
if ((this->y >= rect->y &&
this->y <= rect->y + rect->height) ||
(this->y + this->height >= rect->y &&
this->y + this->height <=
rect->y + rect->height))
y_hit = 1;
return x_hit && y_hit;
}
/**
* Sets the user data.
*/

Loading…
Cancel
Save