[Widget] Add desired width option.

This commit is contained in:
Dave Davenport 2017-05-28 16:10:11 +02:00
parent b82e1c7a78
commit fa0380057b
5 changed files with 67 additions and 11 deletions

View File

@ -79,6 +79,7 @@ struct _widget
gboolean ( *motion_notify )( struct _widget *, xcb_motion_notify_event_t * );
int ( *get_desired_height )( struct _widget * );
int ( *get_desired_width )( struct _widget * );
/** widget clicked callback */
widget_clicked_cb clicked;

View File

@ -210,5 +210,13 @@ gboolean widget_motion_notify ( widget *wid, xcb_motion_notify_event_t *xme );
*/
int widget_get_desired_height ( widget *wid );
/**
* @param wid The widget handle
*
* Get the desired width of this widget recursively.
*
* @returns the desired width of the widget in pixels.
*/
int widget_get_desired_width ( widget *wid );
/*@}*/
#endif // ROFI_WIDGET_H

View File

@ -50,6 +50,42 @@ struct _box
static void box_update ( widget *wid );
static int box_get_desired_width ( widget *wid )
{
box *b = (box *) wid;
int spacing = distance_get_pixel ( b->spacing, b->type == BOX_VERTICAL ? ORIENTATION_VERTICAL : ORIENTATION_HORIZONTAL );
int width = 0;
if ( b->type == BOX_HORIZONTAL ) {
int active_widgets = 0;
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
widget * child = (widget *) iter->data;
if ( !child->enabled ) {
continue;
}
active_widgets++;
if ( child->expand == TRUE ) {
width += widget_get_desired_width ( child );
continue;
}
width += widget_get_desired_width ( child );
}
if ( active_widgets > 0 ) {
width += ( active_widgets - 1 ) * spacing;
}
}
else {
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
widget * child = (widget *) iter->data;
if ( !child->enabled ) {
continue;
}
width = MAX ( widget_get_desired_width ( child ), width );
}
}
width += widget_padding_get_padding_width ( wid );
return width;
}
static int box_get_desired_height ( widget *wid )
{
box *b = (box *) wid;
@ -160,7 +196,9 @@ static void hori_calculate_size ( box *b )
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
widget * child = (widget *) iter->data;
if ( child->enabled && child->expand == FALSE ) {
widget_resize ( child, child->w, rem_height );
widget_resize ( child,
widget_get_desired_width ( child ), //child->w,
rem_height );
}
}
b->max_size = 0;
@ -325,6 +363,7 @@ box * box_create ( const char *name, boxType type )
b->widget.clicked = box_clicked;
b->widget.motion_notify = box_motion_notify;
b->widget.get_desired_height = box_get_desired_height;
b->widget.get_desired_width = box_get_desired_width;
b->widget.enabled = rofi_theme_get_boolean ( WIDGET ( b ), "enabled", TRUE );
b->type = rofi_theme_get_boolean ( WIDGET (b), "vertical",b->type );

View File

@ -118,6 +118,7 @@ textbox* textbox_create ( const char *name, TextboxFlags flags, TextBoxFontType
tb->widget.get_width = textbox_get_width;
tb->widget.get_height = _textbox_get_height;
tb->widget.get_desired_height = textbox_get_desired_height;
tb->widget.get_desired_width = textbox_get_desired_width;
tb->flags = flags;
tb->changed = FALSE;
@ -170,14 +171,6 @@ textbox* textbox_create ( const char *name, TextboxFlags flags, TextBoxFontType
// Enabled by default
tb->widget.enabled = rofi_theme_get_boolean ( WIDGET ( tb ), "enabled", TRUE );
Distance w = rofi_theme_get_distance ( WIDGET ( tb ), "width", 0 );
int wi = distance_get_pixel ( w, ORIENTATION_HORIZONTAL );
if ( wi > 0 )
{
tb->widget.w = wi;
textbox_moveresize ( tb, tb->widget.x, tb->widget.y, tb->widget.w, tb->widget.h );
}
return tb;
}
@ -818,13 +811,21 @@ int textbox_get_desired_width ( widget *wid )
{
textbox *tb = (textbox *) wid;
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
if ( tb->flags & TB_AUTOWIDTH ) {
if ( wid->expand && tb->flags & TB_AUTOWIDTH ) {
return textbox_get_font_width ( tb ) + widget_padding_get_padding_width ( wid ) + offset;
}
Distance w = rofi_theme_get_distance ( WIDGET ( tb ), "width", 0 );
int wi = distance_get_pixel ( w, ORIENTATION_HORIZONTAL );
if ( wi > 0 )
{
return wi;
}
int width = 0;
pango_layout_set_width ( tb->layout, -1);
width = textbox_get_font_width ( tb );
// Restore.
pango_layout_set_width ( tb->layout, PANGO_SCALE * ( tb->widget.w - widget_padding_get_padding_width ( WIDGET ( tb ) ) - offset ) );
return width + widget_padding_get_padding_width ( wid ) + offset;
width = width + widget_padding_get_padding_width ( wid ) + offset;
return width;
}

View File

@ -505,3 +505,10 @@ int widget_get_desired_height ( widget *wid )
}
return 0;
}
int widget_get_desired_width ( widget *wid )
{
if ( wid && wid->get_desired_width ) {
return wid->get_desired_width ( wid );
}
return 0;
}