Skip to content

Commit c377a80

Browse files
Setting back the original state in finally block
1 parent be93787 commit c377a80

File tree

2 files changed

+121
-115
lines changed

2 files changed

+121
-115
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ColorDialog.java

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -233,66 +233,69 @@ public RGB open () {
233233

234234
display.externalEventLoop = true;
235235
display.sendPreExternalEventDispatchEvent ();
236-
237-
/* Temporarily setting the thread dpi awareness to gdi scaling because window dialog has weird resize handling */
238236
long currentDpiAwarenessContext = OS.GetThreadDpiAwarenessContext();
239-
if(display.isRescalingAtRuntime()) {
240-
currentDpiAwarenessContext = OS.SetThreadDpiAwarenessContext(OS.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED);
241-
}
237+
boolean success = false;
238+
try {
239+
/*
240+
* Temporarily setting the thread dpi awareness to gdi scaling because window
241+
* dialog has weird resize handling
242+
*/
243+
if (display.isRescalingAtRuntime()) {
244+
currentDpiAwarenessContext = OS.SetThreadDpiAwarenessContext(OS.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED);
245+
}
242246

243-
/* Open the dialog */
244-
boolean success = OS.ChooseColor (lpcc);
245-
display.externalEventLoop = false;
246-
display.sendPostExternalEventDispatchEvent ();
247+
/* Open the dialog */
248+
success = OS.ChooseColor(lpcc);
249+
display.externalEventLoop = false;
250+
display.sendPostExternalEventDispatchEvent();
247251

248-
/* Clear the temporary dialog modal parent */
249-
if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
250-
display.setModalDialog (oldModal);
251-
}
252+
/* Clear the temporary dialog modal parent */
253+
if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
254+
display.setModalDialog(oldModal);
255+
}
252256

253-
/* Get the Custom Colors (if the user defined any) from the dialog */
254-
boolean customColor = false;
255-
OS.MoveMemory (colors, display.lpCustColors, colors.length * 4);
256-
for (int color : colors) {
257-
if (color != 0x00FFFFFF) {
258-
customColor = true;
259-
break;
257+
/* Get the Custom Colors (if the user defined any) from the dialog */
258+
boolean customColor = false;
259+
OS.MoveMemory(colors, display.lpCustColors, colors.length * 4);
260+
for (int color : colors) {
261+
if (color != 0x00FFFFFF) {
262+
customColor = true;
263+
break;
264+
}
260265
}
261-
}
262-
if (customColor) {
263-
rgbs = new RGB [CUSTOM_COLOR_COUNT];
264-
for (int i=0; i<colors.length; i++) {
265-
int color = colors[i];
266-
int red = color & 0xFF;
267-
int green = (color >> 8) & 0xFF;
268-
int blue = (color >> 16) & 0xFF;
269-
rgbs[i] = new RGB (red, green, blue);
266+
if (customColor) {
267+
rgbs = new RGB[CUSTOM_COLOR_COUNT];
268+
for (int i = 0; i < colors.length; i++) {
269+
int color = colors[i];
270+
int red = color & 0xFF;
271+
int green = (color >> 8) & 0xFF;
272+
int blue = (color >> 16) & 0xFF;
273+
rgbs[i] = new RGB(red, green, blue);
274+
}
270275
}
271-
}
272-
273-
if (success) {
274-
int red = lpcc.rgbResult & 0xFF;
275-
int green = (lpcc.rgbResult >> 8) & 0xFF;
276-
int blue = (lpcc.rgbResult >> 16) & 0xFF;
277-
rgb = new RGB (red, green, blue);
278-
}
279-
280-
/* Reset the dpi awareness context */
281-
if(display.isRescalingAtRuntime()) {
282-
OS.SetThreadDpiAwarenessContext(currentDpiAwarenessContext);
283-
}
284276

285-
/* Free the CCHookProc */
286-
callback.dispose ();
277+
if (success) {
278+
int red = lpcc.rgbResult & 0xFF;
279+
int green = (lpcc.rgbResult >> 8) & 0xFF;
280+
int blue = (lpcc.rgbResult >> 16) & 0xFF;
281+
rgb = new RGB(red, green, blue);
282+
}
283+
} finally {
284+
/* Reset the dpi awareness context */
285+
if (display.isRescalingAtRuntime()) {
286+
OS.SetThreadDpiAwarenessContext(currentDpiAwarenessContext);
287+
}
288+
/* Free the CCHookProc */
289+
callback.dispose ();
290+
/* Destroy the BIDI orientation window */
291+
if (hwndParent != hwndOwner) {
292+
if (enabled) OS.EnableWindow (hwndParent, true);
293+
OS.SetActiveWindow (hwndParent);
294+
OS.DestroyWindow (hwndOwner);
295+
}
287296

288-
/* Destroy the BIDI orientation window */
289-
if (hwndParent != hwndOwner) {
290-
if (enabled) OS.EnableWindow (hwndParent, true);
291-
OS.SetActiveWindow (hwndParent);
292-
OS.DestroyWindow (hwndOwner);
297+
if (!success) return null;
293298
}
294-
295-
if (!success) return null;
296299
return rgb;
297300
}
298301

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/FontDialog.java

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -226,82 +226,85 @@ public FontData open () {
226226

227227
display.externalEventLoop = true;
228228
display.sendPreExternalEventDispatchEvent ();
229-
230-
/* Temporarily setting the thread dpi awareness to gdi scaling because window dialog has weird resize handling */
231229
long currentDpiAwarenessContext = OS.GetThreadDpiAwarenessContext();
232-
if(display.isRescalingAtRuntime()) {
233-
currentDpiAwarenessContext = OS.SetThreadDpiAwarenessContext(OS.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED);
234-
}
230+
boolean success = false;
231+
try {
232+
/*
233+
* Temporarily setting the thread dpi awareness to gdi scaling because window
234+
* dialog has weird resize handling
235+
*/
236+
if (display.isRescalingAtRuntime()) {
237+
currentDpiAwarenessContext = OS.SetThreadDpiAwarenessContext(OS.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED);
238+
}
235239

236-
/* Open the dialog */
237-
boolean success = OS.ChooseFont (lpcf);
238-
display.externalEventLoop = false;
239-
display.sendPostExternalEventDispatchEvent ();
240+
/* Open the dialog */
241+
success = OS.ChooseFont(lpcf);
242+
display.externalEventLoop = false;
243+
display.sendPostExternalEventDispatchEvent();
240244

241-
/* Clear the temporary dialog modal parent */
242-
if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
243-
display.setModalDialog (oldModal);
244-
}
245+
/* Clear the temporary dialog modal parent */
246+
if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
247+
display.setModalDialog(oldModal);
248+
}
245249

246-
/* Compute the result */
247-
if (success) {
248-
LOGFONT logFont = new LOGFONT ();
249-
OS.MoveMemory (logFont, lpLogFont, LOGFONT.sizeof);
250+
/* Compute the result */
251+
if (success) {
252+
LOGFONT logFont = new LOGFONT();
253+
OS.MoveMemory(logFont, lpLogFont, LOGFONT.sizeof);
250254

251-
/*
252-
* This will not work on multiple screens or
253-
* for printing. Should use DC for the proper device.
254-
*/
255-
long hDC = OS.GetDC(0);
256-
int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
257-
int pixels = 0;
258-
if (logFont.lfHeight > 0) {
259255
/*
260-
* Feature in Windows. If the lfHeight of the LOGFONT structure
261-
* is positive, the lfHeight measures the height of the entire
262-
* cell, including internal leading, in logical units. Since the
263-
* height of a font in points does not include the internal leading,
264-
* we must subtract the internal leading, which requires a TEXTMETRIC,
265-
* which in turn requires font creation.
256+
* This will not work on multiple screens or for printing. Should use DC for the
257+
* proper device.
266258
*/
267-
long hFont = OS.CreateFontIndirect(logFont);
268-
long oldFont = OS.SelectObject(hDC, hFont);
269-
TEXTMETRIC lptm = new TEXTMETRIC ();
270-
OS.GetTextMetrics(hDC, lptm);
271-
OS.SelectObject(hDC, oldFont);
272-
OS.DeleteObject(hFont);
273-
pixels = logFont.lfHeight - lptm.tmInternalLeading;
274-
} else {
275-
pixels = -logFont.lfHeight;
276-
}
277-
OS.ReleaseDC(0, hDC);
259+
long hDC = OS.GetDC(0);
260+
int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
261+
int pixels = 0;
262+
if (logFont.lfHeight > 0) {
263+
/*
264+
* Feature in Windows. If the lfHeight of the LOGFONT structure is positive, the
265+
* lfHeight measures the height of the entire cell, including internal leading,
266+
* in logical units. Since the height of a font in points does not include the
267+
* internal leading, we must subtract the internal leading, which requires a
268+
* TEXTMETRIC, which in turn requires font creation.
269+
*/
270+
long hFont = OS.CreateFontIndirect(logFont);
271+
long oldFont = OS.SelectObject(hDC, hFont);
272+
TEXTMETRIC lptm = new TEXTMETRIC();
273+
OS.GetTextMetrics(hDC, lptm);
274+
OS.SelectObject(hDC, oldFont);
275+
OS.DeleteObject(hFont);
276+
pixels = logFont.lfHeight - lptm.tmInternalLeading;
277+
} else {
278+
pixels = -logFont.lfHeight;
279+
}
280+
OS.ReleaseDC(0, hDC);
278281

279-
float points = pixels * 72f /logPixelsY;
280-
fontData = FontData.win32_new (logFont, points);
281-
if (effectsVisible) {
282-
int red = lpcf.rgbColors & 0xFF;
283-
int green = (lpcf.rgbColors >> 8) & 0xFF;
284-
int blue = (lpcf.rgbColors >> 16) & 0xFF;
285-
rgb = new RGB (red, green, blue);
282+
float points = pixels * 72f / logPixelsY;
283+
fontData = FontData.win32_new(logFont, points);
284+
if (effectsVisible) {
285+
int red = lpcf.rgbColors & 0xFF;
286+
int green = (lpcf.rgbColors >> 8) & 0xFF;
287+
int blue = (lpcf.rgbColors >> 16) & 0xFF;
288+
rgb = new RGB(red, green, blue);
289+
}
286290
}
287-
}
288-
289-
/* Reset the dpi awareness context */
290-
if(display.isRescalingAtRuntime()) {
291-
OS.SetThreadDpiAwarenessContext(currentDpiAwarenessContext);
292-
}
293-
294-
/* Free the OS memory */
295-
if (lpLogFont != 0) OS.HeapFree (hHeap, 0, lpLogFont);
291+
} finally {
292+
/* Reset the dpi awareness context */
293+
if (display.isRescalingAtRuntime()) {
294+
OS.SetThreadDpiAwarenessContext(currentDpiAwarenessContext);
295+
}
296+
/* Free the OS memory */
297+
if (lpLogFont != 0) OS.HeapFree (hHeap, 0, lpLogFont);
296298

297-
/* Destroy the BIDI orientation window */
298-
if (hwndParent != hwndOwner) {
299-
if (enabled) OS.EnableWindow (hwndParent, true);
300-
OS.SetActiveWindow (hwndParent);
301-
OS.DestroyWindow (hwndOwner);
299+
/* Destroy the BIDI orientation window */
300+
if (hwndParent != hwndOwner) {
301+
if (enabled) OS.EnableWindow (hwndParent, true);
302+
OS.SetActiveWindow (hwndParent);
303+
OS.DestroyWindow (hwndOwner);
304+
}
305+
if (!success) return null;
302306
}
303307

304-
if (!success) return null;
305308
return fontData;
306309
}
307310

0 commit comments

Comments
 (0)