Slightly improve wx[Generic]RichMessageDialog layout.
[wxWidgets.git] / src / generic / msgdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/msgdlgg.cpp
3 // Purpose: wxGenericMessageDialog
4 // Author: Julian Smart, Robert Roebling
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_MSGDLG
20
21 #ifndef WX_PRECOMP
22 #include "wx/utils.h"
23 #include "wx/dialog.h"
24 #include "wx/button.h"
25 #include "wx/stattext.h"
26 #include "wx/statbmp.h"
27 #include "wx/layout.h"
28 #include "wx/intl.h"
29 #include "wx/icon.h"
30 #include "wx/sizer.h"
31 #include "wx/app.h"
32 #include "wx/settings.h"
33 #endif
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #define __WX_COMPILING_MSGDLGG_CPP__ 1
39 #include "wx/msgdlg.h"
40 #include "wx/artprov.h"
41 #include "wx/textwrapper.h"
42
43 #if wxUSE_STATLINE
44 #include "wx/statline.h"
45 #endif
46
47 // ----------------------------------------------------------------------------
48 // wxTitleTextWrapper: simple class to create wrapped text in "title font"
49 // ----------------------------------------------------------------------------
50
51 class wxTitleTextWrapper : public wxTextSizerWrapper
52 {
53 public:
54 wxTitleTextWrapper(wxWindow *win)
55 : wxTextSizerWrapper(win)
56 {
57 }
58
59 protected:
60 virtual wxWindow *OnCreateLine(const wxString& s)
61 {
62 wxWindow * const win = wxTextSizerWrapper::OnCreateLine(s);
63
64 win->SetFont(win->GetFont().Larger().MakeBold());
65
66 return win;
67 }
68 };
69
70 // ----------------------------------------------------------------------------
71 // icons
72 // ----------------------------------------------------------------------------
73
74 BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
75 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
76 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
77 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
78 END_EVENT_TABLE()
79
80 IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
81
82 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
83 const wxString& message,
84 const wxString& caption,
85 long style,
86 const wxPoint& pos)
87 : wxMessageDialogBase(GetParentForModalDialog(parent, style),
88 message,
89 caption,
90 style),
91 m_pos(pos)
92 {
93 m_created = false;
94 }
95
96 void wxGenericMessageDialog::DoCreateMsgdialog()
97 {
98 wxDialog::Create(m_parent, wxID_ANY, m_caption, m_pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
99
100 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
101
102 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
103
104 wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
105
106 #if wxUSE_STATBMP
107 // 1) icon
108 if (m_dialogStyle & wxICON_MASK)
109 {
110 wxStaticBitmap *icon = new wxStaticBitmap
111 (
112 this,
113 wxID_ANY,
114 wxArtProvider::GetMessageBoxIcon(m_dialogStyle)
115 );
116 if (is_pda)
117 topsizer->Add( icon, 0, wxTOP|wxLEFT|wxRIGHT | wxALIGN_LEFT, 10 );
118 else
119 icon_text->Add(icon, wxSizerFlags().Top().Border(wxRIGHT, 20));
120 }
121 #endif // wxUSE_STATBMP
122
123 #if wxUSE_STATTEXT
124 // 2) text
125
126 wxBoxSizer * const textsizer = new wxBoxSizer(wxVERTICAL);
127
128 // We want to show the main message in a different font to make it stand
129 // out if the extended message is used as well. This looks better and is
130 // more consistent with the native dialogs under MSW and GTK.
131 wxString lowerMessage;
132 if ( !m_extendedMessage.empty() )
133 {
134 wxTitleTextWrapper titleWrapper(this);
135 textsizer->Add(CreateTextSizer(GetMessage(), titleWrapper),
136 wxSizerFlags().Border(wxBOTTOM, 20));
137
138 lowerMessage = GetExtendedMessage();
139 }
140 else // no extended message
141 {
142 lowerMessage = GetMessage();
143 }
144
145 textsizer->Add(CreateTextSizer(lowerMessage));
146
147 icon_text->Add(textsizer, 0, wxALIGN_CENTER, 10);
148 topsizer->Add( icon_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
149 #endif // wxUSE_STATTEXT
150
151 // 3) optional checkbox and detailed text
152 AddMessageDialogCheckBox( topsizer );
153 AddMessageDialogDetails( topsizer );
154
155 // 4) buttons
156 int center_flag = wxEXPAND;
157 if (m_dialogStyle & wxYES_NO)
158 center_flag = wxALIGN_CENTRE;
159 wxSizer *sizerBtn = CreateSeparatedButtonSizer
160 (
161 m_dialogStyle & (wxOK | wxCANCEL | wxYES_NO |
162 wxNO_DEFAULT | wxCANCEL_DEFAULT)
163 );
164 if ( sizerBtn )
165 topsizer->Add(sizerBtn, 0, center_flag | wxALL, 10 );
166
167 SetAutoLayout( true );
168 SetSizer( topsizer );
169
170 topsizer->SetSizeHints( this );
171 topsizer->Fit( this );
172 wxSize size( GetSize() );
173 if (size.x < size.y*3/2)
174 {
175 size.x = size.y*3/2;
176 SetSize( size );
177 }
178
179 Centre( wxBOTH | wxCENTER_FRAME);
180 }
181
182 void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
183 {
184 EndModal( wxID_YES );
185 }
186
187 void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
188 {
189 EndModal( wxID_NO );
190 }
191
192 void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
193 {
194 // Allow cancellation via ESC/Close button except if
195 // only YES and NO are specified.
196 const long style = GetMessageDialogStyle();
197 if ( (style & wxYES_NO) != wxYES_NO || (style & wxCANCEL) )
198 {
199 EndModal( wxID_CANCEL );
200 }
201 }
202
203 int wxGenericMessageDialog::ShowModal()
204 {
205 if ( !m_created )
206 {
207 m_created = true;
208 DoCreateMsgdialog();
209 }
210
211 return wxMessageDialogBase::ShowModal();
212 }
213
214 #endif // wxUSE_MSGDLG