msgdlg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msgdlg.h
  3. // Purpose: common header and base class for wxMessageDialog
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created:
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSGDLG_H_BASE_
  11. #define _WX_MSGDLG_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_MSGDLG
  14. #include "wx/dialog.h"
  15. #include "wx/stockitem.h"
  16. extern WXDLLIMPEXP_DATA_CORE(const char) wxMessageBoxCaptionStr[];
  17. // ----------------------------------------------------------------------------
  18. // wxMessageDialogBase: base class defining wxMessageDialog interface
  19. // ----------------------------------------------------------------------------
  20. class WXDLLIMPEXP_CORE wxMessageDialogBase : public wxDialog
  21. {
  22. public:
  23. // helper class for SetXXXLabels() methods: it makes it possible to pass
  24. // either a stock id (wxID_CLOSE) or a string ("&Close") to them
  25. class ButtonLabel
  26. {
  27. public:
  28. // ctors are not explicit, objects of this class can be implicitly
  29. // constructed from either stock ids or strings
  30. ButtonLabel(int stockId)
  31. : m_stockId(stockId)
  32. {
  33. wxASSERT_MSG( wxIsStockID(stockId), "invalid stock id" );
  34. }
  35. ButtonLabel(const wxString& label)
  36. : m_label(label), m_stockId(wxID_NONE)
  37. {
  38. }
  39. ButtonLabel(const char *label)
  40. : m_label(label), m_stockId(wxID_NONE)
  41. {
  42. }
  43. ButtonLabel(const wchar_t *label)
  44. : m_label(label), m_stockId(wxID_NONE)
  45. {
  46. }
  47. ButtonLabel(const wxCStrData& label)
  48. : m_label(label), m_stockId(wxID_NONE)
  49. {
  50. }
  51. // default copy ctor and dtor are ok
  52. // get the string label, whether it was originally specified directly
  53. // or as a stock id -- this is only useful for platforms without native
  54. // stock items id support
  55. wxString GetAsString() const
  56. {
  57. return m_stockId == wxID_NONE
  58. ? m_label
  59. : wxGetStockLabel(m_stockId, wxSTOCK_FOR_BUTTON);
  60. }
  61. // return the stock id or wxID_NONE if this is not a stock label
  62. int GetStockId() const { return m_stockId; }
  63. private:
  64. // the label if explicitly given or empty if this is a stock item
  65. const wxString m_label;
  66. // the stock item id or wxID_NONE if m_label should be used
  67. const int m_stockId;
  68. };
  69. // ctors
  70. wxMessageDialogBase() { m_dialogStyle = 0; }
  71. wxMessageDialogBase(wxWindow *parent,
  72. const wxString& message,
  73. const wxString& caption,
  74. long style)
  75. : m_message(message),
  76. m_caption(caption)
  77. {
  78. m_parent = parent;
  79. SetMessageDialogStyle(style);
  80. }
  81. // virtual dtor for the base class
  82. virtual ~wxMessageDialogBase() { }
  83. wxString GetCaption() const { return m_caption; }
  84. virtual void SetMessage(const wxString& message)
  85. {
  86. m_message = message;
  87. }
  88. wxString GetMessage() const { return m_message; }
  89. void SetExtendedMessage(const wxString& extendedMessage)
  90. {
  91. m_extendedMessage = extendedMessage;
  92. }
  93. wxString GetExtendedMessage() const { return m_extendedMessage; }
  94. // change the dialog style flag
  95. void SetMessageDialogStyle(long style)
  96. {
  97. wxASSERT_MSG( ((style & wxYES_NO) == wxYES_NO) || !(style & wxYES_NO),
  98. "wxYES and wxNO may only be used together" );
  99. wxASSERT_MSG( !(style & wxYES) || !(style & wxOK),
  100. "wxOK and wxYES/wxNO can't be used together" );
  101. // It is common to specify just the icon, without wxOK, in the existing
  102. // code, especially one written by Windows programmers as MB_OK is 0
  103. // and so they're used to omitting wxOK. Don't complain about it but
  104. // just add wxOK implicitly for compatibility.
  105. if ( !(style & wxYES) && !(style & wxOK) )
  106. style |= wxOK;
  107. wxASSERT_MSG( (style & wxID_OK) != wxID_OK,
  108. "wxMessageBox: Did you mean wxOK (and not wxID_OK)?" );
  109. wxASSERT_MSG( !(style & wxNO_DEFAULT) || (style & wxNO),
  110. "wxNO_DEFAULT is invalid without wxNO" );
  111. wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || (style & wxCANCEL),
  112. "wxCANCEL_DEFAULT is invalid without wxCANCEL" );
  113. wxASSERT_MSG( !(style & wxCANCEL_DEFAULT) || !(style & wxNO_DEFAULT),
  114. "only one default button can be specified" );
  115. m_dialogStyle = style;
  116. }
  117. long GetMessageDialogStyle() const { return m_dialogStyle; }
  118. // customization of the message box buttons
  119. virtual bool SetYesNoLabels(const ButtonLabel& yes,const ButtonLabel& no)
  120. {
  121. DoSetCustomLabel(m_yes, yes);
  122. DoSetCustomLabel(m_no, no);
  123. return true;
  124. }
  125. virtual bool SetYesNoCancelLabels(const ButtonLabel& yes,
  126. const ButtonLabel& no,
  127. const ButtonLabel& cancel)
  128. {
  129. DoSetCustomLabel(m_yes, yes);
  130. DoSetCustomLabel(m_no, no);
  131. DoSetCustomLabel(m_cancel, cancel);
  132. return true;
  133. }
  134. virtual bool SetOKLabel(const ButtonLabel& ok)
  135. {
  136. DoSetCustomLabel(m_ok, ok);
  137. return true;
  138. }
  139. virtual bool SetOKCancelLabels(const ButtonLabel& ok,
  140. const ButtonLabel& cancel)
  141. {
  142. DoSetCustomLabel(m_ok, ok);
  143. DoSetCustomLabel(m_cancel, cancel);
  144. return true;
  145. }
  146. virtual bool SetHelpLabel(const ButtonLabel& help)
  147. {
  148. DoSetCustomLabel(m_help, help);
  149. return true;
  150. }
  151. // test if any custom labels were set
  152. bool HasCustomLabels() const
  153. {
  154. return !(m_ok.empty() && m_cancel.empty() && m_help.empty() &&
  155. m_yes.empty() && m_no.empty());
  156. }
  157. // these functions return the label to be used for the button which is
  158. // either a custom label explicitly set by the user or the default label,
  159. // i.e. they always return a valid string
  160. wxString GetYesLabel() const
  161. { return m_yes.empty() ? GetDefaultYesLabel() : m_yes; }
  162. wxString GetNoLabel() const
  163. { return m_no.empty() ? GetDefaultNoLabel() : m_no; }
  164. wxString GetOKLabel() const
  165. { return m_ok.empty() ? GetDefaultOKLabel() : m_ok; }
  166. wxString GetCancelLabel() const
  167. { return m_cancel.empty() ? GetDefaultCancelLabel() : m_cancel; }
  168. wxString GetHelpLabel() const
  169. { return m_help.empty() ? GetDefaultHelpLabel() : m_help; }
  170. // based on message dialog style, returns exactly one of: wxICON_NONE,
  171. // wxICON_ERROR, wxICON_WARNING, wxICON_QUESTION, wxICON_INFORMATION,
  172. // wxICON_AUTH_NEEDED
  173. virtual long GetEffectiveIcon() const
  174. {
  175. if ( m_dialogStyle & wxICON_NONE )
  176. return wxICON_NONE;
  177. else if ( m_dialogStyle & wxICON_ERROR )
  178. return wxICON_ERROR;
  179. else if ( m_dialogStyle & wxICON_WARNING )
  180. return wxICON_WARNING;
  181. else if ( m_dialogStyle & wxICON_QUESTION )
  182. return wxICON_QUESTION;
  183. else if ( m_dialogStyle & wxICON_INFORMATION )
  184. return wxICON_INFORMATION;
  185. else if ( m_dialogStyle & wxYES )
  186. return wxICON_QUESTION;
  187. else
  188. return wxICON_INFORMATION;
  189. }
  190. protected:
  191. // for the platforms not supporting separate main and extended messages
  192. // this function should be used to combine both of them in a single string
  193. wxString GetFullMessage() const
  194. {
  195. wxString msg = m_message;
  196. if ( !m_extendedMessage.empty() )
  197. msg << "\n\n" << m_extendedMessage;
  198. return msg;
  199. }
  200. wxString m_message,
  201. m_extendedMessage,
  202. m_caption;
  203. long m_dialogStyle;
  204. // this function is called by our public SetXXXLabels() and should assign
  205. // the value to var with possibly some transformation (e.g. Cocoa version
  206. // currently uses this to remove any accelerators from the button strings
  207. // while GTK+ one handles stock items specifically here)
  208. virtual void DoSetCustomLabel(wxString& var, const ButtonLabel& label)
  209. {
  210. var = label.GetAsString();
  211. }
  212. // these functions return the custom label or empty string and should be
  213. // used only in specific circumstances such as creating the buttons with
  214. // these labels (in which case it makes sense to only use a custom label if
  215. // it was really given and fall back on stock label otherwise), use the
  216. // Get{Yes,No,OK,Cancel}Label() methods above otherwise
  217. const wxString& GetCustomYesLabel() const { return m_yes; }
  218. const wxString& GetCustomNoLabel() const { return m_no; }
  219. const wxString& GetCustomOKLabel() const { return m_ok; }
  220. const wxString& GetCustomHelpLabel() const { return m_help; }
  221. const wxString& GetCustomCancelLabel() const { return m_cancel; }
  222. private:
  223. // these functions may be overridden to provide different defaults for the
  224. // default button labels (this is used by wxGTK)
  225. virtual wxString GetDefaultYesLabel() const { return wxGetTranslation("Yes"); }
  226. virtual wxString GetDefaultNoLabel() const { return wxGetTranslation("No"); }
  227. virtual wxString GetDefaultOKLabel() const { return wxGetTranslation("OK"); }
  228. virtual wxString GetDefaultCancelLabel() const { return wxGetTranslation("Cancel"); }
  229. virtual wxString GetDefaultHelpLabel() const { return wxGetTranslation("Help"); }
  230. // labels for the buttons, initially empty meaning that the defaults should
  231. // be used, use GetYes/No/OK/CancelLabel() to access them
  232. wxString m_yes,
  233. m_no,
  234. m_ok,
  235. m_cancel,
  236. m_help;
  237. wxDECLARE_NO_COPY_CLASS(wxMessageDialogBase);
  238. };
  239. #include "wx/generic/msgdlgg.h"
  240. #if defined(__WX_COMPILING_MSGDLGG_CPP__) || \
  241. defined(__WXUNIVERSAL__) || defined(__WXGPE__) || \
  242. (defined(__WXGTK__) && !defined(__WXGTK20__))
  243. #define wxMessageDialog wxGenericMessageDialog
  244. #elif defined(__WXCOCOA__)
  245. #include "wx/cocoa/msgdlg.h"
  246. #elif defined(__WXMSW__)
  247. #include "wx/msw/msgdlg.h"
  248. #elif defined(__WXMOTIF__)
  249. #include "wx/motif/msgdlg.h"
  250. #elif defined(__WXGTK20__)
  251. #include "wx/gtk/msgdlg.h"
  252. #elif defined(__WXMAC__)
  253. #include "wx/osx/msgdlg.h"
  254. #elif defined(__WXPM__)
  255. #include "wx/os2/msgdlg.h"
  256. #endif
  257. // ----------------------------------------------------------------------------
  258. // wxMessageBox: the simplest way to use wxMessageDialog
  259. // ----------------------------------------------------------------------------
  260. int WXDLLIMPEXP_CORE wxMessageBox(const wxString& message,
  261. const wxString& caption = wxMessageBoxCaptionStr,
  262. long style = wxOK | wxCENTRE,
  263. wxWindow *parent = NULL,
  264. int x = wxDefaultCoord, int y = wxDefaultCoord);
  265. #endif // wxUSE_MSGDLG
  266. #endif // _WX_MSGDLG_H_BASE_