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 <gtk/gtk.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" {
gtk_dialog_delete_callback(GtkWidget * WXUNUSED (widget),GdkEvent * WXUNUSED (event),wxPopupWindow * win)66 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxPopupWindow *win )
67 {
68     if (win->IsEnabled())
69         win->Close();
70 
71     return TRUE;
72 }
73 }
74 
75 //-----------------------------------------------------------------------------
76 // wxPopupWindow
77 //-----------------------------------------------------------------------------
78 
79 #ifdef __WXUNIVERSAL__
BEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase)80 BEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase)
81     EVT_SIZE(wxPopupWindow::OnSize)
82 END_EVENT_TABLE()
83 #endif
84 
85 wxPopupWindow::~wxPopupWindow()
86 {
87 }
88 
Create(wxWindow * parent,int style)89 bool wxPopupWindow::Create( wxWindow *parent, int style )
90 {
91     if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
92         !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("popup") ))
93     {
94         wxFAIL_MSG( wxT("wxPopupWindow creation failed") );
95         return false;
96     }
97 
98     // Unlike windows, top level windows are created hidden by default.
99     m_isShown = false;
100 
101     // All dialogs should really have this style
102     m_windowStyle |= wxTAB_TRAVERSAL;
103 
104     m_widget = gtk_window_new( GTK_WINDOW_POPUP );
105     g_object_ref( m_widget );
106 
107     gtk_widget_set_name( m_widget, "wxPopupWindow" );
108     // wxPopupWindow is used for different windows as well
109     // gtk_window_set_type_hint( GTK_WINDOW(m_widget), GDK_WINDOW_TYPE_HINT_COMBO );
110 
111     // Popup windows can be created without parent, so handle this correctly.
112     if (parent)
113     {
114         GtkWidget *toplevel = gtk_widget_get_toplevel( parent->m_widget );
115         if (GTK_IS_WINDOW (toplevel))
116         {
117 #if GTK_CHECK_VERSION(2,10,0)
118 #ifndef __WXGTK3__
119             if (!gtk_check_version(2,10,0))
120 #endif
121             {
122                 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), GTK_WINDOW (m_widget));
123             }
124 #endif
125             gtk_window_set_transient_for (GTK_WINDOW (m_widget), GTK_WINDOW (toplevel));
126         }
127         gtk_window_set_screen (GTK_WINDOW (m_widget), gtk_widget_get_screen (GTK_WIDGET (parent->m_widget)));
128     }
129 
130     gtk_window_set_resizable (GTK_WINDOW (m_widget), FALSE);
131 
132     g_signal_connect (m_widget, "delete_event",
133                       G_CALLBACK (gtk_dialog_delete_callback), this);
134 
135     m_wxwindow = wxPizza::New();
136     gtk_widget_show( m_wxwindow );
137 
138     gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
139 
140     if (m_parent) m_parent->AddChild( this );
141 
142     PostCreation();
143 
144     m_time = gtk_get_current_event_time();
145 
146     g_signal_connect (m_widget, "button_press_event",
147                       G_CALLBACK (gtk_popup_button_press), this);
148 
149     return true;
150 }
151 
DoMoveWindow(int WXUNUSED (x),int WXUNUSED (y),int WXUNUSED (width),int WXUNUSED (height))152 void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
153 {
154     wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") );
155 }
156 
DoSetSize(int x,int y,int width,int height,int sizeFlags)157 void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags )
158 {
159     wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
160     wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
161 
162     int old_x = m_x;
163     int old_y = m_y;
164 
165     int old_width = m_width;
166     int old_height = m_height;
167 
168     if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
169         m_x = x;
170     if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
171         m_y = y;
172     if (width != -1)
173         m_width = width;
174     if (height != -1)
175         m_height = height;
176 
177     ConstrainSize();
178 
179     if ((m_x != -1) || (m_y != -1))
180     {
181         if ((m_x != old_x) || (m_y != old_y))
182         {
183             gtk_window_move( GTK_WINDOW(m_widget), m_x, m_y );
184         }
185     }
186 
187     if ((m_width != old_width) || (m_height != old_height))
188     {
189         // gtk_window_resize does not work for GTK_WINDOW_POPUP
190         gtk_widget_set_size_request( m_widget, m_width, m_height );
191         wxSizeEvent event(GetSize(), GetId());
192         event.SetEventObject(this);
193         HandleWindowEvent(event);
194     }
195 }
196 
SetFocus()197 void wxPopupWindow::SetFocus()
198 {
199     // set the focus to the first child who wants it
200     wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
201     while ( node )
202     {
203         wxWindow *child = node->GetData();
204         node = node->GetNext();
205 
206         if ( child->CanAcceptFocus() && !child->IsTopLevel() )
207         {
208             child->SetFocus();
209             return;
210         }
211     }
212 
213     wxPopupWindowBase::SetFocus();
214 }
215 
Show(bool show)216 bool wxPopupWindow::Show( bool show )
217 {
218     if (show && !IsShown())
219     {
220         wxSizeEvent event(GetSize(), GetId());
221         event.SetEventObject(this);
222         HandleWindowEvent(event);
223     }
224 
225     bool ret = wxWindow::Show( show );
226 
227     return ret;
228 }
229 
230 #endif // wxUSE_POPUPWIN
231