]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/minifram.cpp
fixed resource leak in CreateIconFromBitmap() (Dimitri)
[wxWidgets.git] / src / gtk / 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
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 static void gtk_window_own_expose_callback( GtkWidget *widget, GdkEventExpose *gdk_event, wxFrame *win )
69 {
70 if (g_isIdle) wxapp_install_idle_handler();
71
72 if (!win->m_hasVMT) return;
73 if (gdk_event->count > 0) return;
74
75 GtkPizza *pizza = GTK_PIZZA(widget);
76
77 gtk_draw_shadow( widget->style,
78 pizza->bin_window,
79 GTK_STATE_NORMAL,
80 GTK_SHADOW_OUT,
81 0, 0,
82 win->m_width, win->m_height );
83
84 if (!win->m_title.IsEmpty() &&
85 ((win->GetWindowStyle() & wxCAPTION) ||
86 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
87 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
88 {
89 GdkGC *gc = gdk_gc_new( pizza->bin_window );
90 GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0);
91
92 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
93 gdk_draw_rectangle( pizza->bin_window, gc, TRUE,
94 3,
95 3,
96 win->m_width - 7,
97 font->ascent + font->descent+1 );
98
99 gdk_gc_set_foreground( gc, &widget->style->fg[GTK_STATE_SELECTED] );
100 gdk_draw_string( pizza->bin_window, font, gc,
101 6,
102 3+font->ascent,
103 win->m_title.mb_str() );
104
105 gdk_gc_unref( gc );
106 }
107 }
108
109 //-----------------------------------------------------------------------------
110 // "draw" of m_mainWidget
111 //-----------------------------------------------------------------------------
112
113 static void gtk_window_own_draw_callback( GtkWidget *widget, GdkRectangle *WXUNUSED(rect), wxFrame *win )
114 {
115 if (g_isIdle) wxapp_install_idle_handler();
116
117 if (!win->m_hasVMT) return;
118
119 GtkPizza *pizza = GTK_PIZZA(widget);
120
121 gtk_draw_shadow( widget->style,
122 pizza->bin_window,
123 GTK_STATE_NORMAL,
124 GTK_SHADOW_OUT,
125 0, 0,
126 win->m_width, win->m_height );
127
128 if (!win->m_title.IsEmpty() &&
129 ((win->GetWindowStyle() & wxCAPTION) ||
130 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
131 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
132 {
133 GdkGC *gc = gdk_gc_new( pizza->bin_window );
134 GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0);
135
136 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
137 gdk_draw_rectangle( pizza->bin_window, gc, TRUE,
138 3,
139 3,
140 win->m_width - 7,
141 font->ascent + font->descent+1 );
142
143 gdk_gc_set_foreground( gc, &widget->style->fg[GTK_STATE_SELECTED] );
144 gdk_draw_string( pizza->bin_window, font, gc,
145 6,
146 3+font->ascent,
147 win->m_title.mb_str() );
148
149 gdk_gc_unref( gc );
150 }
151 }
152
153 //-----------------------------------------------------------------------------
154 // "button_press_event" of m_mainWidget
155 //-----------------------------------------------------------------------------
156
157 static gint gtk_window_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
158 {
159 if (g_isIdle) wxapp_install_idle_handler();
160
161 if (!win->m_hasVMT) return FALSE;
162 if (g_blockEventsOnDrag) return TRUE;
163 if (g_blockEventsOnScroll) return TRUE;
164
165 if (win->m_isDragging) return TRUE;
166
167 gdk_window_raise( win->m_widget->window );
168
169 gdk_pointer_grab( widget->window, FALSE,
170 (GdkEventMask)
171 (GDK_BUTTON_PRESS_MASK |
172 GDK_BUTTON_RELEASE_MASK |
173 GDK_POINTER_MOTION_MASK |
174 GDK_POINTER_MOTION_HINT_MASK |
175 GDK_BUTTON_MOTION_MASK |
176 GDK_BUTTON1_MOTION_MASK),
177 (GdkWindow *) NULL,
178 (GdkCursor *) NULL,
179 (unsigned int) GDK_CURRENT_TIME );
180
181 win->m_diffX = (int)gdk_event->x;
182 win->m_diffY = (int)gdk_event->y;
183 DrawFrame( widget, 0, 0, win->m_width, win->m_height );
184 win->m_oldX = 0;
185 win->m_oldY = 0;
186
187 win->m_isDragging = TRUE;
188
189 return TRUE;
190 }
191
192 //-----------------------------------------------------------------------------
193 // "button_release_event" of m_mainWidget
194 //-----------------------------------------------------------------------------
195
196 static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
197 {
198 if (g_isIdle) wxapp_install_idle_handler();
199
200 if (!win->m_hasVMT) return FALSE;
201 if (g_blockEventsOnDrag) return TRUE;
202 if (g_blockEventsOnScroll) return TRUE;
203
204 if (!win->m_isDragging) return TRUE;
205
206 win->m_isDragging = FALSE;
207
208 int x = (int)gdk_event->x;
209 int y = (int)gdk_event->y;
210
211 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
212 gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
213 int org_x = 0;
214 int org_y = 0;
215 gdk_window_get_origin( widget->window, &org_x, &org_y );
216 x += org_x - win->m_diffX;
217 y += org_y - win->m_diffY;
218 win->m_x = x;
219 win->m_y = y;
220 gtk_widget_set_uposition( win->m_widget, x, y );
221
222 return TRUE;
223 }
224
225 //-----------------------------------------------------------------------------
226 // "motion_notify_event" of m_mainWidget
227 //-----------------------------------------------------------------------------
228
229 static gint gtk_window_motion_notify_callback( GtkWidget *widget, GdkEventMotion *gdk_event, wxMiniFrame *win )
230 {
231 if (g_isIdle) wxapp_install_idle_handler();
232
233 if (!win->m_hasVMT) return FALSE;
234 if (g_blockEventsOnDrag) return TRUE;
235 if (g_blockEventsOnScroll) return TRUE;
236
237 if (!win->m_isDragging) return TRUE;
238
239 if (gdk_event->is_hint)
240 {
241 int x = 0;
242 int y = 0;
243 GdkModifierType state;
244 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
245 gdk_event->x = x;
246 gdk_event->y = y;
247 gdk_event->state = state;
248 }
249
250 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
251 win->m_oldX = (int)gdk_event->x - win->m_diffX;
252 win->m_oldY = (int)gdk_event->y - win->m_diffY;
253 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
254
255 return TRUE;
256 }
257
258 //-----------------------------------------------------------------------------
259 // "clicked" of X system button
260 //-----------------------------------------------------------------------------
261
262 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxMiniFrame *mf )
263 {
264 if (g_isIdle) wxapp_install_idle_handler();
265
266 mf->Close();
267 }
268
269 //-----------------------------------------------------------------------------
270 // wxMiniFrame
271 //-----------------------------------------------------------------------------
272
273 static char *cross_xpm[] = {
274 /* columns rows colors chars-per-pixel */
275 "5 5 16 1",
276 " c Gray0",
277 ". c #bf0000",
278 "X c #00bf00",
279 "o c #bfbf00",
280 "O c #0000bf",
281 "+ c #bf00bf",
282 "@ c #00bfbf",
283 "# c None",
284 "$ c #808080",
285 "% c Red",
286 "& c Green",
287 "* c Yellow",
288 "= c Blue",
289 "- c Magenta",
290 "; c Cyan",
291 ": c Gray100",
292 /* pixels */
293 " ### ",
294 "# # #",
295 "## ##",
296 "# # #",
297 " ### ",
298 };
299
300 IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame)
301
302 bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
303 const wxPoint &pos, const wxSize &size,
304 long style, const wxString &name )
305 {
306 style = style | wxCAPTION | wxFRAME_FLOAT_ON_PARENT;
307
308 if ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT))
309 m_miniTitle = 13;
310
311 m_miniEdge = 3;
312 m_isDragging = FALSE;
313 m_oldX = -1;
314 m_oldY = -1;
315 m_diffX = 0;
316 m_diffY = 0;
317
318 wxFrame::Create( parent, id, title, pos, size, style, name );
319
320 if ((style & wxSYSTEM_MENU) &&
321 ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT)))
322 {
323 GdkBitmap *mask = (GdkBitmap*) NULL;
324 GdkPixmap *pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, cross_xpm );
325
326 GtkWidget *pw = gtk_pixmap_new( pixmap, mask );
327 gdk_bitmap_unref( mask );
328 gdk_pixmap_unref( pixmap );
329 gtk_widget_show( pw );
330
331 GtkWidget *close_button = gtk_button_new();
332 gtk_container_add( GTK_CONTAINER(close_button), pw );
333
334 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
335 close_button,
336 size.x-16, 4, 11, 11 );
337
338 gtk_widget_show( close_button );
339
340 gtk_signal_connect( GTK_OBJECT(close_button), "clicked",
341 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
342 }
343
344 /* these are called when the borders are drawn */
345 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "expose_event",
346 GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this );
347
348 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "draw",
349 GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this );
350
351 /* these are required for dragging the mini frame around */
352 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_press_event",
353 GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this );
354
355 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_release_event",
356 GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this );
357
358 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "motion_notify_event",
359 GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this );
360
361 return TRUE;
362 }
363
364 #endif