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