fixed vlist paging, cleaned up 6366f94e
Connor Lane Smith · 2010-06-11 09:24 1 file(s) · +32 −58
dmenu.c +32 −58
111 111
112 112
void
113 113
calcoffsetsh(void) {
114 -
	int tw;
115 114
	unsigned int w;
116 115
117 116
	if(!curr)
118 117
		return;
119 118
	w = promptw + cmdw + 2 * spaceitem;
120 -
	for(next = curr; next; next=next->right) {
121 -
		tw = MIN(textw(next->text), mw / 3);
122 -
		w += tw;
123 -
		if(w > mw)
124 -
			break;
125 -
	}
119 +
	for(next = curr; next && w < mw; next=next->right)
120 +
		w += MIN(textw(next->text), mw / 3);
126 121
	w = promptw + cmdw + 2 * spaceitem;
127 -
	for(prev = curr; prev && prev->left; prev=prev->left) {
128 -
		tw = MIN(textw(prev->left->text), mw / 3);
129 -
		w += tw;
130 -
		if(w > mw)
131 -
			break;
132 -
	}
122 +
	for(prev = curr; prev && prev->left && w < mw; prev=prev->left)
123 +
		w += MIN(textw(prev->left->text), mw / 3);
133 124
}
134 125
135 126
void
136 127
calcoffsetsv(void) {
137 -
	static unsigned int h;
128 +
	unsigned int h;
138 129
139 130
	if(!curr)
140 131
		return;
141 -
	h = (dc.font.height + 2) * (lines + 1);
142 -
	for(next = curr; next; next=next->right) {
132 +
	h = (dc.font.height + 2) * lines;
133 +
	for(next = curr; next && h > 0; next = next->right)
143 134
		h -= dc.font.height + 2;
144 -
		if(h <= 0)
145 -
			break;
146 -
	}
147 -
	h = (dc.font.height + 2) * (lines + 1);
148 -
	for(prev = curr; prev && prev->left; prev=prev->left) {
135 +
	h = (dc.font.height + 2) * lines;
136 +
	for(prev = curr; prev && prev->left && h > 0; prev = prev->left)
149 137
		h -= dc.font.height + 2;
150 -
		if(h <= 0)
151 -
			break;
152 -
	}
153 138
}
154 139
155 140
char *
177 162
178 163
void
179 164
cleanup(void) {
180 -
	Item *itm;
181 -
182 -
	while(allitems) {
183 -
		itm = allitems->next;
184 -
		free(allitems->text);
185 -
		free(allitems);
186 -
		allitems = itm;
187 -
	}
188 165
	if(dc.font.set)
189 166
		XFreeFontSet(dpy, dc.font.set);
190 167
	else
257 234
	Item *i;
258 235
259 236
	dc.w = mw - dc.x;
260 -
	dc.y += dc.font.height + 2;
237 +
	dc.h = dc.font.height + 2;
238 +
	dc.y = dc.h;
261 239
	for(i = curr; i != next; i=i->right) {
262 240
		drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
263 -
		dc.y += dc.font.height + 2;
241 +
		dc.y += dc.h;
264 242
	}
243 +
	dc.h = mh - dc.y;
265 244
	drawtext(NULL, dc.norm);
266 245
}
267 246
309 288
	XColor color;
310 289
311 290
	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
312 -
		eprint("error, cannot allocate color '%s'\n", colstr);
291 +
		eprint("dmenu: cannot allocate color '%s'\n", colstr);
313 292
	return color.pixel;
