Click here to Skip to main content
15,896,500 members
Articles / Desktop Programming / MFC

CRulerRichEditCtrl - a complete RTF mini-editor

Rate me:
Please Sign up or sign in to vote.
4.92/5 (39 votes)
17 May 2005Public Domain7 min read 302.1K   18K   159  
A complete mini-editor with a formatting toolbar and a ruler with editable tab-positions.
/* ==========================================================================
	File :			FontComboBox.cpp

	Class :			CFontComboBox

	Author :		Johan Rosengren, Abstrakt Mekanik AB
					Iain Clarke

	Date :			2005-05-06

	Purpose :		CFontComboBox is derived from "CComboBox" and is an 
					autosizing no-frills combobox for display of the fonts 
					installed in the system.. 

	Description :	Simpel derived class with members to fill the box, to 
					autosize the dropdown and select an entry by name.

	Usage :			Create as any combobox, and call "FillCombo" to fill 
					the control with the names of the fonts installed in 
					the system. Call "SelectFontName" to select a font by 
					name.

   ========================================================================*/

#include "stdafx.h"
#include "FontComboBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// Enumeration callback function for 
// installed fonts
BOOL CALLBACK EnumFontProc( LPLOGFONT lplf, LPTEXTMETRIC /*lptm*/, DWORD /*dwType*/, LPARAM lpData )	
{	
	CFontComboBox *caller = reinterpret_cast< CFontComboBox* > ( lpData );		
	caller->AddString( lplf->lfFaceName );

	CClientDC dc( caller );
	dc.SelectStockObject( ANSI_VAR_FONT );

	// Add a "0" for the margin.
	CSize sz = dc.GetTextExtent( CString( lplf->lfFaceName ) + CString( "0" ) );

	caller->SetMaxWidth( max( caller->GetMaxWidth(), sz.cx ) );

	return TRUE;

}

/////////////////////////////////////////////////////////////////////////////
// CFontComboBox

CFontComboBox::CFontComboBox()
/* ============================================================
	Function :		CFontComboBox::CFontComboBox
	Description :	ctor
	Access :		Public
					
	Return :		void
	Parameters :	none

	Usage :			

   ============================================================*/
{
	m_maxWidth = 0;
}

CFontComboBox::~CFontComboBox()
/* ============================================================
	Function :		CFontComboBox::~CFontComboBox
	Description :	dtor
	Access :		Public
					
	Return :		void
	Parameters :	none

	Usage :			

   ============================================================*/
{
}

BEGIN_MESSAGE_MAP(CFontComboBox, CComboBox)
	//{{AFX_MSG_MAP(CFontComboBox)
	ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFontComboBox message handlers

void CFontComboBox::OnDropdown() 
/* ============================================================
	Function :		CFontComboBox::OnDropdown
	Description :	Sets the dropped down width of the combo 
					control to the max width of the string in 
					the list
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC

   ============================================================*/
{

   int scroll = ::GetSystemMetrics(SM_CXVSCROLL);
   SetDroppedWidth( m_maxWidth + scroll );

}

/////////////////////////////////////////////////////////////////////////////
// CFontComboBox implementation

void CFontComboBox::FillCombo()
/* ============================================================
	Function :		CFontComboBox::FillCombo
	Description :	Fills the combo with the font names from 
					the system
	Access :		Public
					
	Return :		void
	Parameters :	none

	Usage :			Call to fill the combo with the installed 
					fonts.

   ============================================================*/
{

	CClientDC dc( this );		
	::EnumFonts( dc, 0, ( FONTENUMPROC ) EnumFontProc, ( LPARAM ) this );

}

void CFontComboBox::SetMaxWidth( int maxWidth )
/* ============================================================
	Function :		CFontComboBox::SetMaxWidth
	Description :	Sets the "m_maxWidth" member.
	Access :		Public
					
	Return :		void
	Parameters :	int maxWidth	-	New width of the drop
										down list

	Usage :			Call to set a max width for the strings in 
					the list, used to set the width of the drop 
					down list. Called from the font enum 
					callback.

   ============================================================*/
{

	m_maxWidth = maxWidth;

}

int CFontComboBox::GetMaxWidth() const
/* ============================================================
	Function :		CFontComboBox::GetMaxWidth
	Description :	Returns the "m_maxWidth" member of the 
					control.
	Access :		Public
					
	Return :		int		-	Width of the longest string, in 
								pixels.
	Parameters :	none

	Usage :			Call to get the width necessary to display 
					the longest string in the combo drop down.

   ============================================================*/
{

	return m_maxWidth;

}

void CFontComboBox::SelectFontName( const CString& font )
/* ============================================================
	Function :		CFontComboBox::SelectFontName
	Description :	Selects "font" in the list, if it 
					exists.
	Access :		Public
					
	Return :		void
	Parameters :	const CString& font	-	Name of the font to 
											select.

	Usage :			Call to set/change the selection of the 
					combo.

   ============================================================*/
{

	int max = GetCount();
	for( int t = 0 ; t < max ; t++ )
	{
		CString data;
		GetLBText( t, data );
		if( data == font )
		{
			SetCurSel( t );
			return;
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
45 years old, married, three kids.

Started with computers more than 20 years ago on a CBM-64.

Read Theoretical Philosophy at the University of Lund.

Working as a C++ consultant developer.

Science-fiction freak. Enjoy vintage punkrock.

Comments and Discussions