char NetHackQtYnDialog::Exec()

in win/Qt/qt_win.cpp [4085:4260]


char NetHackQtYnDialog::Exec()
{
    QString ch(choices);
    int ch_per_line=6;
    QString qlabel;
    QString enable;
    if ( qt_compact_mode && !choices ) {
	// expand choices from prompt
	// ##### why isn't choices set properly???
	const char* c=question;
	while ( *c && *c != '[' )
	    c++;
	qlabel = QString(question).left(c-question);
	if ( *c ) {
	    c++;
	    if ( *c == '-' )
		ch.append(*c++);
	    char from=0;
	    while ( *c && *c != ']' && *c != ' ' ) {
		if ( *c == '-' ) {
		    from = c[-1];
		} else if ( from ) {
		    for (char f=from+1; f<=*c; f++)
			ch.append(f);
		    from = 0;
		} else {
		    ch.append(*c);
		    from = 0;
		}
		c++;
	    }
	    if ( *c == ' ' ) {
		while ( *c && *c != ']' ) {
		    if ( *c == '*' || *c == '?' )
			ch.append(*c);
		    c++;
		}
	    }
	}
	if ( strstr(question, "what direction") ) {
	    // We replace this regardless, since sometimes you get choices.
	    const char* d = Cmd.dirchars;
	    enable=ch;
	    ch="";
	    ch.append(d[1]);
	    ch.append(d[2]);
	    ch.append(d[3]);
	    ch.append(d[0]);
	    ch.append('.');
	    ch.append(d[4]);
	    ch.append(d[7]);
	    ch.append(d[6]);
	    ch.append(d[5]);
	    ch.append(d[8]);
	    ch.append(d[9]);
	    ch_per_line = 3;
	    def = ' ';
	} else {
	    // Hmm... they'll have to use a virtual keyboard
	}
    } else {
	qlabel = question;
    }
    if (!ch.isNull()) {
	QVBoxLayout vb(this);
	vb.setAutoAdd(TRUE);
	bool bigq = qlabel.length()>40;
	if ( bigq ) {
	    QLabel* q = new QLabel(qlabel,this);
	    q->setAlignment(AlignLeft|WordBreak);
	    q->setMargin(4);
	}
	QButtonGroup group(ch_per_line, Horizontal,
	    bigq ? QString::null : qlabel, this);

	int nchoices=ch.length();

	bool allow_count=ch.contains('#');

	const int margin=8;
	const int gutter=8;
	const int extra=fontMetrics().height(); // Extra for group
	int x=margin, y=extra+margin;
	int butsize=fontMetrics().height()*2+5;

	QPushButton* button;
	for (int i=0; i<nchoices && ch[i]!='\033'; i++) {
	    button=new QPushButton(QString(ch[i]),&group);
	    if ( !enable.isNull() ) {
		if ( !enable.contains(ch[i]) )
		    button->setEnabled(FALSE);
	    }
	    button->setFixedSize(butsize,butsize); // Square
	    if (ch[i]==def) button->setDefault(TRUE);
	    if (i%10==9) {
		// last in row
		x=margin;
		y+=butsize+gutter;
	    } else {
		x+=butsize+gutter;
	    }
	}

	connect(&group,SIGNAL(clicked(int)),this,SLOT(doneItem(int)));

	QLabel* lb=0;
	QLineEdit* le=0;

	if (allow_count) {
	    QHBox *hb = new QHBox(this);
	    lb=new QLabel("Count: ",hb);
	    le=new QLineEdit(hb);
	}

	adjustSize();
	centerOnMain(this);
	show();
	char choice=0;
	char ch_esc=0;
	for (uint i=0; i<ch.length(); i++) {
	    if (ch[i].latin1()=='q') ch_esc='q';
	    else if (!ch_esc && ch[i].latin1()=='n') ch_esc='n';
	}
	setResult(-1);
	while (!choice) {
	    if (!keysource.Empty()) {
		char k=keysource.GetAscii();
		char ch_esc=0;
		for (uint i=0; i<ch.length(); i++)
		    if (ch[i].latin1()==k)
			choice=k;
		if (!choice) {
		    if (k=='\033' && ch_esc)
			choice=ch_esc;
		    else if (k==' ' || k=='\r' || k=='\n')
			choice=def;
		    // else choice remains 0
		}
	    } else if ( result() == 0 ) {
		choice = ch_esc ? ch_esc : def ? def : ' ';
	    } else if ( result() == 1 ) {
		choice = def ? def : ch_esc ? ch_esc : ' ';
	    } else if ( result() >= 1000 ) {
		choice = ch[result() - 1000].latin1();
	    }
	    if ( !choice )
		qApp->enter_loop();
	}
	hide();
	if (allow_count && !le->text().isEmpty()) {
	    yn_number=atoi(le->text());
	    choice='#';
	}
	return choice;
    } else {
	QLabel label(qlabel,this);
	QPushButton cancel("Dismiss",this);
	label.setFrameStyle(QFrame::Box|QFrame::Sunken);
	label.setAlignment(AlignCenter);
	label.resize(fontMetrics().width(qlabel)+60,30+fontMetrics().height());
	cancel.move(width()/2-cancel.width()/2,label.geometry().bottom()+8);
	connect(&cancel,SIGNAL(clicked()),this,SLOT(reject()));
	centerOnMain(this);
	setResult(-1);
	show();
	while (result()<0 && keysource.Empty()) {
	    qApp->enter_loop();
	}
	hide();
	if (keysource.Empty()) {
	    return '\033';
	} else {
	    return keysource.GetAscii();
	}
    }
}