Corrected wxMiniFrame for GTK2 and removed its
[wxWidgets.git] / src / gtk1 / minifram.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minifram.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "minifram.h"
12 #endif
13
14 #include "wx/minifram.h"
15
16 #if wxUSE_MINIFRAME
17
18 #include "wx/dcscreen.h"
19
20 #include "gtk/gtk.h"
21 #include "wx/gtk/win_gtk.h"
22 #include "wx/gtk/private.h"
23
24 #include <gdk/gdk.h>
25 #include <gdk/gdkprivate.h>
26 #include <gdk/gdkx.h>
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40 extern bool g_blockEventsOnScroll;
41 extern GtkWidget *wxGetRootWindow();
42
43 //-----------------------------------------------------------------------------
44 // local functions
45 //-----------------------------------------------------------------------------
46
47 /* draw XOR rectangle when moving mine frame around */
48
49 static void DrawFrame( GtkWidget *widget, int x, int y, int w, int h )
50 {
51 int org_x = 0;
52 int org_y = 0;
53 gdk_window_get_origin( widget->window, &org_x, &org_y );
54 x += org_x;
55 y += org_y;
56
57 GdkGC *gc = gdk_gc_new( GDK_ROOT_PARENT() );
58 gdk_gc_set_subwindow( gc, GDK_INCLUDE_INFERIORS );
59 gdk_gc_set_function( gc, GDK_INVERT );
60
61 gdk_draw_rectangle( GDK_ROOT_PARENT(), gc, FALSE, x, y, w, h );
62 gdk_gc_unref( gc );
63 }
64
65 //-----------------------------------------------------------------------------
66 // "expose_event" of m_mainWidget
67 //-----------------------------------------------------------------------------
68
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_draw_shadow( widget->style,
79 pizza->bin_window,
80 GTK_STATE_NORMAL,
81 GTK_SHADOW_OUT,
82 0, 0,
83 win->m_width, win->m_height );
84
85 if (!win->GetTitle().IsEmpty() &&
86 ((win->GetWindowStyle() & wxCAPTION) ||
87 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
88 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
89 {
90 wxClientDC dc(win);
91 dc.SetFont( *wxSMALL_FONT );
92 int height = dc.GetCharHeight();
93
94 GdkGC *gc = gdk_gc_new( pizza->bin_window );
95 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
96 gdk_draw_rectangle( pizza->bin_window, gc, TRUE,
97 3,
98 3,
99 win->m_width - 7,
100 height+1 );
101 gdk_gc_unref( gc );
102
103 // Hack alert
104 dc.m_window = pizza->bin_window;
105 dc.SetTextForeground( *wxWHITE );
106 dc.DrawText( win->GetTitle(), 6, 3 );
107 }
108 }
109
110 //-----------------------------------------------------------------------------
111 // "draw" of m_mainWidget
112 //-----------------------------------------------------------------------------
113
114 #ifndef __WXGTK20__
115 static void gtk_window_own_draw_callback( GtkWidget *widget, GdkRectangle *WXUNUSED(rect), wxFrame *win )
116 {
117 if (g_isIdle) wxapp_install_idle_handler();
118
119 if (!win->m_hasVMT) return;
120
121 GtkPizza *pizza = GTK_PIZZA(widget);
122
123 gtk_draw_shadow( widget->style,
124 pizza->bin_window,
125 GTK_STATE_NORMAL,
126 GTK_SHADOW_OUT,
127 0, 0,
128 win->m_width, win->m_height );
129
130 if (!win->m_title.IsEmpty() &&
131 ((win->GetWindowStyle() & wxCAPTION) ||
132 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
133 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
134 {
135 wxClientDC dc(win);
136 dc.SetFont( *wxSMALL_FONT );
137 int height = dc.GetCharHeight();
138
139 GdkGC *gc = gdk_gc_new( pizza->bin_window );
140 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
141 gdk_draw_rectangle( pizza->bin_window, gc, TRUE,
142 3,
143 3,
144 win->m_width - 7,
145 height+1 );
146 gdk_gc_unref( gc );
147
148 // Hack alert
149 dc.m_window = pizza->bin_window;
150 dc.SetTextForeground( *wxWHITE );
151 dc.DrawText( win->GetTitle(), 6, 3 );
152 }
153 }
154 #endif
155
156 //-----------------------------------------------------------------------------
157 // "button_press_event" of m_mainWidget
158 //-----------------------------------------------------------------------------
159
160 static gint gtk_window_button_press_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 GtkPizza *pizza = GTK_PIZZA(widget);
171 if (gdk_event->window != pizza->bin_window) return TRUE;
172
173 wxClientDC dc(win);
174 dc.SetFont( *wxSMALL_FONT );
175 int height = dc.GetCharHeight() + 1;
176
177 if (gdk_event->y > height) return TRUE;
178
179 gdk_window_raise( win->m_widget->window );
180
181 gdk_pointer_grab( widget->window, FALSE,
182 (GdkEventMask)
183 (GDK_BUTTON_PRESS_MASK |
184 GDK_BUTTON_RELEASE_MASK |
185 GDK_POINTER_MOTION_MASK |
186 GDK_POINTER_MOTION_HINT_MASK |
187 GDK_BUTTON_MOTION_MASK |
188 GDK_BUTTON1_MOTION_MASK),
189 (GdkWindow *) NULL,
190 (GdkCursor *) NULL,
191 (unsigned int) GDK_CURRENT_TIME );
192
193 win->m_diffX = (int)gdk_event->x;
194 win->m_diffY = (int)gdk_event->y;
195 DrawFrame( widget, 0, 0, win->m_width, win->m_height );
196 win->m_oldX = 0;
197 win->m_oldY = 0;
198
199 win->m_isDragging = TRUE;
200
201 return TRUE;
202 }
203
204 //-----------------------------------------------------------------------------
205 // "button_release_event" of m_mainWidget
206 //-----------------------------------------------------------------------------
207
208 static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
209 {
210 if (g_isIdle) wxapp_install_idle_handler();
211
212 if (!win->m_hasVMT) return FALSE;
213 if (g_blockEventsOnDrag) return TRUE;
214 if (g_blockEventsOnScroll) return TRUE;
215
216 if (!win->m_isDragging) return TRUE;
217
218 win->m_isDragging = FALSE;
219
220 int x = (int)gdk_event->x;
221 int y = (int)gdk_event->y;
222
223 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
224 gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
225 int org_x = 0;
226 int org_y = 0;
227 gdk_window_get_origin( widget->window, &org_x, &org_y );
228 x += org_x - win->m_diffX;
229 y += org_y - win->m_diffY;
230 win->m_x = x;
231 win->m_y = y;
232 gtk_widget_set_uposition( win->m_widget, x, y );
233
234 return TRUE;
235 }
236
237 //-----------------------------------------------------------------------------
238 // "motion_notify_event" of m_mainWidget
239 //-----------------------------------------------------------------------------
240
241 static gint gtk_window_motion_notify_callback( GtkWidget *widget, GdkEventMotion *gdk_event, wxMiniFrame *win )
242 {
243 if (g_isIdle) wxapp_install_idle_handler();
244
245 if (!win->m_hasVMT) return FALSE;
246 if (g_blockEventsOnDrag) return TRUE;
247 if (g_blockEventsOnScroll) return TRUE;
248
249 if (!win->m_isDragging) return TRUE;
250
251 if (gdk_event->is_hint)
252 {
253 int x = 0;
254 int y = 0;
255 GdkModifierType state;
256 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
257 gdk_event->x = x;
258 gdk_event->y = y;
259 gdk_event->state = state;
260 }
261
262 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
263 win->m_oldX = (int)gdk_event->x - win->m_diffX;
264 win->m_oldY = (int)gdk_event->y - win->m_diffY;
265 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
266
267 return TRUE;
268 }
269
270 //-----------------------------------------------------------------------------
271 // "clicked" of X system button
272 //-----------------------------------------------------------------------------
273
274 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxMiniFrame *mf )
275 {
276 if (g_isIdle) wxapp_install_idle_handler();
277
278 mf->Close();
279 }
280
281 //-----------------------------------------------------------------------------
282 // wxMiniFrame
283 //-----------------------------------------------------------------------------
284
285 static const char *cross_xpm[] = {
286 /* columns rows colors chars-per-pixel */
287 "5 5 16 1",
288 " c Gray0",
289 ". c #bf0000",
290 "X c #00bf00",
291 "o c #bfbf00",
292 "O c #0000bf",
293 "+ c #bf00bf",
294 "@ c #00bfbf",
295 "# c None",
296 "$ c #808080",
297 "% c Red",
298 "& c Green",
299 "* c Yellow",
300 "= c Blue",
301 "- c Magenta",
302 "; c Cyan",
303 ": c Gray100",
304 /* pixels */
305 " ### ",
306 "# # #",
307 "## ##",
308 "# # #",
309 " ### ",
310 };
311
312 IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame)
313
314 bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
315 const wxPoint &pos, const wxSize &size,
316 long style, const wxString &name )
317 {
318 style = style | wxCAPTION;
319
320 if ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT))
321 m_miniTitle = 13;
322
323 m_miniEdge = 3;
324 m_isDragging = FALSE;
325 m_oldX = -1;
326 m_oldY = -1;
327 m_diffX = 0;
328 m_diffY = 0;
329
330 wxFrame::Create( parent, id, title, pos, size, style, name );
331
332 if (m_parent && (GTK_IS_WINDOW(m_parent->m_widget)))
333 {
334 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
335 }
336
337 if ((style & wxSYSTEM_MENU) &&
338 ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT)))
339 {
340 GdkBitmap *mask = (GdkBitmap*) NULL;
341 GdkPixmap *pixmap = gdk_pixmap_create_from_xpm_d
342 (
343 wxGetRootWindow()->window,
344 &mask,
345 NULL,
346 (char **)cross_xpm
347 );
348
349 GtkWidget *pw = gtk_pixmap_new( pixmap, mask );
350 gdk_bitmap_unref( mask );
351 gdk_pixmap_unref( pixmap );
352 gtk_widget_show( pw );
353
354 GtkWidget *close_button = gtk_button_new();
355 gtk_container_add( GTK_CONTAINER(close_button), pw );
356
357 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
358 close_button,
359 size.x-16, 4, 11, 11 );
360
361 gtk_widget_show( close_button );
362
363 gtk_signal_connect( GTK_OBJECT(close_button), "clicked",
364 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
365 }
366
367 /* these are called when the borders are drawn */
368 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "expose_event",
369 GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this );
370
371 #ifndef __WXGTK20__
372 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "draw",
373 GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this );
374 #endif
375
376 /* these are required for dragging the mini frame around */
377 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_press_event",
378 GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this );
379
380 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_release_event",
381 GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this );
382
383 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "motion_notify_event",
384 GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this );
385
386 return TRUE;
387 }
388
389 #endif