1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

micro-refactoring: widget.c (#1148)

* widget.c: microrefactoring

* widget.c: join enable/disable functions
This commit is contained in:
nick87720z 2020-07-11 17:48:58 +05:00 committed by GitHub
parent 641cd4ef46
commit 57ee69367d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 105 deletions

View file

@ -157,18 +157,33 @@ void widget_set_type ( widget *widget, WidgetType type );
* @returns TRUE when widget is enabled. * @returns TRUE when widget is enabled.
*/ */
gboolean widget_enabled ( widget *widget ); gboolean widget_enabled ( widget *widget );
/**
* @param widget Handle to widget
* @param enabled The new state
*
* Disable the widget.
*/
void widget_set_enabled ( widget *widget, gboolean enabled );
/** /**
* @param widget Handle to widget * @param widget Handle to widget
* *
* Disable the widget. * Disable the widget.
*/ */
void widget_disable ( widget *widget ); static inline
void widget_disable ( widget *widget ) {
widget_set_enabled ( widget, FALSE );
}
/** /**
* @param widget Handle to widget * @param widget Handle to widget
* *
* Enable the widget. * Enable the widget.
*/ */
void widget_enable ( widget *widget ); static inline
void widget_enable ( widget *widget ) {
widget_set_enabled ( widget, TRUE );
}
/** /**
* @param widget widget Handle to the widget * @param widget widget Handle to the widget

View file

@ -33,6 +33,7 @@
/** Default padding. */ /** Default padding. */
#define WIDGET_DEFAULT_PADDING 0 #define WIDGET_DEFAULT_PADDING 0
#define WIDGET_PADDING_INIT { { WIDGET_DEFAULT_PADDING, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL }, ROFI_HL_SOLID }
/* Corner radius - tl, tr, br, bl */ /* Corner radius - tl, tr, br, bl */
static void draw_rounded_rect ( cairo_t * d, static void draw_rounded_rect ( cairo_t * d,
@ -71,12 +72,10 @@ void widget_init ( widget *wid, widget *parent, WidgetType type, const char *nam
wid->type = type; wid->type = type;
wid->parent = parent; wid->parent = parent;
wid->name = g_strdup ( name ); wid->name = g_strdup ( name );
wid->def_padding = wid->def_padding = (RofiPadding) { WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT };
(RofiPadding){ { {WIDGET_DEFAULT_PADDING, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {WIDGET_DEFAULT_PADDING, ROFI_PU_PX,ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {WIDGET_DEFAULT_PADDING, ROFI_PU_PX,ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, wid->def_border = (RofiPadding) { WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT };
{ {WIDGET_DEFAULT_PADDING, ROFI_PU_PX,ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID } }; wid->def_border_radius = (RofiPadding) { WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT };
wid->def_border = (RofiPadding){ { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL} , ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID } }; wid->def_margin = (RofiPadding) { WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT, WIDGET_PADDING_INIT };
wid->def_border_radius = (RofiPadding){ { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL} , ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID } };
wid->def_margin = (RofiPadding){ { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL} , ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID }, { {0, ROFI_PU_PX, ROFI_DISTANCE_MODIFIER_NONE, NULL, NULL}, ROFI_HL_SOLID } };
wid->padding = rofi_theme_get_padding ( wid, "padding", wid->def_padding ); wid->padding = rofi_theme_get_padding ( wid, "padding", wid->def_padding );
wid->border = rofi_theme_get_padding ( wid, "border", wid->def_border ); wid->border = rofi_theme_get_padding ( wid, "border", wid->def_border );
@ -89,12 +88,15 @@ void widget_init ( widget *wid, widget *parent, WidgetType type, const char *nam
void widget_set_state ( widget *widget, const char *state ) void widget_set_state ( widget *widget, const char *state )
{ {
if ( widget == NULL ) {
return;
}
if ( g_strcmp0 ( widget->state, state ) ) { if ( g_strcmp0 ( widget->state, state ) ) {
widget->state = state; widget->state = state;
// Update border. // Update border.
widget->border = rofi_theme_get_padding ( widget, "border", widget->def_border ); widget->border = rofi_theme_get_padding ( widget, "border", widget->def_border );
widget->border_radius = rofi_theme_get_padding ( widget, "border-radius", widget->def_border_radius ); widget->border_radius = rofi_theme_get_padding ( widget, "border-radius", widget->def_border_radius );
if ( widget->set_state ) { if ( widget->set_state != NULL ) {
widget->set_state ( widget, state ); widget->set_state ( widget, state );
} }
widget_queue_redraw ( widget ); widget_queue_redraw ( widget );
@ -107,17 +109,18 @@ int widget_intersect ( const widget *widget, int x, int y )
return FALSE; return FALSE;
} }
if ( x >= ( widget->x ) && x < ( widget->x + widget->w ) ) { if ( x >= ( widget->x ) && x < ( widget->x + widget->w ) &&
if ( y >= ( widget->y ) && y < ( widget->y + widget->h ) ) { y >= ( widget->y ) && y < ( widget->y + widget->h ) ) {
return TRUE; return TRUE;
} }
}
return FALSE; return FALSE;
} }
void widget_resize ( widget *widget, short w, short h ) void widget_resize ( widget *widget, short w, short h )
{ {
if ( widget != NULL ) { if ( widget == NULL ) {
return;
}
if ( widget->resize != NULL ) { if ( widget->resize != NULL ) {
if ( widget->w != w || widget->h != h ) { if ( widget->w != w || widget->h != h ) {
widget->resize ( widget, w, h ); widget->resize ( widget, w, h );
@ -130,14 +133,14 @@ void widget_resize ( widget *widget, short w, short h )
// On a resize we always want to udpate. // On a resize we always want to udpate.
widget_queue_redraw ( widget ); widget_queue_redraw ( widget );
} }
}
void widget_move ( widget *widget, short x, short y ) void widget_move ( widget *widget, short x, short y )
{ {
if ( widget != NULL ) { if ( widget == NULL ) {
return;
}
widget->x = x; widget->x = x;
widget->y = y; widget->y = y;
} }
}
void widget_set_type ( widget *widget, WidgetType type ) void widget_set_type ( widget *widget, WidgetType type )
{ {
if ( widget == NULL ) { if ( widget == NULL ) {
@ -148,42 +151,40 @@ void widget_set_type ( widget *widget, WidgetType type )
WidgetType widget_type ( widget *widget ) WidgetType widget_type ( widget *widget )
{ {
if ( widget != NULL ) { if ( widget == NULL ) {
return widget->type;
}
return WIDGET_TYPE_UNKNOWN; return WIDGET_TYPE_UNKNOWN;
} }
return widget->type;
}
gboolean widget_enabled ( widget *widget ) gboolean widget_enabled ( widget *widget )
{ {
if ( widget != NULL ) { if ( widget == NULL ) {
return widget->enabled;
}
return FALSE; return FALSE;
} }
return widget->enabled;
}
void widget_enable ( widget *widget ) void widget_set_enabled ( widget *widget, gboolean enabled )
{ {
if ( widget && !widget->enabled ) { if ( widget == NULL ) {
widget->enabled = TRUE; return;
widget_update ( widget ); }
widget_update ( widget->parent ); if ( widget->enabled != enabled ) {
widget_queue_redraw ( widget ); widget->enabled = enabled;
}
}
void widget_disable ( widget *widget )
{
if ( widget && widget->enabled ) {
widget->enabled = FALSE;
widget_update ( widget ); widget_update ( widget );
widget_update ( widget->parent ); widget_update ( widget->parent );
widget_queue_redraw ( widget ); widget_queue_redraw ( widget );
} }
} }
void widget_draw ( widget *widget, cairo_t *d ) void widget_draw ( widget *widget, cairo_t *d )
{ {
if ( widget == NULL ) {
return;
}
// Check if enabled and if draw is implemented. // Check if enabled and if draw is implemented.
if ( widget && widget->enabled && widget->draw ) { if ( widget->enabled && widget->draw ) {
// Don't draw if there is no space. // Don't draw if there is no space.
if ( widget->h < 1 || widget->w < 1 ) { if ( widget->h < 1 || widget->w < 1 ) {
widget->need_redraw = FALSE; widget->need_redraw = FALSE;
@ -267,7 +268,7 @@ void widget_draw ( widget *widget, cairo_t *d )
cairo_restore ( d ); cairo_restore ( d );
if ( left || top || right || bottom ) { if ( left != 0 || top != 0 || right != 0 || bottom != 0 ) {
cairo_save ( d ); cairo_save ( d );
cairo_translate ( d, widget->x, widget->y ); cairo_translate ( d, widget->x, widget->y );
cairo_new_path ( d ); cairo_new_path ( d );
@ -332,65 +333,67 @@ void widget_free ( widget *wid )
if ( wid == NULL ) { if ( wid == NULL ) {
return; return;
} }
if ( wid->name ) { if ( wid->name != NULL ) {
g_free ( wid->name ); g_free ( wid->name );
} }
if ( wid->free ) { if ( wid->free != NULL ) {
wid->free ( wid ); wid->free ( wid );
} }
} }
int widget_get_height ( widget *widget ) int widget_get_height ( widget *widget )
{ {
if ( widget ) { if ( widget == NULL ) {
if ( widget->get_height ) { return 0;
return widget->get_height ( widget );
} }
if ( widget->get_height == NULL ) {
return widget->h; return widget->h;
} }
return 0; return widget->get_height ( widget );
} }
int widget_get_width ( widget *widget ) int widget_get_width ( widget *widget )
{ {
if ( widget ) { if ( widget == NULL ) {
if ( widget->get_width ) { return 0;
return widget->get_width ( widget );
} }
if ( widget->get_width == NULL ) {
return widget->w; return widget->w;
} }
return 0; return widget->get_width ( widget );
} }
int widget_get_x_pos ( widget *widget ) int widget_get_x_pos ( widget *widget )
{ {
if ( widget ) { if ( widget == NULL ) {
return widget->x;
}
return 0; return 0;
} }
return widget->x;
}
int widget_get_y_pos ( widget *widget ) int widget_get_y_pos ( widget *widget )
{ {
if ( widget ) { if ( widget == NULL ) {
return widget->y;
}
return 0; return 0;
} }
return widget->y;
}
void widget_xy_to_relative ( widget *widget, gint *x, gint *y ) void widget_xy_to_relative ( widget *widget, gint *x, gint *y )
{ {
*x -= widget->x; *x -= widget->x;
*y -= widget->y; *y -= widget->y;
if ( widget->parent != NULL ) { if ( widget->parent == NULL ) {
widget_xy_to_relative ( widget->parent, x, y ); return;
} }
widget_xy_to_relative ( widget->parent, x, y );
} }
void widget_update ( widget *widget ) void widget_update ( widget *widget )
{ {
// When (desired )size of widget changes. if ( widget == NULL ) {
if ( widget ) { return;
if ( widget->update ) {
widget->update ( widget );
} }
// When (desired )size of widget changes.
if ( widget->update != NULL ) {
widget->update ( widget );
} }
} }
@ -413,11 +416,11 @@ gboolean widget_need_redraw ( widget *wid )
if ( wid == NULL ) { if ( wid == NULL ) {
return FALSE; return FALSE;
} }
if ( wid->enabled ) { if ( ! wid->enabled ) {
return wid->need_redraw;
}
return FALSE; return FALSE;
} }
return wid->need_redraw;
}
widget *widget_find_mouse_target ( widget *wid, WidgetType type, gint x, gint y ) widget *widget_find_mouse_target ( widget *wid, WidgetType type, gint x, gint y )
{ {
@ -425,7 +428,7 @@ widget *widget_find_mouse_target ( widget *wid, WidgetType type, gint x, gint y
return NULL; return NULL;
} }
if ( wid->find_mouse_target ) { if ( wid->find_mouse_target != NULL ) {
widget *target = wid->find_mouse_target ( wid, type, x, y ); widget *target = wid->find_mouse_target ( wid, type, x, y );
if ( target != NULL ) { if ( target != NULL ) {
return target; return target;
@ -442,11 +445,11 @@ WidgetTriggerActionResult widget_trigger_action ( widget *wid, guint action, gin
if ( wid == NULL ) { if ( wid == NULL ) {
return FALSE; return FALSE;
} }
if ( wid->trigger_action ) { if ( wid->trigger_action == NULL ) {
return wid->trigger_action ( wid, action, x, y, wid->trigger_action_cb_data );
}
return FALSE; return FALSE;
} }
return wid->trigger_action ( wid, action, x, y, wid->trigger_action_cb_data );
}
void widget_set_trigger_action_handler ( widget *wid, widget_trigger_action_cb cb, void * cb_data ) void widget_set_trigger_action_handler ( widget *wid, widget_trigger_action_cb cb, void * cb_data )
{ {
@ -462,12 +465,11 @@ gboolean widget_motion_notify ( widget *wid, gint x, gint y )
if ( wid == NULL ) { if ( wid == NULL ) {
return FALSE; return FALSE;
} }
if ( wid->motion_notify ) { if ( wid->motion_notify == NULL ) {
wid->motion_notify ( wid, x, y );
}
return FALSE; return FALSE;
} }
return wid->motion_notify ( wid, x, y );
}
int widget_padding_get_left ( const widget *wid ) int widget_padding_get_left ( const widget *wid )
{ {
@ -544,21 +546,21 @@ int widget_get_desired_height ( widget *wid )
if ( wid == NULL ) { if ( wid == NULL ) {
return 0; return 0;
} }
if ( wid->get_desired_height ) { if ( wid->get_desired_height == NULL ) {
return wid->get_desired_height ( wid );
}
return wid->h; return wid->h;
} }
return wid->get_desired_height ( wid );
}
int widget_get_desired_width ( widget *wid ) int widget_get_desired_width ( widget *wid )
{ {
if ( wid == NULL ) { if ( wid == NULL ) {
return 0; return 0;
} }
if ( wid->get_desired_width ) { if ( wid->get_desired_width == NULL ) {
return wid->get_desired_width ( wid );
}
return wid->w; return wid->w;
} }
return wid->get_desired_width ( wid );
}
int widget_get_absolute_xpos ( widget *wid ) int widget_get_absolute_xpos ( widget *wid )
{ {
@ -566,7 +568,7 @@ int widget_get_absolute_xpos ( widget *wid )
return 0; return 0;
} }
int retv = wid->x; int retv = wid->x;
if ( wid->parent ) { if ( wid->parent != NULL ) {
retv += widget_get_absolute_xpos ( wid->parent ); retv += widget_get_absolute_xpos ( wid->parent );
} }
return retv; return retv;
@ -577,7 +579,7 @@ int widget_get_absolute_ypos ( widget *wid )
return 0; return 0;
} }
int retv = wid->y; int retv = wid->y;
if ( wid->parent ) { if ( wid->parent != NULL ) {
retv += widget_get_absolute_ypos ( wid->parent ); retv += widget_get_absolute_ypos ( wid->parent );
} }
return retv; return retv;