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