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

📄 scrollbar.c

📁 brew平台下scrollbar控件的实现算法。 scrollbar采用图片背景
💻 C
字号:
/*===========================================================================

  FILE: scrollbar.c
===========================================================================*/


/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h"          // Module interface definitions
#include "AEEAppGen.h"          // Applet interface definitions
#include "AEEShell.h"           // Shell interface definitions
#include <AEEStdLib.h>
#include <AEEImage.h>

#include "scrollbar.bid"

#include "scrollbar.h"

#include "scrollbar.brh"
/*-------------------------------------------------------------------
Applet structure. All variables in here are reference via "pMe->"
-------------------------------------------------------------------*/
typedef struct _scrollbar {
	AEEApplet      a ;	       // First element of this structure must be AEEApplet
    AEEDeviceInfo  DeviceInfo; // always have access to the hardware device information
    IDisplay      *pIDisplay;  // give a standard way to access the Display interface
    IShell        *pIShell;    // give a standard way to access the Shell interface
	
    // add your own variables here...
	ScrollBar   *pBar;
	
	
} scrollbar;

/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static  boolean scrollbar_HandleEvent(scrollbar* pMe, AEEEvent eCode, 
									  uint16 wParam, uint32 dwParam);
boolean scrollbar_InitAppData(scrollbar* pMe);
void    scrollbar_FreeAppData(scrollbar* pMe);


/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */

/*===========================================================================
FUNCTION: AEEClsCreateInstance
===========================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
{
	*ppObj = NULL;
	
	if( ClsId == AEECLSID_SCROLLBAR )
	{
		// Create the applet and make room for the applet structure
		if( AEEApplet_New(sizeof(scrollbar),
			ClsId,
			pIShell,
			po,
			(IApplet**)ppObj,
			(AEEHANDLER)scrollbar_HandleEvent,
			(PFNFREEAPPDATA)scrollbar_FreeAppData) ) // the FreeAppData function is called after sending EVT_APP_STOP to the HandleEvent function
			
		{
			//Initialize applet data, this is called before sending EVT_APP_START
            // to the HandleEvent function
			if(scrollbar_InitAppData((scrollbar*)*ppObj))
			{
				//Data initialized successfully
				return(AEE_SUCCESS);
			}
			else
			{
				//Release the applet. This will free the memory allocated for the applet when
				// AEEApplet_New was called.
				IAPPLET_Release((IApplet*)*ppObj);
				return EFAILED;
			}
			
        } // end AEEApplet_New
		
    }
	
	return(EFAILED);
}


/*===========================================================================
FUNCTION SampleAppWizard_HandleEvent
===========================================================================*/
static boolean scrollbar_HandleEvent(scrollbar* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{  
	AEERect rc;

	if (pMe->pBar && Scrollbar_HandleEvent(pMe->pBar, eCode, wParam, dwParam)) 
	{
		return TRUE;
	}

    switch (eCode) 
	{
        // App is told it is starting up
	case EVT_APP_START:                        
		// Add your code here...
		Scrollbar_New(0, pMe->pIShell, &pMe->pBar);

		ScrollBar_SetLength(pMe->pBar, 2000);
		ScrollBar_SetPos(pMe->pBar, 300);
		ScrollBar_SetStep(pMe->pBar, 100);

		SETAEERECT(&rc, 0, 20, 10, 200);
		ScrollBar_SetRect(pMe->pBar, &rc);

		ScrollBar_Redraw(pMe->pBar);

		return(TRUE);
		
		
        // App is told it is exiting
	case EVT_APP_STOP:
		// Add your code here...
		
      		return(TRUE);
			
			
			// App is being suspended 
	case EVT_APP_SUSPEND:
		// Add your code here...
		
      		return(TRUE);
			
			
			// App is being resumed
	case EVT_APP_RESUME:
		// Add your code here...
		
      		return(TRUE);
			
			
			// An SMS message has arrived for this app. Message is in the dwParam above as (char *)
			// sender simply uses this format "//BREW:ClassId:Message", example //BREW:0x00000001:Hello World
	case EVT_APP_MESSAGE:
		// Add your code here...
		
      		return(TRUE);
			
			// A key was pressed. Look at the wParam above to see which key was pressed. The key
			// codes are in AEEVCodes.h. Example "AVK_1" means that the "1" key was pressed.
	case EVT_KEY:
		// Add your code here...
		
      		return(TRUE);
			
			
			// If nothing fits up to this point then we'll just break out
	default:
		break;
	}
	
	return FALSE;
}


// this function is called when your application is starting up
boolean scrollbar_InitAppData(scrollbar* pMe)
{
    // Get the device information for this handset.
    // Reference all the data by looking at the pMe->DeviceInfo structure
    // Check the API reference guide for all the handy device info you can get
    pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
    ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);
	
    // The display and shell interfaces are always created by
    // default, so we'll asign them so that you can access
    // them via the standard "pMe->" without the "a."
    pMe->pIDisplay = pMe->a.m_pIDisplay;
    pMe->pIShell   = pMe->a.m_pIShell;
	
    // Insert your code here for initializing or allocating resources...
	
	pMe->pBar = NULL;
	
    // if there have been no failures up to this point then return success
    return TRUE;
}

