1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-02-10 15:44:41 -05:00

Re-use XftFont instead of recreating it each and every time.

This commit is contained in:
QC 2014-05-25 23:32:06 +02:00
parent 71b6f8becd
commit 56a911129d
4 changed files with 133 additions and 64 deletions

View file

@ -26,18 +26,23 @@ typedef enum
TB_EDITABLE = 1 << 19, TB_EDITABLE = 1 << 19,
} TextboxFlags; } TextboxFlags;
typedef enum
{
NORMAL,
HIGHLIGHT,
ACTIVE_HIGHLIGHT,
ACTIVE
} TextBoxFontType;
textbox* textbox_create ( Window parent, textbox* textbox_create ( Window parent,
TextboxFlags flags, TextboxFlags flags,
short x, short y, short w, short h, short x, short y, short w, short h,
char *font, char *fg, char *bg, TextBoxFontType tbft,
char *text ); char *text );
void textbox_free ( textbox *tb ); 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_text ( textbox *tb, char *text );
void textbox_show ( textbox *tb ); void textbox_show ( textbox *tb );
@ -48,4 +53,12 @@ int textbox_keypress ( textbox *tb, XEvent *ev );
void textbox_cursor_end ( textbox *tb ); void textbox_cursor_end ( textbox *tb );
void textbox_move ( textbox *tb, int x, int y ); void textbox_move ( textbox *tb, int x, int y );
void textbox_hide ( textbox *tb ); 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__ #endif //__TEXTBOX_H__

View file

@ -59,7 +59,7 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
qsort ( list, length, sizeof ( _element* ), __element_sort_func ); qsort ( list, length, sizeof ( _element* ), __element_sort_func );
// Get minimum index. // Get minimum index.
int min_value = list[length-1]->index; int min_value = list[length - 1]->index;
// Set the max length of the list. // Set the max length of the list.
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length; 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. // Write out entries.
for ( unsigned int iter = 0; iter < length; iter++ ) 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 );
} }
} }

View file

