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

📄 uiutility.c

📁 嵌入工linux开发的源码
💻 C
字号:
/**************************************************************
        INCLUDE FILE
***************************************************************/
#include <pr2k.h>
#include <uiwnd.h>

#include <uiControl.h>
#include <uiViewport.h>
#include <uiUtility.h>
#include <uiGraph.h>
#include <uiScroll.h>
#include <kernel\malloc.h>
//#include <big_gb.res>

#define _DEBUG_UTILITY 0


typedef struct tagCodeTable
{
	unsigned short bigCode;
	unsigned short gbCode;
}TCodeTable; 

//---------------------------------------------------------//
//	Double Linked List
//---------------------------------------------------------//

DList* DList_Create( void ) 
{
	DList *pDList;

	pDList=(DList *)sc_malloc(sizeof(DList));
	if(pDList!=NULL)
	{
		pDList->pPrev=pDList;
		pDList->pNext=pDList;
		pDList->pObj=NULL;
	}
	return pDList;
}

void DList_Delete( DList* pHead ) 
{
	void *pVoid;
	while(pHead->pNext!=pHead)
	{
		pVoid=DList_Remove(pHead,0);
		if(pVoid!=NULL)
		{
			//kernelFree(pVoid);
			sc_free(pVoid);
		}

	}
	sc_free(pHead);
}

int DList_Insert( DList* pHead, int index, void* obj ) 
{
	int i=0;
	DList *pDList,*pCount=pHead;

	do
	{
		if(index==i)
		{
			pDList=(DList *)sc_malloc(sizeof(DList));
			if(pDList==NULL)
			{
				return STATUS_ERR;
			}
			pDList->pObj=obj;

			pDList->pNext=pCount->pNext;
			pDList->pPrev=pCount;
			pCount->pNext->pPrev=pDList;
			pCount->pNext=pDList;
			return STATUS_OK;
		}
		pCount=pCount->pNext;
		i++;
	}while(pCount!=pHead);
	return STATUS_ERR;
}

void* DList_Remove( DList* pHead, int index ) 
{
	int i=0;
	DList *pCount=pHead;
	void *pObj;

	pCount=pCount->pNext;
	while(pCount!=pHead)
	{
		if(index==i)
		{
			pCount->pPrev->pNext=pCount->pNext;
			pCount->pNext->pPrev=pCount->pPrev;
			pObj=pCount->pObj;
			sc_free(pCount);
			return pObj;
		}
		pCount=pCount->pNext;
		i++;
	}
	return NULL;
			
}

int DList_Append( DList* pHead, void* obj ) 
{
	DList *pDList;

	pDList=(DList *)sc_malloc(sizeof(DList));
	if(pDList==NULL)
	{
		return STATUS_ERR;
	}

	pDList->pObj=obj;

	pDList->pNext=pHead;
	pDList->pPrev=pHead->pPrev;
	pHead->pPrev->pNext=pDList;
	pHead->pPrev=pDList;

	return STATUS_OK;

}

int DList_Length( DList* pHead ) 
{
	int i=0;
	DList *pCount=pHead;

	while(pCount->pNext!=pHead)
	{
		i++;
		pCount=pCount->pNext;
	}
	return i;
}

void* DList_GetObject( DList* pHead, int index ) 
{
	int i=0;

	DList *pCount=pHead;

	pCount=pCount->pNext;
	while(pCount!=pHead)
	{
		if(i==index)
		{
			return pCount->pObj;
		}
		i++;
		pCount=pCount->pNext;
	}
	return NULL;
}


/*************************************************************
	Alignment Tools
*************************************************************/

DLL_EXP(short) getAlignCenter(short left, short right, short width)
{
	short ret;

	ret=(left+right+1-width)/2;
	if(ret<left)
	{
		return left;
	}

	return ret;
}

DLL_EXP(short) getAlignRight(short left,short right, short width)
{
	short ret;

	ret=right-width+1;
	if(ret<left)
	{
		return left;
	}

	return ret;
}

/*************************************************************
	Time Format Transfer	
*************************************************************/

