Click here to Skip to main content
14,027,948 members
Click here to Skip to main content

Stats

1.1M views
31.2K downloads
844 bookmarked
Posted 20 Nov 2003
Licenced GPL3

KeePass Password Safe

, 2 Jan 2019
KeePass is a free, open source, light-weight and easy-to-use password manager.
Build
Docs
Ext
Icons_04_CB
Finals
Finals2
plock-blu.ico
plock-dis.ico
plock.ico
plockb.ico
key-blu.png
key-bw.png
key-grn.ico
key-grn.png
plock-blu.ico
plock-blu.png
plock-bw.png
plock-dis.ico
plock-grn.ico
plock-grn.png
plock.ico
plock.png
plockb.png
plockbw-r.png
plockbw.png
Untitled.ico
key-org.ico
keyhole-grn.ico
lockblue.ico
lockgreen.ico
lockred.ico
lockyellow.ico
plockb.ico
plockb2.ico
plockbw.ico
plockg.ico
plocko.ico
plockr.ico
plocky.ico
Icons_15_VA
KeePass_Round
KeePass_Round_1024.png
KeePass_Round_128.png
KeePass_Round_16.png
KeePass_Round_20.png
KeePass_Round_24.png
KeePass_Round_256.png
KeePass_Round_32.png
KeePass_Round_48.png
KeePass_Round_512.png
KeePass_Round_64.png
KeePass_Round_Keyhole
KeePass_Round_Keyhole_128.png
KeePass_Round_Keyhole_16.png
KeePass_Round_Keyhole_256.png
KeePass_Round_Keyhole_32.png
KeePass_Round_Keyhole_48.png
KeePass_Round_Keyhole_64.png
KeePass_Round_Locked
KeePass_Round_Locked_128.png
KeePass_Round_Locked_16.png
KeePass_Round_Locked_256.png
KeePass_Round_Locked_32.png
KeePass_Round_Locked_48.png
KeePass_Round_Locked_64.png
KeePass_Square_Blue
KeePass_Square_Blue_128.png
KeePass_Square_Blue_16.png
KeePass_Square_Blue_24.png
KeePass_Square_Blue_256.png
KeePass_Square_Blue_32.png
KeePass_Square_Blue_48.png
KeePass_Square_Blue_64.png
KeePass_Square_Blue_Locked
KeePass_Square_Blue_Locked_128.png
KeePass_Square_Blue_Locked_16.png
KeePass_Square_Blue_Locked_24.png
KeePass_Square_Blue_Locked_256.png
KeePass_Square_Blue_Locked_32.png
KeePass_Square_Blue_Locked_48.png
KeePass_Square_Blue_Locked_64.png
KeePass_Square_Yellow
KeePass_Square_Yellow_128.png
KeePass_Square_Yellow_16.png
KeePass_Square_Yellow_256.png
KeePass_Square_Yellow_32.png
KeePass_Square_Yellow_48.png
KeePass_Square_Yellow_64.png
Icons_Nuvola
KeePassMsi
KeePassMsi.vdproj
RegKDB.reg
RegUnRegKDB.reg
KeePassLibC
KeePassLibCpp
Crypto
SHA2
DataExchange
Details
IO
PasswordGenerator
SDK
Details
SysSpec_Windows
Util
KeePassAPI
KeePassLib.def
KeePassLib.vcproj
res
WinGUI
NewGUI
TaskbarListEx
TaskDialog
VistaMenu
Plugins
PwSafe.vcproj
res
autotype.bmp
cancel.bmp
clienticex.bmp
clock.bmp
disk.bmp
document.bmp
entry_ed.ico
file.bmp
help_sma.bmp
help_smp.bmp
iconpic.ico
infoicon.bmp
infoiconex.bmp
KeePass.png
key.ico
keyhole.ico
key_smal.bmp
language.bmp
locked.ico
lock_ovr.ico
MostPopularPasswords.bin
mouse_sm.bmp
ok.bmp
optionicex.bmp
options.ico
plugins.ico
PwSafe.ico
randomke.bmp
random_b.bmp
search.ico
tb_about.bmp
tb_adden.bmp
tb_copyp.bmp
tb_copyu.bmp
tb_defau.bmp
tb_delet.bmp
tb_edite.bmp
tb_find.bmp
tb_lock.bmp
tb_new.bmp
tb_open.bmp
tb_save.bmp
tb_save1.bmp
tb_saved.bmp
toolssma.bmp
unlocked.ico
winprops.bmp
world.ico
Util
CmdLine
SprEngine
KeePass.chm
KeePass.exe
Languages
Plugins
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "StdAfx.h"
#include "FontUtil.h"
#include "NewGUICommon.h"
#include "../PwSafe.h"

LOGFONT* CFontUtil::g_plfDefault = NULL;
CFont* CFontUtil::g_pfBold = NULL;
CFont* CFontUtil::g_pfMono = NULL;
CFont* CFontUtil::g_pfSymbol = NULL;
CFont* CFontUtil::g_pfPassword = NULL;

CFontUtil::CFontUtil()
{
}

void CFontUtil::Release()
{
	if(g_plfDefault != NULL)
	{
		delete g_plfDefault;
		g_plfDefault = NULL;
	}

	if(g_pfBold != NULL)
	{
		VERIFY(g_pfBold->DeleteObject());
		delete g_pfBold;
		g_pfBold = NULL;
	}

	if(g_pfMono != NULL)
	{
		VERIFY(g_pfMono->DeleteObject());
		delete g_pfMono;
		g_pfMono = NULL;
	}

	if(g_pfSymbol != NULL)
	{
		VERIFY(g_pfSymbol->DeleteObject());
		delete g_pfSymbol;
		g_pfSymbol = NULL;
	}

	if(g_pfPassword != NULL)
	{
		VERIFY(g_pfPassword->DeleteObject());
		delete g_pfPassword;
		g_pfPassword = NULL;
	}
}

void CFontUtil::SetDefaultFont(CFont* pf)
{
	if(pf == NULL) { ASSERT(FALSE); return; }

	if(g_plfDefault != NULL) return;

	g_plfDefault = new LOGFONT();
	ZeroMemory(g_plfDefault, sizeof(LOGFONT));
	if(pf->GetLogFont(g_plfDefault) == 0)
	{
		ASSERT(FALSE);
		delete g_plfDefault;
		g_plfDefault = NULL;
		return;
	}
}

void CFontUtil::SetDefaultFontFrom(CWnd* pWnd)
{
	if(pWnd == NULL) { ASSERT(FALSE); return; }

	CFontUtil::SetDefaultFont(pWnd->GetFont());
}

void CFontUtil::SetPasswordFont(LPCTSTR lpFont, HWND hWndParent)
{
	if(g_pfPassword != NULL)
	{
		VERIFY(g_pfPassword->DeleteObject());
		delete g_pfPassword;
		g_pfPassword = NULL;
	}

	if(lpFont == NULL) { ASSERT(FALSE); return; }
	if(lpFont[0] == 0) return; // No assert

	LOGFONT lf;
	if(!CFontUtil::Deserialize(&lf, lpFont, hWndParent))
	{
		ASSERT(FALSE);
		return;
	}

	g_pfPassword = new CFont();
	if(g_pfPassword->CreateFontIndirect(&lf) == FALSE)
	{
		ASSERT(FALSE);
		delete g_pfPassword;
		g_pfPassword = NULL;
	}
}

