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