DLL_EXP(void) pdaEncodeTime(sysTime now, unsigned long *time)
{
	unsigned long lTime=0;
	unsigned short wTemp=0;

	wTemp|=now.year<<9;
	wTemp|=now.month<<5;
	wTemp|=now.day;
	lTime=wTemp;
	lTime=lTime<<16;

	wTemp=0;
	wTemp|=now.hour<<11;
	wTemp|=now.minute<<5;
	wTemp|=now.second/2;
	lTime|=wTemp;
	*time=lTime;
}

DLL_EXP(void) pdaDecodeTime(unsigned long time, sysTime *now)
{
	
	now->second=(time&0x1f)*2;
	time>>=5;
	now->minute=time&0x3f;
	time>>=6;
	now->hour=time&0x1f;
	time>>=5;
	now->day=time&0x1f;
	time>>=5;
	now->month=time&0x0f;
	time>>=4;
	now->year=time&0x7f;
}


/*************************************************************
	FUNCTION: layoutTextFile
*************************************************************/
//
// 将文字档或字串转成一行n个字元的的格式     
//

void layoutTextFile(char *srcFile, char *destFile, int width) 
{
	return;
}

/*************************************************************
	Coordinate convert
*************************************************************/

static HNDL ghView=NULL;
static int gView_Left=0,gView_Top=0,gView_Right=0,gView_Bottom=0;
static int gConvert_Left=0,gConvert_Top=0;

int  _guiConvertXY(HNDL handle, int *x, int *y)
{
	int x_In=*x,y_In=*y;
	TControlBase *pCtrlBase;
	TGuiControl	*pCtrl;
	TViewport *pVport;
	TWindow *pWin;

	pCtrlBase=(TControlBase *)handle;
	//pVport=(TViewport *)handle;
	pCtrl=(TGuiControl *)handle;

	if(handle==NULL)
	{
		return STATUS_OK;
	}
#if 0
	if(handle==ghView)
	{
		*x+=gConvert_Left;
		*y+=gConvert_Top;
		return STATUS_OK;
	}
#endif
	while(1)
	{
		switch(pCtrlBase->type)
		{
		case CONTROL_WINDOW:
			*x+=pCtrlBase->left;
			*y+=pCtrlBase->top;
		//tong	return STATUS_OK;

			if(pCtrlBase==(TControlBase *)gpTopWindow)
			{
				return STATUS_OK;
			}
			else
			{
				return STATUS_ERR;
			}
			
		case CONTROL_VIEWPORT:
			pVport=(TViewport *)pCtrlBase;
			pWin=(TWindow *)pVport->base.container;
			if(pWin==NULL)
			{
				return STATUS_ERR;
			}
			*x=*x-pVport->iCurrentLeft+pVport->base.left+pWin->left;
			*y=*y-pVport->iCurrentTop+pVport->base.top+pWin->top;
		//tong	return STATUS_OK;
			
			if(pWin==gpTopWindow)
			{
				return STATUS_OK;
			}
			else
			{
				return STATUS_ERR;
			}
			

		default:
			if(pCtrlBase->checkFlag==GUI_CONTROL_CHECK_FLAG)
			{
				*x+=pCtrlBase->left;
				*y+=pCtrlBase->top;
				if(pCtrl->container!=NULL)
				{
					pCtrl=(TGuiControl *)pCtrl->container;
					pCtrlBase=(TControlBase *)pCtrl;
				}
				else if(pCtrl->vportHandle!=NULL)
				{
					pCtrl=(TGuiControl *)pCtrl->vportHandle;
					pCtrlBase=(TControlBase *)pCtrl;
				//	pVport=(TViewport *)pCtrl;
				}
				else
				{
					return STATUS_ERR;
				}

			}
			else
			{
				return STATUS_ERR;
			}
			break;

		}//switch(pCtrlBase->type)
	}//while(1)
	return STATUS_ERR;
}


