]>
Commit | Line | Data |
---|---|---|
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 = gtk_widget_get_parent(child); | |
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 | //----------------------------------------------------------------------------- | |
77 | // wxPopupWindow | |
78 | //----------------------------------------------------------------------------- | |
79 | ||
80 | #ifdef __WXUNIVERSAL__ | |
81 | BEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase) | |
82 | EVT_SIZE(wxPopupWindow::OnSize) | |
83 | END_EVENT_TABLE() | |
84 | #endif | |
85 | ||
86 | wxPopupWindow::~wxPopupWindow() | |
87 | { | |
88 | } | |
89 | ||
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 | GtkWidget *toplevel = gtk_widget_get_toplevel( parent->m_widget ); | |
113 | if (GTK_IS_WINDOW (toplevel)) | |
114 | { | |
115 | #if GTK_CHECK_VERSION(2,10,0) | |
116 | if (!gtk_check_version(2,10,0)) | |
117 | gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), GTK_WINDOW (m_widget)); | |
118 | #endif | |
119 | ||
120 | gtk_window_set_transient_for (GTK_WINDOW (m_widget), GTK_WINDOW (toplevel)); | |
121 | } | |
122 | gtk_window_set_resizable (GTK_WINDOW (m_widget), FALSE); | |
123 | gtk_window_set_screen (GTK_WINDOW (m_widget), gtk_widget_get_screen (GTK_WIDGET (parent->m_widget))); | |
124 | ||
125 | g_signal_connect (m_widget, "delete_event", | |
126 | G_CALLBACK (gtk_dialog_delete_callback), this); | |
127 | ||
128 | m_wxwindow = wxPizza::New(m_windowStyle); | |
129 | gtk_widget_show( m_wxwindow ); | |
130 | ||
131 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); | |
132 | ||
133 | if (m_parent) m_parent->AddChild( this ); | |
134 | ||
135 | PostCreation(); | |
136 | ||
137 | m_time = gtk_get_current_event_time(); | |
138 | ||
139 | g_signal_connect (m_widget, "button_press_event", | |
140 | G_CALLBACK (gtk_popup_button_press), this); | |
141 | ||
142 | return true; | |
143 | } | |
144 | ||
145 | void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) ) | |
146 | { | |
147 | wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") ); | |
148 | } | |
149 | ||
150 | void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags ) | |
151 | { | |
152 | wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") ); | |
153 | wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") ); | |
154 | ||
155 | int old_x = m_x; | |
156 | int old_y = m_y; | |
157 | ||
158 | int old_width = m_width; | |
159 | int old_height = m_height; | |
160 | ||
161 | if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
162 | m_x = x; | |
163 | if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
164 | m_y = y; | |
165 | if (width != -1) | |
166 | m_width = width; | |
167 | if (height != -1) | |
168 | m_height = height; | |
169 | ||
170 | ConstrainSize(); | |
171 | ||
172 | if ((m_x != -1) || (m_y != -1)) | |
173 | { | |
174 | if ((m_x != old_x) || (m_y != old_y)) | |
175 | { | |
176 | gtk_window_move( GTK_WINDOW(m_widget), m_x, m_y ); | |
177 | } | |
178 | } | |
179 | ||
180 | if ((m_width != old_width) || (m_height != old_height)) | |
181 | { | |
182 | // gtk_window_resize does not work for GTK_WINDOW_POPUP | |
183 | gtk_widget_set_size_request( m_widget, m_width, m_height ); | |
184 | wxSizeEvent event(GetSize(), GetId()); | |
185 | event.SetEventObject(this); | |
186 | HandleWindowEvent(event); | |
187 | } | |
188 | } | |
189 | ||
190 | void wxPopupWindow::SetFocus() | |
191 | { | |
192 | // set the focus to the first child who wants it | |
193 | wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); | |
194 | while ( node ) | |
195 | { | |
196 | wxWindow *child = node->GetData(); | |
197 | node = node->GetNext(); | |
198 | ||
199 | if ( child->CanAcceptFocus() && !child->IsTopLevel() ) | |
200 | { | |
201 | child->SetFocus(); | |
202 | return; | |
203 | } | |
204 | } | |
205 | ||
206 | wxPopupWindowBase::SetFocus(); | |
207 | } | |
208 | ||
209 | bool wxPopupWindow::Show( bool show ) | |
210 | { | |
211 | if (show && !IsShown()) | |
212 | { | |
213 | wxSizeEvent event(GetSize(), GetId()); | |
214 | event.SetEventObject(this); | |
215 | HandleWindowEvent(event); | |
216 | } | |
217 | ||
218 | bool ret = wxWindow::Show( show ); | |
219 | ||
220 | return ret; | |
221 | } | |
222 | ||
223 | #endif // wxUSE_POPUPWIN |