]>
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/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->GetEventHandler()->ProcessEvent( 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 | // "realize" from m_widget | |
78 | //----------------------------------------------------------------------------- | |
79 | ||
80 | /* we cannot MWM hints and icons before the widget has been realized, | |
81 | so we do this directly after realization */ | |
82 | ||
83 | extern "C" { | |
84 | static gint | |
85 | gtk_dialog_realized_callback( GtkWidget * WXUNUSED(widget), wxPopupWindow *win ) | |
86 | { | |
87 | /* all this is for Motif Window Manager "hints" and is supposed to be | |
88 | recognized by other WM as well. not tested. */ | |
89 | long decor = (long) GDK_DECOR_BORDER; | |
90 | long func = (long) GDK_FUNC_MOVE ; | |
91 | ||
92 | gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor); | |
93 | gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func); | |
94 | ||
95 | gtk_window_set_resizable(GTK_WINDOW(win->m_widget), FALSE); | |
96 | ||
97 | return FALSE; | |
98 | } | |
99 | } | |
100 | ||
101 | //----------------------------------------------------------------------------- | |
102 | // InsertChild for wxPopupWindow | |
103 | //----------------------------------------------------------------------------- | |
104 | ||
105 | /* Callback for wxFrame. This very strange beast has to be used because | |
106 | * C++ has no virtual methods in a constructor. We have to emulate a | |
107 | * virtual function here as wxWidgets requires different ways to insert | |
108 | * a child in container classes. */ | |
109 | ||
110 | static void wxInsertChildInPopupWin(wxWindowGTK* parent, wxWindowGTK* child) | |
111 | { | |
112 | gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow), | |
113 | child->m_widget, | |
114 | child->m_x, | |
115 | child->m_y, | |
116 | child->m_width, | |
117 | child->m_height ); | |
118 | ||
119 | if (parent->HasFlag(wxTAB_TRAVERSAL)) | |
120 | { | |
121 | /* we now allow a window to get the focus as long as it | |
122 | doesn't have any children. */ | |
123 | GTK_WIDGET_UNSET_FLAGS( parent->m_wxwindow, GTK_CAN_FOCUS ); | |
124 | } | |
125 | } | |
126 | ||
127 | //----------------------------------------------------------------------------- | |
128 | // wxPopupWindow | |
129 | //----------------------------------------------------------------------------- | |
130 | ||
131 | #ifdef __WXUNIVERSAL__ | |
132 | BEGIN_EVENT_TABLE(wxPopupWindow,wxPopupWindowBase) | |
133 | EVT_SIZE(wxPopupWindow::OnSize) | |
134 | END_EVENT_TABLE() | |
135 | #endif | |
136 | ||
137 | wxPopupWindow::~wxPopupWindow() | |
138 | { | |
139 | } | |
140 | ||
141 | bool wxPopupWindow::Create( wxWindow *parent, int style ) | |
142 | { | |
143 | if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) || | |
144 | !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("popup") )) | |
145 | { | |
146 | wxFAIL_MSG( wxT("wxPopupWindow creation failed") ); | |
147 | return false; | |
148 | } | |
149 | ||
150 | // Unlike windows, top level windows are created hidden by default. | |
151 | m_isShown = false; | |
152 | ||
153 | // All dialogs should really have this style | |
154 | m_windowStyle |= wxTAB_TRAVERSAL; | |
155 | ||
156 | m_insertCallback = wxInsertChildInPopupWin; | |
157 | ||
158 | m_widget = gtk_window_new( GTK_WINDOW_POPUP ); | |
159 | ||
160 | if ((m_parent) && (GTK_IS_WINDOW(m_parent->m_widget))) | |
161 | gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) ); | |
162 | ||
163 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
164 | ||
165 | g_signal_connect (m_widget, "delete_event", | |
166 | G_CALLBACK (gtk_dialog_delete_callback), this); | |
167 | ||
168 | m_wxwindow = gtk_pizza_new(); | |
169 | gtk_widget_show( m_wxwindow ); | |
170 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); | |
171 | ||
172 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); | |
173 | ||
174 | if (m_parent) m_parent->AddChild( this ); | |
175 | ||
176 | PostCreation(); | |
177 | ||
178 | /* we cannot set MWM hints before the widget has | |
179 | been realized, so we do this directly after realization */ | |
180 | g_signal_connect (m_widget, "realize", | |
181 | G_CALLBACK (gtk_dialog_realized_callback), this); | |
182 | ||
183 | m_time = gtk_get_current_event_time(); | |
184 | ||
185 | g_signal_connect (m_widget, "button_press_event", | |
186 | G_CALLBACK (gtk_popup_button_press), this); | |
187 | ||
188 | return true; | |
189 | } | |
190 | ||
191 | void wxPopupWindow::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) ) | |
192 | { | |
193 | wxFAIL_MSG( wxT("DoMoveWindow called for wxPopupWindow") ); | |
194 | } | |
195 | ||
196 | void wxPopupWindow::DoSetSize( int x, int y, int width, int height, int sizeFlags ) | |
197 | { | |
198 | wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") ); | |
199 | wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") ); | |
200 | ||
201 | int old_x = m_x; | |
202 | int old_y = m_y; | |
203 | ||
204 | int old_width = m_width; | |
205 | int old_height = m_height; | |
206 | ||
207 | if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
208 | m_x = x; | |
209 | if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
210 | m_y = y; | |
211 | if (width != -1) | |
212 | m_width = width; | |
213 | if (height != -1) | |
214 | m_height = height; | |
215 | ||
216 | ConstrainSize(); | |
217 | ||
218 | if ((m_x != -1) || (m_y != -1)) | |
219 | { | |
220 | if ((m_x != old_x) || (m_y != old_y)) | |
221 | { | |
222 | gtk_window_move( GTK_WINDOW(m_widget), m_x, m_y ); | |
223 | } | |
224 | } | |
225 | ||
226 | if ((m_width != old_width) || (m_height != old_height)) | |
227 | { | |
228 | // gtk_window_resize does not work for GTK_WINDOW_POPUP | |
229 | gtk_widget_set_size_request( m_widget, m_width, m_height ); | |
230 | wxSizeEvent event(GetSize(), GetId()); | |
231 | event.SetEventObject(this); | |
232 | GetEventHandler()->ProcessEvent(event); | |
233 | } | |
234 | } | |
235 | ||
236 | bool wxPopupWindow::Show( bool show ) | |
237 | { | |
238 | if (show && !IsShown()) | |
239 | { | |
240 | wxSizeEvent event(GetSize(), GetId()); | |
241 | event.SetEventObject(this); | |
242 | GetEventHandler()->ProcessEvent(event); | |
243 | } | |
244 | ||
245 | bool ret = wxWindow::Show( show ); | |
246 | ||
247 | return ret; | |
248 | } | |
249 | ||
250 | #endif // wxUSE_POPUPWIN |