mirror of
https://github.com/davatorium/rofi.git
synced 2025-01-27 15:25:24 -05:00
Re-use XftFont instead of recreating it each and every time.
This commit is contained in:
parent
71b6f8becd
commit
56a911129d
4 changed files with 133 additions and 64 deletions
|
@ -26,18 +26,23 @@ typedef enum
|
|||
TB_EDITABLE = 1 << 19,
|
||||
} TextboxFlags;
|
||||
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
NORMAL,
|
||||
HIGHLIGHT,
|
||||
ACTIVE_HIGHLIGHT,
|
||||
ACTIVE
|
||||
} TextBoxFontType;
|
||||
|
||||
textbox* textbox_create ( Window parent,
|
||||
TextboxFlags flags,
|
||||
short x, short y, short w, short h,
|
||||
char *font, char *fg, char *bg,
|
||||
TextBoxFontType tbft,
|
||||
char *text );
|
||||
|
||||
void textbox_free ( textbox *tb );
|
||||
|
||||
void textbox_font ( textbox *tb, char *font, char *fg, char *bg );
|
||||
void textbox_font ( textbox *tb, TextBoxFontType tbft );
|
||||
|
||||
void textbox_text ( textbox *tb, char *text );
|
||||
void textbox_show ( textbox *tb );
|
||||
|
@ -48,4 +53,12 @@ int textbox_keypress ( textbox *tb, XEvent *ev );
|
|||
void textbox_cursor_end ( textbox *tb );
|
||||
void textbox_move ( textbox *tb, int x, int y );
|
||||
void textbox_hide ( textbox *tb );
|
||||
|
||||
|
||||
void textbox_setup (
|
||||
const char *font_str, const char *font_active_str,
|
||||
const char *bg, const char *fg,
|
||||
const char *hlbg, const char *hlfg
|
||||
);
|
||||
void textbox_cleanup ();
|
||||
#endif //__TEXTBOX_H__
|
||||
|
|
|
@ -59,7 +59,7 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
|
|||
qsort ( list, length, sizeof ( _element* ), __element_sort_func );
|
||||
|
||||
// Get minimum index.
|
||||
int min_value = list[length-1]->index;
|
||||
int min_value = list[length - 1]->index;
|
||||
|
||||
// Set the max length of the list.
|
||||
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
|
||||
|
@ -67,7 +67,7 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
|
|||
// Write out entries.
|
||||
for ( unsigned int iter = 0; iter < length; iter++ )
|
||||
{
|
||||
fprintf ( fd, "%ld %s\n", list[iter]->index-min_value, list[iter]->name );
|
||||
fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -445,8 +445,7 @@ static int take_keyboard ( Window w )
|
|||
return 1;
|
||||
}
|
||||
|
||||
|
||||
struct timespec rsl = { 0, 100000L };
|
||||
struct timespec rsl = { 0, 50000L };
|
||||
nanosleep ( &rsl, NULL );
|
||||
}
|
||||
|
||||
|
@ -799,17 +798,13 @@ void menu_set_arrow_text ( int filtered_lines, int selected, int max_elements,
|
|||
if ( page != 0 && npages > 1 )
|
||||
{
|
||||
textbox_show ( arrowbox_top );
|
||||
textbox_font ( arrowbox_top, config.menu_font,
|
||||
( entry == 0 ) ? config.menu_hlfg : config.menu_fg,
|
||||
( entry == 0 ) ? config.menu_hlbg : config.menu_bg );
|
||||
textbox_font ( arrowbox_top, ( entry == 0 ) ? NORMAL : HIGHLIGHT );
|
||||
textbox_draw ( arrowbox_top );
|
||||
}
|
||||
if ( ( npages - 1 ) != page && npages > 1 )
|
||||
{
|
||||
textbox_show ( arrowbox_bottom );
|
||||
textbox_font ( arrowbox_bottom, config.menu_font,
|
||||
( entry == ( max_elements - 1 ) ) ? config.menu_hlfg : config.menu_fg,
|
||||
( entry == ( max_elements - 1 ) ) ? config.menu_hlbg : config.menu_bg );
|
||||
textbox_font ( arrowbox_bottom, ( entry == 0 ) ? NORMAL : HIGHLIGHT );
|
||||
textbox_draw ( arrowbox_bottom );
|
||||
}
|
||||
}
|
||||
|
@ -841,28 +836,23 @@ void menu_draw ( textbox **boxes,
|
|||
{
|
||||
if ( ( i + offset ) >= num_lines || filtered[i + offset] == NULL )
|
||||
{
|
||||
textbox_font ( boxes[i], config.menu_font,
|
||||
config.menu_fg,
|
||||
config.menu_bg );
|
||||
textbox_font ( boxes[i], NORMAL );
|
||||
textbox_text ( boxes[i], "" );
|
||||
}
|
||||
else
|
||||
{
|
||||
char *text = filtered[i + offset];
|
||||
char *f_fg = ( i + offset ) == selected ? config.menu_hlfg : config.menu_fg;
|
||||
char *f_bg = ( i + offset ) == selected ? config.menu_hlbg : config.menu_bg;
|
||||
char *font = config.menu_font;
|
||||
char *text = filtered[i + offset];
|
||||
TextBoxFontType tbft = ( i + offset ) == selected ? HIGHLIGHT : NORMAL;
|
||||
char *font = config.menu_font;
|
||||
// Check for active
|
||||
if ( text[0] == '*' )
|
||||
{
|
||||
// Skip the '*'
|
||||
text++;
|
||||
// Use the active version of font.
|
||||
font = active_font;
|
||||
tbft = ( tbft == HIGHLIGHT ) ? ACTIVE_HIGHLIGHT : ACTIVE;
|
||||
}
|
||||
textbox_font ( boxes[i], font,
|
||||
f_fg,
|
||||
f_bg );
|
||||
textbox_font ( boxes[i], tbft );
|
||||
textbox_text ( boxes[i], text );
|
||||
}
|
||||
|
||||
|
@ -1100,7 +1090,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
|
|||
( config.padding ),
|
||||
( config.padding ),
|
||||
0, 0,
|
||||
config.menu_font, config.menu_fg, config.menu_bg,
|
||||
NORMAL,
|
||||
prompt );
|
||||
|
||||
textbox *text = textbox_create ( box, TB_AUTOHEIGHT | TB_EDITABLE,
|
||||
|
@ -1108,7 +1098,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
|
|||
( config.padding ),
|
||||
( ( config.hmode == TRUE ) ?
|
||||
element_width : ( w - ( 2 * ( config.padding ) ) ) ) - prompt_tb->w, 1,
|
||||
config.menu_font, config.menu_fg, config.menu_bg,
|
||||
NORMAL,
|
||||
( input != NULL ) ? *input : "" );
|
||||
|
||||
int line_height = text->font->ascent + text->font->descent;
|
||||
|
@ -1130,7 +1120,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
|
|||
line * line_height + config.padding + ( ( config.hmode == TRUE ) ? 0 : LINE_MARGIN ), // y
|
||||
element_width, // w
|
||||
line_height, // h
|
||||
config.menu_font, config.menu_fg, config.menu_bg, "" );
|
||||
NORMAL, "" );
|
||||
textbox_show ( boxes[i] );
|
||||
}
|
||||
// Arrows
|
||||
|
@ -1141,13 +1131,13 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
|
|||
( config.padding ),
|
||||
( config.padding ),
|
||||
0, 0,
|
||||
config.menu_font, config.menu_fg, config.menu_bg,
|
||||
NORMAL,
|
||||
"↑" );
|
||||
arrowbox_bottom = textbox_create ( box, TB_AUTOHEIGHT | TB_AUTOWIDTH,
|
||||
( config.padding ),
|
||||
( config.padding ),
|
||||
0, 0,
|
||||
config.menu_font, config.menu_fg, config.menu_bg,
|
||||
NORMAL,
|
||||
"↓" );
|
||||
|
||||
textbox_move ( arrowbox_top,
|
||||
|
@ -1530,6 +1520,9 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
|
|||
*input = strdup ( text->text );
|
||||
|
||||
textbox_free ( text );
|
||||
textbox_free ( prompt_tb );
|
||||
textbox_free ( arrowbox_bottom );
|
||||
textbox_free ( arrowbox_top );
|
||||
|
||||
for ( i = 0; i < max_elements; i++ )
|
||||
{
|
||||
|
@ -2004,6 +1997,7 @@ static void parse_cmd_options ( int argc, char ** argv )
|
|||
|
||||
static void cleanup ()
|
||||
{
|
||||
textbox_cleanup ();
|
||||
// Cleanup
|
||||
if ( display != NULL )
|
||||
{
|
||||
|
@ -2146,6 +2140,12 @@ int main ( int argc, char *argv[] )
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
textbox_setup ( config.menu_font, active_font,
|
||||
config.menu_bg, config.menu_fg,
|
||||
config.menu_hlbg,
|
||||
config.menu_hlfg );
|
||||
|
||||
XFreeModifiermap ( modmap );
|
||||
|
||||
cache_client = winlist_new ();
|
||||
|
|
124
source/textbox.c
124
source/textbox.c
|
@ -43,13 +43,22 @@
|
|||
|
||||
extern Display *display;
|
||||
|
||||
|
||||
|
||||
XftFont *font = NULL;
|
||||
XftFont *font_active = NULL;
|
||||
XftColor color_fg;
|
||||
XftColor color_bg;
|
||||
XftColor color_hlfg;
|
||||
XftColor color_hlbg;
|
||||
|
||||
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h );
|
||||
|
||||
// Xft text box, optionally editable
|
||||
textbox* textbox_create ( Window parent,
|
||||
TextboxFlags flags,
|
||||
short x, short y, short w, short h,
|
||||
char *font, char *fg, char *bg,
|
||||
TextBoxFontType tbft,
|
||||
char *text )
|
||||
{
|
||||
textbox *tb = calloc ( 1, sizeof ( textbox ) );
|
||||
|
@ -64,12 +73,12 @@ textbox* textbox_create ( Window parent,
|
|||
|
||||
XColor color;
|
||||
Colormap map = DefaultColormap ( display, DefaultScreen ( display ) );
|
||||
unsigned int cp = XAllocNamedColor ( display, map, bg, &color, &color ) ? color.pixel : None;
|
||||
unsigned int cp = ( tbft == NORMAL ) ? color_bg.pixel : color_hlbg.pixel;
|
||||
|
||||
tb->window = XCreateSimpleWindow ( display, tb->parent, tb->x, tb->y, tb->w, tb->h, 0, None, cp );
|
||||
|
||||
// need to preload the font to calc line height
|
||||
textbox_font ( tb, font, fg, bg );
|
||||
textbox_font ( tb, tbft );
|
||||
|
||||
textbox_text ( tb, text ? text : "" );
|
||||
textbox_cursor_end ( tb );
|
||||
|
@ -88,26 +97,32 @@ textbox* textbox_create ( Window parent,
|
|||
}
|
||||
|
||||
// set an Xft font by name
|
||||
void textbox_font ( textbox *tb, char *font, char *fg, char *bg )
|
||||
void textbox_font ( textbox *tb, TextBoxFontType tbft )
|
||||
{
|
||||
if ( tb->font )
|
||||
switch ( tbft )
|
||||
{
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&tb->color_fg );
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&tb->color_bg );
|
||||
|
||||
XftFontClose ( display, tb->font );
|
||||
case HIGHLIGHT:
|
||||
tb->font = font;
|
||||
tb->color_bg = color_hlbg;
|
||||
tb->color_fg = color_hlfg;
|
||||
break;
|
||||
case ACTIVE:
|
||||
tb->font = font_active;
|
||||
tb->color_bg = color_bg;
|
||||
tb->color_fg = color_fg;
|
||||
break;
|
||||
case ACTIVE_HIGHLIGHT:
|
||||
tb->font = font_active;
|
||||
tb->color_bg = color_hlbg;
|
||||
tb->color_fg = color_hlfg;
|
||||
break;
|
||||
case NORMAL:
|
||||
default:
|
||||
tb->font = font;
|
||||
tb->color_bg = color_bg;
|
||||
tb->color_fg = color_fg;
|
||||
break;
|
||||
}
|
||||
|
||||
tb->font = XftFontOpenName ( display, DefaultScreen ( display ), font );
|
||||
|
||||
XftColorAllocName ( display, DefaultVisual ( display, DefaultScreen ( display ) ), DefaultColormap ( display, DefaultScreen ( display ) ), fg, &tb->color_fg );
|
||||
XftColorAllocName ( display, DefaultVisual ( display, DefaultScreen ( display ) ), DefaultColormap ( display, DefaultScreen ( display ) ), bg, &tb->color_bg );
|
||||
}
|
||||
|
||||
// outer code may need line height, width, etc
|
||||
|
@ -192,20 +207,6 @@ void textbox_free ( textbox *tb )
|
|||
free ( tb->text );
|
||||
}
|
||||
|
||||
if ( tb->font )
|
||||
{
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&tb->color_fg );
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&tb->color_bg );
|
||||
|
||||
XftFontClose ( display, tb->font );
|
||||
}
|
||||
|
||||
XDestroyWindow ( display, tb->window );
|
||||
free ( tb );
|
||||
}
|
||||
|
@ -428,3 +429,58 @@ int textbox_keypress ( textbox *tb, XEvent *ev )
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* Font setup.
|
||||
*/
|
||||
|
||||
void textbox_setup (
|
||||
const char *font_str, const char *font_active_str,
|
||||
const char *bg, const char *fg,
|
||||
const char *hlbg, const char *hlfg
|
||||
)
|
||||
{
|
||||
font = XftFontOpenName ( display, DefaultScreen ( display ), font_str );
|
||||
font_active = XftFontOpenName ( display, DefaultScreen ( display ), font_active_str );
|
||||
|
||||
XftColorAllocName ( display, DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ), fg, &color_fg );
|
||||
XftColorAllocName ( display, DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ), bg, &color_bg );
|
||||
XftColorAllocName ( display, DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ), hlfg, &color_hlfg );
|
||||
XftColorAllocName ( display, DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ), hlbg, &color_hlbg );
|
||||
}
|
||||
|
||||
|
||||
void textbox_cleanup ()
|
||||
{
|
||||
if ( font != NULL )
|
||||
{
|
||||
XftFontClose ( display, font );
|
||||
font = NULL;
|
||||
|
||||
XftFontClose ( display, font_active );
|
||||
font_active = NULL;
|
||||
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&color_fg );
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&color_bg );
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&color_hlfg );
|
||||
XftColorFree ( display,
|
||||
DefaultVisual ( display, DefaultScreen ( display ) ),
|
||||
DefaultColormap ( display, DefaultScreen ( display ) ),
|
||||
&color_hlbg );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue