winedbg: Remove an unused debug channel.
[wine] / programs / winedbg / crashdlg.c
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
21 #include "resource.h"
22 #include "debugger.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "psapi.h"
26
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
29
30
31 #define MAX_PROGRAM_NAME_LENGTH 80
32
33 int msgbox_res_id(HWND hwnd, UINT textId, UINT captionId, UINT uType)
34 {
35     WCHAR caption[256];
36     WCHAR text[256];
37     LoadStringW(GetModuleHandleW(NULL), captionId, caption, sizeof(caption)/sizeof(caption[0]));
38     LoadStringW(GetModuleHandleW(NULL), textId, text, sizeof(text)/sizeof(text[0]));
39     return MessageBoxW(hwnd, text, caption, uType);
40 }
41
42 static WCHAR *get_program_name(HANDLE hProcess)
43 {
44     WCHAR image_name[MAX_PATH];
45     WCHAR *programname;
46     WCHAR *output;
47
48     /* GetProcessImageFileNameW gives no way to query the correct buffer size,
49      * but programs with a path longer than MAX_PATH can't be started by the
50      * shell, so we expect they don't happen often */
51     if (!GetProcessImageFileNameW(hProcess, image_name, MAX_PATH))
52     {
53         static WCHAR unidentified[MAX_PROGRAM_NAME_LENGTH];
54         LoadStringW(GetModuleHandleW(NULL), IDS_UNIDENTIFIED,
55                 unidentified, MAX_PROGRAM_NAME_LENGTH);
56         return unidentified;
57     }
58
59     programname = strrchrW(image_name, '\\');
60     if (programname != NULL)
61         programname++;
62     else
63         programname = image_name;
64
65     /* TODO: if the image has a VERSIONINFO, we could try to find there a more
66      * user-friendly program name */
67
68     /* don't display a too long string to the user */
69     if (strlenW(programname) >= MAX_PROGRAM_NAME_LENGTH)
70     {
71         programname[MAX_PROGRAM_NAME_LENGTH - 4] = '.';
72         programname[MAX_PROGRAM_NAME_LENGTH - 3] = '.';
73         programname[MAX_PROGRAM_NAME_LENGTH - 2] = '.';
74         programname[MAX_PROGRAM_NAME_LENGTH - 1] = 0;
75     }
76
77     output = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(programname) + 1));
78     lstrcpyW(output, programname);
79
80     return output;
81 }
82
83 static LPWSTR g_ProgramName;
84 static HFONT g_hBoldFont;
85 static HMENU g_hDebugMenu = NULL;
86
87 static void set_bold_font(HWND hDlg)
88 {
89     HFONT hNormalFont = (HFONT)SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1,
90             WM_GETFONT, 0, 0);
91     LOGFONTW font;
92     GetObjectW(hNormalFont, sizeof(LOGFONTW), &font);
93     font.lfWeight = FW_BOLD;
94     g_hBoldFont = CreateFontIndirectW(&font);
95     SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1, WM_SETFONT, (WPARAM)g_hBoldFont, TRUE);
96 }
97
98 static void set_message_with_filename(HWND hDlg)
99 {
100     WCHAR originalText[1000];
101     WCHAR newText[1000 + MAX_PROGRAM_NAME_LENGTH];
102
103     GetDlgItemTextW(hDlg, IDC_STATIC_TXT1, originalText,
104             sizeof(originalText)/sizeof(originalText[0]));
105     wsprintfW(newText, originalText, g_ProgramName);
106     SetDlgItemTextW(hDlg, IDC_STATIC_TXT1, newText);
107 }
108
109 static INT_PTR WINAPI DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
110 {
111     switch (msg)
112     {
113     case WM_INITDIALOG:
114     {
115         set_bold_font(hwnd);
116         set_message_with_filename(hwnd);
117         return TRUE;
118     }
119     case WM_CTLCOLORSTATIC:
120     {
121         /* WM_CTLCOLOR* don't use DWLP_MSGRESULT */
122         INT_PTR id = GetDlgCtrlID((HWND)lParam);
123         if (id == IDC_STATIC_BG || id == IDC_STATIC_TXT1)
124             return (LONG_PTR)GetSysColorBrush(COLOR_WINDOW);
125
126         return FALSE;
127     }
128     case WM_RBUTTONDOWN:
129     {
130         POINT mousePos;
131         if (!(wParam & MK_SHIFT))
132             return FALSE;
133         if (g_hDebugMenu == NULL)
134             g_hDebugMenu = LoadMenuW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDM_DEBUG_POPUP));
135         GetCursorPos(&mousePos);
136         TrackPopupMenu(GetSubMenu(g_hDebugMenu, 0), TPM_RIGHTBUTTON, mousePos.x, mousePos.y,
137                 0, hwnd, NULL);
138         return TRUE;
139     }
140     case WM_COMMAND:
141         switch (LOWORD(wParam))
142         {
143             case IDOK:
144             case IDCANCEL:
145             case ID_DEBUG:
146                 EndDialog(hwnd, LOWORD(wParam));
147                 return TRUE;
148         }
149         return TRUE;
150     }
151     return FALSE;
152 }
153
154 BOOL display_crash_dialog(void)
155 {
156     INT_PTR result;
157     /* dbg_curr_process->handle is not set */
158     HANDLE hProcess;
159
160     if (!DBG_IVAR(ShowCrashDialog))
161         return TRUE;
162
163     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dbg_curr_pid);
164     g_ProgramName = get_program_name(hProcess);
165     CloseHandle(hProcess);
166     result = DialogBoxW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_CRASH_DLG), NULL, DlgProc);
167     if (result == ID_DEBUG) {
168         AllocConsole();
169         return FALSE;
170     }
171     return TRUE;
172 }