2016-10-08 16:57:59 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include "widgets/widget.h"
|
2016-10-09 08:13:15 +00:00
|
|
|
#include "widgets/widget-internal.h"
|
2016-12-27 21:19:15 +00:00
|
|
|
#include "theme.h"
|
|
|
|
|
|
|
|
|
|
|
|
void widget_init ( widget *widget , const char *name, const char *class_name )
|
|
|
|
{
|
|
|
|
widget->name = g_strdup(name);
|
|
|
|
widget->class_name = g_strdup(class_name);
|
2017-01-03 14:39:19 +00:00
|
|
|
widget->padding = (Padding){ {0, PW_PX}, {0, PW_PX}, {0, PW_PX}, {0, PW_PX}};
|
|
|
|
widget->padding = rofi_theme_get_padding (widget->class_name, widget->name, NULL, "padding", widget->padding);
|
|
|
|
widget->border = (Padding){ {0, PW_PX}, {0, PW_PX}, {0, PW_PX}, {0, PW_PX}};
|
|
|
|
widget->border = rofi_theme_get_padding (widget->class_name, widget->name, NULL, "border", widget->border);
|
2016-12-27 21:19:15 +00:00
|
|
|
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
|
2016-12-28 11:21:42 +00:00
|
|
|
void widget_set_state ( widget *widget, const char *state )
|
|
|
|
{
|
|
|
|
widget->state = state;
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
int widget_intersect ( const widget *widget, int x, int y )
|
|
|
|
{
|
|
|
|
if ( widget == NULL ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( x >= ( widget->x ) && x < ( widget->x + widget->w ) ) {
|
|
|
|
if ( y >= ( widget->y ) && y < ( widget->y + widget->h ) ) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void widget_resize ( widget *widget, short w, short h )
|
|
|
|
{
|
|
|
|
if ( widget != NULL ) {
|
|
|
|
if ( widget->resize != NULL ) {
|
2016-12-30 17:31:30 +00:00
|
|
|
if ( widget->w != w || widget->h != h ) {
|
|
|
|
widget->resize ( widget, w, h );
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget->w = w;
|
|
|
|
widget->h = h;
|
|
|
|
}
|
2017-01-01 01:27:51 +00:00
|
|
|
// On a resize we always want to udpate.
|
|
|
|
widget_queue_redraw ( widget );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void widget_move ( widget *widget, short x, short y )
|
|
|
|
{
|
|
|
|
if ( widget != NULL ) {
|
|
|
|
widget->x = x;
|
|
|
|
widget->y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean widget_enabled ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget != NULL ) {
|
|
|
|
return widget->enabled;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void widget_enable ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget && !widget->enabled ) {
|
|
|
|
widget->enabled = TRUE;
|
|
|
|
widget_update ( widget );
|
2016-12-30 18:59:45 +00:00
|
|
|
widget_update ( widget->parent );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void widget_disable ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget && widget->enabled ) {
|
|
|
|
widget->enabled = FALSE;
|
|
|
|
widget_update ( widget );
|
2016-12-30 18:59:45 +00:00
|
|
|
widget_update ( widget->parent );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void widget_draw ( widget *widget, cairo_t *d )
|
|
|
|
{
|
|
|
|
// Check if enabled and if draw is implemented.
|
|
|
|
if ( widget && widget->enabled && widget->draw ) {
|
2016-12-28 11:21:42 +00:00
|
|
|
// Store current state.
|
|
|
|
cairo_save ( d );
|
|
|
|
// Define a clipmask so we won't draw outside out widget.
|
|
|
|
cairo_rectangle ( d, widget->x, widget->y, widget->w, widget->h );
|
|
|
|
cairo_clip ( d );
|
2017-01-03 14:39:19 +00:00
|
|
|
|
2016-12-28 11:21:42 +00:00
|
|
|
rofi_theme_get_color ( widget->class_name, widget->name, widget->state, "background", d );
|
|
|
|
cairo_paint( d ) ;
|
|
|
|
|
|
|
|
// Set new x/y possition.
|
|
|
|
cairo_translate ( d, widget->x, widget->y );
|
|
|
|
|
2017-01-03 18:23:09 +00:00
|
|
|
int left = distance_get_pixel ( widget->border.left, ORIENTATION_HORIZONTAL );
|
|
|
|
int top = distance_get_pixel ( widget->border.top, ORIENTATION_HORIZONTAL );
|
|
|
|
int right = distance_get_pixel ( widget->border.right, ORIENTATION_VERTICAL );
|
|
|
|
int bottom = distance_get_pixel ( widget->border.bottom, ORIENTATION_VERTICAL );
|
2017-01-03 14:39:19 +00:00
|
|
|
rofi_theme_get_color ( widget->class_name, widget->name, widget->state, "foreground", d );
|
|
|
|
if ( left > 0 ) {
|
|
|
|
cairo_set_line_width ( d, left );
|
|
|
|
cairo_move_to ( d, left/2.0, 0);
|
|
|
|
cairo_line_to ( d, left/2.0, widget->h);
|
|
|
|
cairo_stroke ( d );
|
|
|
|
}
|
|
|
|
if ( right > 0 ) {
|
|
|
|
cairo_set_line_width ( d, right );
|
|
|
|
cairo_move_to ( d, widget->w - right/2.0, 0 );
|
|
|
|
cairo_line_to ( d, widget->w - right/2.0, widget->h );
|
|
|
|
cairo_stroke ( d );
|
|
|
|
}
|
|
|
|
if ( top > 0 ) {
|
|
|
|
cairo_set_line_width ( d, top );
|
|
|
|
cairo_move_to ( d, 0, top/2.0);
|
|
|
|
cairo_line_to ( d, widget->w, top/2.0);
|
|
|
|
cairo_stroke ( d );
|
|
|
|
}
|
|
|
|
if ( bottom > 0 ) {
|
|
|
|
cairo_set_line_width ( d, bottom );
|
|
|
|
cairo_move_to ( d, 0, widget->h-bottom/2.0);
|
|
|
|
cairo_line_to ( d, widget->w , widget->h-bottom/2.0);
|
|
|
|
cairo_stroke ( d );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-08 16:57:59 +00:00
|
|
|
widget->draw ( widget, d );
|
|
|
|
widget->need_redraw = FALSE;
|
2016-12-28 11:21:42 +00:00
|
|
|
|
|
|
|
cairo_restore ( d );
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-18 11:49:24 +00:00
|
|
|
void widget_free ( widget *wid )
|
2016-10-08 16:57:59 +00:00
|
|
|
{
|
2016-12-11 11:19:46 +00:00
|
|
|
if ( wid ) {
|
|
|
|
if ( wid->name ) {
|
|
|
|
g_free ( wid->name );
|
|
|
|
}
|
2016-12-27 21:19:15 +00:00
|
|
|
if ( wid->class_name ) {
|
|
|
|
g_free( wid->class_name );
|
|
|
|
}
|
2016-12-11 11:19:46 +00:00
|
|
|
if ( wid->free ) {
|
|
|
|
wid->free ( wid );
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int widget_get_height ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget ) {
|
|
|
|
if ( widget->get_height ) {
|
|
|
|
return widget->get_height ( widget );
|
|
|
|
}
|
|
|
|
return widget->h;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int widget_get_width ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget ) {
|
|
|
|
if ( widget->get_width ) {
|
|
|
|
return widget->get_width ( widget );
|
|
|
|
}
|
|
|
|
return widget->w;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-17 16:21:03 +00:00
|
|
|
int widget_get_x_pos ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget ) {
|
|
|
|
return widget->x;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int widget_get_y_pos ( widget *widget )
|
|
|
|
{
|
|
|
|
if ( widget ) {
|
|
|
|
return widget->y;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
|
|
|
|
void widget_update ( widget *widget )
|
|
|
|
{
|
|
|
|
// When (desired )size of widget changes.
|
|
|
|
if ( widget ) {
|
|
|
|
if ( widget->update ) {
|
|
|
|
widget->update ( widget );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void widget_queue_redraw ( widget *wid )
|
|
|
|
{
|
|
|
|
if ( wid ) {
|
|
|
|
widget *iter = wid;
|
|
|
|
// Find toplevel widget.
|
|
|
|
while ( iter->parent != NULL ) {
|
2016-10-13 07:22:08 +00:00
|
|
|
iter->need_redraw = TRUE;
|
2016-10-14 14:46:54 +00:00
|
|
|
iter = iter->parent;
|
2016-10-08 16:57:59 +00:00
|
|
|
}
|
|
|
|
iter->need_redraw = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean widget_need_redraw ( widget *wid )
|
|
|
|
{
|
2016-11-10 11:17:27 +00:00
|
|
|
if ( wid && wid->enabled ) {
|
2016-10-08 16:57:59 +00:00
|
|
|
return wid->need_redraw;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gboolean widget_clicked ( widget *wid, xcb_button_press_event_t *xbe )
|
|
|
|
{
|
|
|
|
if ( wid && wid->clicked ) {
|
|
|
|
return wid->clicked ( wid, xbe, wid->clicked_cb_data );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
void widget_set_clicked_handler ( widget *wid, widget_clicked_cb cb, void *udata )
|
|
|
|
{
|
|
|
|
if ( wid ) {
|
|
|
|
wid->clicked = cb;
|
|
|
|
wid->clicked_cb_data = udata;
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 19:19:39 +00:00
|
|
|
|
|
|
|
gboolean widget_motion_notify ( widget *wid, xcb_motion_notify_event_t *xme )
|
|
|
|
{
|
|
|
|
if ( wid && wid->motion_notify ) {
|
|
|
|
wid->motion_notify ( wid, xme );
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-12-11 11:19:46 +00:00
|
|
|
|
|
|
|
void widget_set_name ( widget *wid, const char *name )
|
|
|
|
{
|
|
|
|
if ( wid->name ) {
|
|
|
|
g_free(wid);
|
|
|
|
}
|
|
|
|
wid->name = g_strdup ( name );
|
|
|
|
}
|
2016-12-28 18:42:14 +00:00
|
|
|
|
|
|
|
int widget_padding_get_left ( const widget *wid )
|
|
|
|
{
|
2017-01-03 18:23:09 +00:00
|
|
|
int distance = distance_get_pixel ( wid->padding.left, ORIENTATION_HORIZONTAL );
|
|
|
|
distance += distance_get_pixel ( wid->border.left, ORIENTATION_HORIZONTAL );
|
2017-01-03 14:39:19 +00:00
|
|
|
return distance;
|
2016-12-28 18:42:14 +00:00
|
|
|
}
|
|
|
|
int widget_padding_get_right ( const widget *wid )
|
|
|
|
{
|
2017-01-03 18:23:09 +00:00
|
|
|
int distance = distance_get_pixel ( wid->padding.right, ORIENTATION_HORIZONTAL );
|
|
|
|
distance += distance_get_pixel ( wid->border.right, ORIENTATION_HORIZONTAL );
|
2017-01-03 14:39:19 +00:00
|
|
|
return distance;
|
2016-12-28 18:42:14 +00:00
|
|
|
}
|
|
|
|
int widget_padding_get_top ( const widget *wid )
|
|
|
|
{
|
2017-01-03 18:23:09 +00:00
|
|
|
int distance = distance_get_pixel ( wid->padding.top, ORIENTATION_VERTICAL );
|
|
|
|
distance += distance_get_pixel ( wid->border.top, ORIENTATION_VERTICAL );
|
2017-01-03 14:39:19 +00:00
|
|
|
return distance;
|
2016-12-28 18:42:14 +00:00
|
|
|
}
|
|
|
|
int widget_padding_get_bottom ( const widget *wid )
|
|
|
|
{
|
2017-01-03 18:23:09 +00:00
|
|
|
int distance = distance_get_pixel ( wid->padding.bottom, ORIENTATION_VERTICAL );
|
|
|
|
distance += distance_get_pixel ( wid->border.bottom, ORIENTATION_VERTICAL );
|
2017-01-03 14:39:19 +00:00
|
|
|
return distance;
|
2016-12-28 18:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int widget_padding_get_remaining_width ( const widget *wid )
|
|
|
|
{
|
|
|
|
int width = wid->w;
|
|
|
|
width -= widget_padding_get_left ( wid );
|
|
|
|
width -= widget_padding_get_right ( wid );
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
int widget_padding_get_remaining_height ( const widget *wid )
|
|
|
|
{
|
|
|
|
int height = wid->h;
|
|
|
|
height -= widget_padding_get_top ( wid );
|
|
|
|
height -= widget_padding_get_bottom ( wid );
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
int widget_padding_get_padding_height ( const widget *wid )
|
|
|
|
{
|
2016-12-30 17:31:30 +00:00
|
|
|
int height = 0;
|
2016-12-28 18:42:14 +00:00
|
|
|
height += widget_padding_get_top ( wid );
|
|
|
|
height += widget_padding_get_bottom ( wid );
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
int widget_padding_get_padding_width ( const widget *wid )
|
|
|
|
{
|
2016-12-30 17:31:30 +00:00
|
|
|
int width = 0;
|
2016-12-28 18:42:14 +00:00
|
|
|
width += widget_padding_get_left ( wid );
|
|
|
|
width += widget_padding_get_right ( wid );
|
|
|
|
return width;
|
|
|
|
}
|
2016-12-30 17:31:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
int widget_get_desired_height ( widget *wid )
|
|
|
|
{
|
2016-12-30 18:59:45 +00:00
|
|
|
if ( wid && wid->get_desired_height )
|
2016-12-30 17:31:30 +00:00
|
|
|
{
|
|
|
|
return wid->get_desired_height ( wid );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|