GDK_ROOT_PARENT -> gdk_get_default_root_window
[wxWidgets.git] / src / gtk / minifram.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/minifram.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 #include "wx/minifram.h"
14
15 #if wxUSE_MINIFRAME
16
17 #include "wx/dcscreen.h"
18
19 #include "gtk/gtk.h"
20 #include "wx/gtk/win_gtk.h"
21 #include "wx/gtk/private.h"
22
23 #include <gdk/gdk.h>
24 #include <gdk/gdkprivate.h>
25 #include <gdk/gdkx.h>
26
27 //-----------------------------------------------------------------------------
28 // data
29 //-----------------------------------------------------------------------------
30
31 extern bool g_blockEventsOnDrag;
32 extern bool g_blockEventsOnScroll;
33 extern GtkWidget *wxGetRootWindow();
34
35 //-----------------------------------------------------------------------------
36 // local functions
37 //-----------------------------------------------------------------------------
38
39 /* draw XOR rectangle when moving mine frame around */
40
41 static void DrawFrame( GtkWidget *widget, int x, int y, int w, int h )
42 {
43 int org_x = 0;
44 int org_y = 0;
45 gdk_window_get_origin( widget->window, &org_x, &org_y );
46 x += org_x;
47 y += org_y;
48
49 GdkGC *gc = gdk_gc_new( gdk_get_default_root_window() );
50 gdk_gc_set_subwindow( gc, GDK_INCLUDE_INFERIORS );
51 gdk_gc_set_function( gc, GDK_INVERT );
52
53 gdk_draw_rectangle( gdk_get_default_root_window(), gc, FALSE, x, y, w, h );
54 g_object_unref (G_OBJECT (gc));
55 }
56
57 //-----------------------------------------------------------------------------
58 // "expose_event" of m_mainWidget
59 //-----------------------------------------------------------------------------
60
61 extern "C" {
62 static void gtk_window_own_expose_callback( GtkWidget *widget, GdkEventExpose *gdk_event, wxFrame *win )
63 {
64 if (g_isIdle) wxapp_install_idle_handler();
65
66 if (!win->m_hasVMT) return;
67 if (gdk_event->count > 0) return;
68
69 GtkPizza *pizza = GTK_PIZZA(widget);
70
71 gtk_paint_shadow (widget->style,
72 pizza->bin_window,
73 GTK_STATE_NORMAL,
74 GTK_SHADOW_OUT,
75 NULL, NULL, NULL, // FIXME: No clipping?
76 0, 0,
77 win->m_width, win->m_height);
78
79 if (!win->GetTitle().empty() &&
80 ((win->GetWindowStyle() & wxCAPTION) ||
81 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
82 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
83 {
84 wxClientDC dc(win);
85 dc.SetFont( *wxSMALL_FONT );
86 int height = dc.GetCharHeight();
87
88 GdkGC *gc = gdk_gc_new( pizza->bin_window );
89 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
90 gdk_draw_rectangle( pizza->bin_window, gc, TRUE,
91 3,
92 3,
93 win->m_width - 7,
94 height+1 );
95 g_object_unref (G_OBJECT (gc));
96
97 // Hack alert
98 dc.m_window = pizza->bin_window;
99 dc.SetTextForeground( *wxWHITE );
100 dc.DrawText( win->GetTitle(), 6, 3 );
101 }
102 }
103 }
104
105 //-----------------------------------------------------------------------------
106 // "button_press_event" of m_mainWidget
107 //-----------------------------------------------------------------------------
108
109 extern "C" {
110 static gint gtk_window_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
111 {
112 if (g_isIdle) wxapp_install_idle_handler();
113
114 if (!win->m_hasVMT) return FALSE;
115 if (g_blockEventsOnDrag) return TRUE;
116 if (g_blockEventsOnScroll) return TRUE;
117
118 if (win->m_isDragging) return TRUE;
119
120 GtkPizza *pizza = GTK_PIZZA(widget);
121 if (gdk_event->window != pizza->bin_window) return TRUE;
122
123 wxClientDC dc(win);
124 dc.SetFont( *wxSMALL_FONT );
125 int height = dc.GetCharHeight() + 1;
126
127 if (gdk_event->y > height) return TRUE;
128
129 gdk_window_raise( win->m_widget->window );
130
131 gdk_pointer_grab( widget->window, FALSE,
132 (GdkEventMask)
133 (GDK_BUTTON_PRESS_MASK |
134 GDK_BUTTON_RELEASE_MASK |
135 GDK_POINTER_MOTION_MASK |
136 GDK_POINTER_MOTION_HINT_MASK |
137 GDK_BUTTON_MOTION_MASK |
138 GDK_BUTTON1_MOTION_MASK),
139 (GdkWindow *) NULL,
140 (GdkCursor *) NULL,
141 (unsigned int) GDK_CURRENT_TIME );
142
143 win->m_diffX = (int)gdk_event->x;
144 win->m_diffY = (int)gdk_event->y;
145 DrawFrame( widget, 0, 0, win->m_width, win->m_height );
146 win->m_oldX = 0;
147 win->m_oldY = 0;
148
149 win->m_isDragging = true;
150
151 return TRUE;
152 }
153 }
154
155 //-----------------------------------------------------------------------------
156 // "button_release_event" of m_mainWidget
157 //-----------------------------------------------------------------------------
158
159 extern "C" {
160 static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
161 {
162 if (g_isIdle) wxapp_install_idle_handler();
163
164 if (!win->m_hasVMT) return FALSE;
165 if (g_blockEventsOnDrag) return TRUE;
166 if (g_blockEventsOnScroll) return TRUE;
167
168 if (!win->m_isDragging) return TRUE;
169
170 win->m_isDragging = FALSE;
171
172 int x = (int)gdk_event->x;
173 int y = (int)gdk_event->y;
174
175 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
176 gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
177 int org_x = 0;
178 int org_y = 0;
179 gdk_window_get_origin( widget->window, &org_x, &org_y );
180 x += org_x - win->m_diffX;
181 y += org_y - win->m_diffY;
182 win->m_x = x;
183 win->m_y = y;
184 gtk_window_move( GTK_WINDOW(win->m_widget), x, y );
185
186 return TRUE;
187 }
188 }
189
190 //-----------------------------------------------------------------------------
191 // "motion_notify_event" of m_mainWidget
192 //-----------------------------------------------------------------------------
193
194 extern "C" {
195 static gint gtk_window_motion_notify_callback( GtkWidget *widget, GdkEventMotion *gdk_event, wxMiniFrame *win )
196 {
197 if (g_isIdle) wxapp_install_idle_handler();
198
199 if (!win->m_hasVMT) return FALSE;
200 if (g_blockEventsOnDrag) return TRUE;
201 if (g_blockEventsOnScroll) return TRUE;
202
203 if (!win->m_isDragging) return TRUE;
204
205 if (gdk_event->is_hint)
206 {
207 int x = 0;
208 int y = 0;
209 GdkModifierType state;
210 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
211 gdk_event->x = x;
212 gdk_event->y = y;
213 gdk_event->state = state;
214 }
215
216 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
217 win->m_oldX = (int)gdk_event->x - win->m_diffX;
218 win->m_oldY = (int)gdk_event->y - win->m_diffY;
219 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
220
221 return TRUE;
222 }
223 }
224
225 //-----------------------------------------------------------------------------
226 // "clicked" of X system button
227 //-----------------------------------------------------------------------------
228
229 extern "C" {
230 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxMiniFrame *mf )
231 {
232 if (g_isIdle) wxapp_install_idle_handler();
233
234 mf->Close();
235 }
236 }
237
238 //-----------------------------------------------------------------------------
239 // wxMiniFrame
240 //-----------------------------------------------------------------------------
241
242 static const char *cross_xpm[] = {
243 /* columns rows colors chars-per-pixel */
244 "5 5 2 1",
245 "# c Gray0",
246 " c None",
247 /* pixels */
248 "# #",
249 " # # ",
250 " # ",
251 " # # ",
252 "# #",
253 };
254
255 IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame)
256
257 bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
258 const wxPoint &pos, const wxSize &size,
259 long style, const wxString &name )
260 {
261 style = style | wxCAPTION;
262
263 if ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT))
264 m_miniTitle = 13;
265
266 m_miniEdge = 3;
267 m_isDragging = false;
268 m_oldX = -1;
269 m_oldY = -1;
270 m_diffX = 0;
271 m_diffY = 0;
272
273 wxFrame::Create( parent, id, title, pos, size, style, name );
274
275 if (m_parent && (GTK_IS_WINDOW(m_parent->m_widget)))
276 {
277 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
278 }
279
280 if ((style & wxSYSTEM_MENU) &&
281 ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT)))
282 {
283 GdkBitmap *mask = (GdkBitmap*) NULL;
284 GdkPixmap *pixmap = gdk_pixmap_create_from_xpm_d
285 (
286 wxGetRootWindow()->window,
287 &mask,
288 NULL,
289 (char **)cross_xpm
290 );
291
292 GtkWidget *pw = gtk_pixmap_new( pixmap, mask );
293 g_object_unref (G_OBJECT (mask));
294 g_object_unref (G_OBJECT (pixmap));
295 gtk_widget_show( pw );
296
297 GtkWidget *close_button = gtk_button_new();
298 #ifdef __WXGTK24__
299 if (!gtk_check_version(2,4,0))
300 gtk_button_set_focus_on_click( GTK_BUTTON(close_button), FALSE );
301 #endif
302 gtk_container_add( GTK_CONTAINER(close_button), pw );
303
304 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
305 close_button,
306 size.x-16, 4, 11, 11 );
307
308 gtk_widget_show( close_button );
309
310 g_signal_connect (close_button, "clicked",
311 G_CALLBACK (gtk_button_clicked_callback),
312 this);
313 }
314
315 /* these are called when the borders are drawn */
316 g_signal_connect (m_mainWidget, "expose_event",
317 G_CALLBACK (gtk_window_own_expose_callback), this );
318
319 /* these are required for dragging the mini frame around */
320 g_signal_connect (m_mainWidget, "button_press_event",
321 G_CALLBACK (gtk_window_button_press_callback), this);
322 g_signal_connect (m_mainWidget, "button_release_event",
323 G_CALLBACK (gtk_window_button_release_callback), this);
324 g_signal_connect (m_mainWidget, "motion_notify_event",
325 G_CALLBACK (gtk_window_motion_notify_callback), this);
326
327 return true;
328 }
329
330 void wxMiniFrame::SetTitle( const wxString &title )
331 {
332 wxFrame::SetTitle( title );
333
334 gdk_window_invalidate_rect( GTK_PIZZA(m_mainWidget)->bin_window, NULL, true );
335 }
336
337 #endif