📄 widgets.c
字号:
void addVSlider(void *w, VSLIDER *vs){ BLIST *list; WINDOW *win = (WINDOW *) w; vs->parent = win; vs->x += win->x; vs->y += win->y; if (win->list == NULL) { list = (BLIST *) malloc(sizeof(BLIST)); win->list = list; } else { list = win->list; while (list->next) list = list->next; list->next = (BLIST *) malloc(sizeof(BLIST)); list = list->next; } list->thislist.button = (BUTTON *) vs; list->next = NULL;}// Add a horizontal slider to a windowvoid addHSlider(void *w, HSLIDER *hs){ BLIST *list; WINDOW *win = (WINDOW *) w; hs->parent = win; hs->x += win->x; hs->y += win->y; if (win->list == NULL) { list = (BLIST *) malloc(sizeof(BLIST)); win->list = list; } else { list = win->list; while (list->next) list = list->next; list->next = (BLIST *) malloc(sizeof(BLIST)); list = list->next; } list->thislist.button = (BUTTON *) hs; list->next = NULL;}// Add a TextBox to a windowvoid addTextBox(void *w, TEXTBOX *tbox){ BLIST *list; WINDOW *win = (WINDOW *) w; tbox->parent = win; if (win->list == NULL) { list = (BLIST *) malloc(sizeof(BLIST)); win->list = list; } else { list = win->list; while (list->next) list = list->next; list->next = (BLIST *) malloc(sizeof(BLIST)); list = list->next; } list->thislist.button = (BUTTON *) tbox; list->next = NULL; if (tbox->w > win->w) win->w = tbox->w; tbox->x += win->x; tbox->y += win->y;}// Add a string to a TextBoxvoid addText(TEXTBOX *tbox, char *str){ TLIST *next, *prev; next = prev = tbox->text; if (!next) // first one in list { next = (TLIST *) malloc(sizeof(TLIST)); next->str = str; next->fg = tbox->fg; next->bg = tbox->bg; next->prev = NULL; next->next = NULL; tbox->text = next; tbox->line = 0; } else { while (next->next) // find last in list { prev = next; next = next->next; } prev = next; next->next = (TLIST *) malloc(sizeof(TLIST)); next = next->next; prev->next = next; next->str = str; next->fg = tbox->fg; next->bg = tbox->bg; next->prev = prev; next->next = NULL; } tbox->lines++;}// Add a string to a TextBox at a specific locationvoid addTextEntry(TEXTBOX *tbox, char *str, int line){ int i = 1; TLIST *next, *prev, *entry; next = tbox->text; prev = NULL; entry = (TLIST *) malloc(sizeof(TLIST)); entry->str = str; while (next && i < line) { prev = next; next = next->next; i++; } if (!prev) // add first line in list { entry->prev = NULL; if (!next) // empty list entry->next = NULL; else // add as first line in list entry->next = next; tbox->text = entry; } else // not first in list { prev->next = entry; entry->prev = prev; if (next) { entry->next = next; next->prev = entry; } else entry->next = NULL; }}// Set the current line of a TextBox to a specific linevoid setTListEntry(TEXTBOX *tbox, int line){ int i; TLIST *entry; entry = tbox->text; if (!entry) return; i = 0; while (entry && i < line) { entry = entry->next; i++; } if (entry) { tbox->line = line; showTextBox(tbox); }}// Increment the TextBox line by some amountvoid incTListEntry(TEXTBOX *tbox, int count){ int line; line = tbox->line + count; if (line < 0) line = 0; else if (line >= tbox->lines) line = tbox->lines - 1; tbox->line = line; showTextBox(tbox);}// Decrement the TextBox line by some amountvoid decTListEntry(TEXTBOX *tbox, int count){ int line; line = tbox->line - count; if (line < 0) line = 0; else if (line >= tbox->lines) line = tbox->lines - 1; tbox->line = line; showTextBox(tbox);}// Remove all text from a TextBoxvoid removeTList(TEXTBOX *tbox){ TLIST *entry, *prev; entry = tbox->text; if (!entry) return; tbox->text = NULL; while (entry) { prev = entry; entry = entry->next; free(prev); } tbox->lines = 0;}// Remove a specific string from a TextBoxvoid removeTListEntry(TEXTBOX *tbox, int line){ int i = 1; TLIST *entry, *prev; entry = tbox->text; if (!entry) return; prev = NULL; while (entry && i < line) { prev = entry; entry = entry->next; i++; } if (prev && entry) prev->next = entry->next; if (entry) { if (entry->next) entry->next->prev = prev; free(entry); tbox->lines--; }}// Swap foreground and background colors of a// string in a text box (highlighting)void toggleTextHighlight(TEXTBOX *tbox, int line){ int i = 1; TLIST *entry; entry = tbox->text; if (!entry) return; while (entry && i < line) { entry = entry->next; i++; } if (entry) { i = entry->fg; entry->fg = entry->bg; entry->bg = i; }}// Retrieve the string from a specific line in a TextBoxchar *getText(TEXTBOX *tbox, int line){ int i = 1; TLIST *entry; entry = tbox->text; if (!entry) return NULL; while (entry && i < line) { entry = entry->next; i++; } if (entry) return entry->str; return NULL;}void addInputBox(void *w, INPUTBOX *ibox){ BLIST *list; WINDOW *win = (WINDOW *) w; ibox->parent = win; if (win->list == NULL) { list = (BLIST *) malloc(sizeof(BLIST)); win->list = list; } else { list = win->list; while (list->next) list = list->next; list->next = (BLIST *) malloc(sizeof(BLIST)); list = list->next; } list->thislist.button = (BUTTON *) ibox; list->next = NULL;}// Add a string to an InputBoxvoid addInput(INPUTBOX *ibox, char *str){ SLIST *next, *prev; next = prev = ibox->text; if (!next) // first one in list { next = (SLIST *) malloc(sizeof(SLIST)); next->str = str; next->prev = NULL; next->next = NULL; ibox->text = next; ibox->line = 0; } else { while (next->next) // find last in list { prev = next; next = next->next; } prev = next; next->next = (SLIST *) malloc(sizeof(SLIST)); next = next->next; prev->next = next; next->str = str; next->prev = prev; next->next = NULL; } ibox->lines++;}// Draw a solid bar of given width and heightvoid makeBar(int x, int y, int w, int h, int color){ int i, j; for (i=0; i<w; i++) { for (j=0; j<h; j++) setPixel(x + i, y + j, color); }}// Display a widget on the screenvoid showWidget(void *w){ WIDGET *x = (WIDGET *) w; addWList(w); switch (x->p.type) { case WindowType: case MenuType: showWindow((WINDOW *) x); break; case VMenuType: showVMenu((VMENU *) x); break; case TextBoxType: showTextBox((TEXTBOX *) x); break; case InputBoxType: showInputBox((INPUTBOX *) x); break; case ButtonType: showButton((BUTTON *) x); break; case CheckBoxType: showCheckBox((CHECKBOX *) x); break; case AlertType: showAlertBox((WINDOW *) x); break; case NoticeType: showNoticeBox((WINDOW *) x); break; case LabelType: showLabel((LABEL *) x); break; case HLabelType: showHLabel((HLABEL *) x); break; case VSliderType: showVSlider((VSLIDER *) x); break; case HSliderType: showHSlider((HSLIDER *) x); break; }}// Display a Windowvoid showWindow(WINDOW *w) // render a window or horizontal menu & associated buttons{ int x, y; BUTTON *btn; BLIST *bl; makeBar(w->x + 1, w->y + 1, w->w - 2, w->h - 2, w->bg); // body of window makeBar(w->x, w->y, w->w, 1, w->hi); // top border makeBar(w->x + 1, w->y + w->h - 1, w->w - 1, 1, w->lo); // bottom border makeBar(w->x, w->y, 1, w->h, w->hi); // left border makeBar(w->x + w->w - 1, w->y + 1, 1, w->h - 1, w->lo); // right border x = w->x + 4; y = w->y + 1; bl = w->list; while (bl) { btn = bl->thislist.button; if (btn) showWidget(btn); bl = bl->next; }}// Display a vertical menuvoid showVMenu(VMENU *w) // render a vertical menu & associated buttons{ int x, y; BUTTON *btn; BLIST *bl; makeBar(w->x + 1, w->y + 1, w->w - 2, w->h - 2, w->bg); // body of window makeBar(w->x, w->y, w->w, 1, w->hi); // top border makeBar(w->x + 1, w->y + w->h, w->w - 1, 1, w->lo); // bottom border makeBar(w->x, w->y, 1, w->h, w->hi); // left border makeBar(w->x + w->w - 1, w->y + 1, 1, w->h - 1, w->lo); // right border x = w->x + 4; y = w->y + 1; bl = w->list; while (bl) { btn = bl->thislist.button; if (btn) showWidget(btn); bl = bl->next; }}//// Display an AlertBox//// An alert box has only three "buttons":// 1) The (possibly multi-line) text,// 2) Cancel button,// 3) OK button.//// The Cancel and OK buttons should be on the last line of the// "window", and should be appropriately left/right centered.//void showAlertBox(WINDOW *win) // render an alertbox & associated buttons{ int i, j, x, y, w, center; char *text; BUTTON *btn = NULL; BUTTON *canbtn = NULL; BUTTON *okbtn = NULL; BLIST *bl; char line[128]; x = win->x + 4; y = win->y; w = win->w; bl = win->list; makeBar(x, y + 1, w, win->h, win->bg); // body of box makeBar(x, y, win->w, 1, win->hi); // top of alert box makeBar(x, y + win->h, win->w, 1, win->lo); // bottom of box makeBar(x, y, 1, win->h, win->hi); // left side makeBar(x + win->w - 1, y, 1, win->h, win->lo); // right side y++; btn = bl->thislist.button; // do the text "button" btn->w = w; text = btn->text; x += 4; i = j = 0; while (text[i]) { if (text[i] == '\n') { line[j] = '\0'; printString(x, y, btn->fg, btn->bg, line); y += FONT_HEIGHT; j = 0; i++; } else line[j++] = text[i++]; } if (j) // output last line of text { line[j] = '\0'; printString(x, y, btn->fg, btn->bg, line); y += FONT_HEIGHT; } else if (text[i - 1] == '\n') y += FONT_HEIGHT; bl = bl->next; canbtn = bl->thislist.button; bl = bl->next; okbtn = bl->thislist.button; center = win->w / 2; // center cancel and ok buttons, if possible i = canbtn->w + okbtn->w + 18; w = i / 2; w = center - w; if (w + i < win->w) x += w; // now do the Cancel button x += 2; y += 4; canbtn->x = x; canbtn->y = y; addWList(canbtn); x += canbtn->w; // now do the OK button x += 8; okbtn->x = x; okbtn->y = y; addWList(okbtn);}//// Display a NoticeBox//// A notice box has only two "buttons":// 1) The (possibly multi-line) text,// 2) Close button,//// The Close button should be on the last line of the// "window", and should be appropriately left/right centered.//void showNoticeBox(WINDOW *win) // render a noticebox & associated buttons{ int i, j, x, y, w, center; char *text; BUTTON *btn = NULL; BUTTON *closebtn = NULL; BLIST *bl; char line[128]; x = win->x + 4; y = win->y; w = win->w; bl = win->list; makeBar(x, y + 1, w, win->h, win->bg); // body of box makeBar(x, y, win->w, 1, win->hi); // top of notice box makeBar(x, y + win->h, win->w, 1, win->lo); // bottom of box makeBar(x, y, 1, win->h, win->hi); // left side makeBar(x + win->w - 1, y, 1, win->h, win->lo); // right side y++; btn = bl->thislist.button; // do the text "button" btn->w = w; text = btn->text; x += 4; i = j = 0; while (text[i]) { if (text[i] == '\n') { line[j] = '\0'; printString(x, y, btn->fg, btn->bg, line); y += FONT_HEIGHT; j = 0; i++; } else line[j++] = text[i++]; } if (j) // output last line of text { line[j] = '\0'; printString(x, y, btn->fg, btn->bg, line); y += FONT_HEIGHT; } else if (text[i - 1] == '\n') y += FONT_HEIGHT; bl = bl->next; closebtn = bl->thislist.button; center = win->w / 2; // center close button, if possible i = closebtn->w + 18; w = i / 2; w = center - w; if (w + i < win->w) x += w; // now do the Close button x += 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -