Skip to content

Commit

Permalink
add -t
Browse files Browse the repository at this point in the history
  • Loading branch information
phillbush committed Mar 8, 2021
1 parent 73f754c commit 9e8388d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
12 changes: 11 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@ static struct Config config = {

/* sizes between 0 and 1 */
.separatorbeg = 0.14, /* beginning of the separator */
.separatorend = 0.37 /* end of the separator */
.separatorend = 0.37, /* end of the separator */

/*
* The variables below cannot be set by X resources.
* Their values must be less than .height_pixels.
*/

/* geometry of the right-pointing isoceles triangle for submenus */
.triangle_width = 3,
.triangle_height = 7,
.triangle_distance = 6 /* distance from the border of the menu */
};
5 changes: 4 additions & 1 deletion pmenu.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pmenu \- pie menu utility for X
.SH SYNOPSIS
.B pmenu
.RB [ \-w ]
.RB [ \-tw ]
.SH DESCRIPTION
.B pmenu
is a pie menu for X,
Expand All @@ -14,6 +14,9 @@ The pie menu contains one slice for each item.
.PP
The options are as follows:
.TP
.B \-t
Draw a triangle on the border of slices that spawn a submenu.
.TP
.B \-w
Disable pointer warping when a new submenu spawns.
This option is useful when using
Expand Down
73 changes: 64 additions & 9 deletions pmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static struct Monitor mon;
static struct Pie pie;

/* flags */
static int tflag = 0; /* whether to draw triangle for submenus */
static int wflag = 0; /* whether to disable pointer warping */

