www.pudn.com > screensaver.rar > GUILib.c


#include "GUILib.h" 
#include "genlib.h" 
#include "graphics.h" 
#include "extgraph.h" 
#include "strlib.h" 
 
static void DefaultRedraw(handle_t win); 
static void DrawBox(double left, double top, double width, double height, string color); 
static void CleanScreen(string color); 
static void DrawBlockText(double left, double top, double width, double height, string strText); 
static void RemoveAllObjects(); 
 
static handle_t WinHead; 
static bool blnExit; 
static bool blnInitialized = FALSE; 
static void DefineColors (void); 
 
bool CreateWindow(handle_t win) 
{ 
#ifdef OPTION_DEBUG 
	printf("CreateWindow()\n"); 
#endif 
	win->state = STATE_NORMAL; 
	win->nextWin = WinHead->nextWin; 
	win->prevWin = WinHead; 
	WinHead->nextWin = win; 
	if (win->nextWin != NULL) 
	{ 
		((handle_t)win->nextWin)->prevWin = win; 
	} 
 
	return TRUE; 
} 
 
bool RemoveWindow(handle_t hWin) 
{ 
#ifdef OPTION_DEBUG 
	printf("RemoveWindow()\n"); 
#endif 
	((handle_t)hWin->prevWin)->nextWin = hWin->nextWin; 
	((handle_t)hWin->nextWin)->prevWin = hWin->prevWin; 
 
	free(hWin); 
 
	return TRUE; 
} 
 
void StartGUI() 
{ 
	handle_t lastWin = NULL; 
	handle_t curWin; 
	bool lastMouseState = FALSE; 
	double dblLastX, dblLastY; 
 
#ifdef OPTION_DEBUG 
	printf("StartGUI()\n"); 
#endif 
 
	RedrawAll(); 
 
	dblLastX = GetMouseX(); 
	dblLastY = GetMouseY(); 
	blnExit = FALSE; 
	while(!blnExit) 
	{ 
		curWin = WindowFromPoint(GetMouseX(), GetMouseY()); 
		if (MouseButtonIsDown()) 
		{ 
			if (curWin != NULL) 
			{ 
				if (!lastMouseState) 
				{ 
					curWin->state = STATE_DOWN; 
					if (curWin->onMouseDown != NULL) 
					{ 
						curWin->onMouseDown(curWin); 
						InvokeRedraw(curWin); 
					} 
				} 
			} 
			lastMouseState = TRUE; 
		} else 
		{ 
			if (lastMouseState) 
			{ 
				if (curWin != NULL) 
				{ 
					curWin->state = STATE_HOVER; 
					InvokeRedraw(curWin); 
					if (curWin->onMouseUp != NULL) 
					{ 
						curWin->onMouseUp(curWin); 
					} else if(curWin->onClick != NULL) 
					{ 
						curWin->onClick(curWin); 
					} 
				} 
				lastMouseState = FALSE; 
			} else if ((dblLastX != GetMouseX()) || (dblLastY != GetMouseY()) ) 
			{ 
				if (curWin != lastWin) 
				{ 
					if (lastWin != NULL) 
					{ 
						lastWin->state = STATE_NORMAL; 
						InvokeRedraw(lastWin); 
						if (lastWin->onMouseExit != NULL) 
						{ 
							lastWin->onMouseExit(lastWin); 
						} 
					} 
 
					if (curWin != NULL) 
					{ 
						curWin->state = STATE_HOVER; 
						InvokeRedraw(curWin); 
						if (curWin->onMouseMove != NULL) 
						{ 
							curWin->onMouseMove(curWin); 
						} 
					} 
					 
					lastWin = curWin; 
				} 
			} 
			dblLastX = GetMouseX(); 
			dblLastY = GetMouseY(); 
		} 
	} 
} 
 
void InitGUI() 
{ 
#ifdef OPTION_DEBUG 
	printf("InitGUI()\n"); 
	printf("\tScreen Information:\n"); 
	printf("\t\tWidth: %g\n", GetFullScreenWidth()); 
	printf("\t\tHeight: %g\n", GetFullScreenHeight()); 
#endif 
	WinHead = New(handle_t); 
	WinHead->prevWin = NULL; 
	WinHead->nextWin = NULL; 
 
	SetWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
	InitGraphics(); 
	DefineColors(); 
 
	blnInitialized = TRUE; 
} 
 
void RemoveAllObjects() 
{ 
	handle_t ptrCurWin, ptrNextWin; 
 
	ptrCurWin = WinHead->nextWin; 
 
	while(ptrCurWin != NULL) 
	{ 
		ptrNextWin = ptrCurWin->nextWin; 
		free(ptrCurWin); 
		ptrCurWin = ptrNextWin; 
	} 
} 
 
void ExitGUI() 
{ 
	blnExit = TRUE; 
 
#ifdef OPTION_DEBUG 
	printf("ExitGUI()\n"); 
#endif 
 
	RemoveAllObjects(); 
 
	WinHead->nextWin = NULL; 
} 
 
void EndGUI() 
{ 
	blnExit = TRUE; 
 
#ifdef OPTION_DEBUG 
	printf("EndGUI()\n"); 
#endif 
 
	RemoveAllObjects(); 
 
	free(WinHead); 
 
	blnInitialized = FALSE; 
} 
 
