📄 widgets.c
字号:
menu->x = x; menu->y = y; menu->w = w; menu->h = h; menu->fg = fg; menu->bg = bg; menu->hi = hi; menu->lo = lo; menu->mdfunc = NULL; menu->mufunc = NULL; menu->mmfunc = NULL; menu->kdfunc = NULL; menu->kufunc = NULL; menu->parent = NULL; menu->list = NULL; return menu;}// Allocate a menu structureVMENU *newVMenu(int x, int y, int w, int h, int fg, int bg, int hi, int lo){ VMENU *menu = (VMENU *) malloc(sizeof(VMENU)); menu->type = VMenuType; menu->id = widget_id++; menu->x = x; menu->y = y; menu->w = w; menu->h = h; menu->fg = fg; menu->bg = bg; menu->hi = hi; menu->lo = lo; menu->mdfunc = NULL; menu->mufunc = NULL; menu->mmfunc = NULL; menu->kdfunc = NULL; menu->kufunc = NULL; menu->parent = NULL; menu->list = NULL; return menu;}// Allocate a button structureBUTTON *newButton(int x, int y, int fg, int bg, int hi, int lo, char *text){ BUTTON *button = (BUTTON *) malloc(sizeof(BUTTON)); button->type = ButtonType; button->id = widget_id++; button->x = x; button->y = y; if (text) button->w = strlen(text) * FONT_WIDTH + 8; else button->w = 0; button->h = FONT_HEIGHT + 2; button->fg = fg; button->bg = bg; button->hi = hi; button->lo = lo; button->mdfunc = NULL; button->mufunc = NULL; button->mmfunc = NULL; button->kdfunc = NULL; button->kufunc = NULL; button->parent = NULL; button->border = TRUE; // default - can be changed by addButton() button->text = text; return button;}// Allocate a check box structureCHECKBOX *newCheckBox(int x, int y, int fg, int bg, int hi, int lo, char *text, bool *val){ CHECKBOX *checkbox = (CHECKBOX *) malloc(sizeof(CHECKBOX)); checkbox->type = CheckBoxType; checkbox->id = widget_id++; checkbox->x = x; checkbox->y = y; if (text) checkbox->w = (strlen(text) + 4) * FONT_WIDTH + 8; else checkbox->w = 4 * FONT_WIDTH + 8; checkbox->h = FONT_HEIGHT + 2; checkbox->fg = fg; checkbox->bg = bg; checkbox->hi = hi; checkbox->lo = lo; checkbox->mdfunc = NULL; checkbox->mufunc = NULL; checkbox->mmfunc = NULL; checkbox->kdfunc = NULL; checkbox->kufunc = NULL; checkbox->parent = NULL; checkbox->border = TRUE; // default - can be changed by addCheckBox() checkbox->text = text; checkbox->value = val; return checkbox;}// Allocate a label structureLABEL *newLabel(int x, int y, int fg, int bg, int hi, int lo, char *text){ LABEL *lab = (LABEL *) malloc(sizeof(LABEL)); lab->type = LabelType; lab->id = widget_id++; lab->x = x; lab->y = y; if (text) lab->w = strlen(text) * FONT_WIDTH + 8; else lab->w = 0; lab->h = FONT_HEIGHT + 2; lab->fg = fg; lab->bg = bg; lab->hi = hi; lab->lo = lo; lab->mdfunc = NULL; lab->mufunc = NULL; lab->mmfunc = NULL; lab->kdfunc = NULL; lab->kufunc = NULL; lab->parent = NULL; lab->border = FALSE; // default - can be changed by addLabel() lab->text = text; return lab;}// Allocate a highlighted label structureHLABEL *newHLabel(int x, int y, int fg, int bg, int hi, int lo, char *text){ HLABEL *lab = (HLABEL *) malloc(sizeof(HLABEL)); lab->type = HLabelType; lab->id = widget_id++; lab->x = x; lab->y = y; if (text) lab->w = strlen(text) * FONT_WIDTH + 8; else lab->w = 0; lab->h = FONT_HEIGHT + 2; lab->fg = fg; lab->bg = bg; lab->hi = hi; lab->lo = lo; lab->mdfunc = NULL; lab->mufunc = NULL; lab->mmfunc = NULL; lab->kdfunc = NULL; lab->kufunc = NULL; lab->parent = NULL; lab->border = FALSE; // default - can be changed by addLabel() lab->text = text; lab->hlflag = FALSE; // highlight must be enabled by user return lab;}// Allocate an Alert box structureALERT *newAlertBox(int x, int y, char *text){ int i, w, h, len; ALERT *al; BUTTON *canbtn, *okbtn; i = w = len = 0; h = 1; // at least one line required for the cancel/ok buttons if (text) { if (text[i]) h++; while (text[i]) { if (text[i] == '\n') { if (len > w) w = len; h++; len = 0; } else len++; i++; } } if (len > w) // last line might be the longest w = len; if (w < 18) // need minimum width for cancel and ok buttons w = 18; al = (ALERT *) malloc(sizeof(ALERT)); al->type = AlertType; al->id = widget_id++; al->x = x; al->y = y; al->w = w * FONT_WIDTH + 8; al->h = h * FONT_HEIGHT + 10; al->fg = black; al->bg = lred; al->hi = pink; al->lo = red; al->mdfunc = NULL; al->mufunc = NULL; al->mmfunc = NULL; al->kdfunc = NULL; al->kufunc = NULL; al->parent = NULL; al->list = NULL; al->response = ALERT_NO_RESPONSE; canbtn = newButton(0, 0, black, lgray, white, black, " Cancel "); okbtn = newButton(0, 0, black, lgray, white, black, " OK "); addButton((WINDOW *) al, newButton(0, 0, black, lred, pink, red, text), FALSE); addButton((WINDOW *) al, canbtn, TRUE); addButton((WINDOW *) al, okbtn, TRUE); attachCallBack(canbtn, SDL_MOUSEBUTTONDOWN, processAlertCallBack); attachCallBack(okbtn, SDL_MOUSEBUTTONDOWN, processAlertCallBack); return al;}// Allocate a Notice box structureNOTICE *newNoticeBox(int x, int y, char *text){ int i, w, h, len; NOTICE *note; BUTTON *closebtn; i = w = len = 0; h = 1; // at least one line required for the close button if (text) { if (text[i]) h++; while (text[i]) { if (text[i] == '\n') { if (len > w) w = len; h++; len = 0; } else len++; i++; } } if (len > w) // last line might be the longest w = len; if (w < 18) // need minimum width for close button w = 18; note = (NOTICE *) malloc(sizeof(NOTICE)); note->type = NoticeType; note->id = widget_id++; note->x = x; note->y = y; note->w = w * FONT_WIDTH + 8; note->h = h * FONT_HEIGHT + 10; note->fg = black; note->bg = green; note->hi = lgreen; note->lo = dgreen; note->mdfunc = NULL; note->mufunc = NULL; note->mmfunc = NULL; note->kdfunc = NULL; note->kufunc = NULL; note->parent = NULL; note->list = NULL; closebtn = newButton(0, 0, black, lgray, white, black, " Close "); addButton((WINDOW *) note, newButton(0, 0, black, green, lgreen, dgreen, text), FALSE); addButton((WINDOW *) note, closebtn, TRUE); attachCallBack(closebtn, SDL_MOUSEBUTTONDOWN, processNoticeCallBack); return note;}// Allocate a Text box structureTEXTBOX *newTextBox(int x, int y, int w, int h, int fg, int bg, int hi, int lo){ TEXTBOX *tbox = (TEXTBOX *) malloc(sizeof(TEXTBOX)); tbox->type = TextBoxType; tbox->id = widget_id++; tbox->x = x; tbox->y = y; tbox->w = w; tbox->h = h; tbox->fg = fg; tbox->bg = bg; tbox->hi = hi; tbox->lo = lo; tbox->mdfunc = NULL; tbox->mufunc = NULL; tbox->mmfunc = NULL; tbox->kdfunc = NULL; tbox->kufunc = NULL; tbox->parent = NULL; tbox->list = NULL; tbox->text = NULL; tbox->lines = 0; tbox->vlines = h / FONT_HEIGHT; tbox->line = 0; tbox->xoffset = 0; return tbox;}// Allocate an Input box structureINPUTBOX *newInputBox(int x, int y, int w, int h, int fg, int bg, int hi, int lo){ INPUTBOX *ibox = (INPUTBOX *) malloc(sizeof(INPUTBOX)); ibox->type = InputBoxType; ibox->id = widget_id++; ibox->x = x; ibox->y = y; ibox->w = w; ibox->h = h; ibox->fg = fg; ibox->bg = bg; ibox->hi = hi; ibox->lo = lo; ibox->mdfunc = NULL; ibox->mufunc = NULL; ibox->mmfunc = NULL; ibox->kdfunc = NULL; ibox->kufunc = NULL; ibox->parent = NULL; ibox->list = NULL; ibox->text = NULL; ibox->lines = 0; ibox->vlines = h / FONT_HEIGHT; ibox->line = 0; ibox->cursorx = 0; ibox->cursory = 0; ibox->cursorstate = TRUE; ibox->mode = IBOX_INSERT_MODE; return ibox;}// Allocate a Vertical Slider structureVSLIDER *newVSlider(int x, int y, int w, int h, int fg, int bg, int hi, int lo, int range, int change){ VSLIDER *vs = (VSLIDER *) malloc(sizeof(VSLIDER)); vs->type = VSliderType; vs->id = widget_id++; vs->x = x; vs->y = y; if (w < SLIDERSIZE + 4) vs->w = SLIDERSIZE + 4; else vs->w = w; if (h < SLIDERSIZE + 4) vs->h = SLIDERSIZE + 4; else vs->h = h; vs->size = vs->w - 4; vs->fg = fg; vs->bg = bg; vs->hi = hi; vs->lo = lo; vs->mdfunc = NULL; vs->mufunc = NULL; vs->mmfunc = NULL; vs->kdfunc = NULL; vs->kufunc = NULL; vs->parent = NULL; vs->list = NULL; vs->position = 0; vs->pc = 0; if (range) { vs->range = range; vs->change = change; } else { vs->range = 100; vs->change = 10; } vs->offset = 0; vs->drag = FALSE; vs->state = MOUSE_NOT_IN_SLIDER; vs->adjfunc = NULL; return vs;}// Allocate a Horizontal Slider structureHSLIDER *newHSlider(int x, int y, int w, int h, int fg, int bg, int hi, int lo, int range, int change){ HSLIDER *hs = (HSLIDER *) malloc(sizeof(HSLIDER)); hs->type = HSliderType; hs->id = widget_id++; hs->x = x; hs->y = y; if (w < SLIDERSIZE + 4) hs->w = SLIDERSIZE + 4; else hs->w = w; if (h < SLIDERSIZE + 4) hs->h = SLIDERSIZE + 4; else hs->h = h; hs->size = hs->h - 4; hs->fg = fg; hs->bg = bg; hs->hi = hi; hs->lo = lo; hs->mdfunc = NULL; hs->mufunc = NULL; hs->mmfunc = NULL; hs->kdfunc = NULL; hs->kufunc = NULL; hs->parent = NULL; hs->list = NULL; hs->position = 0; hs->pc = 0; if (range) { hs->range = range; hs->change = change; } else { hs->range = 100; hs->change = 10; } hs->offset = 0; hs->drag = FALSE; hs->state = MOUSE_NOT_IN_SLIDER; hs->adjfunc = NULL; return hs;}// Find the maximum width of all buttons in this windowint maxWidth(WINDOW *win){ int w; BLIST *list; BUTTON *btn; w = win->w; list = win->list; while (list) { btn = list->thislist.button; if (btn && btn->w > w) w = btn->w; list = list->next; } return w;}// Add a button to a windowvoid addButton(void *p, BUTTON *button, bool border){ int x, y, h; BLIST *list; BUTTON *b; WINDOW *win = (WINDOW *) p; h = 0; button->border = border; button->parent = win; x = win->x; y = win->y; if (win->list == NULL) { list = (BLIST *) malloc(sizeof(BLIST)); win->list = list; } else { list = win->list; b = list->thislist.button; x += b->w; y += FONT_HEIGHT + 2; while (list->next) { list = list->next; b = list->thislist.button; x += b->w; y += FONT_HEIGHT + 2; } list->next = (BLIST *) malloc(sizeof(BLIST)); list = list->next; } list->thislist.button = button; list->next = NULL; if (win->type == VMenuType) { if (button->w > win->w) win->w = button->w; button->x = win->x; button->y += y; h = button->h; } else { if (!button->x) button->x += x; else button->x += win->x; button->y += win->y; } if (!button->fg && !button->bg) // if button fg & bg both black, use parent window colors { button->fg = win->fg; button->bg = win->bg; button->hi = win->hi; button->lo = win->lo; } if (win->type == VMenuType) { win->w = maxWidth(win); list = win->list; while (list) { b = list->thislist.button; b->w = win->w; list = list->next; } } win->h += h;}// Add a Check box to a windowvoid addCheckBox(void *p, CHECKBOX *checkbox, bool border) // add checkbox to a window{ int x; CLIST *list; CHECKBOX *c; WINDOW *win = (WINDOW *) p; checkbox->border = border; checkbox->parent = win; x = win->x; if (win->list == NULL) { list = (CLIST *) malloc(sizeof(CLIST)); win->list = list; } else { list = win->list; c = list->thislist.checkbox; x += c->w; while (list->next) { list = list->next; c = list->thislist.checkbox; x += c->w; } list->next = (CLIST *) malloc(sizeof(CLIST)); list = list->next; } list->thislist.checkbox = checkbox; list->next = NULL; if (!checkbox->x) checkbox->x += x; else checkbox->x += win->x; checkbox->y += win->y; if (!checkbox->fg && !checkbox->bg) // if checkbox fg & bg both black, use parent window colors { checkbox->fg = win->fg; checkbox->bg = win->bg; checkbox->hi = win->hi; checkbox->lo = win->lo; } if (win->type == VMenuType) { win->w = maxWidth(win); list = win->list; while (list) { c = list->thislist.checkbox; c->w = win->w; list = list->next; } }}// Add a Label to a widgetvoid addLabel(void *p, LABEL *label, bool border) // add label to a window{ int x; BLIST *list; LABEL *lab; WINDOW *win = (WINDOW *) p; label->border = border; label->parent = win; x = win->x; if (win->list == NULL) { list = (BLIST *) malloc(sizeof(BLIST)); win->list = list; } else { list = win->list; lab = list->thislist.label; x += lab->w; while (list->next) { list = list->next; lab = list->thislist.label; x += lab->w; } list->next = (BLIST *) malloc(sizeof(BLIST)); list = list->next; } list->thislist.label = label; list->next = NULL; if (!label->x) label->x += x; else label->x += win->x; label->y += win->y; if (!label->fg && !label->bg) // if label fg & bg both black, use parent window colors { label->fg = win->fg; label->bg = win->bg; label->hi = win->hi; label->lo = win->lo; } if (win->type == VMenuType || win->type == InputBoxType) { win->w = maxWidth(win); list = win->list; while (list) { lab = list->thislist.label; lab->w = win->w; list = list->next; } }}// Add a vertical slider to a window
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -