From df01605acfda4abba3ddad02457f785ade501d28 Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Thu, 2 Aug 2018 00:14:05 +0200 Subject: [PATCH] + fix: DPI Awareness & Fractional Font Size --- language/np3_af_za/dialogs_af_za.rc | Bin 73224 -> 73240 bytes language/np3_de_de/dialogs_de_de.rc | Bin 74430 -> 74446 bytes language/np3_en_gb/dialogs_en_gb.rc | Bin 73276 -> 73292 bytes language/np3_en_us/dialogs_en_us.rc | Bin 73088 -> 73104 bytes language/np3_es_es/dialogs_es_es.rc | Bin 73340 -> 73356 bytes language/np3_fr_fr/dialogs_fr_fr.rc | Bin 74812 -> 74828 bytes language/np3_nl_nl/dialogs_nl_nl.rc | Bin 73248 -> 73264 bytes language/np3_zh_cn/dialogs_zh_cn.rc | Bin 68222 -> 68238 bytes res/Notepad3.exe.manifest | 8 ++- res/Notepad3.exe.manifest.conf | 2 +- src/Dialogs.c | 3 ++ src/Helpers.c | 78 ++++++++++++++++++++++++++-- src/Helpers.h | 6 ++- src/Notepad3.c | 65 +++++++++++++++++++++-- src/Notepad3.h | 1 + src/Styles.c | 18 ++++--- 16 files changed, 164 insertions(+), 17 deletions(-) diff --git a/language/np3_af_za/dialogs_af_za.rc b/language/np3_af_za/dialogs_af_za.rc index 3f7ba21944d8b974ce708369bbe15d9983937055..bba0f828815b142a209197b37865d2addd6e0d93 100644 GIT binary patch delta 34 ocmeC!!!lzJ%Z4@O!qE)c4EhYZ46zJ(K&Z!%%aFTyeR8T1)+8DbgofKZPimmzoa|BC%90oHa53jhEB delta 18 acmX?ilx5#hmJNR@nDZF&HuG2RUkLzLF9^i| diff --git a/language/np3_en_gb/dialogs_en_gb.rc b/language/np3_en_gb/dialogs_en_gb.rc index c8a035e6ff57dc104e8d288fb63120364c8715fa..577120888dd507687e8734640acdc03752d6787f 100644 GIT binary patch delta 34 ocmdn9hvm#3mJR31g`*j?8T1)+8DbgofKZPimmzoa#q#WB0L^0yKmY&$ delta 18 acmX@Jhh@(mmJR31ne!O(Ha{%SUIqYGX$biM diff --git a/language/np3_en_us/dialogs_en_us.rc b/language/np3_en_us/dialogs_en_us.rc index 744a3586ff9cc4f790f65e4d6054c9a080f3076a..5300f2db8e0304bcf27bc73a87930b452e4bfb1f 100644 GIT binary patch delta 34 ocmZqJ%`#y(%La{d;b;bJ27LxyhFFF?Ak<^XWysyET^_m&0I8x0WB>pF delta 18 ZcmbQRo26kl%La{d<~)YH&DQ0i%K${E2OR(a diff --git a/language/np3_es_es/dialogs_es_es.rc b/language/np3_es_es/dialogs_es_es.rc index d969ab33f79648ecde77ac6b061f6f87852342fc..b9e411fbcd320be0d1ab7180ee57068e7c24cdc1 100644 GIT binary patch delta 34 ocmeyfhoxsP%LbMT;b;bJ27LxyhFFF?Ak<^XWysykUQxOX0JZ!Hxc~qF delta 18 ZcmeC#%kpOr%LbMT<~)YH&EgfM%K%CR2XX)a diff --git a/language/np3_fr_fr/dialogs_fr_fr.rc b/language/np3_fr_fr/dialogs_fr_fr.rc index 2a3ae86d78b33ca8fde22728be739d90491fdac9..7fea931aa700ee24d30d313b0c05e476208a0ff9 100644 GIT binary patch delta 34 ocmdmUg5}H!mJKac!qE)c4EhYZ46zJ(K&Z!%%aFUdy=v=90LV`Z7XSbN delta 18 acmX?ef@RMMmJKac%y|rXo2OT8T?qhGMF`LU diff --git a/language/np3_nl_nl/dialogs_nl_nl.rc b/language/np3_nl_nl/dialogs_nl_nl.rc index 8d2595799b90a9f21b93ecfa780ed1c0d4a69c29..4b567b0457955831a5faf742442a9edb9af6ae96 100644 GIT binary patch delta 34 ncmZ3mhh+m0ohTQMX3%EPXV7JcWyk|UJ%(I{+|8%ULze*n#bFBO delta 18 acmdn6hh@PYmJKJ$ne!O(Hs3A}T?PPBM+lz) diff --git a/language/np3_zh_cn/dialogs_zh_cn.rc b/language/np3_zh_cn/dialogs_zh_cn.rc index 2c55baa96f18b1825e840f6347914a5b4eb1a26c..5838ab597d94cd329053207712a898078180b7c6 100644 GIT binary patch delta 34 ocmex2g{5yQ%LcnV;b;bJ27LxyhFFF?Ak<^XWysy^m}ff&0J0DXivR!s delta 18 ZcmeC1%JOdt%LcnV<~)YH&4GEga{x!#2Sfk> diff --git a/res/Notepad3.exe.manifest b/res/Notepad3.exe.manifest index c6c427f95..11c2c9396 100644 --- a/res/Notepad3.exe.manifest +++ b/res/Notepad3.exe.manifest @@ -31,7 +31,13 @@ - true + true/PM + + + PerMonitorV2,PerMonitor + + + false diff --git a/res/Notepad3.exe.manifest.conf b/res/Notepad3.exe.manifest.conf index 0b61f888f..0a34ff852 100644 --- a/res/Notepad3.exe.manifest.conf +++ b/res/Notepad3.exe.manifest.conf @@ -3,7 +3,7 @@ Notepad3 X_MUI diff --git a/src/Dialogs.c b/src/Dialogs.c index 60e25a7fc..9bde2b665 100644 --- a/src/Dialogs.c +++ b/src/Dialogs.c @@ -2239,6 +2239,7 @@ INT_PTR CALLBACK SelectDefEncodingDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR if (g_hDlgIcon) { SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)g_hDlgIcon); } hbmp = LoadImage(g_hInstance,MAKEINTRESOURCE(IDB_ENCODING),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION); + hbmp = ResizeImageForCurrentDPI(hbmp); himl = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0); ImageList_AddMasked(himl,hbmp,CLR_DEFAULT); @@ -2364,6 +2365,8 @@ INT_PTR CALLBACK SelectEncodingDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM hwndLV = GetDlgItem(hwnd,IDC_ENCODINGLIST); hbmp = LoadImage(g_hInstance,MAKEINTRESOURCE(IDB_ENCODING),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION); + hbmp = ResizeImageForCurrentDPI(hbmp); + himl = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0); ImageList_AddMasked(himl,hbmp,CLR_DEFAULT); DeleteObject(hbmp); diff --git a/src/Helpers.c b/src/Helpers.c index 45dac58e9..cc5f04bbc 100644 --- a/src/Helpers.c +++ b/src/Helpers.c @@ -43,6 +43,7 @@ extern HINSTANCE g_hInstance; extern HMODULE g_hLngResContainer; extern LANGID g_iPrefLngLocID; +extern UINT g_uCurrentDPI; //============================================================================= // @@ -300,6 +301,70 @@ DWORD GetLastErrorToMsgBox(LPWSTR lpszFunction, DWORD dwErrID) } + +//============================================================================= +// +// GetCurrentDPI() +// +UINT GetCurrentDPI(HWND hwnd) { + UINT dpi = 0; + if (IsWin10()) { + FARPROC pfnGetDpiForWindow = GetProcAddress(GetModuleHandle(L"user32.dll"), "GetDpiForWindow"); + if (pfnGetDpiForWindow) { + dpi = (UINT)pfnGetDpiForWindow(hwnd); + } + } + + if (dpi == 0 && IsWin81()) { + HMODULE hShcore = LoadLibrary(L"shcore.dll"); + if (hShcore) { + FARPROC pfnGetDpiForMonitor = GetProcAddress(hShcore, "GetDpiForMonitor"); + if (pfnGetDpiForMonitor) { + HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + UINT dpiX = 0, dpiY = 0; + if (pfnGetDpiForMonitor(hMonitor, 0 /* MDT_EFFECTIVE_DPI */, &dpiX, &dpiY) == S_OK) { + dpi = dpiX; + } + } + FreeLibrary(hShcore); + } + } + + if (dpi == 0) { + // FIXME: seems always get 96 + HDC hDC = GetDC(hwnd); + dpi = GetDeviceCaps(hDC, LOGPIXELSX); + ReleaseDC(hwnd, hDC); + } + + dpi = max(dpi, USER_DEFAULT_SCREEN_DPI); + return dpi; +} + + + +//============================================================================= +// +// ResizeImageForCurrentDPI() +// +HBITMAP ResizeImageForCurrentDPI(HBITMAP hbmp) +{ + if (hbmp) { + BITMAP bmp; + if (g_uCurrentDPI > USER_DEFAULT_SCREEN_DPI && GetObject(hbmp, sizeof(BITMAP), &bmp)) { + int width = MulDiv(bmp.bmWidth, g_uCurrentDPI, USER_DEFAULT_SCREEN_DPI); + int height = MulDiv(bmp.bmHeight, g_uCurrentDPI, USER_DEFAULT_SCREEN_DPI); + HBITMAP hCopy = CopyImage(hbmp, IMAGE_BITMAP, width, height, LR_COPYRETURNORG | LR_COPYDELETEORG); + if (hCopy) { + hbmp = hCopy; + } + } + } + return hbmp; +} + + + //============================================================================= // // PrivateIsAppThemed() @@ -952,6 +1017,7 @@ void MakeBitmapButton(HWND hwnd,int nCtlId,HINSTANCE hInstance,UINT uBmpId) BITMAP bmp; BUTTON_IMAGELIST bi; HBITMAP hBmp = LoadImage(hInstance,MAKEINTRESOURCE(uBmpId),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION); + hBmp = ResizeImageForCurrentDPI(hBmp); GetObject(hBmp,sizeof(BITMAP),&bmp); bi.himl = ImageList_Create(bmp.bmWidth,bmp.bmHeight,ILC_COLOR32|ILC_MASK,1,0); ImageList_AddMasked(bi.himl,hBmp,CLR_DEFAULT); @@ -962,6 +1028,7 @@ void MakeBitmapButton(HWND hwnd,int nCtlId,HINSTANCE hInstance,UINT uBmpId) } + //============================================================================= // // MakeColorPickButton() @@ -1033,11 +1100,14 @@ void DeleteBitmapButton(HWND hwnd,int nCtlId) // // SendWMSize() // -LRESULT SendWMSize(HWND hwnd) +LRESULT SendWMSize(HWND hwnd, RECT* rc) { - RECT rc; - GetClientRect(hwnd, &rc); - return (SendMessage(hwnd, WM_SIZE, SIZE_RESTORED, MAKELPARAM(rc.right, rc.bottom))); + if (!rc) { + RECT _rc; + GetClientRect(hwnd, &_rc); + return (SendMessage(hwnd, WM_SIZE, SIZE_RESTORED, MAKELPARAM(_rc.right, _rc.bottom))); + } + return (SendMessage(hwnd, WM_SIZE, SIZE_RESTORED, MAKELPARAM(rc->right, rc->bottom))); } diff --git a/src/Helpers.h b/src/Helpers.h index 560cf141b..f865f111f 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -28,7 +28,6 @@ extern WCHAR g_wchIniFile[MAX_PATH]; - // ============================================================================ #define STRGFY(X) L##(X) @@ -132,6 +131,9 @@ DWORD GetLastErrorToMsgBox(LPWSTR lpszFunction, DWORD dwErrID); bool SetClipboardTextW(HWND, LPCWSTR); +UINT GetCurrentDPI(HWND hwnd); +HBITMAP ResizeImageForCurrentDPI(HBITMAP hbmp); + bool PrivateIsAppThemed(); HRESULT PrivateSetCurrentProcessExplicitAppUserModelID(PCWSTR); bool IsElevated(); @@ -174,7 +176,7 @@ LONG StatusCalcPaneWidth(HWND,LPCWSTR); int Toolbar_GetButtons(HWND,int,LPWSTR,int); int Toolbar_SetButtons(HWND,int,LPCWSTR,void*,int); -LRESULT SendWMSize(HWND); +LRESULT SendWMSize(HWND, RECT*); bool IsCmdEnabled(HWND, UINT); diff --git a/src/Notepad3.c b/src/Notepad3.c index 3c0fb1876..084971b51 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -67,6 +67,7 @@ HWND g_hwndReBar = NULL; HWND hwndEditFrame = NULL; HWND hwndNextCBChain = NULL; HICON g_hDlgIcon = NULL; +UINT g_uCurrentDPI = USER_DEFAULT_SCREEN_DPI; bool g_bExternalBitmap = false; @@ -1414,6 +1415,10 @@ LRESULT CALLBACK MainWndProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam) MsgThemeChanged(hwnd,wParam,lParam); break; + case WM_DPICHANGED: + MsgDPIChanged(hwnd, wParam, lParam); + break; + // update Scintilla colors case WM_SYSCOLORCHANGE: UpdateLineNumberWidth(); @@ -1710,6 +1715,8 @@ LRESULT MsgCreate(HWND hwnd,WPARAM wParam,LPARAM lParam) HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance; + g_uCurrentDPI = GetCurrentDPI(hwnd); + // Setup edit control g_hwndEdit = CreateWindowEx( WS_EX_CLIENTEDGE, @@ -1870,6 +1877,7 @@ void CreateBars(HWND hwnd,HINSTANCE hInstance) if (!SearchPath(NULL,g_tchToolbarBitmap,L".bmp",COUNTOF(szTmp),szTmp,NULL)) StringCchCopy(szTmp,COUNTOF(szTmp),g_tchToolbarBitmap); hbmp = LoadImage(NULL,szTmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE); + hbmp = ResizeImageForCurrentDPI(hbmp); } if (hbmp) { @@ -1878,8 +1886,10 @@ void CreateBars(HWND hwnd,HINSTANCE hInstance) else { LPWSTR toolBarIntRes = (iHighDpiToolBar > 0) ? MAKEINTRESOURCE(IDR_MAINWNDTB2) : MAKEINTRESOURCE(IDR_MAINWNDTB); hbmp = LoadImage(hInstance, toolBarIntRes, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION); + hbmp = ResizeImageForCurrentDPI(hbmp); hbmpCopy = CopyImage(hbmp, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION); } + GetObject(hbmp,sizeof(BITMAP),&bmp); if (!IsXP()) BitmapMergeAlpha(hbmp,GetSysColor(COLOR_3DFACE)); @@ -1896,6 +1906,7 @@ void CreateBars(HWND hwnd,HINSTANCE hInstance) StringCchCopy(szTmp,COUNTOF(szTmp),g_tchToolbarBitmapHot); hbmp = LoadImage(NULL, szTmp, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE); + hbmp = ResizeImageForCurrentDPI(hbmp); if (hbmp) { GetObject(hbmp,sizeof(BITMAP),&bmp); @@ -1914,6 +1925,7 @@ void CreateBars(HWND hwnd,HINSTANCE hInstance) StringCchCopy(szTmp,COUNTOF(szTmp),g_tchToolbarBitmapDisabled); hbmp = LoadImage(NULL, szTmp, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE); + hbmp = ResizeImageForCurrentDPI(hbmp); if (hbmp) { GetObject(hbmp,sizeof(BITMAP),&bmp); @@ -2092,6 +2104,46 @@ void MsgEndSession(HWND hwnd, UINT umsg) } + +//============================================================================= +// +// MsgDPIChanged() - Handle WM_DPICHANGED +// +// +void MsgDPIChanged(HWND hwnd, WPARAM wParam, LPARAM lParam) +{ + g_uCurrentDPI = HIWORD(wParam); + DocPos const pos = SciCall_GetCurrentPos(); + + HINSTANCE hInstance = (HINSTANCE)(INT_PTR)GetWindowLongPtr(hwnd, GWLP_HINSTANCE); + +#if 0 + char buf[64]; + sprintf_s(buf, COUNTOF(buf), "WM_DPICHANGED: dpi=%u\n", g_uCurrentDPI); + SendMessage(g_hwndEdit, SCI_INSERTTEXT, 0, (LPARAM)buf); +#endif + + Style_ResetCurrentLexer(g_hwndEdit); + SciCall_GotoPos(pos); + + // recreate toolbar and statusbar + Toolbar_GetButtons(g_hwndToolbar, IDT_FILE_NEW, g_tchToolbarButtons, COUNTOF(g_tchToolbarButtons)); + + DestroyWindow(g_hwndToolbar); + DestroyWindow(g_hwndReBar); + DestroyWindow(g_hwndStatus); + CreateBars(hwnd, hInstance); + + RECT* const rc = (RECT*)lParam; + SendWMSize(hwnd, rc); + + UpdateUI(); + UpdateToolbar(); + UpdateStatusbar(true); + UpdateLineNumberWidth(); +} + + //============================================================================= // // MsgThemeChanged() - Handle WM_THEMECHANGED @@ -2147,7 +2199,7 @@ void MsgThemeChanged(HWND hwnd,WPARAM wParam,LPARAM lParam) DestroyWindow(g_hwndStatus); CreateBars(hwnd,hInstance); - SendWMSize(hwnd); + SendWMSize(hwnd, NULL); EditFinalizeStyling(g_hwndEdit, -1); @@ -4816,7 +4868,7 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) UpdateToolbar(); ShowWindow(g_hwndReBar,SW_SHOW); } - SendWMSize(hwnd); + SendWMSize(hwnd, NULL); break; @@ -4840,7 +4892,7 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) UpdateStatusbar(false); ShowWindow(g_hwndStatus,SW_SHOW); } - SendWMSize(hwnd); + SendWMSize(hwnd, NULL); break; @@ -8225,9 +8277,16 @@ void UpdateLineNumberWidth() else { SciCall_SetMarginWidthN(MARGIN_SCI_LINENUM, 0); } + + int const widthBM = g_bShowSelectionMargin ? MulDiv(16, g_uCurrentDPI, USER_DEFAULT_SCREEN_DPI) : 0; + SciCall_SetMarginWidthN(MARGIN_SCI_BOOKMRK, widthBM); + + int const widthCF = (g_bCodeFoldingAvailable && g_bShowCodeFolding) ? MulDiv(13, g_uCurrentDPI, USER_DEFAULT_SCREEN_DPI) : 0; + SciCall_SetMarginWidthN(MARGIN_SCI_FOLDING, widthCF); } + //============================================================================= // // UpdateSettingsCmds() diff --git a/src/Notepad3.h b/src/Notepad3.h index da6a1ea12..603ecd0b3 100644 --- a/src/Notepad3.h +++ b/src/Notepad3.h @@ -172,6 +172,7 @@ LRESULT MsgCreate(HWND, WPARAM, LPARAM); void MsgEndSession(HWND, UINT); void CreateBars(HWND, HINSTANCE); void MsgThemeChanged(HWND, WPARAM, LPARAM); +void MsgDPIChanged(HWND, WPARAM, LPARAM); void MsgSize(HWND, WPARAM, LPARAM); void MsgDropFiles(HWND, WPARAM, LPARAM); LRESULT MsgCopyData(HWND, WPARAM, LPARAM); diff --git a/src/Styles.c b/src/Styles.c index 523ba6b0c..d5c479487 100644 --- a/src/Styles.c +++ b/src/Styles.c @@ -50,6 +50,7 @@ extern HICON g_hDlgIcon; extern HWND g_hwndMain; extern HWND g_hwndDlgCustomizeSchemes; extern EDITFINDREPLACE g_efrData; +extern UINT g_uCurrentDPI; extern int g_iSciFontQuality; extern const int FontQuality[4]; @@ -5566,6 +5567,14 @@ bool Style_SelectColor(HWND hwnd,bool bForeGround,LPWSTR lpszStyle,int cchStyle, } +//============================================================================= + +inline static int _GetDPIAwareFractFontSize(float fFontSize) +{ + return MulDiv((int)(fFontSize + 0.5), (SC_FONT_SIZE_MULTIPLIER * g_uCurrentDPI), USER_DEFAULT_SCREEN_DPI); +} + + //============================================================================= // // Style_SetStyles() @@ -5631,13 +5640,10 @@ void Style_SetStyles(HWND hwnd, int iStyle, LPCWSTR lpszStyle, bool bInitDefault } // Size values are relative to BaseFontSize/CurrentFontSize - POINT dpi = GetSystemDpi(); float fBaseFontSize = _GetCurrentFontSize(); + fBaseFontSize = (float)max(0.0, fBaseFontSize); if (Style_StrGetSize(lpszStyle, &fBaseFontSize)) { - fBaseFontSize = (float)max(0.0, fBaseFontSize); - fBaseFontSize = (float)MulDiv((int)fBaseFontSize, (dpi.x + dpi.y)/2, USER_DEFAULT_SCREEN_DPI); - //SendMessage(hwnd, SCI_STYLESETSIZE, iStyle, (int)fBaseFontSize); - SendMessage(hwnd, SCI_STYLESETSIZEFRACTIONAL, iStyle, (LPARAM)((int)(fBaseFontSize * SC_FONT_SIZE_MULTIPLIER + 0.5))); + SendMessage(hwnd, SCI_STYLESETSIZEFRACTIONAL, iStyle, (LPARAM)_GetDPIAwareFractFontSize(fBaseFontSize)); if (iStyle == STYLE_DEFAULT) { if (bInitDefault) { _SetBaseFontSize(fBaseFontSize); @@ -5647,7 +5653,7 @@ void Style_SetStyles(HWND hwnd, int iStyle, LPCWSTR lpszStyle, bool bInitDefault } else if (bInitDefault) { //SendMessage(hwnd, SCI_STYLESETSIZE, STYLE_DEFAULT, (LPARAM)((int)fBaseFontSize)); - SendMessage(hwnd, SCI_STYLESETSIZEFRACTIONAL, STYLE_DEFAULT, (LPARAM)((int)(fBaseFontSize * SC_FONT_SIZE_MULTIPLIER + 0.5))); + SendMessage(hwnd, SCI_STYLESETSIZEFRACTIONAL, STYLE_DEFAULT, (LPARAM)_GetDPIAwareFractFontSize(fBaseFontSize)); } // Character Set