bool CFontUtil::EnsureBold(CWnd* pWndParent)
{
	UNREFERENCED_PARAMETER(pWndParent);

	if(g_pfBold == NULL)
	{
		if(g_plfDefault == NULL) { ASSERT(FALSE); return false; }

		g_pfBold = new CFont();
		if(g_pfBold->CreateFont(g_plfDefault->lfHeight, 0, 0, 0, FW_BOLD,
			FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, g_plfDefault->lfPitchAndFamily,
			g_plfDefault->lfFaceName) == FALSE)
		{
			ASSERT(FALSE);
			delete g_pfBold;
			g_pfBold = NULL;
			return false;
		}
	}

	return true;
}

CFont* CFontUtil::GetMonoFont(CWnd* pWndParent)
{
	CFontUtil::EnsureMono(pWndParent);
	return g_pfMono;
}

bool CFontUtil::EnsureMono(CWnd* pWndParent)
{
	if(g_pfMono == NULL)
	{
		if(g_plfDefault == NULL) { ASSERT(FALSE); return false; }

		LONG nHeight = g_plfDefault->lfHeight;
		const LONG nAdd = NewGUI_Scale(1, pWndParent);
		if(nHeight < 0) nHeight -= nAdd;
		else if(nHeight > 0) nHeight += nAdd;

		g_pfMono = new CFont();
		if(g_pfMono->CreateFont(nHeight, 0, 0, 0, FW_NORMAL,
			FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, (DEFAULT_PITCH | FF_MODERN),
			CFU_MONO_FONT_FACE) == FALSE)
		{
			ASSERT(FALSE);
			delete g_pfMono;
			g_pfMono = NULL;
			return false;
		}
	}

	return true;
}

bool CFontUtil::EnsureSymbol(CWnd* pWndParent)
{
	if(g_pfSymbol == NULL)
	{
		if(g_plfDefault == NULL) { ASSERT(FALSE); return false; }

		LONG nHeight = g_plfDefault->lfHeight;
		const LONG nAdd = NewGUI_Scale(2, pWndParent);
		if(nHeight < 0) nHeight -= nAdd;
		else if(nHeight > 0) nHeight += nAdd;

		g_pfSymbol = new CFont();
		if(g_pfSymbol->CreateFont(nHeight, 0, 0, 0, FW_NORMAL,
			FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, (DEFAULT_PITCH | FF_DONTCARE),
			CPwSafeApp::GetPasswordFont()) == FALSE)
		{
			ASSERT(FALSE);
			delete g_pfSymbol;
			g_pfSymbol = NULL;
			return false;
		}
	}

	return true;
}

void CFontUtil::AssignBold(CWnd* pWnd, CWnd* pWndParent)
{
	if(pWnd == NULL) { ASSERT(FALSE); return; }
	if(!CFontUtil::EnsureBold(pWndParent)) return;

	pWnd->SetFont(g_pfBold);
}

void CFontUtil::AssignMono(CWnd* pWnd, CWnd* pWndParent)
{
	if(pWnd == NULL) { ASSERT(FALSE); return; }
	if(!CFontUtil::EnsureMono(pWndParent)) return;

	pWnd->SetFont(g_pfMono);
}

void CFontUtil::AssignSymbol(CWnd* pWnd, CWnd* pWndParent)
{
	if(pWnd == NULL) { ASSERT(FALSE); return; }
	if(!CFontUtil::EnsureSymbol(pWndParent)) return;

	pWnd->SetFont(g_pfSymbol);
}

void CFontUtil::AssignPassword(CWnd* pWnd, CWnd* pWndParent)
{
	if(pWnd == NULL) { ASSERT(FALSE); return; }

	if(g_pfPassword != NULL) pWnd->SetFont(g_pfPassword);
	else CFontUtil::AssignMono(pWnd, pWndParent);
}

