bool PrepareMapsFileInfoList(MapsFileInfoList & lists, bool multi)
{
    const Settings & conf = Settings::Get();
    ListFiles maps = conf.GetListFiles("maps", ".mp2");

    // check loyality version
    if(conf.PriceLoyaltyVersion())
        maps.Append(conf.GetListFiles("maps", ".mx2"));

    const ListDirs & list = conf.GetMapsParams();
    if(! list.empty())
    {
	for(ListDirs::const_iterator
	    it = list.begin(); it != list.end(); ++it)
	if(*it != "maps")
	{
    	    maps.Append(conf.GetListFiles(*it, ".mp2"));
	    if(conf.PriceLoyaltyVersion())
    		maps.Append(conf.GetListFiles(*it, ".mx2"));
	}
    }

    if(maps.empty()) return false;
    lists.reserve(maps.size());

    for(ListFiles::const_iterator
	it = maps.begin(); it != maps.end(); ++it)
    {
	Maps::FileInfo fi;
	if(fi.ReadMP2(*it)) lists.push_back(fi);
    }

    std::sort(lists.begin(), lists.end(), Maps::FileInfo::NameSorting);
    lists.resize(std::unique(lists.begin(), lists.end(), Maps::FileInfo::NameCompare) - lists.begin());

    if(multi == false)
    {
	MapsFileInfoList::iterator it = std::remove_if(lists.begin(), lists.end(),
				    std::mem_fun_ref(&Maps::FileInfo::isMultiPlayerMap));
	if(it != lists.begin()) lists.resize(std::distance(lists.begin(), it));
    }

    // set preferably count filter
    if(conf.PreferablyCountPlayers())
    {

	MapsFileInfoList::iterator it = std::remove_if(lists.begin(), lists.end(),
		std::not1(std::bind2nd(std::mem_fun_ref(&Maps::FileInfo::isAllowCountPlayers), conf.PreferablyCountPlayers())));
	if(it != lists.begin()) lists.resize(std::distance(lists.begin(), it));
    }

    return lists.size();
}
Game::menu_t PocketPC::SelectScenario(void)
{
    Settings & conf = Settings::Get();
    Cursor & cursor = Cursor::Get();
    Display & display = Display::Get();
    LocalEvent & le = LocalEvent::Get();

    cursor.Hide();
    cursor.SetThemes(cursor.POINTER);

    const Sprite &sprite = AGG::GetICN(ICN::HEROES, 0);
    Rect src_rt((sprite.w() - display.w()) / 2, 0, display.w(), display.h());
    sprite.Blit(src_rt, 0, 0);

    MapsFileInfoList all;
    if(!PrepareMapsFileInfoList(all, false))
    {
        Dialog::Message(_("Warning"), _("No maps available!"), Font::BIG, Dialog::OK);
        return Game::MAINMENU;
    }

    MapsFileInfoList small;
    MapsFileInfoList medium;
    MapsFileInfoList large;
    MapsFileInfoList xlarge;

    small.reserve(all.size());
    medium.reserve(all.size());
    large.reserve(all.size());
    xlarge.reserve(all.size());

    for(MapsFileInfoList::iterator cur = all.begin(); cur != all.end(); ++ cur)
    {
	switch((*cur).size_w)
	{
    	    case Maps::SMALL:	small.push_back(*cur); break;
    	    case Maps::MEDIUM:	medium.push_back(*cur); break;
    	    case Maps::LARGE:	large.push_back(*cur); break;
    	    case Maps::XLARGE:	xlarge.push_back(*cur); break;
	    default: continue;
	}
    }

    const u16 window_w = 320;
    const u16 window_h = 224;

    Dialog::FrameBorder frameborder;
    frameborder.SetPosition((display.w() - window_w) / 2 - BORDERWIDTH, (display.h() - window_h) / 2 - BORDERWIDTH, window_w, window_h);
    frameborder.Redraw();

    const Rect & rt = frameborder.GetArea();
    const Sprite & background = AGG::GetICN(ICN::STONEBAK, 0);
    background.Blit(Rect(0, 0, window_w, window_h), rt);

    ButtonGroups btnGroups(rt, Dialog::OK|Dialog::CANCEL);

    Button buttonSelectSmall(rt.x + 7, rt.y + 12, ICN::REQUESTS, 9, 10);
    Button buttonSelectMedium(rt.x + 69, rt.y + 12, ICN::REQUESTS, 11, 12);
    Button buttonSelectLarge(rt.x + 131, rt.y + 12, ICN::REQUESTS, 13, 14);
    Button buttonSelectXLarge(rt.x + 193, rt.y + 12, ICN::REQUESTS, 15, 16);
    Button buttonSelectAll(rt.x + 255, rt.y + 12, ICN::REQUESTS, 17, 18);

    if(all.empty()) btnGroups.DisableButton1(true);
    if(small.empty()) buttonSelectSmall.SetDisable(true);
    if(medium.empty()) buttonSelectMedium.SetDisable(true);
    if(large.empty()) buttonSelectLarge.SetDisable(true);
    if(xlarge.empty()) buttonSelectXLarge.SetDisable(true);

    ScenarioListBox listbox(rt);

    listbox.RedrawBackground(rt);
    listbox.SetScrollButtonUp(ICN::REQUESTS, 5, 6, Point(rt.x + 285, rt.y + 40));
    listbox.SetScrollButtonDn(ICN::REQUESTS, 7, 8, Point(rt.x + 285, rt.y + 175));
    listbox.SetScrollSplitter(AGG::GetICN(ICN::ESCROLL, 3), Rect(rt.x + 288, rt.y + 58, 12, 114));
    listbox.SetAreaMaxItems(8);
    listbox.SetAreaItems(Rect(rt.x + 17, rt.y + 37, 266, 156));
    listbox.SetListContent(all);

    listbox.Redraw();

    btnGroups.Draw();

    buttonSelectSmall.Draw();
    buttonSelectMedium.Draw();
    buttonSelectLarge.Draw();
    buttonSelectXLarge.Draw();
    buttonSelectAll.Draw();

    u16 result = Dialog::ZERO;

    cursor.Show();
    display.Flip();

    while(result == Dialog::ZERO && le.HandleEvents())
    {
	le.MousePressLeft(buttonSelectSmall) && buttonSelectSmall.isEnable() ? buttonSelectSmall.PressDraw() : buttonSelectSmall.ReleaseDraw();
	le.MousePressLeft(buttonSelectMedium) && buttonSelectMedium.isEnable() ? buttonSelectMedium.PressDraw() : buttonSelectMedium.ReleaseDraw();
	le.MousePressLeft(buttonSelectLarge) && buttonSelectLarge.isEnable() ? buttonSelectLarge.PressDraw() : buttonSelectLarge.ReleaseDraw();
	le.MousePressLeft(buttonSelectXLarge) && buttonSelectXLarge.isEnable() ? buttonSelectXLarge.PressDraw() : buttonSelectXLarge.ReleaseDraw();
	le.MousePressLeft(buttonSelectAll) ? buttonSelectAll.PressDraw() : buttonSelectAll.ReleaseDraw();

	result = btnGroups.QueueEventProcessing();

	if(((le.MouseClickLeft(buttonSelectSmall) || le.KeyPress(KEY_s)) && buttonSelectSmall.isEnable()) && buttonSelectSmall.isEnable())
	{
	    listbox.SetListContent(small);
	    cursor.Hide();
	}
	else
	if(((le.MouseClickLeft(buttonSelectMedium) || le.KeyPress(KEY_m)) && buttonSelectMedium.isEnable()) && buttonSelectMedium.isEnable())
	{
	    listbox.SetListContent(medium);
	    cursor.Hide();
	}
	else
	if(((le.MouseClickLeft(buttonSelectLarge) || le.KeyPress(KEY_l)) && buttonSelectLarge.isEnable()) && buttonSelectLarge.isEnable())
	{
	    listbox.SetListContent(large);
	    cursor.Hide();
	}
	else
	if(((le.MouseClickLeft(buttonSelectXLarge) || le.KeyPress(KEY_x)) && buttonSelectXLarge.isEnable()) && buttonSelectXLarge.isEnable())
	{
	    listbox.SetListContent(xlarge);
	    cursor.Hide();
	}
	else
	if(le.MouseClickLeft(buttonSelectAll) || le.KeyPress(KEY_a))
	{
	    listbox.SetListContent(all);
	    cursor.Hide();
	}

	listbox.QueueEventProcessing();

	if(!cursor.isVisible())
	{
	    listbox.Redraw();
	    cursor.Show();
	    display.Flip();
	}
    }

    if(Dialog::OK == result)
    {
	conf.SetCurrentFileInfo(listbox.GetCurrent());
    	conf.SetGameDifficulty(Difficulty::NORMAL);

	return Game::SCENARIOINFO;
    }

    return Game::MAINMENU;
}
const Maps::FileInfo* Dialog::SelectScenario(const MapsFileInfoList & all)
{
    Cursor & cursor = Cursor::Get();
    Display & display = Display::Get();
    LocalEvent & le = LocalEvent::Get();

    cursor.Hide();
    cursor.SetThemes(cursor.POINTER);

    const Maps::FileInfo* result = NULL;
    MapsFileInfoList small;
    MapsFileInfoList medium;
    MapsFileInfoList large;
    MapsFileInfoList xlarge;

    small.reserve(all.size());
    medium.reserve(all.size());
    large.reserve(all.size());
    xlarge.reserve(all.size());

    for(MapsFileInfoList::const_iterator cur = all.begin(); cur != all.end(); ++ cur)
    {
	switch((*cur).size_w)
	{
    	    case Maps::SMALL:	small.push_back(*cur); break;
    	    case Maps::MEDIUM:	medium.push_back(*cur); break;
    	    case Maps::LARGE:	large.push_back(*cur); break;
    	    case Maps::XLARGE:	xlarge.push_back(*cur); break;
	    default: continue;
	}
    }

    const Sprite & panel = AGG::GetICN(ICN::REQSBKG, 0);
    Background back((display.w() - panel.w()) / 2, (display.h() - panel.h()) / 2, panel.w(), panel.h());
    back.Save();

    const Rect & rt = back.GetRect();

    const Rect countPlayers(rt.x + 45, rt.y + 55, 20, 175);
    const Rect sizeMaps(rt.x + 62, rt.y + 55, 20, 175);
    const Rect victoryConds(rt.x + 267, rt.y + 55, 20, 175);
    const Rect lossConds(rt.x + 287, rt.y + 55, 20, 175);

    const Rect curCountPlayer(rt.x + 66, rt.y + 264, 18, 18);
    const Rect curMapSize(rt.x + 85, rt.y + 264, 18, 18);
    const Rect curMapName(rt.x + 107, rt.y + 264, 166, 18);
    const Rect curVictoryCond(rt.x + 277, rt.y + 264, 18, 18);
    const Rect curLossCond(rt.x + 295, rt.y + 264, 18, 18);
    const Rect curDifficulty(rt.x + 220, rt.y + 292, 114, 20);
    const Rect curDescription(rt.x + 42, rt.y + 316, 292, 90);

    Button buttonOk(rt.x + 140, rt.y + 410, ICN::REQUESTS, 1, 2);

    Button buttonSelectSmall(rt.x + 37, rt.y + 22, ICN::REQUESTS, 9, 10);
    Button buttonSelectMedium(rt.x + 99, rt.y + 22, ICN::REQUESTS, 11, 12);
    Button buttonSelectLarge(rt.x + 161, rt.y + 22, ICN::REQUESTS, 13, 14);
    Button buttonSelectXLarge(rt.x + 223, rt.y + 22, ICN::REQUESTS, 15, 16);
    Button buttonSelectAll(rt.x + 285, rt.y + 22, ICN::REQUESTS, 17, 18);

    if(small.empty()) buttonSelectSmall.SetDisable(true);
    if(medium.empty()) buttonSelectMedium.SetDisable(true);
    if(large.empty()) buttonSelectLarge.SetDisable(true);
    if(xlarge.empty()) buttonSelectXLarge.SetDisable(true);

    ScenarioListBox listbox(rt);

    listbox.RedrawBackground(rt);
    listbox.SetScrollButtonUp(ICN::REQUESTS, 5, 6, Point(rt.x + 327, rt.y + 55));
    listbox.SetScrollButtonDn(ICN::REQUESTS, 7, 8, Point(rt.x + 327, rt.y + 217));
    listbox.SetScrollSplitter(AGG::GetICN(ICN::ESCROLL, 3), Rect(rt.x + 330, rt.y + 73, 12, 141));
    listbox.SetAreaMaxItems(9);
    listbox.SetAreaItems(Rect(rt.x + 55, rt.y + 55, 270, 175));
    listbox.SetListContent(const_cast<MapsFileInfoList &>(all));
    listbox.Redraw();

    buttonOk.Draw();
    buttonSelectSmall.Draw();
    buttonSelectMedium.Draw();
    buttonSelectLarge.Draw();
    buttonSelectXLarge.Draw();
    buttonSelectAll.Draw();

    cursor.Show();
    display.Flip();

    while(le.HandleEvents())
    {
        if(buttonOk.isEnable()) le.MousePressLeft(buttonOk) ? buttonOk.PressDraw() : buttonOk.ReleaseDraw();
	le.MousePressLeft(buttonSelectSmall) && buttonSelectSmall.isEnable() ? buttonSelectSmall.PressDraw() : buttonSelectSmall.ReleaseDraw();
	le.MousePressLeft(buttonSelectMedium) && buttonSelectMedium.isEnable() ? buttonSelectMedium.PressDraw() : buttonSelectMedium.ReleaseDraw();
	le.MousePressLeft(buttonSelectLarge) && buttonSelectLarge.isEnable() ? buttonSelectLarge.PressDraw() : buttonSelectLarge.ReleaseDraw();
	le.MousePressLeft(buttonSelectXLarge) && buttonSelectXLarge.isEnable() ? buttonSelectXLarge.PressDraw() : buttonSelectXLarge.ReleaseDraw();
	le.MousePressLeft(buttonSelectAll) ? buttonSelectAll.PressDraw() : buttonSelectAll.ReleaseDraw();

        if((buttonOk.isEnable() && le.MouseClickLeft(buttonOk)) ||
	    Game::HotKeyPress(Game::EVENT_DEFAULT_READY) ||
	    listbox.selectOk)
	{
	    MapsFileInfoList::const_iterator it = std::find(all.begin(), all.end(), listbox.GetCurrent());
	    result = it != all.end() ? &(*it) : NULL;
	    break;
	}
	else
        if(Game::HotKeyPress(Game::EVENT_DEFAULT_EXIT))
	{
	    result = NULL;
	    break;
	}
	else
	if(((le.MouseClickLeft(buttonSelectSmall) || le.KeyPress(KEY_s)) && buttonSelectSmall.isEnable()) && buttonSelectSmall.isEnable())
	{
	    listbox.SetListContent(small);
	    cursor.Hide();
	}
	else
	if(((le.MouseClickLeft(buttonSelectMedium) || le.KeyPress(KEY_m)) && buttonSelectMedium.isEnable()) && buttonSelectMedium.isEnable())
	{
	    listbox.SetListContent(medium);
	    cursor.Hide();
	}
	else
	if(((le.MouseClickLeft(buttonSelectLarge) || le.KeyPress(KEY_l)) && buttonSelectLarge.isEnable()) && buttonSelectLarge.isEnable())
	{
	    listbox.SetListContent(large);
	    cursor.Hide();
	}
	else
	if(((le.MouseClickLeft(buttonSelectXLarge) || le.KeyPress(KEY_x)) && buttonSelectXLarge.isEnable()) && buttonSelectXLarge.isEnable())
	{
	    listbox.SetListContent(xlarge);
	    cursor.Hide();
	}
	else
	if(le.MouseClickLeft(buttonSelectAll) || le.KeyPress(KEY_a))
	{
	    listbox.SetListContent(const_cast<MapsFileInfoList &>(all));
	    cursor.Hide();
	}

	listbox.QueueEventProcessing();

	// right info
	if(le.MousePressRight(buttonSelectSmall)) Dialog::Message(_("Small Maps"), _("View only maps of size small (36x36)."), Font::BIG);
	else
	if(le.MousePressRight(buttonSelectMedium)) Dialog::Message(_("Medium Maps"), _("View only maps of size medium (72x72)."), Font::BIG);
	else
	if(le.MousePressRight(buttonSelectLarge)) Dialog::Message(_("Large Maps"), _("View only maps of size large (108x108)."), Font::BIG);
	else
	if(le.MousePressRight(buttonSelectXLarge)) Dialog::Message(_("Extra Large Maps"), _("View only maps of size extra large (144x144)."), Font::BIG);
	else
	if(le.MousePressRight(buttonSelectAll)) Dialog::Message(_("All Maps"), _("View all maps, regardless of size."), Font::BIG);
	else
	if(le.MousePressRight(countPlayers) || le.MousePressRight(curCountPlayer)) Dialog::Message(_("Players Icon"), _("Indicates how many players total are in the EditScenario. Any positions not occupied by humans will be occupied by computer players."), Font::BIG);
	else
	if(le.MousePressRight(sizeMaps) || le.MousePressRight(curMapSize)) Dialog::Message(_("Size Icon"), _("Indicates whether the maps is small (36x36), medium (72x72), large (108x108), or extra large (144x144)."), Font::BIG);
	else
	if(le.MousePressRight(curMapName)) Dialog::Message(_("Selected Name"), _("The name of the currently selected map."), Font::BIG);
	else
	if(le.MousePressRight(victoryConds))
	{
	    const Maps::FileInfo* item = listbox.GetFromPosition(le.GetMouseCursor());
	    if(item) VictoryConditionInfo(*item);
	}
	else
	if(le.MousePressRight(lossConds))
	{
	    const Maps::FileInfo* item = listbox.GetFromPosition(le.GetMouseCursor());
	    if(item) LossConditionInfo(*item);
	}
	else
	if(le.MousePressRight(curVictoryCond)) VictoryConditionInfo(listbox.GetCurrent());
	else
	if(le.MousePressRight(curLossCond)) LossConditionInfo(listbox.GetCurrent());
	else
	if(le.MousePressRight(curDifficulty)) Dialog::Message(_("Selected Map Difficulty"), _("The map difficulty of the currently selected map.  The map difficulty is determined by the EditScenario designer. More difficult maps might include more or stronger enemies, fewer resources, or other special conditions making things tougher for the human player."), Font::BIG);
	else
	if(le.MousePressRight(curDescription)) Dialog::Message(_("Selected Description"), _("The description of the currently selected map."), Font::BIG);
	else
	if(le.MousePressRight(buttonOk)) Dialog::Message(_("OK"), _("Accept the choice made."), Font::BIG);

	if(!cursor.isVisible())
	{
	    listbox.Redraw();
	    buttonOk.Draw();
    	    buttonSelectSmall.Draw();
    	    buttonSelectMedium.Draw();
    	    buttonSelectLarge.Draw();
    	    buttonSelectXLarge.Draw();
    	    buttonSelectAll.Draw();
	    cursor.Show();
	    display.Flip();
	}
    }

    cursor.Hide();
    back.Restore();

    return result;
}