mirror of
https://github.com/davatorium/rofi.git
synced 2025-07-31 21:59:25 -04:00
Make one widget_draw function, abstract rest (textbox, scrollbar) behind it.
This commit is contained in:
parent
769dab5f66
commit
89acc7b6ed
8 changed files with 69 additions and 63 deletions
|
@ -63,14 +63,6 @@ void scrollbar_set_handle ( scrollbar *sb, unsigned int pos );
|
||||||
*/
|
*/
|
||||||
void scrollbar_set_max_value ( scrollbar *sb, unsigned int max );
|
void scrollbar_set_max_value ( scrollbar *sb, unsigned int max );
|
||||||
|
|
||||||
/**
|
|
||||||
* @param sb scrollbar object
|
|
||||||
* @param draw the cairo object used to draw itself
|
|
||||||
*
|
|
||||||
* Draw the scrollbar, used after expose event or update
|
|
||||||
*/
|
|
||||||
void scrollbar_draw ( scrollbar *sb, cairo_t *draw );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param sb scrollbar object
|
* @param sb scrollbar object
|
||||||
* @param y clicked position
|
* @param y clicked position
|
||||||
|
|
|
@ -96,14 +96,6 @@ void textbox_font ( textbox *tb, TextBoxFontType tbft );
|
||||||
*/
|
*/
|
||||||
void textbox_text ( textbox *tb, const char *text );
|
void textbox_text ( textbox *tb, const char *text );
|
||||||
|
|
||||||
/**
|
|
||||||
* @param tb Handle to the textbox
|
|
||||||
* @param draw The cairo object used to draw itself.
|
|
||||||
*
|
|
||||||
* Render the textbox.
|
|
||||||
*/
|
|
||||||
void textbox_draw ( textbox *tb, cairo_t *draw );
|
|
||||||
|
|
||||||
int textbox_keybinding ( textbox *tb, KeyBindingAction action );
|
int textbox_keybinding ( textbox *tb, KeyBindingAction action );
|
||||||
/**
|
/**
|
||||||
* @param tb Handle to the textbox
|
* @param tb Handle to the textbox
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef ROFI_WIDGET_H
|
#ifndef ROFI_WIDGET_H
|
||||||
#define ROFI_WIDGET_H
|
#define ROFI_WIDGET_H
|
||||||
|
|
||||||
|
#include <cairo.h>
|
||||||
/**
|
/**
|
||||||
* @defgroup Widgets Widgets
|
* @defgroup Widgets Widgets
|
||||||
*
|
*
|
||||||
|
@ -8,7 +9,7 @@
|
||||||
*
|
*
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct _Widget
|
||||||
{
|
{
|
||||||
/** X position relative to parent */
|
/** X position relative to parent */
|
||||||
short x;
|
short x;
|
||||||
|
@ -20,10 +21,13 @@ typedef struct
|
||||||
short h;
|
short h;
|
||||||
/** enabled or not */
|
/** enabled or not */
|
||||||
gboolean enabled;
|
gboolean enabled;
|
||||||
|
/** Function prototypes */
|
||||||
|
|
||||||
|
void ( *draw )( struct _Widget *tb, cairo_t *draw );
|
||||||
} Widget;
|
} Widget;
|
||||||
|
|
||||||
/** Macro to get widget from an implementation (e.g. textbox/scrollbar) */
|
/** Macro to get widget from an implementation (e.g. textbox/scrollbar) */
|
||||||
#define WIDGET( a ) ( a != NULL ? &( a->widget ) : NULL )
|
#define WIDGET( a ) ( a != NULL ? (Widget *) ( a ) : NULL )
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param widget The widget to check
|
* @param widget The widget to check
|
||||||
|
@ -49,5 +53,13 @@ gboolean widget_enabled ( Widget *widget );
|
||||||
void widget_disable ( Widget *widget );
|
void widget_disable ( Widget *widget );
|
||||||
void widget_enable ( Widget *widget );
|
void widget_enable ( Widget *widget );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param tb Handle to the widget
|
||||||
|
* @param draw The cairo object used to draw itself.
|
||||||
|
*
|
||||||
|
* Render the textbox.
|
||||||
|
*/
|
||||||
|
void widget_draw ( Widget *widget, cairo_t *d );
|
||||||
|
|
||||||
/*@}*/
|
/*@}*/
|
||||||
#endif // ROFI_WIDGET_H
|
#endif // ROFI_WIDGET_H
|
||||||
|
|
|
@ -29,6 +29,8 @@
|
||||||
#include "x11-helper.h"
|
#include "x11-helper.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
|
static void scrollbar_draw ( Widget *widget, cairo_t *draw );
|
||||||
|
|
||||||
scrollbar *scrollbar_create ( short x, short y, short w, short h )
|
scrollbar *scrollbar_create ( short x, short y, short w, short h )
|
||||||
{
|
{
|
||||||
scrollbar *sb = g_malloc0 ( sizeof ( scrollbar ) );
|
scrollbar *sb = g_malloc0 ( sizeof ( scrollbar ) );
|
||||||
|
@ -37,6 +39,7 @@ scrollbar *scrollbar_create ( short x, short y, short w, short h )
|
||||||
sb->widget.y = y;
|
sb->widget.y = y;
|
||||||
sb->widget.w = MAX ( 1, w );
|
sb->widget.w = MAX ( 1, w );
|
||||||
sb->widget.h = MAX ( 1, h );
|
sb->widget.h = MAX ( 1, h );
|
||||||
|
sb->widget.draw = scrollbar_draw;
|
||||||
|
|
||||||
sb->length = 10;
|
sb->length = 10;
|
||||||
sb->pos = 0;
|
sb->pos = 0;
|
||||||
|
@ -75,9 +78,9 @@ void scrollbar_set_handle_length ( scrollbar *sb, unsigned int pos_length )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scrollbar_draw ( scrollbar *sb, cairo_t *draw )
|
static void scrollbar_draw ( Widget *widget, cairo_t *draw )
|
||||||
{
|
{
|
||||||
if ( sb != NULL && sb->widget.enabled ) {
|
scrollbar *sb = (scrollbar *) widget;
|
||||||
// Calculate position and size.
|
// Calculate position and size.
|
||||||
const short bh = sb->widget.h - 0;
|
const short bh = sb->widget.h - 0;
|
||||||
float sec = ( ( bh ) / (float) sb->length );
|
float sec = ( ( bh ) / (float) sb->length );
|
||||||
|
@ -95,7 +98,6 @@ void scrollbar_draw ( scrollbar *sb, cairo_t *draw )
|
||||||
cairo_rectangle ( draw, sb->widget.x + config.line_margin, sb->widget.y + y, sb->widget.w - config.line_margin, height );
|
cairo_rectangle ( draw, sb->widget.x + config.line_margin, sb->widget.y + y, sb->widget.w - config.line_margin, height );
|
||||||
cairo_fill ( draw );
|
cairo_fill ( draw );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
void scrollbar_resize ( scrollbar *sb, int w, int h )
|
void scrollbar_resize ( scrollbar *sb, int w, int h )
|
||||||
{
|
{
|
||||||
if ( sb != NULL ) {
|
if ( sb != NULL ) {
|
||||||
|
|
|
@ -38,6 +38,8 @@
|
||||||
|
|
||||||
#define DOT_OFFSET 15
|
#define DOT_OFFSET 15
|
||||||
|
|
||||||
|
static void textbox_draw ( Widget *tb, cairo_t *draw );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Font + font color cache.
|
* Font + font color cache.
|
||||||
* Avoid re-loading font on every change on every textbox.
|
* Avoid re-loading font on every change on every textbox.
|
||||||
|
@ -75,6 +77,7 @@ textbox* textbox_create ( TextboxFlags flags, short x, short y, short w, short h
|
||||||
{
|
{
|
||||||
textbox *tb = g_slice_new0 ( textbox );
|
textbox *tb = g_slice_new0 ( textbox );
|
||||||
|
|
||||||
|
tb->widget.draw = textbox_draw;
|
||||||
tb->flags = flags;
|
tb->flags = flags;
|
||||||
|
|
||||||
tb->widget.x = x;
|
tb->widget.x = x;
|
||||||
|
@ -269,8 +272,8 @@ void textbox_free ( textbox *tb )
|
||||||
|
|
||||||
static void texbox_update ( textbox *tb )
|
static void texbox_update ( textbox *tb )
|
||||||
{
|
{
|
||||||
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
|
|
||||||
if ( tb->update ) {
|
if ( tb->update ) {
|
||||||
|
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
|
||||||
if ( tb->main_surface ) {
|
if ( tb->main_surface ) {
|
||||||
cairo_destroy ( tb->main_draw );
|
cairo_destroy ( tb->main_draw );
|
||||||
cairo_surface_destroy ( tb->main_surface );
|
cairo_surface_destroy ( tb->main_surface );
|
||||||
|
@ -353,11 +356,9 @@ static void texbox_update ( textbox *tb )
|
||||||
tb->update = FALSE;
|
tb->update = FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void textbox_draw ( textbox *tb, cairo_t *draw )
|
static void textbox_draw ( Widget *widget, cairo_t *draw )
|
||||||
{
|
{
|
||||||
if ( tb->widget.enabled == FALSE ) {
|
textbox *tb = (textbox *) widget;
|
||||||
return;
|
|
||||||
}
|
|
||||||
texbox_update ( tb );
|
texbox_update ( tb );
|
||||||
|
|
||||||
/* Write buffer */
|
/* Write buffer */
|
||||||
|
|
|
@ -189,10 +189,10 @@ static void rofi_view_calculate_window_position ( RofiViewState *state )
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ( !config.fixed_num_lines && ( config.location == WL_CENTER || config.location == WL_EAST || config.location == WL_WEST ) ) {
|
if ( !config.fixed_num_lines && ( config.location == WL_CENTER || config.location == WL_EAST || config.location == WL_WEST ) ) {
|
||||||
state->y = CacheState.mon.y + CacheState.mon.h / 2 - state->top_offset;
|
state->y = CacheState.mon.y + CacheState.mon.h / 2 - state->top_offset;
|
||||||
}else {
|
}
|
||||||
|
else {
|
||||||
// Default location is center.
|
// Default location is center.
|
||||||
state->y = CacheState.mon.y + ( CacheState.mon.h - state->height ) / 2;
|
state->y = CacheState.mon.y + ( CacheState.mon.h - state->height ) / 2;
|
||||||
}
|
}
|
||||||
|
@ -393,7 +393,7 @@ static void rofi_view_resize ( RofiViewState *state )
|
||||||
textbox_moveresize ( state->modi[j],
|
textbox_moveresize ( state->modi[j],
|
||||||
state->border + j * ( width + config.line_margin ), state->height - state->line_height - state->border,
|
state->border + j * ( width + config.line_margin ), state->height - state->line_height - state->border,
|
||||||
width, state->line_height );
|
width, state->line_height );
|
||||||
textbox_draw ( state->modi[j], CacheState.draw );
|
widget_draw ( WIDGET ( state->modi[j] ), CacheState.draw );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int entrybox_width = state->width - ( 2 * ( state->border ) );
|
int entrybox_width = state->width - ( 2 * ( state->border ) );
|
||||||
|
@ -998,7 +998,7 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d )
|
||||||
unsigned int max_elements = MIN ( a_lines, state->max_rows * columns );
|
unsigned int max_elements = MIN ( a_lines, state->max_rows * columns );
|
||||||
|
|
||||||
scrollbar_set_handle_length ( state->scrollbar, columns * state->max_rows );
|
scrollbar_set_handle_length ( state->scrollbar, columns * state->max_rows );
|
||||||
scrollbar_draw ( state->scrollbar, d );
|
widget_draw ( WIDGET ( state->scrollbar ), d );
|
||||||
// Element width.
|
// Element width.
|
||||||
unsigned int element_width = state->width - ( 2 * ( state->border ) );
|
unsigned int element_width = state->width - ( 2 * ( state->border ) );
|
||||||
if ( state->scrollbar != NULL ) {
|
if ( state->scrollbar != NULL ) {
|
||||||
|
@ -1042,7 +1042,7 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d )
|
||||||
pango_attr_list_unref ( list );
|
pango_attr_list_unref ( list );
|
||||||
g_free ( text );
|
g_free ( text );
|
||||||
}
|
}
|
||||||
textbox_draw ( state->boxes[i], d );
|
widget_draw ( WIDGET ( state->boxes[i] ), d );
|
||||||
}
|
}
|
||||||
tokenize_free ( tokens );
|
tokenize_free ( tokens );
|
||||||
state->rchanged = FALSE;
|
state->rchanged = FALSE;
|
||||||
|
@ -1055,7 +1055,7 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d )
|
||||||
mode_get_display_value ( state->sw, state->line_map[i + offset], &fstate, FALSE );
|
mode_get_display_value ( state->sw, state->line_map[i + offset], &fstate, FALSE );
|
||||||
TextBoxFontType tbft = fstate | ( ( i + offset ) == state->selected ? HIGHLIGHT : type );
|
TextBoxFontType tbft = fstate | ( ( i + offset ) == state->selected ? HIGHLIGHT : type );
|
||||||
textbox_font ( state->boxes[i], tbft );
|
textbox_font ( state->boxes[i], tbft );
|
||||||
textbox_draw ( state->boxes[i], d );
|
widget_draw ( WIDGET ( state->boxes[i] ), d );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1111,16 +1111,16 @@ void rofi_view_update ( RofiViewState *state )
|
||||||
rofi_view_draw ( state, d );
|
rofi_view_draw ( state, d );
|
||||||
}
|
}
|
||||||
if ( state->prompt_tb ) {
|
if ( state->prompt_tb ) {
|
||||||
textbox_draw ( state->prompt_tb, d );
|
widget_draw ( WIDGET ( state->prompt_tb ), d );
|
||||||
}
|
}
|
||||||
if ( state->text ) {
|
if ( state->text ) {
|
||||||
textbox_draw ( state->text, d );
|
widget_draw ( WIDGET ( state->text ), d );
|
||||||
}
|
}
|
||||||
if ( state->case_indicator ) {
|
if ( state->case_indicator ) {
|
||||||
textbox_draw ( state->case_indicator, d );
|
widget_draw ( WIDGET ( state->case_indicator ), d );
|
||||||
}
|
}
|
||||||
if ( state->message_tb ) {
|
if ( state->message_tb ) {
|
||||||
textbox_draw ( state->message_tb, d );
|
widget_draw ( WIDGET ( state->message_tb ), d );
|
||||||
}
|
}
|
||||||
color_separator ( d );
|
color_separator ( d );
|
||||||
|
|
||||||
|
@ -1152,12 +1152,12 @@ void rofi_view_update ( RofiViewState *state )
|
||||||
if ( config.sidebar_mode == TRUE ) {
|
if ( config.sidebar_mode == TRUE ) {
|
||||||
for ( unsigned int j = 0; j < state->num_modi; j++ ) {
|
for ( unsigned int j = 0; j < state->num_modi; j++ ) {
|
||||||
if ( state->modi[j] != NULL ) {
|
if ( state->modi[j] != NULL ) {
|
||||||
textbox_draw ( state->modi[j], d );
|
widget_draw ( WIDGET ( state->modi[j] ), d );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( state->overlay ) {
|
if ( state->overlay ) {
|
||||||
textbox_draw ( state->overlay, d );
|
widget_draw ( WIDGET ( state->overlay ), d );
|
||||||
}
|
}
|
||||||
state->update = FALSE;
|
state->update = FALSE;
|
||||||
|
|
||||||
|
|
|
@ -43,3 +43,10 @@ void widget_disable ( Widget *widget )
|
||||||
widget->enabled = FALSE;
|
widget->enabled = FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void widget_draw ( Widget *widget, cairo_t *d )
|
||||||
|
{
|
||||||
|
// Check if enabled and if draw is implemented.
|
||||||
|
if ( widget && widget->enabled && widget->draw ) {
|
||||||
|
widget->draw ( widget, d );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue