Optimize (for size) the cross_xpm used for the X button in wxMiniFrame. We use only...
[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 2 1",
252 "# c Gray0",
253 " c None",
254 /* pixels */
255 "# #",
256 " # # ",
257 " # ",
258 " # # ",
259 "# #",
260 };
261
262 IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame)
263
264 bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
265 const wxPoint &pos, const wxSize &size,
266 long style, const wxString &name )
267 {
268 style = style | wxCAPTION;
269
270 if ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT))
271 m_miniTitle = 13;
272
273 m_miniEdge = 3;
274 m_isDragging = false;
275 m_oldX = -1;
276 m_oldY = -1;
277 m_diffX = 0;
278 m_diffY = 0;
279
280 wxFrame::Create( parent, id, title, pos, size, style, name );
281
282 if (m_parent && (GTK_IS_WINDOW(m_parent->m_widget)))
283 {
284 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
285 }
286
287 if ((style & wxSYSTEM_MENU) &&
288 ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT)))
289 {
290 GdkBitmap *mask = (GdkBitmap*) NULL;
291 GdkPixmap *pixmap = gdk_pixmap_create_from_xpm_d
292 (
293 wxGetRootWindow()->window,
294 &mask,
295 NULL,
296 (char **)cross_xpm
297 );
298
299 GtkWidget *pw = gtk_pixmap_new( pixmap, mask );
300 gdk_bitmap_unref( mask );
301 gdk_pixmap_unref( pixmap );
302 gtk_widget_show( pw );
303
304 GtkWidget *close_button = gtk_button_new();
305 gtk_container_add( GTK_CONTAINER(close_button), pw );
306
307 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
308 close_button,
309 size.x-16, 4, 11, 11 );
310
311 gtk_widget_show( close_button );
312
313 g_signal_connect (close_button, "clicked",
314 G_CALLBACK (gtk_button_clicked_callback),
315 this);
316 }
317
318 /* these are called when the borders are drawn */
319 g_signal_connect (m_mainWidget, "expose_event",
320 G_CALLBACK (gtk_window_own_expose_callback), this );
321
322 /* these are required for dragging the mini frame around */
323 g_signal_connect (m_mainWidget, "button_press_event",
324 G_CALLBACK (gtk_window_button_press_callback), this);
325 g_signal_connect (m_mainWidget, "button_release_event",
326 G_CALLBACK (gtk_window_button_release_callback), this);
327 g_signal_connect (m_mainWidget, "motion_notify_event",
328 G_CALLBACK (gtk_window_motion_notify_callback), this);
329
330 return true;
331 }
332
333 void wxMiniFrame::SetTitle( const wxString &title )
334 {
335 wxFrame::SetTitle( title );
336
337 gdk_window_invalidate_rect( GTK_PIZZA(m_mainWidget)->bin_window, NULL, true );
338 }
339
340 #endif