dmenu-center-5.2.diff (3133B)
1 diff --git a/config.def.h b/config.def.h 2 index 1edb647..88ef264 100644 3 --- a/config.def.h 4 +++ b/config.def.h 5 @@ -2,6 +2,8 @@ 6 /* Default settings; can be overriden by command line. */ 7 8 static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ 9 +static int centered = 0; /* -c option; centers dmenu on screen */ 10 +static int min_width = 500; /* minimum width when centered */ 11 /* -fn option overrides fonts[0]; default X11 font or font set */ 12 static const char *fonts[] = { 13 "monospace:size=10" 14 diff --git a/dmenu.1 b/dmenu.1 15 index 323f93c..c036baa 100644 16 --- a/dmenu.1 17 +++ b/dmenu.1 18 @@ -40,6 +40,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL. 19 .B \-b 20 dmenu appears at the bottom of the screen. 21 .TP 22 +.B \-c 23 +dmenu appears centered on the screen. 24 +.TP 25 .B \-f 26 dmenu grabs the keyboard before reading stdin if not reading from a tty. This 27 is faster, but will lock up X until stdin reaches end\-of\-file. 28 diff --git a/dmenu.c b/dmenu.c 29 index 27b7a30..427fb04 100644 30 --- a/dmenu.c 31 +++ b/dmenu.c 32 @@ -96,6 +96,15 @@ calcoffsets(void) 33 break; 34 } 35 36 +static int 37 +max_textw(void) 38 +{ 39 + int len = 0; 40 + for (struct item *item = items; item && item->text; item++) 41 + len = MAX(TEXTW(item->text), len); 42 + return len; 43 +} 44 + 45 static void 46 cleanup(void) 47 { 48 @@ -636,6 +645,7 @@ setup(void) 49 bh = drw->fonts->h + 2; 50 lines = MAX(lines, 0); 51 mh = (lines + 1) * bh; 52 + promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 53 #ifdef XINERAMA 54 i = 0; 55 if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { 56 @@ -662,9 +672,16 @@ setup(void) 57 if (INTERSECT(x, y, 1, 1, info[i]) != 0) 58 break; 59 60 - x = info[i].x_org; 61 - y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 62 - mw = info[i].width; 63 + if (centered) { 64 + mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width); 65 + x = info[i].x_org + ((info[i].width - mw) / 2); 66 + y = info[i].y_org + ((info[i].height - mh) / 2); 67 + } else { 68 + x = info[i].x_org; 69 + y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 70 + mw = info[i].width; 71 + } 72 + 73 XFree(info); 74 } else 75 #endif 76 @@ -672,9 +689,16 @@ setup(void) 77 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 78 die("could not get embedding window attributes: 0x%lx", 79 parentwin); 80 - x = 0; 81 - y = topbar ? 0 : wa.height - mh; 82 - mw = wa.width; 83 + 84 + if (centered) { 85 + mw = MIN(MAX(max_textw() + promptw, min_width), wa.width); 86 + x = (wa.width - mw) / 2; 87 + y = (wa.height - mh) / 2; 88 + } else { 89 + x = 0; 90 + y = topbar ? 0 : wa.height - mh; 91 + mw = wa.width; 92 + } 93 } 94 promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 95 inputw = mw / 3; /* input width: ~33% of monitor width */ 96 @@ -733,6 +757,8 @@ main(int argc, char *argv[]) 97 topbar = 0; 98 else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ 99 fast = 1; 100 + else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */ 101 + centered = 1; 102 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 103 fstrncmp = strncasecmp; 104 fstrstr = cistrstr;