⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basewnd.cpp

📁 C++实现的brew程序管理器例子
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////
// Project : AppManager
// File: BaseWnd.cpp
// UI module of this project
// 2005/07/26
/////////////////////////////////////////////////////////////////////

//---------------- Inculde ------------------
#include "BaseWnd.h"
#include ".\basewnd.h"
//-------------------------------------------

//---------------- Define -------------------
#define BASEWND_SOFTKEY_HEIGHT	22
#define BASEWND_TITLE_HEIGHT	22
#define BASEWND_BORDER_WIDTH	1
//-------------------------------------------


CBaseWnd::CBaseWnd(uint16 wType)
{
	//save parameter
	if(wType == BASEWND_TYPE_STATIC || wType == BASEWND_TYPE_MENU || wType == BASEWND_TYPE_DOUBLE)
	{
		m_wWindowType = wType;
	}
	else
	{
		m_wWindowType = BASEWND_TYPE_STATIC;
	}
	//Initialize property
	m_pImgTitle = NULL;
	m_pTextTitle = NULL;
	m_pSoftKeyMenu = new CMenuCtl();
	if(wType == BASEWND_TYPE_STATIC)
	{
		m_pStatic = new CStatic();
		m_pMenuCtl = NULL;
		m_pMenuOption = NULL;
	}
	else if(wType == BASEWND_TYPE_MENU)
	{
		m_pMenuCtl = new CMenuCtl();
		m_pStatic = NULL;
		m_pMenuOption = NULL;
	}
	else
	{
		m_pMenuCtl = new CMenuCtl();
		m_pMenuOption = new CMenuCtl();
		m_pStatic = NULL;
	}
}

CBaseWnd::~CBaseWnd(void)
{
	//Free static control
	if(m_pStatic)
	{
		delete m_pStatic;
	}
	//Free menu control
	if(m_pMenuCtl)
	{
		delete m_pMenuCtl;
	}
	if(m_pMenuOption)
	{
		delete m_pMenuOption;
	}
	//Free soft key control
	if(m_pSoftKeyMenu)
	{
		delete m_pSoftKeyMenu;
	}
	//Free title image 
	if(m_pImgTitle)
	{
		delete m_pImgTitle;
	}
	//Free title text
	if(m_pTextTitle)
	{
		FREE(m_pTextTitle);
	}
}

