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

📄 frmtbar.c

📁 文本编辑器
💻 C
📖 第 1 页 / 共 3 页
字号:
 */
LOCAL void FillColors(HWND hwnd)
{
	FormatBarState *	pfbs = PfbsGetWindowPtr(hwnd);
	HWND				hwndColor = pfbs->hwndColor;
	INT					ilMatch = -1;
	LRESULT				lr;
	INT					icr;
	DWORD *				pdw;

	// Empty the current list
	SendMessage(hwndColor, CB_RESETCONTENT, 0, 0);

	for (icr = 0, pdw = rgrgbColors; icr < crgbColorsMax; ++icr, ++pdw)
	{
		lr = SendMessage(hwndColor, CB_ADDSTRING, 0, (LPARAM) *pdw);
		if (lr == CB_ERR || lr == CB_ERRSPACE)
		{
			TraceError("FillColors: No more room for colors", -1);
			break;
		}

		if (*pdw == pfbs->cf.crTextColor)
			ilMatch = icr;
	}

	SendMessage(hwndColor, CB_SETCURSEL, (WPARAM) ilMatch > 0 ? ilMatch : 0, 0);
}


/*
 *	FB_OnNcCreate
 *
 *	Purpose:
 *		We want to draw our own colors for the colors combobox
 *
 *	Arguments:
 *		hwnd		The window
 *		pmis		Pointer to a MEASUREITEMSTRUCT to be filled out
 *
 *	Returns:
 *		None.
 */
