]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/msgdlgg.cpp
buttons now become default when they have focus
[wxWidgets.git] / src / generic / msgdlgg.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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, Markus Holzem, Robert Roebling
9// Licence: wxWindows license
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
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"
29#endif
30
31#include <stdio.h>
32#include <string.h>
33
34#include "wx/generic/msgdlgg.h"
35
36#if wxUSE_STATLINE
37 #include "wx/statline.h"
38#endif
39
40/* Split message, using constraints to position controls */
41static wxSize wxSplitMessage2( const wxString &message, wxWindow *parent )
42{
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'))
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 }
63 }
64
65 if (!line.IsEmpty())
66 {
67 wxStaticText *s2 = new wxStaticText( parent, -1, line, wxPoint(15,y) );
68 wxSize size2( s2->GetSize() );
69 if (size2.x > w) w = size2.x;
70 }
71
72 y += 18;
73
74 return wxSize(w+30,y);
75}
76
77#if !USE_SHARED_LIBRARY
78BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
79 EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
80 EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
81 EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
82END_EVENT_TABLE()
83
84IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
85#endif
86
87wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent, const wxString& message,
88 const wxString& caption, long style, const wxPoint& pos) :
89 wxDialog( parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE )
90{
91 m_dialogStyle = style;
92
93 wxBeginBusyCursor();
94
95 wxSize message_size( wxSplitMessage2( message, this ) );
96
97 wxButton *ok = (wxButton *) NULL;
98 wxButton *cancel = (wxButton *) NULL;
99 wxButton *yes = (wxButton *) NULL;
100 wxButton *no = (wxButton *) NULL;
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) );
107 m_buttons.Append( yes );
108 no = new wxButton( this, wxID_NO, _("No"), wxPoint(-1,y), wxSize(80,-1) );
109 m_buttons.Append( no );
110 }
111
112 if (style & wxOK)
113 {
114 ok = new wxButton( this, wxID_OK, _("OK"), wxPoint(-1,y), wxSize(80,-1) );
115 m_buttons.Append( ok );
116 }
117
118 if (style & wxCANCEL)
119 {
120 cancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxPoint(-1,y), wxSize(80,-1) );
121 m_buttons.Append( cancel );
122 }
123
124 if (ok)
125 {
126 ok->SetDefault();
127 ok->SetFocus();
128 }
129 else if (yes)
130 {
131 if(style & wxNO_DEFAULT)
132 {
133 no->SetDefault();
134 no->SetFocus();
135 }
136 else
137 {
138 yes->SetDefault();
139 yes->SetFocus();
140 }
141 }
142
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();
152 int x = (n*2+1)*space - 40 + 15;
153 win->Move( x, -1 );
154 node = node->Next();
155 n++;
156 }
157
158#if wxUSE_STATLINE
159 (void) new wxStaticLine( this, -1, wxPoint(0,y-20), wxSize(w+30, 5) );
160#endif
161
162 SetSize( w+30, y+40 );
163
164 Centre( wxBOTH );
165
166 wxEndBusyCursor();
167}
168
169void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
170{
171 EndModal( wxID_YES );
172}
173
174void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
175{
176 EndModal( wxID_NO );
177}
178
179void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
180{
181 /* Allow cancellation via ESC/Close button except if
182 only YES and NO are specified. */
183 if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
184 {
185 EndModal( wxID_CANCEL );
186 }
187}
188
189