2017-04-15 06:32:05 -04:00
|
|
|
/*
|
2016-10-08 12:57:59 -04:00
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2023-01-14 07:02:35 -05:00
|
|
|
* Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
|
2016-10-08 12:57:59 -04:00
|
|
|
*
|
|
|
|
* 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.
|
2017-04-15 06:32:05 -04:00
|
|
|
*
|
2016-10-08 12:57:59 -04:00
|
|
|
*/
|
|
|
|
|
2018-06-12 05:45:42 -04:00
|
|
|
/** The log domain of this widget. */
|
2021-08-17 19:16:45 -04:00
|
|
|
#define G_LOG_DOMAIN "Widgets.Box"
|
2017-04-15 05:58:49 -04:00
|
|
|
|
2016-10-08 12:57:59 -04:00
|
|
|
#include "widgets/box.h"
|
2016-12-11 06:19:46 -05:00
|
|
|
#include "theme.h"
|
2021-08-17 19:16:45 -04:00
|
|
|
#include "widgets/widget-internal.h"
|
|
|
|
#include "widgets/widget.h"
|
|
|
|
#include <stdio.h>
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2017-01-01 12:08:49 -05:00
|
|
|
/** Default spacing used in the box*/
|
2021-08-17 19:16:45 -04:00
|
|
|
#define DEFAULT_SPACING 2
|
2017-01-01 09:39:02 -05:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
struct _box {
|
|
|
|
widget widget;
|
|
|
|
RofiOrientation type;
|
|
|
|
int max_size;
|
|
|
|
// RofiPadding between elements
|
|
|
|
RofiDistance spacing;
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
GList *children;
|
2016-10-08 12:57:59 -04:00
|
|
|
};
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
static void box_update(widget *wid);
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2021-09-07 13:40:07 -04:00
|
|
|
static int box_get_desired_width(widget *wid, const int height) {
|
2021-08-17 19:16:45 -04:00
|
|
|
box *b = (box *)wid;
|
|
|
|
int spacing = distance_get_pixel(b->spacing, b->type);
|
|
|
|
int width = 0;
|
2017-12-08 04:48:22 -05:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
// Allow user to override.
|
|
|
|
RofiDistance w = rofi_theme_get_distance(wid, "width", 0);
|
|
|
|
width = distance_get_pixel(w, ROFI_ORIENTATION_HORIZONTAL);
|
|
|
|
if (width > 0) {
|
|
|
|
return width;
|
|
|
|
}
|
2017-12-08 04:48:22 -05:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
if (b->type == ROFI_ORIENTATION_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) {
|
2021-09-07 13:40:07 -04:00
|
|
|
width += widget_get_desired_width(child, height);
|
2021-08-17 19:16:45 -04:00
|
|
|
continue;
|
|
|
|
}
|
2021-09-07 13:40:07 -04:00
|
|
|
width += widget_get_desired_width(child, height);
|
2017-05-28 10:10:11 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
if (active_widgets > 0) {
|
|
|
|
width += (active_widgets - 1) * spacing;
|
2017-05-28 10:10:11 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
} 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;
|
|
|
|
}
|
2021-09-07 13:40:07 -04:00
|
|
|
width = MAX(widget_get_desired_width(child, height), width);
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
width += widget_padding_get_padding_width(wid);
|
|
|
|
return width;
|
2017-05-28 10:10:11 -04:00
|
|
|
}
|
2021-09-07 13:40:07 -04:00
|
|
|
static int box_get_desired_height(widget *wid, const int width) {
|
2021-08-17 19:16:45 -04:00
|
|
|
box *b = (box *)wid;
|
|
|
|
int spacing = distance_get_pixel(b->spacing, b->type);
|
|
|
|
int height = 0;
|
|
|
|
if (b->type == ROFI_ORIENTATION_VERTICAL) {
|
|
|
|
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++;
|
2021-09-07 13:40:07 -04:00
|
|
|
height += widget_get_desired_height(child, width);
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
if (active_widgets > 0) {
|
|
|
|
height += (active_widgets - 1) * spacing;
|
2017-01-08 15:36:06 -05:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
} 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;
|
|
|
|
}
|
2021-09-07 13:40:07 -04:00
|
|
|
height = MAX(widget_get_desired_height(child, width), height);
|
2016-12-30 12:31:30 -05:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
height += widget_padding_get_padding_height(wid);
|
|
|
|
return height;
|
2016-12-30 12:31:30 -05:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
static void vert_calculate_size(box *b) {
|
|
|
|
int spacing = distance_get_pixel(b->spacing, ROFI_ORIENTATION_VERTICAL);
|
|
|
|
int expanding_widgets = 0;
|
|
|
|
int active_widgets = 0;
|
|
|
|
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) {
|
2021-09-07 13:40:07 -04:00
|
|
|
widget_resize(child, rem_width,
|
|
|
|
widget_get_desired_height(child, rem_width));
|
2016-12-30 12:31:30 -05:00
|
|
|
}
|
2021-08-17 19:16:45 -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;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
active_widgets++;
|
|
|
|
if (child->expand == TRUE) {
|
|
|
|
expanding_widgets++;
|
|
|
|
continue;
|
2016-12-28 13:42:14 -05:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
if (child->h > 0) {
|
|
|
|
b->max_size += child->h;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
if (active_widgets > 0) {
|
|
|
|
b->max_size += (active_widgets - 1) * spacing;
|
|
|
|
}
|
|
|
|
if (b->max_size > rem_height) {
|
|
|
|
b->max_size = rem_height;
|
|
|
|
g_debug("Widgets to large (height) for box: %d %d", b->max_size,
|
|
|
|
b->widget.h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (active_widgets > 0) {
|
|
|
|
int top = widget_padding_get_top(WIDGET(b));
|
|
|
|
double rem = rem_height - b->max_size;
|
|
|
|
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);
|
|
|
|
widget_move(child, widget_padding_get_left(WIDGET(b)), top);
|
|
|
|
top += expanding_widgets_size;
|
|
|
|
widget_resize(child, rem_width, expanding_widgets_size);
|
|
|
|
top += spacing;
|
|
|
|
rem -= expanding_widgets_size;
|
|
|
|
index++;
|
|
|
|
} else {
|
|
|
|
widget_move(child, widget_padding_get_left(WIDGET(b)), top);
|
|
|
|
top += widget_get_height(child);
|
|
|
|
top += spacing;
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
b->max_size += widget_padding_get_padding_height(WIDGET(b));
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
static void hori_calculate_size(box *b) {
|
|
|
|
int spacing = distance_get_pixel(b->spacing, ROFI_ORIENTATION_HORIZONTAL);
|
|
|
|
int expanding_widgets = 0;
|
|
|
|
int active_widgets = 0;
|
|
|
|
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,
|
2021-09-07 13:40:07 -04:00
|
|
|
widget_get_desired_width(child, rem_height), // child->w,
|
2021-08-17 19:16:45 -04:00
|
|
|
rem_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2016-12-30 12:31:30 -05:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
active_widgets++;
|
|
|
|
if (child->expand == TRUE) {
|
|
|
|
expanding_widgets++;
|
|
|
|
continue;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
// Size used by fixed width widgets.
|
|
|
|
if (child->h > 0) {
|
|
|
|
b->max_size += child->w;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
b->max_size += MAX(0, ((active_widgets - 1) * spacing));
|
|
|
|
if (b->max_size > (rem_width)) {
|
|
|
|
b->max_size = rem_width;
|
|
|
|
g_debug("Widgets to large (width) for box: %d %d", b->max_size,
|
|
|
|
b->widget.w);
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
if (active_widgets > 0) {
|
|
|
|
int left = widget_padding_get_left(WIDGET(b));
|
|
|
|
double rem = rem_width - b->max_size;
|
|
|
|
int index = 0;
|
|
|
|
if (rem < 0) {
|
|
|
|
rem = 0;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
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);
|
|
|
|
widget_move(child, left, widget_padding_get_top(WIDGET(b)));
|
|
|
|
left += expanding_widgets_size;
|
|
|
|
widget_resize(child, expanding_widgets_size, rem_height);
|
|
|
|
left += spacing;
|
|
|
|
rem -= expanding_widgets_size;
|
|
|
|
index++;
|
|
|
|
} else {
|
|
|
|
widget_move(child, left, widget_padding_get_top(WIDGET(b)));
|
|
|
|
left += widget_get_width(child);
|
|
|
|
left += spacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b->max_size += widget_padding_get_padding_width(WIDGET(b));
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -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);
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
static void box_free(widget *wid) {
|
|
|
|
box *b = (box *)wid;
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
for (GList *iter = g_list_first(b->children); iter != NULL;
|
|
|
|
iter = g_list_next(iter)) {
|
|
|
|
widget *child = (widget *)iter->data;
|
|
|
|
widget_free(child);
|
|
|
|
}
|
|
|
|
g_list_free(b->children);
|
|
|
|
g_free(b);
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
void box_add(box *box, widget *child, gboolean expand) {
|
|
|
|
if (box == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Make sure box is width/heigh enough.
|
|
|
|
if (box->type == ROFI_ORIENTATION_VERTICAL) {
|
|
|
|
int width = box->widget.w;
|
|
|
|
width =
|
|
|
|
MAX(width, child->w + widget_padding_get_padding_width(WIDGET(box)));
|
|
|
|
box->widget.w = width;
|
|
|
|
} else {
|
|
|
|
int height = box->widget.h;
|
|
|
|
height =
|
|
|
|
MAX(height, child->h + widget_padding_get_padding_height(WIDGET(box)));
|
|
|
|
box->widget.h = height;
|
|
|
|
}
|
|
|
|
child->expand = rofi_theme_get_boolean(child, "expand", expand);
|
|
|
|
g_assert(child->parent == WIDGET(box));
|
|
|
|
box->children = g_list_append(box->children, (void *)child);
|
|
|
|
widget_update(WIDGET(box));
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
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);
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
static widget *box_find_mouse_target(widget *wid, WidgetType type, gint x,
|
|
|
|
gint y) {
|
|
|
|
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, x, y)) {
|
|
|
|
gint rx = x - child->x;
|
|
|
|
gint ry = y - child->y;
|
|
|
|
widget *target = widget_find_mouse_target(child, type, rx, ry);
|
|
|
|
if (target != NULL) {
|
|
|
|
return target;
|
|
|
|
}
|
2016-10-25 15:19:39 -04:00
|
|
|
}
|
2021-08-17 19:16:45 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
2016-10-25 15:19:39 -04:00
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
static void box_set_state(widget *wid, const char *state) {
|
|
|
|
for (GList *iter = g_list_first(((box *)wid)->children); iter != NULL;
|
|
|
|
iter = g_list_next(iter)) {
|
|
|
|
widget *child = (widget *)iter->data;
|
|
|
|
widget_set_state(child, state);
|
|
|
|
}
|
2020-05-13 10:25:12 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
box *box_create(widget *parent, const char *name, RofiOrientation type) {
|
|
|
|
box *b = g_malloc0(sizeof(box));
|
|
|
|
// Initialize widget.
|
|
|
|
widget_init(WIDGET(b), parent, WIDGET_TYPE_UNKNOWN, name);
|
|
|
|
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.find_mouse_target = box_find_mouse_target;
|
|
|
|
b->widget.get_desired_height = box_get_desired_height;
|
|
|
|
b->widget.get_desired_width = box_get_desired_width;
|
|
|
|
b->widget.set_state = box_set_state;
|
2016-10-08 12:57:59 -04:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
b->type = rofi_theme_get_orientation(WIDGET(b), "orientation", b->type);
|
2017-05-26 14:23:46 -04:00
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
b->spacing = rofi_theme_get_distance(WIDGET(b), "spacing", DEFAULT_SPACING);
|
|
|
|
return b;
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:16:45 -04:00
|
|
|
static void box_update(widget *wid) {
|
|
|
|
box *b = (box *)wid;
|
|
|
|
switch (b->type) {
|
|
|
|
case ROFI_ORIENTATION_VERTICAL:
|
|
|
|
vert_calculate_size(b);
|
|
|
|
break;
|
|
|
|
case ROFI_ORIENTATION_HORIZONTAL:
|
|
|
|
default:
|
|
|
|
hori_calculate_size(b);
|
|
|
|
}
|
|
|
|
if (wid->parent) {
|
|
|
|
widget_update(wid->parent);
|
|
|
|
}
|
2016-10-08 12:57:59 -04:00
|
|
|
}
|