From 4f1efc6c40062e5be4d8dbeba4bba5e64182cef3 Mon Sep 17 00:00:00 2001 From: kichik Date: Wed, 28 Aug 2002 10:07:30 +0000 Subject: [PATCH] Trimming added git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@764 212acab6-be3b-0410-9dea-997c60f758d6 --- Source/DialogTemplate.cpp | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Source/DialogTemplate.cpp b/Source/DialogTemplate.cpp index f05f532c..3a0d1689 100644 --- a/Source/DialogTemplate.cpp +++ b/Source/DialogTemplate.cpp @@ -311,6 +311,64 @@ void CDialogTemplate::DlgUnitsToPixels(short& x, short& y) { y = float(y) * (float(r.bottom)/1024); } +// Returns the size of a string in the dialog (in dialog units) +SIZE CDialogTemplate::GetStringSize(char *str) { + DWORD dwTemp; + BYTE* pbDlg = Save(dwTemp); + HWND hDlg = CreateDialogIndirect(GetModuleHandle(0), (DLGTEMPLATE*)pbDlg, 0, 0); + delete [] pbDlg; + + SIZE size; + GetTextExtentPoint32(GetDC(hDlg), str, lstrlen(str), &size); + + DestroyWindow(hDlg); + + PixelsToDlgUnits((short&)size.cx, (short&)size.cy); + + return size; +} + +// Trims the right margins of a control to fit a given text string size. +void CDialogTemplate::RTrimToString(WORD id, char *str, int margins) { + DialogItemTemplate* item = GetItem(id); + if (!item) return; + + SIZE size = GetStringSize(str); + + size.cx += margins; + + item->sWidth = size.cx; + item->sHeight = size.cy; +} + +// Trims the left margins of a control to fit a given text string size. +void CDialogTemplate::LTrimToString(WORD id, char *str, int margins) { + DialogItemTemplate* item = GetItem(id); + if (!item) return; + + SIZE size = GetStringSize(str); + + size.cx += margins; + + item->sX += item->sWidth - size.cx; + item->sWidth = size.cx; + item->sHeight = size.cy; +} + +// Trims the left and right margins of a control to fit a given text string size. +void CDialogTemplate::CTrimToString(WORD id, char *str, int margins) { + DialogItemTemplate* item = GetItem(id); + if (!item) return; + + SIZE size = GetStringSize(str); + + size.cx += margins; + + item->sX += item->sWidth/2 - size.cx/2; + item->sWidth = size.cx; + item->sHeight = size.cy; +} + // Saves the dialog in the form of DLGTEMPLATE[EX] BYTE* CDialogTemplate::Save(DWORD& dwSize) { // We need the size first to know how much memory to allocate