mirror of
https://github.com/davatorium/rofi.git
synced 2024-11-18 13:54:36 -05: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 );
|
||||
|
||||
/**
|
||||
* @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 y clicked position
|
||||
|
|
|
@ -96,14 +96,6 @@ void textbox_font ( textbox *tb, TextBoxFontType tbft );
|
|||
*/
|
||||
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 );
|
||||
/**
|
||||
* @param tb Handle to the textbox
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef ROFI_WIDGET_H
|
||||
#define ROFI_WIDGET_H
|
||||
|
||||
#include <cairo.h>
|
||||
/**
|
||||
* @defgroup Widgets Widgets
|
||||
*
|
||||
|
@ -8,7 +9,7 @@
|
|||
*
|
||||
* @{
|
||||
*/
|
||||
typedef struct
|
||||
typedef struct _Widget
|
||||
{
|
||||
/** X position relative to parent */
|
||||
short x;
|
||||
|
@ -20,10 +21,13 @@ typedef struct
|
|||
short h;
|
||||
/** enabled or not */
|
||||
gboolean enabled;
|
||||
/** Function prototypes */
|
||||
|
||||
void ( *draw )( struct _Widget *tb, cairo_t *draw );
|
||||
} Widget;
|
||||
|
||||
/** 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
|
||||
|
@ -49,5 +53,13 @@ gboolean widget_enabled ( Widget *widget );
|
|||
void widget_disable ( 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
|
||||
|
|
|
@ -29,14 +29,17 @@
|
|||
#include "x11-helper.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 *sb = g_malloc0 ( sizeof ( scrollbar ) );
|
||||
|
||||
sb->widget.x = x;
|
||||
sb->widget.y = y;
|
||||
sb->widget.w = MAX ( 1, w );
|
||||
sb->widget.h = MAX ( 1, h );
|
||||
sb->widget.x = x;
|
||||
sb->widget.y = y;
|
||||
sb->widget.w = MAX ( 1, w );
|
||||
sb->widget.h = MAX ( 1, h );
|
||||
sb->widget.draw = scrollbar_draw;
|
||||
|
||||
sb->length = 10;
|
||||
sb->pos = 0;
|
||||
|
@ -75,26 +78,25 @@ 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 ) {
|
||||
// Calculate position and size.
|
||||
const short bh = sb->widget.h - 0;
|
||||
float sec = ( ( bh ) / (float) sb->length );
|
||||
short height = sb->pos_length * sec;
|
||||
short y = sb->pos * sec;
|
||||
// Set max pos.
|
||||
y = MIN ( y, bh - 2 );
|
||||
// Never go out of bar.
|
||||
height = MAX ( 2, height );
|
||||
// Cap length;
|
||||
height = MIN ( bh - y + 1, ( height ) );
|
||||
// Redraw base window
|
||||
color_separator ( draw );
|
||||
scrollbar *sb = (scrollbar *) widget;
|
||||
// Calculate position and size.
|
||||
const short bh = sb->widget.h - 0;
|
||||
float sec = ( ( bh ) / (float) sb->length );
|
||||
short height = sb->pos_length * sec;
|
||||
short y = sb->pos * sec;
|
||||
// Set max pos.
|
||||
y = MIN ( y, bh - 2 );
|
||||
// Never go out of bar.
|
||||
height = MAX ( 2, height );
|
||||
// Cap length;
|
||||
height = MIN ( bh - y + 1, ( height ) );
|
||||
// Redraw base window
|
||||
color_separator ( draw );
|
||||
|
||||
cairo_rectangle ( draw, sb->widget.x + config.line_margin, sb->widget.y + y, sb->widget.w - config.line_margin, height );
|
||||
cairo_fill ( draw );
|
||||
}
|
||||
cairo_rectangle ( draw, sb->widget.x + config.line_margin, sb->widget.y + y, sb->widget.w - config.line_margin, height );
|
||||
cairo_fill ( draw );
|
||||
}
|
||||
void scrollbar_resize ( scrollbar *sb, int w, int h )
|
||||
{
|
||||
|
|
|
@ -38,6 +38,8 @@
|
|||
|
||||
#define DOT_OFFSET 15
|
||||
|
||||
static void textbox_draw ( Widget *tb, cairo_t *draw );
|
||||
|
||||
/**
|
||||
* Font + font color cache.
|
||||
* Avoid re-loading font on every change on every textbox.
|
||||
|
@ -75,7 +77,8 @@ textbox* textbox_create ( TextboxFlags flags, short x, short y, short w, short h
|
|||
{
|
||||
textbox *tb = g_slice_new0 ( textbox );
|
||||
|
||||
tb->flags = flags;
|
||||
tb->widget.draw = textbox_draw;
|
||||
tb->flags = flags;
|
||||
|
||||
tb->widget.x = x;
|
||||
tb->widget.y = y;
|
||||
|
@ -269,8 +272,8 @@ void textbox_free ( textbox *tb )
|
|||
|
||||
static void texbox_update ( textbox *tb )
|
||||
{
|
||||
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
|
||||
if ( tb->update ) {
|
||||
unsigned int offset = ( tb->flags & TB_INDICATOR ) ? DOT_OFFSET : 0;
|
||||
if ( tb->main_surface ) {
|
||||
cairo_destroy ( tb->main_draw );
|
||||
cairo_surface_destroy ( tb->main_surface );
|
||||
|
@ -353,11 +356,9 @@ static void texbox_update ( textbox *tb )
|
|||
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 ) {
|
||||
return;
|
||||
}
|
||||
textbox *tb = (textbox *) widget;
|
||||
texbox_update ( tb );
|
||||
|
||||
/* Write buffer */
|
||||
|
|
|
@ -189,10 +189,10 @@ static void rofi_view_calculate_window_position ( RofiViewState *state )
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}else {
|
||||
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;
|
||||
}
|
||||
else {
|
||||
// Default location is center.
|
||||
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],
|
||||
state->border + j * ( width + config.line_margin ), state->height - state->line_height - state->border,
|
||||
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 ) );
|
||||
|
@ -426,7 +426,7 @@ static void rofi_view_resize ( RofiViewState *state )
|
|||
h -= state->line_height + config.line_margin;
|
||||
}
|
||||
}
|
||||
state->max_rows = MAX ( 0, ( h / element_height ) );
|
||||
state->max_rows = MAX ( 0, ( h / element_height ) );
|
||||
state->menu_lines = state->max_rows;
|
||||
state->max_elements = state->max_rows * config.menu_columns;
|
||||
// Free boxes no longer needed.
|
||||
|
@ -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 );
|
||||
|
||||
scrollbar_set_handle_length ( state->scrollbar, columns * state->max_rows );
|
||||
scrollbar_draw ( state->scrollbar, d );
|
||||
widget_draw ( WIDGET ( state->scrollbar ), d );
|
||||
// Element width.
|
||||
unsigned int element_width = state->width - ( 2 * ( state->border ) );
|
||||
if ( state->scrollbar != NULL ) {
|
||||
|
@ -1042,7 +1042,7 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d )
|
|||
pango_attr_list_unref ( list );
|
||||
g_free ( text );
|
||||
}
|
||||
textbox_draw ( state->boxes[i], d );
|
||||
widget_draw ( WIDGET ( state->boxes[i] ), d );
|
||||
}
|
||||
tokenize_free ( tokens );
|
||||
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 );
|
||||
TextBoxFontType tbft = fstate | ( ( i + offset ) == state->selected ? HIGHLIGHT : type );
|
||||
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 );
|
||||
}
|
||||
if ( state->prompt_tb ) {
|
||||
textbox_draw ( state->prompt_tb, d );
|
||||
widget_draw ( WIDGET ( state->prompt_tb ), d );
|
||||
}
|
||||
if ( state->text ) {
|
||||
textbox_draw ( state->text, d );
|
||||
widget_draw ( WIDGET ( state->text ), d );
|
||||
}
|
||||
if ( state->case_indicator ) {
|
||||
textbox_draw ( state->case_indicator, d );
|
||||
widget_draw ( WIDGET ( state->case_indicator ), d );
|
||||
}
|
||||
if ( state->message_tb ) {
|
||||
textbox_draw ( state->message_tb, d );
|
||||
widget_draw ( WIDGET ( state->message_tb ), d );
|
||||
}
|
||||
color_separator ( d );
|
||||
|
||||
|
@ -1152,12 +1152,12 @@ void rofi_view_update ( RofiViewState *state )
|
|||
if ( config.sidebar_mode == TRUE ) {
|
||||
for ( unsigned int j = 0; j < state->num_modi; j++ ) {
|
||||
if ( state->modi[j] != NULL ) {
|
||||
textbox_draw ( state->modi[j], d );
|
||||
widget_draw ( WIDGET ( state->modi[j] ), d );
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( state->overlay ) {
|
||||
textbox_draw ( state->overlay, d );
|
||||
widget_draw ( WIDGET ( state->overlay ), d );
|
||||
}
|
||||
state->update = FALSE;
|
||||
|
||||
|
@ -1361,7 +1361,7 @@ static void rofi_view_refilter ( RofiViewState *state )
|
|||
state->quit = TRUE;
|
||||
}
|
||||
scrollbar_set_max_value ( state->scrollbar, state->filtered_lines );
|
||||
if ( current_active_menu && config.fixed_num_lines == FALSE && ( CacheState.flags & MENU_NORMAL_WINDOW ) == 0 ) {
|
||||
if ( current_active_menu && config.fixed_num_lines == FALSE && ( CacheState.flags & MENU_NORMAL_WINDOW ) == 0 ) {
|
||||
int columns = config.menu_columns;
|
||||
// Calculate the number or rows. We do this by getting the num_lines rounded up to X columns
|
||||
// (num elements is better name) then dividing by columns.
|
||||
|
|
|
@ -43,3 +43,10 @@ void widget_disable ( Widget *widget )
|
|||
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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -190,7 +190,7 @@ static XrmOption xrmOptions[] = {
|
|||
"Background to use for fake transparency. (background or screenshot)", CONFIG_DEFAULT },
|
||||
{ xrm_String, "window-format", { .str = &config.window_format }, NULL,
|
||||
"Window Format. w (desktop name), t (title), n (name), r (role), c (class)", CONFIG_DEFAULT },
|
||||
{ xrm_Boolean, "click-to-exit", { .snum = &config.click_to_exit }, NULL,
|
||||
{ xrm_Boolean, "click-to-exit", { .snum = &config.click_to_exit }, NULL,
|
||||
"Click outside the window to exit", CONFIG_DEFAULT },
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue