]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/minifram.cpp
let's not forget that \& has lower priority than == in C
[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
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
41 //-----------------------------------------------------------------------------
42 // local functions
43 //-----------------------------------------------------------------------------
44
45 /* draw XOR rectangle when moving mine frame around */
46
47 static void DrawFrame( GtkWidget *widget, int x, int y, int w, int h )
48 {
49 int org_x = 0;
50 int org_y = 0;
51 gdk_window_get_origin( widget->window, &org_x, &org_y );
52 x += org_x;
53 y += org_y;
54
55 GdkGC *gc = gdk_gc_new( GDK_ROOT_PARENT() );
56 gdk_gc_set_subwindow( gc, GDK_INCLUDE_INFERIORS );
57 gdk_gc_set_function( gc, GDK_INVERT );
58
59 gdk_draw_rectangle( GDK_ROOT_PARENT(), gc, FALSE, x, y, w, h );
60 gdk_gc_unref( gc );
61 }
62
63 //-----------------------------------------------------------------------------
64 // "expose_event" of m_mainWidget
65 //-----------------------------------------------------------------------------
66
67 static void gtk_window_own_expose_callback( GtkWidget *widget, GdkEventExpose *gdk_event, wxFrame *win )
68 {
69 if (g_isIdle) wxapp_install_idle_handler();
70
71 if (!win->m_hasVMT) return;
72 if (gdk_event->count > 0) return;
73
74 GtkMyFixed *myfixed = GTK_MYFIXED(widget);
75
76 gtk_draw_shadow( widget->style,
77 myfixed->bin_window,
78 GTK_STATE_NORMAL,
79 GTK_SHADOW_OUT,
80 0, 0,
81 win->m_width, win->m_height );
82
83 if (!win->m_title.IsEmpty() &&
84 ((win->GetWindowStyle() & wxCAPTION) ||
85 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
86 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
87 {
88 GdkGC *gc = gdk_gc_new( myfixed->bin_window );
89 GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0);
90 int x = 2;
91 if (win->GetWindowStyle() & wxSYSTEM_MENU) x = 18;
92
93 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
94 gdk_draw_rectangle( myfixed->bin_window, gc, TRUE,
95 x,
96 3,
97 win->m_width - 4 - x,
98 font->ascent + font->descent+1 );
99
100 gdk_gc_set_foreground( gc, &widget->style->white );
101 gdk_draw_string( myfixed->bin_window, font, gc,
102 x+2,
103 3+font->ascent,
104 win->m_title.mb_str() );
105
106 gdk_gc_unref( gc );
107 }
108 }
109
110 //-----------------------------------------------------------------------------
111 // "draw" of m_mainWidget
112 //-----------------------------------------------------------------------------
113
114 static void gtk_window_own_draw_callback( GtkWidget *widget, GdkRectangle *WXUNUSED(rect), wxFrame *win )
115 {
116 if (g_isIdle) wxapp_install_idle_handler();
117
118 if (!win->m_hasVMT) return;
119
120 GtkMyFixed *myfixed = GTK_MYFIXED(widget);
121
122 gtk_draw_shadow( widget->style,
123 myfixed->bin_window,
124 GTK_STATE_NORMAL,
125 GTK_SHADOW_OUT,
126 0, 0,
127 win->m_width, win->m_height );
128
129 if (!win->m_title.IsEmpty() &&
130 ((win->GetWindowStyle() & wxCAPTION) ||
131 (win->GetWindowStyle() & wxTINY_CAPTION_HORIZ) ||
132 (win->GetWindowStyle() & wxTINY_CAPTION_VERT)))
133 {
134 GdkGC *gc = gdk_gc_new( myfixed->bin_window );
135 GdkFont *font = wxSMALL_FONT->GetInternalFont(1.0);
136 int x = 2;
137 if (win->GetWindowStyle() & wxSYSTEM_MENU) x = 17;
138
139 gdk_gc_set_foreground( gc, &widget->style->bg[GTK_STATE_SELECTED] );
140 gdk_draw_rectangle( myfixed->bin_window, gc, TRUE,
141 x,
142 3,
143 win->m_width - 4 - x,
144 font->ascent + font->descent+1 );
145
146 gdk_gc_set_foreground( gc, &widget->style->white );
147 gdk_draw_string( myfixed->bin_window, font, gc,
148 x+2,
149 3+font->ascent,
150 win->m_title.mb_str() );
151
152 gdk_gc_unref( gc );
153 }
154 }
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 gdk_window_raise( win->m_widget->window );
171
172 gdk_pointer_grab( widget->window, FALSE,
173 (GdkEventMask)
174 (GDK_BUTTON_PRESS_MASK |
175 GDK_BUTTON_RELEASE_MASK |
176 GDK_POINTER_MOTION_MASK |
177 GDK_POINTER_MOTION_HINT_MASK |
178 GDK_BUTTON_MOTION_MASK |
179 GDK_BUTTON1_MOTION_MASK),
180 (GdkWindow *) NULL,
181 (GdkCursor *) NULL,
182 GDK_CURRENT_TIME );
183
184 win->m_diffX = (int)gdk_event->x;
185 win->m_diffY = (int)gdk_event->y;
186 DrawFrame( widget, 0, 0, win->m_width, win->m_height );
187 win->m_oldX = 0;
188 win->m_oldY = 0;
189
190 win->m_isDragging = TRUE;
191
192 return TRUE;
193 }
194
195 //-----------------------------------------------------------------------------
196 // "button_release_event" of m_mainWidget
197 //-----------------------------------------------------------------------------
198
199 static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
200 {
201 if (g_isIdle) wxapp_install_idle_handler();
202
203 if (!win->m_hasVMT) return FALSE;
204 if (g_blockEventsOnDrag) return TRUE;
205 if (g_blockEventsOnScroll) return TRUE;
206
207 if (!win->m_isDragging) return TRUE;
208
209 win->m_isDragging = FALSE;
210
211 int x = (int)gdk_event->x;
212 int y = (int)gdk_event->y;
213
214 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
215 gdk_pointer_ungrab ( GDK_CURRENT_TIME );
216 int org_x = 0;
217 int org_y = 0;
218 gdk_window_get_origin( widget->window, &org_x, &org_y );
219 x += org_x - win->m_diffX;
220 y += org_y - win->m_diffY;
221 win->m_x = x;
222 win->m_y = y;
223 gtk_widget_set_uposition( win->m_widget, x, y );
224
225 return TRUE;
226 }
227
228 //-----------------------------------------------------------------------------
229 // "motion_notify_event" of m_mainWidget
230 //-----------------------------------------------------------------------------
231
232 static gint gtk_window_motion_notify_callback( GtkWidget *widget, GdkEventMotion *gdk_event, wxMiniFrame *win )
233 {
234 if (g_isIdle) wxapp_install_idle_handler();
235
236 if (!win->m_hasVMT) return FALSE;
237 if (g_blockEventsOnDrag) return TRUE;
238 if (g_blockEventsOnScroll) return TRUE;
239
240 if (!win->m_isDragging) return TRUE;
241
242 if (gdk_event->is_hint)
243 {
244 int x = 0;
245 int y = 0;
246 GdkModifierType state;
247 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
248 gdk_event->x = x;
249 gdk_event->y = y;
250 gdk_event->state = state;
251 }
252
253 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
254 win->m_oldX = (int)gdk_event->x - win->m_diffX;
255 win->m_oldY = (int)gdk_event->y - win->m_diffY;
256 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
257
258 return TRUE;
259 }
260
261 //-----------------------------------------------------------------------------
262 // "clicked" of X system button
263 //-----------------------------------------------------------------------------
264
265 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxMiniFrame *mf )
266 {
267 if (g_isIdle) wxapp_install_idle_handler();
268
269 mf->Close();
270 }
271
272 //-----------------------------------------------------------------------------
273 // wxMiniFrame
274 //-----------------------------------------------------------------------------
275
276 IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame)
277
278 bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
279 const wxPoint &pos, const wxSize &size,
280 long style, const wxString &name )
281 {
282 style = style | wxSIMPLE_BORDER;
283
284 if ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT))
285 m_miniTitle = 13;
286
287 m_miniEdge = 3;
288 m_isDragging = FALSE;
289 m_oldX = -1;
290 m_oldY = -1;
291 m_diffX = 0;
292 m_diffY = 0;
293
294 wxFrame::Create( parent, id, title, pos, size, style, name );
295
296 if ((style & wxSYSTEM_MENU) &&
297 ((style & wxCAPTION) || (style & wxTINY_CAPTION_HORIZ) || (style & wxTINY_CAPTION_VERT)))
298 {
299 GtkWidget *close_button = gtk_button_new_with_label( "x" );
300
301 gtk_myfixed_put( GTK_MYFIXED(m_mainWidget),
302 close_button,
303 4, 4, 12, 11 );
304
305 gtk_widget_show( close_button );
306
307 gtk_signal_connect( GTK_OBJECT(close_button), "clicked",
308 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
309 }
310
311 /* these are called when the borders are drawn */
312 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "expose_event",
313 GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this );
314
315 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "draw",
316 GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this );
317
318 /* these are required for dragging the mini frame around */
319 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_press_event",
320 GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this );
321
322 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_release_event",
323 GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this );
324
325 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "motion_notify_event",
326 GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this );
327
328 return TRUE;
329 }
330
331 #endif