OSDN Git Service

Handling key and mouse events in listctrl is improved
[molby/Molby.git] / wxSources / modalwindow.cpp
1 /**********************************************************************************************
2  *
3  * Filename  : modalwindow.cpp
4  * Purpose   : Allow a modalwindow like wxDialog but allowing menus and such.
5  * Author    : John A. Mason
6  * Created   : 8/27/2008 07:54:12 AM
7  * Copyright : Released under wxWidgets original license.
8  *
9 **********************************************************************************************/
10
11 #include "modalwindow.h"
12
13 IMPLEMENT_DYNAMIC_CLASS(wxModalWindow, wxFrame)
14
15 wxModalWindow::wxModalWindow()
16 : wxFrame() {
17    Init();
18 }
19
20 wxModalWindow::wxModalWindow(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style, const wxString& name)
21 : wxFrame(parent, id, title, pos, size, style, name) {
22    Init();
23 }
24
25
26 wxModalWindow::~wxModalWindow() {
27    delete m_eventLoop;
28 }
29
30
31 void wxModalWindow::Init()
32 {
33     m_returnCode = 0;
34     m_windowDisabler = NULL;
35     m_eventLoop = NULL;
36     m_isShowingModal = false;
37 }
38
39 bool wxModalWindow::Show(bool show)
40 {
41     if ( !show )
42     {
43         // if we had disabled other app windows, reenable them back now because
44         // if they stay disabled Windows will activate another window (one
45         // which is enabled, anyhow) and we will lose activation
46         if ( m_windowDisabler )
47         {
48             delete m_windowDisabler;
49             m_windowDisabler = NULL;
50         }
51
52         if ( IsModal() )
53             EndModal(wxID_CANCEL);
54     }
55
56     bool ret = wxFrame::Show(show);
57
58    //I don't think we need this. Since it is a wxFrame that we are extending,
59    // we don't need wxEVT_INIT_DIALOG firing off - that's what InitDialog does...
60    // and this would only make sense if we have a wxDialog and validators
61 //    if ( show )
62         //InitDialog();
63
64     return ret;
65 }
66
67 bool wxModalWindow::IsModal() const {
68    return m_isShowingModal;
69 }
70
71 int wxModalWindow::ShowModal() {
72   if ( IsModal() )
73     {
74        wxFAIL_MSG( wxT("wxModalWindow:ShowModal called twice") );
75        return GetReturnCode();
76     }
77
78     // use the apps top level window as parent if none given unless explicitly
79     // forbidden
80     if ( !GetParent() )
81     {
82         wxWindow *parent = wxTheApp->GetTopWindow();
83         if ( parent && parent != this )
84         {
85             m_parent = parent;
86         }
87     }
88
89     Show(true);
90
91     m_isShowingModal = true;
92
93     wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
94
95 #if defined(__WXGTK__) || defined(__WXMGL__)
96     wxBusyCursorSuspender suspender;
97     // FIXME (FIXME_MGL) - make sure busy cursor disappears under MSW too
98 #endif
99
100 /*  20140919 Toshi Nagata  */
101 #if defined(__WXMAC__)
102         /*  --- OSX specific --- */
103         {
104                 extern void MacRunModalForWindow(void *w);  /*  Defined in modalwindow_osx.mm  */
105                 WXWindow w = this->GetWXWindow();
106                 MacRunModalForWindow(w);
107         }
108         
109 #else
110         /*  --- The original code ---  */
111
112         m_windowDisabler = new wxWindowDisabler(this);
113     if ( !m_eventLoop )
114         m_eventLoop = new wxEventLoop;
115
116     m_eventLoop->Run();
117
118 #endif
119 /*  End Toshi Nagata  */
120
121     return GetReturnCode();
122 }
123
124
125
126 void wxModalWindow::EndModal(int retCode) {
127
128         /*  20140919 Toshi Nagata  */
129
130 #if defined(__WXMAC__)
131         /*  --- OSX specific ---  */
132         {
133                 extern void MacStopModal(void);  /*  Defined in modalwindow_osx.m  */
134                 SetReturnCode(retCode);
135                 m_isShowingModal = false;
136                 MacStopModal();
137         }
138 #else
139     wxASSERT_MSG( m_eventLoop, _T("wxModalWindow is not modal") );
140
141     SetReturnCode(retCode);
142
143     if ( !IsModal() )
144     {
145         wxFAIL_MSG( wxT("wxModalWindow:EndModal called twice") );
146         return;
147     }
148
149     m_isShowingModal = false;
150
151         m_eventLoop->Exit();
152 #endif
153 /*  End Toshi Nagata  */
154         
155     Show(false);
156 }
157
158
159 void wxModalWindow::SetReturnCode(int retCode) {
160    m_returnCode=retCode;
161 }
162
163
164 int wxModalWindow::GetReturnCode() const {
165    return m_returnCode;
166 }
167