| 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/app.h" |
| 17 | #include "wx/frame.h" |
| 18 | #include "wx/cursor.h" |
| 19 | #endif // WX_PRECOMP |
| 20 | |
| 21 | #include "wx/evtloop.h" |
| 22 | |
| 23 | #include <gdk/gdk.h> |
| 24 | #include <gtk/gtk.h> |
| 25 | #include <gdk/gdkkeysyms.h> |
| 26 | |
| 27 | #include "wx/gtk/win_gtk.h" |
| 28 | |
| 29 | //----------------------------------------------------------------------------- |
| 30 | // global data |
| 31 | //----------------------------------------------------------------------------- |
| 32 | |
| 33 | // Don't allow window closing if there are open dialogs |
| 34 | int g_openDialogs; |
| 35 | |
| 36 | //----------------------------------------------------------------------------- |
| 37 | // wxDialog |
| 38 | //----------------------------------------------------------------------------- |
| 39 | |
| 40 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow) |
| 41 | |
| 42 | void wxDialog::Init() |
| 43 | { |
| 44 | m_returnCode = 0; |
| 45 | m_sizeSet = false; |
| 46 | m_modalShowing = false; |
| 47 | m_themeEnabled = true; |
| 48 | } |
| 49 | |
| 50 | wxDialog::wxDialog( wxWindow *parent, |
| 51 | wxWindowID id, const wxString &title, |
| 52 | const wxPoint &pos, const wxSize &size, |
| 53 | long style, const wxString &name ) |
| 54 | { |
| 55 | Init(); |
| 56 | |
| 57 | (void)Create( parent, id, title, pos, size, style, name ); |
| 58 | } |
| 59 | |
| 60 | bool wxDialog::Create( wxWindow *parent, |
| 61 | wxWindowID id, const wxString &title, |
| 62 | const wxPoint &pos, const wxSize &size, |
| 63 | long style, const wxString &name ) |
| 64 | { |
| 65 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
| 66 | |
| 67 | // all dialogs should have tab traversal enabled |
| 68 | style |= wxTAB_TRAVERSAL; |
| 69 | |
| 70 | return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name); |
| 71 | } |
| 72 | |
| 73 | bool wxDialog::Show( bool show ) |
| 74 | { |
| 75 | if (!show && IsModal()) |
| 76 | { |
| 77 | EndModal( wxID_CANCEL ); |
| 78 | } |
| 79 | |
| 80 | if (show && !m_sizeSet) |
| 81 | { |
| 82 | /* by calling GtkOnSize here, we don't have to call |
| 83 | either after showing the frame, which would entail |
| 84 | much ugly flicker nor from within the size_allocate |
| 85 | handler, because GTK 1.1.X forbids that. */ |
| 86 | |
| 87 | GtkOnSize(); |
| 88 | } |
| 89 | |
| 90 | bool ret = wxWindow::Show( show ); |
| 91 | |
| 92 | if (show) InitDialog(); |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | bool wxDialog::IsModal() const |
| 98 | { |
| 99 | return m_modalShowing; |
| 100 | } |
| 101 | |
| 102 | void wxDialog::SetModal( bool WXUNUSED(flag) ) |
| 103 | { |
| 104 | wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") ); |
| 105 | } |
| 106 | |
| 107 | int wxDialog::ShowModal() |
| 108 | { |
| 109 | if (IsModal()) |
| 110 | { |
| 111 | wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") ); |
| 112 | return GetReturnCode(); |
| 113 | } |
| 114 | |
| 115 | // use the apps top level window as parent if none given unless explicitly |
| 116 | // forbidden |
| 117 | if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) ) |
| 118 | { |
| 119 | wxWindow * const parent = GetParentForModalDialog(); |
| 120 | if ( parent && parent != this ) |
| 121 | { |
| 122 | gtk_window_set_transient_for( GTK_WINDOW(m_widget), |
| 123 | GTK_WINDOW(parent->m_widget) ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | wxBusyCursorSuspender cs; // temporarily suppress the busy cursor |
| 128 | |
| 129 | Show( true ); |
| 130 | |
| 131 | m_modalShowing = true; |
| 132 | |
| 133 | g_openDialogs++; |
| 134 | |
| 135 | // NOTE: gtk_window_set_modal internally calls gtk_grab_add() ! |
| 136 | gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE); |
| 137 | |
| 138 | wxGUIEventLoop().Run(); |
| 139 | |
| 140 | gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE); |
| 141 | |
| 142 | g_openDialogs--; |
| 143 | |
| 144 | return GetReturnCode(); |
| 145 | } |
| 146 | |
| 147 | void wxDialog::EndModal( int retCode ) |
| 148 | { |
| 149 | SetReturnCode( retCode ); |
| 150 | |
| 151 | if (!IsModal()) |
| 152 | { |
| 153 | wxFAIL_MSG( wxT("wxDialog:EndModal called twice") ); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | m_modalShowing = false; |
| 158 | |
| 159 | gtk_main_quit(); |
| 160 | |
| 161 | Show( false ); |
| 162 | } |