Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * The dialog that displays after a crash |
| 3 | * |
| 4 | * Copyright 2008 Mikolaj Zalewski |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 19 | */ |
| 20 | |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 21 | #include "debugger.h" |
| 22 | #include "wingdi.h" |
| 23 | #include "winuser.h" |
Alexandre Julliard | 96e13da | 2011-11-18 12:17:30 +0100 | [diff] [blame] | 24 | #include "commctrl.h" |
| 25 | #include "shellapi.h" |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 26 | #include "psapi.h" |
| 27 | |
| 28 | #include "wine/debug.h" |
| 29 | #include "wine/unicode.h" |
| 30 | |
Michael Stefaniuc | 34f771c | 2009-07-01 10:15:12 +0200 | [diff] [blame] | 31 | #include "resource.h" |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 32 | |
| 33 | #define MAX_PROGRAM_NAME_LENGTH 80 |
| 34 | |
| 35 | int msgbox_res_id(HWND hwnd, UINT textId, UINT captionId, UINT uType) |
| 36 | { |
| 37 | WCHAR caption[256]; |
| 38 | WCHAR text[256]; |
| 39 | LoadStringW(GetModuleHandleW(NULL), captionId, caption, sizeof(caption)/sizeof(caption[0])); |
| 40 | LoadStringW(GetModuleHandleW(NULL), textId, text, sizeof(text)/sizeof(text[0])); |
| 41 | return MessageBoxW(hwnd, text, caption, uType); |
| 42 | } |
| 43 | |
| 44 | static WCHAR *get_program_name(HANDLE hProcess) |
| 45 | { |
| 46 | WCHAR image_name[MAX_PATH]; |
| 47 | WCHAR *programname; |
| 48 | WCHAR *output; |
| 49 | |
| 50 | /* GetProcessImageFileNameW gives no way to query the correct buffer size, |
| 51 | * but programs with a path longer than MAX_PATH can't be started by the |
| 52 | * shell, so we expect they don't happen often */ |
| 53 | if (!GetProcessImageFileNameW(hProcess, image_name, MAX_PATH)) |
| 54 | { |
| 55 | static WCHAR unidentified[MAX_PROGRAM_NAME_LENGTH]; |
| 56 | LoadStringW(GetModuleHandleW(NULL), IDS_UNIDENTIFIED, |
| 57 | unidentified, MAX_PROGRAM_NAME_LENGTH); |
| 58 | return unidentified; |
| 59 | } |
| 60 | |
| 61 | programname = strrchrW(image_name, '\\'); |
| 62 | if (programname != NULL) |
| 63 | programname++; |
| 64 | else |
| 65 | programname = image_name; |
| 66 | |
| 67 | /* TODO: if the image has a VERSIONINFO, we could try to find there a more |
| 68 | * user-friendly program name */ |
| 69 | |
| 70 | /* don't display a too long string to the user */ |
| 71 | if (strlenW(programname) >= MAX_PROGRAM_NAME_LENGTH) |
| 72 | { |
| 73 | programname[MAX_PROGRAM_NAME_LENGTH - 4] = '.'; |
| 74 | programname[MAX_PROGRAM_NAME_LENGTH - 3] = '.'; |
| 75 | programname[MAX_PROGRAM_NAME_LENGTH - 2] = '.'; |
| 76 | programname[MAX_PROGRAM_NAME_LENGTH - 1] = 0; |
| 77 | } |
| 78 | |
| 79 | output = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(programname) + 1)); |
| 80 | lstrcpyW(output, programname); |
| 81 | |
| 82 | return output; |
| 83 | } |
| 84 | |
| 85 | static LPWSTR g_ProgramName; |
| 86 | static HFONT g_hBoldFont; |
| 87 | static HMENU g_hDebugMenu = NULL; |
| 88 | |
| 89 | static void set_bold_font(HWND hDlg) |
| 90 | { |
| 91 | HFONT hNormalFont = (HFONT)SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1, |
| 92 | WM_GETFONT, 0, 0); |
| 93 | LOGFONTW font; |
| 94 | GetObjectW(hNormalFont, sizeof(LOGFONTW), &font); |
| 95 | font.lfWeight = FW_BOLD; |
| 96 | g_hBoldFont = CreateFontIndirectW(&font); |
| 97 | SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1, WM_SETFONT, (WPARAM)g_hBoldFont, TRUE); |
| 98 | } |
| 99 | |
| 100 | static void set_message_with_filename(HWND hDlg) |
| 101 | { |
| 102 | WCHAR originalText[1000]; |
| 103 | WCHAR newText[1000 + MAX_PROGRAM_NAME_LENGTH]; |
| 104 | |
| 105 | GetDlgItemTextW(hDlg, IDC_STATIC_TXT1, originalText, |
| 106 | sizeof(originalText)/sizeof(originalText[0])); |
| 107 | wsprintfW(newText, originalText, g_ProgramName); |
| 108 | SetDlgItemTextW(hDlg, IDC_STATIC_TXT1, newText); |
| 109 | } |
| 110 | |
| 111 | static INT_PTR WINAPI DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 112 | { |
Alexandre Julliard | 96e13da | 2011-11-18 12:17:30 +0100 | [diff] [blame] | 113 | static const WCHAR openW[] = {'o','p','e','n',0}; |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 114 | switch (msg) |
| 115 | { |
| 116 | case WM_INITDIALOG: |
| 117 | { |
| 118 | set_bold_font(hwnd); |
| 119 | set_message_with_filename(hwnd); |
| 120 | return TRUE; |
| 121 | } |
| 122 | case WM_CTLCOLORSTATIC: |
| 123 | { |
| 124 | /* WM_CTLCOLOR* don't use DWLP_MSGRESULT */ |
| 125 | INT_PTR id = GetDlgCtrlID((HWND)lParam); |
| 126 | if (id == IDC_STATIC_BG || id == IDC_STATIC_TXT1) |
| 127 | return (LONG_PTR)GetSysColorBrush(COLOR_WINDOW); |
| 128 | |
| 129 | return FALSE; |
| 130 | } |
| 131 | case WM_RBUTTONDOWN: |
| 132 | { |
| 133 | POINT mousePos; |
| 134 | if (!(wParam & MK_SHIFT)) |
| 135 | return FALSE; |
| 136 | if (g_hDebugMenu == NULL) |
| 137 | g_hDebugMenu = LoadMenuW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDM_DEBUG_POPUP)); |
| 138 | GetCursorPos(&mousePos); |
| 139 | TrackPopupMenu(GetSubMenu(g_hDebugMenu, 0), TPM_RIGHTBUTTON, mousePos.x, mousePos.y, |
| 140 | 0, hwnd, NULL); |
| 141 | return TRUE; |
| 142 | } |
Alexandre Julliard | 96e13da | 2011-11-18 12:17:30 +0100 | [diff] [blame] | 143 | case WM_NOTIFY: |
| 144 | switch (((NMHDR *)lParam)->code) |
| 145 | { |
| 146 | case NM_CLICK: |
| 147 | case NM_RETURN: |
| 148 | if (wParam == IDC_STATIC_TXT2) |
| 149 | ShellExecuteW( NULL, openW, ((NMLINK *)lParam)->item.szUrl, NULL, NULL, SW_SHOW ); |
| 150 | break; |
| 151 | } |
| 152 | break; |
| 153 | |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 154 | case WM_COMMAND: |
| 155 | switch (LOWORD(wParam)) |
| 156 | { |
| 157 | case IDOK: |
| 158 | case IDCANCEL: |
| 159 | case ID_DEBUG: |
| 160 | EndDialog(hwnd, LOWORD(wParam)); |
| 161 | return TRUE; |
| 162 | } |
| 163 | return TRUE; |
| 164 | } |
| 165 | return FALSE; |
| 166 | } |
| 167 | |
| 168 | BOOL display_crash_dialog(void) |
| 169 | { |
Alexandre Julliard | 8c7feab | 2009-06-23 16:20:33 +0200 | [diff] [blame] | 170 | static const WCHAR winedeviceW[] = {'w','i','n','e','d','e','v','i','c','e','.','e','x','e',0}; |
Alexandre Julliard | 96e13da | 2011-11-18 12:17:30 +0100 | [diff] [blame] | 171 | static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_LINK_CLASS }; |
Alexandre Julliard | 8c7feab | 2009-06-23 16:20:33 +0200 | [diff] [blame] | 172 | |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 173 | INT_PTR result; |
| 174 | /* dbg_curr_process->handle is not set */ |
| 175 | HANDLE hProcess; |
| 176 | |
| 177 | if (!DBG_IVAR(ShowCrashDialog)) |
| 178 | return TRUE; |
| 179 | |
| 180 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dbg_curr_pid); |
| 181 | g_ProgramName = get_program_name(hProcess); |
| 182 | CloseHandle(hProcess); |
Alexandre Julliard | 8c7feab | 2009-06-23 16:20:33 +0200 | [diff] [blame] | 183 | if (!strcmpW( g_ProgramName, winedeviceW )) return TRUE; |
Alexandre Julliard | 96e13da | 2011-11-18 12:17:30 +0100 | [diff] [blame] | 184 | InitCommonControlsEx( &init ); |
Mikołaj Zalewski | 1e256e4 | 2008-12-21 22:28:32 +0100 | [diff] [blame] | 185 | result = DialogBoxW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_CRASH_DLG), NULL, DlgProc); |
| 186 | if (result == ID_DEBUG) { |
| 187 | AllocConsole(); |
| 188 | return FALSE; |
| 189 | } |
| 190 | return TRUE; |
| 191 | } |