int _guiGetHandleView(HNDL handle, int *left, int *top, int *right, int *bottom)
{
	static int iRecursion=0;
	static BOOL IsVportScrollbar_H=FALSE,IsVportScrollbar_V=FALSE;
	int iContainer_Left,iContainer_Top,iContainer_Right,iContainer_Bottom;
	int iConvert_Left,iConvert_Top;

	TControlBase *pCtrlBase;
	TGuiControl *pCtrl;
	TViewport *pVport;

	iRecursion++;
	pCtrlBase=(TControlBase *)handle;
	if(handle==NULL)
	{
		*left=0;
		*top=0;
		*right=GUI_SCREEN_WIDTH-1;
		*bottom=GUI_SCREEN_HEIGHT-1;
		//return STATUS_OK;
		goto RETURN_OK;
	}
#if 0
	if(handle==ghView)
	{
		*left=gView_Left;
		*top=gView_Top;
		*right=gView_Right;
		*bottom=gView_Bottom;
		goto RETURN_OK;
	}
#endif
	//get original coordinate
	*left=0;
	*top=0;
	*right=pCtrlBase->right-pCtrlBase->left;
	*bottom=pCtrlBase->bottom-pCtrlBase->top;

	// the handle is window's.if the window have border, subtract border width
	if(pCtrlBase->checkFlag==GUI_WIN_CHECK_FLAG)
	{
		if(pCtrlBase->style&GUIWIN_BORDER)
		{
			*left+=WIN_BORDER_WIDTH;
			*top+=WIN_BORDER_WIDTH;
			*right-=WIN_BORDER_WIDTH;
			*bottom-=WIN_BORDER_WIDTH;
		}
	}
	//the handle is viewport's.subtract viewport width.special deal to the scrollbar of viewport.
	else if(pCtrlBase->checkFlag==GUI_CONTROL_CHECK_FLAG&&pCtrlBase->type==CONTROL_VIEWPORT)
	{
		pVport=(TViewport *)handle;
		
		*left+=pVport->iCurrentLeft;
		*top+=pVport->iCurrentTop;
		*right+=pVport->iCurrentLeft;
		*bottom+=pVport->iCurrentTop;

		if(!(pVport->base.style&VPORT_NONE_BORDER))
		{
			*left+=1;
			*top+=1;
			*right-=1;
			*bottom-=1;
		}

		if(pVport->hVScroll&&IsVportScrollbar_V==FALSE)
		{
			*right-=_SCROLLBAR_WIDTH_;
			if(pVport->base.style&VPORT_NONE_BORDER)
			{
				*right-=1;
			}
		}
		else
		{
			IsVportScrollbar_V=FALSE;
		}

		if(pVport->hHScroll&&IsVportScrollbar_H==FALSE)
		{
			*bottom-=_SCROLLBAR_WIDTH_;
			if(!(pVport->base.style&VPORT_NONE_BORDER))
			{
				*bottom-=1;
			}
		}
		else
		{
			IsVportScrollbar_H=FALSE;
		}
		
	}
	//the handle is common control's. if need,should subtract width of contorl's border.
	//but don't do now.
	else if(pCtrlBase->checkFlag==GUI_CONTROL_CHECK_FLAG)
	{
		;
	}

	//get left-top corner absolute coordinate
	if(_guiConvertXY(handle,left,top)==STATUS_ERR)
	{
		//return STATUS_ERR;
		goto RETURN_ERR;
	}

	//get right-bottom corner absolute coordinate
	if(_guiConvertXY(handle,right,bottom)==STATUS_ERR)
	{
		//return STATUS_ERR;
		goto RETURN_ERR;
	}

	//the handle is window
	if(pCtrlBase->checkFlag==GUI_WIN_CHECK_FLAG)
	{
		if(STATUS_ERR==_guiGetHandleView(0,&iContainer_Left,&iContainer_Top,&iContainer_Right,&iContainer_Bottom))
		{
			//return STATUS_ERR;
			goto RETURN_ERR;
		}
	}
	//the handle is control
	else if(pCtrlBase->checkFlag==GUI_CONTROL_CHECK_FLAG)
	{
		pCtrl=(TGuiControl *)pCtrlBase;
		//the control in the window
		if(pCtrl->container)
		{
			if(STATUS_ERR==_guiGetHandleView(pCtrl->container,&iContainer_Left,&iContainer_Top,&iContainer_Right,&iContainer_Bottom))
			{
				//return STATUS_ERR;
				goto RETURN_ERR;
			}
		}
		//the control in the viewport
		else if(pCtrl->vportHandle)
		{
			pVport=(TViewport *)pCtrl->vportHandle;
			if(pVport->hVScroll!=NULL&&(HNDL)pCtrl==pVport->hVScroll)
			{
				IsVportScrollbar_V=TRUE;
			}
			if(pVport->hHScroll!=NULL&&(HNDL)pCtrl==pVport->hHScroll)
			{
				IsVportScrollbar_H=TRUE;
			}
			if(STATUS_ERR==_guiGetHandleView(pCtrl->vportHandle,&iContainer_Left,&iContainer_Top,&iContainer_Right,&iContainer_Bottom))
			{
				//return STATUS_ERR;
				goto RETURN_ERR;
			}

		}
	}

	//get final result;
	iConvert_Left=*left;
	iConvert_Top=*top;
	if(*left<iContainer_Left)
	{
		*left=iContainer_Left;
	}
	if(*top<iContainer_Top)
	{
		*top=iContainer_Top;
	}
	if(*right>iContainer_Right)
	{
		*right=iContainer_Right;
	}
	if(*bottom>iContainer_Bottom)
	{
		*bottom=iContainer_Bottom;
	}

	if(*left>*right||*top>*bottom)
	{
		//return STATUS_ERR;
		goto RETURN_ERR;
	}

#if _DEBUG&&_DEBUG_UTILITY
	displayDrawHLine(*left, *right, *top, GUI_BLACK, GUI_SOLID);
	displayDrawHLine(*left, *right, *bottom, GUI_BLACK, GUI_SOLID);
	displayDrawVLine(*left, *top, *bottom, GUI_BLACK);
	displayDrawVLine(*right, *top, *bottom, GUI_BLACK);
#endif

	
RETURN_OK:
	iRecursion-=1;
	if(iRecursion==0&&ghView!=handle)
	{
		ghView=handle;
		gView_Left=*left;
		gView_Top=*top;
		gView_Right=*right;
		gView_Bottom=*bottom;
		gConvert_Left=iConvert_Left;
		gConvert_Top=iConvert_Top;
	}
	return STATUS_OK;
RETURN_ERR:
	iRecursion=0;
	return STATUS_ERR;
}

void _guiSetViewHandleInvalid(void)
{
	ghView=0;
}
/*************************************************************
	get window handle of the control
*************************************************************/
HNDL guiQueryWindowOfControl(HNDL hCtrl)
{
	HNDL hWnd=NULL;

	TGuiControl *pCtrl;
	if(hCtrl==0)
	{
		return NULL;
	}

	pCtrl=(TGuiControl *)hCtrl;
	guiEnterWCS();

	if(pCtrl->checkFlag!=GUI_CONTROL_CHECK_FLAG)
	{
		guiExitWCS();
		return NULL;
	}

	if(pCtrl->container)
	{
		hWnd=pCtrl->container;
	}
	else if(pCtrl->vportHandle)
	{
		hWnd=guiQueryWindowOfControl(pCtrl->vportHandle);
	}
	guiExitWCS();

	return hWnd;
}


unsigned short SwapWord(unsigned short code)
{
	unsigned short sTemp;
	sTemp = ((code>>8)&0x00ff);
	sTemp |= ((code<<8)&0xff00);
	return sTemp;

} 

unsigned int SwapDWord(unsigned int code)
{                     
	unsigned int temp;
	temp = ((code>>24)&0x000000ff);
	//temp |= ((code>>16)&0x0000ff00);
	temp |= ((code>>8)&0x0000ff00);
	temp |= ((code<<24)&0xff000000);
	//temp |= ((code<<16)&0x00ff0000);
	temp |= ((code<<8)&0x00ff0000);
	return temp;
} 

/*
unsigned short Big5ToGB(unsigned short bigCode)
{
	TCodeTable   *pTable;
	int size;

	pTable = (TCodeTable *)Big_GB;
	//size = sizeof(Big_GB);
	while(pTable->bigCode)
	{
		if(pTable->bigCode == bigCode)
		{
			return pTable->gbCode;
		}
		pTable++;
	}

	return 0xfefe;
}
*/

#ifdef _DEBUG
int _debugDisplayMemInfo(char *szCaption)
{
	unsigned long max=0,used=0,remain=0;

	getMemoryUsageInfo(&max,&used);
	remain=max-used;
	sc_logString(szCaption,TO_MONITOR);
	sc_logInt(remain,HEX,TO_MONITOR);
	return remain;
}
#endif

⌨️ 快捷键说明

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