]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dialog.cpp
Implement wxComboCtrl::GetSizeFromTextSize().
[wxWidgets.git] / src / gtk / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/dialog.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/cursor.h"
17 #endif // WX_PRECOMP
18
19 #include "wx/evtloop.h"
20
21 #include "wx/scopedptr.h"
22 #include "wx/testing.h"
23
24 #include <gtk/gtk.h>
25
26 // this is defined in src/gtk/toplevel.cpp
27 extern int wxOpenModalDialogsCount;
28
29 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxGUIEventLoop)
30
31
32 //-----------------------------------------------------------------------------
33 // wxDialog
34 //-----------------------------------------------------------------------------
35
36 void wxDialog::Init()
37 {
38 m_modalLoop = NULL;
39 m_returnCode = 0;
40 m_modalShowing = false;
41 m_themeEnabled = true;
42 }
43
44 wxDialog::wxDialog( wxWindow *parent,
45 wxWindowID id, const wxString &title,
46 const wxPoint &pos, const wxSize &size,
47 long style, const wxString &name )
48 {
49 Init();
50
51 (void)Create( parent, id, title, pos, size, style, name );
52 }
53
54 bool wxDialog::Create( wxWindow *parent,
55 wxWindowID id, const wxString &title,
56 const wxPoint &pos, const wxSize &size,
57 long style, const wxString &name )
58 {
59 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
60
61 // all dialogs should have tab traversal enabled
62 style |= wxTAB_TRAVERSAL;
63
64 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
65 }
66
67 bool wxDialog::Show( bool show )
68 {
69 if (!show && IsModal())
70 {
71 EndModal( wxID_CANCEL );
72 }
73
74 if (show && CanDoLayoutAdaptation())
75 DoLayoutAdaptation();
76
77 bool ret = wxDialogBase::Show(show);
78
79 if (show)
80 InitDialog();
81
82 return ret;
83 }
84
85 wxDialog::~wxDialog()
86 {
87 // if the dialog is modal, this will end its event loop
88 if ( IsModal() )
89 EndModal(wxID_CANCEL);
90 }
91
92 bool wxDialog::IsModal() const
93 {
94 return m_modalShowing;
95 }
96
97 void wxDialog::SetModal( bool WXUNUSED(flag) )
98 {
99 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
100 }
101
102 int wxDialog::ShowModal()
103 {
104 WX_TESTING_SHOW_MODAL_HOOK();
105
106 wxASSERT_MSG( !IsModal(), "ShowModal() can't be called twice" );
107
108 // release the mouse if it's currently captured as the window having it
109 // will be disabled when this dialog is shown -- but will still keep the
110 // capture making it impossible to do anything in the modal dialog itself
111 wxWindow * const win = wxWindow::GetCapture();
112 if ( win )
113 win->GTKReleaseMouseAndNotify();
114
115 wxWindow * const parent = GetParentForModalDialog();
116 if ( parent )
117 {
118 gtk_window_set_transient_for( GTK_WINDOW(m_widget),
119 GTK_WINDOW(parent->m_widget) );
120 }
121
122 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
123
124 Show( true );
125
126 m_modalShowing = true;
127
128 wxOpenModalDialogsCount++;
129
130 // NOTE: gtk_window_set_modal internally calls gtk_grab_add() !
131 gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE);
132
133 // Run modal dialog event loop.
134 {
135 wxGUIEventLoopTiedPtr modal(&m_modalLoop, new wxGUIEventLoop());
136 m_modalLoop->Run();
137 }
138
139 gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE);
140
141 wxOpenModalDialogsCount--;
142
143 return GetReturnCode();
144 }
145
146 void wxDialog::EndModal( int retCode )
147 {
148 SetReturnCode( retCode );
149
150 if (!IsModal())
151 {
152 wxFAIL_MSG( "either wxDialog:EndModal called twice or ShowModal wasn't called" );
153 return;
154 }
155
156 m_modalShowing = false;
157
158 // Ensure Exit() is only called once. The dialog's event loop may be terminated
159 // externally due to an uncaught exception.
160 if (m_modalLoop && m_modalLoop->IsRunning())
161 m_modalLoop->Exit();
162
163 Show( false );
164 }