// Function to create base window
boolean CBaseWnd::Create(CShell* pShell, CDisplay *pDisplay, const char *pszResFile, boolean bShow)
{
	if((!m_pMenuCtl && !m_pStatic) || !m_pSoftKeyMenu)
	{
		return FALSE;
	}
	// call base function
	if(!CWnd::Create(pShell, pDisplay, pszResFile, bShow))
	{
		return FALSE;
	}
	
	// create static control
	if(m_wWindowType == BASEWND_TYPE_STATIC)
	{
		if(m_pShell->CreateInstance(AEECLSID_STATIC, m_pStatic) != SUCCESS)
		{
			return FALSE;
		}
		//static rect
		m_pStatic->SetRect(BASEWND_BORDER_WIDTH, 
			BASEWND_BORDER_WIDTH + BASEWND_TITLE_HEIGHT, 
			m_cxScreen - (BASEWND_BORDER_WIDTH << 1), 
			m_cyScreen - BASEWND_SOFTKEY_HEIGHT - BASEWND_TITLE_HEIGHT - (BASEWND_BORDER_WIDTH << 1));
		//
		m_pStatic->SetProperties(ST_UNDERLINE | ST_CENTERTITLE |  ST_MIDDLETEXT);
	}
	
	//AEE Item style for menuctl
	AEEItemStyle itemNormalStyle, itemSelStyle;
	itemNormalStyle.ft = AEE_FT_3D_EMPTY;
	itemNormalStyle.roImage = AEE_RO_TRANSPARENT;
	itemNormalStyle.xOffset = 0;
	itemNormalStyle.yOffset = 0;
	itemSelStyle.ft = AEE_FT_RAISED;
	itemSelStyle.roImage = AEE_RO_TRANSPARENT;
	itemSelStyle.xOffset = 0;
	itemSelStyle.yOffset = 0;
	
	// create menu control
	if(m_wWindowType == BASEWND_TYPE_MENU)
	{
		if(m_pShell->CreateInstance(AEECLSID_MENUCTL, m_pMenuCtl) != SUCCESS)
		{
			return FALSE;
		}
		//set menu rect	
		m_pMenuCtl->SetRect(BASEWND_BORDER_WIDTH, 
			BASEWND_BORDER_WIDTH + BASEWND_TITLE_HEIGHT, 
			m_cxScreen - (BASEWND_BORDER_WIDTH << 1), 
			m_cyScreen - BASEWND_TITLE_HEIGHT - BASEWND_SOFTKEY_HEIGHT - (BASEWND_BORDER_WIDTH << 1));
		//set menu style
		m_pMenuCtl->SetStyle(&itemNormalStyle, &itemSelStyle);
	}

	//双菜单
	if(m_wWindowType == BASEWND_TYPE_DOUBLE)
	{
		if(m_pShell->CreateInstance(AEECLSID_MENUCTL, m_pMenuCtl) != SUCCESS)
		{
			return FALSE;
		}
		//set menu rect	
		m_pMenuCtl->SetRect(BASEWND_BORDER_WIDTH, 
			BASEWND_BORDER_WIDTH + BASEWND_TITLE_HEIGHT, 
			m_cxScreen - (BASEWND_BORDER_WIDTH << 1) - 18, 
			m_cyScreen - BASEWND_TITLE_HEIGHT - BASEWND_SOFTKEY_HEIGHT - (BASEWND_BORDER_WIDTH << 1));
		//set menu style
		m_pMenuCtl->SetStyle(&itemNormalStyle, &itemSelStyle);
		if(m_pShell->CreateInstance(AEECLSID_MENUCTL, m_pMenuOption) != SUCCESS)
		{
			return FALSE;
		}
		//set menu rect	
		m_pMenuOption->SetRect(m_cxScreen - 18, 
			BASEWND_BORDER_WIDTH + BASEWND_TITLE_HEIGHT, 
			18, 
			m_cyScreen - BASEWND_TITLE_HEIGHT - BASEWND_SOFTKEY_HEIGHT - (BASEWND_BORDER_WIDTH << 1));
		//set menu style
		m_pMenuOption->SetStyle(&itemNormalStyle, &itemSelStyle);
	}

	// create soft key control
	if(m_pShell->CreateInstance(AEECLSID_SOFTKEYCTL, m_pSoftKeyMenu) != SUCCESS)
	{
		return FALSE;
	}
	//set softkey menu rect
	m_pSoftKeyMenu->SetRect(BASEWND_BORDER_WIDTH, m_cyScreen - BASEWND_SOFTKEY_HEIGHT - BASEWND_BORDER_WIDTH, 
		m_cxScreen - BASEWND_BORDER_WIDTH * 2, BASEWND_SOFTKEY_HEIGHT);
	//set softkey style
	m_pSoftKeyMenu->SetStyle(&itemNormalStyle, &itemSelStyle);

	m_wActiveCtl = BASEWND_ACTIVE_BODY;
	return TRUE;
}

