New idle handling. Only that.
[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 #include "wx/dcscreen.h"
16
17 #include "gtk/gtk.h"
18 #include "wx/gtk/win_gtk.h"
19
20 #include "gdk/gdk.h"
21 #include "gdk/gdkprivate.h"
22 #include "gdk/gdkx.h"
23
24 //-----------------------------------------------------------------------------
25 // idle system
26 //-----------------------------------------------------------------------------
27
28 extern void wxapp_install_idle_handler();
29 extern bool g_isIdle;
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern bool g_blockEventsOnDrag;
36 extern bool g_blockEventsOnScroll;
37
38 //-----------------------------------------------------------------------------
39 // local functions
40 //-----------------------------------------------------------------------------
41
42 /* draw XOR rectangle when moving mine frame around */
43
44 static void DrawFrame( GtkWidget *widget, int x, int y, int w, int h )
45 {
46 int org_x = 0;
47 int org_y = 0;
48 gdk_window_get_origin( widget->window, &org_x, &org_y );
49 x += org_x;
50 y += org_y;
51
52 GdkGC *gc = gdk_gc_new( GDK_ROOT_PARENT() );
53 gdk_gc_set_subwindow( gc, GDK_INCLUDE_INFERIORS );
54 gdk_gc_set_function( gc, GDK_INVERT );
55
56 gdk_draw_rectangle( GDK_ROOT_PARENT(), gc, FALSE, x, y, w, h );
57 gdk_gc_unref( gc );
58 }
59
60 //-----------------------------------------------------------------------------
61 // "expose_event" of m_mainWidget
62 //-----------------------------------------------------------------------------
63
64 static void gtk_window_own_expose_callback( GtkWidget *widget, GdkEventExpose *gdk_event, wxFrame *win )
65 {
66 if (g_isIdle) wxapp_install_idle_handler();
67
68 if (!win->HasVMT()) return;
69 if (gdk_event->count > 0) return;
70
71 gtk_draw_shadow( widget->style,
72 widget->window,
73 GTK_STATE_NORMAL,
74 GTK_SHADOW_OUT,
75 0, 0,
76 win->m_width, win->m_height );
77 }
78
79 //-----------------------------------------------------------------------------
80 // "draw" of m_mainWidget
81 //-----------------------------------------------------------------------------
82
83 static void gtk_window_own_draw_callback( GtkWidget *widget, GdkRectangle *WXUNUSED(rect), wxFrame *win )
84 {
85 if (g_isIdle) wxapp_install_idle_handler();
86
87 if (!win->HasVMT()) return;
88
89 gtk_draw_shadow( widget->style,
90 widget->window,
91 GTK_STATE_NORMAL,
92 GTK_SHADOW_OUT,
93 0, 0,
94 win->m_width, win->m_height );
95 }
96
97 //-----------------------------------------------------------------------------
98 // "button_press_event" of m_mainWidget
99 //-----------------------------------------------------------------------------
100
101 static gint gtk_window_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
102 {
103 if (g_isIdle) wxapp_install_idle_handler();
104
105 if (!win->HasVMT()) return FALSE;
106 if (g_blockEventsOnDrag) return TRUE;
107 if (g_blockEventsOnScroll) return TRUE;
108
109 if (win->m_isDragging) return TRUE;
110
111 gdk_pointer_grab( widget->window, FALSE,
112 (GdkEventMask)
113 (GDK_BUTTON_PRESS_MASK |
114 GDK_BUTTON_RELEASE_MASK |
115 GDK_POINTER_MOTION_MASK |
116 GDK_POINTER_MOTION_HINT_MASK |
117 GDK_BUTTON_MOTION_MASK |
118 GDK_BUTTON1_MOTION_MASK),
119 (GdkWindow *) NULL,
120 (GdkCursor *) NULL,
121 GDK_CURRENT_TIME );
122
123 win->m_diffX = (int)gdk_event->x;
124 win->m_diffY = (int)gdk_event->y;
125 DrawFrame( widget, 0, 0, win->m_width, win->m_height );
126 win->m_oldX = 0;
127 win->m_oldY = 0;
128
129 win->m_isDragging = TRUE;
130
131 return TRUE;
132 }
133
134 //-----------------------------------------------------------------------------
135 // "button_release_event" of m_mainWidget
136 //-----------------------------------------------------------------------------
137
138 static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxMiniFrame *win )
139 {
140 if (g_isIdle) wxapp_install_idle_handler();
141
142 if (!win->HasVMT()) return FALSE;
143 if (g_blockEventsOnDrag) return TRUE;
144 if (g_blockEventsOnScroll) return TRUE;
145
146 if (!win->m_isDragging) return TRUE;
147
148 win->m_isDragging = FALSE;
149
150 int x = (int)gdk_event->x;
151 int y = (int)gdk_event->y;
152
153 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
154 gdk_pointer_ungrab ( GDK_CURRENT_TIME );
155 int org_x = 0;
156 int org_y = 0;
157 gdk_window_get_origin( widget->window, &org_x, &org_y );
158 x += org_x - win->m_diffX;
159 y += org_y - win->m_diffY;
160 win->m_x = x;
161 win->m_y = y;
162 gtk_widget_set_uposition( win->m_widget, x, y );
163
164 return TRUE;
165 }
166
167 //-----------------------------------------------------------------------------
168 // "motion_notify_event" of m_mainWidget
169 //-----------------------------------------------------------------------------
170
171 static gint gtk_window_motion_notify_callback( GtkWidget *widget, GdkEventMotion *gdk_event, wxMiniFrame *win )
172 {
173 if (g_isIdle) wxapp_install_idle_handler();
174
175 if (!win->HasVMT()) return FALSE;
176 if (g_blockEventsOnDrag) return TRUE;
177 if (g_blockEventsOnScroll) return TRUE;
178
179 if (!win->m_isDragging) return TRUE;
180
181 if (gdk_event->is_hint)
182 {
183 int x = 0;
184 int y = 0;
185 GdkModifierType state;
186 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
187 gdk_event->x = x;
188 gdk_event->y = y;
189 gdk_event->state = state;
190 }
191
192 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
193 win->m_oldX = (int)gdk_event->x - win->m_diffX;
194 win->m_oldY = (int)gdk_event->y - win->m_diffY;
195 DrawFrame( widget, win->m_oldX, win->m_oldY, win->m_width, win->m_height );
196
197 return TRUE;
198 }
199
200 //-----------------------------------------------------------------------------
201 // "clicked" of X system button
202 //-----------------------------------------------------------------------------
203
204 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxMiniFrame *mf )
205 {
206 if (g_isIdle) wxapp_install_idle_handler();
207
208 mf->Close();
209 }
210
211 //-----------------------------------------------------------------------------
212 // wxMiniFrame
213 //-----------------------------------------------------------------------------
214
215 IMPLEMENT_DYNAMIC_CLASS(wxMiniFrame,wxFrame)
216
217 bool wxMiniFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
218 const wxPoint &pos, const wxSize &size,
219 long style, const wxString &name )
220 {
221 style = style | wxSIMPLE_BORDER;
222
223 m_miniEdge = 3;
224 m_miniTitle = 13;
225 m_isDragging = FALSE;
226 m_oldX = -1;
227 m_oldY = -1;
228 m_diffX = 0;
229 m_diffY = 0;
230
231 wxFrame::Create( parent, id, title, pos, size, style, name );
232
233 GtkWidget *close_button = gtk_button_new_with_label( "x" );
234
235 gtk_myfixed_put( GTK_MYFIXED(m_mainWidget), close_button, 4, 4 );
236 gtk_widget_set_usize( close_button, 12, 11 );
237
238 gtk_widget_show( close_button );
239
240 gtk_signal_connect( GTK_OBJECT(close_button), "clicked",
241 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
242
243 /* these are called when the borders are drawn */
244 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "expose_event",
245 GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this );
246
247 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "draw",
248 GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this );
249
250 /* these are required for dragging the mini frame around */
251 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_press_event",
252 GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this );
253
254 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "button_release_event",
255 GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this );
256
257 gtk_signal_connect( GTK_OBJECT(m_mainWidget), "motion_notify_event",
258 GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this );
259
260 return TRUE;
261 }