1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-02-03 15:34:54 -05:00

Remove prompt option from textbox, simplify code, fix small drawing issue.

This commit is contained in:
Qball Cow 2014-05-21 17:33:28 +02:00
parent 90e91aca13
commit e3c20447c7
3 changed files with 34 additions and 60 deletions

View file

@ -9,7 +9,7 @@ typedef struct
short cursor; short cursor;
XftFont *font; XftFont *font;
XftColor color_fg, color_bg; XftColor color_fg, color_bg;
char *text, *prompt; char *text;
XIM xim; XIM xim;
XIC xic; XIC xic;
XGlyphInfo extents; XGlyphInfo extents;
@ -33,7 +33,7 @@ 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, char *font, char *fg, char *bg,
char *text, char *prompt ); char *text );
void textbox_free ( textbox *tb ); void textbox_free ( textbox *tb );

View file

@ -808,8 +808,7 @@ void menu_set_arrow_text ( int filtered_lines, int selected, int max_elements,
} }
} }
void menu_draw ( textbox *text, void menu_draw ( textbox **boxes,
textbox **boxes,
int max_elements, int max_elements,
int num_lines, int num_lines,
int *last_offset, int *last_offset,
@ -817,7 +816,6 @@ void menu_draw ( textbox *text,
char **filtered ) char **filtered )
{ {
int i, offset = 0; int i, offset = 0;
textbox_draw ( text );
// selected row is always visible. // selected row is always visible.
// If selected is visible do not scroll. // If selected is visible do not scroll.
@ -1068,18 +1066,25 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
// search text input // search text input
textbox *prompt_tb = textbox_create ( box, TB_AUTOHEIGHT | TB_AUTOWIDTH,
( config.padding ),
( config.padding ),
0,0,
config.menu_font, config.menu_fg, config.menu_bg,
prompt);
textbox *text = textbox_create ( box, TB_AUTOHEIGHT | TB_EDITABLE, textbox *text = textbox_create ( box, TB_AUTOHEIGHT | TB_EDITABLE,
( config.padding )+prompt_tb->w,
( config.padding ), ( config.padding ),
( config.padding ), ((config.hmode == HORIZONTAL)?
(config.hmode == HORIZONTAL)? element_width:(w - (2 * ( config.padding ) ) ))-prompt_tb->w, 1,
element_width:(w - (2 * ( config.padding ) ) ), 1,
config.menu_font, config.menu_fg, config.menu_bg, config.menu_font, config.menu_fg, config.menu_bg,
( input != NULL ) ? *input : "", prompt ); ( input != NULL ) ? *input : "" );
int line_height = text->font->ascent + text->font->descent; int line_height = text->font->ascent + text->font->descent;
textbox_show ( text ); textbox_show ( text );
textbox_show ( prompt_tb );
// filtered list display // filtered list display
@ -1095,7 +1100,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
line * line_height + config.padding + ( ( config.hmode == HORIZONTAL ) ? 0 : LINE_MARGIN ), // y line * line_height + config.padding + ( ( config.hmode == HORIZONTAL ) ? 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, "", NULL ); config.menu_font, config.menu_fg, config.menu_bg, "" );
textbox_show ( boxes[i] ); textbox_show ( boxes[i] );
} }
// Arrows // Arrows
@ -1107,13 +1112,13 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
( config.padding ), ( config.padding ),
0, 0, 0, 0,
config.menu_font, config.menu_fg, config.menu_bg, config.menu_font, config.menu_fg, config.menu_bg,
"", NULL ); "" );
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, config.menu_font, config.menu_fg, config.menu_bg,
"", NULL ); "" );
textbox_move ( arrowbox_top, textbox_move ( arrowbox_top,
w - config.padding - arrowbox_top->w, w - config.padding - arrowbox_top->w,
@ -1233,7 +1238,9 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
; ;
} }
menu_draw ( text, boxes, max_elements, num_lines, &last_offset, selected, filtered ); textbox_draw ( text );
textbox_draw( prompt_tb );
menu_draw ( boxes, max_elements, num_lines, &last_offset, selected, filtered );
menu_set_arrow_text ( filtered_lines, selected, menu_set_arrow_text ( filtered_lines, selected,
max_elements, arrowbox_top, max_elements, arrowbox_top,
arrowbox_bottom ); arrowbox_bottom );
@ -1475,7 +1482,9 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
menu_hide_arrow_text ( filtered_lines, selected, menu_hide_arrow_text ( filtered_lines, selected,
max_elements, arrowbox_top, max_elements, arrowbox_top,
arrowbox_bottom ); arrowbox_bottom );
menu_draw ( text, boxes, max_elements, num_lines, &last_offset, selected, filtered ); textbox_draw ( text );
textbox_draw( prompt_tb );
menu_draw ( boxes, max_elements, num_lines, &last_offset, selected, filtered );
menu_set_arrow_text ( filtered_lines, selected, menu_set_arrow_text ( filtered_lines, selected,
max_elements, arrowbox_top, max_elements, arrowbox_top,
arrowbox_bottom ); arrowbox_bottom );

View file

@ -50,7 +50,7 @@ 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, char *font, char *fg, char *bg,
char *text, char *prompt ) char *text )
{ {
textbox *tb = calloc ( 1, sizeof ( textbox ) ); textbox *tb = calloc ( 1, sizeof ( textbox ) );
@ -71,7 +71,6 @@ textbox* textbox_create ( Window parent,
// 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, font, fg, bg );
tb->prompt = strdup ( prompt ? prompt : "" );
textbox_text ( tb, text ? text : "" ); textbox_text ( tb, text ? text : "" );
textbox_cursor_end ( tb ); textbox_cursor_end ( tb );
@ -114,10 +113,7 @@ void textbox_font ( textbox *tb, char *font, char *fg, char *bg )
// outer code may need line height, width, etc // outer code may need line height, width, etc
void textbox_extents ( textbox *tb ) void textbox_extents ( textbox *tb )
{ {
int length = strlen ( tb->text ) + strlen ( tb->prompt ) + 1; XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) tb->text, strlen ( tb->text ), &tb->extents );
char line[length + 1 ];
sprintf ( line, "%s %s", tb->prompt, tb->text );
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) line, length, &tb->extents );
} }
// set the default text to display // set the default text to display
@ -196,11 +192,6 @@ void textbox_free ( textbox *tb )
free ( tb->text ); free ( tb->text );
} }
if ( tb->prompt )
{
free ( tb->prompt );
}
if ( tb->font ) if ( tb->font )
{ {
XftColorFree ( display, XftColorFree ( display,
@ -230,10 +221,7 @@ void textbox_draw ( textbox *tb )
// clear canvas // clear canvas
XftDrawRect ( draw, &tb->color_bg, 0, 0, tb->w, tb->h ); XftDrawRect ( draw, &tb->color_bg, 0, 0, tb->w, tb->h );
char *line = NULL, char *text = tb->text ? tb->text : "";
*text = tb->text ? tb->text : "",
*prompt = tb->prompt ? tb->prompt : "";
int text_len = strlen ( text ); int text_len = strlen ( text );
int length = text_len; int length = text_len;
int line_height = tb->font->ascent + tb->font->descent; int line_height = tb->font->ascent + tb->font->descent;
@ -245,47 +233,26 @@ void textbox_draw ( textbox *tb )
if ( tb->flags & TB_EDITABLE ) if ( tb->flags & TB_EDITABLE )
{ {
int cursor_offset = 0; int cursor_offset = 0;
int prompt_len = strlen ( prompt ) + 1; cursor_offset = MIN ( tb->cursor, text_len );
length = text_len + prompt_len;
cursor_offset = MIN ( tb->cursor + prompt_len, length );
if(asprintf ( &line, "%s %s", prompt, text ) == -1) {
// Something is _really_ wrong.. bail out
fprintf(stderr, "Failed to allocate string\n");
abort();
}
// Trailing spaces are ignored, so fix this.
// Previous version replaced all spaces, this seems to be incorrect.
for( int j = strlen(line)-1; j >= 0 && line[j] == ' '; j--) {
line[j] = '_';
}
// calc cursor position // calc cursor position
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) line, cursor_offset, &extents ); XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, cursor_offset, &extents );
// Add a small 4px offset between cursor and last glyph. // Add a small 4px offset between cursor and last glyph.
cursor_x = extents.width+4; cursor_x = extents.width + ((extents.width > 0)?2:0);
// We known size it good, no need for double check.
sprintf(line, "%s %s", prompt, text);
}
else
{
line = strdup(text);
} }
// calc full input text width // calc full input text width
// Calculate the right size, so no characters are cut off. // Calculate the right size, so no characters are cut off.
// TODO: Check performance of this. // TODO: Check performance of this.
do{ do{
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) line, length, &extents ); XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, length, &extents );
line_width = extents.width; line_width = extents.width;
if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) ) if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) )
{ {
break; break;
} }
for ( length -= 1; length > 0 && ( line[length] & 0xc0 ) == 0x80; length -= 1 ) for ( length -= 1; length > 0 && ( text[length] & 0xc0 ) == 0x80; length -= 1 )
{ {
; ;
} }
@ -304,8 +271,8 @@ void textbox_draw ( textbox *tb )
x = ( tb->w - line_width ) / 2; x = ( tb->w - line_width ) / 2;
} }
// draw the text, including any prompt in edit mode // draw the text.
XftDrawStringUtf8 ( draw, &tb->color_fg, tb->font, x, y, ( unsigned char * ) line, length ); XftDrawStringUtf8 ( draw, &tb->color_fg, tb->font, x, y, ( unsigned char * ) text, length );
// draw the cursor // draw the cursor
if ( tb->flags & TB_EDITABLE ) if ( tb->flags & TB_EDITABLE )
@ -313,9 +280,7 @@ void textbox_draw ( textbox *tb )
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 ); XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 );
} }
free(line); XftDrawRect ( draw, &tb->color_bg, tb->w, 0,0, tb->h );
XftDrawRect ( draw, &tb->color_bg, tb->w - SIDE_MARGIN, 0, SIDE_MARGIN, tb->h );
// flip canvas to window // flip canvas to window
XCopyArea ( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 ); XCopyArea ( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 );