// Display base window
void CBaseWnd::Update(CDisplay *pDisplay)
{
	
	if(pDisplay)
	{
		
		//Draw base window frame	
		AEERect rect;
		rect.x = 0;
		rect.y = 0;
		rect.dx = m_cxScreen/* - BASEWND_BORDER_WIDTH*/;
		rect.dy = m_cyScreen/* - BASEWND_SOFTKEY_HEIGHT - BASEWND_BORDER_WIDTH*/;
		pDisplay->DrawFrame(&rect, AEE_FT_BOX, MAKE_RGB(255, 255, 255));
		//Draw window title
		pDisplay->SetColor(CLR_USER_TEXT, MAKE_RGB(0, 0, 0xFF));
		int nTextWidth = 0;
		int nImgWidth = 0;
		if(m_pTextTitle)
		{
			nTextWidth = pDisplay->MeasureText(AEE_FONT_BOLD, m_pTextTitle);
			pDisplay->DrawText(AEE_FONT_BOLD, m_pTextTitle, -1, 0, BASEWND_BORDER_WIDTH + 1, NULL, IDF_ALIGN_CENTER);
		}
		if(m_pImgTitle)
		{
			AEEImageInfo imgInfo;
			m_pImgTitle->GetInfo(&imgInfo);
			nImgWidth =imgInfo.cx;
			uint16 imgX = ((m_cxScreen - nTextWidth) >> 1) - nImgWidth ;
			m_pImgTitle->Draw(imgX, BASEWND_BORDER_WIDTH + 1);
		}
		pDisplay->DrawHLine(0, BASEWND_TITLE_HEIGHT, m_cxScreen);
		pDisplay->DrawHLine(0, m_cyScreen - BASEWND_SOFTKEY_HEIGHT, m_cxScreen);
		//pDisplay->Update();
		pDisplay->SetColor(CLR_USER_TEXT, MAKE_RGB(0, 0, 0));
	}

	//Draw static control or menu control
	if(m_wWindowType == BASEWND_TYPE_STATIC)
	{
		m_pStatic->Redraw();
	}
	else if(m_wWindowType == BASEWND_TYPE_MENU)
	{
		m_pMenuCtl->Redraw();
	}
	else if(m_wWindowType == BASEWND_TYPE_DOUBLE)
	{
		m_pMenuCtl->Redraw();
		m_pMenuOption->Redraw();
	}	

	//draw soft key
	m_pSoftKeyMenu->Redraw();
}

// Event handler of base window
boolean CBaseWnd::HandleEvent(AEEEvent evt, uint16 wParam, uint32 dwParam)
{
	CControl *pControl = NULL;
	//
	if(evt == EVT_APP_SUSPEND)			//suspended?
	{
		Pause();
		return TRUE;
	}
	else if(evt == EVT_APP_RESUME)		//resumed?
	{
		Resume();
		return TRUE;
	}

	boolean bCtlEventReturn = FALSE;
	if(m_wActiveCtl == BASEWND_ACTIVE_BODY)
	{
		if(m_wWindowType == BASEWND_TYPE_STATIC)
		{
			bCtlEventReturn = m_pStatic->HandleEvent(evt, wParam, dwParam);
		}
		else if(m_wWindowType == BASEWND_TYPE_MENU)
		{
			bCtlEventReturn = m_pMenuCtl->HandleEvent(evt, wParam, dwParam);
		}
		else if(m_wWindowType == BASEWND_TYPE_DOUBLE)
		{

		}
	}
	else
	{
		bCtlEventReturn = m_pSoftKeyMenu->HandleEvent(evt, wParam, dwParam);
	}
	//Control have handled this event?
	if(bCtlEventReturn)
		return TRUE;

	switch(evt)
	{
	case EVT_CTL_TAB:
		if(m_wActiveCtl == BASEWND_ACTIVE_BODY)
		{
			m_wActiveCtl = BASEWND_ACTIVE_SOFTKEY;
			if(m_wWindowType == BASEWND_TYPE_STATIC)
			{
				m_pStatic->SetActive(FALSE);
			}
			else if(m_wWindowType == BASEWND_TYPE_MENU)
			{
				m_pMenuCtl->SetActive(FALSE);
			}
			m_pSoftKeyMenu->SetActive(TRUE);
		}
		else
		{
			m_wActiveCtl = BASEWND_ACTIVE_BODY;
			if(m_wWindowType == BASEWND_TYPE_STATIC)
			{
				m_pStatic->SetActive(TRUE);
			}
			else if(m_wWindowType == BASEWND_TYPE_MENU)
			{
				m_pMenuCtl->SetActive(TRUE);
			}
			m_pSoftKeyMenu->SetActive(FALSE);
		}
		return TRUE;
	case EVT_KEY:
		//Get active control
		if(m_wActiveCtl == BASEWND_ACTIVE_BODY)
		{
			if(m_wWindowType == BASEWND_TYPE_STATIC)
			{
				pControl = m_pStatic;
			}
			else if(m_wWindowType == BASEWND_TYPE_MENU)
			{
				pControl = m_pMenuCtl;
			}
		}
		else if(m_wActiveCtl == BASEWND_ACTIVE_SOFTKEY)
		{
			pControl = m_pSoftKeyMenu;
		}
		//valid?
		if(!pControl)
			return FALSE;
		//send tab event
		if(wParam == AVK_UP || wParam == AVK_LEFT)
		{
			return m_pShell->HandleEvent(EVT_CTL_TAB, 0, (uint32)pControl);
		}
		else if(wParam == AVK_DOWN || wParam == AVK_RIGHT)
		{
			return m_pShell->HandleEvent(EVT_CTL_TAB, 1, (uint32)pControl);
		}
	}

	return FALSE;
}