// this function is called when your application is exiting
void scrollbar_FreeAppData(scrollbar* pMe)
{
    // insert your code here for freeing any resources you have allocated...
	
    if (pMe->pBar)
	{
		ScrollBar_Reset(pMe->pBar);
    }
	
	
}

boolean Scrollbar_InitData(ScrollBar *po)
{
	if (NULL == po)
	{
		return FALSE;
	}
	po->pScrlImg = ISHELL_LoadResImage(po->pIShell, SCROLLBAR_RES_FILE, IDI_SCROLLBAR);
	po->pSlpImg  = ISHELL_LoadResImage(po->pIShell, SCROLLBAR_RES_FILE, IDI_SLIP);
	
	if (NULL == po->pScrlImg || NULL == po->pSlpImg)
	{
		return FALSE;
	}
	
	return TRUE;
}

int Scrollbar_New(uint16 nSize, IShell *pIShell, ScrollBar **ppo)
{
	ScrollBar *pThis;
	
	//创建
	pThis = MALLOCREC(ScrollBar);
	if (NULL == pThis)
	{
		return EFAILED;
	}
	MEMSET(pThis, 0, sizeof(ScrollBar));
	
	ISHELL_CreateInstance(pIShell, AEECLSID_DISPLAY, (void**)&pThis->pIDisplay);
	if (NULL == pThis->pIDisplay)
	{
		FREEIF(pThis);
		return EFAILED;
	}
	
	pThis->pIShell = pIShell;
	ISHELL_AddRef(pIShell);
	
	if (!Scrollbar_InitData(pThis))
	{
		ScrollBar_Reset(pThis);
		FREEIF(pThis);
		return EFAILED;
	}
	
	*ppo = pThis;
	return SUCCESS;
}
/*-----------------------------------------------------------
事件处理
-----------------------------------------------------------*/
boolean Scrollbar_HandleEvent(ScrollBar* pMe, AEEEvent eCode, 
							  uint16 wParam, uint32 dwParam)
{
	if (eCode != EVT_KEY)
	{
		return FALSE;
	}

	switch (wParam)
	{
	case AVK_UP:
		ScrollBar_StepIt(pMe, FALSE);
		break;
	case AVK_DOWN:
		ScrollBar_StepIt(pMe, TRUE);
	default:
		break;
	}
	
	return FALSE;
}
/*----------------------------------------------
  用图片填充指定长度矩形,垂直方向
  -----------------------------------------------*/
void V_DrawImgByLength(IImage *pImg, const AEERect *prc)
{
	AEEImageInfo iif;
	uint32 nBarPos, nBarMaxPos;

	IIMAGE_GetInfo(pImg, &iif);

	//符合要求,直接绘制
	if (iif.cy >= prc->dy)
	{
		IIMAGE_SetDrawSize(pImg,iif.cx,prc->dy);
		IIMAGE_Draw(pImg, prc->x, prc->y);
		return;
	}

	//循环绘制
	nBarPos = prc->y;
	nBarMaxPos = (prc->y + prc->dy) - iif.cy;

	while (nBarPos <= nBarMaxPos)
	{
		IIMAGE_Draw(pImg, prc->x, nBarPos);
		nBarPos += iif.cy;
	}
	IIMAGE_Draw(pImg, prc->x, nBarMaxPos);
}
/*-----------------------------------------------------------
  计算滑块大小,公式:

    滑块长度        滚动条长度 
  ------------ = ---------------
   滚动条长度         总长度 
  ----------------------------------------------------------*/
uint32 CalSlipLength(ScrollBar* pMe)
{
	return ( pMe->m_rcRect.dy * pMe->m_rcRect.dy ) / pMe->nLength + 1;
}
/*-----------------------------------------------------------
  计算滑块位置,公式

    滚动条长度      实际位置 
  ------------ = ---------------
      总长度        逻辑位置 
  -----------------------------------------------------------*/
