diff --git a/include/scrollbar.h b/include/scrollbar.h index 24dee506..7b3a1205 100644 --- a/include/scrollbar.h +++ b/include/scrollbar.h @@ -32,13 +32,6 @@ typedef struct _scrollbar */ scrollbar *scrollbar_create ( short x, short y, short w, short h ); -/** - * @param sb scrollbar object - * - * Free the resources used by the scrollbar. - */ -void scrollbar_free ( scrollbar *sb ); - /** * @param sb scrollbar object * @param pos_length new length diff --git a/include/textbox.h b/include/textbox.h index 7252f311..ab704e9d 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -73,13 +73,6 @@ textbox* textbox_create ( TextboxFlags flags, short x, short y, short w, short h, TextBoxFontType tbft, const char *text ); -/** - * @param tb Handle to the textbox - * - * Free the textbox and all allocated memory. - */ -void textbox_free ( textbox *tb ); - /** * @param tb Handle to the textbox * @param tbft The style of font to render. diff --git a/include/widget.h b/include/widget.h index 6ecb7b18..522288d5 100644 --- a/include/widget.h +++ b/include/widget.h @@ -23,7 +23,8 @@ typedef struct _Widget gboolean enabled; /** Function prototypes */ - void ( *draw )( struct _Widget *tb, cairo_t *draw ); + void ( *draw )( struct _Widget *widget, cairo_t *draw ); + void ( *free )( struct _Widget *widget ); } Widget; /** Macro to get widget from an implementation (e.g. textbox/scrollbar) */ @@ -61,5 +62,12 @@ void widget_enable ( Widget *widget ); */ void widget_draw ( Widget *widget, cairo_t *d ); +/** + * @param tb Handle to the widget + * + * Free the widget and all allocated memory. + */ +void widget_free ( Widget *widget ); + /*@}*/ #endif // ROFI_WIDGET_H diff --git a/source/scrollbar.c b/source/scrollbar.c index e58ebb5c..a53b4144 100644 --- a/source/scrollbar.c +++ b/source/scrollbar.c @@ -30,16 +30,19 @@ #include "settings.h" static void scrollbar_draw ( Widget *widget, cairo_t *draw ); +static void scrollbar_free ( Widget * ); scrollbar *scrollbar_create ( short x, short y, short w, short h ) { scrollbar *sb = g_malloc0 ( sizeof ( scrollbar ) ); - sb->widget.x = x; - sb->widget.y = y; - sb->widget.w = MAX ( 1, w ); - sb->widget.h = MAX ( 1, h ); + sb->widget.x = x; + sb->widget.y = y; + sb->widget.w = MAX ( 1, w ); + sb->widget.h = MAX ( 1, h ); + sb->widget.draw = scrollbar_draw; + sb->widget.free = scrollbar_free; sb->length = 10; sb->pos = 0; @@ -50,11 +53,10 @@ scrollbar *scrollbar_create ( short x, short y, short w, short h ) return sb; } -void scrollbar_free ( scrollbar *sb ) +static void scrollbar_free ( Widget *widget ) { - if ( sb != NULL ) { - g_free ( sb ); - } + scrollbar *sb = (scrollbar *) widget; + g_free ( sb ); } void scrollbar_set_max_value ( scrollbar *sb, unsigned int max ) diff --git a/source/textbox.c b/source/textbox.c index 3429da45..10d6b044 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -38,7 +38,8 @@ #define DOT_OFFSET 15 -static void textbox_draw ( Widget *tb, cairo_t *draw ); +static void textbox_draw ( Widget *, cairo_t * ); +static void textbox_free ( Widget * ); /** * Font + font color cache. @@ -78,6 +79,7 @@ textbox* textbox_create ( TextboxFlags flags, short x, short y, short w, short h textbox *tb = g_slice_new0 ( textbox ); tb->widget.draw = textbox_draw; + tb->widget.free = textbox_free; tb->flags = flags; tb->widget.x = x; @@ -243,11 +245,9 @@ void textbox_moveresize ( textbox *tb, int x, int y, int w, int h ) } // will also unmap the window if still displayed -void textbox_free ( textbox *tb ) +static void textbox_free ( Widget *widget ) { - if ( tb == NULL ) { - return; - } + textbox *tb = (textbox *) widget; if ( tb->blink_timeout > 0 ) { g_source_remove ( tb->blink_timeout ); tb->blink_timeout = 0; diff --git a/source/view.c b/source/view.c index bb7f1cbe..db4d0c62 100644 --- a/source/view.c +++ b/source/view.c @@ -323,16 +323,14 @@ void rofi_view_free ( RofiViewState *state ) { // Do this here? // Wait for final release? - textbox_free ( state->text ); - textbox_free ( state->prompt_tb ); - textbox_free ( state->case_indicator ); - scrollbar_free ( state->scrollbar ); - if ( state->overlay ) { - textbox_free ( state->overlay ); - } + widget_free ( WIDGET ( state->text ) ); + widget_free ( WIDGET ( state->prompt_tb ) ); + widget_free ( WIDGET ( state->case_indicator ) ); + widget_free ( WIDGET ( state->scrollbar ) ); + widget_free ( WIDGET ( state->overlay ) ); for ( unsigned int i = 0; i < state->max_elements; i++ ) { - textbox_free ( state->boxes[i] ); + widget_free ( WIDGET ( state->boxes[i] ) ); } g_free ( state->boxes ); @@ -342,7 +340,7 @@ void rofi_view_free ( RofiViewState *state ) // When state is free'ed we should no longer need these. if ( config.sidebar_mode == TRUE ) { for ( unsigned int j = 0; j < state->num_modi; j++ ) { - textbox_free ( state->modi[j] ); + widget_free ( WIDGET ( state->modi[j] ) ); state->modi[j] = NULL; } g_free ( state->modi ); @@ -431,7 +429,7 @@ static void rofi_view_resize ( RofiViewState *state ) state->max_elements = state->max_rows * config.menu_columns; // Free boxes no longer needed. for ( unsigned int i = state->max_elements; i < last_length; i++ ) { - textbox_free ( state->boxes[i] ); + widget_free ( WIDGET ( state->boxes[i] ) ); } // resize array. state->boxes = g_realloc ( state->boxes, state->max_elements * sizeof ( textbox* ) ); diff --git a/source/widget.c b/source/widget.c index 7fc9d957..6d697467 100644 --- a/source/widget.c +++ b/source/widget.c @@ -50,3 +50,9 @@ void widget_draw ( Widget *widget, cairo_t *d ) widget->draw ( widget, d ); } } +void widget_free ( Widget *widget ) +{ + if ( widget ) { + widget->free ( widget ); + } +} diff --git a/test/textbox-test.c b/test/textbox-test.c index a7093d70..cb679695 100644 --- a/test/textbox-test.c +++ b/test/textbox-test.c @@ -129,6 +129,6 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) TASSERT ( box->widget.x == 12 ); TASSERT ( box->widget.y == 13 ); - textbox_free ( box ); + widget_free ( WIDGET ( box ) ); textbox_cleanup ( ); }