| 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/ptr_scpd.h" |
| 22 | |
| 23 | #include <gtk/gtk.h> |
| 24 | |
| 25 | // this is defined in src/gtk/toplevel.cpp |
| 26 | extern int wxOpenModalDialogsCount; |
| 27 | |
| 28 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxGUIEventLoop) |
| 29 | |
| 30 | |
| 31 | //----------------------------------------------------------------------------- |
| 32 | // wxDialog |
| 33 | //----------------------------------------------------------------------------- |
| 34 | |
| 35 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow) |
| 36 | |
| 37 | void wxDialog::Init() |
| 38 | { |
| 39 | m_modalLoop = NULL; |
| 40 | m_returnCode = 0; |
| 41 | m_modalShowing = false; |
| 42 | m_themeEnabled = true; |
| 43 | } |
| 44 | |
| 45 | wxDialog::wxDialog( wxWindow *parent, |
| 46 | wxWindowID id, const wxString &title, |
| 47 | const wxPoint &pos, const wxSize &size, |
| 48 | long style, const wxString &name ) |
| 49 | { |
| 50 | Init(); |
| 51 | |
| 52 | (void)Create( parent, id, title, pos, size, style, name ); |
| 53 | } |
| 54 | |
| 55 | bool wxDialog::Create( wxWindow *parent, |
| 56 | wxWindowID id, const wxString &title, |
| 57 | const wxPoint &pos, const wxSize &size, |
| 58 | long style, const wxString &name ) |
| 59 | { |
| 60 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
| 61 | |
| 62 | // all dialogs should have tab traversal enabled |
| 63 | style |= wxTAB_TRAVERSAL; |
| 64 | |
| 65 | return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name); |
| 66 | } |
| 67 | |
| 68 | bool wxDialog::Show( bool show ) |
| 69 | { |
| 70 | if (!show && IsModal()) |
| 71 | { |
| 72 | EndModal( wxID_CANCEL ); |
| 73 | } |
| 74 | |
| 75 | if (show && CanDoLayoutAdaptation()) |
| 76 | DoLayoutAdaptation(); |
| 77 | |
| 78 | bool ret = wxDialogBase::Show(show); |
| 79 | |
| 80 | if (show) |
| 81 | InitDialog(); |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | wxDialog::~wxDialog() |
| 87 | { |
| 88 | // if the dialog is modal, this will end its event loop |
| 89 | if ( IsModal() ) |
| 90 | EndModal(wxID_CANCEL); |
| 91 | } |
| 92 | |
| 93 | bool wxDialog::IsModal() const |
| 94 | { |
| 95 | return m_modalShowing; |
| 96 | } |
| 97 | |
| 98 | void wxDialog::SetModal( bool WXUNUSED(flag) ) |
| 99 | { |
| 100 | wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") ); |
| 101 | } |
| 102 | |
| 103 | int wxDialog::ShowModal() |
| 104 | { |
| 105 | wxASSERT_MSG( !IsModal(), "ShowModal() can't be called twice" ); |
| 106 | |
| 107 | // release the mouse if it's currently captured as the window having it |
| 108 | // will be disabled when this dialog is shown -- but will still keep the |
| 109 | // capture making it impossible to do anything in the modal dialog itself |
| 110 | wxWindow * const win = wxWindow::GetCapture(); |
| 111 | if ( win ) |
| 112 | win->GTKReleaseMouseAndNotify(); |
| 113 | |
| 114 | // use the apps top level window as parent if none given unless explicitly |
| 115 | // forbidden |
| 116 | if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) ) |
| 117 | { |
| 118 | wxWindow * const parent = GetParentForModalDialog(); |
| 119 | if ( parent && parent != this ) |
| 120 | { |
| 121 | gtk_window_set_transient_for( GTK_WINDOW(m_widget), |
| 122 | GTK_WINDOW(parent->m_widget) ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | wxBusyCursorSuspender cs; // temporarily suppress the busy cursor |
| 127 | |
| 128 | Show( true ); |
| 129 | |
| 130 | m_modalShowing = true; |
| 131 | |
| 132 | wxOpenModalDialogsCount++; |
| 133 | |
| 134 | // NOTE: gtk_window_set_modal internally calls gtk_grab_add() ! |
| 135 | gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE); |
| 136 | |
| 137 | // Run modal dialog event loop. |
| 138 | { |
| 139 | wxGUIEventLoopTiedPtr modal(&m_modalLoop, new wxGUIEventLoop()); |
| 140 | m_modalLoop->Run(); |
| 141 | } |
| 142 | |
| 143 | gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE); |
| 144 | |
| 145 | wxOpenModalDialogsCount--; |
| 146 | |
| 147 | return GetReturnCode(); |
| 148 | } |
| 149 | |
| 150 | void wxDialog::EndModal( int retCode ) |
| 151 | { |
| 152 | SetReturnCode( retCode ); |
| 153 | |
| 154 | if (!IsModal()) |
| 155 | { |
| 156 | wxFAIL_MSG( "either wxDialog:EndModal called twice or ShowModal wasn't called" ); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | m_modalShowing = false; |
| 161 | |
| 162 | // Ensure Exit() is only called once. The dialog's event loop may be terminated |
| 163 | // externally due to an uncaught exception. |
| 164 | if (m_modalLoop && m_modalLoop->IsRunning()) |
| 165 | m_modalLoop->Exit(); |
| 166 | |
| 167 | Show( false ); |
| 168 | } |