uint32 CalSlipPos(ScrollBar*pMe, uint32 nSlip)
{
    return (pMe->m_rcRect.dy * pMe->nPos) / pMe->nLength;
}
/*------------------------------------------------------------
重绘
------------------------------------------------------------*/
boolean ScrollBar_Redraw(ScrollBar* pMe)
{
	AEERect rc;
	uint32 nSlipLen;
	uint32 nSlipPos;

	if (NULL == pMe || NULL == pMe->pScrlImg || NULL == pMe->pSlpImg)
	{
		return FALSE;
	}
	
	if (pMe->m_rcRect.dx ==0 || pMe->m_rcRect.dy == 0)
	{
		SETAEERECT(&pMe->m_rcRect, 0, 20, SCROLLBAR_WIDTH, 180);
	}

//	IDISPLAY_SetColor(pMe->pIDisplay, CLR_USER_BACKGROUND, MAKE_RGB(255,0,0));
//	IDISPLAY_ClearScreen(pMe->pIDisplay);
	V_DrawImgByLength(pMe->pScrlImg, &pMe->m_rcRect);
	
	nSlipLen = CalSlipLength(pMe);
	nSlipPos = CalSlipPos(pMe, nSlipLen);

	if (nSlipPos  > (uint16) pMe->m_rcRect.dy - nSlipLen)
	{
		nSlipPos = pMe->m_rcRect.dy - nSlipLen;
	}

	SETAEERECT(&rc, pMe->m_rcRect.x+2, 
		       nSlipPos + pMe->m_rcRect.y,
			   SCROLLBAR_WIDTH-4,
			   nSlipLen);

	V_DrawImgByLength(pMe->pSlpImg, &rc);

	IDISPLAY_DrawRect(pMe->pIDisplay, &rc, MAKE_RGB(200,200,200), 0, IDF_RECT_FRAME);
	IDISPLAY_Update(pMe->pIDisplay);
	return TRUE;
}

/*----------------------------------------------------------
销毁所有数据 
----------------------------------------------------------*/
void ScrollBar_Reset(ScrollBar *po)
{
	if (po->pScrlImg) 
	{
		IIMAGE_Release(po->pScrlImg);
	}
	
	if (po->pSlpImg)
	{
		IIMAGE_Release(po->pSlpImg);
	}
	
	if (po->pIDisplay)
	{
		IDISPLAY_Release(po->pIDisplay);
	}
	ISHELL_Release(po->pIShell);
}
/*-------------------------------------------------------
属性操作
-------------------------------------------------------*/
void ScrollBar_SetLength(ScrollBar *po, uint32 nLength)
{
	if (NULL == po)
	{
		return;
	}
	
	po->nLength = nLength;
}
void ScrollBar_SetStep(ScrollBar *po, uint32 nStep)
{
	if (NULL == po)
	{
		return;
	}
	
	po->nStep = nStep;
}
void ScrollBar_SetPos(ScrollBar *po, uint32 nPos)
{
	if (NULL == po)
	{
		return;
	}
	
	po->nPos = nPos;
}
uint32 ScrollBar_GetLength(ScrollBar *po)
{
	if (NULL == po)
	{
		return 0;
	}
	
	return po->nLength;
}
uint32 ScrollBar_GetStep(ScrollBar *po)
{
	if (NULL == po)
	{
		return 0;
	}
	return po->nStep;
	
}
uint32 ScrollBar_GetPos(ScrollBar *po)
{
	if (NULL == po)
	{
		return 0;
	}
	
	return po->nPos;
}
void ScrollBar_SetRect(ScrollBar *po, const AEERect *prc)
{
	if(NULL == po || NULL == prc)
	{
		return ;
	}

	MEMMOVE(&po->m_rcRect, prc, sizeof(AEERect));
}
void ScrollBar_GetRect(ScrollBar *po, AEERect *prc)
{
	if(NULL == po || NULL == prc)
	{
		return ;
	}

	MEMMOVE(prc, &po->m_rcRect, sizeof(AEERect));
}
/*------------------------------------------------------
滑动
------------------------------------------------------*/
void ScrollBar_StepIt(ScrollBar *po, boolean bDown)
{
	int step;
	int newPos;
	uint32 slipLen;
	uint32 nMaxPos;

	if (NULL == po)
	{
		return;
	}

	step =(int)po->nStep;
	if (!bDown)
	{
		step = -step;
	}
	
	slipLen = CalSlipLength(po);

	newPos = po->nPos + step;
	nMaxPos = po->nLength - po->m_rcRect.dy;

	if (newPos > (int)nMaxPos)
	{
		po->nPos = nMaxPos;
	}
	else if ( newPos < 0 )
	{
		po->nPos = 0;
	}
	else
	{
		po->nPos= newPos;
	}
	 
	ScrollBar_Redraw(po);
}	

⌨️ 快捷键说明

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