⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 widgets.c

📁 一个很好的SDL建立应用程序界面的例子
💻 C
📖 第 1 页 / 共 5 页
字号:
	y += 4;	closebtn->x = x;	closebtn->y = y;	addWList(closebtn);}// Display a TextBoxvoid showTextBox(TEXTBOX *t){	int		i, j, x, y, xlim, ylim, fg, bg, xoffset, pos;	BUTTON	*btn;	BLIST		*bl;	TLIST		*tl;	char		*str, chr;	makeBar(t->x + 1, t->y + 1, t->w - 2, t->h - 2, t->bg);	// body of window	makeBar(t->x, t->y, t->w, 1, t->hi);							// top border	makeBar(t->x + 1, t->y + t->h - 1, t->w - 1, 1, t->lo);	// bottom border	makeBar(t->x, t->y, 1, t->h, t->hi);							// left border	makeBar(t->x + t->w - 1, t->y + 1, 1, t->h - 1, t->lo);	// right border	x = t->x + 4;	y = t->y + 1;	xlim = t->w - (2 + FONT_WIDTH);	ylim = t->y + 1 + t->h - FONT_HEIGHT;	fg = t->fg;	bg = t->bg;	bl = t->list;	while (bl)	{		btn = bl->thislist.button;		if (btn)			showWidget(btn);		bl = bl->next;	}	tl = t->text;	i = t->line;	while (i && tl && tl->next)	{		tl = tl->next;		--i;	}	while (tl && y < ylim)	{		str = tl->str;		fg = tl->fg;		bg = tl->bg;		i = 0;		j = x;		pos = 0;		if (t->xoffset)		{			xoffset = t->xoffset;			while (*str && xoffset)			{				str++;				--xoffset;			}		}		while (str && *str && i < xlim)		{			chr = *str++;			if ((chr & 0x7f) == '\n')				break;			if ((chr & 0x7f) == '\t')			{				drawChar(j, y, fg, bg, ' ');				pos++;				i += FONT_WIDTH;				j += FONT_WIDTH;				if (i >= xlim)					break;				while (pos & 7)				{					drawChar(j, y, fg, bg, ' ');					pos++;					i += FONT_WIDTH;					j += FONT_WIDTH;					if (i >= xlim)						break;				}			}			else			{				if (chr & 0x80)					drawChar(j, y, bg, fg, chr & 0x7f);				else					drawChar(j, y, fg, bg, chr);				i += FONT_WIDTH;				j += FONT_WIDTH;				pos++;			}		}		y += FONT_HEIGHT;		tl = tl->next;	}}// Display an InputBoxvoid showInputBox(INPUTBOX *ib){	int		i, j, x, y, xlim, ylim, fg, bg, xc, yc, line;	BUTTON	*btn;	BLIST		*bl;	SLIST		*sl;	char		*str, chr;	makeBar(ib->x + 1, ib->y + 1, ib->w - 2, ib->h - 2, ib->bg);	// body of window	makeBar(ib->x, ib->y, ib->w, 1, ib->hi);								// top border	makeBar(ib->x + 1, ib->y + ib->h - 1, ib->w - 1, 1, ib->lo);	// bottom border	makeBar(ib->x, ib->y, 1, ib->h, ib->hi);								// left border	makeBar(ib->x + ib->w - 1, ib->y + 1, 1, ib->h - 1, ib->lo);	// right border	x = ib->x + 4;	y = ib->y + 1;	yc = y;	xlim = ib->w - (2 + FONT_WIDTH);	ylim = ib->y + 1 + ib->h - FONT_HEIGHT;	fg = ib->fg;	bg = ib->bg;	bl = ib->list;	while (bl)	{		btn = bl->thislist.button;		if (btn)		{			showWidget(btn);			if (childInParent(ib, btn))			{				y += btn->h;				yc += btn->h;			}		}		bl = bl->next;	}	sl = ib->text;	i = ib->line;	line = i;	while (i && sl && sl->next)	{		sl = sl->next;		--i;		line++;	}	while (sl && y < ylim)	{		str = sl->str;		i = 0;		j = x;		while (*str && i < xlim)		{			chr = *str++;			if (chr == '\n')				break;			drawChar(j, y, fg, bg, chr);			i += FONT_WIDTH;			j += FONT_WIDTH;		}		y += FONT_HEIGHT;		line++;		sl = sl->next;	}	xc = ib->cursorx * FONT_WIDTH;	y = ib->cursory;	if (y >= line)	{		y = line - 1;		ib->line++;	}}// Display a Buttonvoid showButton(BUTTON *b){	int		x, y;	char		*text;	WINDOW	*w = b->parent;	if (b->border)		y = b->y;	else		y = b->y + 1;	makeBar(b->x + 1, y, b->w - 2, b->h - 1, b->bg);	// body of button	x = b->x + 4;	text = b->text;	if (text)	{		if (b->y < w->y || b->y > w->y + w->h ||			 b->x < w->x || b->x > w->x + w->w)			// button outside the parent box		{			printString(b->x + 4, b->y, b->fg, b->bg, text);		}		else														// button inside the parent box		{			printString(x, b->y, b->fg, b->bg, text);			x += strlen(text) * FONT_WIDTH + 8;		}		if (b->border)		{			makeBar(b->x, b->y - 1, b->w, 1, b->hi);						// top			makeBar(b->x + 1, b->y + b->h - 1, b->w - 1, 1, b->lo);	// bottom			makeBar(b->x, b->y, 1, b->h, b->hi);							// left			makeBar(b->x + b->w - 1, b->y, 1, b->h - 1, b->lo);		// right		}	}}// Display a CheckBoxvoid showCheckBox(CHECKBOX *cb){	int		x;	char		*text;	makeBar(cb->x + 1, cb->y + 1, cb->w - 2, cb->h - 1, cb->bg);		// body of checkbox	x = cb->x + 4;	text = cb->text;	if (text)	{		drawChar(x, cb->y, cb->fg, cb->bg, '[');		if (*cb->value)			drawChar(x + FONT_WIDTH, cb->y, cb->fg, cb->bg, CHECK_MARK);		else			drawChar(x + FONT_WIDTH, cb->y, cb->fg, cb->bg, ' ');		drawChar(x + FONT_WIDTH * 2, cb->y, cb->fg, cb->bg, ']');		printString(x + FONT_WIDTH * 4, cb->y, cb->fg, cb->bg, text);		if (cb->border)		{			makeBar(cb->x, cb->y, 1, cb->h, cb->hi);		// left			makeBar(cb->x + cb->w - 1, cb->y + 1, 1, cb->h - 1, cb->lo);	// right		}		x += (strlen(text) + 4) * FONT_WIDTH + 8;	}}// Display a Labelvoid showLabel(LABEL *lab){	int	y, len, max;	char	*cptr;	if (lab->border)		y = lab->y;	else		y = lab->y + 1;	makeBar(lab->x + 1, y, lab->w - 2, lab->h - 1, lab->bg);	// body of label	if (lab->border)	{		makeBar(lab->x, lab->y - 1, lab->w, 1, lab->hi);						// top		makeBar(lab->x + 1, lab->y + lab->h - 1, lab->w - 1, 1, lab->lo);	// bottom		makeBar(lab->x, lab->y, 1, lab->h, lab->hi);								// left		makeBar(lab->x + lab->w - 1, lab->y, 1, lab->h - 1, lab->lo);		// right	}	cptr = lab->text;	if (cptr)	{		len = strlen(cptr);		max = lab->w / FONT_WIDTH - 1;// Since label names may change, usually because it's a directory or// file name, adjust the text pointer to show the tail end of the text.		if (len >= max)			cptr = (char *) ((int) cptr + len - max);		printString(lab->x + 4, lab->y, lab->fg, lab->bg, cptr);	}}// Display a Highlighted Labelvoid showHLabel(HLABEL *lab){	int	y, len, max;	char	*cptr;	if (lab->border)		y = lab->y;	else		y = lab->y + 1;	makeBar(lab->x + 1, y, lab->w - 2, lab->h - 1, lab->bg);	// body of label	if (lab->border)	{		makeBar(lab->x, lab->y - 1, lab->w, 1, lab->hi);						// top		makeBar(lab->x + 1, lab->y + lab->h - 1, lab->w - 1, 1, lab->lo);	// bottom		makeBar(lab->x, lab->y, 1, lab->h, lab->hi);								// left		makeBar(lab->x + lab->w - 1, lab->y, 1, lab->h - 1, lab->lo);		// right	}	cptr = lab->text;	if (cptr)	{		len = strlen(cptr);		max = lab->w / FONT_WIDTH - 1;// Since label names may change, usually because it's a directory or// file name, adjust the text pointer to show the tail end of the text.		if (len >= max)			cptr = (char *) ((int) cptr + len - max);		if (lab->hlflag)			printHLString(lab->x + 4, lab->y, lab->fg, lab->bg, cptr);		else			printString(lab->x + 4, lab->y, lab->fg, lab->bg, cptr);	}}// Display a left pointing arrowheadvoid showLeftArrow(int x, int y){	int	i;	setPixel(x, y, black);	for (i = -1; i < 2; i++)		setPixel(x + 1, y + i, black);	for (i = -2; i < 3; i++)		setPixel(x + 2, y + i, black);	for (i = -3; i < 4; i++)		setPixel(x + 3, y + i, black);}// Display a right pointing arrowheadvoid showRightArrow(int x, int y){	int	i;	setPixel(x, y, black);	for (i = -1; i < 2; i++)		setPixel(x - 1, y + i, black);	for (i = -2; i < 3; i++)		setPixel(x - 2, y + i, black);	for (i = -3; i < 4; i++)		setPixel(x - 3, y + i, black);}// Display an upward pointing arrowheadvoid showUpArrow(int x, int y){	int	i;	setPixel(x, y, black);	for (i = -1; i < 2; i++)		setPixel(x + i, y + 1, black);	for (i = -2; i < 3; i++)		setPixel(x + i, y + 2, black);	for (i = -3; i < 4; i++)		setPixel(x + i, y + 3, black);}// Display a downward pointing arrowheadvoid showDownArrow(int x, int y){	int	i;	setPixel(x, y, black);	for (i = -1; i < 2; i++)		setPixel(x + i, y - 1, black);	for (i = -2; i < 3; i++)		setPixel(x + i, y - 2, black);	for (i = -3; i < 4; i++)		setPixel(x + i, y - 3, black);}// Display a Vertical Slidervoid showVSlider(VSLIDER *vs){	int	x, y, w, h, p;	int	sstart, sstop, size;	BUTTON	*btn;	BLIST		*bl;	x = vs->x;	y = vs->y;	w = vs->w;	h = vs->h;	p = vs->position;	if (p < 0)		vs->position = p = 0;	bl = vs->list;	while (bl)	{		btn = bl->thislist.button;		if (btn)			showWidget(btn);		bl = bl->next;	}	makeBar(x, y, w + 1, 1, vs->hi);				// top border	makeBar(x + 1, y + h, w, 1, vs->lo);		// bottom	makeBar(x, y, 1, h + 1, vs->hi);				// left	makeBar(x + w, y + 1, 1, h - 1, vs->lo);	// right	size = vs->size;	sstart = p + SLIDERBUTTONSIZE;	if (sstart > h - size - SLIDERBUTTONSIZE)	// at bottom of slider		sstart = h - size - SLIDERBUTTONSIZE;	sstop = sstart + size;	if (sstart)														// slider not at top,		makeBar(x + 1, y + 1, w - 1, sstart, vs->bg);	// show area preceeding slider	makeBar(x + 1, y + sstart, w - 1, size, lgray);		// show the slider	if (sstop < h)		makeBar(x + 1, y + sstop, w - 1, h - sstop, vs->bg);	// show area following slider// do marker border	makeBar(x + 1, y + sstart, w - 1, 1, white);				// top	makeBar(x + 1, y + sstart + size, w - 1, 1, black);	// bottom	makeBar(x + 1, y + sstart, 1, size, white);				// left	makeBar(x + w - 1, y + sstart, 1, size, black);			// right	makeBar(x, y + SLIDERBUTTONSIZE - 2, w + 1, 1, black);			// top divider	showUpArrow(x + vs->w / 2, y + 2);	makeBar(x, y + vs->h - SLIDERBUTTONSIZE + 1, w + 1, 1, white);	// bottom divider	showDownArrow(x + vs->w / 2, y + vs->h - 2);}// Display a Horizontal Slidervoid showHSlider(HSLIDER *hs){	int	x, y, w, h, p;	int	sstart, sstop, size;	BUTTON	*btn;	BLIST		*bl;	x = hs->x;	y = hs->y;	w = hs->w;	h = hs->h;	p = hs->position;	if (p < 0)		hs->position = p = 0;	bl = hs->list;	while (bl)	{		btn = bl->thislist.button;		if (btn)			showWidget(btn);		bl = bl->next;	}	makeBar(x, y, w, 1, hs->hi);			// top border	makeBar(x, y + h, w, 1, hs->lo);		// bottom	makeBar(x, y, 1, h, hs->hi);			// left	makeBar(x + w, y, 1, h, hs->lo);		// right	size = hs->size;	sstart = p + SLIDERBUTTONSIZE;	if (sstart > w - size - SLIDERBUTTONSIZE)	// at right end of slider		sstart = w - size - SLIDERBUTTONSIZE;	sstop = sstart + size;	if (sstart)														// slider not at left end,		makeBar(x + 1, y + 1, sstart, h - 1, hs->bg);	// show area preceeding slider	makeBar(x + sstart, y + 1, size, h - 1, lgray);		// show the slider	if (sstop < w)		makeBar(x + sstop, y + 1, w - sstop, h - 1, hs->bg);	// show area following slider// do marker border	makeBar(x + sstart, y + 1, size, 1, white);			// top	makeBar(x + sstart, y + h - 1, size, 1, black);		// bottom	makeBar(x + sstart, y + 1, 1, h - 1, white);			// left	makeBar(x + sstop, y + 1, 1, h - 1, black);			// right	makeBar(x + SLIDERBUTTONSIZE - 1, y, 1, h + 1, black);			// left divider	showLeftArrow(x + 2, y + h / 2);	makeBar(x + hs->w - SLIDERBUTTONSIZE + 2, y, 1, h + 1, white);	// right divider	showRightArrow(x + hs->w - 2, y + h / 2);}// Return code for where the mouse is within the slider.// Input mx & my are the current mouse coordinates.int whereInSlider(void *slider, int mx, int my){	int		top, bottom, loc, pos;	int		left, right, e;	VSLIDER	*sp;	sp = (VSLIDER *) slider;	pos = sp->position;	if (sp->type == VSliderType)	{		top = sp->y;		bottom = top + sp->h;		left = sp->x;		right = left + sp->w;		loc = my;		e = mx;	}	else if (sp->type == HSliderType)	{		top = sp->x;		bottom = top + sp->w;		left = sp->y;		right = left + sp->h;		loc = mx;		e = my;	}	else		return MOUSE_NOT_IN_SLIDER;	pos += (top + SLIDERBUTTONSIZE);	if (loc < top || loc > bottom)		return MOUSE_NOT_IN_SLIDER;	if (loc < top + SLIDERBUTTONSIZE)	{		if (e >= left && e <= right)			return MOUSE_IN_SLIDER_UPBUTTON;		return MOUSE_NOT_IN_SLIDER;	}	if (loc < pos)		return MOUSE_IN_UPPER_SLIDER;	if (loc < pos + sp->size)		return MOUSE_ON_SLIDER;	if (loc < bottom - SLIDERBUTTONSIZE)		return MOUSE_IN_LOWER_SLIDER;	if (e >= left && e <= right)		return MOUSE_IN_SLIDER_DOWNBUTTON;	return MOUSE_NOT_IN_SLIDER;}// Adjust slider position and return TRUE if position change caused a state changebool tweakSlider(VSLIDER *sp, int x, int y, int where){	int	state, pos, len, pc;	bool	change;	change = FALSE;	state = whereInSlider(sp, x, y);	if (state != where)						// if adjustment caused a state change...	{		if (sp->type == VSliderType)		{			pos = y - sp->y;			len = sp->h;		}		else		{			pos = x - sp->x;			len = sp->w;		}		len -= (sp->size + 2 * SLIDERBUTTONSIZE);		pos -= (SLIDERBUTTONSIZE + SLIDERBUTTONSIZE/ 2);		if (pos < 0)			pos = 0;		if (pos > len)			pos = len;		sp->position = pos;		pc = (pos * 100) / len;		sp->pc = pc;		sp->offset = (sp->range * pc) / 100;		change = TRUE;	}	return change;}// Adjust slider position by some amountvoid adjustSlider(void *slider, int amount){	int		pos, pc, loc, count, len;	int		offset;	VSLIDER	*sp;	if (!topWidget)		return;	if ((((WIDGET *) topWidget)->p.type == AlertType) ||			(((WIDGET *) topWidget)->p.type == NoticeType))		return;	sp = (VSLIDER *) slider;	if (sp->type == VSliderType)		len = sp->h;	else if (sp->type == HSliderType)		len = sp->w;	else		return;	len -= (sp->size + 2 * SLIDERBUTTONSIZE);	offset = sp->offset;	count = sp->range;	if (!count)		count = 1;	loc = offset + amount;	if (loc < 0)		loc = 0;	else if (loc > count)		loc = count;	sp->offset = loc;	pc = (loc * 100) / count;	pos = (pc * len) / 100;	sp->position = pos;	sp->pc = pc;}// Update slider parametersvoid updateSlider(SDL_Event *ev, void *slider){	int		x, y, pos, pc, len, size;	VSLIDER	*sp;	int		where;	sp = (VSLIDER *) slider;	size = sp->size;	x = ev->button.x;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -