Minor cleanup
[TortoiseGit.git] / src / Utils / MiscUI / MessageBox.h
blobc6d6cdb77275becd152f5f4b76719a26a5d1a2ef
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
21 #include "htmlformatter.h"
22 #include "dlgtemplate.h"
23 #include "cursor.h"
25 #define IDC_MESSAGEBOX_BUTTON1 101
26 #define IDC_MESSAGEBOX_BUTTON2 102
27 #define IDC_MESSAGEBOX_BUTTON3 103
28 #define IDC_MESSAGEBOX_CHECKBOX 104
30 #define MESSAGEBOX_BUTTONMARGIN 5
31 #define MESSAGEBOX_ICONMARGIN 15
32 #define MESSAGEBOX_BORDERMARGINX 10
33 #define MESSAGEBOX_BORDERMARGINY 5
34 #define MESSAGEBOX_TEXTBUTTONMARGIN 15
35 #define MESSAGEBOX_BUTTONCHECKMARGIN 5
36 #define MESSAGEBOX_BUTTONX 20
37 #define MESSAGEBOX_BUTTONY 5
39 /**
40 * \ingroup Utils
41 * Implements an enhanced MessageBox().\n
42 * It supports limited html formatting of the text (inherited from CHTMLFormatter).
43 * Also it supports hyperlinks and starts the web browser if you click on a link.
44 * \image html "CMessageBox_1.jpg"
45 * \image html "CMessageBox_4.jpg"
46 * \image html "CMessageBox_5.jpg"
47 * and also a checkbox for "Do not show again" functionality.\n
48 * \image html "CMessageBox_2.jpg"
50 * You can use CMessageBox::Show() as a replacement for the Platform SDK version of
51 * MessageBox(). Most of the flags are supported (param uType):\n
52 * To indicate the buttons displayed in the message box, specify one of the following values:
53 * <TABLE>
54 * <TR VALIGN="top">
55 * <TH align=left width=39%>Value</TH>
56 * <TH align=left width=61%>Meaning</TH>
57 * </TR>
59 * <TR VALIGN="top">
60 * <TD width=39%>MB_ABORTRETRYIGNORE</TD>
61 * <TD width=61%>The message box contains three push buttons: <B>Abort</B>, <B>Retry</B>, and <B>Ignore</B>.</TD>
62 * </TR>
64 * <TR VALIGN="top">
65 * <TD width=39%>MB_CANCELTRYCONTINUE</TD>
66 * <TD width=61%>The message box contains three push buttons: <B>Cancel</B>, <B>Try Again</B>, <B>Continue</B>. Use this message box type instead of MB_ABORTRETRYIGNORE.</TD>
67 * </TR>
69 * <TR VALIGN="top">
70 * <TD width=39%>MB_OK</TD>
71 * <TD width=61%>The message box contains one push button: <B>OK</B>. This is the default.</TD>
72 * </TR>
74 * <TR VALIGN="top">
75 * <TD width=39%>MB_OKCANCEL</TD>
76 * <TD width=61%>The message box contains two push buttons: <B>OK</B> and <B>Cancel</B>.</TD>
77 * </TR>
79 * <TR VALIGN="top">
80 * <TD width=39%>MB_RETRYCANCEL</TD>
81 * <TD width=61%>The message box contains two push buttons: <B>Retry</B> and <B>Cancel</B>.</TD>
82 * </TR>
84 * <TR VALIGN="top">
85 * <TD width=39%>MB_YESNO</TD>
86 * <TD width=61%>The message box contains two push buttons: <B>Yes</B> and <B>No</B>.</TD>
87 * </TR>
89 * <TR VALIGN="top">
90 * <TD width=39%>MB_YESNOCANCEL</TD>
91 * <TD width=61%>The message box contains three push buttons: <B>Yes</B>, <B>No</B>, and <B>Cancel</B>.</TD>
92 * </TR>
93 * </TABLE>
94 * To display an icon in the message box, specify one of the following values
95 * <TABLE>
97 * <TR VALIGN="top">
98 * <TH align=left width=38%>Value</TH>
99 * <TH align=left width=62%>Meaning</TH>
100 * </TR>
102 * <TR VALIGN="top">
103 * <TD width=38%>MB_ICONEXCLAMATION, <BR>
104 * MB_ICONWARNING</TD>
105 * <TD width=62%>An exclamation-point icon appears in the message box.</TD>
106 * </TR>
108 * <TR VALIGN="top">
109 * <TD width=38%>MB_ICONINFORMATION, MB_ICONASTERISK</TD>
110 * <TD width=62%>An icon consisting of a lowercase letter <I>i</I> in a circle appears in the message box.</TD>
111 * </TR>
113 * <TR VALIGN="top">
114 * <TD width=38%>MB_ICONQUESTION</TD>
115 * <TD width=62%>A question-mark icon appears in the message box.</TD>
116 * </TR>
118 * <TR VALIGN="top">
119 * <TD width=38%>MB_ICONSTOP, <BR>
120 * MB_ICONERROR, <BR>
121 * MB_ICONHAND</TD>
122 * <TD width=62%>A stop-sign icon appears in the message box.</TD>
123 * </TR>
124 * </TABLE>
125 * To indicate the default button, specify one of the following values
126 * <TABLE>
128 * <TR VALIGN="top">
129 * <TH align=left width=39%>Value</TH>
130 * <TH align=left width=61%>Meaning</TH>
131 * </TR>
133 * <TR VALIGN="top">
134 * <TD width=39%>MB_DEFBUTTON1</TD>
135 * <TD width=61%>The first button is the default button.
136 * <P>MB_DEFBUTTON1 is the default unless MB_DEFBUTTON2, MB_DEFBUTTON3, or MB_DEFBUTTON4 is specified.</P>
137 * </TD>
138 * </TR>
140 * <TR VALIGN="top">
141 * <TD width=39%>MB_DEFBUTTON2</TD>
142 * <TD width=61%>The second button is the default button.</TD>
143 * </TR>
145 * <TR VALIGN="top">
146 * <TD width=39%>MB_DEFBUTTON3</TD>
147 * <TD width=61%>The third button is the default button.</TD>
148 * </TR>
150 * <TR VALIGN="top">
151 * <TD width=39%>MB_DEFBUTTON4</TD>
152 * <TD width=61%>The fourth button is the default button.</TD>
153 * </TR>
154 * </TABLE>
156 * <b>return values:</b>\n
157 * <TABLE>
159 * <TR VALIGN="top">
160 * <TH align=left width=22%>Value</TH>
161 * <TH align=left width=78%>Meaning</TH>
162 * </TR>
164 * <TR VALIGN="top">
165 * <TD width=22%>IDABORT</TD>
166 * <TD width=78%><B>Abort</B> button was selected.</TD>
167 * </TR>
169 * <TR VALIGN="top">
170 * <TD width=22%>IDCANCEL</TD>
171 * <TD width=78%><B>Cancel</B> button was selected.</TD>
172 * </TR>
174 * <TR VALIGN="top">
175 * <TD width=22%>IDCONTINUE</TD>
176 * <TD width=78%><B>Continue</B> button was selected.</TD>
177 * </TR>
179 * <TR VALIGN="top">
180 * <TD width=22%>IDIGNORE</TD>
181 * <TD width=78%><B>Ignore</B> button was selected.</TD>
182 * </TR>
184 * <TR VALIGN="top">
185 * <TD width=22%>IDNO</TD>
186 * <TD width=78%><B>No</B> button was selected.</TD>
187 * </TR>
189 * <TR VALIGN="top">
190 * <TD width=22%>IDOK</TD>
191 * <TD width=78%><B>OK</B> button was selected.</TD>
192 * </TR>
194 * <TR VALIGN="top">
195 * <TD width=22%>IDRETRY</TD>
196 * <TD width=78%><B>Retry</B> button was selected.</TD>
197 * </TR>
199 * <TR VALIGN="top">
200 * <TD width=22%>IDTRYAGAIN</TD>
201 * <TD width=78%><B>Try Again</B> button was selected.</TD>
202 * </TR>
204 * <TR VALIGN="top">
205 * <TD width=22%>IDYES</TD>
206 * <TD width=78%><B>Yes</B> button was selected.</TD>
207 * </TR>
208 * </TABLE>
209 * If a message box has a <B>Cancel</B> button, the function returns the IDCANCEL value
210 * if either the ESC key is pressed or the <B>Cancel</B> button is selected. If the
211 * message box has no <B>Cancel</B> button, pressing ESC has no effect.
213 * To get I18L you can define the following strings in your resource string table:\n
214 * - IDS_MSGBOX_ABORT
215 * - IDS_MSGBOX_RETRY
216 * - IDS_MSGBOX_IGNORE
217 * - IDS_MSGBOX_CANCEL
218 * - IDS_MSGBOX_TRYAGAIN
219 * - IDS_MSGBOX_CONTINUE
220 * - IDS_MSGBOX_OK
221 * - IDS_MSGBOX_YES
222 * - IDS_MSGBOX_NO
223 * - IDS_MSGBOX_DONOTSHOWAGAIN
225 * It is also possible to set the button texts for each one of the three buttons.
226 * Use the corresponding method provided for this.
227 * \image html "CMessageBox_3.jpg"
229 * For the "Do not show again" functionality you can define the registry base path
230 * for storing the return values with\n
231 * \code
232 * #define XMESSAGEBOX_APPREGPATH "Software\\MyApplication\\MsgDialogs\\"
233 * \endcode
234 * if you don't do that define then CMessageBox will use the default path "Software\\YourApplicationName\\"
236 class CMessageBox : public CDialog, public CHTMLFormatter
238 public:
239 CMessageBox(void);
240 ~CMessageBox(void);
242 * Shows a message box. Use this as a replacement for the usual ::MessageBox() calls.
243 * Most of the flags of the Platform SDK version are supported. See the class descriptions
244 * for details.
245 * \param hWnd handle to the parent window or NULL
246 * \param lpMessage the message string to show on the message box
247 * \param lpCaption the dialog title
248 * \param uType see class description for details
249 * \param sHelpPath if uType has MB_HELP, the path for the help file
250 * \return see class descriptions for details
252 static UINT Show(HWND hWnd, LPCTSTR lpMessage, LPCTSTR lpCaption, UINT uType, LPCTSTR sHelpPath = NULL);
254 * Shows a message box.
255 * \param hWnd handle to the parent window or NULL
256 * \param nMessage resource ID of the message string
257 * \param nCaption resource ID of the title string
258 * \param uType see class description for details
259 * \param sHelpPath if uType has MB_HELP, this is the path of the help file to use
260 * \return see class description for details
262 static UINT Show(HWND hWnd, UINT nMessage, UINT nCaption, UINT uType, LPCTSTR sHelpPath = NULL);
264 * Shows a message box.
265 * \param hWnd handle to the parent window or NULL
266 * \param nMessage resource ID of the message string
267 * \param nCaption resource ID of the title string
268 * \param uType see class description for details
269 * \param nHelpID if uType has MB_HELP, this is the help ID to use
270 * \return see class description for details
272 static UINT Show(HWND hWnd, UINT nMessage, UINT nCaption, UINT uType, UINT nHelpID);
274 * Shows a message box with a checkbox. If the user checks it then the next time
275 * the message box isn't shown anymore - the method simply returns the same value as
276 * the last time. Use this to give the user the possibility to ignore specific message
277 * boxes ("Do not show again"-checkbox).
278 * \param hWnd handle to the parent window or NULL
279 * \param lpMessage the message string to show on the message box
280 * \param lpCaption the dialog title
281 * \param uType see class description for details
282 * \param lpRegistry a value string to store the return value of this specific message box. Put NULL or empty if you do not use registry.
283 * Each one of your message boxes must have it's own value string! Examples for such values
284 * might be "WarnOverwrite", "InformAboutMissingMailSubject", ...
285 * \param lpCheckMessage the message to show on the checkbox label. If this parameter is omitted
286 * then it defaults to "do not show again" or the string with resource ID IDS_MSGBOX_DONOTSHOWAGAIN.
287 * \param bChecked get and set checkbox check state. This is optional.
288 * \return see class description for details
290 static UINT ShowCheck(HWND hWnd, LPCTSTR lpMessage, LPCTSTR lpCaption, UINT uType, LPCTSTR lpRegistry, LPCTSTR lpCheckMessage = NULL, BOOL *bChecked = NULL);
292 * Shows a message box with a checkbox. If the user checks it then the next time
293 * the message box isn't shown anymore - the method simply returns the same value as
294 * the last time. Use this to give the user the possibility to ignore specific message
295 * boxes ("Do not show again"-checkbox).
296 * \param hWnd handle to the parent window or NULL
297 * \param nMessage resource ID of the message string
298 * \param nCaption resource ID of the title string
299 * \param uType see class description for details
300 * \param lpRegistry a value string to store the return value of this specific message box. Put NULL or empty if you do not use registry.
301 * Each one of your message boxes must have it's own value string! Examples for such values
302 * might be "WarnOverwrite", "InformAboutMissingMailSubject", ...
303 * \param nCheckMessage resource ID of the checkbox string
304 * \param bChecked get and set checkbox check state. This is optional.
305 * \return see class description for details
307 static UINT ShowCheck(HWND hWnd, UINT nMessage, UINT nCaption, UINT uType, LPCTSTR lpRegistry, UINT nCheckMessage, BOOL *bChecked = NULL);
310 * Shows a message box with user defined button texts.
311 * \param hWnd handle to the parent window or NULL
312 * \param lpMessage the message string
313 * \param lpCaption the title string
314 * \param nDef number of the default button (1,2 or 3)
315 * \param icon an icon loaded with MAKEINTRESOURCE() or one of
316 * the system default icons.
317 * \param lpButton1 text for the first button
318 * \param lpButton2 text for the second button
319 * \param lpButton3 text for the third button
320 * \return the number of the button pressed (1,2 or 3)
322 static UINT Show(HWND hWnd, LPCTSTR lpMessage, LPCTSTR lpCaption, int nDef, LPCTSTR icon, LPCTSTR lpButton1, LPCTSTR lpButton2 = NULL, LPCTSTR lpButton3 = NULL);
324 * Shows a message box with user defined button texts.
325 * \param hWnd handle to the parent window or NULL
326 * \param nMessage resource ID of the message string
327 * \param nCaption resource ID of the title string
328 * \param nDef number of the default button (1,2 or 3)
329 * \param icon an icon loaded with MAKEINTRESOURCE() or one of
330 * the system default icons.
331 * \param nButton1 resource ID of the text for the first button
332 * \param nButton2 resource ID of the text for the second button
333 * \param nButton3 resource ID of the text for the third button
334 * \return the number of the button pressed (1,2 or 3)
336 static UINT Show(HWND hWnd, UINT nMessage, UINT nCaption, int nDef, LPCTSTR icon, UINT nButton1, UINT nButton2 = NULL, UINT nButton3 = NULL);
338 * Shows a message box with user defined button texts and a checkbox.
339 * \param hWnd handle to the parent window or NULL
340 * \param lpMessage the message string
341 * \param lpCaption the title string
342 * \param nDef number of the default button (1,2 or 3)
343 * \param icon an icon loaded with MAKEINTRESOURCE() or one
344 * of the system default icons
345 * \param lpButton1 string for the first button
346 * \param lpButton2 string for the second button
347 * \param lpButton3 string for the third button
348 * \param lpRegistry a value string to store the return value of this specific message box. Put NULL or empty if you do not use registry.
349 * Each one of your message boxes must have it's own value string! Examples for such values
350 * might be "WarnOverwrite", "InformAboutMissingMailSubject", ...
351 * \param lpCheckMessage the message to show on the checkbox label. If this parameter is omitted
352 * then it defaults to "do not show again" or the string with resource ID IDS_MSGBOX_DONOTSHOWAGAIN.
353 * \param bChecked get and set checkbox check state. This is optional.
354 * \return the number of the button pressed (1,2 or 3)
356 static UINT ShowCheck(HWND hWnd, LPCTSTR lpMessage, LPCTSTR lpCaption, int nDef, LPCTSTR icon, LPCTSTR lpButton1, LPCTSTR lpButton2, LPCTSTR lpButton3, LPCTSTR lpRegistry, LPCTSTR lpCheckMessage = NULL, BOOL *bChecked = NULL);
358 * Shows a message box with user defined button texts and a checkbox.
359 * \param hWnd handle to the parent window or NULL
360 * \param nMessage resource ID of the message string
361 * \param nCaption resource ID of the title string
362 * \param nDef number of the default button (1,2 or 3)
363 * \param icon an icon loaded with MAKEINTRESOURCE() or one
364 * of the system default icons
365 * \param nButton1 resource ID of string for the first button
366 * \param nButton2 resource ID of string for the second button
367 * \param nButton3 resource ID of string for the third button
368 * \param lpRegistry a value string to store the return value of this specific message box. Put NULL or empty if you do not use registry.
369 * Each one of your message boxes must have it's own value string! Examples for such values
370 * might be "WarnOverwrite", "InformAboutMissingMailSubject", ...
371 * \param nCheckMessage resource ID of the checkbox string
372 * \param bChecked get and set checkbox check state. This is optional.
373 * \return the number of the button pressed (1,2 or 3)
375 static UINT ShowCheck(HWND hWnd, UINT nMessage, UINT nCaption, int nDef, LPCTSTR icon, UINT nButton1, UINT nButton2, UINT nButton3, LPCTSTR lpRegistry, UINT nCheckMessage = NULL, BOOL *bChecked = NULL);
378 * Removes the registry key
380 static bool RemoveRegistryKey(LPCTSTR lpRegistry);
382 protected:
384 * Stores the value in the registry
385 * \param sValue the value name
386 * \param value DWORD to store
388 static void SetRegistryValue(const CString& sValue, DWORD value);
390 * Shows the modal dialog
391 * \param pWnd handle to the parent window or NULL
392 * \param title message box title
393 * \param msg message to show
394 * \param nDefaultButton number of the default button
395 * \return the value stored in the member variables for the buttons (m_uButtonXRet)
396 * of the button pressed
398 UINT GoModal(CWnd * pWnd, const CString& title, const CString& msg, int nDefaultButton);
401 * Fills in the member variables according to the uType parameter
403 int FillBoxStandard(UINT uType);
406 * Calculates the size of the string in pixels
407 * \param str the string to check the size
409 CSize GetTextSize(const CString& str);
411 * Returns the size of the icon
413 CSize GetIconSize(HICON hIcon);
415 * Returns the size of all four buttons (three pushbuttons and the checkbox).
416 * Also resizes the buttons accordingly and fills in m_szAllButtons and m_szButtons.
418 CSize GetButtonSize();
420 void SetHelpPath(LPCTSTR sHelpPath) {m_sHelpPath = sHelpPath;}
422 LOGFONT m_LogFont;
423 CCursor m_Cursor;
424 CString m_sMessage;
425 HICON m_hIcon;
426 BOOL m_bDestroyIcon;
427 int m_nDefButton;
428 CString m_sButton1;
429 CString m_sButton2;
430 CString m_sButton3;
431 CString m_sCheckbox;
432 CString m_sHelpPath;
433 UINT m_uButton1Ret;
434 UINT m_uButton2Ret;
435 UINT m_uButton3Ret;
436 UINT m_uCancelRet;
437 UINT m_uType;
438 CSize m_szIcon;
439 CSize m_szAllButtons;
440 CSize m_szButtons;
442 BOOL m_bShowCheck;
443 CString m_sRegistryValue;
445 CString m_i18l; //only used if some strings are defined for internationalization
447 public:
448 DECLARE_MESSAGE_MAP()
449 afx_msg void OnPaint();
450 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
451 afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
452 afx_msg void OnButton1();
453 afx_msg void OnButton2();
454 afx_msg void OnButton3();
455 virtual BOOL OnInitDialog();
457 BOOL m_bChecked;
458 protected:
459 virtual void OnCancel();
460 virtual BOOL PreTranslateMessage(MSG* pMsg);