po: Tweak some Catalan translations.
[wine/multimedia.git] / programs / winedbg / crashdlg.c
blobfe135024bb0efceb1a324edd7afd65e3ed104061
1 /*
2 * The dialog that displays after a crash
4 * Copyright 2008 Mikolaj Zalewski
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.
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.
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
21 #include "debugger.h"
22 #include "wingdi.h"
23 #include "winuser.h"
24 #include "commctrl.h"
25 #include "commdlg.h"
26 #include "shellapi.h"
27 #include "psapi.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 #include "resource.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
36 #define MAX_PROGRAM_NAME_LENGTH 80
38 static char *crash_log;
40 int msgbox_res_id(HWND hwnd, UINT textId, UINT captionId, UINT uType)
42 WCHAR caption[256];
43 WCHAR text[256];
44 LoadStringW(GetModuleHandleW(NULL), captionId, caption, sizeof(caption)/sizeof(caption[0]));
45 LoadStringW(GetModuleHandleW(NULL), textId, text, sizeof(text)/sizeof(text[0]));
46 return MessageBoxW(hwnd, text, caption, uType);
49 static WCHAR *get_program_name(HANDLE hProcess)
51 WCHAR image_name[MAX_PATH];
52 WCHAR *programname;
53 WCHAR *output;
55 /* GetProcessImageFileNameW gives no way to query the correct buffer size,
56 * but programs with a path longer than MAX_PATH can't be started by the
57 * shell, so we expect they don't happen often */
58 if (!GetProcessImageFileNameW(hProcess, image_name, MAX_PATH))
60 static WCHAR unidentified[MAX_PROGRAM_NAME_LENGTH];
61 LoadStringW(GetModuleHandleW(NULL), IDS_UNIDENTIFIED,
62 unidentified, MAX_PROGRAM_NAME_LENGTH);
63 return unidentified;
66 programname = strrchrW(image_name, '\\');
67 if (programname != NULL)
68 programname++;
69 else
70 programname = image_name;
72 /* TODO: if the image has a VERSIONINFO, we could try to find there a more
73 * user-friendly program name */
75 /* don't display a too long string to the user */
76 if (strlenW(programname) >= MAX_PROGRAM_NAME_LENGTH)
78 programname[MAX_PROGRAM_NAME_LENGTH - 4] = '.';
79 programname[MAX_PROGRAM_NAME_LENGTH - 3] = '.';
80 programname[MAX_PROGRAM_NAME_LENGTH - 2] = '.';
81 programname[MAX_PROGRAM_NAME_LENGTH - 1] = 0;
84 output = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(programname) + 1));
85 lstrcpyW(output, programname);
87 return output;
90 static LPWSTR g_ProgramName;
91 static HFONT g_hBoldFont;
92 static HMENU g_hDebugMenu = NULL;
94 static void set_bold_font(HWND hDlg)
96 HFONT hNormalFont = (HFONT)SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1,
97 WM_GETFONT, 0, 0);
98 LOGFONTW font;
99 GetObjectW(hNormalFont, sizeof(LOGFONTW), &font);
100 font.lfWeight = FW_BOLD;
101 g_hBoldFont = CreateFontIndirectW(&font);
102 SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1, WM_SETFONT, (WPARAM)g_hBoldFont, TRUE);
105 static void set_fixed_font( HWND dlg, UINT id )
107 HFONT hfont = (HFONT)SendDlgItemMessageW( dlg, id, WM_GETFONT, 0, 0);
108 LOGFONTW font;
110 GetObjectW(hfont, sizeof(LOGFONTW), &font);
111 font.lfPitchAndFamily = FIXED_PITCH;
112 font.lfFaceName[0] = 0;
113 hfont = CreateFontIndirectW(&font);
114 SendDlgItemMessageW( dlg, id, WM_SETFONT, (WPARAM)hfont, TRUE );
117 static void set_message_with_filename(HWND hDlg)
119 WCHAR originalText[1000];
120 WCHAR newText[1000 + MAX_PROGRAM_NAME_LENGTH];
122 GetDlgItemTextW(hDlg, IDC_STATIC_TXT1, originalText,
123 sizeof(originalText)/sizeof(originalText[0]));
124 wsprintfW(newText, originalText, g_ProgramName);
125 SetDlgItemTextW(hDlg, IDC_STATIC_TXT1, newText);
128 static void load_crash_log( HANDLE file )
130 DWORD len, pos = 0, size = 65536;
132 crash_log = HeapAlloc( GetProcessHeap(), 0, size );
133 SetFilePointer( file, 0, NULL, FILE_BEGIN );
134 while (ReadFile( file, crash_log + pos, size - pos - 1, &len, NULL ) && len)
136 pos += len;
137 if (pos == size - 1) crash_log = HeapReAlloc( GetProcessHeap(), 0, crash_log, size *= 2 );
139 crash_log[pos] = 0;
142 static void save_crash_log( HWND hwnd )
144 OPENFILENAMEW save;
145 HANDLE handle;
146 DWORD err, written;
147 WCHAR *p, path[MAX_PATH], buffer[1024];
148 static const WCHAR default_name[] = { 'b','a','c','k','t','r','a','c','e','.','t','x','t',0 };
149 static const WCHAR default_ext[] = { 't','x','t',0 };
150 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
151 static const WCHAR all_files[] = { '*','.','*',0 };
153 memset( &save, 0, sizeof(save) );
154 lstrcpyW( path, default_name );
156 LoadStringW( GetModuleHandleW(0), IDS_TEXT_FILES, buffer, sizeof(buffer)/sizeof(buffer[0]) );
157 p = buffer + lstrlenW(buffer) + 1;
158 lstrcpyW(p, txt_files);
159 p += lstrlenW(p) + 1;
160 LoadStringW( GetModuleHandleW(0), IDS_ALL_FILES, p, sizeof(buffer)/sizeof(buffer[0]) - (p - buffer) );
161 p += lstrlenW(p) + 1;
162 lstrcpyW(p, all_files);
163 p += lstrlenW(p) + 1;
164 *p = '\0';
166 save.lStructSize = sizeof(OPENFILENAMEW);
167 save.hwndOwner = hwnd;
168 save.hInstance = GetModuleHandleW(0);
169 save.lpstrFilter = buffer;
170 save.lpstrFile = path;
171 save.nMaxFile = MAX_PATH;
172 save.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
173 OFN_HIDEREADONLY | OFN_ENABLESIZING;
174 save.lpstrDefExt = default_ext;
176 if (!GetSaveFileNameW( &save )) return;
177 handle = CreateFileW( save.lpstrFile, GENERIC_WRITE, FILE_SHARE_READ,
178 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
179 if (handle != INVALID_HANDLE_VALUE)
181 if (!WriteFile( handle, crash_log, strlen(crash_log), &written, NULL ))
182 err = GetLastError();
183 else if (written != strlen(crash_log))
184 err = GetLastError();
185 else
187 CloseHandle( handle );
188 return;
190 CloseHandle( handle );
191 DeleteFileW( save.lpstrFile );
193 else err = GetLastError();
195 LoadStringW( GetModuleHandleW(0), IDS_SAVE_ERROR, buffer, sizeof(buffer)/sizeof(WCHAR) );
196 FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
197 NULL, err, 0, (LPWSTR)&p, 0, NULL);
198 MessageBoxW( 0, p, buffer, MB_OK | MB_ICONERROR);
199 LocalFree( p );
202 static INT_PTR WINAPI crash_dlg_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
204 static const WCHAR openW[] = {'o','p','e','n',0};
205 switch (msg)
207 case WM_INITDIALOG:
209 set_bold_font(hwnd);
210 set_message_with_filename(hwnd);
211 return TRUE;
213 case WM_CTLCOLORSTATIC:
215 /* WM_CTLCOLOR* don't use DWLP_MSGRESULT */
216 INT_PTR id = GetDlgCtrlID((HWND)lParam);
217 if (id == IDC_STATIC_BG || id == IDC_STATIC_TXT1)
218 return (LONG_PTR)GetSysColorBrush(COLOR_WINDOW);
220 return FALSE;
222 case WM_RBUTTONDOWN:
224 POINT mousePos;
225 if (!(wParam & MK_SHIFT))
226 return FALSE;
227 if (g_hDebugMenu == NULL)
228 g_hDebugMenu = LoadMenuW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDM_DEBUG_POPUP));
229 GetCursorPos(&mousePos);
230 TrackPopupMenu(GetSubMenu(g_hDebugMenu, 0), TPM_RIGHTBUTTON, mousePos.x, mousePos.y,
231 0, hwnd, NULL);
232 return TRUE;
234 case WM_NOTIFY:
235 switch (((NMHDR *)lParam)->code)
237 case NM_CLICK:
238 case NM_RETURN:
239 if (wParam == IDC_STATIC_TXT2)
240 ShellExecuteW( NULL, openW, ((NMLINK *)lParam)->item.szUrl, NULL, NULL, SW_SHOW );
241 break;
243 break;
245 case WM_COMMAND:
246 switch (LOWORD(wParam))
248 case IDOK:
249 case IDCANCEL:
250 case ID_DEBUG:
251 case ID_DETAILS:
252 EndDialog(hwnd, LOWORD(wParam));
253 return TRUE;
255 return TRUE;
257 return FALSE;
260 static INT_PTR WINAPI details_dlg_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
262 static const WCHAR openW[] = {'o','p','e','n',0};
263 static POINT orig_size, min_size, edit_size, text_pos, save_pos, close_pos;
265 switch (msg)
267 case WM_INITDIALOG:
269 RECT rect;
270 WCHAR buffer[256];
272 set_fixed_font( hwnd, IDC_CRASH_TXT );
273 LoadStringW( GetModuleHandleW(0), IDS_LOADING, buffer, 256 );
274 SetDlgItemTextW( hwnd, IDC_CRASH_TXT, buffer );
275 EnableWindow( GetDlgItem( hwnd, IDC_CRASH_TXT ), FALSE );
276 EnableWindow( GetDlgItem( hwnd, ID_SAVEAS ), FALSE );
278 GetClientRect( hwnd, &rect );
279 orig_size.x = rect.right;
280 orig_size.y = rect.bottom;
282 GetWindowRect( hwnd, &rect );
283 min_size.x = rect.right - rect.left;
284 min_size.y = rect.bottom - rect.top;
286 GetWindowRect( GetDlgItem( hwnd, IDOK ), &rect );
287 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
288 close_pos.x = rect.left;
289 close_pos.y = rect.top;
291 GetWindowRect( GetDlgItem( hwnd, ID_SAVEAS ), &rect );
292 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
293 save_pos.x = rect.left;
294 save_pos.y = rect.top;
296 GetWindowRect( GetDlgItem( hwnd, IDC_STATIC_TXT2 ), &rect );
297 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
298 text_pos.x = rect.left;
299 text_pos.y = rect.top;
301 GetWindowRect( GetDlgItem( hwnd, IDC_CRASH_TXT ), &rect );
302 MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
303 edit_size.x = rect.right - rect.left;
304 edit_size.y = rect.bottom - rect.top;
305 return TRUE;
308 case WM_GETMINMAXINFO:
309 ((MINMAXINFO *)lparam)->ptMinTrackSize = min_size;
310 return TRUE;
312 case WM_SIZE:
313 if (wparam == SIZE_RESTORED || wparam == SIZE_MAXIMIZED)
315 int off_x = (short)LOWORD( lparam ) - orig_size.x;
316 int off_y = (short)HIWORD( lparam ) - orig_size.y;
318 SetWindowPos( GetDlgItem( hwnd, IDOK ), 0, close_pos.x + off_x,
319 close_pos.y + off_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
320 SetWindowPos( GetDlgItem( hwnd, ID_SAVEAS ), 0, save_pos.x + off_x,
321 save_pos.y + off_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
322 SetWindowPos( GetDlgItem( hwnd, IDC_STATIC_TXT2 ), 0, text_pos.x,
323 text_pos.y + off_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
324 SetWindowPos( GetDlgItem( hwnd, IDC_CRASH_TXT ), 0, 0, 0, edit_size.x + off_x,
325 edit_size.y + off_y, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
327 return TRUE;
329 case WM_NOTIFY:
330 switch (((NMHDR *)lparam)->code)
332 case NM_CLICK:
333 case NM_RETURN:
334 if (wparam == IDC_STATIC_TXT2)
335 ShellExecuteW( NULL, openW, ((NMLINK *)lparam)->item.szUrl, NULL, NULL, SW_SHOW );
336 break;
338 break;
340 case WM_COMMAND:
341 switch (LOWORD(wparam))
343 case ID_SAVEAS:
344 save_crash_log( hwnd );
345 break;
346 case IDOK:
347 case IDCANCEL:
348 PostQuitMessage( 0 );
349 break;
351 return TRUE;
353 return FALSE;
356 int display_crash_dialog(void)
358 static const WCHAR winedeviceW[] = {'w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
359 static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_LINK_CLASS };
361 /* dbg_curr_process->handle is not set */
362 HANDLE hProcess;
364 if (!DBG_IVAR(ShowCrashDialog))
365 return TRUE;
367 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dbg_curr_pid);
368 g_ProgramName = get_program_name(hProcess);
369 CloseHandle(hProcess);
370 if (!strcmpW( g_ProgramName, winedeviceW )) return TRUE;
371 InitCommonControlsEx( &init );
372 return DialogBoxW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_CRASH_DLG), NULL, crash_dlg_proc);
375 static DWORD WINAPI crash_details_thread( void *event )
377 MSG msg;
378 HWND dialog;
380 dialog = CreateDialogW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DETAILS_DLG), 0, details_dlg_proc );
381 if (!dialog) return 1;
383 for (;;)
385 if (MsgWaitForMultipleObjects( 1, &event, FALSE, INFINITE, QS_ALLINPUT ) == 0)
387 load_crash_log( dbg_houtput );
388 SetDlgItemTextA( dialog, IDC_CRASH_TXT, crash_log );
389 EnableWindow( GetDlgItem( dialog, IDC_CRASH_TXT ), TRUE );
390 EnableWindow( GetDlgItem( dialog, ID_SAVEAS ), TRUE );
391 break;
393 while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
395 if (msg.message == WM_QUIT) return 0;
396 TranslateMessage( &msg );
397 DispatchMessageW( &msg );
401 while (GetMessageW( &msg, 0, 0, 0 ))
403 TranslateMessage( &msg );
404 DispatchMessageW( &msg );
406 return 0;
409 HANDLE display_crash_details( HANDLE event )
411 return CreateThread( NULL, 0, crash_details_thread, event, 0, NULL );