handle_t WindowFromPoint(double x, double y) 
{ 
	handle_t ptrCurWin; 
	double left, top, right, bottom; 
 
	ptrCurWin = WinHead; 
 
	while(ptrCurWin != NULL) 
	{ 
		ptrCurWin = ptrCurWin->nextWin; 
 
		if (ptrCurWin != NULL) 
		{ 
			left = ptrCurWin->left; 
			top = ptrCurWin->top; 
			right = left + ptrCurWin->width; 
			bottom = top + ptrCurWin->height; 
 
			if ( (x >= left) && (x <= right) && (y >= top) && (y <= bottom) ) 
			{ 
				return ptrCurWin; 
			} 
		} 
	} 
 
	return NULL; 
} 
 
void RedrawAll() 
{ 
	handle_t ptrCurWin; 
	 
#ifdef OPTION_DEBUG 
	printf("RedrawAll()\n"); 
#endif 
	if (blnInitialized) 
	{ 
		CleanScreen("White"); 
 
		ptrCurWin = WinHead->nextWin; 
 
		while(ptrCurWin != NULL) 
		{ 
			if (ptrCurWin->onRedraw != NULL) 
			{ 
				ptrCurWin->onRedraw(ptrCurWin); 
			} else 
			{ 
				DefaultRedraw(ptrCurWin); 
			} 
			ptrCurWin = ptrCurWin->nextWin; 
		} 
 
		UpdateDisplay(); 
	} 
} 
 
void DefaultRedraw(handle_t win) 
{ 
	string color; 
 
#ifdef OPTION_DEBUG 
	printf("DefaultRedraw()\n"); 
#endif 
	switch (win->state) 
	{ 
		case STATE_NORMAL: 
			color = "Black"; 
			break; 
		case STATE_HOVER: 
			color = "Blue"; 
			break; 
		case STATE_DOWN: 
			color = "Orange"; 
			break; 
		default: 
			color = "Black"; 
			break; 
	} 
	StartFilledRegion(1.0); 
	DrawBox(win->left, win->top, win->width, win->height, "White"); 
	EndFilledRegion();	 
	DrawBox(win->left, win->top, win->width, win->height, color); 
	SetPenColor(color); 
	DrawBlockText(win->left, win->top, win->width, win->height, win->text); 
} 
 
void DrawBox(double left, double top, double width, double height, string color) 
{ 
	SetPenColor(color); 
	MovePen(left, top); 
	DrawLine(width, 0); 
	DrawLine(0, height); 
	DrawLine(-width, 0); 
	DrawLine(0, -height); 
} 
 
void CleanScreen(string color) 
{ 
	StartFilledRegion(1); 
	DrawBox(0, 0, GetWindowWidth(), GetWindowHeight(), color); 
	EndFilledRegion(); 
} 
 
void DefineColors (void) 
{ 
	DefineColor("Orange", 0.9, 0.5, 0); 
	DefineColor("Gold", 0.9, 0.8, 0); 
	DefineColor("Emerald", 0, 0.8, 0); 
	DefineColor("Teal", 0, 0.8, 0.7); 
	DefineColor("Aqua", 0, 0.6, 1.0); 
	DefineColor("Periwinkle", 0.5, 0, 0.7); 
	DefineColor("Purple", 0.6, 0, 0.7); 
	DefineColor("Violet", 0.6, 0, 0.6); 
	DefineColor("HotPink", 1.0, 0, 0.5); 
} 
 
string GetTextSummary(string strOrg, double width) 
{ 
	string strNewText; 
	double dblTextLen; 
 
	dblTextLen = TextStringWidth(strOrg); 
 
	strNewText = strOrg; 
	if (dblTextLen > width) 
	{ 
		while ((dblTextLen > width) && ( StringLength(strNewText) > 1) ) 
		{ 
			strNewText = SubString(strNewText, 0, StringLength(strNewText) - 2); 
			dblTextLen = TextStringWidth(strNewText) + TextStringWidth("..."); 
		} 
		strNewText = Concat(strNewText, "..."); 
	} 
 
	return strNewText; 
} 
 
void DrawBlockText(double left, double top, double width, double height, string strText) 
{ 
	double dblTextLen; 
	double dblNewX, dblNewY; 
	string strNewText; 
 
	strNewText = GetTextSummary(strText, width); 
 
	dblTextLen = TextStringWidth(strNewText); 
 
	dblNewX = left + ((width - dblTextLen) / 2); 
	dblNewY = top + ((height - GetFontHeight()) / 2); 
 
	MovePen(dblNewX, dblNewY); 
	DrawTextString(strNewText); 
} 
 
void InvokeRedraw(handle_t win) 
{ 
	if (win->onRedraw != NULL) 
	{ 
		win->onRedraw(win); 
	} else 
	{ 
		DefaultRedraw(win); 
	} 
} 
 
 
handle_t GenWindow(double left, double top, double width, double height, string text,  
				   void (*onClick)(handle_t), void (*onMouseMove)(handle_t), void (*onMouseDown)(handle_t), 
				   void (*onMouseUp)(handle_t), void (*onMouseExit)(handle_t), void (*onRedraw)(handle_t)) 
{ 
	handle_t pWin; 
 
	pWin = New(handle_t); 
 
	pWin->left = left; 
	pWin->top = top; 
	pWin->width = width; 
	pWin->height = height; 
	pWin->text = text; 
	pWin->onClick = onClick; 
	pWin->onMouseMove = onMouseMove; 
	pWin->onMouseDown = onMouseDown; 
	pWin->onMouseUp = onMouseUp; 
	pWin->onMouseExit = onMouseExit; 
	pWin->onRedraw = onRedraw; 
 
	return pWin; 
}