From a8843af9f39c43be0db1b3c61e6767fc681a03e6 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Mon, 12 Jan 2015 19:14:46 +0100 Subject: [PATCH] Add support for alternating row colors. --- config/config.def.c | 2 ++ doc/rofi-manpage.markdown | 23 +++++++----- include/rofi.h | 2 ++ include/textbox.h | 7 ++-- source/rofi.c | 25 ++++++++----- source/textbox.c | 23 ++++++++++-- source/xrmoptions.c | 74 ++++++++++++++++++++------------------- test/textbox-test.c | 2 +- 8 files changed, 99 insertions(+), 59 deletions(-) diff --git a/config/config.def.c b/config/config.def.c index 53a19eab..f0dc8375 100644 --- a/config/config.def.c +++ b/config/config.def.c @@ -50,6 +50,8 @@ Settings config = { .menu_fg = "#222222", /** Background color */ .menu_bg = "#f2f1f0", + /** Background color alternate row */ + .menu_bg_alt = NULL, /** Foreground color (selected) */ .menu_hlfg = "#ffffff", /** Background color (selected) */ diff --git a/doc/rofi-manpage.markdown b/doc/rofi-manpage.markdown index 7339190c..552bb34a 100644 --- a/doc/rofi-manpage.markdown +++ b/doc/rofi-manpage.markdown @@ -7,13 +7,14 @@ rofi - A window switcher, run dialog and dmenu replacement ## SYNOPSIS **rofi** [ -width *pct_scr* ] [ -lines *lines* ] [ -columns *columns* ] [ -font *pangofont* ] [ -fg -*color* ] [ -bg *color* ] [ -hlfg *color* ] [ -hlbg *color* ] [ -key *combo* ] [ -dkey *comdo* ] [ --rkey *comdo* ] [ -terminal *terminal* ] [ -location *position* ] [ -hmode ] [ -fixed-num-lines ] [ --padding *padding* ] [ -opacity *opacity%* ] [ -display *display* ] [ -bc *color* ] [ -bw *width* ] -[ -dmenu [ -p *prompt* ] ] [ -ssh-client *client* ] [ -ssh-command *command* ] [ -now ] [ -rnow ] [ --snow ] [ -version ] [ -help] [ -dump-xresources ] [ -disable-history ] [ -levenshtein-sort ] [ --show *mode* ] [ -switcher *mode1,mode2* ] [ -e *message*] [ -sep *separator* ] [ -eh *element -height* ] [ -l *selected line* ] [ -run-list-command *cmd* ] +*color* ] [ -bg *color* ] [ -bgalt *color* ] [ -hlfg *color* ] [ -hlbg *color* ] [ -key *combo* ] [ +-dkey *comdo* ] [ -rkey *comdo* ] [ -terminal *terminal* ] [ -location *position* ] [ -hmode ] [ +-fixed-num-lines ] [ -padding *padding* ] [ -opacity *opacity%* ] [ -display *display* ] [ -bc +*color* ] [ -bw *width* ] [ -dmenu [ -p *prompt* ] ] [ -ssh-client *client* ] [ -ssh-command +*command* ] [ -now ] [ -rnow ] [ -snow ] [ -version ] [ -help] [ -dump-xresources ] [ +-disable-history ] [ -levenshtein-sort ] [ -show *mode* ] [ -switcher *mode1,mode2* ] [ -e +*message*] [ -sep *separator* ] [ -eh *element height* ] [ -l *selected line* ] [ -run-list-command +*cmd* ] ## DESCRIPTION @@ -126,8 +127,14 @@ The default key combinations are: Set the background text color (X11 named color or hex #rrggbb) for the menu (default: #222222). - rofi -fg "#222222" + rofi -bg "#222222" +`-bg` + + Set the background text color for alternating rows (X11 named color or hex #rrggbb) for the menu + (default: #222222). + + rofi -bgalt "#222222" `-bc` diff --git a/include/rofi.h b/include/rofi.h index d1e4f7b4..dd3929f4 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -153,6 +153,8 @@ typedef struct _Settings char * menu_fg; /** Background color */ char * menu_bg; + /** Background color alt */ + char * menu_bg_alt; /** Highlight foreground color */ char * menu_hlfg; /** Highlight background color */ diff --git a/include/textbox.h b/include/textbox.h index e080079b..5f7d1bc5 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -35,6 +35,8 @@ typedef enum { // Render font normally NORMAL, + // Alternating row. + ALT, // Render font highlighted (inverted colors.) HIGHLIGHT, } TextBoxFontType; @@ -132,9 +134,8 @@ void textbox_insert ( textbox *tb, int pos, char *str ); void textbox_hide ( textbox *tb ); /** - * @param font_str The font to use. - * @param font_active_str The font to use for active entries. * @param bg The background color. + * @param bg_alt The background color for alternating row. * @param fg The foreground color. * @param hlbg The background color for a highlighted entry. * @param hlfg The foreground color for a highlighted entry. @@ -144,7 +145,7 @@ void textbox_hide ( textbox *tb ); * Clean with textbox_cleanup() */ void textbox_setup ( - const char *bg, const char *fg, + const char *bg, const char *bg_alt, const char *fg, const char *hlbg, const char *hlfg ); diff --git a/source/rofi.c b/source/rofi.c index 4f95c6f7..cf3b0fdc 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -1281,8 +1281,9 @@ static void menu_draw ( MenuState *state ) textbox_text ( state->boxes[i], "" ); } else{ + TextBoxFontType type = ( ( ( i % state->max_rows ) & 1 ) == 0 ) ? NORMAL : ALT; char *text = state->filtered[i + offset]; - TextBoxFontType tbft = ( i + offset ) == state->selected ? HIGHLIGHT : NORMAL; + TextBoxFontType tbft = ( i + offset ) == state->selected ? HIGHLIGHT : type; textbox_font ( state->boxes[i], tbft ); textbox_text ( state->boxes[i], text ); } @@ -1940,7 +1941,7 @@ static int run_dmenu () { int ret_state; textbox_setup ( - config.menu_bg, config.menu_fg, + config.menu_bg, config.menu_bg_alt, config.menu_fg, config.menu_hlbg, config.menu_hlfg ); char *input = NULL; @@ -1973,7 +1974,7 @@ static void run_switcher ( int do_fork, SwitcherMode mode ) // Because of the above fork, we want to do this here. // Make sure this is isolated to its own thread. textbox_setup ( - config.menu_bg, config.menu_fg, + config.menu_bg, config.menu_bg_alt, config.menu_fg, config.menu_hlbg, config.menu_hlfg ); char *input = NULL; @@ -2190,6 +2191,7 @@ static void parse_cmd_options ( int argc, char ** argv ) find_arg_str ( argc, argv, "-font", &( config.menu_font ) ); find_arg_str ( argc, argv, "-fg", &( config.menu_fg ) ); find_arg_str ( argc, argv, "-bg", &( config.menu_bg ) ); + find_arg_str ( argc, argv, "-bgalt", &( config.menu_bg_alt ) ); find_arg_str ( argc, argv, "-hlfg", &( config.menu_hlfg ) ); find_arg_str ( argc, argv, "-hlbg", &( config.menu_hlbg ) ); find_arg_str ( argc, argv, "-bc", &( config.menu_bc ) ); @@ -2238,11 +2240,6 @@ static void parse_cmd_options ( int argc, char ** argv ) if ( find_arg ( argc, argv, "-sidebar-mode" ) >= 0 ) { config.sidebar_mode = TRUE; } - // Dump. - if ( find_arg ( argc, argv, "-dump-xresources" ) >= 0 ) { - xresource_dump (); - exit ( EXIT_SUCCESS ); - } } /** @@ -2315,6 +2312,10 @@ static void config_sanity_check ( void ) fprintf ( stderr, "config.hmode is invalid.\n" ); exit ( 1 ); } + // If alternative row is not set, copy the normal background color. + if ( config.menu_bg_alt == NULL ) { + config.menu_bg_alt = config.menu_bg; + } } /** @@ -2445,6 +2446,12 @@ int main ( int argc, char *argv[] ) load_configuration ( display ); + // Dump. + if ( find_arg ( argc, argv, "-dump-xresources" ) >= 0 ) { + xresource_dump (); + exit ( EXIT_SUCCESS ); + } + // setup_switchers setup_switchers (); @@ -2484,7 +2491,7 @@ int main ( int argc, char *argv[] ) char *msg = NULL; if ( find_arg_str ( argc, argv, "-e", &( msg ) ) ) { textbox_setup ( - config.menu_bg, config.menu_fg, + config.menu_bg, config.menu_bg_alt, config.menu_fg, config.menu_hlbg, config.menu_hlfg ); error_dialog ( msg ); diff --git a/source/textbox.c b/source/textbox.c index 0972fd66..a323a2c7 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -54,6 +54,7 @@ XftColor color_fg; XftColor color_bg; XftColor color_hlfg; XftColor color_hlbg; +XftColor color_bg_alt; PangoContext *p_context = NULL; @@ -83,7 +84,19 @@ textbox* textbox_create ( Window parent, pango_layout_set_font_description ( tb->layout, pfd ); pango_font_description_free ( pfd ); - unsigned int cp = ( tbft == NORMAL ) ? color_bg.pixel : color_hlbg.pixel; + unsigned int cp; + switch ( tbft ) + { + case HIGHLIGHT: + cp = color_hlbg.pixel; + break; + case ALT: + cp = color_bg_alt.pixel; + break; + default: + cp = color_bg.pixel; + break; + } tb->window = XCreateSimpleWindow ( display, tb->parent, tb->x, tb->y, tb->w, tb->h, 0, None, cp ); @@ -119,6 +132,10 @@ void textbox_font ( textbox *tb, TextBoxFontType tbft ) tb->color_bg = color_hlbg; tb->color_fg = color_hlfg; break; + case ALT: + tb->color_bg = color_bg_alt; + tb->color_fg = color_fg; + break; case NORMAL: default: tb->color_bg = color_bg; @@ -432,7 +449,7 @@ int textbox_keypress ( textbox *tb, XEvent *ev ) */ void textbox_setup ( - const char *bg, const char *fg, + const char *bg, const char *bg_alt, const char *fg, const char *hlbg, const char *hlfg ) { @@ -441,6 +458,7 @@ void textbox_setup ( XftColorAllocName ( display, visual, colormap, fg, &color_fg ); XftColorAllocName ( display, visual, colormap, bg, &color_bg ); + XftColorAllocName ( display, visual, colormap, bg_alt, &color_bg_alt ); XftColorAllocName ( display, visual, colormap, hlfg, &color_hlfg ); XftColorAllocName ( display, visual, colormap, hlbg, &color_hlbg ); @@ -458,6 +476,7 @@ void textbox_cleanup () XftColorFree ( display, visual, colormap, &color_fg ); XftColorFree ( display, visual, colormap, &color_bg ); + XftColorFree ( display, visual, colormap, &color_bg_alt ); XftColorFree ( display, visual, colormap, &color_hlfg ); XftColorFree ( display, visual, colormap, &color_hlbg ); g_object_unref ( p_context ); diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 560899ed..26bc6daf 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -60,57 +60,59 @@ typedef struct * Currently supports string and number. */ static XrmOption xrmOptions[] = { - { xrm_String, "switchers", { .str = &config.switchers }, NULL }, - { xrm_Number, "opacity", { .num = &config.window_opacity }, NULL }, + { xrm_String, "switchers", { .str = &config.switchers }, NULL }, + { xrm_Number, "opacity", { .num = &config.window_opacity }, NULL }, - { xrm_SNumber, "width", { .snum = &config.menu_width }, NULL }, + { xrm_SNumber, "width", { .snum = &config.menu_width }, NULL }, - { xrm_Number, "lines", { .num = &config.menu_lines }, NULL }, - { xrm_Number, "columns", { .num = &config.menu_columns }, NULL }, + { xrm_Number, "lines", { .num = &config.menu_lines }, NULL }, + { xrm_Number, "columns", { .num = &config.menu_columns }, NULL }, - { xrm_String, "font", { .str = &config.menu_font }, NULL }, + { xrm_String, "font", { .str = &config.menu_font }, NULL }, /* Foreground color */ - { xrm_String, "foreground", { .str = &config.menu_fg }, NULL }, - { xrm_String, "fg", { .str = &config.menu_fg }, NULL }, + { xrm_String, "foreground", { .str = &config.menu_fg }, NULL }, + { xrm_String, "fg", { .str = &config.menu_fg }, NULL }, - { xrm_String, "background", { .str = &config.menu_bg }, NULL }, - { xrm_String, "bg", { .str = &config.menu_bg }, NULL }, + { xrm_String, "background", { .str = &config.menu_bg }, NULL }, + { xrm_String, "bg", { .str = &config.menu_bg }, NULL }, + { xrm_String, "background-alternate", { .str = &config.menu_bg_alt }, NULL }, + { xrm_String, "bgalt", { .str = &config.menu_bg_alt }, NULL }, - { xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL }, - { xrm_String, "hlfg", { .str = &config.menu_hlfg }, NULL }, + { xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL }, + { xrm_String, "hlfg", { .str = &config.menu_hlfg }, NULL }, - { xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL }, - { xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL }, + { xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL }, + { xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL }, - { xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL }, - { xrm_String, "bc", { .str = &config.menu_bc }, NULL }, + { xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL }, + { xrm_String, "bc", { .str = &config.menu_bc }, NULL }, - { xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL }, - { xrm_Number, "bw", { .num = &config.menu_bw }, NULL }, + { xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL }, + { xrm_Number, "bw", { .num = &config.menu_bw }, NULL }, - { xrm_Number, "location", { .num = &config.location }, NULL }, + { xrm_Number, "location", { .num = &config.location }, NULL }, - { xrm_Number, "padding", { .num = &config.padding }, NULL }, - { xrm_SNumber, "yoffset", { .snum = &config.y_offset }, NULL }, - { xrm_SNumber, "xoffset", { .snum = &config.x_offset }, NULL }, - { xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL }, - { xrm_Boolean, "hmode", { .num = &config.hmode }, NULL }, + { xrm_Number, "padding", { .num = &config.padding }, NULL }, + { xrm_SNumber, "yoffset", { .snum = &config.y_offset }, NULL }, + { xrm_SNumber, "xoffset", { .snum = &config.x_offset }, NULL }, + { xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL }, + { xrm_Boolean, "hmode", { .num = &config.hmode }, NULL }, - { xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL }, - { xrm_String, "ssh-client", { .str = &config.ssh_client }, NULL }, - { xrm_String, "ssh-command", { .str = &config.ssh_command }, NULL }, - { xrm_String, "run-command", { .str = &config.run_command }, NULL }, - { xrm_String, "run-list-command", { .str = &config.run_list_command }, NULL }, - { xrm_String, "run-shell-command", { .str = &config.run_shell_command }, NULL }, + { xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL }, + { xrm_String, "ssh-client", { .str = &config.ssh_client }, NULL }, + { xrm_String, "ssh-command", { .str = &config.ssh_command }, NULL }, + { xrm_String, "run-command", { .str = &config.run_command }, NULL }, + { xrm_String, "run-list-command", { .str = &config.run_list_command }, NULL }, + { xrm_String, "run-shell-command", { .str = &config.run_shell_command }, NULL }, - { xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL }, - { xrm_Boolean, "levenshtein-sort", { .num = &config.levenshtein_sort }, NULL }, + { xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL }, + { xrm_Boolean, "levenshtein-sort", { .num = &config.levenshtein_sort }, NULL }, /* Key bindings */ - { xrm_String, "key", { .str = &config.window_key }, NULL }, - { xrm_String, "rkey", { .str = &config.run_key }, NULL }, - { xrm_String, "skey", { .str = &config.ssh_key }, NULL }, - { xrm_Boolean, "sidebar-mode", { .num = &config.sidebar_mode }, NULL } + { xrm_String, "key", { .str = &config.window_key }, NULL }, + { xrm_String, "rkey", { .str = &config.run_key }, NULL }, + { xrm_String, "skey", { .str = &config.ssh_key }, NULL }, + { xrm_Boolean, "sidebar-mode", { .num = &config.sidebar_mode }, NULL } }; diff --git a/test/textbox-test.c b/test/textbox-test.c index 09ba9973..fdeac0c3 100644 --- a/test/textbox-test.c +++ b/test/textbox-test.c @@ -49,7 +49,7 @@ int main ( int argc, char **argv ) color_get ( display, config.menu_bg ) ); TASSERT( mw != None ); - textbox_setup ( config.menu_bg, config.menu_fg, + textbox_setup ( config.menu_bg, config.menu_bg_alt, config.menu_fg, config.menu_hlbg, config.menu_hlfg ); textbox *box = textbox_create(mw , TB_EDITABLE|TB_AUTOWIDTH|TB_AUTOHEIGHT, 0,0, -1, -1, NORMAL, "test"); TASSERT( box != NULL );