33#include " fb_tooltip.h"
44
55#include < js_engine/js_to_native_invoker.h>
6+
67#include < js_utils/js_error_helper.h>
8+ #include < js_utils/js_hwnd_helpers.h>
79#include < js_utils/js_object_helper.h>
810
911#include < qwr/final_action.h>
@@ -81,9 +83,9 @@ JsFbTooltip::JsFbTooltip( JSContext* cx, HWND hParentWnd )
8183 : pJsCtx_( cx )
8284 , hParentWnd_( hParentWnd )
8385 , tipBuffer_( TEXT( SMP_NAME ) )
84- , pFont_( smp::gdi::CreateUniquePtr<HFONT>( nullptr ) )
8586{
86- hTooltipWnd_ = CreateWindowEx (
87+ hTooltipWnd_ = CreateWindowEx
88+ (
8789 WS_EX_TOPMOST,
8890 TOOLTIPS_CLASS,
8991 nullptr ,
@@ -95,14 +97,14 @@ JsFbTooltip::JsFbTooltip( JSContext* cx, HWND hParentWnd )
9597 hParentWnd_,
9698 nullptr ,
9799 core_api::get_my_instance (),
98- nullptr );
100+ nullptr
101+ );
102+
99103 qwr::error::CheckWinApi ( hTooltipWnd_, " CreateWindowEx" );
100104
101105 qwr::final_action autoHwnd ( [hTooltipWnd = hTooltipWnd_] {
102106 if ( IsWindow ( hTooltipWnd ) )
103- {
104107 DestroyWindow ( hTooltipWnd );
105- }
106108 } );
107109
108110 // Original position
@@ -173,46 +175,39 @@ void JsFbTooltip::SetDelayTime( uint32_t type, int32_t time )
173175 SendMessage ( hTooltipWnd_, TTM_SETDELAYTIME, type, static_cast <LPARAM>( static_cast <int >( MAKELONG ( time, 0 ) ) ) );
174176}
175177
176- void JsFbTooltip::SetFont ( const std::wstring& name, uint32_t pxSize , uint32_t style )
178+ void JsFbTooltip::SetFont ( const std::wstring& fontName, int32_t fontSize , uint32_t fontStyle )
177179{
178- fontName_ = name;
179- fontSize_ = pxSize;
180- fontStyle_ = style;
180+ LOGFONTW logfont;
181+ logfont::Make ( fontName, fontSize, fontStyle, logfont );
181182
182- if ( !fontName_.empty () )
183- {
184- pFont_.reset ( CreateFont (
185- // from msdn: "< 0, The font mapper transforms this value into device units
186- // and matches its absolute value against the character height of the available fonts."
187- -static_cast <int >( fontSize_ ),
188- 0 ,
189- 0 ,
190- 0 ,
191- ( fontStyle_ & Gdiplus::FontStyleBold ) ? FW_BOLD : FW_NORMAL,
192- ( fontStyle_ & Gdiplus::FontStyleItalic ) ? TRUE : FALSE ,
193- ( fontStyle_ & Gdiplus::FontStyleUnderline ) ? TRUE : FALSE ,
194- ( fontStyle_ & Gdiplus::FontStyleStrikeout ) ? TRUE : FALSE ,
195- DEFAULT_CHARSET,
196- OUT_DEFAULT_PRECIS,
197- CLIP_DEFAULT_PRECIS,
198- DEFAULT_QUALITY,
199- DEFAULT_PITCH | FF_DONTCARE,
200- fontName_.c_str () ) );
201- qwr::error::CheckWinApi ( !!pFont_, " CreateFont" );
202- SendMessage ( hTooltipWnd_, WM_SETFONT, (WPARAM)pFont_.get (), MAKELPARAM ( FALSE , 0 ) );
203- }
183+ #if FONT_CACHE_ABSOLUTE_HEIGHT
184+ const HWND wnd = GetPanelHwndForCurrentGlobal ( pJsCtx_ );
185+ const HDC dc = GetDC ( wnd );
186+ qwr::final_action autoHdcReleaser ( [wnd, dc] { ReleaseDC ( wnd, dc ); } );
187+ logfont::Normalize ( dc, logfont );
188+ #endif
189+
190+ fontcache::shared_hfont hfont = fontcache::Cache ( logfont );
191+
192+ if ( font == hfont )
193+ return ;
194+
195+ font = hfont;
196+
197+ SendMessage ( hTooltipWnd_, WM_SETFONT, (WPARAM)font.get (), MAKELPARAM ( FALSE , 0 ) );
204198}
205199
206- void JsFbTooltip::SetFontWithOpt ( size_t optArgCount, const std::wstring& name, uint32_t pxSize, uint32_t style )
200+ void JsFbTooltip::SetFontWithOpt ( size_t optArgCount,
201+ const std::wstring& fontName, int32_t fontSize, uint32_t fontStyle )
207202{
208203 switch ( optArgCount )
209204 {
210205 case 0 :
211- return SetFont ( name, pxSize, style );
206+ return SetFont ( fontName, fontSize, fontStyle );
212207 case 1 :
213- return SetFont ( name, pxSize );
208+ return SetFont ( fontName, fontSize );
214209 case 2 :
215- return SetFont ( name );
210+ return SetFont ( fontName );
216211 default :
217212 throw qwr::QwrException ( " Internal error: invalid number of optional arguments specified: {}" , optArgCount );
218213 }
0 commit comments