// Pause the window
void CBaseWnd::Pause()
{
	if(m_wActiveCtl == BASEWND_ACTIVE_BODY)
	{
		if(m_wWindowType == BASEWND_TYPE_STATIC)
		{
			m_pStatic->SetActive(FALSE);
		}
		else if(m_wWindowType == BASEWND_TYPE_MENU)
		{
			m_pMenuCtl->SetActive(FALSE);
		}
	}
	else
	{
		m_pSoftKeyMenu->SetActive(FALSE);
	}
	CWnd::Pause();
}

// Resume the window
void CBaseWnd::Resume()
{
	if(m_wActiveCtl == BASEWND_ACTIVE_BODY)
	{
		if(m_wWindowType == BASEWND_TYPE_STATIC)
		{
			m_pStatic->SetActive(TRUE);
		}
		else if(m_wWindowType == BASEWND_TYPE_MENU)
		{
			m_pMenuCtl->SetActive(TRUE);
		}
	}
	else
	{
		m_pSoftKeyMenu->SetActive(TRUE);
	}
	CWnd::Resume();
}

// Set text of the static control, return success or not.
// pwszText : The AECHAR string to set, if the string is NULL, it will clear static control
boolean CBaseWnd::SetText(AECHAR *pwszText,  AECHAR *pwszTitle)
{
	if(!m_pStatic)
	{
		return FALSE;
	}	
	
	return m_pStatic->SetText(pwszTitle, pwszText);
}

// Add a menu item to base window
// pszResFile: Resource file name
// wResID: Item text resource ID
// nItemID: Item ID
// pText: Item text string
// lData: Item data
boolean CBaseWnd::AddItem(const char * pszResFile, uint16 wResID, uint16 nItemID, AECHAR * pText, uint32 lData)
{
	if(!m_pSoftKeyMenu)
	{
		return FALSE;
	}
	return m_pMenuCtl->AddItem(pszResFile, wResID, nItemID, pText, lData);
}

// Extend function to add a menu item to base window
// pai: Pointer of type CtlAddItem to add menu item.
boolean CBaseWnd::AddItemEx(CtlAddItem * pai)
{
	if(!m_pMenuCtl || !pai)
	{
		return FALSE;
	}
	return m_pMenuCtl->AddItemEx(pai);
}

// Extend function to add a menu item to base window
// pszResFile: Resource file name
// wResImgID: Item icon resource ID
// nItemID: Item ID
// pText: Item text string
// lData: Item data
boolean CBaseWnd::AddItemEx(const char * pszResFile, uint16 wResImgID, uint16 wResTextID, uint16 nItemID, AECHAR *pText, uint32 lData)
{
	CtlAddItem *pai;
	boolean rtn = FALSE;
	pai = (CtlAddItem*)MALLOC(sizeof(CtlAddItem));
	if(pai)
	{
		pai->wItemID = nItemID;
		pai->dwData = lData;
		pai->wImage = wResImgID;
		pai->wText = wResTextID;
		pai->pszResImage = pszResFile;
		pai->pszResText = pszResFile;
		pai->pText = pText;
	//	pai->wFont = AEE_FONT_NORMAL;
		rtn = AddItemEx(pai);
		FREE(pai);
	}
	
	if(!rtn)
		return AddItem(NULL, NULL, nItemID, pText, lData);
	return TRUE;
	
}

