]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dialog.cpp
OnEnter/OnDragOver API update (compiles, links, somewhat runs under wxGTK)
[wxWidgets.git] / src / os2 / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose: wxDialog class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/14/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/dialog.h"
17 #include "wx/utils.h"
18 #include "wx/frame.h"
19 #include "wx/app.h"
20 #include "wx/settings.h"
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #endif
24
25 #include "wx/os2/private.h"
26 #include "wx/log.h"
27
28 #define wxDIALOG_DEFAULT_X 300
29 #define wxDIALOG_DEFAULT_Y 300
30
31 // Lists to keep track of windows, so we can disable/enable them
32 // for modal dialogs
33 wxWindowList wxModalDialogs;
34 wxWindowList wxModelessWindows; // Frames and modeless dialogs
35 extern wxList WXDLLEXPORT wxPendingDelete;
36
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
39
40 BEGIN_EVENT_TABLE(wxDialog, wxPanel)
41 EVT_SIZE(wxDialog::OnSize)
42 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
43 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
44 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
45 EVT_CHAR_HOOK(wxDialog::OnCharHook)
46 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
47 EVT_CLOSE(wxDialog::OnCloseWindow)
48 END_EVENT_TABLE()
49 #endif
50
51 wxDialog::wxDialog()
52 {
53 m_isShown = FALSE;
54 m_modalShowing = FALSE;
55
56 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
57 }
58
59 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
60 const wxString& title,
61 const wxPoint& pos,
62 const wxSize& size,
63 long style,
64 const wxString& name)
65 {
66 #if wxUSE_TOOLTIPS
67 m_hwndToolTip = 0;
68 #endif
69
70 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
71 SetName(name);
72
73 if (!parent)
74 wxTopLevelWindows.Append(this);
75
76 if (parent) parent->AddChild(this);
77
78 if ( id == -1 )
79 m_windowId = (int)NewControlId();
80 else
81 m_windowId = id;
82
83 int x = pos.x;
84 int y = pos.y;
85 int width = size.x;
86 int height = size.y;
87
88 if (x < 0) x = wxDIALOG_DEFAULT_X;
89 if (y < 0) y = wxDIALOG_DEFAULT_Y;
90
91 m_windowStyle = style;
92
93 m_isShown = FALSE;
94 m_modalShowing = FALSE;
95
96 if (width < 0)
97 width = 500;
98 if (height < 0)
99 height = 500;
100
101 // TODO: convert below to OS/2 PM code
102
103 // All dialogs should really have this style
104 // m_windowStyle |= wxTAB_TRAVERSAL;
105 //
106 // WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
107 // if (m_windowStyle & wxSTAY_ON_TOP)
108 // extendedStyle |= WS_EX_TOPMOST;
109 //
110 // Allows creation of dialogs with & without captions under MSWindows,
111 // resizeable or not (but a resizeable dialog always has caption -
112 // otherwise it would look too strange)
113 // const wxChar *dlg;
114 // if ( style & wxRESIZE_BORDER )
115 // dlg = wxT("wxResizeableDialog");
116 // else if ( style & wxCAPTION )
117 // dlg = wxT("wxCaptionDialog");
118 // else
119 // dlg = wxT("wxNoCaptionDialog");
120 // MSWCreate(m_windowId, parent, NULL, this, NULL,
121 // x, y, width, height,
122 // 0, // style is not used if we have dlg template
123 // dlg,
124 // extendedStyle);
125 //
126 // HWND hwnd = (HWND)GetHWND();
127 //
128 // if ( !hwnd )
129 // {
130 // wxLogError(wxT("Failed to create dialog."));
131 //
132 // return FALSE;
133 // }
134 //
135 // SubclassWin(GetHWND());
136 //
137 // SetWindowText(hwnd, title);
138 // SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
139 //
140 return TRUE;
141 }
142
143 void wxDialog::SetModal(bool flag)
144 {
145 if ( flag )
146 m_windowStyle |= wxDIALOG_MODAL ;
147 else if ( m_windowStyle & wxDIALOG_MODAL )
148 m_windowStyle -= wxDIALOG_MODAL ;
149
150 wxModelessWindows.DeleteObject(this);
151 if (!flag)
152 wxModelessWindows.Append(this);
153 }
154
155 wxDialog::~wxDialog()
156 {
157 m_isBeingDeleted = TRUE;
158
159 wxTopLevelWindows.DeleteObject(this);
160
161 Show(FALSE);
162
163 if (m_modalShowing)
164 {
165 if (GetParent() && GetParent()->GetHWND())
166 // TODO: bring the parent to the top
167 return;
168 }
169
170 m_modalShowing = FALSE;
171 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL) != wxDIALOG_MODAL )
172 wxModelessWindows.DeleteObject(this);
173
174
175 // If this is the last top-level window, exit.
176 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
177 {
178 wxTheApp->SetTopWindow(NULL);
179
180 if (wxTheApp->GetExitOnFrameDelete())
181 {
182 // TODO: exit
183 }
184 }
185 }
186
187 // By default, pressing escape cancels the dialog
188 void wxDialog::OnCharHook(wxKeyEvent& event)
189 {
190 if (GetHWND())
191 {
192 if (event.m_keyCode == WXK_ESCAPE)
193 {
194 // Behaviour changed in 2.0: we'll send a Cancel message
195 // to the dialog instead of Close.
196 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
197 cancelEvent.SetEventObject( this );
198 GetEventHandler()->ProcessEvent(cancelEvent);
199
200 return;
201 }
202 }
203 // We didn't process this event.
204 event.Skip();
205 }
206
207 void wxDialog::OnPaint(wxPaintEvent& event)
208 {
209 // No: if you call the default procedure, it makes
210 // the following painting code not work.
211 // wxWindow::OnPaint(event);
212 }
213
214 void wxDialog::Fit()
215 {
216 wxWindow::Fit();
217 }
218
219 void wxDialog::Iconize(bool WXUNUSED(iconize))
220 {
221 // Windows dialog boxes can't be iconized
222 }
223
224 bool wxDialog::IsIconized() const
225 {
226 return FALSE;
227 }
228
229 void wxDialog::DoSetClientSize(int width, int height)
230 {
231 // TODO: Convert the below to OS/2 PM code
232
233 // HWND hWnd = (HWND) GetHWND();
234 // RECT rect;
235 // ::GetClientRect(hWnd, &rect);
236 //
237 // RECT rect2;
238 // GetWindowRect(hWnd, &rect2);
239 //
240 // Find the difference between the entire window (title bar and all)
241 // and the client area; add this to the new client size to move the
242 // window
243 // int actual_width = rect2.right - rect2.left - rect.right + width;
244 // int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
245
246 // MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
247 //
248 // wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
249 // event.SetEventObject( this );
250 // GetEventHandler()->ProcessEvent(event);
251 }
252 void wxDialog::GetPosition(int *x, int *y) const
253 {
254 // TODO: Convert
255 // HWND hWnd = (HWND) GetHWND();
256 // RECT rect;
257 // GetWindowRect(hWnd, &rect);
258
259 // *x = rect.left;
260 // *y = rect.top;
261 }
262
263 bool wxDialog::IsShown() const
264 {
265 return m_isShown;
266 }
267
268 bool wxDialog::IsModal() const
269 {
270 return wxModalDialogs.Find((wxDialog *)this) != 0; // const_cast
271 }
272
273 bool wxDialog::Show(bool show)
274 {
275 // TODO: This is involved code, look at msw port for details
276 return FALSE;
277 }
278
279 void wxDialog::SetTitle(const wxString& title)
280 {
281 ::WinSetWindowText((HWND) GetHWND(), title.c_str());
282 }
283
284 wxString wxDialog::GetTitle() const
285 {
286 ::WinQueryWindowText((HWND) GetHWND(), 1000, wxBuffer);
287 return wxString(wxBuffer);
288 }
289
290 void wxDialog::Centre(int direction)
291 {
292 int x_offset,y_offset ;
293 int display_width, display_height;
294 int width, height, x, y;
295 wxWindow *parent = GetParent();
296 if ((direction & wxCENTER_FRAME) && parent)
297 {
298 parent->GetPosition(&x_offset,&y_offset) ;
299 parent->GetSize(&display_width,&display_height) ;
300 }
301 else
302 {
303 wxDisplaySize(&display_width, &display_height);
304 x_offset = 0 ;
305 y_offset = 0 ;
306 }
307
308 GetSize(&width, &height);
309 GetPosition(&x, &y);
310
311 if (direction & wxHORIZONTAL)
312 x = (int)((display_width - width)/2);
313 if (direction & wxVERTICAL)
314 y = (int)((display_height - height)/2);
315
316 SetSize(x+x_offset, y+y_offset, width, height);
317 }
318
319 // Replacement for Show(TRUE) for modal dialogs - returns return code
320 int wxDialog::ShowModal()
321 {
322 m_windowStyle |= wxDIALOG_MODAL;
323 Show(TRUE);
324 return GetReturnCode();
325 }
326
327 void wxDialog::EndModal(int retCode)
328 {
329 SetReturnCode(retCode);
330 // TODO modal un-showing
331 Show(FALSE);
332 }
333
334 // Define for each class of dialog and control
335 WXHBRUSH wxDialog::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
336 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
337 {
338 return 0;
339 }
340 // Standard buttons
341 void wxDialog::OnOK(wxCommandEvent& event)
342 {
343 if ( Validate() && TransferDataFromWindow() )
344 {
345 if ( IsModal() )
346 EndModal(wxID_OK);
347 else
348 {
349 SetReturnCode(wxID_OK);
350 this->Show(FALSE);
351 }
352 }
353 }
354
355 void wxDialog::OnApply(wxCommandEvent& event)
356 {
357 if (Validate())
358 TransferDataFromWindow();
359 // TODO probably need to disable the Apply button until things change again
360 }
361
362 void wxDialog::OnCancel(wxCommandEvent& event)
363 {
364 if ( IsModal() )
365 EndModal(wxID_CANCEL);
366 else
367 {
368 SetReturnCode(wxID_CANCEL);
369 this->Show(FALSE);
370 }
371 }
372
373 void wxDialog::OnCloseWindow(wxCloseEvent& event)
374 {
375 // We'll send a Cancel message by default,
376 // which may close the dialog.
377 // Check for looping if the Cancel event handler calls Close().
378
379 // Note that if a cancel button and handler aren't present in the dialog,
380 // nothing will happen when you close the dialog via the window manager, or
381 // via Close().
382 // We wouldn't want to destroy the dialog by default, since the dialog may have been
383 // created on the stack.
384 // However, this does mean that calling dialog->Close() won't delete the dialog
385 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
386 // sure to destroy the dialog.
387 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
388
389 static wxList closing;
390
391 if ( closing.Member(this) )
392 return;
393
394 closing.Append(this);
395
396 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
397 cancelEvent.SetEventObject( this );
398 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
399
400 closing.DeleteObject(this);
401 }
402
403 // Destroy the window (delayed, if a managed window)
404 bool wxDialog::Destroy()
405 {
406 if (!wxPendingDelete.Member(this))
407 wxPendingDelete.Append(this);
408 return TRUE;
409 }
410
411 void wxDialog::OnSize(wxSizeEvent& WXUNUSED(event))
412 {
413 // if we're using constraints - do use them
414 #if wxUSE_CONSTRAINTS
415 if ( GetAutoLayout() )
416 {
417 Layout();
418 }
419 #endif
420 }
421
422 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
423 {
424 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
425 Refresh();
426 }
427
428 MRESULT wxDialog::OS2WindowProc(HWND hwnd, WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
429 {
430 MRESULT rc = 0;
431 bool processed = FALSE;
432
433 switch ( message )
434 {
435 case WM_CLOSE:
436 // if we can't close, tell the system that we processed the
437 // message - otherwise it would close us
438 processed = !Close();
439 break;
440 }
441
442 if ( !processed )
443 rc = wxWindow::OS2WindowProc(hwnd, message, wParam, lParam);
444
445 return rc;
446 }
447