menu.c
changeset 26 e8f627998d6f
parent 13 5cc5e55a132d
child 31 386649deb651
equal deleted inserted replaced
25:e238dc4844d7 26:e8f627998d6f
    29 };
    29 };
    30 
    30 
    31 static Display *dpy;
    31 static Display *dpy;
    32 static Window root;
    32 static Window root;
    33 static Window win;
    33 static Window win;
    34 static XRectangle rect;
       
    35 static Bool done = False;
    34 static Bool done = False;
    36 
    35 
    37 static Item *allitem = NULL;	/* first of all items */
    36 static Item *allitem = NULL;	/* first of all items */
    38 static Item *item = NULL;	/* first of pattern matching items */
    37 static Item *item = NULL;	/* first of pattern matching items */
    39 static Item *sel = NULL;
    38 static Item *sel = NULL;
    40 static Item *nextoff = NULL;
    39 static Item *nextoff = NULL;
    41 static Item *prevoff = NULL;
    40 static Item *prevoff = NULL;
    42 static Item *curroff = NULL;
    41 static Item *curroff = NULL;
    43 
    42 
    44 static int screen;
    43 static int screen, mx, my, mw, mh;
    45 static char *title = NULL;
    44 static char *title = NULL;
    46 static char text[4096];
    45 static char text[4096];
    47 static int ret = 0;
    46 static int ret = 0;
    48 static int nitem = 0;
    47 static int nitem = 0;
    49 static unsigned int cmdw = 0;
    48 static unsigned int cmdw = 0;
    50 static unsigned int twidth = 0;
    49 static unsigned int tw = 0;
    51 static unsigned int cwidth = 0;
    50 static unsigned int cw = 0;
    52 static const int seek = 30;		/* 30px */
    51 static const int seek = 30;		/* 30px */
    53 
    52 
    54 static Brush brush = {0};
    53 static Brush brush = {0};
    55 
    54 
    56 static void draw_menu();
    55 static void draw_menu();
    72 
    71 
    73 	if(!curroff)
    72 	if(!curroff)
    74 		return;
    73 		return;
    75 
    74 
    76 	for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
    75 	for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
    77 		tw = textwidth(&brush.font, nextoff->text);
    76 		tw = textw(&brush.font, nextoff->text);
    78 		if(tw > rect.width / 3)
    77 		if(tw > mw / 3)
    79 			tw = rect.width / 3;
    78 			tw = mw / 3;
    80 		w += tw + brush.font.height;
    79 		w += tw + brush.font.height;
    81 		if(w > rect.width)
    80 		if(w > mw)
    82 			break;
    81 			break;
    83 	}
    82 	}
    84 
    83 
    85 	w = cmdw + 2 * seek;
    84 	w = cmdw + 2 * seek;
    86 	for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
    85 	for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
    87 		tw = textwidth(&brush.font, prevoff->left->text);
    86 		tw = textw(&brush.font, prevoff->left->text);
    88 		if(tw > rect.width / 3)
    87 		if(tw > mw / 3)
    89 			tw = rect.width / 3;
    88 			tw = mw / 3;
    90 		w += tw + brush.font.height;
    89 		w += tw + brush.font.height;
    91 		if(w > rect.width)
    90 		if(w > mw)
    92 			break;
    91 			break;
    93 	}
    92 	}
    94 }
    93 }
    95 
    94 
    96 static void
    95 static void
   101 
   100 
   102 	if(!pattern)
   101 	if(!pattern)
   103 		return;
   102 		return;
   104 
   103 
   105 	if(!title || *pattern)
   104 	if(!title || *pattern)
   106 		cmdw = cwidth;
   105 		cmdw = cw;
   107 	else
   106 	else
   108 		cmdw = twidth;
   107 		cmdw = tw;
   109 
   108 
   110 	item = j = NULL;
   109 	item = j = NULL;
   111 	nitem = 0;
   110 	nitem = 0;
   112 
   111 
   113 	for(i = allitem; i; i=i->next)
   112 	for(i = allitem; i; i=i->next)
   141 
   140 
   142 /* creates brush structs for brush mode drawing */
   141 /* creates brush structs for brush mode drawing */
   143 static void
   142 static void
   144 draw_menu()
   143 draw_menu()
   145 {
   144 {
   146 	unsigned int offx = 0;
       
   147 	Item *i;
   145 	Item *i;
   148 
   146 
   149 	brush.rect = rect;
   147 	brush.x = 0;
   150 	brush.rect.x = 0;
   148 	brush.y = 0;
   151 	brush.rect.y = 0;
   149 	brush.w = mw;
       
   150 	brush.h = mh;
   152 	draw(dpy, &brush, False, 0);
   151 	draw(dpy, &brush, False, 0);
   153 
   152 
   154 	/* print command */
   153 	/* print command */
   155 	if(!title || text[0]) {
   154 	if(!title || text[0]) {
   156 		cmdw = cwidth;
   155 		cmdw = cw;
   157 		if(cmdw && item)
   156 		if(cmdw && item)
   158 			brush.rect.width = cmdw;
   157 			brush.w = cmdw;
   159 		draw(dpy, &brush, False, text);
   158 		draw(dpy, &brush, False, text);
   160 	}
   159 	}
   161 	else {
   160 	else {
   162 		cmdw = twidth;
   161 		cmdw = tw;
   163 		brush.rect.width = cmdw;
   162 		brush.w = cmdw;
   164 		draw(dpy, &brush, False, title);
   163 		draw(dpy, &brush, False, title);
   165 	}
   164 	}
   166 	offx += brush.rect.width;
   165 	brush.x += brush.w;
   167 
   166 
   168 	if(curroff) {
   167 	if(curroff) {
   169 		brush.rect.x = offx;
   168 		brush.w = seek;
   170 		brush.rect.width = seek;
       
   171 		offx += brush.rect.width;
       
   172 		draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
   169 		draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
       
   170 		brush.x += brush.w;
   173 
   171 
   174 		/* determine maximum items */
   172 		/* determine maximum items */
   175 		for(i = curroff; i != nextoff; i=i->right) {
   173 		for(i = curroff; i != nextoff; i=i->right) {
   176 			brush.border = False;
   174 			brush.border = False;
   177 			brush.rect.x = offx;
   175 			brush.w = textw(&brush.font, i->text);
   178 			brush.rect.width = textwidth(&brush.font, i->text);
   176 			if(brush.w > mw / 3)
   179 			if(brush.rect.width > rect.width / 3)
   177 				brush.w = mw / 3;
   180 				brush.rect.width = rect.width / 3;
   178 			brush.w += brush.font.height;
   181 			brush.rect.width += brush.font.height;
       
   182 			if(sel == i) {
   179 			if(sel == i) {
   183 				swap((void **)&brush.fg, (void **)&brush.bg);
   180 				swap((void **)&brush.fg, (void **)&brush.bg);
   184 				draw(dpy, &brush, True, i->text);
   181 				draw(dpy, &brush, True, i->text);
   185 				swap((void **)&brush.fg, (void **)&brush.bg);
   182 				swap((void **)&brush.fg, (void **)&brush.bg);
   186 			}
   183 			}
   187 			else
   184 			else
   188 				draw(dpy, &brush, False, i->text);
   185 				draw(dpy, &brush, False, i->text);
   189 			offx += brush.rect.width;
   186 			brush.x += brush.w;
   190 		}
   187 		}
   191 
   188 
   192 		brush.rect.x = rect.width - seek;
   189 		brush.x = mw - seek;
   193 		brush.rect.width = seek;
   190 		brush.w = seek;
   194 		draw(dpy, &brush, False, nextoff ? ">" : 0);
   191 		draw(dpy, &brush, False, nextoff ? ">" : 0);
   195 	}
   192 	}
   196 	XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, rect.width,
   193 	XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
   197 			rect.height, 0, 0);
       
   198 	XFlush(dpy);
   194 	XFlush(dpy);
   199 }
   195 }
   200 
   196 
   201 static void
   197 static void
   202 kpress(XKeyEvent * e)
   198 kpress(XKeyEvent * e)
   397 
   393 
   398 	wa.override_redirect = 1;
   394 	wa.override_redirect = 1;
   399 	wa.background_pixmap = ParentRelative;
   395 	wa.background_pixmap = ParentRelative;
   400 	wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
   396 	wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
   401 
   397 
   402 	rect.width = DisplayWidth(dpy, screen);
   398 	mx = my = 0;
   403 	rect.height = labelheight(&brush.font);
   399 	mw = DisplayWidth(dpy, screen);
   404 	rect.y = DisplayHeight(dpy, screen) - rect.height;
   400 	mh = texth(&brush.font);
   405 	rect.x = 0;
   401 
   406 
   402 	win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
   407 	win = XCreateWindow(dpy, root, rect.x, rect.y,
   403 			DefaultDepth(dpy, screen), CopyFromParent,
   408 			rect.width, rect.height, 0, DefaultDepth(dpy, screen),
   404 			DefaultVisual(dpy, screen),
   409 			CopyFromParent, DefaultVisual(dpy, screen),
       
   410 			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
   405 			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
   411 	XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
   406 	XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
   412 	XFlush(dpy);
   407 	XFlush(dpy);
   413 
   408 
   414 	/* pixmap */
   409 	/* pixmap */
   415 	brush.gc = XCreateGC(dpy, root, 0, 0);
   410 	brush.gc = XCreateGC(dpy, root, 0, 0);
   416 	brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height,
   411 	brush.drawable = XCreatePixmap(dpy, win, mw, mh,
   417 			DefaultDepth(dpy, screen));
   412 			DefaultDepth(dpy, screen));
   418 	XFlush(dpy);
   413 	XFlush(dpy);
   419 
   414 
   420 	if(maxname)
   415 	if(maxname)
   421 		cwidth = textwidth(&brush.font, maxname) + brush.font.height;
   416 		cw = textw(&brush.font, maxname) + brush.font.height;
   422 	if(cwidth > rect.width / 3)
   417 	if(cw > mw / 3)
   423 		cwidth = rect.width / 3;
   418 		cw = mw / 3;
   424 
   419 
   425 	if(title) {
   420 	if(title) {
   426 		twidth = textwidth(&brush.font, title) + brush.font.height;
   421 		tw = textw(&brush.font, title) + brush.font.height;
   427 		if(twidth > rect.width / 3)
   422 		if(tw > mw / 3)
   428 			twidth = rect.width / 3;
   423 			tw = mw / 3;
   429 	}
   424 	}
   430 
   425 
   431 	cmdw = title ? twidth : cwidth;
   426 	cmdw = title ? tw : cw;
   432 
   427 
   433 	text[0] = 0;
   428 	text[0] = 0;
   434 	update_items(text);
   429 	update_items(text);
   435 	XMapRaised(dpy, win);
   430 	XMapRaised(dpy, win);
   436 	draw_menu();
   431 	draw_menu();