summaryrefslogtreecommitdiffstats
path: root/Examples/WhisperDesktop/CircleIndicator.cpp
blob: 593bd4a358145845882b776b58a55881130acb84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "stdafx.h"
#include "CircleIndicator.h"
#include <atltypes.h>
#include "AppState.h"

namespace
{
	static const LPCTSTR windowClassName = L"CircleIndicator";

	// Font with these symbols, shipped with Windows since forever:
	// https://learn.microsoft.com/en-us/typography/font-list/segoe-ui-symbol
	static const LPCTSTR fontName = L"Segoe UI Symbol";

	// Outlined circle
	static const LPCTSTR circleOutline = L"⚪";
	// Filled circle
	static const LPCTSTR circleFilled = L"⚫";

	// Font size for that symbol font, in points
	constexpr int fontSizePoints = 14;

	// Default active color for the indicator
	constexpr uint32_t defaultActiveColor = 0x7FFF7F;
}

CircleIndicator::CircleIndicator() :
	activeColor( defaultActiveColor )
{ }

ATL::CWndClassInfo& CircleIndicator::GetWndClassInfo()
{
	// Use custom class style with CS_PARENTDC, and COLOR_3DFACE for the background
	static ATL::CWndClassInfo wc = 
	{
		{ sizeof( WNDCLASSEX ),
		CS_HREDRAW | CS_VREDRAW | CS_PARENTDC,
		StartWindowProc,
		0, 0, NULL, NULL, NULL, (HBRUSH)( COLOR_3DFACE + 1 ), NULL, windowClassName, NULL },
		NULL, NULL, IDC_ARROW, TRUE, 0, _T( "" )
	};
	return wc;
}

// Class registration
HRESULT CircleIndicator::registerClass()
{
	WNDPROC pUnusedWndSuperProc = nullptr;
	ATOM a = GetWndClassInfo().Register( &pUnusedWndSuperProc );
	if( 0 != a )
		return S_OK;
	return getLastHr();
}

HRESULT CircleIndicator::createFont( int height )
{
	LOGFONT logFont;
	memset( &logFont, 0, sizeof( logFont ) );
	logFont.lfHeight = height;
	logFont.lfCharSet = ANSI_CHARSET;
	logFont.lfOutPrecision = OUT_TT_PRECIS;
	logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	wcsncpy_s( logFont.lfFaceName, fontName, _TRUNCATE );
	font.CreateFontIndirect( &logFont );
	if( font )
		return S_OK;
	return E_FAIL;
}

void CircleIndicator::onDestroy()
{
	if( font )
		font.DeleteObject();
}

void CircleIndicator::onPaint( CDCHandle dc )
{
	CRect rectInt32;
	GetClientRect( &rectInt32 );

	CPaintDC pdc( m_hWnd );

	const int logPixels = pdc.GetDeviceCaps( LOGPIXELSY );
	int fontSize = -MulDiv( fontSizePoints, logPixels, 72 );
	if( !font || fontHeight != fontSize )
	{
		if( font )
			font.DeleteObject();
		HRESULT hr = createFont( fontSize );
		if( FAILED( hr ) )
			return;
		fontHeight = fontSize;
	}

	pdc.SetBkColor( GetSysColor( COLOR_3DFACE ) );
	pdc.SelectFont( font );
	pdc.SetBkMode( OPAQUE );
	constexpr UINT textFormat = DT_CENTER | DT_VCENTER;

	if( isActive )
	{
		pdc.SetTextColor( activeColor );
		pdc.DrawText( circleFilled, 1, rectInt32, textFormat );
		pdc.SetBkMode( TRANSPARENT );
	}

	pdc.SetTextColor( 0 );
	pdc.DrawText( circleOutline, 1, rectInt32, textFormat );
}

void CircleIndicator::setActive( bool nowActive )
{
	if( nowActive == isActive )
		return;

	// Repaint the control
	isActive = nowActive;
	InvalidateRect( nullptr );
}