From 068592414e6993274c37e894acaf4d371600ddaf Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 31 Dec 2016 22:47:22 +0100 Subject: [PATCH] Support em format in padding --- include/theme.h | 20 +++++++++++++++----- lexer/theme-lexer.l | 9 ++++++++- lexer/theme-parser.y | 7 ++++--- source/theme.c | 11 +++++++++++ source/view.c | 10 ---------- source/widgets/widget.c | 29 ++++++++++++++++------------- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/include/theme.h b/include/theme.h index 3cf34387..89b4ac2f 100644 --- a/include/theme.h +++ b/include/theme.h @@ -2,6 +2,17 @@ #define THEME_H #include #include +typedef enum { + PW_PX, + PW_EM, + +} PixelWidth; + +typedef struct { + int distance; + PixelWidth type; +} Distance; + typedef enum { P_INTEGER, P_DOUBLE, @@ -26,11 +37,10 @@ typedef struct typedef struct { - int left; - int right; - int top; - int bottom; - gboolean percentual; + Distance left; + Distance right; + Distance top; + Distance bottom; } Padding; typedef struct { diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l index 0cf11f66..1288e07b 100644 --- a/lexer/theme-lexer.l +++ b/lexer/theme-lexer.l @@ -25,6 +25,7 @@ STRING [[:print:]]+ HEX [[:xdigit:]] NUMBER [[:digit:]] PX (px) +EM (em) NEWLINES (\r|\n)+ %x PROPERTIES @@ -106,7 +107,13 @@ if ( queue == NULL ){ \"{STRING}\" { yytext[yyleng-1] = '\0'; yylval->sval = g_strdup(&yytext[1]); return T_STRING;} {NUMBER}+{PX} { - yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); + yylval->distance.distance = (int)g_ascii_strtoll(yytext, NULL, 10); + yylval->distance.type = PW_PX; + return T_PIXEL; +} +{NUMBER}+{EM} { + yylval->distance.distance = (int)g_ascii_strtoll(yytext, NULL, 10); + yylval->distance.type = PW_EM; return T_PIXEL; } #{HEX}{8} { diff --git a/lexer/theme-parser.y b/lexer/theme-parser.y index 1384f7fe..6b0d1aa0 100644 --- a/lexer/theme-parser.y +++ b/lexer/theme-parser.y @@ -31,6 +31,7 @@ int yylex (YYSTYPE *, YYLTYPE *); GList *name_path; Property *property; GHashTable *property_list; + Distance distance; } %token T_INT @@ -40,7 +41,7 @@ int yylex (YYSTYPE *, YYLTYPE *); %token NAME_ELEMENT %token T_BOOLEAN %token T_COLOR -%token T_PIXEL +%token T_PIXEL %token CLASS_NAME %token FIRST_NAME @@ -170,12 +171,12 @@ property | pvalue PSEP T_PIXEL PCLOSE { $$ = rofi_theme_property_create ( P_PADDING ); $$->name = $1; - $$->value.padding = (Padding){ $3, $3, $3, $3, FALSE }; + $$->value.padding = (Padding){ $3, $3, $3, $3 }; } | pvalue PSEP T_PIXEL T_PIXEL T_PIXEL T_PIXEL PCLOSE { $$ = rofi_theme_property_create ( P_PADDING ); $$->name = $1; - $$->value.padding = (Padding){ $3, $4, $5, $6, FALSE }; + $$->value.padding = (Padding){ $3, $4, $5, $6 }; } ; diff --git a/source/theme.c b/source/theme.c index 93ff78ee..f4dd9ffc 100644 --- a/source/theme.c +++ b/source/theme.c @@ -87,6 +87,17 @@ static void rofi_theme_print_property_index ( int depth, Property *p ) (unsigned char)(p->value.color.green*255.0), (unsigned char)(p->value.color.blue*255.0)); break; + case P_PADDING: + printf("%d%s %d%s %d%s %d%s", + p->value.padding.left.distance, + p->value.padding.left.type == PW_PX? "px":"em", + p->value.padding.right.distance, + p->value.padding.right.type == PW_PX? "px":"em", + p->value.padding.top.distance, + p->value.padding.top.type == PW_PX? "px":"em", + p->value.padding.bottom.distance, + p->value.padding.bottom.type == PW_PX? "px":"em" + ); } putchar ( '\n' ); } diff --git a/source/view.c b/source/view.c index a120ce2c..ac0ff9b8 100644 --- a/source/view.c +++ b/source/view.c @@ -863,8 +863,6 @@ static void rofi_view_mouse_navigation ( RofiViewState *state, xcb_button_press_ } else { xcb_button_press_event_t rel = *xbe; - rel.event_x -= state->pad.left; - rel.event_y -= state->pad.top; if ( widget_clicked ( WIDGET ( state->main_window ), &rel ) ) { return; } @@ -1279,8 +1277,6 @@ void rofi_view_itterrate ( RofiViewState *state, xcb_generic_event_t *ev, xkb_st state->mouse_seen = TRUE; } xcb_motion_notify_event_t xme = *( (xcb_motion_notify_event_t *) ev ); - xme.event_x -= state->pad.left; - xme.event_y -= state->pad.top; if ( widget_motion_notify ( WIDGET ( state->main_window ), &xme ) ) { return; } @@ -1417,9 +1413,6 @@ RofiViewState *rofi_view_create ( Mode *sw, state->finalize = finalize; state->mouse_seen = FALSE; - state->pad = (Padding){config.padding,config.padding, config.padding, config.padding, FALSE}; - state->pad = rofi_theme_get_padding ( "@window", "window", NULL, "padding", state->pad); - // Request the lines to show. state->num_lines = mode_get_num_entries ( sw ); @@ -1539,15 +1532,12 @@ int rofi_view_error_dialog ( const char *msg, int markup ) state->retv = MENU_CANCEL; state->menu_flags = MENU_ERROR_DIALOG; state->finalize = process_result; - state->pad = (Padding){config.padding,config.padding, config.padding, config.padding, FALSE}; - state->pad = rofi_theme_get_padding ( "@window", "window", NULL, "padding", state->pad); state->border += rofi_theme_get_integer ( "@window", "window", NULL, "border-width" , config.menu_bw); rofi_view_calculate_window_and_element_width ( state ); state->main_window = window_create ( "window" ); state->main_box = box_create ( "mainbox.box", BOX_VERTICAL); window_add ( state->main_window, WIDGET ( state->main_box ) ); - widget_move ( WIDGET ( state->main_box ), state->border+state->pad.left, state->border+state->pad.top ); state->text = textbox_create ( "message", ( TB_AUTOHEIGHT | TB_WRAP ) + ( ( markup ) ? TB_MARKUP : 0 ), NORMAL, ( msg != NULL ) ? msg : "" ); box_add ( state->main_box, WIDGET ( state->text ), TRUE, FALSE ); diff --git a/source/widgets/widget.c b/source/widgets/widget.c index f8adafc0..81ed45f1 100644 --- a/source/widgets/widget.c +++ b/source/widgets/widget.c @@ -8,7 +8,8 @@ void widget_init ( widget *widget , const char *name, const char *class_name ) { widget->name = g_strdup(name); widget->class_name = g_strdup(class_name); - widget->pad = rofi_theme_get_padding (widget->class_name, widget->name, NULL, "padding", (Padding){0,0,0,0,FALSE}); + widget->pad = (Padding){ {0, PW_PX}, {0, PW_PX}, {0, PW_PX}, {0, PW_PX}}; + widget->pad = rofi_theme_get_padding (widget->class_name, widget->name, NULL, "padding", widget->pad); } @@ -210,33 +211,35 @@ void widget_set_name ( widget *wid, const char *name ) wid->name = g_strdup ( name ); } +// External +double textbox_get_estimated_char_width ( void ); int widget_padding_get_left ( const widget *wid ) { - if ( wid->pad.left < 0 ){ - return (wid->w*-wid->pad.left)/100; + if ( wid->pad.left.type == PW_EM ){ + return wid->pad.left.distance*textbox_get_estimated_char_width(); } - return wid->pad.left; + return wid->pad.left.distance; } int widget_padding_get_right ( const widget *wid ) { - if ( wid->pad.right < 0 ){ - return (wid->w*-wid->pad.right)/100; + if ( wid->pad.right.type == PW_EM ){ + return wid->pad.right.distance*textbox_get_estimated_char_width(); } - return wid->pad.right; + return wid->pad.right.distance; } int widget_padding_get_top ( const widget *wid ) { - if ( wid->pad.top < 0 ){ - return (wid->h*-wid->pad.top)/100; + if ( wid->pad.top.type == PW_EM ){ + return wid->pad.top.distance*textbox_get_estimated_char_width(); } - return wid->pad.top; + return wid->pad.top.distance; } int widget_padding_get_bottom ( const widget *wid ) { - if ( wid->pad.bottom < 0 ){ - return (wid->h*-wid->pad.bottom)/100; + if ( wid->pad.bottom.type == PW_EM ){ + return wid->pad.bottom.distance*textbox_get_estimated_char_width(); } - return wid->pad.bottom; + return wid->pad.bottom.distance; } int widget_padding_get_remaining_width ( const widget *wid )