#include #include "TDialog.h" #include #include #include #include #include const int INPUT_BOX_WIDTH = 120; const int INPUT_BOX_HEIGHT = 16; TextDialogWindow::TextDialogWindow(int init_x1, int init_y1, int init_x2, int init_y2) { init(init_x1, init_y1, init_x2, init_y2); strcpy(inputBuffer, "|"); // blank input buffer (cursor only) position = 0; // cursor position in input buffer box[0] = (getMaxX() - INPUT_BOX_WIDTH) / 2; box[1] = getMaxY()*(2.0/3.0) - INPUT_BOX_HEIGHT / 2.0; box[2] = (getMaxX() + INPUT_BOX_WIDTH) / 2; box[3] = box[1]; box[4] = box[2]; box[5] = getMaxY()*(2.0/3.0) + INPUT_BOX_HEIGHT / 2.0; box[6] = box[0]; box[7] = box[5]; } void TextDialogWindow::draw(char *promptText) { clear(GREEN); setColor(WHITE); border(); // render prompt text setColor(BLACK); text(getMaxX()/2, getMaxY()/3.0, promptText, CENTER_TEXT); } void TextDialogWindow::drawInputBox(void) { // render input box setColor(WHITE); setfillstyle(SOLID_FILL, WHITE); fillpoly(4, box); // render input buffer setColor(BLACK); text((getMaxX()-INPUT_BOX_WIDTH)/2 + 3, getMaxY()*(2.0/3.0), inputBuffer, LEFT_TEXT); } char *TextDialogWindow::prompt(char *defaultValue) { setWindow(CLIP_NO); sprintf(inputBuffer, "%s|", defaultValue); // initialize input buffer position = strlen(inputBuffer) - 1; while(1) { drawInputBox(); // redraw box and inputBuffer char key = getch(); if ( isprint(key) && (position < INPUT_BUFFER_SIZE-2)) { inputBuffer[position] = key; position++; inputBuffer[position] = '|'; // fix cursor inputBuffer[position+1] = 0; // replace end-of-string 0 } if ( (key == 8) && (position > 0) ) { // backspace inputBuffer[position] = 0; // mark end of buffer, overwriting cursor position--; // back up inputBuffer[position] = '|'; // replace cursor } if (key == 13) { // enter hit inputBuffer[position] = 0; // mark end of buffer, overwriting cursor return(inputBuffer); } if (key == 27) { // esc hit, cancel return(defaultValue); } } }