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