core/MenuStyle_Base.cpp
author Michael McKoy <ferret99gt@gmail.com>
Sat Nov 24 03:28:33 2007 +0000 (2007-11-24)
changeset 1719 c9d5e55f0899
parent 1653 ad81268fb2f2
child 1766 d30c252089be
permissions -rw-r--r--
The current map played shouldn't appear in MapChooser votes or RTV votes now. It can still be nominated in RTV though.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdarg.h>
    33 #include "sm_stringutil.h"
    34 #include "MenuStyle_Base.h"
    35 #include "PlayerManager.h"
    36 #include "MenuManager.h"
    37 #include "HandleSys.h"
    38 #include "CellRecipientFilter.h"
    39 #if defined MENU_DEBUG
    40 #include "Logger.h"
    41 #endif
    42 
    43 BaseMenuStyle::BaseMenuStyle() : m_WatchList(256), m_hHandle(BAD_HANDLE)
    44 {
    45 }
    46 
    47 Handle_t BaseMenuStyle::GetHandle()
    48 {
    49 	/* Don't create the handle until we need it */
    50 	if (m_hHandle == BAD_HANDLE)
    51 	{
    52 		m_hHandle = g_Menus.CreateStyleHandle(this);
    53 	}
    54 
    55 	return m_hHandle;
    56 }
    57 
    58 void BaseMenuStyle::AddClientToWatch(int client)
    59 {
    60 #if defined MENU_DEBUG
    61 	g_Logger.LogMessage("[SM_MENU] AddClientToWatch(%d)", client);
    62 #endif
    63 	m_WatchList.push_back(client);
    64 }
    65 
    66 void BaseMenuStyle::RemoveClientFromWatch(int client)
    67 {
    68 #if defined MENU_DEBUG
    69 	g_Logger.LogMessage("[SM_MENU] RemoveClientFromWatch(%d)", client);
    70 #endif
    71 	m_WatchList.remove(client);
    72 }
    73 
    74 void BaseMenuStyle::_CancelClientMenu(int client, MenuCancelReason reason, bool bAutoIgnore/* =false */)
    75 {
    76 #if defined MENU_DEBUG
    77 	g_Logger.LogMessage("[SM_MENU] _CancelClientMenu() (client %d) (bAutoIgnore %d) (reason %d)", client, bAutoIgnore, reason);
    78 #endif
    79 	CBaseMenuPlayer *player = GetMenuPlayer(client);
    80 	menu_states_t &states = player->states;
    81 
    82 	bool bOldIgnore = player->bAutoIgnore;
    83 	if (bAutoIgnore)
    84 	{
    85 		player->bAutoIgnore = true;
    86 	}
    87 
    88 	/* Save states */
    89 	IMenuHandler *mh = states.mh;
    90 	IBaseMenu *menu = states.menu;
    91 
    92 	/* Clear menu */
    93 	player->bInMenu = false;
    94 	if (player->menuHoldTime)
    95 	{
    96 		RemoveClientFromWatch(client);
    97 	}
    98 
    99 	/* Fire callbacks */
   100 	mh->OnMenuCancel(menu, client, reason);
   101 	
   102 	/* Only fire end if there's a valid menu */
   103 	if (menu)
   104 	{
   105 		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
   106 	}
   107 
   108 	if (bAutoIgnore)
   109 	{
   110 		player->bAutoIgnore = bOldIgnore;
   111 	}
   112 }
   113 
   114 void BaseMenuStyle::CancelMenu(CBaseMenu *menu)
   115 {
   116 #if defined MENU_DEBUG
   117 	g_Logger.LogMessage("[SM_MENU] CancelMenu() (menu %p)", menu);
   118 #endif
   119 	int maxClients = g_Players.GetMaxClients();
   120 	for (int i=1; i<=maxClients; i++)
   121 	{
   122 		CBaseMenuPlayer *player = GetMenuPlayer(i);
   123 		if (player->bInMenu)
   124 		{
   125 			menu_states_t &states = player->states;
   126 			if (states.menu == menu)
   127 			{
   128 				_CancelClientMenu(i, MenuCancel_Interrupted);
   129 			}
   130 		}
   131 	}
   132 }
   133 
   134 bool BaseMenuStyle::CancelClientMenu(int client, bool autoIgnore)
   135 {
   136 #if defined MENU_DEBUG
   137 	g_Logger.LogMessage("[SM_MENU] CancelClientMenu() (client %d) (bAutoIgnore %d)", client, autoIgnore);
   138 #endif
   139 	if (client < 1 || client > g_Players.MaxClients())
   140 	{
   141 		return false;
   142 	}
   143 
   144 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   145 	if (!player->bInMenu)
   146 	{
   147 		return false;
   148 	}
   149 
   150 	_CancelClientMenu(client, MenuCancel_Interrupted, autoIgnore);
   151 
   152 	return true;
   153 }
   154 
   155 MenuSource BaseMenuStyle::GetClientMenu(int client, void **object)
   156 {
   157 	if (client < 1 || client > g_Players.GetMaxClients())
   158 	{
   159 		return MenuSource_None;
   160 	}
   161 
   162 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   163 
   164 	if (player->bInMenu)
   165 	{
   166 		IBaseMenu *menu;
   167 		if ((menu=player->states.menu) != NULL)
   168 		{
   169 			if (object)
   170 			{
   171 				*object = menu;
   172 			}
   173 			return MenuSource_BaseMenu;
   174 		}
   175 
   176 		return MenuSource_Display;
   177 	} else if (player->bInExternMenu) {
   178 		if (player->menuHoldTime != 0
   179 			&& (gpGlobals->curtime > player->menuStartTime + player->menuHoldTime))
   180 		{
   181 			player->bInExternMenu = false;
   182 			return MenuSource_None;
   183 		}
   184 		return MenuSource_External;
   185 	}
   186 
   187 	return MenuSource_None;
   188 }
   189 
   190 void BaseMenuStyle::OnClientDisconnected(int client)
   191 {
   192 #if defined MENU_DEBUG
   193 	g_Logger.LogMessage("[SM_MENU] OnClientDisconnected(%d)", client);
   194 #endif
   195 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   196 	if (!player->bInMenu)
   197 	{
   198 		return;
   199 	}
   200 
   201 	_CancelClientMenu(client, MenuCancel_Disconnected, true);
   202 
   203 	player->bInMenu = false;
   204 	player->bInExternMenu = false;
   205 }
   206 
   207 static int do_lookup[256];
   208 void BaseMenuStyle::ProcessWatchList()
   209 {
   210 	if (!m_WatchList.size())
   211 	{
   212 		return;
   213 	}
   214 
   215 #if defined MENU_DEBUG
   216 	g_Logger.LogMessage("BaseMenuStyle::ProcessWatchList(%d,%d,%d,%d,%d,%p)", 
   217 		m_WatchList.m_Size,
   218 		m_WatchList.m_FirstLink,
   219 		m_WatchList.m_FreeNodes,
   220 		m_WatchList.m_LastLink,
   221 		m_WatchList.m_MaxSize,
   222 		m_WatchList.m_Nodes);
   223 #endif
   224 
   225 	unsigned int total = 0;
   226 	for (FastLink<int>::iterator iter=m_WatchList.begin(); iter!=m_WatchList.end(); ++iter)
   227 	{
   228 		do_lookup[total++] = (*iter);
   229 	}
   230 
   231 #if defined MENU_DEBUG
   232 	if (total)
   233 	{
   234 		g_Logger.LogMessage("[SM_MENU] ProcessWatchList() found %d clients", total);
   235 	}
   236 #endif
   237 
   238 	int client;
   239 	CBaseMenuPlayer *player;
   240 	float curTime = gpGlobals->curtime;
   241 	for (unsigned int i=0; i<total; i++)
   242 	{
   243 		client = do_lookup[i];
   244 		player = GetMenuPlayer(client);
   245 #if defined MENU_DEBUG
   246 		g_Logger.LogMessage("[SM_MENU] ProcessWatchList() (client %d) (bInMenu %d) (menuHoldTime %d) (curTime %f) (menuStartTime %f)",
   247 			client,
   248 			player->bInMenu,
   249 			player->menuHoldTime,
   250 			curTime,
   251 			player->menuStartTime);
   252 #endif
   253 		if (!player->bInMenu || !player->menuHoldTime)
   254 		{
   255 #if defined MENU_DEBUG
   256 			g_Logger.LogMessage("[SM_MENU] ProcessWatchList() removing client %d", client);
   257 #endif
   258 			m_WatchList.remove(client);
   259 			continue;
   260 		}
   261 		if (curTime > player->menuStartTime + player->menuHoldTime)
   262 		{
   263 			_CancelClientMenu(client, MenuCancel_Timeout, false);
   264 		}
   265 	}
   266 }
   267 
   268 void BaseMenuStyle::ClientPressedKey(int client, unsigned int key_press)
   269 {
   270 #if defined MENU_DEBUG
   271 	g_Logger.LogMessage("[SM_MENU] ClientPressedKey() (client %d) (key_press %d)", client, key_press);
   272 #endif
   273 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   274 
   275 	/* First question: Are we in a menu? */
   276 	if (!player->bInMenu)
   277 	{
   278 		return;
   279 	}
   280 
   281 	bool cancel = false;
   282 	unsigned int item = 0;
   283 	MenuCancelReason reason = MenuCancel_Exit;
   284 	MenuEndReason end_reason = MenuEnd_Selected;
   285 	menu_states_t &states = player->states;
   286 
   287 	/* Save variables */
   288 	IMenuHandler *mh = states.mh;
   289 	IBaseMenu *menu = states.menu;
   290 
   291 	unsigned int item_on_page = states.item_on_page;
   292 
   293 	assert(mh != NULL);
   294 
   295 	if (menu == NULL)
   296 	{
   297 		item = key_press;
   298 	} else if (key_press < 1 || key_press > GetMaxPageItems()) {
   299 		cancel = true;
   300 	} else {
   301 		ItemSelection type = states.slots[key_press].type;
   302 
   303 		/* Check if we should play a sound about the type */
   304 		if (g_Menus.MenuSoundsEnabled() && 
   305 			(!menu || (menu->GetMenuOptionFlags() & MENUFLAG_NO_SOUND) != MENUFLAG_NO_SOUND))
   306 		{
   307 			CellRecipientFilter filter;
   308 			cell_t clients[1];
   309 
   310 			clients[0] = client;
   311 			filter.Initialize(clients, 1);
   312 
   313 			const char *sound = g_Menus.GetMenuSound(type);
   314 
   315 			if (sound != NULL)
   316 			{
   317 				edict_t *pEdict = engine->PEntityOfEntIndex(client);
   318 				if (pEdict)
   319 				{
   320 					ICollideable *pCollideable = pEdict->GetCollideable();
   321 
   322 					if (pCollideable)
   323 					{
   324 						const Vector & pos = pCollideable->GetCollisionOrigin();
   325 		
   326 						enginesound->EmitSound(filter, 
   327 							client, 
   328 							CHAN_AUTO, 
   329 							sound, 
   330 							VOL_NORM, 
   331 							ATTN_NORM, 
   332 							0, 
   333 							PITCH_NORM, 
   334 							&pos);
   335 					}
   336 				}
   337 			}
   338 		}
   339 
   340 		/* For navigational items, we're going to redisplay */
   341 		if (type == ItemSel_Back)
   342 		{
   343 			if (!RedoClientMenu(client, ItemOrder_Descending))
   344 			{
   345 				cancel = true;
   346 				reason = MenuCancel_NoDisplay;
   347 				end_reason = MenuEnd_Cancelled;
   348 			} else {
   349 				return;
   350 			}
   351 		} else if (type == ItemSel_Next) {
   352 			if (!RedoClientMenu(client, ItemOrder_Ascending))
   353 			{
   354 				cancel = true;						/* I like Saltines. */
   355 				reason = MenuCancel_NoDisplay;
   356 				end_reason = MenuEnd_Cancelled;
   357 			} else {
   358 				return;
   359 			}
   360 		} else if (type == ItemSel_Exit || type == ItemSel_None) {
   361 			cancel = true;
   362 			reason = MenuCancel_Exit;
   363 			end_reason = MenuEnd_Exit;
   364 		} else if (type == ItemSel_ExitBack) {
   365 			cancel = true;
   366 			reason = MenuCancel_ExitBack;
   367 			end_reason = MenuEnd_ExitBack;
   368 		} else {
   369 			item = states.slots[key_press].item;
   370 		}
   371 	}
   372 
   373 	/* Clear states */
   374 	player->bInMenu = false;
   375 	if (player->menuHoldTime)
   376 	{
   377 		RemoveClientFromWatch(client);
   378 	}
   379 
   380 	if (cancel)
   381 	{
   382 		mh->OnMenuCancel(menu, client, reason);
   383 	} else {
   384 		mh->OnMenuSelect(menu, client, item);
   385 		if (mh->GetMenuAPIVersion2() >= 13)
   386 		{
   387 			mh->OnMenuSelect2(menu, client, item, item_on_page);
   388 		}
   389 	}
   390 
   391 	/* Only fire end for valid menus */
   392 	if (menu)
   393 	{
   394 		mh->OnMenuEnd(menu, end_reason);
   395 	}
   396 }
   397 
   398 bool BaseMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
   399 {
   400 #if defined MENU_DEBUG
   401 	g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (panel %p) (mh %p) (time %d)",
   402 		client,
   403 		menu,
   404 		mh,
   405 		time);
   406 #endif
   407 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   408 	if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
   409 	{
   410 		return false;
   411 	}
   412 
   413 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   414 	if (player->bAutoIgnore)
   415 	{
   416 		return false;
   417 	}
   418 
   419 	/* For the duration of this, we are going to totally ignore whether
   420 	* the player is already in a menu or not (except to cancel the old one).
   421 	* Instead, we are simply going to ignore any further menu displays, so
   422 	* this display can't be interrupted.
   423 	*/
   424 	player->bAutoIgnore = true;
   425 
   426 	/* Cancel any old menus */
   427 	menu_states_t &states = player->states;
   428 	if (player->bInMenu)
   429 	{
   430 		_CancelClientMenu(client, MenuCancel_Interrupted, true);
   431 	}
   432 
   433 	states.firstItem = 0;
   434 	states.lastItem = 0;
   435 	states.menu = NULL;
   436 	states.mh = mh;
   437 	states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
   438 	player->bInMenu = true;
   439 	player->bInExternMenu = false;
   440 	player->menuStartTime = gpGlobals->curtime;
   441 	player->menuHoldTime = time;
   442 
   443 	if (time)
   444 	{
   445 		AddClientToWatch(client);
   446 	}
   447 
   448 	/* Draw the display */
   449 	SendDisplay(client, menu);
   450 
   451 	/* We can be interrupted again! */
   452 	player->bAutoIgnore = false;
   453 
   454 	return true;
   455 }
   456 
   457 bool BaseMenuStyle::DoClientMenu(int client,
   458 								 CBaseMenu *menu,
   459 								 unsigned int first_item,
   460 								 IMenuHandler *mh,
   461 								 unsigned int time)
   462 {
   463 #if defined MENU_DEBUG
   464 	g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (menu %p) (mh %p) (time %d)",
   465 		client,
   466 		menu,
   467 		mh,
   468 		time);
   469 #endif
   470 	mh->OnMenuStart(menu);
   471 
   472 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   473 	if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
   474 	{
   475 #if defined MENU_DEBUG
   476 		g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to display to client %d", client);
   477 #endif
   478 		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
   479 		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
   480 		return false;
   481 	}
   482 
   483 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   484 	if (player->bAutoIgnore)
   485 	{
   486 #if defined MENU_DEBUG
   487 		g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Client %d is autoIgnoring", client);
   488 #endif
   489 		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
   490 		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
   491 		return false;
   492 	}
   493 
   494 	/* For the duration of this, we are going to totally ignore whether
   495 	 * the player is already in a menu or not (except to cancel the old one).
   496 	 * Instead, we are simply going to ignore any further menu displays, so
   497 	 * this display can't be interrupted.
   498 	 */
   499 	player->bAutoIgnore = true;
   500 
   501 	/* Cancel any old menus */
   502 	menu_states_t &states = player->states;
   503 	if (player->bInMenu)
   504 	{
   505 #if defined MENU_DEBUG
   506 		g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Cancelling old menu to client %d", client);
   507 #endif
   508 		_CancelClientMenu(client, MenuCancel_Interrupted, true);
   509 	}
   510 
   511 	states.firstItem = 0;
   512 	states.lastItem = first_item;
   513 	states.menu = menu;
   514 	states.mh = mh;
   515 	states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
   516 
   517 	IMenuPanel *display = g_Menus.RenderMenu(client, states, ItemOrder_Ascending);
   518 	if (!display)
   519 	{
   520 #if defined MENU_DEBUG
   521 		g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to render to client %d", client);
   522 #endif
   523 		player->bAutoIgnore = false;
   524 		player->bInMenu = false;
   525 		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
   526 		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
   527 		return false;
   528 	}
   529 
   530 	/* Finally, set our states */
   531 	player->bInMenu = true;
   532 	player->bInExternMenu = false;
   533 	player->menuStartTime = gpGlobals->curtime;
   534 	player->menuHoldTime = time;
   535 
   536 	if (time)
   537 	{
   538 		AddClientToWatch(client);
   539 	}
   540 
   541 	/* Draw the display */
   542 	SendDisplay(client, display);
   543 
   544 	/* Free the display pointer */
   545 	display->DeleteThis();
   546 
   547 	/* We can be interrupted again! */
   548 	player->bAutoIgnore = false;
   549 
   550 #if defined MENU_DEBUG
   551 	g_Logger.LogMessage("[SM_MENU] DoClientMenu() finished successfully (client %d)", client);
   552 #endif
   553 
   554 	return true;
   555 }
   556 
   557 bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
   558 {
   559 #if defined MENU_DEBUG
   560 	g_Logger.LogMessage("[SM_MENU] RedoClientMenu() (client %d) (order %d)", client, order);
   561 #endif
   562 	CBaseMenuPlayer *player = GetMenuPlayer(client);
   563 	menu_states_t &states = player->states;
   564 
   565 	player->bAutoIgnore = true;
   566 	IMenuPanel *display = g_Menus.RenderMenu(client, states, order);
   567 	if (!display)
   568 	{
   569 #if defined MENU_DEBUG
   570 		g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Failed to render menu");
   571 #endif
   572 		if (player->menuHoldTime)
   573 		{
   574 			RemoveClientFromWatch(client);
   575 		}
   576 		player->bAutoIgnore = false;
   577 		return false;
   578 	}
   579 
   580 	SendDisplay(client, display);
   581 
   582 	display->DeleteThis();
   583 
   584 	player->bAutoIgnore = false;
   585 
   586 #if defined MENU_DEBUG
   587 	g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Succeeded to client %d", client);
   588 #endif
   589 
   590 	return true;
   591 }
   592 
   593 CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) : 
   594 m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false), 
   595 m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false), 
   596 m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT)
   597 {
   598 }
   599 
   600 CBaseMenu::~CBaseMenu()
   601 {
   602 }
   603 
   604 Handle_t CBaseMenu::GetHandle()
   605 {
   606 	if (!m_hHandle)
   607 	{
   608 		m_hHandle = g_Menus.CreateMenuHandle(this, m_pOwner);
   609 	}
   610 
   611 	return m_hHandle;
   612 }
   613 
   614 bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
   615 {
   616 	if (m_Pagination == (unsigned)MENU_NO_PAGINATION
   617 		&& m_items.size() >= m_pStyle->GetMaxPageItems())
   618 	{
   619 		return false;
   620 	}
   621 
   622 	CItem item;
   623 
   624 	item.infoString = m_Strings.AddString(info);
   625 	if (draw.display)
   626 	{
   627 		item.displayString = m_Strings.AddString(draw.display);
   628 	}
   629 	item.style = draw.style;
   630 
   631 	m_items.push_back(item);
   632 
   633 	return true;
   634 }
   635 
   636 bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
   637 {
   638 	if (m_Pagination == (unsigned)MENU_NO_PAGINATION
   639 		&& m_items.size() >= m_pStyle->GetMaxPageItems())
   640 	{
   641 		return false;
   642 	}
   643 
   644 	if (position >= m_items.size())
   645 	{
   646 		return false;
   647 	}
   648 
   649 	CItem item;
   650 	item.infoString = m_Strings.AddString(info);
   651 	if (draw.display)
   652 	{
   653 		item.displayString = m_Strings.AddString(draw.display);
   654 	}
   655 	item.style = draw.style;
   656 
   657 	CVector<CItem>::iterator iter = m_items.iterAt(position);
   658 	m_items.insert(iter, item);
   659 
   660 	return true;
   661 }
   662 
   663 bool CBaseMenu::RemoveItem(unsigned int position)
   664 {
   665 	if (position >= m_items.size())
   666 	{
   667 		return false;
   668 	}
   669 
   670 	m_items.erase(m_items.iterAt(position));
   671 
   672 	if (m_items.size() == 0)
   673 	{
   674 		m_Strings.Reset();
   675 	}
   676 
   677 	return true;
   678 }
   679 
   680 void CBaseMenu::RemoveAllItems()
   681 {
   682 	m_items.clear();
   683 	m_Strings.Reset();
   684 }
   685 
   686 const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
   687 {
   688 	if (position >= m_items.size())
   689 	{
   690 		return NULL;
   691 	}
   692 
   693 	if (draw)
   694 	{
   695 		draw->display = m_Strings.GetString(m_items[position].displayString);
   696 		draw->style = m_items[position].style;
   697 	}
   698 
   699 	return m_Strings.GetString(m_items[position].infoString);
   700 }
   701 
   702 unsigned int CBaseMenu::GetItemCount()
   703 {
   704 	return m_items.size();
   705 }
   706 
   707 bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
   708 {
   709 	if (itemsPerPage > 7 || itemsPerPage == 1)
   710 	{
   711 		return false;
   712 	}
   713 
   714 	if (itemsPerPage == MENU_NO_PAGINATION
   715 		&& m_Pagination != MENU_NO_PAGINATION)
   716 	{
   717 		m_nFlags &= ~MENUFLAG_BUTTON_EXIT;
   718 	}
   719 
   720 	m_Pagination = itemsPerPage;
   721 
   722 	return true;
   723 }
   724 
   725 unsigned int CBaseMenu::GetPagination()
   726 {
   727 	return m_Pagination;
   728 }
   729 
   730 IMenuStyle *CBaseMenu::GetDrawStyle()
   731 {
   732 	return m_pStyle;
   733 }
   734 
   735 void CBaseMenu::SetDefaultTitle(const char *message)
   736 {
   737 	m_Title.assign(message);
   738 }
   739 
   740 const char *CBaseMenu::GetDefaultTitle()
   741 {
   742 	return m_Title.c_str();
   743 }
   744 
   745 void CBaseMenu::Cancel()
   746 {
   747 	if (m_bCancelling)
   748 	{
   749 		return;
   750 	}
   751 
   752 #if defined MENU_DEBUG
   753 	g_Logger.LogMessage("[SM_MENU] CBaseMenu::Cancel(%p) (m_bShouldDelete %d)",
   754 		this,
   755 		m_bShouldDelete);
   756 #endif
   757 
   758 	m_bCancelling = true;
   759 	Cancel_Finally();
   760 	m_bCancelling = false;
   761 
   762 	if (m_bShouldDelete)
   763 	{
   764 		InternalDelete();
   765 	}
   766 }
   767 
   768 void CBaseMenu::Destroy(bool releaseHandle)
   769 {
   770 	/* Check if we shouldn't be here */
   771 	if (m_bDeleting)
   772 	{
   773 		return;
   774 	}
   775 
   776 #if defined MENU_DEBUG
   777 	g_Logger.LogMessage("[SM_MENU] CBaseMenu::Destroy(%p) (release %d) (m_bCancelling %d) (m_bShouldDelete %d)",
   778 		this,
   779 		releaseHandle,
   780 		m_bCancelling,
   781 		m_bShouldDelete);
   782 #endif
   783 
   784 	/* Save the destruction hint about our handle */
   785 	m_bWillFreeHandle = releaseHandle;
   786 
   787 	/* Now actually do stuff */
   788 	if (!m_bCancelling || m_bShouldDelete)
   789 	{
   790 		Cancel();
   791 		InternalDelete();
   792 	} else {
   793 		m_bShouldDelete = true;
   794 	}
   795 }
   796 
   797 void CBaseMenu::InternalDelete()
   798 {
   799 	if (m_bWillFreeHandle && m_hHandle != BAD_HANDLE)
   800 	{
   801 		Handle_t hndl = m_hHandle;
   802 		HandleSecurity sec;
   803 
   804 		sec.pOwner = m_pOwner;
   805 		sec.pIdentity = g_pCoreIdent;
   806 
   807 		m_hHandle = BAD_HANDLE;
   808 		m_bDeleting = true;
   809 		g_HandleSys.FreeHandle(hndl, &sec);
   810 	}
   811 
   812 	m_pHandler->OnMenuDestroy(this);
   813 
   814 	delete this;
   815 }
   816 
   817 unsigned int CBaseMenu::GetMenuOptionFlags()
   818 {
   819 	return m_nFlags;
   820 }
   821 
   822 void CBaseMenu::SetMenuOptionFlags(unsigned int flags)
   823 {
   824 	m_nFlags = flags;
   825 }
   826 
   827 IMenuHandler *CBaseMenu::GetHandler()
   828 {
   829 	return m_pHandler;
   830 }