LOCAL BOOL FB_OnNcCreate(HWND hwnd, CREATESTRUCT * pcs)
{
	RECT				rc;
	INT					cyButton;
	INT					cyList;
	INT					yPos;
	INT					xPos;
	HFONT				hfont;
	FormatBarState *	pfbs = NULL;
	HINSTANCE			hinst = pcs->hInstance;
	WNDPROC 			pfnWndProcT;
	POINT				pt = { 1, 1 };
	HWND				hwndT;

	// Tell format bar where to find it's state information
	pfbs = (FormatBarState *) GlobalAllocPtr(GHND, sizeof(FormatBarState));
	if (!pfbs)
		goto ErrorNoState;
	SetWindowPtr(hwnd, pfbs);
	AssertSz(PfbsGetWindowPtr(hwnd) == pfbs, "We didn't write it!");

	// Get a few brushes that we'll be using all the time
	UpdateBrushes(hwnd);

	// Create the toolbar
	pfbs->hwndToolbar = CreateToolbarEx(hwnd, CCS_TOP | WS_CHILD,
										0, tbFormatBarMax, hinst,
										BMP_FormatBar, rgtbbutton, ctbbutton,
										16, 16, 16, 16, sizeof(TBBUTTON));

	if (!pfbs->hwndToolbar)
		goto ErrorNoToolbar;

	// Subclass the toolbar
	pfnWndProcT = (WNDPROC) SetWindowLong(pfbs->hwndToolbar, GWL_WNDPROC,
												(LONG) LFBToolbarWndProc);

	// If we don't know what the global ToolbarWndProc is, save it
	if (!pfnToolbarWndProc)
		pfnToolbarWndProc = pfnWndProcT;
	
	// Determine how tall the buttons are so we can size our other controls
	// accordingly
	SendMessage(pfbs->hwndToolbar, TB_GETITEMRECT, 1, (LPARAM) &rc);
	cyButton = rc.bottom - rc.top + 1;

	// Determine how tall the toolbar is so that we can center the comboboxes
	GetClientRect(pfbs->hwndToolbar, &rc);
	yPos = (rc.bottom - rc.top + 1 - cyButton) / 2 + 1;

	// Let's make the comboboxes dropdown about 5 times the height
	cyList = 5 * cyButton;

	// Get the font to use for the comboboxes
	hfont = (HFONT) SendMessage(pfbs->hwndToolbar, WM_GETFONT, 0, 0);

	// Now create the other format bar controls

	// The name
	xPos = 8;
	pfbs->hwndName = CreateWindow(szComboBox, NULL,
									WS_CHILD | WS_VSCROLL | CBS_DROPDOWN |
									CBS_SORT | CBS_HASSTRINGS | WS_VISIBLE,
									xPos, yPos, cxName, cyList,
									pfbs->hwndToolbar, (HMENU) TBI_Name,
									hinst, NULL);
	if (!pfbs->hwndName)
		goto ErrorNoName;
	SetWindowFont(pfbs->hwndName, hfont, TRUE);
	xPos += cxName + 8;


	// The Size
	pfbs->hwndSize = CreateWindow(szComboBox, NULL,
									WS_CHILD | WS_VSCROLL | CBS_DROPDOWN |
									WS_VISIBLE,
									xPos, yPos, cxSize, cyList,
									pfbs->hwndToolbar, (HMENU) TBI_Size,
									hinst, NULL);
	if (!pfbs->hwndSize)
		goto ErrorNoSize;
	SetWindowFont(pfbs->hwndSize, hfont, TRUE);

	// The color
	//$ REVIEW: Magic number
	SendMessage(pfbs->hwndToolbar, TB_GETITEMRECT, 3, (LPARAM) &rc);
	pfbs->hwndColor = CreateWindow(szComboBox, NULL,
									WS_CHILD | WS_VSCROLL | CBS_DROPDOWNLIST |
									CBS_OWNERDRAWFIXED | WS_VISIBLE,
									rc.right, yPos - 1, cxColor, cyList,
									pfbs->hwndToolbar, (HMENU) TBI_Color,
									hinst, NULL);
	if (!pfbs->hwndColor)
		goto ErrorNoColor;
	SetWindowFont(pfbs->hwndColor, hfont, TRUE);

	// Set the initial color to black so it won't be redrawn.
	pfbs->cf.dwMask |= INITIAL_COLOR;

	// Subclass the comboboxes' edit controls

	// Do the name first
	hwndT = ChildWindowFromPoint(pfbs->hwndName, pt);
	pfnWndProcT = (WNDPROC) SetWindowLong(hwndT, GWL_WNDPROC,
												(LONG) LFBEditWndProc);

	// If we don't know what the global EditWndProc is, save it
	if (!pfnEditWndProc)
		pfnEditWndProc = pfnWndProcT;

	// Next the size
	hwndT = ChildWindowFromPoint(pfbs->hwndSize, pt);
	(WNDPROC) SetWindowLong(hwndT, GWL_WNDPROC, (LONG) LFBEditWndProc);
	
	// Lastly the color
	pfnWndProcT = (WNDPROC) SetWindowLong(pfbs->hwndColor, GWL_WNDPROC,
												(LONG) LFBComboBoxWndProc);

	// If we don't know what the global ComboBoxWndProc is, save it
	if (!pfnComboBoxWndProc)
		pfnComboBoxWndProc = pfnWndProcT;
	
	// Create a copy of the DC that the user gave up to play with
	if(pcs->lpCreateParams)
	{
		HDC *	phdc = (HDC *) pcs->lpCreateParams;

		pfbs->hdc = CreateCompatibleDC(*phdc);
	}
	else
	{
		HDC hdc = GetDC(pcs->hwndParent);

		pfbs->hdc = CreateCompatibleDC(hdc);
		ReleaseDC(pcs->hwndParent, hdc);
	}

	if (!pfbs->hdc)
		goto ErrorNoColor;
	
	// Get the number of pixels per inch vertically so we can do point sizes
	pfbs->cyPerInch = GetDeviceCaps(pfbs->hdc, LOGPIXELSY);

	// Load up the names of the fonts and colors
	FillNames(hwnd);
	FillColors(hwnd);
	// Continue creating
	return TRUE;

ErrorNoColor:
	DestroyWindow(pfbs->hwndSize);
	pfbs->hwndSize = NULL;

ErrorNoSize:
	DestroyWindow(pfbs->hwndName);
	pfbs->hwndName = NULL;

ErrorNoName:
	DestroyWindow(pfbs->hwndToolbar);
	pfbs->hwndToolbar = NULL;

ErrorNoToolbar:
	if (pfbs)
		GlobalFreePtr(pfbs);

ErrorNoState:
	return FALSE;
}


/*
 *	FB_OnMeasureItem
 *
 *	Purpose:
 *		We want to draw our own colors for the colors combobox
 *
 *	Arguments:
 *		hwnd		The window
 *		pmis		Pointer to a MEASUREITEMSTRUCT to be filled out
 *
 *	Returns:
 *		None.
 */
LOCAL void FB_OnMeasureItem(HWND hwnd, MEASUREITEMSTRUCT * pmis)
{
	switch (pmis->CtlID)
	{
	case TBI_Color:
		pmis->itemWidth = 128;
		pmis->itemHeight = 16;
		break;
	default:
		TraceError("Don't know anything about CtlID", -1);
		break;
	}
}


/*
 *	FB_OnDrawItem
 *
 *	Purpose:
 *		We want to drawn our own colors for our color combobox
 *
 *	Arguments:
 *		hwnd		The window
 *		pdis		Pointer to a DRAWITEMSTRUCT to be filled out
 *
 *	Returns:
 *		None.
 */
LOCAL void FB_OnDrawItem(HWND hwnd, DRAWITEMSTRUCT * pdis)
{
	FormatBarState *	pfbs = PfbsGetWindowPtr(hwnd);
	HDC					hdc = pdis->hDC;
	RECT				rc;
	HBRUSH				hbrush;
	COLORREF			cr = (COLORREF) pdis->itemData;
	INT					nIter = 2;

	// As far as we know we have only one owner drawn control
	AssertSz(pdis->CtlType == ODT_COMBOBOX, "Know only comboboxes");
	if (pdis->CtlID != TBI_Color)
		return;

	// NULL object wanted ?
	if (pdis->itemData == -1)
		goto HandleFocus;

	switch (pdis->itemAction)
	{
	case ODA_DRAWENTIRE:
		rc = pdis->rcItem;
		InflateRect(&rc, -3, -3);
		hbrush = CreateSolidBrush((COLORREF)cr);
		FillRect(hdc, &rc, hbrush);
		DeleteObject(hbrush);
		FrameRect(hdc, &rc, GetStockObject(BLACK_BRUSH));

		// *** FALL THROUGH ***

	case ODA_SELECT:
		rc = pdis->rcItem;
		if (pdis->itemState & ODS_SELECTED)
			hbrush = pfbs->hbrushHighlight;
		else
			hbrush = pfbs->hbrushWindow;
		while (nIter--)
		{
			InflateRect(&rc, -1, -1);
			FrameRect(hdc, &rc, hbrush);
		}

		if (pdis->itemAction != ODA_DRAWENTIRE)
			break;

		// *** FALL THROUGH ***

HandleFocus:
	case ODA_FOCUS:
		if (pdis->itemState & ODS_FOCUS)
			hbrush = pfbs->hbrushHighlight;
		else
			hbrush = pdis->itemData == -1 ? pfbs->hbrushButtonFace :
											pfbs->hbrushWindow;
		FrameRect(hdc, &pdis->rcItem, hbrush);
		break;
	}

}


/*
 *	PaintName
 *
 *	Purpose:
 *		Paint the current name
 *
 *	Arguments:
 *		pfbs		The current format bar state
 *
 *	Returns:
 *		None.
 */
LOCAL VOID PaintName(FormatBarState * pfbs)
{
	LONG	ilFound;

	TraceCharFormat("PaintName", &pfbs->cf);
	if (pfbs->cf.dwMask & CFM_FACE)
	{
		ilFound = SendMessage(pfbs->hwndName, CB_FINDSTRING, 0,
								(LPARAM) pfbs->cf.szFaceName);
		SendMessage(pfbs->hwndName, CB_SETCURSEL, (WPARAM) ilFound, 0);
		if (ilFound == CB_ERR)
			SetWindowText(pfbs->hwndName, pfbs->cf.szFaceName);
	}
	else
		SetWindowText(pfbs->hwndName, TEXT(""));

}


/*
 *	PaintSize
 *
 *	Purpose:
 *		Paint the current size
 *
 *	Arguments:
 *		pfbs		The current format bar state
 *
 *	Returns:
 *		None.
 */
LOCAL VOID PaintSize(FormatBarState * pfbs)
{
	LONG	lSize = MulDiv((INT) pfbs->cf.yHeight, 72, 1440);
	LONG	ilFound;
	TCHAR	szT[10];

	TraceCharFormat("PaintSize", &pfbs->cf);
	szT[0] = 0;
	if (lSize > 0 && (pfbs->cf.dwMask & CFM_SIZE))
		wsprintf(szT, "%ld", lSize);
	ilFound = SendMessage(pfbs->hwndSize, CB_FINDSTRINGEXACT, 0,
							(LPARAM) szT);
	SendMessage(pfbs->hwndSize, CB_SETCURSEL, (WPARAM) ilFound, 0);
	SetWindowText(pfbs->hwndSize, szT);
}


/*
 *	PaintColor
 *
 *	Purpose:
 *		Paint the current color
 *
 *	Arguments:
 *		pfbs		The current format bar state
 *
 *	Returns:
 *		None.
 */
LOCAL VOID PaintColor(FormatBarState * pfbs)
{
	LONG		ilFound = -1;
	COLORREF	crTextColor = pfbs->cf.crTextColor;

	//$ FUTURE: Handle autocolor

	// If color isn't known, choose white
	TraceCharFormat("PaintColor", &pfbs->cf);
	if (pfbs->cf.dwMask & CFM_COLOR)
	{
		ilFound = SendMessage(pfbs->hwndColor, CB_FINDSTRINGEXACT, 0,
								(LPARAM) crTextColor);
		if (ilFound < 0)
		{
			// Not found, add a new color
			ilFound = SendMessage(pfbs->hwndColor, CB_ADDSTRING, 0,
								(LPARAM) crTextColor);
		}
	}
	SendMessage(pfbs->hwndColor, CB_SETCURSEL, (WPARAM) ilFound, 0);
}


/*
 *	PaintEffects
 *
 *	Purpose:
 *		Paint the current effects
 *
 *	Arguments:
 *		pfbs		The current format bar state
 *
 *	Returns:
 *		None.
 */
LOCAL VOID PaintEffects(FormatBarState * pfbs)
{
	HWND	hwndToolbar = pfbs->hwndToolbar;
	DWORD	dwMask = pfbs->cf.dwMask;
	DWORD	dwEffects = pfbs->cf.dwEffects;
	BOOL	fEffect;
	BOOL	fMask;
	INT		nID;

	TraceCharFormat("PaintEffects", &pfbs->cf);
	for (nID = TBI_Bold; nID <= TBI_Underline; nID++)
	{
		fMask = dwMask & rgdwCFEffect[nID - TBI_Bold] ? TRUE : FALSE;
		fEffect = dwEffects & rgdwCFEffect[nID - TBI_Bold] ? TRUE : FALSE;
#ifdef LIKE_WORD2
		// Act like Word
		SendMessage(hwndToolbar, TB_CHECKBUTTON, nID, MAKELONG(fEffect, 0));
		SendMessage(hwndToolbar, TB_INDETERMINATE, nID, MAKELONG(!fMask, 0));
#elif defined(LIKE_T3)
		// Act like T3
		SendMessage(hwndToolbar, TB_CHECKBUTTON, nID,
					MAKELONG(fEffect && fMask, 0));
#else
		//$ Raid 2375: Show the user what is actually going to happen
		SendMessage(hwndToolbar, TB_CHECKBUTTON, nID, MAKELONG(fEffect, 0));
#endif
	}

}


/*
 *	PaintAlignment
 *
 *	Purpose:
 *		Paint the current alignment
 *
 *	Arguments:
 *		pfbs		The current format bar state
 *
 *	Returns:
 *		None.
 */
