2016-10-08 12:57:59 -04:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
|
|
|
* Modified 2016 Qball Cow <qball@gmpclient.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
2016-10-09 04:13:15 -04:00
|
|
|
#include "widgets/widget.h"
|
|
|
|
#include "widgets/widget-internal.h"
|
2016-10-08 12:57:59 -04:00
|
|
|
#include "widgets/box.h"
|
2016-12-11 06:19:46 -05:00
|
|
|
#include "theme.h"
|
|
|
|
#include "settings.h"
|
2016-10-08 12:57:59 -04:00
|
|
|
|
|
|
|
#define LOG_DOMAIN "Widgets.Box"
|
2016-12-27 16:19:15 -05:00
|
|
|
const char *BOX_CLASS_NAME = "@box";
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2016-12-11 06:19:46 -05:00
|
|
|
/**
|
|
|
|
* @param box Handle to the box widget.
|
2016-12-27 16:19:15 -05:00
|
|
|
* @param spacing The spacing to apply.
|
2016-12-11 06:19:46 -05:00
|
|
|
*
|
2016-12-27 16:19:15 -05:00
|
|
|
* Set the spacing to apply between the children in pixels.
|
2016-12-11 06:19:46 -05:00
|
|
|
*/
|
2016-12-27 16:19:15 -05:00
|
|
|
void box_set_spacing ( box * box, unsigned int spacing );
|
2016-12-11 06:19:46 -05:00
|
|
|
|
2016-10-08 12:57:59 -04:00
|
|
|
struct _box
|
|
|
|
{
|
|
|
|
widget widget;
|
|
|
|
boxType type;
|
|
|
|
int max_size;
|
|
|
|
// Padding between elements
|
2016-12-27 16:19:15 -05:00
|
|
|
int spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
|
|
|
|
GList *children;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void box_update ( widget *wid );
|
|
|
|
|
2016-12-30 12:31:30 -05:00
|
|
|
static int box_get_desired_height ( widget *wid )
|
|
|
|
{
|
|
|
|
box *b = (box *)wid;
|
|
|
|
int active_widgets = 0;
|
|
|
|
int height = widget_padding_get_padding_height ( wid );
|
|
|
|
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 ) {
|
|
|
|
height += widget_get_desired_height ( child );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
height += child->h;
|
|
|
|
}
|
|
|
|
if ( active_widgets > 0 ){
|
|
|
|
height += (active_widgets - 1)*b->spacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-08 12:57:59 -04:00
|
|
|
static void vert_calculate_size ( box *b )
|
|
|
|
{
|
|
|
|
int expanding_widgets = 0;
|
|
|
|
int active_widgets = 0;
|
2016-12-30 12:31:30 -05:00
|
|
|
int rem_width = widget_padding_get_remaining_width ( WIDGET (b) );
|
|
|
|
int rem_height = widget_padding_get_remaining_height ( WIDGET (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, rem_width, child->h );
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
b->max_size = 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 ) {
|
|
|
|
expanding_widgets++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-30 12:31:30 -05:00
|
|
|
if ( child->h > 0 ){
|
|
|
|
b->max_size += child->h;
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2016-12-28 13:42:14 -05:00
|
|
|
if ( active_widgets > 0 ){
|
|
|
|
b->max_size += ( active_widgets - 1 ) * b->spacing;
|
|
|
|
}
|
2016-12-27 16:19:15 -05:00
|
|
|
if ( b->max_size > rem_height ) {
|
2016-12-30 12:31:30 -05:00
|
|
|
b->max_size = rem_height;
|
2016-10-08 12:57:59 -04:00
|
|
|
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Widgets to large (height) for box: %d %d", b->max_size, b->widget.h );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( active_widgets > 0 ) {
|
2016-12-29 11:25:24 -05:00
|
|
|
int bottom = b->widget.h - widget_padding_get_bottom ( WIDGET( b ) );
|
|
|
|
int top = widget_padding_get_top ( WIDGET ( b ) );
|
2016-12-27 16:19:15 -05:00
|
|
|
double rem = rem_height - b->max_size;
|
2016-10-08 12:57:59 -04:00
|
|
|
int index = 0;
|
|
|
|
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
|
|
|
|
widget * child = (widget *) iter->data;
|
|
|
|
if ( child->enabled == FALSE ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( child->expand == TRUE ) {
|
|
|
|
// Re-calculate to avoid round issues leaving one pixel left.
|
|
|
|
int expanding_widgets_size = ( rem ) / ( expanding_widgets - index );
|
|
|
|
if ( child->end ) {
|
|
|
|
bottom -= expanding_widgets_size;
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), bottom );
|
2016-12-27 16:19:15 -05:00
|
|
|
widget_resize ( child, rem_width, expanding_widgets_size );
|
|
|
|
bottom -= b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), top );
|
2016-10-08 12:57:59 -04:00
|
|
|
top += expanding_widgets_size;
|
2016-12-27 16:19:15 -05:00
|
|
|
widget_resize ( child, rem_width, expanding_widgets_size );
|
|
|
|
top += b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
rem -= expanding_widgets_size;
|
|
|
|
index++;
|
2016-12-30 12:31:30 -05:00
|
|
|
// b->max_size += widget_padding_get_padding_height ( child);
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
else if ( child->end ) {
|
|
|
|
bottom -= widget_get_height ( child );
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), bottom );
|
2016-12-27 16:19:15 -05:00
|
|
|
bottom -= b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), top );
|
2016-10-08 12:57:59 -04:00
|
|
|
top += widget_get_height ( child );
|
2016-12-27 16:19:15 -05:00
|
|
|
top += b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 11:25:24 -05:00
|
|
|
b->max_size += widget_padding_get_padding_height ( WIDGET (b) );
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
static void hori_calculate_size ( box *b )
|
|
|
|
{
|
|
|
|
int expanding_widgets = 0;
|
|
|
|
int active_widgets = 0;
|
2016-12-30 12:31:30 -05:00
|
|
|
int rem_width = widget_padding_get_remaining_width ( WIDGET (b) );
|
|
|
|
int rem_height = widget_padding_get_remaining_height ( WIDGET (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 );
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
b->max_size = 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 ) {
|
|
|
|
expanding_widgets++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Size used by fixed width widgets.
|
2016-12-30 12:31:30 -05:00
|
|
|
if ( child->h > 0 ){
|
2016-10-08 12:57:59 -04:00
|
|
|
b->max_size += child->w;
|
2016-12-30 12:31:30 -05:00
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2016-12-27 16:19:15 -05:00
|
|
|
b->max_size += MAX ( 0, ( ( active_widgets - 1 ) * b->spacing ) );
|
|
|
|
if ( b->max_size > (rem_width)) {
|
2016-12-30 12:31:30 -05:00
|
|
|
b->max_size = rem_width;
|
2016-10-08 12:57:59 -04:00
|
|
|
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Widgets to large (width) for box: %d %d", b->max_size, b->widget.w );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( active_widgets > 0 ) {
|
2016-12-29 11:25:24 -05:00
|
|
|
int right = b->widget.w-widget_padding_get_right ( WIDGET (b) );
|
|
|
|
int left = widget_padding_get_left ( WIDGET (b) );
|
2016-12-27 16:19:15 -05:00
|
|
|
double rem = rem_width - b->max_size;
|
2016-10-08 12:57:59 -04:00
|
|
|
int index = 0;
|
|
|
|
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
|
|
|
|
widget * child = (widget *) iter->data;
|
|
|
|
if ( child->enabled == FALSE ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( child->expand == TRUE ) {
|
|
|
|
// Re-calculate to avoid round issues leaving one pixel left.
|
|
|
|
int expanding_widgets_size = ( rem ) / ( expanding_widgets - index );
|
|
|
|
if ( child->end ) {
|
|
|
|
right -= expanding_widgets_size;
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, right, widget_padding_get_top ( WIDGET ( b ) ));
|
2016-12-27 16:19:15 -05:00
|
|
|
widget_resize ( child, expanding_widgets_size, rem_height );
|
|
|
|
right -= b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, left, widget_padding_get_top ( WIDGET ( b ) ) );
|
2016-10-08 12:57:59 -04:00
|
|
|
left += expanding_widgets_size;
|
2016-12-27 16:19:15 -05:00
|
|
|
widget_resize ( child, expanding_widgets_size, rem_height );
|
|
|
|
left += b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
rem -= expanding_widgets_size;
|
|
|
|
index++;
|
2016-12-30 12:31:30 -05:00
|
|
|
// b->max_size += widget_padding_get_padding_width ( child);
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
else if ( child->end ) {
|
|
|
|
right -= widget_get_width ( child );
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, right, widget_padding_get_top ( WIDGET ( b ) ) );
|
2016-12-27 16:19:15 -05:00
|
|
|
right -= b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-12-29 11:25:24 -05:00
|
|
|
widget_move ( child, left, widget_padding_get_top ( WIDGET ( b ) ) );
|
2016-10-08 12:57:59 -04:00
|
|
|
left += widget_get_width ( child );
|
2016-12-27 16:19:15 -05:00
|
|
|
left += b->spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 11:25:24 -05:00
|
|
|
b->max_size += widget_padding_get_padding_width ( WIDGET ( b ) );
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void box_draw ( widget *wid, cairo_t *draw )
|
|
|
|
{
|
|
|
|
box *b = (box *) wid;
|
|
|
|
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
|
|
|
|
widget * child = (widget *) iter->data;
|
|
|
|
widget_draw ( child, draw );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void box_free ( widget *wid )
|
|
|
|
{
|
|
|
|
box *b = (box *) wid;
|
|
|
|
|
|
|
|
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
|
|
|
|
widget * child = (widget *) iter->data;
|
|
|
|
widget_free ( child );
|
|
|
|
}
|
2016-10-18 07:49:24 -04:00
|
|
|
g_list_free ( b->children );
|
2016-10-08 12:57:59 -04:00
|
|
|
g_free ( b );
|
|
|
|
}
|
|
|
|
|
|
|
|
void box_add ( box *box, widget *child, gboolean expand, gboolean end )
|
|
|
|
{
|
2016-10-18 07:49:24 -04:00
|
|
|
if ( box == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-27 18:37:32 -05:00
|
|
|
// Make sure box is width/heigh enough.
|
|
|
|
if ( box->type == BOX_VERTICAL){
|
|
|
|
int width=box->widget.w;
|
2016-12-29 11:25:24 -05:00
|
|
|
width = MAX ( width, child->w+widget_padding_get_padding_width ( WIDGET ( box ) ));
|
2016-12-27 18:37:32 -05:00
|
|
|
box->widget.w = width;
|
|
|
|
} else {
|
|
|
|
int height = box->widget.h;
|
2016-12-29 11:25:24 -05:00
|
|
|
height = MAX (height, child->h+widget_padding_get_padding_height ( WIDGET ( box )));
|
2016-12-27 18:37:32 -05:00
|
|
|
box->widget.h = height;
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
child->expand = expand;
|
|
|
|
child->end = end;
|
|
|
|
child->parent = WIDGET ( box );
|
2016-11-27 10:02:37 -05:00
|
|
|
box->children = g_list_append ( box->children, (void *) child );
|
2016-10-08 12:57:59 -04:00
|
|
|
widget_update ( WIDGET ( box ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void box_resize ( widget *widget, short w, short h )
|
|
|
|
{
|
|
|
|
box *b = (box *) widget;
|
|
|
|
if ( b->widget.w != w || b->widget.h != h ) {
|
|
|
|
b->widget.w = w;
|
|
|
|
b->widget.h = h;
|
|
|
|
widget_update ( widget );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean box_clicked ( widget *wid, xcb_button_press_event_t *xbe, G_GNUC_UNUSED void *udata )
|
|
|
|
{
|
|
|
|
box *b = (box *) wid;
|
|
|
|
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
|
|
|
|
widget * child = (widget *) iter->data;
|
|
|
|
if ( !child->enabled ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( widget_intersect ( child, xbe->event_x, xbe->event_y ) ) {
|
|
|
|
xcb_button_press_event_t rel = *xbe;
|
|
|
|
rel.event_x -= child->x;
|
|
|
|
rel.event_y -= child->y;
|
|
|
|
return widget_clicked ( child, &rel );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-10-25 15:19:39 -04:00
|
|
|
static gboolean box_motion_notify ( widget *wid, xcb_motion_notify_event_t *xme )
|
|
|
|
{
|
|
|
|
box *b = (box *) wid;
|
|
|
|
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
|
|
|
|
widget * child = (widget *) iter->data;
|
|
|
|
if ( !child->enabled ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( widget_intersect ( child, xme->event_x, xme->event_y ) ) {
|
|
|
|
xcb_motion_notify_event_t rel = *xme;
|
|
|
|
rel.event_x -= child->x;
|
|
|
|
rel.event_y -= child->y;
|
|
|
|
return widget_motion_notify ( child, &rel );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2016-12-27 18:37:32 -05:00
|
|
|
box * box_create ( const char *name, boxType type )
|
2016-10-08 12:57:59 -04:00
|
|
|
{
|
|
|
|
box *b = g_malloc0 ( sizeof ( box ) );
|
2016-12-27 16:19:15 -05:00
|
|
|
// Initialize widget.
|
|
|
|
widget_init ( WIDGET(b), name, BOX_CLASS_NAME);
|
2016-12-30 12:31:30 -05:00
|
|
|
b->type = type;
|
|
|
|
b->widget.draw = box_draw;
|
|
|
|
b->widget.free = box_free;
|
|
|
|
b->widget.resize = box_resize;
|
|
|
|
b->widget.update = box_update;
|
|
|
|
b->widget.clicked = box_clicked;
|
|
|
|
b->widget.motion_notify = box_motion_notify;
|
|
|
|
b->widget.get_desired_height = box_get_desired_height;
|
|
|
|
b->widget.enabled = TRUE;
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2016-12-27 16:19:15 -05:00
|
|
|
box_set_spacing ( b, rofi_theme_get_integer ( b->widget.class_name, b->widget.name, NULL, "spacing",config.line_margin ));
|
2016-10-08 12:57:59 -04:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void box_update ( widget *wid )
|
|
|
|
{
|
|
|
|
box *b = (box *) wid;
|
|
|
|
switch ( b->type )
|
|
|
|
{
|
|
|
|
case BOX_VERTICAL:
|
|
|
|
vert_calculate_size ( b );
|
|
|
|
break;
|
|
|
|
case BOX_HORIZONTAL:
|
|
|
|
default:
|
|
|
|
hori_calculate_size ( b );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int box_get_fixed_pixels ( box *box )
|
|
|
|
{
|
2016-10-15 09:39:08 -04:00
|
|
|
if ( box != NULL ) {
|
2016-12-30 12:31:30 -05:00
|
|
|
printf("max size: %d\n", box->max_size);
|
2016-10-15 09:39:08 -04:00
|
|
|
return box->max_size;
|
|
|
|
}
|
|
|
|
return 0;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2016-12-27 16:19:15 -05:00
|
|
|
void box_set_spacing ( box * box, unsigned int spacing )
|
2016-10-08 12:57:59 -04:00
|
|
|
{
|
2016-10-15 09:39:08 -04:00
|
|
|
if ( box != NULL ) {
|
2016-12-27 16:19:15 -05:00
|
|
|
box->spacing = spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
widget_queue_redraw ( WIDGET ( box ) );
|
|
|
|
}
|
|
|
|
}
|