CString CFontUtil::Serialize(const CFontDialog& dlg)
{
	CString str = dlg.GetFaceName();
	str += _T(';');

	int dSize = dlg.GetSize();
	dSize = ((dSize >= 0) ? dSize : -dSize);
	CString strTemp;
	strTemp.Format(_T("%d"), dSize / 10);
	str += strTemp;

	str += _T(',');
	str += ((dlg.IsBold() != FALSE) ? _T('1') : _T('0'));
	str += ((dlg.IsItalic() != FALSE) ? _T('1') : _T('0'));
	str += ((dlg.IsUnderline() != FALSE) ? _T('1') : _T('0'));
	str += ((dlg.IsStrikeOut() != FALSE) ? _T('1') : _T('0'));

	return str;
}

bool CFontUtil::Deserialize(LOGFONT* pFont, LPCTSTR lpFont, HWND hWnd)
{
	if(pFont == NULL) { ASSERT(FALSE); return false; }

	ZeroMemory(pFont, sizeof(LOGFONT));

	if(lpFont == NULL) { ASSERT(FALSE); return false; }
	CString strFontSpec = lpFont;

	const int nChars = strFontSpec.ReverseFind(_T(';'));
	const int nSizeEnd = strFontSpec.ReverseFind(_T(','));
	if(nChars < 0) { ASSERT(FALSE); return false; }
	if(nSizeEnd <= nChars) { ASSERT(FALSE); return false; }

	CString strFace, strSize, strFlags;
	strFace = strFontSpec.Left(nChars);
	strSize = strFontSpec.Mid(nChars + 1, nSizeEnd - nChars - 1);
	strFlags = strFontSpec.Mid(nSizeEnd + 1);
	if(strFlags.GetLength() < 4) { ASSERT(FALSE); return false; }

	const int nSize = _ttoi(strSize);
	const bool bBold = (strFlags.GetAt(0) == _T('1'));
	const bool bItalic = (strFlags.GetAt(1) == _T('1'));
	const bool bUnderlined = (strFlags.GetAt(2) == _T('1'));
	const bool bStrikeOut = (strFlags.GetAt(3) == _T('1'));

	HDC hDC = GetDC(hWnd);
	if(hDC != NULL)
		pFont->lfHeight = -MulDiv(nSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
	else { ASSERT(FALSE); pFont->lfHeight = -nSize; }
	ReleaseDC(hWnd, hDC);

	// pFont->lfWidth = 0;
	// pFont->lfEscapement = 0;
	// pFont->lfOrientation = 0;
	pFont->lfWeight = (bBold ? FW_BOLD : FW_NORMAL);
	pFont->lfItalic = (bItalic ? TRUE : FALSE);
	pFont->lfUnderline = (bUnderlined ? TRUE : FALSE);
	pFont->lfStrikeOut = (bStrikeOut ? TRUE : FALSE);
	pFont->lfCharSet = DEFAULT_CHARSET;
	pFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
	pFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
	pFont->lfQuality = DEFAULT_QUALITY;
	pFont->lfPitchAndFamily = (DEFAULT_PITCH | FF_DONTCARE);

	BOOST_STATIC_ASSERT(_countof(pFont->lfFaceName) == LF_FACESIZE);
	if(strFace.GetLength() >= LF_FACESIZE)
	{
		ASSERT(FALSE);
		strFace = strFace.Left(LF_FACESIZE - 1);
	}
	_tcscpy_s(pFont->lfFaceName, strFace);

	return true;
}

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 The GNU General Public License (GPLv3)

Share

About the Author

Dominik Reichl
Software Developer
Unknown
Dominik started programming in Omikron Basic, a programming language for the good old Atari ST. After this, there was some short period of QBasic programming on the PC, but soon he began learning C++, which is his favorite language up to now.

Today, his programming experience includes C / C++ / [Visual] C++ [MFC], C#/.NET, Java, JavaScript, PHP and HTML and the basics of pure assembler.

He is interested in almost everything that has to do with computing; his special interests are security, cryptography and data compression.

You can find his latest freeware, open source projects and articles on his website: https://www.dominik-reichl.de/.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190419.4 | Last Updated 2 Jan 2019
Article Copyright 2003 by Dominik Reichl
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid