commit 36a15a9f3da905e359588b809b80a25122991586 from: Omar Polo date: Sat May 19 20:52:11 2018 UTC new layout: vertical commit - bda0e6f801f4111d451e606e344b7269823477fa commit + 36a15a9f3da905e359588b809b80a25122991586 blob - f5176fbd890e8c45b726bdc99b5c71b968cbe06e blob + 720726fdd4c9a690f9637dd86ddbe4c7188f8aad --- README.md +++ README.md @@ -4,6 +4,8 @@ ![MyMenu works!](screen.png) +![MyMenu alternate layout](screen-alt.png) + --- ## What? @@ -47,10 +49,6 @@ delete `-DUSE_XINERAMA` from the `CFLAGS` and `xineram - Optional TrueType support - Opacity support - - - Alternate layout - - An alternate layout (similar to rofi) would be nice too ## Scripts blob - 74deb60ec919eecce294db09315f9235a8ccf1a6 blob + 7f09237cd9c8f5fdde0beafb622649617859876e --- Xexample +++ Xexample @@ -1,4 +1,5 @@ MyMenu.font: -misc-tamsyn-medium-r-normal--15-108-100-100-c-80-iso8859-1 +MyMenu.layout: horizontal MyMenu.width: 100% MyMenu.height: 30 blob - 41924fc32ab81f146abbdcf58194de3713f8a476 blob + 8572eb423a8ba658eaf5e7b7b4260727c10626ae --- mymenu.1 +++ mymenu.1 @@ -22,6 +22,12 @@ Application specific resources: .It MyMenu.font The font name, only bitmap font are supported. By default is set to "fixed" +.It MyMenu.layout +The layout of the menu. The possible values are "horizontal" and +"vertical", with the default being "horizontal". Every other value +than "horizontal" is treated like "vertical", but this is kinda an +implementation detail and not something to be relied on, since in the +future other layout could be added as well .It MyMenu.width The width of the menu. If a numeric value is given (e.g. 400) is interpreted as pixel, if it ends with a percentage symbol `%' blob - cdd416370501371d4b269f9be538a9454b2535fa blob + a4b83d01291ed0299ed8e3fcdfe2ae2ba72cf8a0 --- mymenu.c +++ mymenu.c @@ -82,6 +82,7 @@ struct rendering { int width; int height; XFontSet *font; + bool horizontal_layout; }; struct completions { @@ -283,7 +284,7 @@ int readlines (char **lines) { // |------------------|----------------------------------------------| // | 20 char text | completion | completion | completion | compl | // |------------------|----------------------------------------------| -void draw(struct rendering *r, char *text, struct completions *cs) { +void draw_horizontally(struct rendering *r, char *text, struct completions *cs) { // TODO: make these dynamic? int prompt_width = 20; // char int padding = 10; @@ -325,7 +326,51 @@ void draw(struct rendering *r, char *text, struct comp XFlush(r->d); } + +// |-----------------------------------------------------------------| +// | prompt | +// |-----------------------------------------------------------------| +// | completion | +// |-----------------------------------------------------------------| +// | completion | +// |-----------------------------------------------------------------| +// TODO: dunno why but in the call to Xutf8DrawString, by logic, +// should be padding and not padding*2, but the text doesn't seem to +// be vertically centered otherwise.... +void draw_vertically(struct rendering *r, char *text, struct completions *cs) { + int padding = 10; // TODO make this dynamic + XRectangle rect; + XmbTextExtents(*r->font, "fjpgl", 5, &rect, nil); + int start_at = rect.height + padding*2; + + XFillRectangle(r->d, r->w, r->completion_bg, 0, 0, r->width, r->height); + XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, r->width, start_at); + Xutf8DrawString(r->d, r->w, *r->font, r->prompt, padding, padding*2, text, strlen(text)); + + while (cs != nil) { + GC g = cs->selected ? r->completion_highlighted : r->completion; + GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg; + + int len = strlen(cs->completion); + XmbTextExtents(*r->font, cs->completion, len, &rect, nil); + XFillRectangle(r->d, r->w, h, 0, start_at, r->width, rect.height + padding*2); + Xutf8DrawString(r->d, r->w, *r->font, g, padding, start_at + padding*2, cs->completion, len); + + start_at += rect.height + padding *2; + cs = cs->next; + } + + XFlush(r->d); +} + +void draw(struct rendering *r, char *text, struct completions *cs) { + if (r->horizontal_layout) + draw_horizontally(r, text, cs); + else + draw_vertically(r, text, cs); +} + /* Set some WM stuff */ void set_win_atoms_hints(Display *d, Window w, int width, int height) { Atom type; @@ -518,6 +563,8 @@ int main() { compl_fg, compl_bg, compl_highlighted_fg, compl_highlighted_bg; + bool horizontal_layout = true; + // read resource XrmInitialize(); char *xrm = XResourceManagerString(d); @@ -533,6 +580,15 @@ int main() { } else fprintf(stderr, "no font defined, using %s\n", fontname); + + if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true) { + char *v = strdup(value.addr); + check_allocation(v); + horizontal_layout = !strcmp(v, "horizontal"); + free(v); + } + else + fprintf(stderr, "no layout defined, using horizontal\n"); if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true) width = parse_integer(value.addr, width, d_width); @@ -671,7 +727,8 @@ int main() { /* .completion_highlighted_bg = XCreateGC(d, w, GCFont, &values), */ .width = width, .height = height, - .font = &font + .font = &font, + .horizontal_layout = horizontal_layout }; // load the colors in our GCs blob - /dev/null blob + d922d7eb9ccd5920807654268f17275e5dbf8fb0 (mode 644) Binary files /dev/null and screen-alt.png differ