File: pyi_exception_dialog.c

package info (click to toggle)
pyinstaller 6.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,824 kB
  • sloc: python: 41,828; ansic: 12,123; makefile: 171; sh: 131; xml: 19
file content (442 lines) | stat: -rw-r--r-- 13,485 bytes parent folder | download | duplicates (4)
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
/*
 * ****************************************************************************
 * Copyright (c) 2021-2023, PyInstaller Development Team.
 *
 * Distributed under the terms of the GNU General Public License (version 2
 * or later) with exception for distributing the bootloader.
 *
 * The full license is in the file COPYING.txt, distributed with this software.
 *
 * SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 * ****************************************************************************
 */

/*
 * Implementation of unhandled exception dialog for windowed mode on Windows.
 */

/* Having a header included outside of the ifdef block prevents the compilation
 * unit from becoming empty, which is disallowed by pedantic ISO C. */
#include "pyi_global.h"

#if defined(WINDOWED) && defined(_WIN32)

#include <stdio.h>
#include <windows.h>
#include <commctrl.h>

#include "pyi_utils.h"


/*
 * Dialog template structure, described in
 * https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-dlgtemplate
 */

/* The template structure must be aligned on WORD boundaries */
#pragma pack(push, 4)

struct DIALOG_TEMPLATE
{
    /* DLGTEMPLATE */
    DWORD style;
    DWORD dwExtendedStyle;
    WORD  cdit;
    short x;
    short y;
    short cx;
    short cy;

    /*
     * As per above MSDN link, in a standard template for a dialog box,
     * the DLGTEMPLATE structure is always immediately followed by three
     * variable-length arrays that specify the menu, class, and title
     * for the dialog box.
     *
     * When the DS_SETFONT style is specified, these arrays are also
     * followed by a 16-bit value specifying point size and another
     * variable-length array specifying a typeface name.
     *
     * In our implementation, we use 64-character fields for strings
     * we want to pass and WORD fields for unused entries.
     */
    WORD menu;
    WORD windowClass;
    WCHAR wszTitle[64];

    WORD pointsize;
    WCHAR wszFont[64];
};

#pragma pack(pop)


/* The dialog context, which we use to pass data between callbacks */
struct DIALOG_CONTEXT
{
    HINSTANCE hInstance;  /* Parent module instance */
    HWND hDialog;  /* Dialog handle */

    /* Input exception data to display */
    WCHAR *wszScriptName;
    WCHAR *wszExceptionMessage;
    WCHAR *wszTraceback;

    /* Formatted label message */
    WCHAR wszLabelMessage[PYI_PATH_MAX];

    /* UI elements / controls */
    HWND hStaticIcon;
    HWND hStaticLabel;
    HWND hTracebackView;
    HWND hCloseButton;

    /* Resources */
    HFONT hDialogFont;
    HICON hErrorIcon;

    /* Misc */
    WORD wMargin;
    WORD wButtonWidth;
    WORD wButtonHeight;
    WORD wIconWidth;
    WORD wIconHeight;
};


/* Resize the dialog's contents to the new client area width and height. */
static void
_exception_dialog_resize(struct DIALOG_CONTEXT *dialog, WORD wAreaWidth, WORD wAreaHeight)
{
    WORD wLabelHeight;
    WORD wPosY, wPosX, wWidth, wHeight;
    HDC hDC;

    /* Estimate the required height of the text label. */
    hDC = GetDC(dialog->hStaticLabel);
    if (hDC) {
        HFONT hOldFont = NULL;
        RECT rectText;

        rectText.left = 0;
        rectText.top = 0;
        rectText.right = wAreaWidth - 3*dialog->wMargin - dialog->wIconWidth;
        rectText.bottom = 0;

        /* Estimate text rectangle */
        if (dialog->hDialogFont) {
            /* To obtain correct font metrics, we need to select the
             * font into device context */
            hOldFont = SelectObject(hDC, dialog->hDialogFont);
        }
        DrawTextW(hDC, dialog->wszLabelMessage, -1, &rectText, DT_LEFT | DT_CALCRECT | DT_NOCLIP | DT_WORDBREAK | DT_EDITCONTROL | DT_EXPANDTABS);
        /* Cleanup */
        if (dialog->hDialogFont) {
            SelectObject(hDC, hOldFont);  /* Restore old font, just in case */
        }
        ReleaseDC(dialog->hStaticLabel, hDC);
        /* Estimate label height */
        wLabelHeight = (WORD)(rectText.bottom - rectText.top);
    } else {
        /* Fall-back value, just in case */
        wLabelHeight = 20;
    }
    if (wLabelHeight < dialog->wIconHeight) {
        wLabelHeight = dialog->wIconHeight;
    }

    /*
     * Adjust controls' position and size
     */
    /* Icon */
    wPosX = dialog->wMargin;
    wPosY = dialog->wMargin;
    wWidth = dialog->wIconWidth;
    wHeight = dialog->wIconHeight;
    MoveWindow(dialog->hStaticIcon, wPosX, wPosY, wWidth, wHeight, TRUE);

    /* Label */
    wPosX = dialog->wMargin + dialog->wIconWidth + dialog->wMargin;
    wPosY = dialog->wMargin;
    wWidth = wAreaWidth - dialog->wMargin - wPosX;
    wHeight = wLabelHeight;
    MoveWindow(dialog->hStaticLabel, wPosX, wPosY, wWidth, wHeight, TRUE);

    /* Traceback view */
    wPosX = dialog->wMargin;
    wPosY += wLabelHeight + dialog->wMargin;
    wWidth = wAreaWidth - dialog->wMargin - wPosX;
    wHeight = wAreaHeight - wPosY - dialog->wMargin - dialog->wButtonHeight - dialog->wMargin;
    MoveWindow(dialog->hTracebackView, wPosX, wPosY, wWidth, wHeight, TRUE);

    /* Close button */
    wPosX = wAreaWidth - dialog->wMargin - dialog->wButtonWidth;
    wPosY = wAreaHeight - dialog->wMargin - dialog->wButtonHeight;
    wWidth = dialog->wButtonWidth;
    wHeight = dialog->wButtonHeight;
    MoveWindow(dialog->hCloseButton, wPosX, wPosY, wWidth, wHeight, TRUE);
}


/* Initialize the dialog. */
static void
_exception_dialog_initialze(struct DIALOG_CONTEXT *dialog)
{
    NONCLIENTMETRICSW metrics;
    LONG lUnits;
    RECT rect;

    /*
     * Initialize resources
     */
    /* Format the label message */
    swprintf(dialog->wszLabelMessage, PYI_PATH_MAX, L"Failed to execute script '%ls' due to unhandled exception: %ls", dialog->wszScriptName, dialog->wszExceptionMessage);

    /* Estimate button dimensions */
    lUnits = GetDialogBaseUnits();
    dialog->wButtonWidth = MulDiv(LOWORD(lUnits), 50, 4);
    dialog->wButtonHeight = MulDiv(HIWORD(lUnits), 14, 8);

    /* Set icon dimensions */
    dialog->wIconWidth = 32;
    dialog->wIconHeight = 32;

    /* Set margin in the layout */
    dialog->wMargin = 8;

    /* Query default dialog font*/
    ZeroMemory(&metrics, sizeof(metrics));
    metrics.cbSize = sizeof(metrics);
    if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0)) {
        dialog->hDialogFont = CreateFontIndirectW(&metrics.lfMessageFont);
    } else {
        dialog->hDialogFont = NULL;
    }

    /* Load the icon; LoadIconMetric() gives modern icon, but requires
     * Microsoft.Windows.Common-Controls version='6.0.0.0' dependency
     * in the manifest. */
#if 0
    dialog->hErrorIcon = LoadIconW(NULL, IDI_ERROR);
#else
    LoadIconMetric(NULL, IDI_ERROR, LIM_LARGE, &dialog->hErrorIcon);
#endif

    /*
     * Create UI controls
     * NOTE: positions and dimensions of controls do not matter, as we
     * reposition and resize them using dynamic layout function.
     */
    /* Icon */
    dialog->hStaticIcon = CreateWindowExW(
        0,
        L"STATIC",
		NULL,
		WS_CHILD | WS_VISIBLE | SS_ICON,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		dialog->hDialog,
		NULL,
		dialog->hInstance,
		NULL
    );

    /* Basic information label */
    dialog->hStaticLabel = CreateWindowExW(
        0,
        L"STATIC",
		NULL,
		WS_CHILD | WS_VISIBLE | SS_LEFT,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		dialog->hDialog,
		NULL,
		dialog->hInstance,
		NULL
    );

    /* Create traceback view (text edit) */
    dialog->hTracebackView = CreateWindowExW(
        WS_EX_CLIENTEDGE,  /* border */
        L"EDIT",
		NULL,
		WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |ES_LEFT |ES_MULTILINE | ES_AUTOHSCROLL | ES_READONLY,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		dialog->hDialog,
		NULL,
		dialog->hInstance,
		NULL
    );

    /* Close button */
    dialog->hCloseButton = CreateWindowExW(
        0,
        L"BUTTON",
		L"Close",
		WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		dialog->hDialog,
		(HMENU)(UINT_PTR)IDOK,  /* assign control ID */
		dialog->hInstance,
		NULL
    );

    /* Set icon */
    SendMessageW(dialog->hDialog, WM_SETICON, ICON_SMALL, (LPARAM)dialog->hErrorIcon);
    SendMessageW(dialog->hDialog, WM_SETICON, ICON_BIG, (LPARAM)dialog->hErrorIcon);
    SendMessageW(dialog->hStaticIcon, STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)dialog->hErrorIcon);

    /* Set font to dialog and controls */
    if (dialog->hDialogFont) {
        SendMessageW(dialog->hDialog, WM_SETFONT, (WPARAM)dialog->hDialogFont, TRUE);
        SendMessageW(dialog->hStaticLabel, WM_SETFONT, (WPARAM)dialog->hDialogFont, TRUE);
        SendMessageW(dialog->hTracebackView, WM_SETFONT, (WPARAM)dialog->hDialogFont, TRUE);
        SendMessageW(dialog->hCloseButton, WM_SETFONT, (WPARAM)dialog->hDialogFont, TRUE);
    }

    /* Set text to controls */
    SendMessageW(dialog->hStaticLabel, WM_SETTEXT, 0, (LPARAM)dialog->wszLabelMessage);
    SendMessageW(dialog->hTracebackView, WM_SETTEXT, 0, (LPARAM)dialog->wszTraceback);

    /* Perform initial resize */
    if (GetClientRect(dialog->hDialog, &rect)) {
        _exception_dialog_resize(
            dialog,
            (WORD)(rect.right - rect.left),
            (WORD)(rect.bottom - rect.top)
        );
    }
}


/* Clean up dialog data */
static void
_exception_dialog_cleanup(struct DIALOG_CONTEXT *dialog)
{
    /* Clean-up exception data */
    free(dialog->wszScriptName);
    free(dialog->wszExceptionMessage);
    free(dialog->wszTraceback);

    /* Free font */
    if (dialog->hDialogFont) {
        DeleteObject(dialog->hDialogFont);
    }

    /* Free icon */
    if (dialog->hErrorIcon) {
        DestroyIcon(dialog->hErrorIcon);
    }
}


/* The DLGPROC callback procedure */
static INT_PTR CALLBACK
_exception_dialog_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
        case WM_INITDIALOG: {
            struct DIALOG_CONTEXT *dialog = (struct DIALOG_CONTEXT *)lParam;
            /* Store dialog handle in the context */
            dialog->hDialog = hwnd;
            /* Set pointer to context data as dialog's user data, so we
             * can retrieve it in other commands */
            SetWindowLongPtrW(hwnd, DWLP_USER, lParam);
            /* Initialize dialog */
           _exception_dialog_initialze(dialog);
           return TRUE;
        }
        case WM_COMMAND: {
            UINT wId = LOWORD(wParam);
            if (wId == IDOK || wId == IDCANCEL) {
                EndDialog(hwnd, wId);
            }
            return TRUE;
        }
        case WM_CLOSE: {
            EndDialog(hwnd, IDCANCEL);
            return TRUE;
        }
        case WM_SIZE: {
            struct DIALOG_CONTEXT *dialog = (struct DIALOG_CONTEXT *)GetWindowLongPtrW(hwnd, DWLP_USER);
            /* Resize dialog UI */
            _exception_dialog_resize(dialog, LOWORD(lParam), HIWORD(lParam));
            /* Redraw */
            InvalidateRect(hwnd, NULL, FALSE);
            break;
        }
    }

    return FALSE;
}


/* Create and display modal dialog with information about uncaught exception */
static int
_pyi_unhandled_exception_dialog_w(const wchar_t *script_name, const wchar_t *exception_message, const wchar_t *traceback)
{
    HINSTANCE hInstance;
    struct DIALOG_TEMPLATE template;
    struct DIALOG_CONTEXT dialog;
    int ret;

    hInstance = GetModuleHandleW(NULL);

    /* Prepare template for empty dialog */
    ZeroMemory(&template, sizeof(template));

    template.style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME| DS_MODALFRAME | DS_3DLOOK | DS_CENTER;
    template.dwExtendedStyle = 0;
    template.cdit = 0;  /* No items in template; we'll create them manually */
    template.x = 0;
    template.y = 0;
    template.cx = 200;
    template.cy = 150;
    template.menu = 0;
    template.windowClass = 0;
    swprintf(template.wszTitle, sizeof(template.wszTitle)/sizeof(template.wszTitle[0]), L"Unhandled exception in script");

    /* Prepare dialog context */
    ZeroMemory(&dialog, sizeof(dialog));
    dialog.hInstance = hInstance;

    dialog.wszScriptName = wcsdup(script_name);
    dialog.wszExceptionMessage = wcsdup(exception_message);
    dialog.wszTraceback = wcsdup(traceback);

    /* Create and run the dialog */
    ret = (int)DialogBoxIndirectParamW(hInstance, (LPCDLGTEMPLATEW)&template, NULL, _exception_dialog_proc, (LPARAM)&dialog);

    /* Cleanup */
    _exception_dialog_cleanup(&dialog);

    return ret;
}

/* The actual entry point function */
int
pyi_unhandled_exception_dialog(const char *script_name, const char *exception_message, const char *traceback)
{
    wchar_t *script_name_w = NULL;
    wchar_t *exception_message_w = NULL;
    wchar_t *traceback_w = NULL;
    int ret;

    if (script_name) {
        script_name_w = pyi_win32_utf8_to_wcs(script_name, NULL, 0);
    }
    if (exception_message) {
        exception_message_w = pyi_win32_utf8_to_wcs(exception_message, NULL, 0);
    }
    if (traceback) {
        traceback_w = pyi_win32_utf8_to_wcs(traceback, NULL, 0);
    }

    ret = _pyi_unhandled_exception_dialog_w(script_name_w, exception_message_w, traceback_w ? traceback_w : L"Failed to obtain/convert traceback!");

    free(script_name_w);
    free(exception_message_w);
    free(traceback_w);

    return ret;
}

#endif /* defined(WINDOWED) && defined(_WIN32) */