/* * Print list item */ static void print_item(WINDOW *win, char **items, int status, int choice, int selected) { int i; chtype attr = A_NORMAL; const int *indx; int limit; /* Clear 'residue' of last item */ wattrset(win, menubox_attr); (void) wmove(win, choice, 0); for (i = 0; i < list_width; i++) (void) waddch(win, ' '); (void) wmove(win, choice, check_x); wattrset(win, selected ? check_selected_attr : check_attr); (void) wprintw(win, (checkflag == FLAG_CHECK) ? "[%c]" : "(%c)", status ? 'X' : ' '); wattrset(win, menubox_attr); (void) waddch(win, ' '); if (strlen(ItemName(0)) != 0) { indx = dlg_index_wchars(ItemName(0)); limit = dlg_count_wchars(ItemName(0)); wattrset(win, selected ? tag_key_selected_attr : tag_key_attr); (void) waddnstr(win, ItemName(0), indx[1]); if ((int) strlen(ItemName(0)) > indx[1]) { limit = dlg_limit_columns(ItemName(0), (item_x - check_x - 6), 1); if (limit > 1) { wattrset(win, selected ? tag_selected_attr : tag_attr); (void) waddnstr(win, ItemName(0) + indx[1], indx[limit] - indx[1]); } } } if (strlen(ItemText(0)) != 0) { indx = dlg_index_wchars(ItemText(0)); limit = dlg_limit_columns(ItemText(0), (getmaxx(win) - item_x - 1), 0); if (limit > 0) { (void) wmove(win, choice, item_x); wattrset(win, selected ? item_selected_attr : item_attr); dlg_print_text(win, ItemText(0), indx[limit], &attr); } } if (selected) { dlg_item_help(ItemHelp(0)); } }
TPtrC CEikListBoxTextEditor::EditableItemText(TRect* aRect) { _AKNTRACE_FUNC_ENTER; TPtrC itemtext = ItemText(); if (iItemPos==0) // not yet set (and even if set, cannot be zero) { iItemPos = itemtext.Locate('\n'); // loacte partly editable item start if (iItemPos != KErrNotFound) { iItemPos++; // jump over mark character iItemLen = itemtext.Mid( iItemPos ).Locate('\n'); // locate string end if ( iItemLen == KErrNotFound ) Panic( ETUiklbedPanicEndMarkMissing ); if ( iItemLen==0 ) Panic( ETUiklbedPanicEmptyField ); TPtrC head = itemtext.Left( iItemPos ); TPtrC body = itemtext.Mid( iItemPos, iItemLen ); if ( aRect ) // adjust the rect if it is given { aRect->iTl.iX += iFont->TextWidthInPixels( head ); aRect->iBr.iX = aRect->iTl.iX + iFont->TextWidthInPixels( body ) + 4; } _AKNTRACE_FUNC_EXIT; return body; } iItemPos = 0; // partly editable text not found } _AKNTRACE_FUNC_EXIT; return itemtext; }
/* * Print menu item */ static void print_item(WINDOW *win, const char **items, int choice, int selected) { int i; /* Clear 'residue' of last item */ wattrset(win, menubox_attr); (void) wmove(win, choice, 0); for (i = 0; i < menu_width; i++) (void) waddch(win, ' '); (void) wmove(win, choice, tag_x); wattrset(win, selected ? tag_key_selected_attr : tag_key_attr); (void) waddch(win, CharOf(ItemName(0)[0])); wattrset(win, selected ? tag_selected_attr : tag_attr); (void) wprintw(win, "%.*s", item_x - tag_x - 2, ItemName(0) + 1); (void) wmove(win, choice, item_x); wattrset(win, selected ? item_selected_attr : item_attr); (void) wprintw(win, "%.*s", getmaxx(win) - item_x, ItemText(0)); if (selected) { dlg_item_help(ItemHelp(0)); } }
String FontComboBox::CurrentFontFace() const { int index = CurrentItem(); if ( index < 0 ) return String(); String face = ItemText( index ); face.Trim(); return face; }
void FontComboBox::__ItemHighlighted( ComboBox& sender, int index ) { if ( onFontHighlighted != 0 ) { String face = (index < 0) ? String() : ItemText( index ); face.Trim(); (onFontHighlightedReceiver->*onFontHighlighted)( *this, face ); } }
EXPORT_C TBool CEikListBoxTextEditor::UpdateModelL() // virtual - needs to be rewritten if editing other than single column text list { _AKNTRACE_FUNC_ENTER; if (!Editor()) { _AKNTRACE_FUNC_EXIT; return EFalse; // quit if editing is not currently on } const MDesCArray* matchableTextArray=ListBoxModel()->MatchableTextArray(); CDesCArray* textArray=(CDesCArray*)matchableTextArray; TPtrC itemtext = ItemText(); if ( iItemPos ) // partly editable item? { HBufC* itemBuffer= HBufC::New(itemtext.Length()); CleanupStack::PushL( itemBuffer ); TPtr itemPointer = itemBuffer->Des(); itemPointer.Append( itemtext.Left( iItemPos ) ); HBufC* ptr=iEditor->GetTextInHBufL(); TPtrC newText; if (ptr) { newText.Set(ptr->Des()); } TInt addSpaces = iItemLen - newText.Length(); for (TInt index=0; ((addSpaces>0) && (index<addSpaces)); index++) itemPointer.Append(_L(" ")); itemPointer.Append( newText ); itemPointer.Append( itemtext.Right( itemtext.Length()-iItemPos-iItemLen ) ); delete ptr; textArray->InsertL( ItemIndex(), *itemBuffer ); CleanupStack::PopAndDestroy(); // itemBuffer textArray->Delete( ItemIndex()+1 ); } else // replace the whole list item { HBufC* newText = iEditor->GetTextInHBufL(); if (!newText) return ETrue; // if user tries to insert an empty text... CleanupStack::PushL(newText); textArray->InsertL(ItemIndex(),*newText); CleanupStack::PopAndDestroy(); // newText textArray->Delete( ItemIndex() + 1 ); } _AKNTRACE_FUNC_EXIT; return ETrue; }
/* * Print menu item */ static void print_item(WINDOW *win, char **items, int choice, int selected) { int n; int my_width = menu_width; int my_x = item_x; int my_y = ItemToRow(choice); chtype attr = A_NORMAL; if (items == 0) return; /* Clear 'residue' of last item and mark current current item */ if (dialog_vars.input_menu) { wattrset(win, selected ? item_selected_attr : item_attr); for (n = my_y - 1; n < my_y + INPUT_ROWS - 1; n++) { wmove(win, n, 0); wprintw(win, "%*s", my_width, " "); } } else { wattrset(win, menubox_attr); wmove(win, my_y, 0); wprintw(win, "%*s", my_width, " "); } print_tag(win, items, choice, selected); /* Draw the input field box (only for inputmenu) */ (void) wmove(win, my_y, my_x); if (dialog_vars.input_menu) { my_width -= 1; draw_box(win, my_y - 1, my_x, INPUT_ROWS, my_width - my_x - tag_x, selected ? item_selected_attr : item_attr, selected ? item_selected_attr : item_attr); my_width -= 1; ++my_x; } /* print actual item */ wmove(win, my_y, my_x); wattrset(win, selected ? item_selected_attr : item_attr); dlg_print_text(win, ItemText(0), my_width - my_x, &attr); if (selected) { dlg_item_help(ItemHelp(0)); } }
static char * input_menu_edit(WINDOW *win, char **items, int choice) { char *result; int offset = 0; int key = 0, fkey; int first = TRUE; /* see above */ int y = ItemToRow(choice); result = malloc(dialog_vars.max_input); assert_ptr(result, "input_menu_edit"); dialog_vars.max_input = dialog_vars.max_input; /* original item is used to initialize the input string. */ result[0] = '\0'; strcpy(result, ItemText(0)); print_tag(win, items, choice, TRUE); /* taken out of inputbox.c - but somewhat modified */ while (key != '\n' && key != '\r') { if (!first) key = mouse_wgetch(win, &fkey); if (dlg_edit_string(result, &offset, key, fkey, first)) { /* * menu_width - 2 ..... it's the actual number of maximal * possible characters could be written * to the screen. * * item_x - tag_x - 2 . same as "name_width" * ( see in dialog_menu() ) */ dlg_show_string(win, result, offset, item_selected_attr, y, item_x + 1, menu_width - item_x - 3, FALSE, first); first = FALSE; } } return result; }
int FindIndex(SOCKET Socket) { int ItemIndex = SendMessage(lsClients, LB_GETCOUNT, 0, 0) - 1; if (ItemIndex < 0) return 0; // Loop through the list in reverse until a match is found while (ItemIndex >= 0) { int Length = (int)SendMessage(lsClients, LB_GETTEXTLEN, (WPARAM)ItemIndex, 0); if (Length > 0) { // Get text of item std::vector<WCHAR> Buffer(Length + 1); SendMessage(lsClients, LB_GETTEXT, (WPARAM)ItemIndex, (LPARAM)&Buffer[0]); std::wstring ItemText(&Buffer[0]); // Add space to avoid errors ItemText += L" "; // Separate first word int Space = ItemText.find(L" "); std::string StrSocket(ItemText.begin(), ItemText.begin() + Space); // Convert word and SOCKET to integars to compare int iSocket = atoi(StrSocket.c_str()); if (iSocket == (int)Socket) return ItemIndex; } --ItemIndex; } return 0; };
void DisplayFile(std::string Filename, const int Position, const int Size) { int ItemIndex = SendMessage(lsFiles, LB_GETCOUNT, 0, 0) -1; if (ItemIndex >= 0) { std::wstring wsFilename(Filename.begin(), Filename.end()); // Loop through the list in reverse until a match is found while (ItemIndex >= 0) { int Length = (int)SendMessage(lsFiles, LB_GETTEXTLEN, (WPARAM)ItemIndex, 0); if (Length > 0) { // Get text of item std::vector<WCHAR> Buffer(Length + 1); SendMessage(lsFiles, LB_GETTEXT, (WPARAM)ItemIndex, (LPARAM)&Buffer[0]); std::wstring ItemText(&Buffer[0]); // If matched filename, then update file completion if (ItemText.substr(0, wsFilename.size()) == wsFilename) { std::wstringstream Text; Text << wsFilename << L" - (" << Position << " / " << Size << L") [" << (int)(Position * 100 / Size) << "% Complete]"; SendMessage(lsFiles, LB_DELETESTRING, (WPARAM)ItemIndex, 0); SendMessage(lsFiles, LB_INSERTSTRING, (WPARAM)ItemIndex, (LPARAM)Text.str().c_str()); break; } } --ItemIndex; } } };
/* * Display a dialog box with a list of options that can be turned on or off * The `flag' parameter is used to select between radiolist and checklist. */ int dialog_checklist(const char *title, const char *cprompt, int height, int width, int list_height, int item_no, char **items, int flag, int separate_output) { int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y; int key = 0, fkey; int button = 0; int choice = 0; int scrollamt = 0; int max_choice, *status; int use_width, name_width, text_width; int result = DLG_EXIT_UNKNOWN; WINDOW *dialog, *list; char *prompt = strclone(cprompt); const char **buttons = dlg_ok_labels(); tab_correct_str(prompt); if (list_height == 0) { use_width = calc_listw(item_no, items, CHECKBOX_TAGS) + 10; /* calculate height without items (4) */ auto_size(title, prompt, &height, &width, 4, MAX(26, use_width)); calc_listh(&height, &list_height, item_no); } else { auto_size(title, prompt, &height, &width, 4 + list_height, 26); } print_size(height, width); ctl_size(height, width); checkflag = flag; /* Allocate space for storing item on/off status */ status = malloc(sizeof(int) * item_no); assert_ptr(status, "dialog_checklist"); /* Initializes status */ for (i = 0; i < item_no; i++) status[i] = !dlg_strcmp(ItemStatus(i), "on"); max_choice = MIN(list_height, item_no); x = box_x_ordinate(width); y = box_y_ordinate(height); dialog = new_window(height, width, y, x); mouse_setbase(x, y); draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); draw_bottom_box(dialog); draw_title(dialog, title); wattrset(dialog, dialog_attr); print_autowrap(dialog, prompt, height, width); list_width = width - 6; getyx(dialog, cur_y, cur_x); box_y = cur_y + 1; box_x = (width - list_width) / 2 - 1; /* create new window for the list */ list = sub_window(dialog, list_height, list_width, y + box_y + 1, x + box_x + 1); /* draw a box around the list items */ draw_box(dialog, box_y, box_x, list_height + 2 * MARGIN, list_width + 2 * MARGIN, menubox_border_attr, menubox_attr); text_width = 0; name_width = 0; /* Find length of longest item to center checklist */ for (i = 0; i < item_no; i++) { text_width = MAX(text_width, (int) strlen(ItemText(i))); name_width = MAX(name_width, (int) strlen(ItemName(i))); } /* If the name+text is wider than the list is allowed, then truncate * one or both of them. If the name is no wider than 1/4 of the list, * leave it intact. */ use_width = (list_width - 6); if (text_width + name_width > use_width) { int need = 0.25 * use_width; if (name_width > need) { int want = use_width * ((double) name_width) / (text_width + name_width); name_width = (want > need) ? want : need; } text_width = use_width - name_width; } check_x = (use_width - (text_width + name_width)) / 2; item_x = name_width + check_x + 6; /* Print the list */ for (i = 0; i < max_choice; i++) print_item(list, ItemData(i), status[i], i, i == choice); (void) wnoutrefresh(list); /* register the new window, along with its borders */ mouse_mkbigregion(box_y + 1, box_x, list_height, list_width + 2, KEY_MAX, 1, 1, 1 /* by lines */ ); dlg_draw_arrows(dialog, scrollamt, scrollamt + max_choice < item_no - 1, box_x + check_x + 5, box_y, box_y + list_height + 1); dlg_draw_buttons(dialog, height - 2, 0, buttons, 0, FALSE, width); wtimeout(dialog, WTIMEOUT_VAL); while (result == DLG_EXIT_UNKNOWN) { key = mouse_wgetch(dialog, &fkey); if (fkey && (key >= (M_EVENT + KEY_MAX))) { getyx(dialog, cur_y, cur_x); /* De-highlight current item */ print_item(list, ItemData(scrollamt + choice), status[scrollamt + choice], choice, FALSE); /* Highlight new item */ choice = (key - (M_EVENT + KEY_MAX)); print_item(list, ItemData(scrollamt + choice), status[scrollamt + choice], choice, TRUE); (void) wnoutrefresh(list); (void) wmove(dialog, cur_y, cur_x); key = ' '; /* force the selected item to toggle */ fkey = FALSE; } /* * A space toggles the item status. We handle either a checklist * (any number of items can be selected) or radio list (zero or one * items can be selected). */ if (key == ' ') { getyx(dialog, cur_y, cur_x); if (flag == FLAG_CHECK) { /* checklist? */ status[scrollamt + choice] = !status[scrollamt + choice]; print_item(list, ItemData(scrollamt + choice), status[scrollamt + choice], choice, TRUE); } else { /* radiolist */ if (!status[scrollamt + choice]) { for (i = 0; i < item_no; i++) status[i] = FALSE; status[scrollamt + choice] = TRUE; for (i = 0; i < max_choice; i++) print_item(list, ItemData(scrollamt + i), status[scrollamt + i], i, i == choice); } } (void) wnoutrefresh(list); (void) wmove(dialog, cur_y, cur_x); wrefresh(dialog); continue; /* wait for another key press */ } else if (key == ESC) { result = DLG_EXIT_ESC; continue; } if (!fkey) { fkey = TRUE; switch (key) { case '\n': case '\r': key = KEY_ENTER; break; case '-': key = KEY_UP; break; case '+': key = KEY_DOWN; break; case TAB: key = KEY_RIGHT; break; default: fkey = FALSE; break; } } /* * Check if key pressed matches first character of any item tag in * list. If there is more than one match, we will cycle through * each one as the same key is pressed repeatedly. */ found = FALSE; if (!fkey) { for (j = scrollamt + choice + 1; j < item_no; j++) { if (dlg_match_char(dlg_last_getc(), ItemName(j))) { found = TRUE; i = j - scrollamt; break; } } if (!found) { for (j = 0; j <= scrollamt + choice; j++) { if (dlg_match_char(dlg_last_getc(), ItemName(j))) { found = TRUE; i = j - scrollamt; break; } } } if (found) dlg_flush_getc(); } /* * A single digit (1-9) positions the selection to that line in the * current screen. */ if (!found && (key <= '9') && (key > '0') && (key - '1' < max_choice)) { found = TRUE; i = key - '1'; } if (!found) { if (fkey) { found = TRUE; switch (key) { case KEY_HOME: i = -scrollamt; break; case KEY_LL: case KEY_END: i = item_no - 1 - scrollamt; break; case M_EVENT + KEY_PPAGE: case KEY_PPAGE: if (choice) i = 0; else if (scrollamt != 0) i = -MIN(scrollamt, max_choice); else continue; break; case M_EVENT + KEY_NPAGE: case KEY_NPAGE: i = MIN(choice + max_choice, item_no - scrollamt - 1); break; case KEY_UP: i = choice - 1; if (choice == 0 && scrollamt == 0) continue; break; case KEY_DOWN: i = choice + 1; if (scrollamt + choice >= item_no - 1) continue; break; default: found = FALSE; break; } } } if (found) { if (i != choice) { getyx(dialog, cur_y, cur_x); if (i < 0 || i >= max_choice) { #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5 /* * Using wscrl to assist ncurses scrolling is not needed * in version 5.x */ if (i == -1) { if (list_height > 1) { /* De-highlight current first item */ print_item(list, ItemData(scrollamt), status[scrollamt], 0, FALSE); scrollok(list, TRUE); wscrl(list, -1); scrollok(list, FALSE); } scrollamt--; print_item(list, ItemData(scrollamt), status[scrollamt], 0, TRUE); } else if (i == max_choice) { if (list_height > 1) { /* De-highlight current last item before scrolling up */ print_item(list, ItemData(scrollamt + max_choice - 1), status[scrollamt + max_choice - 1], max_choice - 1, FALSE); scrollok(list, TRUE); wscrl(list, 1); scrollok(list, FALSE); } scrollamt++; print_item(list, ItemData(scrollamt + max_choice - 1), status[scrollamt + max_choice - 1], max_choice - 1, TRUE); } else #endif { if (i < 0) { scrollamt += i; choice = 0; } else { choice = max_choice - 1; scrollamt += (i - max_choice + 1); } for (i = 0; i < max_choice; i++) { print_item(list, ItemData(scrollamt + i), status[scrollamt + i], i, i == choice); } } (void) wnoutrefresh(list); dlg_draw_arrows(dialog, scrollamt, scrollamt + choice < item_no - 1, box_x + check_x + 5, box_y, box_y + list_height + 1); } else { /* De-highlight current item */ print_item(list, ItemData(scrollamt + choice), status[scrollamt + choice], choice, FALSE); /* Highlight new item */ choice = i; print_item(list, ItemData(scrollamt + choice), status[scrollamt + choice], choice, TRUE); (void) wnoutrefresh(list); (void) wmove(dialog, cur_y, cur_x); wrefresh(dialog); } } continue; /* wait for another key press */ } if (fkey) { switch (key) { case KEY_ENTER: result = dlg_ok_buttoncode(button); break; case KEY_BTAB: case KEY_LEFT: button = dlg_prev_button(buttons, button); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); break; case KEY_RIGHT: button = dlg_next_button(buttons, button); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); break; default: if (key >= M_EVENT) { if ((key2 = dlg_ok_buttoncode(key - M_EVENT)) >= 0) { result = key2; break; } beep(); } } } else { beep(); } } del_window(dialog); switch (result) { case DLG_EXIT_OK: /* FALLTHRU */ case DLG_EXIT_EXTRA: for (i = 0; i < item_no; i++) { if (status[i]) { if (flag == FLAG_CHECK) { if (separate_output) { dlg_add_result(ItemName(i)); dlg_add_result("\n"); } else { dlg_add_result("\""); dlg_add_result(ItemName(i)); dlg_add_result("\" "); } } else { dlg_add_result(ItemName(i)); } } } break; case DLG_EXIT_HELP: dlg_add_result("HELP "); if (USE_ITEM_HELP(ItemHelp(scrollamt + choice))) { dlg_add_result(ItemHelp(scrollamt + choice)); result = DLG_EXIT_OK; /* this is inconsistent */ } else { dlg_add_result(ItemName(scrollamt + choice)); } break; } mouse_free_regions(); free(status); free(prompt); return result; }
/* * Display a menu for choosing among a number of options */ int dialog_menu(const char *title, const char *cprompt, int height, int width, int menu_height, int item_no, const char **items) { int i, j, x, y, cur_x, cur_y, box_x, box_y; int key = 0; int button = 0; int choice = dlg_default_item(items, MENUBOX_TAGS); int scrollamt = 0; int max_choice, min_width; int found; int use_width, name_width, text_width; WINDOW *dialog, *menu; char *prompt = strclone(cprompt); const char **buttons = dlg_ok_labels(); tab_correct_str(prompt); if (menu_height == 0) { min_width = calc_listw(item_no, items, MENUBOX_TAGS) + 10; /* calculate height without items (4) */ auto_size(title, prompt, &height, &width, 4, MAX(26, min_width)); calc_listh(&height, &menu_height, item_no); } else { auto_size(title, prompt, &height, &width, 4 + menu_height, 26); } print_size(height, width); ctl_size(height, width); max_choice = MIN(menu_height, item_no); x = box_x_ordinate(width); y = box_y_ordinate(height); dialog = new_window(height, width, y, x); mouse_setbase(x, y); draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); draw_bottom_box(dialog); draw_title(dialog, title); wattrset(dialog, dialog_attr); print_autowrap(dialog, prompt, height, width); menu_width = width - 6; getyx(dialog, cur_y, cur_x); box_y = cur_y + 1; box_x = (width - menu_width) / 2 - 1; /* create new window for the menu */ menu = sub_window(dialog, menu_height, menu_width, y + box_y + 1, x + box_x + 1); /* draw a box around the menu items */ draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2, menubox_border_attr, menubox_attr); name_width = 0; text_width = 0; /* Find length of longest item to center menu */ for (i = 0; i < item_no; i++) { name_width = MAX(name_width, (int) strlen(ItemName(i))); text_width = MAX(text_width, (int) strlen(ItemText(i))); } /* If the name+text is wider than the list is allowed, then truncate * one or both of them. If the name is no wider than 1/4 of the list, * leave it intact. */ use_width = (menu_width - 2); if (text_width + name_width > use_width) { int need = 0.25 * use_width; if (name_width > need) { int want = use_width * ((double) name_width) / (text_width + name_width); name_width = (want > need) ? want : need; } text_width = use_width - name_width; } tag_x = (use_width - text_width - name_width) / 2; item_x = name_width + tag_x + 2; if (choice - scrollamt >= max_choice) { scrollamt = choice - (max_choice - 1); choice = max_choice - 1; } /* Print the menu */ for (i = 0; i < max_choice; i++) print_item(menu, ItemData(i + scrollamt), i, i == choice); (void) wnoutrefresh(menu); /* register the new window, along with its borders */ mouse_mkbigregion(box_y, box_x, menu_height + 2, menu_width + 2, item_no, item_x, /* the threshold */ 1 /* dirty mode */ ); dlg_draw_arrows(dialog, scrollamt, scrollamt + max_choice < item_no, box_x + tag_x + 1, box_y, box_y + menu_height + 1); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); wtimeout(dialog, WTIMEOUT_VAL); while (key != ESC) { key = mouse_wgetch(dialog); /* Check if key pressed matches first character of any item tag in menu */ for (i = 0; i < max_choice; i++) if (toupper(key) == toupper(UCH(ItemName(scrollamt + i)[0]))) break; /* * Check if key pressed matches first character of any item tag in * list. If there is more than one match, we will cycle through * each one as the same key is pressed repeatedly. */ found = FALSE; for (j = scrollamt + choice + 1; j < item_no; j++) { if (toupper(key) == toupper(UCH(ItemName(j)[0]))) { found = TRUE; i = j - scrollamt; break; } } if (!found) { for (j = 0; j <= scrollamt + choice; j++) { if (toupper(key) == toupper(UCH(ItemName(j)[0]))) { found = TRUE; i = j - scrollamt; break; } } } /* * A single digit (1-9) positions the selection to that line in the * current screen. */ if (!found && (key <= '9') && (key > '0') && (key - '1' <= max_choice)) { found = TRUE; i = key - '1'; } if (!found) { found = TRUE; switch (key) { case KEY_HOME: i = -scrollamt; break; case KEY_LL: case KEY_END: i = item_no - 1 - scrollamt; break; case KEY_PPAGE: if (choice) i = 0; else if (scrollamt != 0) i = -MIN(scrollamt, max_choice); else continue; break; case KEY_NPAGE: i = MIN(choice + max_choice, item_no - scrollamt - 1); break; case KEY_UP: case '-': i = choice - 1; if (choice == 0 && scrollamt == 0) continue; break; case KEY_DOWN: case '+': i = choice + 1; if (scrollamt + choice >= item_no - 1) continue; break; default: found = FALSE; break; } } if (found) { if (i != choice) { getyx(dialog, cur_y, cur_x); if (i < 0 || i >= max_choice) { #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5 /* * Using wscrl to assist ncurses scrolling is not needed * in version 5.x */ if (i == -1) { if (menu_height > 1) { /* De-highlight current first item */ print_item(menu, ItemData(scrollamt), 0, FALSE); scrollok(menu, TRUE); wscrl(menu, -1); scrollok(menu, FALSE); } scrollamt--; print_item(menu, ItemData(scrollamt), 0, TRUE); } else if (i == max_choice) { if (menu_height > 1) { /* De-highlight current last item before scrolling up */ print_item(menu, ItemData(scrollamt + max_choice - 1), max_choice - 1, FALSE); scrollok(menu, TRUE); wscrl(menu, 1); scrollok(menu, FALSE); } scrollamt++; print_item(menu, ItemData(scrollamt + max_choice - 1), max_choice - 1, TRUE); } else #endif { if (i < 0) { scrollamt += i; choice = 0; } else { choice = max_choice - 1; scrollamt += (i - max_choice + 1); } for (i = 0; i < max_choice; i++) { print_item(menu, ItemData(scrollamt + i), i, i == choice); } } (void) wnoutrefresh(menu); dlg_draw_arrows(dialog, scrollamt, scrollamt + choice < item_no - 1, box_x + tag_x + 1, box_y, box_y + menu_height + 1); } else { /* De-highlight current item */ print_item(menu, ItemData(scrollamt + choice), choice, FALSE); /* Highlight new item */ choice = i; print_item(menu, ItemData(scrollamt + choice), choice, TRUE); (void) wnoutrefresh(menu); (void) wmove(dialog, cur_y, cur_x); wrefresh(dialog); } } continue; /* wait for another key press */ } switch (key) { case M_EVENT + 'O': del_window(dialog); return scrollamt + choice; case M_EVENT + 'C': del_window(dialog); return -2; case M_EVENT + 'o': /* mouse enter... */ case M_EVENT + 'c': /* use the code for toggling */ button = (key == M_EVENT + 'o'); /* FALLTHRU */ case ' ': case KEY_BTAB: case TAB: case KEY_LEFT: case KEY_RIGHT: if (!dialog_vars.nocancel) button = !button; dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); break; case '\n': del_window(dialog); return (button ? -2 : (scrollamt + choice)); } } del_window(dialog); return -1; /* ESC pressed */ }
/* * Display a dialog box with a list of options that can be turned on or off * The `flag' parameter is used to select between radiolist and checklist. */ int dialog_checklist(const char *title, const char *cprompt, int height, int width, int list_height, int item_no, char **items, int flag) { int result; int i; DIALOG_LISTITEM *listitems; bool separate_output = ((flag == FLAG_CHECK) && (dialog_vars.separate_output)); bool show_status = FALSE; int current = 0; listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1); assert_ptr(listitems, "dialog_checklist"); for (i = 0; i < item_no; ++i) { listitems[i].name = ItemName(i); listitems[i].text = ItemText(i); listitems[i].help = ((dialog_vars.item_help) ? ItemHelp(i) : dlg_strempty()); listitems[i].state = !dlg_strcmp(ItemStatus(i), "on"); } dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no); result = dlg_checklist(title, cprompt, height, width, list_height, item_no, listitems, NULL, flag, ¤t); switch (result) { case DLG_EXIT_OK: /* FALLTHRU */ case DLG_EXIT_EXTRA: show_status = TRUE; break; case DLG_EXIT_HELP: dlg_add_result("HELP "); show_status = dialog_vars.help_status; if (USE_ITEM_HELP(listitems[current].help)) { if (show_status) { if (separate_output) { dlg_add_string(listitems[current].help); dlg_add_separator(); } else { dlg_add_quoted(listitems[current].help); } } else { dlg_add_string(listitems[current].help); } result = DLG_EXIT_ITEM_HELP; } else { if (show_status) { if (separate_output) { dlg_add_string(listitems[current].name); dlg_add_separator(); } else { dlg_add_quoted(listitems[current].name); } } else { dlg_add_string(listitems[current].name); } } break; } if (show_status) { for (i = 0; i < item_no; i++) { if (listitems[i].state) { if (separate_output) { dlg_add_string(listitems[i].name); dlg_add_separator(); } else { if (dlg_need_separator()) dlg_add_separator(); dlg_add_string(listitems[i].name); } } } } dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no); free(listitems); return result; }
int dialog_mixedform(const char *title, const char *cprompt, int height, int width, int form_height, int item_no, char **items) { int result; int choice; int i; DIALOG_FORMITEM *listitems; DIALOG_VARS save_vars; bool show_status = FALSE; dlg_save_vars(&save_vars); dialog_vars.separate_output = TRUE; listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1); assert_ptr(listitems, "dialog_mixedform"); for (i = 0; i < item_no; ++i) { listitems[i].type = dialog_vars.formitem_type; listitems[i].name = ItemName(i); listitems[i].name_len = (int) strlen(ItemName(i)); listitems[i].name_y = dlg_ordinate(ItemNameY(i)); listitems[i].name_x = dlg_ordinate(ItemNameX(i)); listitems[i].text = ItemText(i); listitems[i].text_len = (int) strlen(ItemText(i)); listitems[i].text_y = dlg_ordinate(ItemTextY(i)); listitems[i].text_x = dlg_ordinate(ItemTextX(i)); listitems[i].text_flen = atoi(ItemTextFLen(i)); listitems[i].text_ilen = atoi(ItemTextILen(i)); listitems[i].help = (dialog_vars.item_help ? ItemHelp(i) : dlg_strempty()); listitems[i].type = (unsigned) atoi(ItemTypep(i)); } result = dlg_form(title, cprompt, height, width, form_height, item_no, listitems, &choice); switch (result) { case DLG_EXIT_OK: /* FALLTHRU */ case DLG_EXIT_EXTRA: show_status = TRUE; break; case DLG_EXIT_HELP: dlg_add_result("HELP "); show_status = dialog_vars.help_status; if (USE_ITEM_HELP(listitems[choice].help)) { dlg_add_string(listitems[choice].help); result = DLG_EXIT_ITEM_HELP; } else { dlg_add_string(listitems[choice].name); } if (show_status) dlg_add_separator(); break; } if (show_status) { for (i = 0; i < item_no; i++) { if (listitems[i].text_flen > 0) { dlg_add_string(listitems[i].text); dlg_add_separator(); } } dlg_add_last_key(-1); } dlg_free_formitems(listitems); dlg_restore_vars(&save_vars); return result; }
/* * Display a menu for choosing among a number of options */ int dialog_menu(const char *title, const char *cprompt, int height, int width, int menu_height, int item_no, char **items) { int i, j, x, y, cur_x, cur_y, box_x, box_y; int key = 0, fkey; int button = 0; int choice = dlg_default_item(items, MENUBOX_TAGS); int result = DLG_EXIT_UNKNOWN; int scrollamt = 0; int max_choice, min_width; int found; int use_width, name_width, text_width; WINDOW *dialog, *menu; char *prompt = strclone(cprompt); const char **buttons = dlg_ok_labels(); tab_correct_str(prompt); if (menu_height == 0) { min_width = calc_listw(item_no, items, MENUBOX_TAGS) + 10; /* calculate height without items (4) */ auto_size(title, prompt, &height, &width, 4, MAX(26, min_width)); calc_listh(&height, &menu_height, item_no); } else { auto_size(title, prompt, &height, &width, 4 + menu_height, 26); } print_size(height, width); ctl_size(height, width); /* Find out maximal number of displayable items at once. */ max_choice = MIN(menu_height, RowHeight(item_no)); if (dialog_vars.input_menu) max_choice /= INPUT_ROWS; x = box_x_ordinate(width); y = box_y_ordinate(height); dialog = new_window(height, width, y, x); mouse_setbase(x, y); draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); draw_bottom_box(dialog); draw_title(dialog, title); wattrset(dialog, dialog_attr); print_autowrap(dialog, prompt, height, width); menu_width = width - 6; getyx(dialog, cur_y, cur_x); box_y = cur_y + 1; box_x = (width - menu_width) / 2 - 1; /* create new window for the menu */ menu = sub_window(dialog, menu_height, menu_width, y + box_y + 1, x + box_x + 1); /* draw a box around the menu items */ draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2, menubox_border_attr, menubox_attr); name_width = 0; text_width = 0; /* Find length of longest item to center menu * * only if --menu was given, using --inputmenu * * won't be centered. */ for (i = 0; i < item_no; i++) { name_width = MAX(name_width, dlg_count_columns(ItemName(i))); text_width = MAX(text_width, dlg_count_columns(ItemText(i))); } /* If the name+text is wider than the list is allowed, then truncate * one or both of them. If the name is no wider than 1/4 of the list, * leave it intact. */ use_width = (menu_width - GUTTER); if (text_width + name_width > use_width) { int need = 0.25 * use_width; if (name_width > need) { int want = use_width * ((double) name_width) / (text_width + name_width); name_width = (want > need) ? want : need; } text_width = use_width - name_width; } tag_x = (dialog_vars.input_menu ? 0 : (use_width - text_width - name_width) / 2); item_x = name_width + tag_x + GUTTER; if (choice - scrollamt >= max_choice) { scrollamt = choice - (max_choice - 1); choice = max_choice - 1; } /* Print the menu */ for (i = 0; i < max_choice; i++) print_item(menu, ItemData(i + scrollamt), i, i == choice); (void) wnoutrefresh(menu); /* register the new window, along with its borders */ mouse_mkbigregion(box_y + 1, box_x, menu_height + 2, menu_width + 2, KEY_MAX, 1, 1, 1 /* by lines */ ); dlg_draw_arrows(dialog, scrollamt, scrollamt + max_choice < item_no, box_x + tag_x + 1, box_y, box_y + menu_height + 1); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); wtimeout(dialog, WTIMEOUT_VAL); while (result == DLG_EXIT_UNKNOWN) { key = mouse_wgetch(dialog, &fkey); if (!fkey) { fkey = TRUE; switch (key) { case '\n': case '\r': key = KEY_ENTER; break; case '-': key = KEY_UP; break; case '+': key = KEY_DOWN; break; case ' ': case TAB: key = KEY_RIGHT; break; case ESC: result = DLG_EXIT_ESC; continue; default: fkey = FALSE; break; } } found = FALSE; if (fkey) { /* * Allow a mouse-click on a box to switch selection to that box. * Handling a button click is a little more complicated, since we * push a KEY_ENTER back onto the input stream so we'll put the * cursor at the right place before handling the "keypress". */ if (key >= (M_EVENT + KEY_MAX)) { key -= (M_EVENT + KEY_MAX); i = RowToItem(key); found = TRUE; } else if (key >= M_EVENT && dlg_ok_buttoncode(key - M_EVENT) >= 0) { button = (key - M_EVENT); ungetch('\n'); continue; } } else { /* * Check if key pressed matches first character of any item tag in * list. If there is more than one match, we will cycle through * each one as the same key is pressed repeatedly. */ for (j = scrollamt + choice + 1; j < item_no; j++) { if (dlg_match_char(dlg_last_getc(), ItemName(j))) { found = TRUE; i = j - scrollamt; break; } } if (!found) { for (j = 0; j <= scrollamt + choice; j++) { if (dlg_match_char(dlg_last_getc(), ItemName(j))) { found = TRUE; i = j - scrollamt; break; } } } if (found) dlg_flush_getc(); /* * A single digit (1-9) positions the selection to that line in the * current screen. */ if (!found && (key <= '9') && (key > '0') && (key - '1' < max_choice)) { found = TRUE; i = key - '1'; } } if (!found && fkey) { found = TRUE; switch (key) { case KEY_HOME: i = -scrollamt; break; case KEY_LL: case KEY_END: i = item_no - 1 - scrollamt; break; case M_EVENT + KEY_PPAGE: case KEY_PPAGE: if (choice) i = 0; else if (scrollamt != 0) i = -MIN(scrollamt, max_choice); else continue; break; case M_EVENT + KEY_NPAGE: case KEY_NPAGE: i = MIN(choice + max_choice, item_no - scrollamt - 1); break; case KEY_UP: i = choice - 1; if (choice == 0 && scrollamt == 0) continue; break; case KEY_DOWN: i = choice + 1; if (scrollamt + choice >= item_no - 1) continue; break; default: found = FALSE; break; } } if (found) { if (i != choice) { getyx(dialog, cur_y, cur_x); if (i < 0 || i >= max_choice) { #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5 /* * Using wscrl to assist ncurses scrolling is not needed * in version 5.x */ if (i == -1) { if (menu_height > 1) { /* De-highlight current first item */ print_item(menu, ItemData(scrollamt), 0, FALSE); scrollok(menu, TRUE); wscrl(menu, -RowHeight(1)); scrollok(menu, FALSE); } scrollamt--; print_item(menu, ItemData(scrollamt), 0, TRUE); } else if (i == max_choice) { if (menu_height > 1) { /* De-highlight current last item before scrolling up */ print_item(menu, ItemData(scrollamt + max_choice - 1), max_choice - 1, FALSE); scrollok(menu, TRUE); wscrl(menu, RowHeight(1)); scrollok(menu, FALSE); } scrollamt++; print_item(menu, ItemData(scrollamt + max_choice - 1), max_choice - 1, TRUE); } else #endif { if (i < 0) { scrollamt += i; choice = 0; } else { choice = max_choice - 1; scrollamt += (i - max_choice + 1); } for (i = 0; i < max_choice; i++) { print_item(menu, ItemData(scrollamt + i), i, i == choice); } } /* Clean bottom lines */ if (dialog_vars.input_menu) { int spare_lines, x_count; spare_lines = menu_height % INPUT_ROWS; wattrset(menu, menubox_attr); for (; spare_lines; spare_lines--) { wmove(menu, menu_height - spare_lines, 0); for (x_count = 0; x_count < menu_width; x_count++) { waddch(menu, ' '); } } } (void) wnoutrefresh(menu); dlg_draw_arrows(dialog, scrollamt, scrollamt + choice < item_no - 1, box_x + tag_x + 1, box_y, box_y + menu_height + 1); } else { /* De-highlight current item */ print_item(menu, ItemData(scrollamt + choice), choice, FALSE); /* Highlight new item */ choice = i; print_item(menu, ItemData(scrollamt + choice), choice, TRUE); (void) wnoutrefresh(menu); (void) wmove(dialog, cur_y, cur_x); wrefresh(dialog); } } continue; /* wait for another key press */ } if (fkey) { switch (key) { case KEY_BTAB: case KEY_LEFT: button = dlg_prev_button(buttons, button); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); break; case KEY_RIGHT: button = dlg_next_button(buttons, button); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); break; case KEY_ENTER: del_window(dialog); result = handle_button(dlg_ok_buttoncode(button), items, scrollamt + choice); if (dialog_vars.input_menu && result == DLG_EXIT_EXTRA) { char *tmp; tmp = input_menu_edit(menu, ItemData(scrollamt + choice), choice); dialog_vars.input_result[0] = '\0'; dlg_add_result("RENAMED "); dlg_add_result(ItemName(scrollamt + choice)); dlg_add_result(" "); dlg_add_result(tmp); free(tmp); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); } break; default: flash(); break; } } } mouse_free_regions(); del_window(dialog); free(prompt); return result; }