LOCAL VOID PaintAlignment(FormatBarState * pfbs)
{
	HWND	hwndToolbar = pfbs->hwndToolbar;
	DWORD	wAlignment = pfbs->pf.wAlignment;
	INT		nID;

	if (pfbs->pf.dwMask & PFM_ALIGNMENT)
	{
#ifdef LIKE_WORD
		// Make all the buttons active
		for (nID = TBI_Left; nID <= TBI_Right; nID++)
			SendMessage(hwndToolbar, TB_INDETERMINATE, nID,
						MAKELONG(FALSE, 0));
#endif
		// And press down one of them
		switch (wAlignment)
		{
		case PFA_CENTER:
			nID = TBI_Center;
			break;

		case PFA_RIGHT:
			nID = TBI_Right;
			break;

		case PFA_LEFT:
		default:
			nID = TBI_Left;
			break;
		}
		SendMessage(hwndToolbar, TB_CHECKBUTTON, nID, MAKELONG(TRUE, 0));
	}
	else
	{
#ifdef LIKE_WORD
		// Make all the buttons indeterminate
		for (nID = TBI_Left; nID <= TBI_Right; nID++)
			SendMessage(hwndToolbar, TB_INDETERMINATE, nID, MAKELONG(TRUE, 0));
#else
		// Pop all the buttons
		for (nID = TBI_Left; nID <= TBI_Right; nID++)
			SendMessage(hwndToolbar, TB_CHECKBUTTON, nID, MAKELONG(FALSE, 0));
#endif
	}
}


/*
 *	FB_OnCommand
 *
 *	Purpose:
 *		Handle some of the notifications
 *
 *	Arguments:
 *		hwnd		The window
 *		pdis		Pointer to a DRAWITEMSTRUCT to be filled out
 *
 *	Returns:
 *		None.
 */
LOCAL void FB_OnCommand(HWND hwnd, INT nID, HWND hwndCtl, INT nNotify)
{
	FormatBarState *	pfbs = PfbsGetWindowPtr(hwnd);
	LONG				iSel;
	if (nNotify == CBN_ERRSPACE)
	{
		//$ FUTURE: Handle error out of space
		return;
	}

	switch (nNotify)
	{
	case CBN_SELENDOK:
		// Flag the next combobox message as somethng we want
		pfbs->fExpectChoice = TRUE;
		pfbs->fGiveUpFocus = TRUE;
		return;

	case 0:
		// This is a button from the toolbar if hwndCtl == pfbs->hwndToolbar
		if (hwndCtl != pfbs->hwndToolbar)
			return;
		break;

	case CBN_KILLFOCUS:
		switch (nID)
		{
		case TBI_Name:
			PaintName(pfbs);
			break;

		case TBI_Size:
			PaintSize(pfbs);
			break;

		case TBI_Color:
			PaintColor(pfbs);
			break;
		}
		return;

	case CBN_SELCHANGE:
		// The user is making up his mind so ignore the sel change
		if (!pfbs->fExpectChoice)
			return;
		break;

	case CBN_DROPDOWN:
		if (nID == TBI_Size)
			FFillSizes(hwnd);
		return;

	//$ REVIEW: Just check for fExpectChoice to shorten this switch ?

	// Throw away these notifications from the toolbar
	case TBN_BEGINDRAG:
	case TBN_ENDDRAG:

	// Throw away these notifications from the comboboxes
	case CBN_SETFOCUS:
	case CBN_CLOSEUP:
	case CBN_SELENDCANCEL:
	case CBN_EDITCHANGE:
	case CBN_EDITUPDATE:
	case CBN_DBLCLK:

	default:
		return;
	}

	switch (nID)
	{
	case TBI_Name:
		if (pfbs->fExpectChoice)
		{
			DWORD	dw;

			// Get the new font name
			pfbs->cf.dwMask |= CFM_FACE | CFM_CHARSET;
			pfbs->dwCFMaskChange = CFM_FACE | CFM_CHARSET;

			// Get the new selection
			iSel = SendMessage(pfbs->hwndName, CB_GETCURSEL, 0, 0);
			if (iSel < 0)
			{
		//DebugStr("\pBefore GetWindowText in Frmtbar.c");

⌨️ 快捷键说明

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