#include "config.h"
Expand Down Expand Up @@ -82,8 +83,11 @@ getoptions(int *argc, char ***argv)
{
int ch;

while ((ch = getopt(*argc, *argv, "w")) != -1) {
while ((ch = getopt(*argc, *argv, "tw")) != -1) {
switch (ch) {
case 't':
tflag = 1;
break;
case 'w':
wflag = 1;
break;
Expand Down Expand Up @@ -149,7 +153,7 @@ static void
initdc(void)
{
XGCValues values;
Pixmap pbg, pselbg, separator;
Pixmap pbg, pfg, pselbg, pselfg, separator;
unsigned long valuemask;

/* get color pixels */
Expand All @@ -173,16 +177,24 @@ initdc(void)
dc.pictattr.repeat = 1;
dc.pictattr.poly_edge = PolyEdgeSmooth;
pbg = XCreatePixmap(dpy, rootwin, 1, 1, depth);
pfg = XCreatePixmap(dpy, rootwin, 1, 1, depth);
pselbg = XCreatePixmap(dpy, rootwin, 1, 1, depth);
pselfg = XCreatePixmap(dpy, rootwin, 1, 1, depth);
separator = XCreatePixmap(dpy, rootwin, 1, 1, depth);
pie.bg = XRenderCreatePicture(dpy, pbg, xformat, CPRepeat, &dc.pictattr);
pie.fg = XRenderCreatePicture(dpy, pfg, xformat, CPRepeat, &dc.pictattr);
pie.selbg = XRenderCreatePicture(dpy, pselbg, xformat, CPRepeat, &dc.pictattr);
pie.selfg = XRenderCreatePicture(dpy, pselfg, xformat, CPRepeat, &dc.pictattr);
pie.separator = XRenderCreatePicture(dpy, separator, xformat, CPRepeat, &dc.pictattr);
XRenderFillRectangle(dpy, PictOpOver, pie.bg, &dc.normal[ColorBG].color, 0, 0, 1, 1);
XRenderFillRectangle(dpy, PictOpOver, pie.fg, &dc.normal[ColorFG].color, 0, 0, 1, 1);
XRenderFillRectangle(dpy, PictOpOver, pie.selbg, &dc.selected[ColorBG].color, 0, 0, 1, 1);
XRenderFillRectangle(dpy, PictOpOver, pie.selfg, &dc.selected[ColorFG].color, 0, 0, 1, 1);
XRenderFillRectangle(dpy, PictOpOver, pie.separator, &dc.separator.color, 0, 0, 1, 1);
XFreePixmap(dpy, pbg);
XFreePixmap(dpy, pfg);
XFreePixmap(dpy, pselbg);
XFreePixmap(dpy, pselfg);
XFreePixmap(dpy, separator);
}

Expand All @@ -199,6 +211,11 @@ initpie(void)
pie.radius = (pie.diameter + 1) / 2;
pie.fulldiameter = pie.diameter + (pie.border * 2);

/* set the geometry of the triangle for submenus */
pie.triangleouter = pie.radius - config.triangle_distance;
pie.triangleinner = pie.radius - config.triangle_distance - config.triangle_width;
pie.triangleangle = ((double)config.triangle_height / 2.0) / (double)pie.triangleinner;

/* set the separator beginning and end */
pie.separatorbeg = pie.radius * config.separatorbeg;
pie.separatorend = pie.radius * config.separatorend;
Expand Down Expand Up @@ -921,9 +938,6 @@ drawslice(struct Menu *menu, struct Slice *slice)
int i, outer, inner, npoints;
double h, a, b;

if (slice == NULL)
return;

/* determine number of segments to draw */
h = hypot(pie.radius, pie.radius)/2;
outer = ((2 * M_PI) / (menu->nslices * acos(h/(h+1.0)))) + 0.5;
Expand Down Expand Up @@ -964,7 +978,7 @@ drawseparator(Picture picture, struct Menu *menu, struct Slice *slice)
XPointDouble p[4];
double a;

a = (M_PI / menu->nslices) + ((2 * M_PI) / menu->nslices) * slice->slicen;
a = -((M_PI + 2 * M_PI * slice->slicen) / menu->nslices);
p[0].x = pie.radius + pie.separatorbeg * cos(a - pie.innerangle);
p[0].y = pie.radius + pie.separatorbeg * sin(a - pie.innerangle);
p[1].x = pie.radius + pie.separatorbeg * cos(a + pie.innerangle);
Expand All @@ -978,6 +992,25 @@ drawseparator(Picture picture, struct Menu *menu, struct Slice *slice)
0, 0, 0, 0, p, 4, 0);
}

/* draw triangle for slice with submenu */
static void
drawtriangle(Picture source, Picture picture, struct Menu *menu, struct Slice *slice)
{
XPointDouble p[3];
double a;

a = - (((2 * M_PI) / menu->nslices) * slice->slicen);
p[0].x = pie.radius + pie.triangleinner * cos(a - pie.triangleangle);
p[0].y = pie.radius + pie.triangleinner * sin(a - pie.triangleangle);
p[1].x = pie.radius + pie.triangleouter * cos(a);
p[1].y = pie.radius + pie.triangleouter * sin(a);
p[2].x = pie.radius + pie.triangleinner * cos(a + pie.triangleangle);
p[2].y = pie.radius + pie.triangleinner * sin(a + pie.triangleangle);
XRenderCompositeDoublePoly(dpy, PictOpOver, source, picture,
XRenderFindStandardFormat(dpy, PictStandardA8),
0, 0, 0, 0, p, 3, 0);
}

/* draw regular slice */
static void
drawmenu(struct Menu *menu, struct Slice *selected)
Expand All @@ -987,6 +1020,7 @@ drawmenu(struct Menu *menu, struct Slice *selected)
XftDraw *draw;
Drawable pixmap;
Picture picture;
Picture source;

if (selected) {
pixmap = selected->pixmap;
Expand All @@ -1001,14 +1035,18 @@ drawmenu(struct Menu *menu, struct Slice *selected)
/* draw background */
XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel);
XFillRectangle(dpy, pixmap, dc.gc, 0, 0, pie.diameter, pie.diameter);
drawslice(menu, selected);
if (selected)
drawslice(menu, selected);

/* draw slice foreground */
for (slice = menu->list; slice; slice = slice->next) {
if (slice == selected)
if (slice == selected) {
color = dc.selected;
else
source = pie.selfg;
} else {
color = dc.normal;
source = pie.fg;
}

if (slice->file) { /* if there is an icon, draw it */
imlib_context_set_drawable(pixmap);
Expand All @@ -1024,6 +1062,11 @@ drawmenu(struct Menu *menu, struct Slice *selected)

/* draw separator */
drawseparator(picture, menu, slice);

/* draw triangle */
if (slice->submenu && tflag) {
drawtriangle(source, picture, menu, slice);
}
}
}

Expand Down Expand Up @@ -1243,6 +1286,17 @@ cleanmenu(struct Menu *menu)
free(menu);
}

/* free pictures */
static void
cleanpictures(void)
{
XRenderFreePicture(dpy, pie.bg);
XRenderFreePicture(dpy, pie.fg);
XRenderFreePicture(dpy, pie.selbg);
XRenderFreePicture(dpy, pie.selfg);
XRenderFreePicture(dpy, pie.separator);
}

/* cleanup drawing context */
static void
cleandc(void)
Expand Down Expand Up @@ -1300,6 +1354,7 @@ main(int argc, char *argv[])

/* freeing stuff */
cleanmenu(rootmenu);
cleanpictures();
cleandc();
XCloseDisplay(dpy);

Expand Down
8 changes: 8 additions & 0 deletions pmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ struct Config {
const char *border_color;
int border_pixels;
int separator_pixels;
int triangle_width;
int triangle_height;
int triangle_distance;
unsigned diameter_pixels;
double separatorbeg;
double separatorend;
Expand Down Expand Up @@ -98,12 +101,17 @@ struct Pie {
int radius; /* radius of the pie */
int border; /* border of the pie */

int triangleinner;
int triangleouter;
int separatorbeg;
int separatorend;
double triangleangle;
double innerangle;
double outerangle;

Picture bg;
Picture fg;
Picture selbg;
Picture selfg;
Picture separator;
};

0 comments on commit 9e8388d

Please sign in to comment.