]> git.saurik.com Git - wxWidgets.git/blame - src/generic/msgdlgg.cpp
buttons now become default when they have focus
[wxWidgets.git] / src / generic / msgdlgg.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: msgdlgg.cpp
3// Purpose: wxGenericMessageDialog
15b24b14 4// Author: Julian Smart, Robert Roebling
c801d85f
KB
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
15b24b14 8// Copyright: (c) Julian Smart, Markus Holzem, Robert Roebling
f03fc89f 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "msgdlgg.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
dcf924a3
RR
24 #include "wx/utils.h"
25 #include "wx/dialog.h"
26 #include "wx/button.h"
27 #include "wx/stattext.h"
28 #include "wx/intl.h"
c801d85f
KB
29#endif
30
31#include <stdio.h>
32#include <string.h>
33
34#include "wx/generic/msgdlgg.h"
35
dcf924a3
RR
36#if wxUSE_STATLINE
37 #include "wx/statline.h"
b0351fc9
RR
38#endif
39
dcf924a3
RR
40/* Split message, using constraints to position controls */
41static wxSize wxSplitMessage2( const wxString &message, wxWindow *parent )
c801d85f 42{
15b24b14
RR
43 int y = 10;
44 int w = 50;
45 wxString line( _T("") );
46 for (uint pos = 0; pos < message.Len(); pos++)
47 {
48 if (message[pos] == _T('\n'))
f03fc89f
VZ
49 {
50 if (!line.IsEmpty())
51 {
52 wxStaticText *s1 = new wxStaticText( parent, -1, line, wxPoint(15,y) );
53 wxSize size1( s1->GetSize() );
54 if (size1.x > w) w = size1.x;
55 line = _T("");
56 }
57 y += 18;
58 }
59 else
60 {
61 line += message[pos];
62 }
15b24b14
RR
63 }
64
65 if (!line.IsEmpty())
66 {
f03fc89f
VZ
67 wxStaticText *s2 = new wxStaticText( parent, -1, line, wxPoint(15,y) );
68 wxSize size2( s2->GetSize() );
69 if (size2.x > w) w = size2.x;
15b24b14 70 }
f03fc89f 71
15b24b14
RR
72 y += 18;
73
74 return wxSize(w+30,y);
c801d85f
KB
75}
76
77#if !USE_SHARED_LIBRARY
78BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
f03fc89f
VZ
79 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
80 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
81 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
c801d85f
KB
82END_EVENT_TABLE()
83
84IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
85#endif
86
15b24b14
RR
87wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent, const wxString& message,
88 const wxString& caption, long style, const wxPoint& pos) :
aa64626e 89 wxDialog( parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE )
c801d85f 90{
dfc54541 91 m_dialogStyle = style;
c801d85f 92
dcf924a3
RR
93 wxBeginBusyCursor();
94
15b24b14 95 wxSize message_size( wxSplitMessage2( message, this ) );
c801d85f 96
c67daf87 97 wxButton *ok = (wxButton *) NULL;
dfc54541
JS
98 wxButton *cancel = (wxButton *) NULL;
99 wxButton *yes = (wxButton *) NULL;
100 wxButton *no = (wxButton *) NULL;
15b24b14
RR
101
102 int y = message_size.y + 30;
103
104 if (style & wxYES_NO)
105 {
106 yes = new wxButton( this, wxID_YES, _("Yes"), wxPoint(-1,y), wxSize(80,-1) );
f03fc89f 107 m_buttons.Append( yes );
15b24b14 108 no = new wxButton( this, wxID_NO, _("No"), wxPoint(-1,y), wxSize(80,-1) );
f03fc89f 109 m_buttons.Append( no );
dfc54541
JS
110 }
111
15b24b14
RR
112 if (style & wxOK)
113 {
114 ok = new wxButton( this, wxID_OK, _("OK"), wxPoint(-1,y), wxSize(80,-1) );
f03fc89f 115 m_buttons.Append( ok );
dfc54541
JS
116 }
117
15b24b14
RR
118 if (style & wxCANCEL)
119 {
120 cancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxPoint(-1,y), wxSize(80,-1) );
f03fc89f 121 m_buttons.Append( cancel );
dfc54541
JS
122 }
123
124 if (ok)
125 {
15b24b14
RR
126 ok->SetDefault();
127 ok->SetFocus();
dfc54541
JS
128 }
129 else if (yes)
130 {
96c5bd7f
KB
131 if(style & wxNO_DEFAULT)
132 {
133 no->SetDefault();
134 no->SetFocus();
135 }
136 else
137 {
138 yes->SetDefault();
139 yes->SetFocus();
140 }
dfc54541 141 }
227e5e99 142
15b24b14
RR
143 int w = m_buttons.GetCount() * 100;
144 if (message_size.x > w) w = message_size.x;
145 int space = w / (m_buttons.GetCount()*2);
146
147 int n = 0;
148 wxNode *node = m_buttons.First();
149 while (node)
150 {
151 wxWindow *win = (wxWindow*)node->Data();
f03fc89f
VZ
152 int x = (n*2+1)*space - 40 + 15;
153 win->Move( x, -1 );
15b24b14 154 node = node->Next();
f03fc89f 155 n++;
15b24b14
RR
156 }
157
dcf924a3 158#if wxUSE_STATLINE
b0351fc9
RR
159 (void) new wxStaticLine( this, -1, wxPoint(0,y-20), wxSize(w+30, 5) );
160#endif
161
15b24b14 162 SetSize( w+30, y+40 );
c801d85f 163
15b24b14 164 Centre( wxBOTH );
dcf924a3
RR
165
166 wxEndBusyCursor();
c801d85f
KB
167}
168
169void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
170{
15b24b14 171 EndModal( wxID_YES );
c801d85f
KB
172}
173
174void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
175{
15b24b14 176 EndModal( wxID_NO );
c801d85f
KB
177}
178
179void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
180{
15b24b14
RR
181 /* Allow cancellation via ESC/Close button except if
182 only YES and NO are specified. */
dfc54541 183 if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
15b24b14
RR
184 {
185 EndModal( wxID_CANCEL );
186 }
c801d85f
KB
187}
188
189