int NetHackQtMenuWindow::SelectMenu()

in win/Qt/qt_win.cpp [2840:2959]


int NetHackQtMenuWindow::SelectMenu(int h, MENU_ITEM_P **menu_list)
{
    setFont(str_fixed ?
	qt_settings->normalFixedFont() : qt_settings->normalFont());

    for (int i=0; i<itemcount; i++) {
	str_width=QMAX(str_width,
	    STR_MARGIN+fontMetrics().width(item[i].str));
    }

    setCellHeight(QMAX(qt_settings->glyphs().height()+1,fontMetrics().height()));
    setNumRows(itemcount);

    int buth=fontMetrics().height()+8; // 8 for spacing & mitres

    how=h;

    ok->setEnabled(how!=PICK_ONE);ok->setDefault(how!=PICK_ONE);
    cancel->setEnabled(TRUE);
    all->setEnabled(how==PICK_ANY);
    none->setEnabled(how==PICK_ANY);
    invert->setEnabled(how==PICK_ANY);
    search->setEnabled(how!=PICK_NONE);

    dialog->SetResult(-1);

    // 20 allows for scrollbar or spacing
    // 4 for frame borders
    int mh = QApplication::desktop()->height()*3/5;
    if ( qt_compact_mode && totalHeight() > mh ) {
	// big, so make it fill
	dialog->showMaximized();
    } else {
	dialog->resize(totalWidth()+20,
	    QMIN(totalHeight(), mh)+buth+4+(prompt.text().isNull() ? 0 : buth));
	if ( dialog->width() > QApplication::desktop()->width() )
	    dialog->resize(QApplication::desktop()->width(),dialog->height()+16);
	centerOnMain(dialog);
	dialog->show();
    }

    setFocus();
    while (dialog->result()<0) {
	// changed the defaults below to the values in wintype.h 000119 - azy
	if (!keysource.Empty()) {
	    char k=keysource.GetAscii();
	    k=map_menu_cmd(k); /* added 000119 - azy */
	    if (k=='\033')
		dialog->Reject();
	    else if (k=='\r' || k=='\n' || k==' ')
		dialog->Accept();
	    else if (k==MENU_SEARCH)
		Search();
	    else if (k==MENU_SELECT_ALL)
		All();
	    else if (k==MENU_INVERT_ALL)
		Invert();
	    else if (k==MENU_UNSELECT_ALL)
		ChooseNone();
	    else {
		for (int i=0; i<itemcount; i++) {
		    if (item[i].ch==k)
			ToggleSelect(i);
		}
	    }
	}
	if (dialog->result()<0)
	    qApp->enter_loop();
    }
    //if ( (nhid != WIN_INVEN || !iflags.perm_invent) ) // doesn't work yet
    {
	dialog->hide();
    }
    int result=dialog->result();

    // Consume ^M (which QDialog steals for default button)
    while (!keysource.Empty() &&
	(keysource.TopAscii()=='\n' || keysource.TopAscii()=='\r'))
	keysource.GetAscii();

    *menu_list=0;
    if (how==PICK_NONE)
	return result==0 ? -1 : 0;

    if (result>0) {
	if (how==PICK_ONE) {
	    int i;
	    for (i=0; i<itemcount && !item[i].selected; i++)
		;
	    if (i<itemcount) {
		*menu_list=(MENU_ITEM_P*)malloc(sizeof(MENU_ITEM_P));
		(*menu_list)[0].item=item[i].identifier;
		(*menu_list)[0].count=item[i].count;
		return 1;
	    } else {
		return 0;
	    }
	} else {
	    int count=0;
	    for (int i=0; i<itemcount; i++)
		if (item[i].selected) count++;
	    if (count) {
		*menu_list=(MENU_ITEM_P*)malloc(count*sizeof(MENU_ITEM_P));
		int j=0;
		for (int i=0; i<itemcount; i++) {
		    if (item[i].selected) {
			(*menu_list)[j].item=item[i].identifier;
			(*menu_list)[j].count=item[i].count;
			j++;
		    }
		}
		return count;
	    } else {
		return 0;
	    }
	}
    } else {
	return -1;
    }
}