From 7b0e0643c856a4a366a434ae15ee706166d19078 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 31 Dec 2016 23:27:17 +0100 Subject: [PATCH] Make spacing be of type distance. --- include/theme.h | 5 ++++- include/widgets/textbox.h | 1 + lexer/theme-lexer.l | 13 +++++++------ source/theme.c | 21 ++++++++++++++++++++- source/widgets/box.c | 2 +- source/widgets/listview.c | 2 +- source/widgets/widget.c | 20 ++++---------------- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/theme.h b/include/theme.h index 89b4ac2f..043cf6f8 100644 --- a/include/theme.h +++ b/include/theme.h @@ -9,7 +9,7 @@ typedef enum { } PixelWidth; typedef struct { - int distance; + double distance; PixelWidth type; } Distance; @@ -86,10 +86,13 @@ void rofi_theme_widget_add_properties ( Widget *widget, GHashTable *table ); * Public API */ +Distance rofi_theme_get_distance ( const char *wclass, const char *name, const char *state, const char *property, int def ); int rofi_theme_get_integer ( const char *wclass, const char *name, const char *state, const char *property, int def ); int rofi_theme_get_boolean ( const char *wclass, const char *name, const char *state, const char *property, int def ); char *rofi_theme_get_string ( const char *wclass, const char *name, const char *state, const char *property, char *def ); double rofi_theme_get_double ( const char *wclass, const char *name, const char *state, const char *property, double def ); void rofi_theme_get_color ( const char *wclass, const char *name, const char *state, const char *property, cairo_t *d); Padding rofi_theme_get_padding ( const char *wclass, const char *name, const char *state, const char *property, Padding pad ); + +int distance_get_pixel ( Distance d ); #endif diff --git a/include/widgets/textbox.h b/include/widgets/textbox.h index 15d2ef5d..14484a25 100644 --- a/include/widgets/textbox.h +++ b/include/widgets/textbox.h @@ -255,5 +255,6 @@ PangoAttrList *textbox_get_pango_attributes ( textbox *tb ); * @returns the visible text. */ const char *textbox_get_visible_text ( const textbox *tb ); +int distance_get_pixel ( Distance d ); /*@}*/ #endif //ROFI_TEXTBOX_H diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l index 1288e07b..08aab7aa 100644 --- a/lexer/theme-lexer.l +++ b/lexer/theme-lexer.l @@ -24,6 +24,7 @@ WORD [[:alnum:]-]+ STRING [[:print:]]+ HEX [[:xdigit:]] NUMBER [[:digit:]] +REAL [[:digit:]]+(\.[[:digit:]]+)? PX (px) EM (em) NEWLINES (\r|\n)+ @@ -106,14 +107,14 @@ if ( queue == NULL ){ {NUMBER}+\.{NUMBER}+ { yylval->fval = g_ascii_strtod(yytext, NULL); return T_DOUBLE;} \"{STRING}\" { yytext[yyleng-1] = '\0'; yylval->sval = g_strdup(&yytext[1]); return T_STRING;} -{NUMBER}+{PX} { - yylval->distance.distance = (int)g_ascii_strtoll(yytext, NULL, 10); - yylval->distance.type = PW_PX; +{REAL}{EM} { + yylval->distance.distance = (double)g_ascii_strtod(yytext, NULL); + yylval->distance.type = PW_EM; return T_PIXEL; } -{NUMBER}+{EM} { - yylval->distance.distance = (int)g_ascii_strtoll(yytext, NULL, 10); - yylval->distance.type = PW_EM; +{NUMBER}+{PX} { + yylval->distance.distance = (double)g_ascii_strtoll(yytext, NULL, 10); + yylval->distance.type = PW_PX; return T_PIXEL; } #{HEX}{8} { diff --git a/source/theme.c b/source/theme.c index f4dd9ffc..ce6e997e 100644 --- a/source/theme.c +++ b/source/theme.c @@ -5,6 +5,8 @@ #include "theme.h" #include "lexer/theme-parser.h" #include "helper.h" +#include "widgets/textbox.h" + void yyerror ( YYLTYPE *ylloc, const char *); Widget *rofi_theme_find_or_create_class ( Widget *base, const char *class ) @@ -88,7 +90,7 @@ static void rofi_theme_print_property_index ( int depth, Property *p ) (unsigned char)(p->value.color.blue*255.0)); break; case P_PADDING: - printf("%d%s %d%s %d%s %d%s", + printf("%f%s %f%s %f%s %f%s", p->value.padding.left.distance, p->value.padding.left.type == PW_PX? "px":"em", p->value.padding.right.distance, @@ -250,6 +252,15 @@ int rofi_theme_get_integer ( const char *wclass, const char *name, const char * } return def; } +Distance rofi_theme_get_distance ( const char *wclass, const char *name, const char *state, const char *property, int def ) +{ + Widget *widget = rofi_theme_find_widget ( wclass, name, state ); + Property *p = rofi_theme_find_property ( widget, P_PADDING, property ); + if ( p ){ + return p->value.padding.left; + } + return (Distance){def, PW_PX}; +} int rofi_theme_get_boolean ( const char *wclass, const char *name, const char *state, const char *property, int def ) { @@ -302,3 +313,11 @@ Padding rofi_theme_get_padding ( const char *wclass, const char *name, const ch } return pad; } + +int distance_get_pixel ( Distance d ) +{ + if ( d.type == PW_EM ){ + return d.distance*textbox_get_estimated_char_height(); + } + return d.distance; +} diff --git a/source/widgets/box.c b/source/widgets/box.c index a6d5187a..255f9b43 100644 --- a/source/widgets/box.c +++ b/source/widgets/box.c @@ -351,7 +351,7 @@ box * box_create ( const char *name, boxType type ) b->widget.get_desired_height = box_get_desired_height; b->widget.enabled = TRUE; - box_set_spacing ( b, rofi_theme_get_integer ( b->widget.class_name, b->widget.name, NULL, "spacing",config.line_margin )); + box_set_spacing ( b, distance_get_pixel (rofi_theme_get_distance ( b->widget.class_name, b->widget.name, NULL, "spacing",config.line_margin ))); return b; } diff --git a/source/widgets/listview.c b/source/widgets/listview.c index 210f6f0e..19f3f305 100644 --- a/source/widgets/listview.c +++ b/source/widgets/listview.c @@ -349,7 +349,7 @@ listview *listview_create ( const char *name, listview_update_callback cb, void lv->udata = udata; // Some settings. - lv->spacing = rofi_theme_get_integer (lv->widget.class_name, lv->widget.name, NULL, "spacing", config.line_margin ); + lv->spacing = distance_get_pixel (rofi_theme_get_distance (lv->widget.class_name, lv->widget.name, NULL, "spacing", config.line_margin )); lv->menu_lines = rofi_theme_get_integer (lv->widget.class_name, lv->widget.name, NULL, "lines", config.menu_lines ); lv->menu_columns = rofi_theme_get_integer (lv->widget.class_name, lv->widget.name, NULL, "columns", config.menu_columns); lv->fixed_num_lines = rofi_theme_get_boolean (lv->widget.class_name, lv->widget.name, NULL, "fixed-height", config.fixed_num_lines ); diff --git a/source/widgets/widget.c b/source/widgets/widget.c index 77c4cf2e..279c5f24 100644 --- a/source/widgets/widget.c +++ b/source/widgets/widget.c @@ -215,31 +215,19 @@ void widget_set_name ( widget *wid, const char *name ) double textbox_get_estimated_char_height ( void ); int widget_padding_get_left ( const widget *wid ) { - if ( wid->pad.left.type == PW_EM ){ - return wid->pad.left.distance*textbox_get_estimated_char_height(); - } - return wid->pad.left.distance; + return distance_get_pixel ( wid->pad.left ); } int widget_padding_get_right ( const widget *wid ) { - if ( wid->pad.right.type == PW_EM ){ - return wid->pad.right.distance*textbox_get_estimated_char_height(); - } - return wid->pad.right.distance; + return distance_get_pixel ( wid->pad.right ); } int widget_padding_get_top ( const widget *wid ) { - if ( wid->pad.top.type == PW_EM ){ - return wid->pad.top.distance*textbox_get_estimated_char_height(); - } - return wid->pad.top.distance; + return distance_get_pixel ( wid->pad.top ); } int widget_padding_get_bottom ( const widget *wid ) { - if ( wid->pad.bottom.type == PW_EM ){ - return wid->pad.bottom.distance*textbox_get_estimated_char_height(); - } - return wid->pad.bottom.distance; + return distance_get_pixel ( wid->pad.bottom ); } int widget_padding_get_remaining_width ( const widget *wid )