diff options
| author | Anselm R. Garbe <garbeam@wmii.de> | 2006-07-12 15:17:22 +0200 |
|---|---|---|
| committer | Anselm R. Garbe <garbeam@wmii.de> | 2006-07-12 15:17:22 +0200 |
| commit | dfd84f9bf3b9d949412a73bc62a43109b340d395 (patch) | |
| tree | ebeac582951e2ef574f50018298c4bc928849d64 /menu.c | |
| parent | fixed configurerequest (diff) | |
| download | dwm-dfd84f9bf3b9d949412a73bc62a43109b340d395.tar.gz dwm-dfd84f9bf3b9d949412a73bc62a43109b340d395.zip | |
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Diffstat (limited to 'menu.c')
| -rw-r--r-- | menu.c | 97 |
1 files changed, 46 insertions, 51 deletions
| @@ -31,7 +31,6 @@ struct Item { | |||
| 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 */ |
| @@ -41,14 +40,14 @@ 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}; |
| @@ -74,21 +73,21 @@ update_offsets() | |||
| 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 | } |
| @@ -103,9 +102,9 @@ update_items(char *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; |
| @@ -143,42 +142,40 @@ update_items(char *pattern) | |||
| 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); |
| @@ -186,15 +183,14 @@ draw_menu() | |||
| 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 | ||
| @@ -399,36 +395,35 @@ main(int argc, char *argv[]) | |||
| 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; | ||
| 406 | 401 | ||
| 407 | win = XCreateWindow(dpy, root, rect.x, rect.y, | 402 | win = XCreateWindow(dpy, root, mx, my, mw, mh, 0, |
| 408 | rect.width, rect.height, 0, DefaultDepth(dpy, screen), | 403 | DefaultDepth(dpy, screen), CopyFromParent, |
| 409 | CopyFromParent, DefaultVisual(dpy, screen), | 404 | 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); |
