234 lines
6.2 KiB
C++
234 lines
6.2 KiB
C++
// XMonoFontListComboEdit.cpp
|
|
//
|
|
// Author: Hans Dietrich
|
|
// hdietrich@gmail.com
|
|
//
|
|
// Description:
|
|
// XMonoFontListComboEdit.cpp implements CXMonoFontListComboEdit, a class
|
|
// used by CXMonoFontListCombo.
|
|
//
|
|
// License:
|
|
// This software is released into the public domain. You are free to use
|
|
// it in any way you like, except that you may not sell this source code.
|
|
//
|
|
// This software is provided "as is" with no expressed or implied warranty.
|
|
// I accept no liability for any damage or loss of business that this
|
|
// software may cause.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
#include "XMonoFontListComboEdit.h"
|
|
|
|
#ifdef _DEBUG
|
|
#define new DEBUG_NEW
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[] = __FILE__;
|
|
#endif
|
|
|
|
#ifndef __noop
|
|
#if _MSC_VER < 1300
|
|
#define __noop ((void)0)
|
|
#endif
|
|
#endif
|
|
|
|
#undef TRACE
|
|
#define TRACE __noop
|
|
|
|
//=============================================================================
|
|
// if you want to see the TRACE output, uncomment this line:
|
|
//#include "XTrace.h"
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
|
BEGIN_MESSAGE_MAP(CXMonoFontListComboEdit, CEdit)
|
|
//=============================================================================
|
|
//{{AFX_MSG_MAP(CXMonoFontListComboEdit)
|
|
ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
|
|
ON_WM_CTLCOLOR_REFLECT()
|
|
//}}AFX_MSG_MAP
|
|
ON_WM_PAINT()
|
|
END_MESSAGE_MAP()
|
|
|
|
//=============================================================================
|
|
CXMonoFontListComboEdit::CXMonoFontListComboEdit()
|
|
//=============================================================================
|
|
{
|
|
m_bBold = FALSE;
|
|
m_strText = _T("");
|
|
}
|
|
|
|
//=============================================================================
|
|
CXMonoFontListComboEdit::~CXMonoFontListComboEdit()
|
|
//=============================================================================
|
|
{
|
|
if (m_Font.GetSafeHandle())
|
|
m_Font.DeleteObject();
|
|
if (m_BoldFont.GetSafeHandle())
|
|
m_BoldFont.DeleteObject();
|
|
if (m_brush.GetSafeHandle())
|
|
m_brush.DeleteObject();
|
|
}
|
|
|
|
//=============================================================================
|
|
void CXMonoFontListComboEdit::PreSubclassWindow()
|
|
//=============================================================================
|
|
{
|
|
TRACE(_T("in CXMonoFontListComboEdit::PreSubclassWindow ================\n"));
|
|
|
|
// get current font
|
|
CFont* pFont = GetSafeFont();
|
|
|
|
// create the font for this control
|
|
LOGFONT lf;
|
|
pFont->GetLogFont(&lf);
|
|
m_Font.CreateFontIndirect(&lf);
|
|
|
|
lf.lfWeight = FW_BOLD;
|
|
m_BoldFont.CreateFontIndirect(&lf);
|
|
|
|
m_brush.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
|
|
|
|
CEdit::PreSubclassWindow();
|
|
}
|
|
|
|
//=============================================================================
|
|
void CXMonoFontListComboEdit::OnPaint()
|
|
//=============================================================================
|
|
{
|
|
CPaintDC dc(this); // device context for painting
|
|
|
|
BOOL bFocus = FALSE;
|
|
CWnd *pWndFocus = GetFocus();
|
|
if (pWndFocus && IsWindow(pWndFocus->m_hWnd))
|
|
{
|
|
if (pWndFocus->m_hWnd == m_hWnd)
|
|
bFocus = TRUE;
|
|
}
|
|
|
|
CString strText = m_strText;
|
|
if (strText.IsEmpty())
|
|
GetWindowText(strText);
|
|
|
|
TRACE(_T("strText=%s\n"), strText);
|
|
|
|
CRect rc;
|
|
GetClientRect(&rc);
|
|
dc.FillSolidRect(&rc, GetSysColor(COLOR_WINDOW));
|
|
|
|
CEdit::SetSel(0, 0);
|
|
|
|
CFont *pOldFont = 0;
|
|
if (m_bBold)
|
|
pOldFont = dc.SelectObject(&m_BoldFont);
|
|
else
|
|
pOldFont = dc.SelectObject(&m_Font);
|
|
|
|
dc.SetBkMode(OPAQUE);
|
|
|
|
if (bFocus)
|
|
{
|
|
dc.SetTextColor(GetSysColor(COLOR_WINDOW));
|
|
dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
|
|
}
|
|
else
|
|
{
|
|
dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
|
|
dc.SetBkColor(GetSysColor(COLOR_WINDOW));
|
|
}
|
|
|
|
dc.TextOut(rc.left, rc.top, strText);
|
|
|
|
dc.SelectObject(pOldFont);
|
|
}
|
|
|
|
//=============================================================================
|
|
CFont * CXMonoFontListComboEdit::GetSafeFont()
|
|
//=============================================================================
|
|
{
|
|
// get current font
|
|
CFont *pFont = CWnd::GetFont();
|
|
|
|
if (pFont == 0)
|
|
{
|
|
// try to get parent font
|
|
CWnd *pParent = GetParent();
|
|
if (pParent && IsWindow(pParent->m_hWnd))
|
|
pFont = pParent->GetFont();
|
|
|
|
if (pFont == 0)
|
|
{
|
|
// no font, so get a system font
|
|
HFONT hFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
|
|
if (hFont == 0)
|
|
hFont = (HFONT)::GetStockObject(SYSTEM_FONT);
|
|
if (hFont == 0)
|
|
hFont = (HFONT)::GetStockObject(ANSI_VAR_FONT);
|
|
if (hFont)
|
|
pFont = CFont::FromHandle(hFont);
|
|
}
|
|
}
|
|
|
|
return pFont;
|
|
}
|
|
|
|
//=============================================================================
|
|
void CXMonoFontListComboEdit::SetWindowText(LPCTSTR lpszString)
|
|
//=============================================================================
|
|
{
|
|
m_strText = lpszString;
|
|
RedrawWindow();
|
|
}
|
|
|
|
//=============================================================================
|
|
LRESULT CXMonoFontListComboEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
|
|
//=============================================================================
|
|
{
|
|
if (message == WM_SETTEXT)
|
|
{
|
|
// don't allow CEdit to paint text
|
|
m_strText = (LPCTSTR) lParam;
|
|
lParam = (LPARAM)_T("");
|
|
RedrawWindow();
|
|
}
|
|
else if ((message == WM_LBUTTONDOWN) ||
|
|
(message == WM_LBUTTONDBLCLK))
|
|
{
|
|
GetParent()->SendMessage(message, wParam, lParam);
|
|
return TRUE;
|
|
}
|
|
else if (message == WM_CHAR)
|
|
{
|
|
// let combobox handle it
|
|
GetParent()->SendMessage(message, wParam, lParam);
|
|
return TRUE;
|
|
}
|
|
else if (message == WM_CONTEXTMENU)
|
|
{
|
|
// suppress context menu
|
|
return TRUE;
|
|
}
|
|
else if (message == WM_ERASEBKGND)
|
|
{
|
|
// entire control is painted in OnPaint
|
|
return TRUE;
|
|
}
|
|
|
|
return CEdit::WindowProc(message, wParam, lParam);
|
|
}
|
|
|
|
//=============================================================================
|
|
void CXMonoFontListComboEdit::OnSetfocus()
|
|
//=============================================================================
|
|
{
|
|
RedrawWindow();
|
|
}
|
|
|
|
//=============================================================================
|
|
HBRUSH CXMonoFontListComboEdit::CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/)
|
|
//=============================================================================
|
|
{
|
|
// this is necessary on Vista
|
|
return m_brush;
|
|
}
|