From 273fb103c9af68297bfeb46aac58a97c5192951c Mon Sep 17 00:00:00 2001 From: speedie Date: Sun, 16 Apr 2023 23:57:02 +0200 Subject: [PATCH] add the ability to have vertical/horizontal outer padding (margin) --- docs/docs.md | 6 ++++++ docs/example.Xresources | 2 ++ docs/spmenu.conf | 6 ++++-- libs/argv.c | 6 ++++++ libs/client.c | 2 +- libs/conf/config.c | 6 ++++++ libs/draw.c | 24 ++++++++++++++++-------- libs/img.c | 18 +++++++++++------- libs/mouse.c | 5 ++++- libs/xresources.h | 2 ++ options.h | 6 ++++-- spmenu.1 | 6 ++++++ spmenu.c | 2 +- 13 files changed, 69 insertions(+), 22 deletions(-) diff --git a/docs/docs.md b/docs/docs.md index 45bff3a..abe1807 100644 --- a/docs/docs.md +++ b/docs/docs.md @@ -245,6 +245,12 @@ You may use long, descriptive arguments or the shorter arguments. `-txp, --text-padding padding` : Set text padding to padding +`-vem, --vertical-margin margin` +: Set the vertical margin + +`-hem, --horizontal-margin margin` +: Set the horizontal margin + `-lp, --vertical-padding padding` : Set the vertical padding diff --git a/docs/example.Xresources b/docs/example.Xresources index c6bc635..1f6cce5 100644 --- a/docs/example.Xresources +++ b/docs/example.Xresources @@ -87,6 +87,8 @@ spmenu.menuposition: 1 spmenu.menupaddingv: 0 spmenu.menupaddingh: 0 spmenu.menuwidth: 0 +spmenu.menumarginv: 0 +spmenu.menumarginh: 0 spmenu.xpos: 0 spmenu.ypos: 0 spmenu.minwidth: 1000 diff --git a/docs/spmenu.conf b/docs/spmenu.conf index e5cf387..29a2a4d 100644 --- a/docs/spmenu.conf +++ b/docs/spmenu.conf @@ -10,8 +10,10 @@ spmenu = // General window options window = ( { position = 1; border = 0; - paddingv = 0; - paddingh = 0; + margin-vertical = 0; + margin-horizontal = 0; + padding-vertical = 0; + padding-horizontal = 0; x = 0; y = 0; width = 0; diff --git a/libs/argv.c b/libs/argv.c index 2066c46..cd9d573 100644 --- a/libs/argv.c +++ b/libs/argv.c @@ -214,6 +214,10 @@ readargs(int argc, char *argv[]) menupaddingv = atoi(argv[++i]); } else if (!strcmp(argv[i], "-hp") || (!strcmp(argv[i], "--horizontal-padding"))) { // horizontal padding menupaddingh = atoi(argv[++i]); + } else if (!strcmp(argv[i], "-vem") || (!strcmp(argv[i], "--vertical-margin"))) { // vertical margin + menumarginv = atoi(argv[++i]); + } else if (!strcmp(argv[i], "-hom") || (!strcmp(argv[i], "--horizontal-margin"))) { // horizontal margin + menumarginh = atoi(argv[++i]); } else if (!strcmp(argv[i], "-pri") || (!strcmp(argv[i], "--priority"))) { // high priority (csv format) hpitems = tokenize(argv[++i], ",", &hplength); } else if (!strcmp(argv[i], "-ig") || (!strcmp(argv[i], "--image-gaps"))) { // gaps between image @@ -477,6 +481,8 @@ usage(void) "spmenu -txp, --text-padding Set text padding to \n" "spmenu -lp, --vertical-padding Set the vertical padding\n" "spmenu -hp, --horizontal-padding Set the horizontal padding\n" + "spmenu -vem, --vertical-margin Set the vertical margin\n" + "spmenu -hem, --horizontal-margin Set the horizontal margin\n" "spmenu -la, --left-arrow-symbol Set the left arrow to \n" "spmenu -ra, --right-arrow-symbol Set the right arrow to \n" "spmenu -is, --image-size Image size\n" diff --git a/libs/client.c b/libs/client.c index 0f13cc4..da8562a 100644 --- a/libs/client.c +++ b/libs/client.c @@ -70,7 +70,7 @@ resizeclient(void) if (image) resizetoimageheight(imageheight); #endif - mh = (lines + 1) * bh; + mh = (lines + 1) * bh + 2 * menumarginv; // why have an empty line? when there's nothing to draw there anyway? if (hideprompt && hideinput && hidemode && hidematchcount) diff --git a/libs/conf/config.c b/libs/conf/config.c index 4af53fe..f0652d6 100644 --- a/libs/conf/config.c +++ b/libs/conf/config.c @@ -70,8 +70,14 @@ conf_init(void) // look up config_setting_lookup_int(conf, "position", &menuposition); // spmenu.window.menuposition + config_setting_lookup_int(conf, "paddingv", &menupaddingv); // spmenu.window.paddingv config_setting_lookup_int(conf, "paddingh", &menupaddingh); // spmenu.window.paddingh + config_setting_lookup_int(conf, "padding-vertical", &menupaddingv); // spmenu.window.padding-vertical + config_setting_lookup_int(conf, "padding-horizontal", &menupaddingh); // spmenu.window.padding-horizontal + config_setting_lookup_int(conf, "margin-vertical", &menumarginv); // spmenu.window.margin-vertical + config_setting_lookup_int(conf, "margin-horizontal", &menumarginh); // spmenu.window.margin-horizontal + config_setting_lookup_int(conf, "x", &xpos); // spmenu.window.x config_setting_lookup_int(conf, "y", &xpos); // spmenu.window.y config_setting_lookup_int(conf, "width", &menuwidth); // spmenu.window.width diff --git a/libs/draw.c b/libs/draw.c index 8d1eed8..874c366 100644 --- a/libs/draw.c +++ b/libs/draw.c @@ -204,12 +204,13 @@ drawitem(int x, int y, int w) if (lines > 0) { int i = 0; int rx = 0; + int ew = 0; // draw image first #if USEIMAGE if (!hideimage && longestedge != 0) { rx = ox; - rx += (imagegaps * 2) + imagewidth; + rx += (imagegaps * 2) + imagewidth + menumarginh; } else #endif if (!indentitems) { @@ -218,12 +219,16 @@ drawitem(int x, int y, int w) rx = x; } + if (!hidepowerline && (powerlinemode || powerlinecount)) { + ew = plw / 2; + } + for (item = curr; item != next; item = item->right, i++) { x = drawitemtext( item, rx + ((i / lines) * ((mw - rx) / columns)), y + (((i % lines) + 1) * bh), - (mw - rx) / columns + (mw - rx) / columns - (menumarginh + ew / 2) ); } @@ -235,9 +240,9 @@ drawitem(int x, int y, int w) x = drawlarrow(x, y, w); for (item = curr; item != next; item = item->right) // draw items - x = drawitemtext(item, x, y, MIN(pango_item ? TEXTWM(item->text) : TEXTW(item->text), mw - x - rarrowWidth - numberWidth - modeWidth)); + x = drawitemtext(item, x, y, MIN(pango_item ? TEXTWM(item->text) : TEXTW(item->text), mw - x - rarrowWidth - numberWidth - modeWidth - menumarginh)); - w = rarrowWidth + numberWidth + modeWidth; + w = rarrowWidth + numberWidth + modeWidth + menumarginh; x = drawrarrow(mw - w, y, w); } @@ -294,7 +299,7 @@ drawinput(int x, int y, int w) if ((curpos += lrpad / 2 - 1) < w && !hidecaret && !hideprompt) { drw_setscheme(drw, scheme[SchemeCaret]); - drw_rect(drw, x + curpos, 2 + (bh - fh) / 2, 2, fh - 4, 1, 0); + drw_rect(drw, x + curpos, 2 + (bh - fh) / 2 + y, 2, fh - 4, 1, 0); } return x; @@ -400,6 +405,9 @@ drawmenu(void) numberWidth = TEXTW(numbers); } + x += menumarginh; + y += menumarginv; + // why have an empty line? if ((hideprompt && hideinput && hidemode && hidematchcount #if USEIMAGE @@ -408,7 +416,7 @@ drawmenu(void) )) { #endif y -= bh; - mh = (lines + 1) * bh - bh; + mh = (lines + 1) * bh - bh + 2 * menumarginv; if (!win) return; @@ -438,12 +446,12 @@ drawmenu(void) if (!hidematchcount) { w = numberWidth; - drawnumber(mw - numberWidth - modeWidth - 2 * sp - 2 * borderwidth, y, w); + drawnumber(mw - numberWidth - modeWidth - 2 * sp - 2 * borderwidth - menumarginh, y, w); } if (!hidemode) { w = modeWidth; - drawmode(mw - modeWidth - 2 * sp - 2 * borderwidth, y, w); + drawmode(mw - modeWidth - 2 * sp - 2 * borderwidth - menumarginh, y, w); } drw_map(drw, win, 0, 0, mw, mh); diff --git a/libs/img.c b/libs/img.c index 770ad2c..7de0b5c 100644 --- a/libs/img.c +++ b/libs/img.c @@ -94,13 +94,17 @@ drawimage(void) int leftmargin = imagegaps; int wtr = 0; int wta = 0; + int xta = 0; if (hideprompt && hideinput && hidemode && hidematchcount) { wtr = bh; } else { - wta = bh; + wta = bh + menumarginv; } + // margin + xta += menumarginh; + if (mh != bh + height + imagegaps * 2 - wtr) { // menu height cannot be smaller than image height resizetoimageheight(height); } @@ -109,16 +113,16 @@ drawimage(void) if (!imageposition) { // top mode = 0 if (height > width) width = height; - imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, wta+imagegaps); + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, wta+imagegaps); } else if (imageposition == 1) { // bottom mode = 1 if (height > width) width = height; - imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, mh-height-imagegaps); + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, mh-height-imagegaps); } else if (imageposition == 2) { // center mode = 2 - imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, (mh-wta-height)/2+wta); + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, (mh-wta-height)/2+wta); } else { int minh = MIN(height, mh-bh-imagegaps*2); - imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2, (minh-height)/2+wta+imagegaps); + imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, (minh-height)/2+wta+imagegaps); } } @@ -349,10 +353,10 @@ resizetoimageheight(int imageheight) wtr = bh; } - mh = MAX((lines + 1) * bh, ((lines + 1) * bh) - wtr); + mh = MAX((lines + 1) * bh + 2 * menumarginv, ((lines + 1) * bh) - wtr + 2 * menumarginv); if (mh - bh < imageheight + imagegaps * 2) { - mh = (imageheight + imagegaps * 2 + bh) - wtr; + mh = (imageheight + imagegaps * 2 + bh) - wtr + 2 * menumarginv; } if (!win || omh == mh) { diff --git a/libs/mouse.c b/libs/mouse.c index acb9445..3e6a289 100644 --- a/libs/mouse.c +++ b/libs/mouse.c @@ -39,9 +39,10 @@ motionevent(XButtonEvent *ev) if (!hiderarrow) rarrowWidth = pango_rightarrow ? TEXTWM(rightarrow) : TEXTW(rightarrow); xy = lines > 0 ? bh : inputw + promptw + larrowWidth; + xy += menumarginv; ev_xy = lines > 0 ? ev->y : ev->x; for (item = curr; item && item != next; item = item->right) { - int wh = lines > 0 ? bh : textw_clamp(item->text, mw - xy - rarrowWidth); + int wh = lines > 0 ? bh : textw_clamp(item->text, mw - xy - rarrowWidth - menumarginh); if (ev_xy >= xy && ev_xy < (xy + wh)) { sel = item; calcoffsets(); @@ -65,6 +66,8 @@ buttonpress(XEvent *e) x = xpad = plw; } + x += menumarginh; + int larrowWidth = 0; int rarrowWidth = 0; int numberWidth = 0; diff --git a/libs/xresources.h b/libs/xresources.h index 32bff16..b184751 100644 --- a/libs/xresources.h +++ b/libs/xresources.h @@ -63,6 +63,8 @@ ResourcePref resources[] = { { "menuwidth", INTEGER, &menuwidth }, { "menupaddingv", INTEGER, &menupaddingv }, { "menupaddingh", INTEGER, &menupaddingh }, + { "menumarginv", INTEGER, &menumarginv }, + { "menumarginh", INTEGER, &menumarginh }, { "textpadding", INTEGER, &textpadding }, { "indentitems", INTEGER, &indentitems }, { "accuratewidth", INTEGER, &accuratewidth }, diff --git a/options.h b/options.h index 92b80d3..71403c8 100644 --- a/options.h +++ b/options.h @@ -15,9 +15,11 @@ static int mon = -1; /* Monitor to run spmenu on */ /* Window options */ static int alpha = 1; /* Enable alpha */ static int menuposition = 1; /* Position of the menu (0: Bottom, 1: Top, 2: Center */ -static int menupaddingv = 0; /* Vertical padding of bar (in pixels) */ -static int menupaddingh = 0; /* Horizontal padding of bar (in pixels) */ +static int menupaddingv = 0; /* Vertical padding inside the menu (in pixels) */ +static int menupaddingh = 0; /* Horizontal padding inside the menu (in pixels) */ static int menuwidth = 0; /* spmenu width when setting X/Y position */ +static int menumarginv = 0; /* Vertical padding around the menu */ +static int menumarginh = 0; /* Horizontal padding around the menu */ static int minwidth = 1000; /* Minimum width when centered */ static int xpos = 0; /* X position to offset spmenu */ static int ypos = 0; /* Y position to offset spmenu */ diff --git a/spmenu.1 b/spmenu.1 index 5704226..de5c278 100644 --- a/spmenu.1 +++ b/spmenu.1 @@ -263,6 +263,12 @@ Set image gaps to gaps \f[V]-txp, --text-padding padding\f[R] Set text padding to padding .TP +\f[V]-vem, --vertical-margin margin\f[R] +Set the vertical margin +.TP +\f[V]-hem, --horizontal-margin margin\f[R] +Set the horizontal margin +.TP \f[V]-lp, --vertical-padding padding\f[R] Set the vertical padding .TP diff --git a/spmenu.c b/spmenu.c index 0170f95..c3a8a71 100644 --- a/spmenu.c +++ b/spmenu.c @@ -533,7 +533,7 @@ setupdisplay(void) if (image) resizetoimageheight(imageheight); #endif - mh = (lines + 1) * bh; // lines + 1 * bh is the menu height + mh = (lines + 1) * bh + 2 * menumarginv; // lines + 1 * bh is the menu height // set prompt width based on prompt size promptw = (prompt && *prompt)