1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/popupwin.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #if wxUSE_POPUPWIN
13 
14 #include "wx/popupwin.h"
15 
16 #ifndef WX_PRECOMP
17 #endif // WX_PRECOMP
18 
19 #include "wx/gtk/private/wrapgtk.h"
20 
21 #include "wx/gtk/private/win_gtk.h"
22 
23 //-----------------------------------------------------------------------------
24 // "button_press"
25 //-----------------------------------------------------------------------------
26 
27 extern "C" {
gtk_popup_button_press(GtkWidget * widget,GdkEvent * gdk_event,wxPopupWindow * win)28 static gint gtk_popup_button_press (GtkWidget *widget, GdkEvent *gdk_event, wxPopupWindow* win )
29 {
30     GtkWidget *child = gtk_get_event_widget (gdk_event);
31 
32     /* Ignore events sent out before we connected to the signal */
33     if (win->m_time >= ((GdkEventButton*)gdk_event)->time)
34         return FALSE;
35 
36     /*  We don't ask for button press events on the grab widget, so
37      *  if an event is reported directly to the grab widget, it must
38      *  be on a window outside the application (and thus we remove
39      *  the popup window). Otherwise, we check if the widget is a child
40      *  of the grab widget, and only remove the popup window if it
41      *  is not. */
42     if (child != widget)
43     {
44         while (child)
45         {
46             if (child == widget)
47                 return FALSE;
48             child = gtk_widget_get_parent(child);
49         }
50     }
51 
52     wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
53     event.SetEventObject( win );
54 
55     (void)win->HandleWindowEvent( event );
56 
57     return TRUE;
58 }
59 }
60 
61 //-----------------------------------------------------------------------------
62 // "delete_event"
63 //-----------------------------------------------------------------------------
64 
65 extern "C" {
66 static
gtk_dialog_delete_callback(GtkWidget * WXUNUSED (widget),GdkEvent * WXUNUSED (event),wxPopupWindow * win)67 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxPopupWindow *win )
68 {
69     if (win->IsEnabled())
70         win->Close();
71 
72     return TRUE;
73 }
74 }
75 
76 //-----------------------------------------------------------------------------
77 // wxPopupWindow
78 //-----------------------------------------------------------------------------
79 
80 #ifdef __WXUNIVERSAL__
wxBEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase)81 wxBEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase)
82     EVT_SIZE(wxPopupWindow::OnSize)
83 wxEND_EVENT_TABLE()
84 #endif
85 
86 wxPopupWindow::~wxPopupWindow()
87 {
88 }
89 
Create(wxWindow * parent,int style)90 bool wxPopupWindow::Create( wxWindow *parent, int style )
91 {
92     if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
93         !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("popup") ))
94     {
95         wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
96         return false;
97     }
98 
99     // Unlike windows, top level windows are created hidden by default.
100     m_isShown = false;
101 
102     // All dialogs should really have this style
103     m_windowStyle |= wxTAB_TRAVERSAL;
104 
105     m_widget = gtk_window_new( GTK_WINDOW_POPUP );
106     g_object_ref( m_widget );
107 
108     gtk_widget_set_name( m_widget, "wxPopupWindow" );
109     // wxPopupWindow is used for different windows as well
110     // gtk_window_set_type_hint( GTK_WINDOW(m_widget), GDK_WINDOW_TYPE_HINT_COMBO );
111 
112     // Popup windows can be created without parent, so handle this correctly.
113     if (parent)
114     {
115         GtkWidget *toplevel = gtk_widget_get_toplevel( parent->m_widget );
116         if (GTK_IS_WINDOW (toplevel))
117             gtk_window_set_transient_for (GTK_WINDOW (m_widget), GTK_WINDOW (toplevel));
118     }
119 
120     gtk_window_set_resizable (GTK_WINDOW (m_widget), FALSE);
121 
122     g_signal_connect (m_widget, "delete_event",
123                       G_CALLBACK (gtk_dialog_delete_callback), this);
124 
125     m_wxwindow = wxPizza::New();
126     gtk_widget_show( m_wxwindow );
127 
128     gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
129 
130     if (m_parent) m_parent->AddChild( this );
131 
132     PostCreation();
133 
134     m_time = gtk_get_current_event_time();
135 
136     g_signal_connect (m_widget, "button_press_event",
137                       G_CALLBACK (gtk_popup_button_press), this);
138 
139     return true;
140 }
141 
DoMoveWindow(int WXUNUSED (x),int WXUNUSED (y),int WXUNUSED (width),int WXUNUSED (height))142 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
143 {
144     wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
145 }
146 
DoSetSize(int x,int y,int width,int height,int sizeFlags)147 void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags )
148 {
149     wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
150     wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
151 
152     int old_x = m_x;
153     int old_y = m_y;
154 
155     int old_width = m_width;
156     int old_height = m_height;
157 
158     if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
159         m_x = x;
160     if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
161         m_y = y;
162     if (width != -1)
163         m_width = width;
164     if (height != -1)
165         m_height = height;
166 
167     ConstrainSize();
168 
169     if (m_x != old_x || m_y != old_y)
170     {
171         gtk_window_move(GTK_WINDOW(m_widget), m_x, m_y);
172         wxMoveEvent event(wxPoint(m_x, m_y), GetId());
173         event.SetEventObject(this);
174         HandleWindowEvent(event);
175     }
176 
177     if ((m_width != old_width) || (m_height != old_height))
178     {
179         // gtk_window_resize does not work for GTK_WINDOW_POPUP
180         gtk_widget_set_size_request( m_widget, m_width, m_height );
181         wxSizeEvent event(GetSize(), GetId());
182         event.SetEventObject(this);
183         HandleWindowEvent(event);
184     }
185 }
186 
SetFocus()187 void wxPopupWindow::SetFocus()
188 {
189     // set the focus to the first child who wants it
190     wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
191     while ( node )
192     {
193         wxWindow *child = node->GetData();
194         node = node->GetNext();
195 
196         if ( child->CanAcceptFocus() && !child->IsTopLevel() )
197         {
198             child->SetFocus();
199             return;
200         }
201     }
202 
203     wxPopupWindowBase::SetFocus();
204 }
205 
Show(bool show)206 bool wxPopupWindow::Show( bool show )
207 {
208     if (show && !IsShown())
209     {
210         wxSizeEvent event(GetSize(), GetId());
211         event.SetEventObject(this);
212         HandleWindowEvent(event);
213     }
214 
215     bool ret = wxWindow::Show( show );
216 
217     return ret;
218 }
219 
220 #endif // wxUSE_POPUPWIN
221