314 293
}
315 294
328 307
329 308
void
330 309
initfont(const char *fontstr) {
331 -
	char *def, **missing;
310 +
	char *def, **missing = NULL;
332 311
	int i, n;
333 312
334 313
	if(!fontstr || fontstr[0] == '\0')
335 -
		eprint("error, cannot load font: '%s'\n", fontstr);
336 -
	missing = NULL;
314 +
		eprint("dmenu: cannot load font: '%s'\n", fontstr);
337 315
	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
338 316
	if(missing)
339 317
		XFreeStringList(missing);
351 329
	else {
352 330
		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
353 331
		&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
354 -
			eprint("error, cannot load font: '%s'\n", fontstr);
332 +
			eprint("dmenu: cannot load font: '%s'\n", fontstr);
355 333
		dc.font.ascent = dc.font.xfont->ascent;
356 334
		dc.font.descent = dc.font.xfont->descent;
357 335
	}
361 339
void
362 340
kpress(XKeyEvent * e) {
363 341
	char buf[sizeof text];
364 -
	int i, num, off;
342 +
	int i, num;
365 343
	unsigned int len;
366 344
	KeySym ksym;
367 345
368 346
	len = strlen(text);
369 -
	buf[0] = '\0';
370 347
	num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
371 348
	if(IsKeypadKey(ksym)) {
372 349
		if(ksym == XK_KP_Enter)
451 428
			{
452 429
				FILE *fp;
453 430
				char *s;
454 -
				if(!(fp = (FILE*)popen("sselp", "r")))
455 -
					eprint("dmenu: Could not popen sselp\n");
431 +
				if(!(fp = popen("sselp", "r")))
432 +
					eprint("dmenu: cannot popen sselp\n");
456 433
				s = fgets(buf, sizeof buf, fp);
457 434
				pclose(fp);
458 435
				if(s == NULL)
470 447
		if(num && !iscntrl((int) buf[0])) {
471 448
			memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
472 449
			memcpy(text + cursor, buf, num);
473 -
			cursor+=num;
450 +
			cursor += num;
474 451
			match(text);
475 452
		}
476 453
		break;
477 454
	case XK_BackSpace:
478 455
		if(cursor > 0) {
479 -
			off = 1;
480 -
			while(cursor > off && !IS_UTF8_1ST_CHAR(text[cursor - off]))
481 -
				off++;
482 -
			memmove(text + cursor - off, text + cursor, sizeof text - cursor + off);
483 -
			cursor -= off;
456 +
			for(i = 1; cursor - i > 0 && !IS_UTF8_1ST_CHAR(text[cursor - i]); i++);
457 +
			memmove(text + cursor - i, text + cursor, sizeof text - cursor + i);
458 +
			cursor -= i;
484 459
			match(text);
485 460
		}
486 461
		break;
487 462
	case XK_Delete:
488 -
		off = 1;
489 -
		while(cursor + off < sizeof text - 1 && !IS_UTF8_1ST_CHAR(text[cursor + off]))
490 -
			off++;
491 -
		memmove(text + cursor, text + cursor + off, sizeof text - cursor);
463 +
		for(i = 1; cursor + i < len && !IS_UTF8_1ST_CHAR(text[cursor + i]); i++);
464 +
		memmove(text + cursor, text + cursor + i, sizeof text - cursor);
492 465
		match(text);
493 466
		break;
494 467
	case XK_End:
631 604
		if(buf[len-1] == '\n')
632 605
			buf[--len] = '\0';
633 606
		if(!(p = strdup(buf)))
634 -
			eprint("fatal: could not strdup() %u bytes\n", len);
607 +
			eprint("dmenu: cannot strdup %u bytes\n", len);
635 608
		if(max < len || !maxname) {
636 609
			maxname = p;
637 610
			max = len;
638 611
		}
639 -
		if(!(new = (Item *)malloc(sizeof(Item))))
640 -
			eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
612 +
		if(!(new = malloc(sizeof *new)))
613 +
			eprint("dmenu: cannot malloc %u bytes\n", sizeof *new);
641 614
		new->next = new->left = new->right = NULL;
642 615
		new->text = p;
643 616
		if(!i)
785 758
			if(++i < argc) parent = atoi(argv[i]);
786 759
		}
787 760
		else if(!strcmp(argv[i], "-l")) {
788 -
			calcoffsets = calcoffsetsv;
789 761
			if(++i < argc) lines = atoi(argv[i]);
762 +
			if(lines > 0)
763 +
				calcoffsets = calcoffsetsv;
790 764
		}
791 765
		else if(!strcmp(argv[i], "-fn")) {
792 766
			if(++i < argc) font = argv[i];
812 786
			eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
813 787
			       "             [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
814 788
	if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
815 -
		fprintf(stderr, "warning: no locale support\n");
789 +
		fprintf(stderr, "dmenu: warning: no locale support\n");
816 790
	if(!(dpy = XOpenDisplay(NULL)))
817 791
		eprint("dmenu: cannot open display\n");
818 792
	screen = DefaultScreen(dpy);