@ -445,8 +445,7 @@ static int take_keyboard ( Window w )
return 1; return 1;
} }
struct timespec rsl = { 0, 50000L };
struct timespec rsl = { 0, 100000L };
nanosleep ( &rsl, NULL ); 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 ) if ( page != 0 && npages > 1 )
{ {
textbox_show ( arrowbox_top ); textbox_show ( arrowbox_top );
textbox_font ( arrowbox_top, config.menu_font, textbox_font ( arrowbox_top, ( entry == 0 ) ? NORMAL : HIGHLIGHT );
( entry == 0 ) ? config.menu_hlfg : config.menu_fg,
( entry == 0 ) ? config.menu_hlbg : config.menu_bg );
textbox_draw ( arrowbox_top ); textbox_draw ( arrowbox_top );
} }
if ( ( npages - 1 ) != page && npages > 1 ) if ( ( npages - 1 ) != page && npages > 1 )
{ {
textbox_show ( arrowbox_bottom ); textbox_show ( arrowbox_bottom );
textbox_font ( arrowbox_bottom, config.menu_font, textbox_font ( arrowbox_bottom, ( entry == 0 ) ? NORMAL : HIGHLIGHT );
( entry == ( max_elements - 1 ) ) ? config.menu_hlfg : config.menu_fg,
( entry == ( max_elements - 1 ) ) ? config.menu_hlbg : config.menu_bg );
textbox_draw ( arrowbox_bottom ); textbox_draw ( arrowbox_bottom );
} }
} }
@ -841,28 +836,23 @@ void menu_draw ( textbox **boxes,
{ {
if ( ( i + offset ) >= num_lines || filtered[i + offset] == NULL ) if ( ( i + offset ) >= num_lines || filtered[i + offset] == NULL )
{ {
textbox_font ( boxes[i], config.menu_font, textbox_font ( boxes[i], NORMAL );
config.menu_fg,
config.menu_bg );
textbox_text ( boxes[i], "" ); textbox_text ( boxes[i], "" );
} }
else else
{ {
char *text = filtered[i + offset]; char *text = filtered[i + offset];
char *f_fg = ( i + offset ) == selected ? config.menu_hlfg : config.menu_fg; TextBoxFontType tbft = ( i + offset ) == selected ? HIGHLIGHT : NORMAL;
char *f_bg = ( i + offset ) == selected ? config.menu_hlbg : config.menu_bg; char *font = config.menu_font;
char *font = config.menu_font;
// Check for active // Check for active
if ( text[0] == '*' ) if ( text[0] == '*' )
{ {
// Skip the '*' // Skip the '*'
text++; text++;
// Use the active version of font. // Use the active version of font.
font = active_font; tbft = ( tbft == HIGHLIGHT ) ? ACTIVE_HIGHLIGHT : ACTIVE;
} }
textbox_font ( boxes[i], font, textbox_font ( boxes[i], tbft );
f_fg,
f_bg );
textbox_text ( boxes[i], text ); 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 ),
( config.padding ), ( config.padding ),
0, 0, 0, 0,
config.menu_font, config.menu_fg, config.menu_bg, NORMAL,
prompt ); prompt );
textbox *text = textbox_create ( box, TB_AUTOHEIGHT | TB_EDITABLE, 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.padding ),
( ( config.hmode == TRUE ) ? ( ( config.hmode == TRUE ) ?
element_width : ( w - ( 2 * ( config.padding ) ) ) ) - prompt_tb->w, 1, element_width : ( w - ( 2 * ( config.padding ) ) ) ) - prompt_tb->w, 1,
config.menu_font, config.menu_fg, config.menu_bg, NORMAL,
( input != NULL ) ? *input : "" ); ( input != NULL ) ? *input : "" );
int line_height = text->font->ascent + text->font->descent; 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 line * line_height + config.padding + ( ( config.hmode == TRUE ) ? 0 : LINE_MARGIN ), // y
element_width, // w element_width, // w
line_height, // h line_height, // h
config.menu_font, config.menu_fg, config.menu_bg, "" ); NORMAL, "" );
textbox_show ( boxes[i] ); textbox_show ( boxes[i] );
} }
// Arrows // Arrows
@ -1141,13 +1131,13 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
( config.padding ), ( config.padding ),
( config.padding ), ( config.padding ),
0, 0, 0, 0,
config.menu_font, config.menu_fg, config.menu_bg, NORMAL,
"" ); "" );
arrowbox_bottom = textbox_create ( box, TB_AUTOHEIGHT | TB_AUTOWIDTH, arrowbox_bottom = textbox_create ( box, TB_AUTOHEIGHT | TB_AUTOWIDTH,
( config.padding ), ( config.padding ),
( config.padding ), ( config.padding ),
0, 0, 0, 0,
config.menu_font, config.menu_fg, config.menu_bg, NORMAL,
"" ); "" );
textbox_move ( arrowbox_top, textbox_move ( arrowbox_top,
@ -1530,6 +1520,9 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
*input = strdup ( text->text ); *input = strdup ( text->text );
textbox_free ( text ); textbox_free ( text );
textbox_free ( prompt_tb );
textbox_free ( arrowbox_bottom );
textbox_free ( arrowbox_top );
for ( i = 0; i < max_elements; i++ ) for ( i = 0; i < max_elements; i++ )
{ {
@ -2004,6 +1997,7 @@ static void parse_cmd_options ( int argc, char ** argv )
static void cleanup () static void cleanup ()
{ {
textbox_cleanup ();
// Cleanup // Cleanup
if ( display != NULL ) 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 ); XFreeModifiermap ( modmap );
cache_client = winlist_new (); cache_client = winlist_new ();

View file

@ -43,13 +43,22 @@
extern Display *display; 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 ); void textbox_moveresize ( textbox *tb, int x, int y, int w, int h );
// Xft text box, optionally editable // Xft text box, optionally editable
textbox* textbox_create ( Window parent, textbox* textbox_create ( Window parent,
TextboxFlags flags, TextboxFlags flags,
short x, short y, short w, short h, short x, short y, short w, short h,
char *font, char *fg, char *bg, TextBoxFontType tbft,
char *text ) char *text )
{ {
textbox *tb = calloc ( 1, sizeof ( textbox ) ); textbox *tb = calloc ( 1, sizeof ( textbox ) );
@ -64,12 +73,12 @@ textbox* textbox_create ( Window parent,
XColor color; XColor color;
Colormap map = DefaultColormap ( display, DefaultScreen ( display ) ); 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 ); 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 // 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_text ( tb, text ? text : "" );
textbox_cursor_end ( tb ); textbox_cursor_end ( tb );
@ -88,26 +97,32 @@ textbox* textbox_create ( Window parent,
} }
// set an Xft font by name // 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, case HIGHLIGHT:
DefaultVisual ( display, DefaultScreen ( display ) ), tb->font = font;
DefaultColormap ( display, DefaultScreen ( display ) ), tb->color_bg = color_hlbg;
&tb->color_fg ); tb->color_fg = color_hlfg;
XftColorFree ( display, break;
DefaultVisual ( display, DefaultScreen ( display ) ), case ACTIVE:
DefaultColormap ( display, DefaultScreen ( display ) ), tb->font = font_active;
&tb->color_bg ); tb->color_bg = color_bg;
tb->color_fg = color_fg;
XftFontClose ( display, tb->font ); 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 // outer code may need line height, width, etc
@ -192,20 +207,6 @@ void textbox_free ( textbox *tb )
free ( tb->text ); 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 ); XDestroyWindow ( display, tb->window );
free ( tb ); free ( tb );
} }
@ -428,3 +429,58 @@ int textbox_keypress ( textbox *tb, XEvent *ev )
return 0; 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 );
}
}