//add option item
boolean CBaseWnd::AddItemOption(const char *pszResFile, uint16 wResImgID, uint16 nTextID, uint16 nItemID, uint32 lData)
{
	CtlAddItem *pai;
	boolean rtn = FALSE;
	if(!m_pMenuOption)
		return FALSE;
	pai = (CtlAddItem*)MALLOC(sizeof(CtlAddItem));
	if(pai)
	{
		pai->wItemID = nItemID;
		pai->dwData = lData;
		pai->wImage = wResImgID;
		pai->wText = nTextID;
		pai->pszResImage = pszResFile;
		pai->pszResText = pszResFile;
		rtn = m_pMenuOption->AddItemEx(pai);
		FREE(pai);
	}
	
	return rtn;
	
}

// Delete a special menu item
// nID: ID of the item to be deleted
boolean CBaseWnd::DeleteItem(uint16 nItemID)
{
	if(!m_pMenuCtl)
	{
		return FALSE;
	}
	return m_pMenuCtl->DeleteItem(nItemID);
}

// Delete all menu items
boolean CBaseWnd::DeleteAllItems()
{
	if(!m_pSoftKeyMenu)
	{
		return FALSE;
	}
	return m_pSoftKeyMenu->DeleteAll();
}

// Add a soft key item to base window
// pszResFile: Resource file name
// wResID: Item text resource ID
// nItemID: Item ID
// pText: Item text string
// lData: Item data
boolean CBaseWnd::AddSoftkey(const char * pszResFile, uint16 wResID, uint16 nItemID, AECHAR * pText, uint32 lData)
{
	if(!m_pSoftKeyMenu)
	{
		return FALSE;
	}
	//m_pSoftKeyMenu->SetActive(TRUE);
	//m_wActiveCtl = BASEWND_ACTIVE_SOFTKEY;
	return m_pSoftKeyMenu->AddItem(pszResFile, wResID, nItemID, pText, lData);
	
}

// Extend function to add a soft key item to base window
// pai: Pointer of type CtlAddItem to add soft key item.
boolean CBaseWnd::AddSoftkeyEx(CtlAddItem * pai)
{
	if(!m_pSoftKeyMenu || !pai)
	{
		return FALSE;
	}
	//m_pSoftKeyMenu->SetActive(TRUE);
	//m_wActiveCtl = BASEWND_ACTIVE_SOFTKEY;
	return m_pSoftKeyMenu->AddItemEx(pai);
}

// Delete a special soft key item
// nID: ID of the item to be deleted
boolean CBaseWnd::DeleteSoftkey(uint16 nItemID)
{
	if(!m_pSoftKeyMenu)
	{
		return FALSE;
	}
	return m_pSoftKeyMenu->DeleteItem(nItemID);
}

// Delete all soft key items
boolean CBaseWnd::DeleteAllSoftkeys()
{
	if(!m_pSoftKeyMenu)
	{
		return FALSE;
	}
	return m_pSoftKeyMenu->DeleteAll();
}

//set which key item is focused
boolean CBaseWnd::SetSoftKeySel(uint16 keyID)
{
	if(!m_pSoftKeyMenu)
	{
		return FALSE;
	}
	m_pSoftKeyMenu->SetSel(keyID); 
	return TRUE;
}


// Set base window title item
// pszResFile : Resource file name
// wResTextID : title text res id
// wResImgID
void CBaseWnd::SetWindowTitle(const char * pszResFile , uint16  wResTextID , uint16  wResImgID)
{	
	if(m_pImgTitle)
	{
		delete m_pImgTitle;
		m_pImgTitle = NULL;
	}
	if(m_pTextTitle)
	{
		FREE(m_pTextTitle);
		m_pTextTitle = NULL;
	}
	m_pImgTitle = m_pShell->LoadResImage(pszResFile, wResImgID);
	m_pTextTitle = m_pShell->LoadResString(pszResFile, wResTextID);
}

void CBaseWnd::SetWindowTitle(AECHAR* pTitle)
{
	if(m_pTextTitle)
	{
		FREE(m_pTextTitle);
		m_pTextTitle = NULL;
	}
	m_pTextTitle = (AECHAR*)MALLOC((WSTRLEN(pTitle) + 1) * sizeof(AECHAR));
	WSTRCPY(m_pTextTitle , pTitle);
}

boolean CBaseWnd::GetMenuItemData(uint16 itemID, uint32 *plData)
{
	if(!m_pMenuCtl)
		return FALSE;
	return m_pMenuCtl->GetItemData(itemID, plData);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -