-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
FlatWndProc.cpp
448 lines (366 loc) · 13.9 KB
/
FlatWndProc.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* Copyright 2021 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// avoid inlining of printf()
#define _NO_CRT_STDIO_INLINE
#include <windows.h>
#include <windowsx.h>
#include <shellapi.h>
#include <jawt.h>
#include <jawt_md.h>
#include "FlatWndProc.h"
#include "com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h"
/**
* @author Karl Tauber
*/
//---- JNI methods ------------------------------------------------------------
extern "C"
JNIEXPORT jlong JNICALL Java_com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_00024WndProc_installImpl
( JNIEnv *env, jobject obj, jobject window )
{
return reinterpret_cast<jlong>( FlatWndProc::install( env, obj, window ) );
}
extern "C"
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_00024WndProc_uninstallImpl
( JNIEnv* env, jobject obj, jlong hwnd )
{
FlatWndProc::uninstall( env, obj, reinterpret_cast<HWND>( hwnd ) );
}
extern "C"
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_00024WndProc_updateFrame
( JNIEnv* env, jobject obj, jlong hwnd, jint state )
{
FlatWndProc::updateFrame( reinterpret_cast<HWND>( hwnd ), state );
}
extern "C"
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_00024WndProc_showWindow
( JNIEnv* env, jobject obj, jlong hwnd, jint cmd )
{
::ShowWindow( reinterpret_cast<HWND>( hwnd ), cmd );
}
//---- class FlatWndProc fields -----------------------------------------------
int FlatWndProc::initialized = 0;
jmethodID FlatWndProc::onNcHitTestMID;
jmethodID FlatWndProc::isFullscreenMID;
jmethodID FlatWndProc::fireStateChangedLaterOnceMID;
HWNDMap* FlatWndProc::hwndMap;
#define java_awt_Frame_ICONIFIED 1
#define java_awt_Frame_MAXIMIZED_BOTH (4 | 2)
//---- class FlatWndProc methods ----------------------------------------------
FlatWndProc::FlatWndProc() {
jvm = NULL;
env = NULL;
obj = NULL;
hwnd = NULL;
defaultWndProc = NULL;
wmSizeWParam = -1;
}
HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) {
initIDs( env, obj );
if( initialized < 0 )
return 0;
// create HWND map
if( hwndMap == NULL )
hwndMap = new HWNDMap();
// get window handle
HWND hwnd = getWindowHandle( env, window );
if( hwnd == NULL || hwndMap->get( hwnd ) != NULL )
return 0;
FlatWndProc* fwp = new FlatWndProc();
env->GetJavaVM( &fwp->jvm );
fwp->obj = env->NewGlobalRef( obj );
fwp->hwnd = hwnd;
hwndMap->put( hwnd, fwp );
// replace window procedure
fwp->defaultWndProc = reinterpret_cast<WNDPROC>(
::SetWindowLongPtr( hwnd, GWLP_WNDPROC, (LONG_PTR) FlatWndProc::StaticWindowProc ) );
return hwnd;
}
void FlatWndProc::uninstall( JNIEnv *env, jobject obj, HWND hwnd ) {
if( hwnd == NULL )
return;
FlatWndProc* fwp = (FlatWndProc*) hwndMap->get( hwnd );
if( fwp == NULL )
return;
hwndMap->remove( hwnd );
// restore original window procedure
::SetWindowLongPtr( hwnd, GWLP_WNDPROC, (LONG_PTR) fwp->defaultWndProc );
// show the OS window title bar
updateFrame( hwnd, 0 );
// cleanup
env->DeleteGlobalRef( fwp->obj );
delete fwp;
}
void FlatWndProc::initIDs( JNIEnv *env, jobject obj ) {
if( initialized )
return;
initialized = -1;
jclass cls = env->GetObjectClass( obj );
onNcHitTestMID = env->GetMethodID( cls, "onNcHitTest", "(IIZ)I" );
isFullscreenMID = env->GetMethodID( cls, "isFullscreen", "()Z" );
fireStateChangedLaterOnceMID = env->GetMethodID( cls, "fireStateChangedLaterOnce", "()V" );
// check whether all IDs were found
if( onNcHitTestMID != NULL &&
isFullscreenMID != NULL &&
fireStateChangedLaterOnceMID != NULL )
initialized = 1;
}
void FlatWndProc::updateFrame( HWND hwnd, int state ) {
// Following SetWindowPos() sends a WM_SIZE(SIZE_RESTORED) message to the window
// (although SWP_NOSIZE is set), which would prevent maximizing/minimizing
// when making the frame visible.
// AWT uses WM_SIZE wParam SIZE_RESTORED to update JFrame.extendedState and
// removes MAXIMIZED_BOTH and ICONIFIED. (see method AwtFrame::WmSize() in awt_Frame.cpp)
// To avoid this, change WM_SIZE wParam to SIZE_MAXIMIZED or SIZE_MINIMIZED if necessary.
FlatWndProc* fwp = (FlatWndProc*) hwndMap->get( hwnd );
if( fwp != NULL ) {
if( (state & java_awt_Frame_ICONIFIED) != 0 )
fwp->wmSizeWParam = SIZE_MINIMIZED;
else if( (state & java_awt_Frame_MAXIMIZED_BOTH) == java_awt_Frame_MAXIMIZED_BOTH )
fwp->wmSizeWParam = SIZE_MAXIMIZED;
else
fwp->wmSizeWParam = -1;
}
// this sends WM_NCCALCSIZE and removes/shows the window title bar
::SetWindowPos( hwnd, hwnd, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
if( fwp != NULL )
fwp->wmSizeWParam = -1;
}
LRESULT CALLBACK FlatWndProc::StaticWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
FlatWndProc* fwp = (FlatWndProc*) hwndMap->get( hwnd );
return fwp->WindowProc( hwnd, uMsg, wParam, lParam );
}
/**
* NOTE: This method is invoked on the AWT-Windows thread (not the AWT-EventQueue thread).
*/
LRESULT CALLBACK FlatWndProc::WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
switch( uMsg ) {
case WM_NCCALCSIZE:
return WmNcCalcSize( hwnd, uMsg, wParam, lParam );
case WM_NCHITTEST:
return WmNcHitTest( hwnd, uMsg, wParam, lParam );
case WM_NCRBUTTONUP:
if( wParam == HTCAPTION || wParam == HTSYSMENU )
openSystemMenu( hwnd, GET_X_LPARAM( lParam ), GET_Y_LPARAM( lParam ) );
break;
case WM_DWMCOLORIZATIONCOLORCHANGED:
fireStateChangedLaterOnce();
break;
case WM_SIZE:
if( wmSizeWParam >= 0 )
wParam = wmSizeWParam;
break;
case WM_DESTROY:
return WmDestroy( hwnd, uMsg, wParam, lParam );
}
return ::CallWindowProc( defaultWndProc, hwnd, uMsg, wParam, lParam );
}
/**
* Handle WM_DESTROY
*
* https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-destroy
*/
LRESULT FlatWndProc::WmDestroy( HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam ) {
// restore original window procedure
::SetWindowLongPtr( hwnd, GWLP_WNDPROC, (LONG_PTR) defaultWndProc );
WNDPROC defaultWndProc2 = defaultWndProc;
// cleanup
getEnv()->DeleteGlobalRef( obj );
hwndMap->remove( hwnd );
delete this;
// call original AWT window procedure because it may fire window closed event in AwtWindow::WmDestroy()
return ::CallWindowProc( defaultWndProc2, hwnd, uMsg, wParam, lParam );
}
/**
* Handle WM_NCCALCSIZE
*
* https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-nccalcsize
*
* See also NonClientIslandWindow::_OnNcCalcSize() here:
* https://github.com/microsoft/terminal/blob/main/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp
*/
LRESULT FlatWndProc::WmNcCalcSize( HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam ) {
if( wParam != TRUE )
return ::CallWindowProc( defaultWndProc, hwnd, uMsg, wParam, lParam );
NCCALCSIZE_PARAMS* params = reinterpret_cast<NCCALCSIZE_PARAMS*>( lParam );
// store the original top before the default window proc applies the default frame
int originalTop = params->rgrc[0].top;
// apply the default frame
LRESULT lResult = ::CallWindowProc( defaultWndProc, hwnd, uMsg, wParam, lParam );
if( lResult != 0 )
return lResult;
// re-apply the original top from before the size of the default frame was applied
params->rgrc[0].top = originalTop;
bool isMaximized = ::IsZoomed( hwnd );
if( isMaximized && !isFullscreen() ) {
// When a window is maximized, its size is actually a little bit more
// than the monitor's work area. The window is positioned and sized in
// such a way that the resize handles are outside of the monitor and
// then the window is clipped to the monitor so that the resize handle
// do not appear because you don't need them (because you can't resize
// a window when it's maximized unless you restore it).
params->rgrc[0].top += getResizeHandleHeight();
// check whether taskbar is in the autohide state
APPBARDATA autohide{ 0 };
autohide.cbSize = sizeof( autohide );
UINT state = (UINT) ::SHAppBarMessage( ABM_GETSTATE, &autohide );
if( (state & ABS_AUTOHIDE) != 0 ) {
// get monitor info
// (using MONITOR_DEFAULTTONEAREST finds right monitor when restoring from minimized)
HMONITOR hMonitor = ::MonitorFromWindow( hwnd, MONITOR_DEFAULTTONEAREST );
MONITORINFO monitorInfo{ 0 };
::GetMonitorInfo( hMonitor, &monitorInfo );
// If there's a taskbar on any side of the monitor, reduce our size
// a little bit on that edge.
if( hasAutohideTaskbar( ABE_TOP, monitorInfo.rcMonitor ) )
params->rgrc[0].top++;
if( hasAutohideTaskbar( ABE_BOTTOM, monitorInfo.rcMonitor ) )
params->rgrc[0].bottom--;
if( hasAutohideTaskbar( ABE_LEFT, monitorInfo.rcMonitor ) )
params->rgrc[0].left++;
if( hasAutohideTaskbar( ABE_RIGHT, monitorInfo.rcMonitor ) )
params->rgrc[0].right--;
}
}
return lResult;
}
/**
* Handle WM_NCHITTEST
*
* https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-nchittest
*
* See also NonClientIslandWindow::_OnNcHitTest() here:
* https://github.com/microsoft/terminal/blob/main/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp
*/
LRESULT FlatWndProc::WmNcHitTest( HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam ) {
// this will handle the left, right and bottom parts of the frame because we didn't change them
LRESULT lResult = ::CallWindowProc( defaultWndProc, hwnd, uMsg, wParam, lParam );
if( lResult != HTCLIENT )
return lResult;
// get window rectangle needed to convert mouse x/y from screen to window coordinates
RECT rcWindow;
::GetWindowRect( hwnd, &rcWindow );
// get mouse x/y in window coordinates
int x = GET_X_LPARAM( lParam ) - rcWindow.left;
int y = GET_Y_LPARAM( lParam ) - rcWindow.top;
int resizeBorderHeight = getResizeHandleHeight();
bool isOnResizeBorder = (y < resizeBorderHeight) &&
(::GetWindowLong( hwnd, GWL_STYLE ) & WS_THICKFRAME) != 0;
return onNcHitTest( x, y, isOnResizeBorder );
}
/**
* Returns the height of the little space at the top of the window used to
* resize the window.
*
* See also NonClientIslandWindow::_GetResizeHandleHeight() here:
* https://github.com/microsoft/terminal/blob/main/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp
*/
int FlatWndProc::getResizeHandleHeight() {
int dpi = ::GetDpiForWindow( hwnd );
// there isn't a SM_CYPADDEDBORDER for the Y axis
return ::GetSystemMetricsForDpi( SM_CXPADDEDBORDER, dpi )
+ ::GetSystemMetricsForDpi( SM_CYSIZEFRAME, dpi );
}
/**
* Returns whether there is an autohide taskbar on the given edge.
*/
bool FlatWndProc::hasAutohideTaskbar( UINT edge, RECT rcMonitor ) {
APPBARDATA data{ 0 };
data.cbSize = sizeof( data );
data.uEdge = edge;
data.rc = rcMonitor;
HWND hTaskbar = (HWND) ::SHAppBarMessage( ABM_GETAUTOHIDEBAREX, &data );
return hTaskbar != nullptr;
}
BOOL FlatWndProc::isFullscreen() {
JNIEnv* env = getEnv();
if( env == NULL )
return FALSE;
return env->CallBooleanMethod( obj, isFullscreenMID );
}
int FlatWndProc::onNcHitTest( int x, int y, boolean isOnResizeBorder ) {
JNIEnv* env = getEnv();
if( env == NULL )
return isOnResizeBorder ? HTTOP : HTCLIENT;
return env->CallIntMethod( obj, onNcHitTestMID, (jint) x, (jint) y, (jboolean) isOnResizeBorder );
}
void FlatWndProc::fireStateChangedLaterOnce() {
JNIEnv* env = getEnv();
if( env == NULL )
return;
env->CallVoidMethod( obj, fireStateChangedLaterOnceMID );
}
// similar to JNU_GetEnv() in jni_util.c
JNIEnv* FlatWndProc::getEnv() {
if( env != NULL )
return env;
jvm->GetEnv( (void **) &env, JNI_VERSION_1_2 );
return env;
}
/**
* Opens the window's system menu.
* The system menu is the menu that opens when the user presses Alt+Space or
* right clicks on the title bar
*/
void FlatWndProc::openSystemMenu( HWND hwnd, int x, int y ) {
// get system menu
HMENU systemMenu = ::GetSystemMenu( hwnd, false );
// update system menu
LONG style = ::GetWindowLong( hwnd, GWL_STYLE );
bool isMaximized = ::IsZoomed( hwnd );
setMenuItemState( systemMenu, SC_RESTORE, isMaximized );
setMenuItemState( systemMenu, SC_MOVE, !isMaximized );
setMenuItemState( systemMenu, SC_SIZE, (style & WS_THICKFRAME) != 0 && !isMaximized );
setMenuItemState( systemMenu, SC_MINIMIZE, (style & WS_MINIMIZEBOX) != 0 );
setMenuItemState( systemMenu, SC_MAXIMIZE, (style & WS_MAXIMIZEBOX) != 0 && !isMaximized );
setMenuItemState( systemMenu, SC_CLOSE, true );
// make "Close" item the default to be consistent with the system menu shown
// when pressing Alt+Space
::SetMenuDefaultItem( systemMenu, SC_CLOSE, 0 );
// show system menu
int ret = ::TrackPopupMenu( systemMenu, TPM_RETURNCMD, x, y, 0, hwnd, nullptr );
if( ret != 0 )
::PostMessage( hwnd, WM_SYSCOMMAND, ret, 0 );
}
void FlatWndProc::setMenuItemState( HMENU systemMenu, int item, bool enabled ) {
MENUITEMINFO mii{ 0 };
mii.cbSize = sizeof( mii );
mii.fMask = MIIM_STATE;
mii.fType = MFT_STRING;
mii.fState = enabled ? MF_ENABLED : MF_DISABLED;
::SetMenuItemInfo( systemMenu, item, FALSE, &mii );
}
HWND FlatWndProc::getWindowHandle( JNIEnv* env, jobject window ) {
JAWT awt;
awt.version = JAWT_VERSION_1_4;
if( !JAWT_GetAWT( env, &awt ) )
return 0;
jawt_DrawingSurface* ds = awt.GetDrawingSurface( env, window );
if( ds == NULL )
return 0;
jint lock = ds->Lock( ds );
if( (lock & JAWT_LOCK_ERROR) != 0 ) {
awt.FreeDrawingSurface( ds );
return 0;
}
JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo( ds );
JAWT_Win32DrawingSurfaceInfo* wdsi = (JAWT_Win32DrawingSurfaceInfo*) dsi->platformInfo;
HWND hwnd = wdsi->hwnd;
ds->FreeDrawingSurfaceInfo( dsi );
ds->Unlock( ds );
awt.FreeDrawingSurface( ds );
return hwnd;
}