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