]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/window.cpp
Added wxImage.GetDataBuffer which returns an in-place edit buffer of
[wxWidgets.git] / src / gtk / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/window.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "window.h"
13 #endif
14
15 #ifdef __VMS
16 #define XWarpPointer XWARPPOINTER
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/window.h"
21 #include "wx/dcclient.h"
22 #include "wx/frame.h"
23 #include "wx/app.h"
24 #include "wx/layout.h"
25 #include "wx/utils.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
28 #include "wx/module.h"
29
30 #if wxUSE_DRAG_AND_DROP
31 #include "wx/dnd.h"
32 #endif
33
34 #if wxUSE_TOOLTIPS
35 #include "wx/tooltip.h"
36 #endif
37
38 #if wxUSE_CARET
39 #include "wx/caret.h"
40 #endif // wxUSE_CARET
41
42 #if wxUSE_TEXTCTRL
43 #include "wx/textctrl.h"
44 #endif
45
46 #include "wx/menu.h"
47 #include "wx/statusbr.h"
48 #include "wx/intl.h"
49 #include "wx/settings.h"
50 #include "wx/log.h"
51
52 #ifdef __WXDEBUG__
53 #include "wx/thread.h"
54 #endif
55
56 #include <math.h>
57
58 #include "wx/gtk/private.h"
59 #include <gdk/gdkprivate.h>
60 #include <gdk/gdkkeysyms.h>
61 #include <gdk/gdkx.h>
62
63 #include <gtk/gtk.h>
64 #include <gtk/gtkprivate.h>
65
66 #include "wx/gtk/win_gtk.h"
67
68 #ifdef __WXGTK20__
69 #define SET_CONTAINER_FOCUS(w, d) gtk_widget_child_focus((w), (d))
70 #else
71 #define SET_CONTAINER_FOCUS(w, d) gtk_container_focus(GTK_CONTAINER(w), (d))
72 #endif
73
74 #ifdef __WXGTK20__
75 #ifdef HAVE_XIM
76 #undef HAVE_XIM
77 #endif
78 #endif
79
80 #ifdef __WXGTK20__
81 extern GtkContainerClass *pizza_parent_class;
82 #endif
83
84 //-----------------------------------------------------------------------------
85 // documentation on internals
86 //-----------------------------------------------------------------------------
87
88 /*
89 I have been asked several times about writing some documentation about
90 the GTK port of wxWindows, especially its internal structures. Obviously,
91 you cannot understand wxGTK without knowing a little about the GTK, but
92 some more information about what the wxWindow, which is the base class
93 for all other window classes, does seems required as well.
94
95 I)
96
97 What does wxWindow do? It contains the common interface for the following
98 jobs of its descendants:
99
100 1) Define the rudimentary behaviour common to all window classes, such as
101 resizing, intercepting user input (so as to make it possible to use these
102 events for special purposes in a derived class), window names etc.
103
104 2) Provide the possibility to contain and manage children, if the derived
105 class is allowed to contain children, which holds true for those window
106 classes which do not display a native GTK widget. To name them, these
107 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
108 work classes are a special case and are handled a bit differently from
109 the rest. The same holds true for the wxNotebook class.
110
111 3) Provide the possibility to draw into a client area of a window. This,
112 too, only holds true for classes that do not display a native GTK widget
113 as above.
114
115 4) Provide the entire mechanism for scrolling widgets. This actual inter-
116 face for this is usually in wxScrolledWindow, but the GTK implementation
117 is in this class.
118
119 5) A multitude of helper or extra methods for special purposes, such as
120 Drag'n'Drop, managing validators etc.
121
122 6) Display a border (sunken, raised, simple or none).
123
124 Normally one might expect, that one wxWindows window would always correspond
125 to one GTK widget. Under GTK, there is no such allround widget that has all
126 the functionality. Moreover, the GTK defines a client area as a different
127 widget from the actual widget you are handling. Last but not least some
128 special classes (e.g. wxFrame) handle different categories of widgets and
129 still have the possibility to draw something in the client area.
130 It was therefore required to write a special purpose GTK widget, that would
131 represent a client area in the sense of wxWindows capable to do the jobs
132 2), 3) and 4). I have written this class and it resides in win_gtk.c of
133 this directory.
134
135 All windows must have a widget, with which they interact with other under-
136 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
137 thw wxWindow class has a member variable called m_widget which holds a
138 pointer to this widget. When the window class represents a GTK native widget,
139 this is (in most cases) the only GTK widget the class manages. E.g. the
140 wxStatitText class handles only a GtkLabel widget a pointer to which you
141 can find in m_widget (defined in wxWindow)
142
143 When the class has a client area for drawing into and for containing children
144 it has to handle the client area widget (of the type GtkPizza, defined in
145 win_gtk.c), but there could be any number of widgets, handled by a class
146 The common rule for all windows is only, that the widget that interacts with
147 the rest of GTK must be referenced in m_widget and all other widgets must be
148 children of this widget on the GTK level. The top-most widget, which also
149 represents the client area, must be in the m_wxwindow field and must be of
150 the type GtkPizza.
151
152 As I said, the window classes that display a GTK native widget only have
153 one widget, so in the case of e.g. the wxButton class m_widget holds a
154 pointer to a GtkButton widget. But windows with client areas (for drawing
155 and children) have a m_widget field that is a pointer to a GtkScrolled-
156 Window and a m_wxwindow field that is pointer to a GtkPizza and this
157 one is (in the GTK sense) a child of the GtkScrolledWindow.
158
159 If the m_wxwindow field is set, then all input to this widget is inter-
160 cepted and sent to the wxWindows class. If not, all input to the widget
161 that gets pointed to by m_widget gets intercepted and sent to the class.
162
163 II)
164
165 The design of scrolling in wxWindows is markedly different from that offered
166 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
167 clicking on a scrollbar belonging to scrolled window will inevitably move
168 the window. In wxWindows, the scrollbar will only emit an event, send this
169 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
170 which actually moves the window and its subchildren. Note that GtkPizza
171 memorizes how much it has been scrolled but that wxWindows forgets this
172 so that the two coordinates systems have to be kept in synch. This is done
173 in various places using the pizza->xoffset and pizza->yoffset values.
174
175 III)
176
177 Singularily the most broken code in GTK is the code that is supposes to
178 inform subwindows (child windows) about new positions. Very often, duplicate
179 events are sent without changes in size or position, equally often no
180 events are sent at all (All this is due to a bug in the GtkContainer code
181 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
182 GTK's own system and it simply waits for size events for toplevel windows
183 and then iterates down the respective size events to all window. This has
184 the disadvantage, that windows might get size events before the GTK widget
185 actually has the reported size. This doesn't normally pose any problem, but
186 the OpenGl drawing routines rely on correct behaviour. Therefore, I have
187 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
188 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
189 window that is used for OpenGl output really has that size (as reported by
190 GTK).
191
192 IV)
193
194 If someone at some point of time feels the immense desire to have a look at,
195 change or attempt to optimse the Refresh() logic, this person will need an
196 intimate understanding of what a "draw" and what an "expose" events are and
197 what there are used for, in particular when used in connection with GTK's
198 own windowless widgets. Beware.
199
200 V)
201
202 Cursors, too, have been a constant source of pleasure. The main difficulty
203 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
204 for the parent. To prevent this from doing too much harm, I use idle time
205 to set the cursor over and over again, starting from the toplevel windows
206 and ending with the youngest generation (speaking of parent and child windows).
207 Also don't forget that cursors (like much else) are connected to GdkWindows,
208 not GtkWidgets and that the "window" field of a GtkWidget might very well
209 point to the GdkWindow of the parent widget (-> "window less widget") and
210 that the two obviously have very different meanings.
211
212 */
213
214 //-----------------------------------------------------------------------------
215 // data
216 //-----------------------------------------------------------------------------
217
218 extern wxList wxPendingDelete;
219 extern bool g_blockEventsOnDrag;
220 extern bool g_blockEventsOnScroll;
221 extern wxCursor g_globalCursor;
222
223 static GdkGC *g_eraseGC = NULL;
224
225 // mouse capture state: the window which has it and if the mouse is currently
226 // inside it
227 static wxWindowGTK *g_captureWindow = (wxWindowGTK*) NULL;
228 static bool g_captureWindowHasMouse = FALSE;
229
230 /* extern */ wxWindowGTK *g_focusWindow = (wxWindowGTK*) NULL;
231
232 // the last window which had the focus - this is normally never NULL (except
233 // if we never had focus at all) as even when g_focusWindow is NULL it still
234 // keeps its previous value
235 static wxWindowGTK *g_focusWindowLast = (wxWindowGTK*) NULL;
236
237 // the frame that is currently active (i.e. its child has focus). It is
238 // used to generate wxActivateEvents
239 static wxWindowGTK *g_activeFrame = (wxWindowGTK*) NULL;
240 static bool g_activeFrameLostFocus = FALSE;
241
242 // If a window get the focus set but has not been realized
243 // yet, defer setting the focus to idle time.
244 wxWindowGTK *g_delayedFocus = (wxWindowGTK*) NULL;
245
246 // if we detect that the app has got/lost the focus, we set this variable to
247 // either TRUE or FALSE and an activate event will be sent during the next
248 // OnIdle() call and it is reset to -1: this value means that we shouldn't
249 // send any activate events at all
250 static int g_sendActivateEvent = -1;
251
252 /* hack: we need something to pass to gtk_menu_popup, so we store the time of
253 the last click here */
254 static guint32 gs_timeLastClick = 0;
255
256 extern bool g_mainThreadLocked;
257
258 //-----------------------------------------------------------------------------
259 // debug
260 //-----------------------------------------------------------------------------
261
262 #ifndef __WXGTK20__
263 #define DISABLE_STYLE_IF_BROKEN_THEME 1
264 #endif
265
266 #ifdef __WXDEBUG__
267
268 #if wxUSE_THREADS
269 # define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
270 #else
271 # define DEBUG_MAIN_THREAD
272 #endif
273 #else
274 #define DEBUG_MAIN_THREAD
275 #endif // Debug
276
277 //-----------------------------------------------------------------------------
278 // missing gdk functions
279 //-----------------------------------------------------------------------------
280
281 void
282 gdk_window_warp_pointer (GdkWindow *window,
283 gint x,
284 gint y)
285 {
286 #ifndef __WXGTK20__
287 GdkWindowPrivate *priv;
288 #endif
289
290 if (!window)
291 window = GDK_ROOT_PARENT();
292
293 #ifdef __WXGTK20__
294 if (!GDK_WINDOW_DESTROYED(window))
295 {
296 XWarpPointer (GDK_WINDOW_XDISPLAY(window),
297 None, /* not source window -> move from anywhere */
298 GDK_WINDOW_XID(window), /* dest window */
299 0, 0, 0, 0, /* not source window -> move from anywhere */
300 x, y );
301 }
302 #else
303 priv = (GdkWindowPrivate*) window;
304
305 if (!priv->destroyed)
306 {
307 XWarpPointer (priv->xdisplay,
308 None, /* not source window -> move from anywhere */
309 priv->xwindow, /* dest window */
310 0, 0, 0, 0, /* not source window -> move from anywhere */
311 x, y );
312 }
313 #endif
314 }
315
316 //-----------------------------------------------------------------------------
317 // idle system
318 //-----------------------------------------------------------------------------
319
320 extern void wxapp_install_idle_handler();
321 extern bool g_isIdle;
322
323 //-----------------------------------------------------------------------------
324 // local code (see below)
325 //-----------------------------------------------------------------------------
326
327 // returns the child of win which currently has focus or NULL if not found
328 //
329 // Note: can't be static, needed by textctrl.cpp.
330 wxWindow *wxFindFocusedChild(wxWindowGTK *win)
331 {
332 wxWindow *winFocus = wxWindowGTK::FindFocus();
333 if ( !winFocus )
334 return (wxWindow *)NULL;
335
336 if ( winFocus == win )
337 return (wxWindow *)win;
338
339 for ( wxWindowList::Node *node = win->GetChildren().GetFirst();
340 node;
341 node = node->GetNext() )
342 {
343 wxWindow *child = wxFindFocusedChild(node->GetData());
344 if ( child )
345 return child;
346 }
347
348 return (wxWindow *)NULL;
349 }
350
351 static void draw_frame( GtkWidget *widget, wxWindowGTK *win )
352 {
353 // wxUniversal widgets draw the borders and scrollbars themselves
354 #ifndef __WXUNIVERSAL__
355 if (!win->m_hasVMT)
356 return;
357
358 int dw = 0;
359 int dh = 0;
360
361 if (win->m_hasScrolling)
362 {
363 GtkScrolledWindow *scroll_window = GTK_SCROLLED_WINDOW(widget);
364
365 GtkRequisition vscroll_req;
366 vscroll_req.width = 2;
367 vscroll_req.height = 2;
368 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->vscrollbar) )->size_request )
369 (scroll_window->vscrollbar, &vscroll_req );
370
371 GtkRequisition hscroll_req;
372 hscroll_req.width = 2;
373 hscroll_req.height = 2;
374 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->hscrollbar) )->size_request )
375 (scroll_window->hscrollbar, &hscroll_req );
376
377 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(widget) );
378
379 if (scroll_window->vscrollbar_visible)
380 {
381 dw += vscroll_req.width;
382 dw += scroll_class->scrollbar_spacing;
383 }
384
385 if (scroll_window->hscrollbar_visible)
386 {
387 dh += hscroll_req.height;
388 dh += scroll_class->scrollbar_spacing;
389 }
390 }
391
392 int dx = 0;
393 int dy = 0;
394 if (GTK_WIDGET_NO_WINDOW (widget))
395 {
396 dx += widget->allocation.x;
397 dy += widget->allocation.y;
398 }
399
400 if (win->HasFlag(wxRAISED_BORDER))
401 {
402 gtk_draw_shadow( widget->style,
403 widget->window,
404 GTK_STATE_NORMAL,
405 GTK_SHADOW_OUT,
406 dx, dy,
407 widget->allocation.width-dw, widget->allocation.height-dh );
408 return;
409 }
410
411 if (win->HasFlag(wxSUNKEN_BORDER))
412 {
413 gtk_draw_shadow( widget->style,
414 widget->window,
415 GTK_STATE_NORMAL,
416 GTK_SHADOW_IN,
417 dx, dy,
418 widget->allocation.width-dw, widget->allocation.height-dh );
419 return;
420 }
421
422 if (win->HasFlag(wxSIMPLE_BORDER))
423 {
424 GdkGC *gc;
425 gc = gdk_gc_new( widget->window );
426 gdk_gc_set_foreground( gc, &widget->style->black );
427 gdk_draw_rectangle( widget->window, gc, FALSE,
428 dx, dy,
429 widget->allocation.width-dw-1, widget->allocation.height-dh-1 );
430 gdk_gc_unref( gc );
431 return;
432 }
433 #endif // __WXUNIVERSAL__
434 }
435
436 //-----------------------------------------------------------------------------
437 // "expose_event" of m_widget
438 //-----------------------------------------------------------------------------
439
440 gint gtk_window_own_expose_callback( GtkWidget *widget, GdkEventExpose *gdk_event, wxWindowGTK *win )
441 {
442 if (gdk_event->count > 0) return FALSE;
443
444 draw_frame( widget, win );
445
446 #ifdef __WXGTK20__
447
448 (* GTK_WIDGET_CLASS (pizza_parent_class)->expose_event) (widget, gdk_event);
449
450 #endif
451 return TRUE;
452 }
453
454 //-----------------------------------------------------------------------------
455 // "draw" of m_widget
456 //-----------------------------------------------------------------------------
457
458 #ifndef __WXGTK20__
459
460 static void gtk_window_own_draw_callback( GtkWidget *widget, GdkRectangle *WXUNUSED(rect), wxWindowGTK *win )
461 {
462 draw_frame( widget, win );
463 }
464
465 #endif // GTK+ < 2.0
466
467 //-----------------------------------------------------------------------------
468 // "size_request" of m_widget
469 //-----------------------------------------------------------------------------
470
471 static void gtk_window_size_request_callback( GtkWidget *widget, GtkRequisition *requisition, wxWindow *win )
472 {
473 int w,h;
474 win->GetSize( &w, &h );
475 if (w < 2) w = 2;
476 if (h < 2) h = 2;
477
478 requisition->height = h;
479 requisition->width = w;
480 }
481
482 //-----------------------------------------------------------------------------
483 // "expose_event" of m_wxwindow
484 //-----------------------------------------------------------------------------
485
486 static int gtk_window_expose_callback( GtkWidget *widget,
487 GdkEventExpose *gdk_event,
488 wxWindow *win )
489 {
490 DEBUG_MAIN_THREAD
491
492 if (g_isIdle)
493 wxapp_install_idle_handler();
494
495 #if 0
496 if (win->GetName())
497 {
498 wxPrintf( wxT("OnExpose from ") );
499 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
500 wxPrintf( win->GetClassInfo()->GetClassName() );
501 wxPrintf( wxT(" %d %d %d %d\n"), (int)gdk_event->area.x,
502 (int)gdk_event->area.y,
503 (int)gdk_event->area.width,
504 (int)gdk_event->area.height );
505 }
506 #endif
507
508 win->GetUpdateRegion().Union( gdk_event->area.x,
509 gdk_event->area.y,
510 gdk_event->area.width,
511 gdk_event->area.height );
512
513 // Actual redrawing takes place in idle time.
514 win->GtkUpdate();
515
516 #ifdef __WXGTK20__
517
518 (* GTK_WIDGET_CLASS (pizza_parent_class)->expose_event) (widget, gdk_event);
519
520 #endif
521
522 return TRUE;
523 }
524
525 //-----------------------------------------------------------------------------
526 // "event" of m_wxwindow
527 //-----------------------------------------------------------------------------
528
529 // GTK thinks it is clever and filters out a certain amount of "unneeded"
530 // expose events. We need them, of course, so we override the main event
531 // procedure in GtkWidget by giving our own handler for all system events.
532 // There, we look for expose events ourselves whereas all other events are
533 // handled normally.
534
535 gint gtk_window_event_event_callback( GtkWidget *widget,
536 GdkEventExpose *event,
537 wxWindow *win )
538 {
539 if (event->type == GDK_EXPOSE)
540 {
541 gint ret = gtk_window_expose_callback( widget, event, win );
542 return ret;
543 }
544
545 return FALSE;
546 }
547
548 //-----------------------------------------------------------------------------
549 // "draw" of m_wxwindow
550 //-----------------------------------------------------------------------------
551
552 #ifndef __WXGTK20__
553
554 // This callback is a complete replacement of the gtk_pizza_draw() function,
555 // which is disabled.
556
557 static void gtk_window_draw_callback( GtkWidget *widget,
558 GdkRectangle *rect,
559 wxWindow *win )
560 {
561 DEBUG_MAIN_THREAD
562
563 if (g_isIdle)
564 wxapp_install_idle_handler();
565
566 // The wxNO_FULL_REPAINT_ON_RESIZE flag only works if
567 // there are no child windows.
568 if ((win->HasFlag(wxNO_FULL_REPAINT_ON_RESIZE)) &&
569 (win->GetChildren().GetCount() == 0))
570 {
571 return;
572 }
573
574 #if 0
575 if (win->GetName())
576 {
577 wxPrintf( wxT("OnDraw from ") );
578 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
579 wxPrintf( win->GetClassInfo()->GetClassName() );
580 wxPrintf( wxT(" %d %d %d %d\n"), (int)rect->x,
581 (int)rect->y,
582 (int)rect->width,
583 (int)rect->height );
584 }
585 #endif
586
587 #ifndef __WXUNIVERSAL__
588 GtkPizza *pizza = GTK_PIZZA (widget);
589
590 if (win->GetThemeEnabled())
591 {
592 wxWindow *parent = win->GetParent();
593 while (parent && !parent->IsTopLevel())
594 parent = parent->GetParent();
595 if (!parent)
596 parent = win;
597
598 gtk_paint_flat_box (parent->m_widget->style,
599 pizza->bin_window,
600 GTK_STATE_NORMAL,
601 GTK_SHADOW_NONE,
602 rect,
603 parent->m_widget,
604 (char *)"base",
605 0, 0, -1, -1);
606 }
607
608
609 if (!(GTK_WIDGET_APP_PAINTABLE (widget)) &&
610 (pizza->clear_on_draw))
611 {
612 gdk_window_clear_area( pizza->bin_window,
613 rect->x, rect->y, rect->width, rect->height);
614 }
615 #endif
616
617 win->GetUpdateRegion().Union( rect->x, rect->y, rect->width, rect->height );
618
619 // Actual redrawing takes place in idle time.
620
621 win->GtkUpdate();
622
623 #ifndef __WXUNIVERSAL__
624 // Redraw child widgets
625 GList *children = pizza->children;
626 while (children)
627 {
628 GtkPizzaChild *child = (GtkPizzaChild*) children->data;
629 children = children->next;
630
631 GdkRectangle child_area;
632 if (gtk_widget_intersect (child->widget, rect, &child_area))
633 {
634 gtk_widget_draw (child->widget, &child_area /* (GdkRectangle*) NULL*/ );
635 }
636 }
637 #endif
638 }
639
640 #endif
641
642 //-----------------------------------------------------------------------------
643 // "key_press_event" from any window
644 //-----------------------------------------------------------------------------
645
646 // set WXTRACE to this to see the key event codes on the console
647 #define TRACE_KEYS _T("keyevent")
648
649 // translates an X key symbol to WXK_XXX value
650 //
651 // if isChar is true it means that the value returned will be used for EVT_CHAR
652 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
653 // for example, while if it is false it means that the value is going to be
654 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
655 // WXK_NUMPAD_DIVIDE
656 static long wxTranslateKeySymToWXKey(KeySym keysym, bool isChar)
657 {
658 long key_code;
659
660 switch ( keysym )
661 {
662 // Shift, Control and Alt don't generate the CHAR events at all
663 case GDK_Shift_L:
664 case GDK_Shift_R:
665 key_code = isChar ? 0 : WXK_SHIFT;
666 break;
667 case GDK_Control_L:
668 case GDK_Control_R:
669 key_code = isChar ? 0 : WXK_CONTROL;
670 break;
671 case GDK_Meta_L:
672 case GDK_Meta_R:
673 case GDK_Alt_L:
674 case GDK_Alt_R:
675 case GDK_Super_L:
676 case GDK_Super_R:
677 key_code = isChar ? 0 : WXK_ALT;
678 break;
679
680 // neither do the toggle modifies
681 case GDK_Scroll_Lock:
682 key_code = isChar ? 0 : WXK_SCROLL;
683 break;
684
685 case GDK_Caps_Lock:
686 key_code = isChar ? 0 : WXK_CAPITAL;
687 break;
688
689 case GDK_Num_Lock:
690 key_code = isChar ? 0 : WXK_NUMLOCK;
691 break;
692
693
694 // various other special keys
695 case GDK_Menu:
696 key_code = WXK_MENU;
697 break;
698
699 case GDK_Help:
700 key_code = WXK_HELP;
701 break;
702
703 case GDK_BackSpace:
704 key_code = WXK_BACK;
705 break;
706
707 case GDK_ISO_Left_Tab:
708 case GDK_Tab:
709 key_code = WXK_TAB;
710 break;
711
712 case GDK_Linefeed:
713 case GDK_Return:
714 key_code = WXK_RETURN;
715 break;
716
717 case GDK_Clear:
718 key_code = WXK_CLEAR;
719 break;
720
721 case GDK_Pause:
722 key_code = WXK_PAUSE;
723 break;
724
725 case GDK_Select:
726 key_code = WXK_SELECT;
727 break;
728
729 case GDK_Print:
730 key_code = WXK_PRINT;
731 break;
732
733 case GDK_Execute:
734 key_code = WXK_EXECUTE;
735 break;
736
737 case GDK_Escape:
738 key_code = WXK_ESCAPE;
739 break;
740
741 // cursor and other extended keyboard keys
742 case GDK_Delete:
743 key_code = WXK_DELETE;
744 break;
745
746 case GDK_Home:
747 key_code = WXK_HOME;
748 break;
749
750 case GDK_Left:
751 key_code = WXK_LEFT;
752 break;
753
754 case GDK_Up:
755 key_code = WXK_UP;
756 break;
757
758 case GDK_Right:
759 key_code = WXK_RIGHT;
760 break;
761
762 case GDK_Down:
763 key_code = WXK_DOWN;
764 break;
765
766 case GDK_Prior: // == GDK_Page_Up
767 key_code = WXK_PRIOR;
768 break;
769
770 case GDK_Next: // == GDK_Page_Down
771 key_code = WXK_NEXT;
772 break;
773
774 case GDK_End:
775 key_code = WXK_END;
776 break;
777
778 case GDK_Begin:
779 key_code = WXK_HOME;
780 break;
781
782 case GDK_Insert:
783 key_code = WXK_INSERT;
784 break;
785
786
787 // numpad keys
788 case GDK_KP_0:
789 case GDK_KP_1:
790 case GDK_KP_2:
791 case GDK_KP_3:
792 case GDK_KP_4:
793 case GDK_KP_5:
794 case GDK_KP_6:
795 case GDK_KP_7:
796 case GDK_KP_8:
797 case GDK_KP_9:
798 key_code = (isChar ? '0' : WXK_NUMPAD0) + keysym - GDK_KP_0;
799 break;
800
801 case GDK_KP_Space:
802 key_code = isChar ? ' ' : WXK_NUMPAD_SPACE;
803 break;
804
805 case GDK_KP_Tab:
806 key_code = isChar ? WXK_TAB : WXK_NUMPAD_TAB;
807 break;
808
809 case GDK_KP_Enter:
810 key_code = isChar ? WXK_RETURN : WXK_NUMPAD_ENTER;
811 break;
812
813 case GDK_KP_F1:
814 key_code = isChar ? WXK_F1 : WXK_NUMPAD_F1;
815 break;
816
817 case GDK_KP_F2:
818 key_code = isChar ? WXK_F2 : WXK_NUMPAD_F2;
819 break;
820
821 case GDK_KP_F3:
822 key_code = isChar ? WXK_F3 : WXK_NUMPAD_F3;
823 break;
824
825 case GDK_KP_F4:
826 key_code = isChar ? WXK_F4 : WXK_NUMPAD_F4;
827 break;
828
829 case GDK_KP_Home:
830 key_code = isChar ? WXK_HOME : WXK_NUMPAD_HOME;
831 break;
832
833 case GDK_KP_Left:
834 key_code = isChar ? WXK_LEFT : WXK_NUMPAD_LEFT;
835 break;
836
837 case GDK_KP_Up:
838 key_code = isChar ? WXK_UP : WXK_NUMPAD_UP;
839 break;
840
841 case GDK_KP_Right:
842 key_code = isChar ? WXK_RIGHT : WXK_NUMPAD_RIGHT;
843 break;
844
845 case GDK_KP_Down:
846 key_code = isChar ? WXK_DOWN : WXK_NUMPAD_DOWN;
847 break;
848
849 case GDK_KP_Prior: // == GDK_KP_Page_Up
850 key_code = isChar ? WXK_PRIOR : WXK_NUMPAD_PRIOR;
851 break;
852
853 case GDK_KP_Next: // == GDK_KP_Page_Down
854 key_code = isChar ? WXK_NEXT : WXK_NUMPAD_NEXT;
855 break;
856
857 case GDK_KP_End:
858 key_code = isChar ? WXK_END : WXK_NUMPAD_END;
859 break;
860
861 case GDK_KP_Begin:
862 key_code = isChar ? WXK_HOME : WXK_NUMPAD_BEGIN;
863 break;
864
865 case GDK_KP_Insert:
866 key_code = isChar ? WXK_INSERT : WXK_NUMPAD_INSERT;
867 break;
868
869 case GDK_KP_Delete:
870 key_code = isChar ? WXK_DELETE : WXK_NUMPAD_DELETE;
871 break;
872
873 case GDK_KP_Equal:
874 key_code = isChar ? '=' : WXK_NUMPAD_EQUAL;
875 break;
876
877 case GDK_KP_Multiply:
878 key_code = isChar ? '*' : WXK_NUMPAD_MULTIPLY;
879 break;
880
881 case GDK_KP_Add:
882 key_code = isChar ? '+' : WXK_NUMPAD_ADD;
883 break;
884
885 case GDK_KP_Separator:
886 // FIXME: what is this?
887 key_code = isChar ? '.' : WXK_NUMPAD_SEPARATOR;
888 break;
889
890 case GDK_KP_Subtract:
891 key_code = isChar ? '-' : WXK_NUMPAD_SUBTRACT;
892 break;
893
894 case GDK_KP_Decimal:
895 key_code = isChar ? '.' : WXK_NUMPAD_DECIMAL;
896 break;
897
898 case GDK_KP_Divide:
899 key_code = isChar ? '/' : WXK_NUMPAD_DIVIDE;
900 break;
901
902
903 // function keys
904 case GDK_F1:
905 case GDK_F2:
906 case GDK_F3:
907 case GDK_F4:
908 case GDK_F5:
909 case GDK_F6:
910 case GDK_F7:
911 case GDK_F8:
912 case GDK_F9:
913 case GDK_F10:
914 case GDK_F11:
915 case GDK_F12:
916 key_code = WXK_F1 + keysym - GDK_F1;
917 break;
918
919 default:
920 key_code = 0;
921 }
922
923 return key_code;
924 }
925
926 static inline bool wxIsAsciiKeysym(KeySym ks)
927 {
928 return ks < 256;
929 }
930
931 static bool
932 wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
933 wxWindowGTK *win,
934 GdkEventKey *gdk_event)
935 {
936 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
937 // but only event->keyval which is quite useless to us, so remember
938 // the last character from GDK_KEY_PRESS and reuse it as last resort
939 //
940 // NB: should be MT-safe as we're always called from the main thread only
941 static struct
942 {
943 KeySym keysym;
944 long keycode;
945 } s_lastKeyPress = { 0, 0 };
946
947 KeySym keysym = gdk_event->keyval;
948
949 wxLogTrace(TRACE_KEYS, _T("Key %s event: keysym = %d"),
950 event.GetEventType() == wxEVT_KEY_UP ? _T("release")
951 : _T("press"),
952 keysym);
953
954 long key_code = wxTranslateKeySymToWXKey(keysym, FALSE /* !isChar */);
955
956 if ( !key_code )
957 {
958 // do we have the translation or is it a plain ASCII character?
959 if ( (gdk_event->length == 1) || wxIsAsciiKeysym(keysym) )
960 {
961 // we should use keysym if it is ASCII as X does some translations
962 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
963 // which we don't want here (but which we do use for OnChar())
964 if ( !wxIsAsciiKeysym(keysym) )
965 {
966 keysym = (KeySym)gdk_event->string[0];
967 }
968
969 // we want to always get the same key code when the same key is
970 // pressed regardless of the state of the modifies, i.e. on a
971 // standard US keyboard pressing '5' or '%' ('5' key with
972 // Shift) should result in the same key code in OnKeyDown():
973 // '5' (although OnChar() will get either '5' or '%').
974 //
975 // to do it we first translate keysym to keycode (== scan code)
976 // and then back but always using the lower register
977 Display *dpy = (Display *)wxGetDisplay();
978 KeyCode keycode = XKeysymToKeycode(dpy, keysym);
979
980 wxLogTrace(TRACE_KEYS, _T("\t-> keycode %d"), keycode);
981
982 KeySym keysymNormalized = XKeycodeToKeysym(dpy, keycode, 0);
983
984 // use the normalized, i.e. lower register, keysym if we've
985 // got one
986 key_code = keysymNormalized ? keysymNormalized : keysym;
987
988 // as explained above, we want to have lower register key codes
989 // normally but for the letter keys we want to have the upper ones
990 //
991 // NB: don't use XConvertCase() here, we want to do it for letters
992 // only
993 key_code = toupper(key_code);
994 }
995 else // non ASCII key, what to do?
996 {
997 // by default, ignore it
998 key_code = 0;
999
1000 // but if we have cached information from the last KEY_PRESS
1001 if ( gdk_event->type == GDK_KEY_RELEASE )
1002 {
1003 // then reuse it
1004 if ( keysym == s_lastKeyPress.keysym )
1005 {
1006 key_code = s_lastKeyPress.keycode;
1007 }
1008 }
1009 }
1010
1011 if ( gdk_event->type == GDK_KEY_PRESS )
1012 {
1013 // remember it to be reused for KEY_UP event later
1014 s_lastKeyPress.keysym = keysym;
1015 s_lastKeyPress.keycode = key_code;
1016 }
1017 }
1018
1019 wxLogTrace(TRACE_KEYS, _T("\t-> wxKeyCode %d"), key_code);
1020
1021 // sending unknown key events doesn't really make sense
1022 if ( !key_code )
1023 return FALSE;
1024
1025 // now fill all the other fields
1026 int x = 0;
1027 int y = 0;
1028 GdkModifierType state;
1029 if (gdk_event->window)
1030 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
1031
1032 event.SetTimestamp( gdk_event->time );
1033 event.m_shiftDown = (gdk_event->state & GDK_SHIFT_MASK) != 0;
1034 event.m_controlDown = (gdk_event->state & GDK_CONTROL_MASK) != 0;
1035 event.m_altDown = (gdk_event->state & GDK_MOD1_MASK) != 0;
1036 event.m_metaDown = (gdk_event->state & GDK_MOD2_MASK) != 0;
1037 event.m_keyCode = key_code;
1038 event.m_scanCode = gdk_event->keyval;
1039 event.m_rawCode = (wxUint32) gdk_event->keyval;
1040 event.m_rawFlags = 0;
1041 event.m_x = x;
1042 event.m_y = y;
1043 event.SetEventObject( win );
1044
1045 return TRUE;
1046 }
1047
1048 static gint gtk_window_key_press_callback( GtkWidget *widget,
1049 GdkEventKey *gdk_event,
1050 wxWindow *win )
1051 {
1052 DEBUG_MAIN_THREAD
1053
1054 if (g_isIdle)
1055 wxapp_install_idle_handler();
1056
1057 if (!win->m_hasVMT)
1058 return FALSE;
1059 if (g_blockEventsOnDrag)
1060 return FALSE;
1061
1062 wxKeyEvent event( wxEVT_KEY_DOWN );
1063 if ( !wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
1064 {
1065 // unknown key pressed, ignore (the event would be useless anyhow)
1066 return FALSE;
1067 }
1068
1069 bool ret = win->GetEventHandler()->ProcessEvent( event );
1070
1071 #if wxUSE_ACCEL
1072 if (!ret)
1073 {
1074 wxWindowGTK *ancestor = win;
1075 while (ancestor)
1076 {
1077 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1078 if (command != -1)
1079 {
1080 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1081 ret = ancestor->GetEventHandler()->ProcessEvent( command_event );
1082 break;
1083 }
1084 if (ancestor->IsTopLevel())
1085 break;
1086 ancestor = ancestor->GetParent();
1087 }
1088 }
1089 #endif // wxUSE_ACCEL
1090
1091 /* Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
1092 will only be sent if it is not in an accelerator table. */
1093 if ( !ret )
1094 {
1095 KeySym keysym = gdk_event->keyval;
1096 long key_code = wxTranslateKeySymToWXKey(keysym, TRUE /* isChar */);
1097 if ( !key_code )
1098 {
1099 if ( gdk_event->length == 1 )
1100 {
1101 key_code = (unsigned char)gdk_event->string[0];
1102 }
1103 else if ( wxIsAsciiKeysym(keysym) )
1104 {
1105 // ASCII key
1106 key_code = (unsigned char)keysym;
1107 }
1108 }
1109
1110 if ( key_code )
1111 {
1112 wxLogTrace(TRACE_KEYS, _T("Char event: %ld"), key_code);
1113
1114 // reuse the same event object, just change its type and use the
1115 // translated keycode instead of the raw one
1116 event.SetEventType(wxEVT_CHAR);
1117 event.m_keyCode = key_code;
1118
1119 ret = win->GetEventHandler()->ProcessEvent( event );
1120 }
1121 }
1122
1123 /* win is a control: tab can be propagated up */
1124 if ( !ret &&
1125 ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) &&
1126 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1127 // have this style, yet choose not to process this particular TAB in which
1128 // case TAB must still work as a navigational character
1129 #if 0
1130 !win->HasFlag(wxTE_PROCESS_TAB) &&
1131 #endif // 0
1132 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1133 {
1134 wxNavigationKeyEvent new_event;
1135 new_event.SetEventObject( win->GetParent() );
1136 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
1137 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
1138 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1139 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
1140 new_event.SetCurrentFocus( win );
1141 ret = win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1142 }
1143
1144 /* generate wxID_CANCEL if <esc> has been pressed (typically in dialogs) */
1145 if ( !ret &&
1146 (gdk_event->keyval == GDK_Escape) )
1147 {
1148 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1149 new_event.SetEventObject( win );
1150 ret = win->GetEventHandler()->ProcessEvent( new_event );
1151 }
1152
1153 /* Doesn't work. */
1154 #if 0 // (GTK_MINOR_VERSION > 0)
1155 /* Pressing F10 will activate the menu bar of the top frame. */
1156 if ( (!ret) &&
1157 (gdk_event->keyval == GDK_F10) )
1158 {
1159 wxWindowGTK *ancestor = win;
1160 while (ancestor)
1161 {
1162 if (wxIsKindOf(ancestor,wxFrame))
1163 {
1164 wxFrame *frame = (wxFrame*) ancestor;
1165 wxMenuBar *menubar = frame->GetMenuBar();
1166 if (menubar)
1167 {
1168 wxNode *node = menubar->GetMenus().First();
1169 if (node)
1170 {
1171 wxMenu *firstMenu = (wxMenu*) node->Data();
1172 gtk_menu_item_select( GTK_MENU_ITEM(firstMenu->m_owner) );
1173 ret = TRUE;
1174 break;
1175 }
1176 }
1177 }
1178 ancestor = ancestor->GetParent();
1179 }
1180 }
1181 #endif // 0
1182
1183 if (ret)
1184 {
1185 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
1186 return TRUE;
1187 }
1188
1189 return FALSE;
1190 }
1191
1192 //-----------------------------------------------------------------------------
1193 // "key_release_event" from any window
1194 //-----------------------------------------------------------------------------
1195
1196 static gint gtk_window_key_release_callback( GtkWidget *widget,
1197 GdkEventKey *gdk_event,
1198 wxWindowGTK *win )
1199 {
1200 DEBUG_MAIN_THREAD
1201
1202 if (g_isIdle)
1203 wxapp_install_idle_handler();
1204
1205 if (!win->m_hasVMT)
1206 return FALSE;
1207
1208 if (g_blockEventsOnDrag)
1209 return FALSE;
1210
1211 wxKeyEvent event( wxEVT_KEY_UP );
1212 if ( !wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
1213 {
1214 // unknown key pressed, ignore (the event would be useless anyhow
1215 return FALSE;
1216 }
1217
1218 if ( !win->GetEventHandler()->ProcessEvent( event ) )
1219 return FALSE;
1220
1221 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_release_event" );
1222 return TRUE;
1223 }
1224
1225 // ============================================================================
1226 // the mouse events
1227 // ============================================================================
1228
1229 // init wxMouseEvent with the info from gdk_event
1230 #define InitMouseEvent(win, event, gdk_event) \
1231 { \
1232 event.SetTimestamp( gdk_event->time ); \
1233 event.m_shiftDown = (gdk_event->state & GDK_SHIFT_MASK); \
1234 event.m_controlDown = (gdk_event->state & GDK_CONTROL_MASK); \
1235 event.m_altDown = (gdk_event->state & GDK_MOD1_MASK); \
1236 event.m_metaDown = (gdk_event->state & GDK_MOD2_MASK); \
1237 event.m_leftDown = (gdk_event->state & GDK_BUTTON1_MASK); \
1238 event.m_middleDown = (gdk_event->state & GDK_BUTTON2_MASK); \
1239 event.m_rightDown = (gdk_event->state & GDK_BUTTON3_MASK); \
1240 \
1241 wxPoint pt = win->GetClientAreaOrigin(); \
1242 event.m_x = (wxCoord)gdk_event->x - pt.x; \
1243 event.m_y = (wxCoord)gdk_event->y - pt.y; \
1244 }
1245
1246 // ----------------------------------------------------------------------------
1247 // mouse event processing helper
1248 // ----------------------------------------------------------------------------
1249
1250 static void AdjustEventButtonState(wxMouseEvent& event)
1251 {
1252 // GDK reports the old state of the button for a button press event, but
1253 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1254 // for a LEFT_DOWN event, not FALSE, so we will invert
1255 // left/right/middleDown for the corresponding click events
1256
1257 if ((event.GetEventType() == wxEVT_LEFT_DOWN) ||
1258 (event.GetEventType() == wxEVT_LEFT_DCLICK) ||
1259 (event.GetEventType() == wxEVT_LEFT_UP))
1260 {
1261 event.m_leftDown = !event.m_leftDown;
1262 return;
1263 }
1264
1265 if ((event.GetEventType() == wxEVT_MIDDLE_DOWN) ||
1266 (event.GetEventType() == wxEVT_MIDDLE_DCLICK) ||
1267 (event.GetEventType() == wxEVT_MIDDLE_UP))
1268 {
1269 event.m_middleDown = !event.m_middleDown;
1270 return;
1271 }
1272
1273 if ((event.GetEventType() == wxEVT_RIGHT_DOWN) ||
1274 (event.GetEventType() == wxEVT_RIGHT_DCLICK) ||
1275 (event.GetEventType() == wxEVT_RIGHT_UP))
1276 {
1277 event.m_rightDown = !event.m_rightDown;
1278 return;
1279 }
1280 }
1281
1282 //-----------------------------------------------------------------------------
1283 // "button_press_event"
1284 //-----------------------------------------------------------------------------
1285
1286 static gint gtk_window_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxWindowGTK *win )
1287 {
1288 DEBUG_MAIN_THREAD
1289
1290 if (g_isIdle)
1291 wxapp_install_idle_handler();
1292
1293 /*
1294 wxPrintf( wxT("1) OnButtonPress from ") );
1295 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1296 wxPrintf( win->GetClassInfo()->GetClassName() );
1297 wxPrintf( wxT(".\n") );
1298 */
1299 if (!win->m_hasVMT) return FALSE;
1300 if (g_blockEventsOnDrag) return TRUE;
1301 if (g_blockEventsOnScroll) return TRUE;
1302
1303 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1304
1305 if (win->m_wxwindow && (g_focusWindow != win) && win->AcceptsFocus())
1306 {
1307 gtk_widget_grab_focus( win->m_wxwindow );
1308 /*
1309 wxPrintf( wxT("GrabFocus from ") );
1310 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1311 wxPrintf( win->GetClassInfo()->GetClassName() );
1312 wxPrintf( wxT(".\n") );
1313 */
1314 }
1315
1316 wxEventType event_type = wxEVT_NULL;
1317
1318 if (gdk_event->button == 1)
1319 {
1320 switch (gdk_event->type)
1321 {
1322 case GDK_BUTTON_PRESS: event_type = wxEVT_LEFT_DOWN; break;
1323 case GDK_2BUTTON_PRESS: event_type = wxEVT_LEFT_DCLICK; break;
1324 default: break;
1325 }
1326 }
1327 else if (gdk_event->button == 2)
1328 {
1329 switch (gdk_event->type)
1330 {
1331 case GDK_BUTTON_PRESS: event_type = wxEVT_MIDDLE_DOWN; break;
1332 case GDK_2BUTTON_PRESS: event_type = wxEVT_MIDDLE_DCLICK; break;
1333 default: break;
1334 }
1335 }
1336 else if (gdk_event->button == 3)
1337 {
1338 switch (gdk_event->type)
1339 {
1340 case GDK_BUTTON_PRESS: event_type = wxEVT_RIGHT_DOWN; break;
1341 case GDK_2BUTTON_PRESS: event_type = wxEVT_RIGHT_DCLICK; break;
1342 default: break;
1343 }
1344 }
1345
1346 if ( event_type == wxEVT_NULL )
1347 {
1348 // unknown mouse button or click type
1349 return FALSE;
1350 }
1351
1352 wxMouseEvent event( event_type );
1353 InitMouseEvent( win, event, gdk_event );
1354
1355 AdjustEventButtonState(event);
1356
1357 // wxListBox actually get mouse events from the item
1358
1359 if (win->m_isListBox)
1360 {
1361 event.m_x += widget->allocation.x;
1362 event.m_y += widget->allocation.y;
1363 }
1364
1365 // Some control don't have their own X window and thus cannot get
1366 // any events.
1367
1368 if (!g_captureWindow)
1369 {
1370 wxCoord x = event.m_x;
1371 wxCoord y = event.m_y;
1372 if (win->m_wxwindow)
1373 {
1374 GtkPizza *pizza = GTK_PIZZA(win->m_wxwindow);
1375 x += pizza->xoffset;
1376 y += pizza->yoffset;
1377 }
1378
1379 wxNode *node = win->GetChildren().First();
1380 while (node)
1381 {
1382 wxWindowGTK *child = (wxWindowGTK*)node->Data();
1383
1384 node = node->Next();
1385 if (!child->IsShown())
1386 continue;
1387
1388 if (child->m_isStaticBox)
1389 {
1390 // wxStaticBox is transparent in the box itself
1391 int xx1 = child->m_x;
1392 int yy1 = child->m_y;
1393 int xx2 = child->m_x + child->m_width;
1394 int yy2 = child->m_x + child->m_height;
1395
1396 // left
1397 if (((x >= xx1) && (x <= xx1+10) && (y >= yy1) && (y <= yy2)) ||
1398 // right
1399 ((x >= xx2-10) && (x <= xx2) && (y >= yy1) && (y <= yy2)) ||
1400 // top
1401 ((x >= xx1) && (x <= xx2) && (y >= yy1) && (y <= yy1+10)) ||
1402 // bottom
1403 ((x >= xx1) && (x <= xx2) && (y >= yy2-1) && (y <= yy2)))
1404 {
1405 win = child;
1406 event.m_x -= child->m_x;
1407 event.m_y -= child->m_y;
1408 break;
1409 }
1410
1411 }
1412 else
1413 {
1414 if ((child->m_wxwindow == (GtkWidget*) NULL) &&
1415 (child->m_x <= x) &&
1416 (child->m_y <= y) &&
1417 (child->m_x+child->m_width >= x) &&
1418 (child->m_y+child->m_height >= y))
1419 {
1420 win = child;
1421 event.m_x -= child->m_x;
1422 event.m_y -= child->m_y;
1423 break;
1424 }
1425 }
1426 }
1427 }
1428
1429 event.SetEventObject( win );
1430
1431 gs_timeLastClick = gdk_event->time;
1432
1433 /*
1434 wxPrintf( wxT("2) OnButtonPress from ") );
1435 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1436 wxPrintf( win->GetClassInfo()->GetClassName() );
1437 wxPrintf( wxT(".\n") );
1438 */
1439
1440 if (win->GetEventHandler()->ProcessEvent( event ))
1441 {
1442 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "button_press_event" );
1443 return TRUE;
1444 }
1445
1446 return FALSE;
1447 }
1448
1449 //-----------------------------------------------------------------------------
1450 // "button_release_event"
1451 //-----------------------------------------------------------------------------
1452
1453 static gint gtk_window_button_release_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxWindowGTK *win )
1454 {
1455 DEBUG_MAIN_THREAD
1456
1457 if (g_isIdle)
1458 wxapp_install_idle_handler();
1459
1460 if (!win->m_hasVMT) return FALSE;
1461 if (g_blockEventsOnDrag) return FALSE;
1462 if (g_blockEventsOnScroll) return FALSE;
1463
1464 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1465
1466 /*
1467 printf( "OnButtonRelease from " );
1468 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1469 printf( win->GetClassInfo()->GetClassName() );
1470 printf( ".\n" );
1471 */
1472
1473 wxEventType event_type = wxEVT_NULL;
1474
1475 switch (gdk_event->button)
1476 {
1477 case 1: event_type = wxEVT_LEFT_UP; break;
1478 case 2: event_type = wxEVT_MIDDLE_UP; break;
1479 case 3: event_type = wxEVT_RIGHT_UP; break;
1480 default: return FALSE;
1481 }
1482
1483 wxMouseEvent event( event_type );
1484 InitMouseEvent( win, event, gdk_event );
1485
1486 AdjustEventButtonState(event);
1487
1488 // wxListBox actually get mouse events from the item
1489
1490 if (win->m_isListBox)
1491 {
1492 event.m_x += widget->allocation.x;
1493 event.m_y += widget->allocation.y;
1494 }
1495
1496 // Some control don't have their own X window and thus cannot get
1497 // any events.
1498
1499 if (!g_captureWindow)
1500 {
1501 wxCoord x = event.m_x;
1502 wxCoord y = event.m_y;
1503 if (win->m_wxwindow)
1504 {
1505 GtkPizza *pizza = GTK_PIZZA(win->m_wxwindow);
1506 x += pizza->xoffset;
1507 y += pizza->yoffset;
1508 }
1509
1510 wxNode *node = win->GetChildren().First();
1511 while (node)
1512 {
1513 wxWindowGTK *child = (wxWindowGTK*)node->Data();
1514
1515 node = node->Next();
1516 if (!child->IsShown())
1517 continue;
1518
1519 if (child->m_isStaticBox)
1520 {
1521 // wxStaticBox is transparent in the box itself
1522 int xx1 = child->m_x;
1523 int yy1 = child->m_y;
1524 int xx2 = child->m_x + child->m_width;
1525 int yy2 = child->m_x + child->m_height;
1526
1527 // left
1528 if (((x >= xx1) && (x <= xx1+10) && (y >= yy1) && (y <= yy2)) ||
1529 // right
1530 ((x >= xx2-10) && (x <= xx2) && (y >= yy1) && (y <= yy2)) ||
1531 // top
1532 ((x >= xx1) && (x <= xx2) && (y >= yy1) && (y <= yy1+10)) ||
1533 // bottom
1534 ((x >= xx1) && (x <= xx2) && (y >= yy2-1) && (y <= yy2)))
1535 {
1536 win = child;
1537 event.m_x -= child->m_x;
1538 event.m_y -= child->m_y;
1539 break;
1540 }
1541
1542 }
1543 else
1544 {
1545 if ((child->m_wxwindow == (GtkWidget*) NULL) &&
1546 (child->m_x <= x) &&
1547 (child->m_y <= y) &&
1548 (child->m_x+child->m_width >= x) &&
1549 (child->m_y+child->m_height >= y))
1550 {
1551 win = child;
1552 event.m_x -= child->m_x;
1553 event.m_y -= child->m_y;
1554 break;
1555 }
1556 }
1557 }
1558 }
1559
1560 event.SetEventObject( win );
1561
1562 if (win->GetEventHandler()->ProcessEvent( event ))
1563 {
1564 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "button_release_event" );
1565 return TRUE;
1566 }
1567
1568 return FALSE;
1569 }
1570
1571 //-----------------------------------------------------------------------------
1572 // "motion_notify_event"
1573 //-----------------------------------------------------------------------------
1574
1575 static gint gtk_window_motion_notify_callback( GtkWidget *widget,
1576 GdkEventMotion *gdk_event,
1577 wxWindowGTK *win )
1578 {
1579 DEBUG_MAIN_THREAD
1580
1581 if (g_isIdle)
1582 wxapp_install_idle_handler();
1583
1584 if (!win->m_hasVMT) return FALSE;
1585 if (g_blockEventsOnDrag) return FALSE;
1586 if (g_blockEventsOnScroll) return FALSE;
1587
1588 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1589
1590 if (gdk_event->is_hint)
1591 {
1592 int x = 0;
1593 int y = 0;
1594 GdkModifierType state;
1595 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
1596 gdk_event->x = x;
1597 gdk_event->y = y;
1598 }
1599
1600 /*
1601 printf( "OnMotion from " );
1602 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1603 printf( win->GetClassInfo()->GetClassName() );
1604 printf( ".\n" );
1605 */
1606
1607 wxMouseEvent event( wxEVT_MOTION );
1608 InitMouseEvent(win, event, gdk_event);
1609
1610 if ( g_captureWindow )
1611 {
1612 // synthetize a mouse enter or leave event if needed
1613 GdkWindow *winUnderMouse = gdk_window_at_pointer(NULL, NULL);
1614 bool hasMouse = winUnderMouse == gdk_event->window;
1615 if ( hasMouse != g_captureWindowHasMouse )
1616 {
1617 // the mouse changed window
1618 g_captureWindowHasMouse = hasMouse;
1619
1620 wxMouseEvent event(g_captureWindowHasMouse ? wxEVT_ENTER_WINDOW
1621 : wxEVT_LEAVE_WINDOW);
1622 InitMouseEvent(win, event, gdk_event);
1623 event.SetEventObject(win);
1624 win->GetEventHandler()->ProcessEvent(event);
1625 }
1626 }
1627 else // no capture
1628 {
1629 // Some control don't have their own X window and thus cannot get
1630 // any events.
1631
1632 wxCoord x = event.m_x;
1633 wxCoord y = event.m_y;
1634 if (win->m_wxwindow)
1635 {
1636 GtkPizza *pizza = GTK_PIZZA(win->m_wxwindow);
1637 x += pizza->xoffset;
1638 y += pizza->yoffset;
1639 }
1640
1641 wxNode *node = win->GetChildren().First();
1642 while (node)
1643 {
1644 wxWindowGTK *child = (wxWindowGTK*)node->Data();
1645
1646 node = node->Next();
1647 if (!child->IsShown())
1648 continue;
1649
1650 if (child->m_isStaticBox)
1651 {
1652 // wxStaticBox is transparent in the box itself
1653 int xx1 = child->m_x;
1654 int yy1 = child->m_y;
1655 int xx2 = child->m_x + child->m_width;
1656 int yy2 = child->m_x + child->m_height;
1657
1658 // left
1659 if (((x >= xx1) && (x <= xx1+10) && (y >= yy1) && (y <= yy2)) ||
1660 // right
1661 ((x >= xx2-10) && (x <= xx2) && (y >= yy1) && (y <= yy2)) ||
1662 // top
1663 ((x >= xx1) && (x <= xx2) && (y >= yy1) && (y <= yy1+10)) ||
1664 // bottom
1665 ((x >= xx1) && (x <= xx2) && (y >= yy2-1) && (y <= yy2)))
1666 {
1667 win = child;
1668 event.m_x -= child->m_x;
1669 event.m_y -= child->m_y;
1670 break;
1671 }
1672
1673 }
1674 else
1675 {
1676 if ((child->m_wxwindow == (GtkWidget*) NULL) &&
1677 (child->m_x <= x) &&
1678 (child->m_y <= y) &&
1679 (child->m_x+child->m_width >= x) &&
1680 (child->m_y+child->m_height >= y))
1681 {
1682 win = child;
1683 event.m_x -= child->m_x;
1684 event.m_y -= child->m_y;
1685 break;
1686 }
1687 }
1688 }
1689 }
1690
1691 event.SetEventObject( win );
1692
1693 if (win->GetEventHandler()->ProcessEvent( event ))
1694 {
1695 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "motion_notify_event" );
1696 return TRUE;
1697 }
1698
1699 return FALSE;
1700 }
1701
1702 //-----------------------------------------------------------------------------
1703 // "focus_in_event"
1704 //-----------------------------------------------------------------------------
1705
1706 // send the wxChildFocusEvent and wxFocusEvent, common code of
1707 // gtk_window_focus_in_callback() and SetFocus()
1708 static bool DoSendFocusEvents(wxWindow *win)
1709 {
1710 // Notify the parent keeping track of focus for the kbd navigation
1711 // purposes that we got it.
1712 wxChildFocusEvent eventChildFocus(win);
1713 (void)win->GetEventHandler()->ProcessEvent(eventChildFocus);
1714
1715 wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId());
1716 eventFocus.SetEventObject(win);
1717
1718 return win->GetEventHandler()->ProcessEvent(eventFocus);
1719 }
1720
1721 static gint gtk_window_focus_in_callback( GtkWidget *widget,
1722 GdkEvent *WXUNUSED(event),
1723 wxWindow *win )
1724 {
1725 DEBUG_MAIN_THREAD
1726
1727 if (g_isIdle)
1728 wxapp_install_idle_handler();
1729
1730 if (!win->m_hasVMT) return FALSE;
1731 if (g_blockEventsOnDrag) return FALSE;
1732
1733 switch ( g_sendActivateEvent )
1734 {
1735 case -1:
1736 // we've got focus from outside, synthetize wxActivateEvent
1737 g_sendActivateEvent = 1;
1738 break;
1739
1740 case 0:
1741 // another our window just lost focus, it was already ours before
1742 // - don't send any wxActivateEvent
1743 g_sendActivateEvent = -1;
1744 break;
1745 }
1746
1747 g_focusWindowLast =
1748 g_focusWindow = win;
1749
1750 #if 0
1751 printf( "OnSetFocus 2 from %s\n", win->GetName().c_str() );
1752 #endif
1753
1754 #ifdef HAVE_XIM
1755 if (win->m_ic)
1756 gdk_im_begin(win->m_ic, win->m_wxwindow->window);
1757 #endif
1758
1759 #if wxUSE_CARET
1760 // caret needs to be informed about focus change
1761 wxCaret *caret = win->GetCaret();
1762 if ( caret )
1763 {
1764 caret->OnSetFocus();
1765 }
1766 #endif // wxUSE_CARET
1767
1768 g_activeFrameLostFocus = FALSE;
1769
1770 wxWindowGTK *active = wxGetTopLevelParent(win);
1771 if ( active != g_activeFrame )
1772 {
1773 if ( g_activeFrame )
1774 {
1775 wxLogTrace(wxT("activate"), wxT("Deactivating frame %p (from focus_in)"), g_activeFrame);
1776 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, g_activeFrame->GetId());
1777 event.SetEventObject(g_activeFrame);
1778 g_activeFrame->GetEventHandler()->ProcessEvent(event);
1779 }
1780
1781 wxLogTrace(wxT("activate"), wxT("Activating frame %p (from focus_in)"), active);
1782 g_activeFrame = active;
1783 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, g_activeFrame->GetId());
1784 event.SetEventObject(g_activeFrame);
1785 g_activeFrame->GetEventHandler()->ProcessEvent(event);
1786
1787 // Don't send focus events in addition to activate
1788 // if (win == g_activeFrame)
1789 // return TRUE;
1790 }
1791
1792 if ( DoSendFocusEvents(win) )
1793 {
1794 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus_in_event" );
1795 return TRUE;
1796 }
1797
1798 return FALSE;
1799 }
1800
1801 //-----------------------------------------------------------------------------
1802 // "focus_out_event"
1803 //-----------------------------------------------------------------------------
1804
1805 static gint gtk_window_focus_out_callback( GtkWidget *widget, GdkEventFocus *gdk_event, wxWindowGTK *win )
1806 {
1807 DEBUG_MAIN_THREAD
1808
1809 if (g_isIdle)
1810 wxapp_install_idle_handler();
1811
1812 if (!win->m_hasVMT) return FALSE;
1813 if (g_blockEventsOnDrag) return FALSE;
1814
1815 #if 0
1816 wxLogDebug( wxT("OnKillFocus from %s"), win->GetName().c_str() );
1817 #endif
1818
1819 if ( !g_activeFrameLostFocus && g_activeFrame )
1820 {
1821 // VZ: commenting this out because it does happen (although not easy
1822 // to reproduce, I only see it when using wxMiniFrame and not
1823 // always) and makes using Mahogany quite annoying
1824 #if 0
1825 wxASSERT_MSG( wxGetTopLevelParent(win) == g_activeFrame,
1826 wxT("unfocusing window that hasn't gained focus properly") )
1827 #endif // 0
1828
1829 g_activeFrameLostFocus = TRUE;
1830 }
1831
1832 // if the focus goes out of our app alltogether, OnIdle() will send
1833 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
1834 // g_sendActivateEvent to -1
1835 g_sendActivateEvent = 0;
1836
1837 wxWindowGTK *winFocus = wxFindFocusedChild(win);
1838 if ( winFocus )
1839 win = winFocus;
1840
1841 g_focusWindow = (wxWindowGTK *)NULL;
1842
1843 #ifdef HAVE_XIM
1844 if (win->m_ic)
1845 gdk_im_end();
1846 #endif
1847
1848 #if wxUSE_CARET
1849 // caret needs to be informed about focus change
1850 wxCaret *caret = win->GetCaret();
1851 if ( caret )
1852 {
1853 caret->OnKillFocus();
1854 }
1855 #endif // wxUSE_CARET
1856
1857 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
1858 event.SetEventObject( win );
1859
1860 if (win->GetEventHandler()->ProcessEvent( event ))
1861 {
1862 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus_out_event" );
1863 return TRUE;
1864 }
1865
1866 return FALSE;
1867 }
1868
1869 //-----------------------------------------------------------------------------
1870 // "enter_notify_event"
1871 //-----------------------------------------------------------------------------
1872
1873 static gint gtk_window_enter_callback( GtkWidget *widget, GdkEventCrossing *gdk_event, wxWindowGTK *win )
1874 {
1875 DEBUG_MAIN_THREAD
1876
1877 if (g_isIdle)
1878 wxapp_install_idle_handler();
1879
1880 if (!win->m_hasVMT) return FALSE;
1881 if (g_blockEventsOnDrag) return FALSE;
1882
1883 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1884
1885 wxMouseEvent event( wxEVT_ENTER_WINDOW );
1886 event.SetTimestamp( gdk_event->time );
1887 event.SetEventObject( win );
1888
1889 int x = 0;
1890 int y = 0;
1891 GdkModifierType state = (GdkModifierType)0;
1892
1893 gdk_window_get_pointer( widget->window, &x, &y, &state );
1894
1895 InitMouseEvent(win, event, gdk_event);
1896 wxPoint pt = win->GetClientAreaOrigin();
1897 event.m_x = x + pt.x;
1898 event.m_y = y + pt.y;
1899
1900 if (win->GetEventHandler()->ProcessEvent( event ))
1901 {
1902 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "enter_notify_event" );
1903 return TRUE;
1904 }
1905
1906 return FALSE;
1907 }
1908
1909 //-----------------------------------------------------------------------------
1910 // "leave_notify_event"
1911 //-----------------------------------------------------------------------------
1912
1913 static gint gtk_window_leave_callback( GtkWidget *widget, GdkEventCrossing *gdk_event, wxWindowGTK *win )
1914 {
1915 DEBUG_MAIN_THREAD
1916
1917 if (g_isIdle)
1918 wxapp_install_idle_handler();
1919
1920 if (!win->m_hasVMT) return FALSE;
1921 if (g_blockEventsOnDrag) return FALSE;
1922
1923 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1924
1925 wxMouseEvent event( wxEVT_LEAVE_WINDOW );
1926 event.SetTimestamp( gdk_event->time );
1927 event.SetEventObject( win );
1928
1929 int x = 0;
1930 int y = 0;
1931 GdkModifierType state = (GdkModifierType)0;
1932
1933 gdk_window_get_pointer( widget->window, &x, &y, &state );
1934
1935 event.m_shiftDown = (state & GDK_SHIFT_MASK) != 0;
1936 event.m_controlDown = (state & GDK_CONTROL_MASK) != 0;
1937 event.m_altDown = (state & GDK_MOD1_MASK) != 0;
1938 event.m_metaDown = (state & GDK_MOD2_MASK) != 0;
1939 event.m_leftDown = (state & GDK_BUTTON1_MASK) != 0;
1940 event.m_middleDown = (state & GDK_BUTTON2_MASK) != 0;
1941 event.m_rightDown = (state & GDK_BUTTON3_MASK) != 0;
1942
1943 wxPoint pt = win->GetClientAreaOrigin();
1944 event.m_x = x + pt.x;
1945 event.m_y = y + pt.y;
1946
1947 if (win->GetEventHandler()->ProcessEvent( event ))
1948 {
1949 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "leave_notify_event" );
1950 return TRUE;
1951 }
1952
1953 return FALSE;
1954 }
1955
1956 //-----------------------------------------------------------------------------
1957 // "value_changed" from m_vAdjust
1958 //-----------------------------------------------------------------------------
1959
1960 static void gtk_window_vscroll_callback( GtkAdjustment *adjust,
1961 SCROLLBAR_CBACK_ARG
1962 wxWindowGTK *win )
1963 {
1964 DEBUG_MAIN_THREAD
1965
1966 if (g_isIdle)
1967 wxapp_install_idle_handler();
1968
1969 if (g_blockEventsOnDrag) return;
1970
1971 if (!win->m_hasVMT) return;
1972
1973 float diff = adjust->value - win->m_oldVerticalPos;
1974 if (fabs(diff) < 0.2) return;
1975
1976 win->m_oldVerticalPos = adjust->value;
1977
1978 wxEventType command = GtkScrollWinTypeToWx(GET_SCROLL_TYPE(win->m_widget));
1979
1980 int value = (int)(adjust->value+0.5);
1981
1982 wxScrollWinEvent event( command, value, wxVERTICAL );
1983 event.SetEventObject( win );
1984 win->GetEventHandler()->ProcessEvent( event );
1985 }
1986
1987 //-----------------------------------------------------------------------------
1988 // "value_changed" from m_hAdjust
1989 //-----------------------------------------------------------------------------
1990
1991 static void gtk_window_hscroll_callback( GtkAdjustment *adjust,
1992 SCROLLBAR_CBACK_ARG
1993 wxWindowGTK *win )
1994 {
1995 DEBUG_MAIN_THREAD
1996
1997 if (g_isIdle)
1998 wxapp_install_idle_handler();
1999
2000 if (g_blockEventsOnDrag) return;
2001 if (!win->m_hasVMT) return;
2002
2003 float diff = adjust->value - win->m_oldHorizontalPos;
2004 if (fabs(diff) < 0.2) return;
2005
2006 wxEventType command = GtkScrollWinTypeToWx(GET_SCROLL_TYPE(win->m_widget));
2007
2008 win->m_oldHorizontalPos = adjust->value;
2009
2010 int value = (int)(adjust->value+0.5);
2011
2012 wxScrollWinEvent event( command, value, wxHORIZONTAL );
2013 event.SetEventObject( win );
2014 win->GetEventHandler()->ProcessEvent( event );
2015 }
2016
2017 //-----------------------------------------------------------------------------
2018 // "button_press_event" from scrollbar
2019 //-----------------------------------------------------------------------------
2020
2021 static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
2022 GdkEventButton *gdk_event,
2023 wxWindowGTK *win)
2024 {
2025 DEBUG_MAIN_THREAD
2026
2027 if (g_isIdle)
2028 wxapp_install_idle_handler();
2029
2030
2031 g_blockEventsOnScroll = TRUE;
2032
2033 // FIXME: there is no 'slider' field in GTK+ 2.0 any more
2034 #ifndef __WXGTK20__
2035 win->m_isScrolling = (gdk_event->window == widget->slider);
2036 #endif
2037
2038 return FALSE;
2039 }
2040
2041 //-----------------------------------------------------------------------------
2042 // "button_release_event" from scrollbar
2043 //-----------------------------------------------------------------------------
2044
2045 static gint gtk_scrollbar_button_release_callback( GtkRange *widget,
2046 GdkEventButton *WXUNUSED(gdk_event),
2047 wxWindowGTK *win)
2048 {
2049 DEBUG_MAIN_THREAD
2050
2051 // don't test here as we can release the mouse while being over
2052 // a different window than the slider
2053 //
2054 // if (gdk_event->window != widget->slider) return FALSE;
2055
2056 g_blockEventsOnScroll = FALSE;
2057
2058 if (win->m_isScrolling)
2059 {
2060 wxEventType command = wxEVT_SCROLLWIN_THUMBRELEASE;
2061 int value = -1;
2062 int dir = -1;
2063
2064 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(win->m_widget);
2065 if (widget == GTK_RANGE(scrolledWindow->hscrollbar))
2066 {
2067 value = (int)(win->m_hAdjust->value+0.5);
2068 dir = wxHORIZONTAL;
2069 }
2070 if (widget == GTK_RANGE(scrolledWindow->vscrollbar))
2071 {
2072 value = (int)(win->m_vAdjust->value+0.5);
2073 dir = wxVERTICAL;
2074 }
2075
2076 wxScrollWinEvent event( command, value, dir );
2077 event.SetEventObject( win );
2078 win->GetEventHandler()->ProcessEvent( event );
2079 }
2080
2081 win->m_isScrolling = FALSE;
2082
2083 return FALSE;
2084 }
2085
2086 // ----------------------------------------------------------------------------
2087 // this wxWindowBase function is implemented here (in platform-specific file)
2088 // because it is static and so couldn't be made virtual
2089 // ----------------------------------------------------------------------------
2090
2091 wxWindow *wxWindowBase::FindFocus()
2092 {
2093 // the cast is necessary when we compile in wxUniversal mode
2094 return (wxWindow *)g_focusWindow;
2095 }
2096
2097 //-----------------------------------------------------------------------------
2098 // "realize" from m_widget
2099 //-----------------------------------------------------------------------------
2100
2101 /* We cannot set colours and fonts before the widget has
2102 been realized, so we do this directly after realization. */
2103
2104 static gint
2105 gtk_window_realized_callback( GtkWidget *WXUNUSED(m_widget), wxWindow *win )
2106 {
2107 DEBUG_MAIN_THREAD
2108
2109 if (g_isIdle)
2110 wxapp_install_idle_handler();
2111
2112 if (win->m_delayedBackgroundColour)
2113 win->GtkSetBackgroundColour( win->GetBackgroundColour() );
2114
2115 if (win->m_delayedForegroundColour)
2116 win->GtkSetForegroundColour( win->GetForegroundColour() );
2117
2118 wxWindowCreateEvent event( win );
2119 event.SetEventObject( win );
2120 win->GetEventHandler()->ProcessEvent( event );
2121
2122 return FALSE;
2123 }
2124
2125 //-----------------------------------------------------------------------------
2126 // "size_allocate"
2127 //-----------------------------------------------------------------------------
2128
2129 static
2130 void gtk_window_size_callback( GtkWidget *WXUNUSED(widget),
2131 GtkAllocation *WXUNUSED(alloc),
2132 wxWindow *win )
2133 {
2134 if (g_isIdle)
2135 wxapp_install_idle_handler();
2136
2137 if (!win->m_hasScrolling) return;
2138
2139 int client_width = 0;
2140 int client_height = 0;
2141 win->GetClientSize( &client_width, &client_height );
2142 if ((client_width == win->m_oldClientWidth) && (client_height == win->m_oldClientHeight))
2143 return;
2144
2145 win->m_oldClientWidth = client_width;
2146 win->m_oldClientHeight = client_height;
2147
2148 if (!win->m_nativeSizeEvent)
2149 {
2150 wxSizeEvent event( win->GetSize(), win->GetId() );
2151 event.SetEventObject( win );
2152 win->GetEventHandler()->ProcessEvent( event );
2153 }
2154 }
2155
2156
2157 #ifdef HAVE_XIM
2158 #define WXUNUSED_UNLESS_XIM(param) param
2159 #else
2160 #define WXUNUSED_UNLESS_XIM(param) WXUNUSED(param)
2161 #endif
2162
2163 /* Resize XIM window */
2164
2165 static
2166 void gtk_wxwindow_size_callback( GtkWidget* WXUNUSED_UNLESS_XIM(widget),
2167 GtkAllocation* WXUNUSED_UNLESS_XIM(alloc),
2168 wxWindowGTK* WXUNUSED_UNLESS_XIM(win) )
2169 {
2170 if (g_isIdle)
2171 wxapp_install_idle_handler();
2172
2173 #ifdef HAVE_XIM
2174 if (!win->m_ic)
2175 return;
2176
2177 if (gdk_ic_get_style (win->m_ic) & GDK_IM_PREEDIT_POSITION)
2178 {
2179 gint width, height;
2180
2181 gdk_window_get_size (widget->window, &width, &height);
2182 win->m_icattr->preedit_area.width = width;
2183 win->m_icattr->preedit_area.height = height;
2184 gdk_ic_set_attr (win->m_ic, win->m_icattr, GDK_IC_PREEDIT_AREA);
2185 }
2186 #endif // HAVE_XIM
2187 }
2188
2189 //-----------------------------------------------------------------------------
2190 // "realize" from m_wxwindow
2191 //-----------------------------------------------------------------------------
2192
2193 /* Initialize XIM support */
2194
2195 static gint
2196 gtk_wxwindow_realized_callback( GtkWidget * WXUNUSED_UNLESS_XIM(widget),
2197 wxWindowGTK * WXUNUSED_UNLESS_XIM(win) )
2198 {
2199 if (g_isIdle)
2200 wxapp_install_idle_handler();
2201
2202 #ifdef HAVE_XIM
2203 if (win->m_ic) return FALSE;
2204 if (!widget) return FALSE;
2205 if (!gdk_im_ready()) return FALSE;
2206
2207 win->m_icattr = gdk_ic_attr_new();
2208 if (!win->m_icattr) return FALSE;
2209
2210 gint width, height;
2211 GdkEventMask mask;
2212 GdkColormap *colormap;
2213 GdkICAttr *attr = win->m_icattr;
2214 unsigned attrmask = GDK_IC_ALL_REQ;
2215 GdkIMStyle style;
2216 GdkIMStyle supported_style = (GdkIMStyle)
2217 (GDK_IM_PREEDIT_NONE |
2218 GDK_IM_PREEDIT_NOTHING |
2219 GDK_IM_PREEDIT_POSITION |
2220 GDK_IM_STATUS_NONE |
2221 GDK_IM_STATUS_NOTHING);
2222
2223 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
2224 supported_style = (GdkIMStyle)(supported_style & ~GDK_IM_PREEDIT_POSITION);
2225
2226 attr->style = style = gdk_im_decide_style (supported_style);
2227 attr->client_window = widget->window;
2228
2229 if ((colormap = gtk_widget_get_colormap (widget)) !=
2230 gtk_widget_get_default_colormap ())
2231 {
2232 attrmask |= GDK_IC_PREEDIT_COLORMAP;
2233 attr->preedit_colormap = colormap;
2234 }
2235
2236 attrmask |= GDK_IC_PREEDIT_FOREGROUND;
2237 attrmask |= GDK_IC_PREEDIT_BACKGROUND;
2238 attr->preedit_foreground = widget->style->fg[GTK_STATE_NORMAL];
2239 attr->preedit_background = widget->style->base[GTK_STATE_NORMAL];
2240
2241 switch (style & GDK_IM_PREEDIT_MASK)
2242 {
2243 case GDK_IM_PREEDIT_POSITION:
2244 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
2245 {
2246 g_warning ("over-the-spot style requires fontset");
2247 break;
2248 }
2249
2250 gdk_window_get_size (widget->window, &width, &height);
2251
2252 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
2253 attr->spot_location.x = 0;
2254 attr->spot_location.y = height;
2255 attr->preedit_area.x = 0;
2256 attr->preedit_area.y = 0;
2257 attr->preedit_area.width = width;
2258 attr->preedit_area.height = height;
2259 attr->preedit_fontset = widget->style->font;
2260
2261 break;
2262 }
2263
2264 win->m_ic = gdk_ic_new (attr, (GdkICAttributesType)attrmask);
2265
2266 if (win->m_ic == NULL)
2267 g_warning ("Can't create input context.");
2268 else
2269 {
2270 mask = gdk_window_get_events (widget->window);
2271 mask = (GdkEventMask)(mask | gdk_ic_get_events (win->m_ic));
2272 gdk_window_set_events (widget->window, mask);
2273
2274 if (GTK_WIDGET_HAS_FOCUS(widget))
2275 gdk_im_begin (win->m_ic, widget->window);
2276 }
2277 #endif // HAVE_XIM
2278
2279 return FALSE;
2280 }
2281
2282 //-----------------------------------------------------------------------------
2283 // InsertChild for wxWindowGTK.
2284 //-----------------------------------------------------------------------------
2285
2286 /* Callback for wxWindowGTK. This very strange beast has to be used because
2287 * C++ has no virtual methods in a constructor. We have to emulate a
2288 * virtual function here as wxNotebook requires a different way to insert
2289 * a child in it. I had opted for creating a wxNotebookPage window class
2290 * which would have made this superfluous (such in the MDI window system),
2291 * but no-one was listening to me... */
2292
2293 static void wxInsertChildInWindow( wxWindowGTK* parent, wxWindowGTK* child )
2294 {
2295 /* the window might have been scrolled already, do we
2296 have to adapt the position */
2297 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
2298 child->m_x += pizza->xoffset;
2299 child->m_y += pizza->yoffset;
2300
2301 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
2302 GTK_WIDGET(child->m_widget),
2303 child->m_x,
2304 child->m_y,
2305 child->m_width,
2306 child->m_height );
2307 }
2308
2309 //-----------------------------------------------------------------------------
2310 // global functions
2311 //-----------------------------------------------------------------------------
2312
2313 wxWindow *wxGetActiveWindow()
2314 {
2315 // the cast is necessary when we compile in wxUniversal mode
2316 return (wxWindow *)g_focusWindow;
2317 }
2318
2319 //-----------------------------------------------------------------------------
2320 // wxWindowGTK
2321 //-----------------------------------------------------------------------------
2322
2323 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2324 // method
2325 #ifdef __WXUNIVERSAL__
2326 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK, wxWindowBase)
2327 #else // __WXGTK__
2328 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
2329 #endif // __WXUNIVERSAL__/__WXGTK__
2330
2331 void wxWindowGTK::Init()
2332 {
2333 // common init
2334 InitBase();
2335
2336 // GTK specific
2337 m_widget = (GtkWidget *) NULL;
2338 m_wxwindow = (GtkWidget *) NULL;
2339 m_focusWidget = (GtkWidget *) NULL;
2340
2341 // position/size
2342 m_x = 0;
2343 m_y = 0;
2344 m_width = 0;
2345 m_height = 0;
2346
2347 m_sizeSet = FALSE;
2348 m_hasVMT = FALSE;
2349 m_needParent = TRUE;
2350 m_isBeingDeleted = FALSE;
2351
2352 m_noExpose = FALSE;
2353 m_nativeSizeEvent = FALSE;
2354
2355 m_hasScrolling = FALSE;
2356 m_isScrolling = FALSE;
2357
2358 m_hAdjust = (GtkAdjustment*) NULL;
2359 m_vAdjust = (GtkAdjustment*) NULL;
2360 m_oldHorizontalPos = 0.0;
2361 m_oldVerticalPos = 0.0;
2362
2363 m_resizing = FALSE;
2364 m_widgetStyle = (GtkStyle*) NULL;
2365
2366 m_insertCallback = (wxInsertChildFunction) NULL;
2367
2368 m_isStaticBox = FALSE;
2369 m_isRadioButton = FALSE;
2370 m_isListBox = FALSE;
2371 m_isFrame = FALSE;
2372 m_acceptsFocus = FALSE;
2373
2374 m_clipPaintRegion = FALSE;
2375
2376 m_cursor = *wxSTANDARD_CURSOR;
2377
2378 m_delayedForegroundColour = FALSE;
2379 m_delayedBackgroundColour = FALSE;
2380
2381 #ifdef HAVE_XIM
2382 m_ic = (GdkIC*) NULL;
2383 m_icattr = (GdkICAttr*) NULL;
2384 #endif
2385 }
2386
2387 wxWindowGTK::wxWindowGTK()
2388 {
2389 Init();
2390 }
2391
2392 wxWindowGTK::wxWindowGTK( wxWindow *parent,
2393 wxWindowID id,
2394 const wxPoint &pos,
2395 const wxSize &size,
2396 long style,
2397 const wxString &name )
2398 {
2399 Init();
2400
2401 Create( parent, id, pos, size, style, name );
2402 }
2403
2404 bool wxWindowGTK::Create( wxWindow *parent,
2405 wxWindowID id,
2406 const wxPoint &pos,
2407 const wxSize &size,
2408 long style,
2409 const wxString &name )
2410 {
2411 if (!PreCreation( parent, pos, size ) ||
2412 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
2413 {
2414 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2415 return FALSE;
2416 }
2417
2418 m_insertCallback = wxInsertChildInWindow;
2419
2420 // always needed for background clearing
2421 m_delayedBackgroundColour = TRUE;
2422
2423 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
2424 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2425
2426 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
2427
2428 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
2429 scroll_class->scrollbar_spacing = 0;
2430
2431 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
2432
2433 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
2434 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
2435
2436 m_wxwindow = gtk_pizza_new();
2437
2438 #ifndef __WXUNIVERSAL__
2439 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
2440
2441 if (HasFlag(wxRAISED_BORDER))
2442 {
2443 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
2444 }
2445 else if (HasFlag(wxSUNKEN_BORDER))
2446 {
2447 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
2448 }
2449 else if (HasFlag(wxSIMPLE_BORDER))
2450 {
2451 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
2452 }
2453 else
2454 {
2455 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
2456 }
2457 #endif // __WXUNIVERSAL__
2458
2459 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2460
2461 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
2462 m_acceptsFocus = TRUE;
2463
2464 // I _really_ don't want scrollbars in the beginning
2465 m_vAdjust->lower = 0.0;
2466 m_vAdjust->upper = 1.0;
2467 m_vAdjust->value = 0.0;
2468 m_vAdjust->step_increment = 1.0;
2469 m_vAdjust->page_increment = 1.0;
2470 m_vAdjust->page_size = 5.0;
2471 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
2472 m_hAdjust->lower = 0.0;
2473 m_hAdjust->upper = 1.0;
2474 m_hAdjust->value = 0.0;
2475 m_hAdjust->step_increment = 1.0;
2476 m_hAdjust->page_increment = 1.0;
2477 m_hAdjust->page_size = 5.0;
2478 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
2479
2480 // these handlers block mouse events to any window during scrolling such as
2481 // motion events and prevent GTK and wxWindows from fighting over where the
2482 // slider should be
2483
2484 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
2485 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
2486
2487 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
2488 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
2489
2490 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
2491 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
2492
2493 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
2494 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
2495
2496 // these handlers get notified when screen updates are required either when
2497 // scrolling or when the window size (and therefore scrollbar configuration)
2498 // has changed
2499
2500 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
2501 (GtkSignalFunc) gtk_window_hscroll_callback, (gpointer) this );
2502 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
2503 (GtkSignalFunc) gtk_window_vscroll_callback, (gpointer) this );
2504
2505 gtk_widget_show( m_wxwindow );
2506
2507 if (m_parent)
2508 m_parent->DoAddChild( this );
2509
2510 m_focusWidget = m_wxwindow;
2511
2512 PostCreation();
2513
2514 Show( TRUE );
2515
2516 return TRUE;
2517 }
2518
2519 wxWindowGTK::~wxWindowGTK()
2520 {
2521 if (g_focusWindow == this)
2522 g_focusWindow = NULL;
2523
2524 if (g_activeFrame == this)
2525 g_activeFrame = NULL;
2526
2527 if ( g_delayedFocus == this )
2528 g_delayedFocus = NULL;
2529
2530 m_isBeingDeleted = TRUE;
2531 m_hasVMT = FALSE;
2532
2533 if (m_widget)
2534 Show( FALSE );
2535
2536 DestroyChildren();
2537
2538 if (m_parent)
2539 m_parent->RemoveChild( this );
2540
2541 #ifdef HAVE_XIM
2542 if (m_ic)
2543 gdk_ic_destroy (m_ic);
2544 if (m_icattr)
2545 gdk_ic_attr_destroy (m_icattr);
2546 #endif
2547
2548 if (m_widgetStyle)
2549 {
2550 #if DISABLE_STYLE_IF_BROKEN_THEME
2551 // don't delete if it's a pixmap theme style
2552 if (!m_widgetStyle->engine_data)
2553 gtk_style_unref( m_widgetStyle );
2554 #endif
2555 m_widgetStyle = (GtkStyle*) NULL;
2556 }
2557
2558 if (m_wxwindow)
2559 {
2560 gtk_widget_destroy( m_wxwindow );
2561 m_wxwindow = (GtkWidget*) NULL;
2562 }
2563
2564 if (m_widget)
2565 {
2566 gtk_widget_destroy( m_widget );
2567 m_widget = (GtkWidget*) NULL;
2568 }
2569 }
2570
2571 bool wxWindowGTK::PreCreation( wxWindowGTK *parent, const wxPoint &pos, const wxSize &size )
2572 {
2573 wxCHECK_MSG( !m_needParent || parent, FALSE, wxT("Need complete parent.") );
2574
2575 /* this turns -1 into 20 so that a minimal window is
2576 visible even although -1,-1 has been given as the
2577 size of the window. the same trick is used in other
2578 ports and should make debugging easier */
2579 m_width = WidthDefault(size.x);
2580 m_height = HeightDefault(size.y);
2581
2582 m_x = (int)pos.x;
2583 m_y = (int)pos.y;
2584
2585 /* some reasonable defaults */
2586 if (!parent)
2587 {
2588 if (m_x == -1)
2589 {
2590 m_x = (gdk_screen_width () - m_width) / 2;
2591 if (m_x < 10) m_x = 10;
2592 }
2593 if (m_y == -1)
2594 {
2595 m_y = (gdk_screen_height () - m_height) / 2;
2596 if (m_y < 10) m_y = 10;
2597 }
2598 }
2599
2600 return TRUE;
2601 }
2602
2603 void wxWindowGTK::PostCreation()
2604 {
2605 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2606
2607 if (m_wxwindow)
2608 {
2609 if (!m_noExpose)
2610 {
2611 // these get reported to wxWindows -> wxPaintEvent
2612
2613 gtk_pizza_set_external( GTK_PIZZA(m_wxwindow), TRUE );
2614
2615 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
2616 GTK_SIGNAL_FUNC(gtk_window_expose_callback), (gpointer)this );
2617
2618 #ifndef __WXGTK20__
2619 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
2620 GTK_SIGNAL_FUNC(gtk_window_draw_callback), (gpointer)this );
2621
2622 if (HasFlag(wxNO_FULL_REPAINT_ON_RESIZE))
2623 {
2624 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "event",
2625 GTK_SIGNAL_FUNC(gtk_window_event_event_callback), (gpointer)this );
2626 }
2627 #else
2628 gtk_widget_set_redraw_on_allocate( GTK_WIDGET(m_wxwindow), HasFlag( wxNO_FULL_REPAINT_ON_RESIZE ) );
2629 #endif
2630 }
2631
2632 // these are called when the "sunken" or "raised" borders are drawn
2633 gtk_signal_connect( GTK_OBJECT(m_widget), "expose_event",
2634 GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this );
2635
2636 #ifndef __WXGTK20__
2637 gtk_signal_connect( GTK_OBJECT(m_widget), "draw",
2638 GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this );
2639 #endif
2640 }
2641
2642 // focus handling
2643
2644 if (m_focusWidget == NULL)
2645 m_focusWidget = m_widget;
2646
2647 gtk_signal_connect( GTK_OBJECT(m_focusWidget), "focus_in_event",
2648 GTK_SIGNAL_FUNC(gtk_window_focus_in_callback), (gpointer)this );
2649
2650 gtk_signal_connect( GTK_OBJECT(m_focusWidget), "focus_out_event",
2651 GTK_SIGNAL_FUNC(gtk_window_focus_out_callback), (gpointer)this );
2652
2653 // connect to the various key and mouse handlers
2654
2655 GtkWidget *connect_widget = GetConnectWidget();
2656
2657 ConnectWidget( connect_widget );
2658
2659 /* We cannot set colours, fonts and cursors before the widget has
2660 been realized, so we do this directly after realization */
2661 gtk_signal_connect( GTK_OBJECT(connect_widget), "realize",
2662 GTK_SIGNAL_FUNC(gtk_window_realized_callback), (gpointer) this );
2663
2664 if (m_wxwindow)
2665 {
2666 // Catch native resize events
2667 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "size_allocate",
2668 GTK_SIGNAL_FUNC(gtk_window_size_callback), (gpointer)this );
2669
2670 // Initialize XIM support
2671 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
2672 GTK_SIGNAL_FUNC(gtk_wxwindow_realized_callback), (gpointer) this );
2673
2674 // And resize XIM window
2675 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "size_allocate",
2676 GTK_SIGNAL_FUNC(gtk_wxwindow_size_callback), (gpointer)this );
2677 }
2678
2679 if (!GTK_IS_COMBO(m_widget))
2680 {
2681 // This is needed if we want to add our windows into native
2682 // GTK control, such as the toolbar. With this callback, the
2683 // toolbar gets to know the correct size (the one set by the
2684 // programmer). Sadly, it misbehaves for wxComboBox. FIXME
2685 // when moving to GTK 2.0.
2686 gtk_signal_connect( GTK_OBJECT(m_widget), "size_request",
2687 GTK_SIGNAL_FUNC(gtk_window_size_request_callback), (gpointer) this );
2688 }
2689
2690 m_hasVMT = TRUE;
2691 }
2692
2693 void wxWindowGTK::ConnectWidget( GtkWidget *widget )
2694 {
2695 gtk_signal_connect( GTK_OBJECT(widget), "key_press_event",
2696 GTK_SIGNAL_FUNC(gtk_window_key_press_callback), (gpointer)this );
2697
2698 gtk_signal_connect( GTK_OBJECT(widget), "key_release_event",
2699 GTK_SIGNAL_FUNC(gtk_window_key_release_callback), (gpointer)this );
2700
2701 gtk_signal_connect( GTK_OBJECT(widget), "button_press_event",
2702 GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this );
2703
2704 gtk_signal_connect( GTK_OBJECT(widget), "button_release_event",
2705 GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this );
2706
2707 gtk_signal_connect( GTK_OBJECT(widget), "motion_notify_event",
2708 GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this );
2709
2710 gtk_signal_connect( GTK_OBJECT(widget), "enter_notify_event",
2711 GTK_SIGNAL_FUNC(gtk_window_enter_callback), (gpointer)this );
2712
2713 gtk_signal_connect( GTK_OBJECT(widget), "leave_notify_event",
2714 GTK_SIGNAL_FUNC(gtk_window_leave_callback), (gpointer)this );
2715 }
2716
2717 bool wxWindowGTK::Destroy()
2718 {
2719 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2720
2721 m_hasVMT = FALSE;
2722
2723 return wxWindowBase::Destroy();
2724 }
2725
2726 void wxWindowGTK::DoMoveWindow(int x, int y, int width, int height)
2727 {
2728 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), m_widget, x, y, width, height );
2729 }
2730
2731 void wxWindowGTK::DoSetSize( int x, int y, int width, int height, int sizeFlags )
2732 {
2733 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2734 wxASSERT_MSG( (m_parent != NULL), wxT("wxWindowGTK::SetSize requires parent.\n") );
2735
2736 /*
2737 printf( "DoSetSize: name %s, x,y,w,h: %d,%d,%d,%d \n", GetName().c_str(), x,y,width,height );
2738 */
2739
2740 if (m_resizing) return; /* I don't like recursions */
2741 m_resizing = TRUE;
2742
2743 int currentX, currentY;
2744 GetPosition(&currentX, &currentY);
2745 if (x == -1)
2746 x = currentX;
2747 if (y == -1)
2748 y = currentY;
2749 AdjustForParentClientOrigin(x, y, sizeFlags);
2750
2751 if (m_parent->m_wxwindow == NULL) /* i.e. wxNotebook */
2752 {
2753 /* don't set the size for children of wxNotebook, just take the values. */
2754 m_x = x;
2755 m_y = y;
2756 m_width = width;
2757 m_height = height;
2758 }
2759 else
2760 {
2761 GtkPizza *pizza = GTK_PIZZA(m_parent->m_wxwindow);
2762 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
2763 {
2764 if (x != -1) m_x = x + pizza->xoffset;
2765 if (y != -1) m_y = y + pizza->yoffset;
2766 if (width != -1) m_width = width;
2767 if (height != -1) m_height = height;
2768 }
2769 else
2770 {
2771 m_x = x + pizza->xoffset;
2772 m_y = y + pizza->yoffset;
2773 m_width = width;
2774 m_height = height;
2775 }
2776
2777 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
2778 {
2779 if (width == -1) m_width = 80;
2780 }
2781
2782 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
2783 {
2784 if (height == -1) m_height = 26;
2785 }
2786
2787 int minWidth = GetMinWidth(),
2788 minHeight = GetMinHeight(),
2789 maxWidth = GetMaxWidth(),
2790 maxHeight = GetMaxHeight();
2791
2792 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
2793 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
2794 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
2795 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
2796
2797 int border = 0;
2798 int bottom_border = 0;
2799
2800 #ifndef __WXGTK20__
2801 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
2802 {
2803 /* the default button has a border around it */
2804 border = 6;
2805 bottom_border = 5;
2806 }
2807 #endif
2808
2809 DoMoveWindow( m_x-border,
2810 m_y-border,
2811 m_width+2*border,
2812 m_height+border+bottom_border );
2813 }
2814
2815 if (m_hasScrolling)
2816 {
2817 /* Sometimes the client area changes size without the
2818 whole windows's size changing, but if the whole
2819 windows's size doesn't change, no wxSizeEvent will
2820 normally be sent. Here we add an extra test if
2821 the client test has been changed and this will
2822 be used then. */
2823 GetClientSize( &m_oldClientWidth, &m_oldClientHeight );
2824 }
2825
2826 /*
2827 wxPrintf( "OnSize sent from " );
2828 if (GetClassInfo() && GetClassInfo()->GetClassName())
2829 wxPrintf( GetClassInfo()->GetClassName() );
2830 wxPrintf( " %d %d %d %d\n", (int)m_x, (int)m_y, (int)m_width, (int)m_height );
2831 */
2832
2833 if (!m_nativeSizeEvent)
2834 {
2835 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
2836 event.SetEventObject( this );
2837 GetEventHandler()->ProcessEvent( event );
2838 }
2839
2840 m_resizing = FALSE;
2841 }
2842
2843 void wxWindowGTK::OnInternalIdle()
2844 {
2845 // Update invalidated regions.
2846 GtkUpdate();
2847
2848 // Synthetize activate events.
2849 if ( g_sendActivateEvent != -1 )
2850 {
2851 bool activate = g_sendActivateEvent != 0;
2852
2853 // do it only once
2854 g_sendActivateEvent = -1;
2855
2856 wxTheApp->SetActive(activate, (wxWindow *)g_focusWindowLast);
2857 }
2858
2859 if ( g_activeFrameLostFocus )
2860 {
2861 if ( g_activeFrame )
2862 {
2863 wxLogTrace(wxT("activate"), wxT("Deactivating frame %p (from idle)"), g_activeFrame);
2864 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, g_activeFrame->GetId());
2865 event.SetEventObject(g_activeFrame);
2866 g_activeFrame->GetEventHandler()->ProcessEvent(event);
2867 g_activeFrame = NULL;
2868 }
2869 g_activeFrameLostFocus = FALSE;
2870 }
2871
2872 wxCursor cursor = m_cursor;
2873 if (g_globalCursor.Ok()) cursor = g_globalCursor;
2874
2875 if (cursor.Ok())
2876 {
2877 /* I now set the cursor anew in every OnInternalIdle call
2878 as setting the cursor in a parent window also effects the
2879 windows above so that checking for the current cursor is
2880 not possible. */
2881
2882 if (m_wxwindow)
2883 {
2884 GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window;
2885 if (window)
2886 gdk_window_set_cursor( window, cursor.GetCursor() );
2887
2888 if (!g_globalCursor.Ok())
2889 cursor = *wxSTANDARD_CURSOR;
2890
2891 window = m_widget->window;
2892 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
2893 gdk_window_set_cursor( window, cursor.GetCursor() );
2894
2895 }
2896 else
2897 {
2898
2899 GdkWindow *window = m_widget->window;
2900 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
2901 gdk_window_set_cursor( window, cursor.GetCursor() );
2902
2903 }
2904 }
2905
2906 UpdateWindowUI();
2907 }
2908
2909 void wxWindowGTK::DoGetSize( int *width, int *height ) const
2910 {
2911 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
2912
2913 if (width) (*width) = m_width;
2914 if (height) (*height) = m_height;
2915 }
2916
2917 void wxWindowGTK::DoSetClientSize( int width, int height )
2918 {
2919 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
2920
2921 if (!m_wxwindow)
2922 {
2923 SetSize( width, height );
2924 }
2925 else
2926 {
2927 int dw = 0;
2928 int dh = 0;
2929
2930 #ifndef __WXUNIVERSAL__
2931 if (HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER))
2932 {
2933 /* when using GTK 1.2 we set the shadow border size to 2 */
2934 dw += 2 * 2;
2935 dh += 2 * 2;
2936 }
2937 if (HasFlag(wxSIMPLE_BORDER))
2938 {
2939 /* when using GTK 1.2 we set the simple border size to 1 */
2940 dw += 1 * 2;
2941 dh += 1 * 2;
2942 }
2943 #endif // __WXUNIVERSAL__
2944
2945 if (m_hasScrolling)
2946 {
2947 GtkScrolledWindow *scroll_window = GTK_SCROLLED_WINDOW(m_widget);
2948
2949 GtkRequisition vscroll_req;
2950 vscroll_req.width = 2;
2951 vscroll_req.height = 2;
2952 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->vscrollbar) )->size_request )
2953 (scroll_window->vscrollbar, &vscroll_req );
2954
2955 GtkRequisition hscroll_req;
2956 hscroll_req.width = 2;
2957 hscroll_req.height = 2;
2958 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->hscrollbar) )->size_request )
2959 (scroll_window->hscrollbar, &hscroll_req );
2960
2961 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
2962
2963 if (scroll_window->vscrollbar_visible)
2964 {
2965 dw += vscroll_req.width;
2966 dw += scroll_class->scrollbar_spacing;
2967 }
2968
2969 if (scroll_window->hscrollbar_visible)
2970 {
2971 dh += hscroll_req.height;
2972 dh += scroll_class->scrollbar_spacing;
2973 }
2974 }
2975
2976 SetSize( width+dw, height+dh );
2977 }
2978 }
2979
2980 void wxWindowGTK::DoGetClientSize( int *width, int *height ) const
2981 {
2982 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
2983
2984 if (!m_wxwindow)
2985 {
2986 if (width) (*width) = m_width;
2987 if (height) (*height) = m_height;
2988 }
2989 else
2990 {
2991 int dw = 0;
2992 int dh = 0;
2993
2994 #ifndef __WXUNIVERSAL__
2995 if (HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER))
2996 {
2997 /* when using GTK 1.2 we set the shadow border size to 2 */
2998 dw += 2 * 2;
2999 dh += 2 * 2;
3000 }
3001 if (HasFlag(wxSIMPLE_BORDER))
3002 {
3003 /* when using GTK 1.2 we set the simple border size to 1 */
3004 dw += 1 * 2;
3005 dh += 1 * 2;
3006 }
3007 #endif // __WXUNIVERSAL__
3008
3009 if (m_hasScrolling)
3010 {
3011 GtkScrolledWindow *scroll_window = GTK_SCROLLED_WINDOW(m_widget);
3012
3013 GtkRequisition vscroll_req;
3014 vscroll_req.width = 2;
3015 vscroll_req.height = 2;
3016 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->vscrollbar) )->size_request )
3017 (scroll_window->vscrollbar, &vscroll_req );
3018
3019 GtkRequisition hscroll_req;
3020 hscroll_req.width = 2;
3021 hscroll_req.height = 2;
3022 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->hscrollbar) )->size_request )
3023 (scroll_window->hscrollbar, &hscroll_req );
3024
3025 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
3026
3027 if (scroll_window->vscrollbar_visible)
3028 {
3029 dw += vscroll_req.width;
3030 dw += scroll_class->scrollbar_spacing;
3031 }
3032
3033 if (scroll_window->hscrollbar_visible)
3034 {
3035 dh += hscroll_req.height;
3036 dh += scroll_class->scrollbar_spacing;
3037 }
3038 }
3039
3040 if (width) (*width) = m_width - dw;
3041 if (height) (*height) = m_height - dh;
3042 }
3043
3044 /*
3045 printf( "GetClientSize, name %s ", GetName().c_str() );
3046 if (width) printf( " width = %d", (*width) );
3047 if (height) printf( " height = %d", (*height) );
3048 printf( "\n" );
3049 */
3050 }
3051
3052 void wxWindowGTK::DoGetPosition( int *x, int *y ) const
3053 {
3054 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3055
3056 int dx = 0;
3057 int dy = 0;
3058 if (m_parent && m_parent->m_wxwindow)
3059 {
3060 GtkPizza *pizza = GTK_PIZZA(m_parent->m_wxwindow);
3061 dx = pizza->xoffset;
3062 dy = pizza->yoffset;
3063 }
3064
3065 if (x) (*x) = m_x - dx;
3066 if (y) (*y) = m_y - dy;
3067 }
3068
3069 void wxWindowGTK::DoClientToScreen( int *x, int *y ) const
3070 {
3071 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3072
3073 if (!m_widget->window) return;
3074
3075 GdkWindow *source = (GdkWindow *) NULL;
3076 if (m_wxwindow)
3077 source = GTK_PIZZA(m_wxwindow)->bin_window;
3078 else
3079 source = m_widget->window;
3080
3081 int org_x = 0;
3082 int org_y = 0;
3083 gdk_window_get_origin( source, &org_x, &org_y );
3084
3085 if (!m_wxwindow)
3086 {
3087 if (GTK_WIDGET_NO_WINDOW (m_widget))
3088 {
3089 org_x += m_widget->allocation.x;
3090 org_y += m_widget->allocation.y;
3091 }
3092 }
3093
3094 if (x) *x += org_x;
3095 if (y) *y += org_y;
3096 }
3097
3098 void wxWindowGTK::DoScreenToClient( int *x, int *y ) const
3099 {
3100 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3101
3102 if (!m_widget->window) return;
3103
3104 GdkWindow *source = (GdkWindow *) NULL;
3105 if (m_wxwindow)
3106 source = GTK_PIZZA(m_wxwindow)->bin_window;
3107 else
3108 source = m_widget->window;
3109
3110 int org_x = 0;
3111 int org_y = 0;
3112 gdk_window_get_origin( source, &org_x, &org_y );
3113
3114 if (!m_wxwindow)
3115 {
3116 if (GTK_WIDGET_NO_WINDOW (m_widget))
3117 {
3118 org_x += m_widget->allocation.x;
3119 org_y += m_widget->allocation.y;
3120 }
3121 }
3122
3123 if (x) *x -= org_x;
3124 if (y) *y -= org_y;
3125 }
3126
3127 bool wxWindowGTK::Show( bool show )
3128 {
3129 wxCHECK_MSG( (m_widget != NULL), FALSE, wxT("invalid window") );
3130
3131 if (!wxWindowBase::Show(show))
3132 {
3133 // nothing to do
3134 return FALSE;
3135 }
3136
3137 if (show)
3138 gtk_widget_show( m_widget );
3139 else
3140 gtk_widget_hide( m_widget );
3141
3142 return TRUE;
3143 }
3144
3145 static void wxWindowNotifyEnable(wxWindowGTK* win, bool enable)
3146 {
3147 win->OnParentEnable(enable);
3148
3149 // Recurse, so that children have the opportunity to Do The Right Thing
3150 // and reset colours that have been messed up by a parent's (really ancestor's)
3151 // Enable call
3152 for ( wxWindowList::Node *node = win->GetChildren().GetFirst();
3153 node;
3154 node = node->GetNext() )
3155 {
3156 wxWindow *child = node->GetData();
3157 if (!child->IsKindOf(CLASSINFO(wxDialog)) && !child->IsKindOf(CLASSINFO(wxFrame)))
3158 wxWindowNotifyEnable(child, enable);
3159 }
3160 }
3161
3162 bool wxWindowGTK::Enable( bool enable )
3163 {
3164 wxCHECK_MSG( (m_widget != NULL), FALSE, wxT("invalid window") );
3165
3166 if (!wxWindowBase::Enable(enable))
3167 {
3168 // nothing to do
3169 return FALSE;
3170 }
3171
3172 gtk_widget_set_sensitive( m_widget, enable );
3173 if ( m_wxwindow )
3174 gtk_widget_set_sensitive( m_wxwindow, enable );
3175
3176 wxWindowNotifyEnable(this, enable);
3177
3178 return TRUE;
3179 }
3180
3181 int wxWindowGTK::GetCharHeight() const
3182 {
3183 wxCHECK_MSG( (m_widget != NULL), 12, wxT("invalid window") );
3184
3185 wxCHECK_MSG( m_font.Ok(), 12, wxT("invalid font") );
3186
3187 GdkFont *font = m_font.GetInternalFont( 1.0 );
3188
3189 return font->ascent + font->descent;
3190 }
3191
3192 int wxWindowGTK::GetCharWidth() const
3193 {
3194 wxCHECK_MSG( (m_widget != NULL), 8, wxT("invalid window") );
3195
3196 wxCHECK_MSG( m_font.Ok(), 8, wxT("invalid font") );
3197
3198 GdkFont *font = m_font.GetInternalFont( 1.0 );
3199
3200 return gdk_string_width( font, "H" );
3201 }
3202
3203 void wxWindowGTK::GetTextExtent( const wxString& string,
3204 int *x,
3205 int *y,
3206 int *descent,
3207 int *externalLeading,
3208 const wxFont *theFont ) const
3209 {
3210 wxFont fontToUse = m_font;
3211 if (theFont) fontToUse = *theFont;
3212
3213 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
3214
3215 GdkFont *font = fontToUse.GetInternalFont( 1.0 );
3216 if (x) (*x) = gdk_string_width( font, string.mbc_str() );
3217 if (y) (*y) = font->ascent + font->descent;
3218 if (descent) (*descent) = font->descent;
3219 if (externalLeading) (*externalLeading) = 0; // ??
3220 }
3221
3222 void wxWindowGTK::SetFocus()
3223 {
3224 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3225
3226 if (m_wxwindow)
3227 {
3228 if (!GTK_WIDGET_HAS_FOCUS (m_wxwindow))
3229 {
3230 gtk_widget_grab_focus (m_wxwindow);
3231 }
3232 }
3233 else if (m_widget)
3234 {
3235 if (GTK_WIDGET_CAN_FOCUS(m_widget) && !GTK_WIDGET_HAS_FOCUS (m_widget) )
3236 {
3237 if (!GTK_WIDGET_REALIZED(m_widget))
3238 {
3239 wxLogTrace(_T("focus"),
3240 _T("Delaying setting focus to %s(%s)\n"),
3241 GetClassInfo()->GetClassName(), GetLabel().c_str());
3242
3243 g_delayedFocus = this;
3244 }
3245 else
3246 {
3247 wxLogTrace(_T("focus"),
3248 _T("Setting focus to %s(%s)\n"),
3249 GetClassInfo()->GetClassName(), GetLabel().c_str());
3250
3251 gtk_widget_grab_focus (m_widget);
3252 }
3253 }
3254 else if (GTK_IS_CONTAINER(m_widget))
3255 {
3256 SET_CONTAINER_FOCUS( m_widget, GTK_DIR_TAB_FORWARD );
3257 }
3258 else
3259 {
3260 // ?
3261 }
3262 }
3263
3264 (void)DoSendFocusEvents(this);
3265 }
3266
3267 bool wxWindowGTK::AcceptsFocus() const
3268 {
3269 return m_acceptsFocus && wxWindowBase::AcceptsFocus();
3270 }
3271
3272 bool wxWindowGTK::Reparent( wxWindowBase *newParentBase )
3273 {
3274 wxCHECK_MSG( (m_widget != NULL), FALSE, wxT("invalid window") );
3275
3276 wxWindowGTK *oldParent = m_parent,
3277 *newParent = (wxWindowGTK *)newParentBase;
3278
3279 wxASSERT( GTK_IS_WIDGET(m_widget) );
3280
3281 if ( !wxWindowBase::Reparent(newParent) )
3282 return FALSE;
3283
3284 wxASSERT( GTK_IS_WIDGET(m_widget) );
3285
3286 /* prevent GTK from deleting the widget arbitrarily */
3287 gtk_widget_ref( m_widget );
3288
3289 if (oldParent)
3290 {
3291 gtk_container_remove( GTK_CONTAINER(m_widget->parent), m_widget );
3292 }
3293
3294 wxASSERT( GTK_IS_WIDGET(m_widget) );
3295
3296 if (newParent)
3297 {
3298 /* insert GTK representation */
3299 (*(newParent->m_insertCallback))(newParent, this);
3300 }
3301
3302 /* reverse: prevent GTK from deleting the widget arbitrarily */
3303 gtk_widget_unref( m_widget );
3304
3305 return TRUE;
3306 }
3307
3308 void wxWindowGTK::DoAddChild(wxWindowGTK *child)
3309 {
3310 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
3311
3312 wxASSERT_MSG( (child != NULL), wxT("invalid child window") );
3313
3314 wxASSERT_MSG( (m_insertCallback != NULL), wxT("invalid child insertion function") );
3315
3316 /* add to list */
3317 AddChild( child );
3318
3319 /* insert GTK representation */
3320 (*m_insertCallback)(this, child);
3321 }
3322
3323 void wxWindowGTK::Raise()
3324 {
3325 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3326
3327 if (!m_widget->window) return;
3328
3329 gdk_window_raise( m_widget->window );
3330 }
3331
3332 void wxWindowGTK::Lower()
3333 {
3334 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3335
3336 if (!m_widget->window) return;
3337
3338 gdk_window_lower( m_widget->window );
3339 }
3340
3341 bool wxWindowGTK::SetCursor( const wxCursor &cursor )
3342 {
3343 wxCHECK_MSG( (m_widget != NULL), FALSE, wxT("invalid window") );
3344
3345 if (cursor == m_cursor)
3346 return FALSE;
3347
3348 if (g_isIdle)
3349 wxapp_install_idle_handler();
3350
3351 if (cursor == wxNullCursor)
3352 return wxWindowBase::SetCursor( *wxSTANDARD_CURSOR );
3353 else
3354 return wxWindowBase::SetCursor( cursor );
3355 }
3356
3357 void wxWindowGTK::WarpPointer( int x, int y )
3358 {
3359 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3360
3361 // We provide this function ourselves as it is
3362 // missing in GDK (top of this file).
3363
3364 GdkWindow *window = (GdkWindow*) NULL;
3365 if (m_wxwindow)
3366 window = GTK_PIZZA(m_wxwindow)->bin_window;
3367 else
3368 window = GetConnectWidget()->window;
3369
3370 if (window)
3371 gdk_window_warp_pointer( window, x, y );
3372 }
3373
3374 void wxWindowGTK::Refresh( bool eraseBackground, const wxRect *rect )
3375 {
3376 if (!m_widget) return;
3377 if (!m_widget->window) return;
3378
3379 #ifndef __WXGTK20__
3380 if (g_isIdle)
3381 wxapp_install_idle_handler();
3382
3383 if (eraseBackground && m_wxwindow && m_wxwindow->window)
3384 {
3385 if (rect)
3386 {
3387 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
3388 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
3389 }
3390 else
3391 {
3392 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
3393 m_clearRegion.Clear();
3394 m_clearRegion.Union( 0, 0, m_wxwindow->allocation.width, m_wxwindow->allocation.height );
3395 }
3396 }
3397
3398 if (rect)
3399 {
3400 if (m_wxwindow)
3401 {
3402 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
3403 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
3404 }
3405 else
3406 {
3407 GdkRectangle gdk_rect;
3408 gdk_rect.x = rect->x;
3409 gdk_rect.y = rect->y;
3410 gdk_rect.width = rect->width;
3411 gdk_rect.height = rect->height;
3412 gtk_widget_draw( m_widget, &gdk_rect );
3413 }
3414 }
3415 else
3416 {
3417 if (m_wxwindow)
3418 {
3419 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
3420 m_updateRegion.Clear();
3421 m_updateRegion.Union( 0, 0, m_wxwindow->allocation.width, m_wxwindow->allocation.height );
3422 }
3423 else
3424 {
3425 gtk_widget_draw( m_widget, (GdkRectangle*) NULL );
3426 }
3427 }
3428 #else
3429 if (m_wxwindow)
3430 {
3431 if (rect)
3432 {
3433 GdkRectangle gdk_rect;
3434 gdk_rect.x = rect->x;
3435 gdk_rect.y = rect->y;
3436 gdk_rect.width = rect->width;
3437 gdk_rect.height = rect->height;
3438 gdk_window_invalidate_rect( GTK_PIZZA(m_wxwindow)->bin_window, &gdk_rect, TRUE );
3439 }
3440 else
3441 {
3442 gdk_window_invalidate_rect( GTK_PIZZA(m_wxwindow)->bin_window, NULL, TRUE );
3443 }
3444 }
3445 #endif
3446 }
3447
3448 void wxWindowGTK::Update()
3449 {
3450 GtkUpdate();
3451 }
3452
3453 void wxWindowGTK::GtkUpdate()
3454 {
3455 #ifdef __WXGTK20__
3456 if (m_wxwindow && GTK_PIZZA(m_wxwindow)->bin_window)
3457 gdk_window_process_updates( GTK_PIZZA(m_wxwindow)->bin_window, FALSE );
3458 #endif
3459
3460 if (!m_updateRegion.IsEmpty())
3461 GtkSendPaintEvents();
3462 }
3463
3464 void wxWindowGTK::GtkSendPaintEvents()
3465 {
3466 if (!m_wxwindow)
3467 {
3468 m_clearRegion.Clear();
3469 m_updateRegion.Clear();
3470 return;
3471 }
3472
3473 // widget to draw on
3474 GtkPizza *pizza = GTK_PIZZA (m_wxwindow);
3475
3476 // Clip to paint region in wxClientDC
3477 m_clipPaintRegion = TRUE;
3478
3479 if (GetThemeEnabled())
3480 {
3481 // find ancestor from which to steal background
3482 wxWindow *parent = GetParent();
3483 while (parent && !parent->IsTopLevel())
3484 parent = parent->GetParent();
3485 if (!parent)
3486 parent = this;
3487
3488 wxRegionIterator upd( m_updateRegion );
3489 while (upd)
3490 {
3491 GdkRectangle rect;
3492 rect.x = upd.GetX();
3493 rect.y = upd.GetY();
3494 rect.width = upd.GetWidth();
3495 rect.height = upd.GetHeight();
3496
3497 gtk_paint_flat_box( parent->m_widget->style,
3498 pizza->bin_window,
3499 GTK_STATE_NORMAL,
3500 GTK_SHADOW_NONE,
3501 &rect,
3502 parent->m_widget,
3503 (char *)"base",
3504 0, 0, -1, -1 );
3505
3506 upd ++;
3507 }
3508 }
3509 else
3510 // if (!m_clearRegion.IsEmpty()) // always send an erase event
3511 {
3512 wxWindowDC dc( (wxWindow*)this );
3513 dc.SetClippingRegion( m_clearRegion );
3514
3515 wxEraseEvent erase_event( GetId(), &dc );
3516 erase_event.SetEventObject( this );
3517
3518 if (!GetEventHandler()->ProcessEvent(erase_event))
3519 {
3520 if (!g_eraseGC)
3521 {
3522 g_eraseGC = gdk_gc_new( pizza->bin_window );
3523 gdk_gc_set_fill( g_eraseGC, GDK_SOLID );
3524 }
3525 gdk_gc_set_foreground( g_eraseGC, m_backgroundColour.GetColor() );
3526
3527 wxRegionIterator upd( m_clearRegion );
3528 while (upd)
3529 {
3530 gdk_draw_rectangle( pizza->bin_window, g_eraseGC, 1,
3531 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
3532 upd ++;
3533 }
3534 }
3535 m_clearRegion.Clear();
3536 }
3537
3538 wxNcPaintEvent nc_paint_event( GetId() );
3539 nc_paint_event.SetEventObject( this );
3540 GetEventHandler()->ProcessEvent( nc_paint_event );
3541
3542 wxPaintEvent paint_event( GetId() );
3543 paint_event.SetEventObject( this );
3544 GetEventHandler()->ProcessEvent( paint_event );
3545
3546 m_clipPaintRegion = FALSE;
3547
3548 #ifndef __WXUNIVERSAL__
3549 #ifndef __WXGTK20__
3550 // The following code will result in all window-less widgets
3551 // being redrawn because the wxWindows class is allowed to
3552 // paint over the window-less widgets.
3553
3554 GList *children = pizza->children;
3555 while (children)
3556 {
3557 GtkPizzaChild *child = (GtkPizzaChild*) children->data;
3558 children = children->next;
3559
3560 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
3561 GTK_WIDGET_DRAWABLE (child->widget))
3562 {
3563 // Get intersection of widget area and update region
3564 wxRegion region( m_updateRegion );
3565
3566 GdkEventExpose gdk_event;
3567 gdk_event.type = GDK_EXPOSE;
3568 gdk_event.window = pizza->bin_window;
3569 gdk_event.count = 0;
3570
3571 wxRegionIterator upd( m_updateRegion );
3572 while (upd)
3573 {
3574 GdkRectangle rect;
3575 rect.x = upd.GetX();
3576 rect.y = upd.GetY();
3577 rect.width = upd.GetWidth();
3578 rect.height = upd.GetHeight();
3579
3580 if (gtk_widget_intersect (child->widget, &rect, &gdk_event.area))
3581 {
3582 gtk_widget_event (child->widget, (GdkEvent*) &gdk_event);
3583 }
3584
3585 upd ++;
3586 }
3587 }
3588 }
3589 #endif
3590 #endif
3591
3592 m_updateRegion.Clear();
3593 }
3594
3595 void wxWindowGTK::Clear()
3596 {
3597 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3598
3599 if (m_wxwindow && m_wxwindow->window)
3600 {
3601 m_clearRegion.Clear();
3602 wxSize size( GetClientSize() );
3603 m_clearRegion.Union( 0,0,size.x,size.y );
3604
3605 // Better do this in idle?
3606 GtkUpdate();
3607 }
3608 }
3609
3610 #if wxUSE_TOOLTIPS
3611 void wxWindowGTK::DoSetToolTip( wxToolTip *tip )
3612 {
3613 wxWindowBase::DoSetToolTip(tip);
3614
3615 if (m_tooltip)
3616 m_tooltip->Apply( (wxWindow *)this );
3617 }
3618
3619 void wxWindowGTK::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
3620 {
3621 gtk_tooltips_set_tip( tips, GetConnectWidget(), wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
3622 }
3623 #endif // wxUSE_TOOLTIPS
3624
3625 void wxWindowGTK::GtkSetBackgroundColour( const wxColour &colour )
3626 {
3627 GdkWindow *window = (GdkWindow*) NULL;
3628 if (m_wxwindow)
3629 window = GTK_PIZZA(m_wxwindow)->bin_window;
3630 else
3631 window = GetConnectWidget()->window;
3632
3633 wxASSERT( window );
3634
3635 // We need the pixel value e.g. for background clearing.
3636 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
3637
3638 if (m_wxwindow)
3639 {
3640 // wxMSW doesn't clear the window here, either.
3641 gdk_window_set_background( window, m_backgroundColour.GetColor() );
3642 }
3643
3644 ApplyWidgetStyle();
3645 }
3646
3647 bool wxWindowGTK::SetBackgroundColour( const wxColour &colour )
3648 {
3649 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid window") );
3650
3651 if (!wxWindowBase::SetBackgroundColour(colour))
3652 return FALSE;
3653
3654 GdkWindow *window = (GdkWindow*) NULL;
3655 if (m_wxwindow)
3656 window = GTK_PIZZA(m_wxwindow)->bin_window;
3657 else
3658 window = GetConnectWidget()->window;
3659
3660 if (!window)
3661 {
3662 // indicate that a new style has been set
3663 // but it couldn't get applied as the
3664 // widget hasn't been realized yet.
3665 m_delayedBackgroundColour = TRUE;
3666 return TRUE;
3667 }
3668 else
3669 {
3670 GtkSetBackgroundColour( colour );
3671 }
3672
3673 return TRUE;
3674 }
3675
3676 void wxWindowGTK::GtkSetForegroundColour( const wxColour &colour )
3677 {
3678 GdkWindow *window = (GdkWindow*) NULL;
3679 if (m_wxwindow)
3680 window = GTK_PIZZA(m_wxwindow)->bin_window;
3681 else
3682 window = GetConnectWidget()->window;
3683
3684 wxASSERT( window );
3685
3686 ApplyWidgetStyle();
3687 }
3688
3689 bool wxWindowGTK::SetForegroundColour( const wxColour &colour )
3690 {
3691 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid window") );
3692
3693 if (!wxWindowBase::SetForegroundColour(colour))
3694 {
3695 // don't leave if the GTK widget has just
3696 // been realized
3697 if (!m_delayedForegroundColour) return FALSE;
3698 }
3699
3700 GdkWindow *window = (GdkWindow*) NULL;
3701 if (m_wxwindow)
3702 window = GTK_PIZZA(m_wxwindow)->bin_window;
3703 else
3704 window = GetConnectWidget()->window;
3705
3706 if (!window)
3707 {
3708 // indicate that a new style has been set
3709 // but it couldn't get applied as the
3710 // widget hasn't been realized yet.
3711 m_delayedForegroundColour = TRUE;
3712 }
3713 else
3714 {
3715 GtkSetForegroundColour( colour );
3716 }
3717
3718 return TRUE;
3719 }
3720
3721 GtkStyle *wxWindowGTK::GetWidgetStyle()
3722 {
3723 if (m_widgetStyle)
3724 {
3725 GtkStyle *remake = gtk_style_copy( m_widgetStyle );
3726
3727 // FIXME: no more klass in 2.0
3728 #ifndef __WXGTK20__
3729 remake->klass = m_widgetStyle->klass;
3730 #endif
3731
3732 gtk_style_unref( m_widgetStyle );
3733 m_widgetStyle = remake;
3734 }
3735 else
3736 {
3737 GtkStyle *def = gtk_rc_get_style( m_widget );
3738
3739 if (!def)
3740 def = gtk_widget_get_default_style();
3741
3742 m_widgetStyle = gtk_style_copy( def );
3743
3744 // FIXME: no more klass in 2.0
3745 #ifndef __WXGTK20__
3746 m_widgetStyle->klass = def->klass;
3747 #endif
3748 }
3749
3750 return m_widgetStyle;
3751 }
3752
3753 void wxWindowGTK::SetWidgetStyle()
3754 {
3755 #if DISABLE_STYLE_IF_BROKEN_THEME
3756 if (m_widget->style->engine_data)
3757 {
3758 static bool s_warningPrinted = FALSE;
3759 if (!s_warningPrinted)
3760 {
3761 printf( "wxWindows warning: Widget styles disabled due to buggy GTK theme.\n" );
3762 s_warningPrinted = TRUE;
3763 }
3764 m_widgetStyle = m_widget->style;
3765 return;
3766 }
3767 #endif
3768
3769 GtkStyle *style = GetWidgetStyle();
3770
3771 if (m_font != wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ))
3772 {
3773 SET_STYLE_FONT(style, m_font.GetInternalFont( 1.0 ));
3774 }
3775
3776 if (m_foregroundColour.Ok())
3777 {
3778 m_foregroundColour.CalcPixel( gtk_widget_get_colormap( m_widget ) );
3779 if (m_foregroundColour != wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT))
3780 {
3781 style->fg[GTK_STATE_NORMAL] = *m_foregroundColour.GetColor();
3782 style->fg[GTK_STATE_PRELIGHT] = *m_foregroundColour.GetColor();
3783 style->fg[GTK_STATE_ACTIVE] = *m_foregroundColour.GetColor();
3784 }
3785 else
3786 {
3787 // Try to restore the gtk default style. This is still a little
3788 // oversimplified for what is probably really needed here for controls
3789 // other than buttons, but is better than not being able to (re)set a
3790 // control's foreground colour to *wxBLACK -- RL
3791 GtkStyle *def = gtk_rc_get_style( m_widget );
3792
3793 if (!def)
3794 def = gtk_widget_get_default_style();
3795
3796 style->fg[GTK_STATE_NORMAL] = def->fg[GTK_STATE_NORMAL];
3797 style->fg[GTK_STATE_PRELIGHT] = def->fg[GTK_STATE_PRELIGHT];
3798 style->fg[GTK_STATE_ACTIVE] = def->fg[GTK_STATE_ACTIVE];
3799 }
3800 }
3801
3802 if (m_backgroundColour.Ok())
3803 {
3804 m_backgroundColour.CalcPixel( gtk_widget_get_colormap( m_widget ) );
3805 if (m_backgroundColour != wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE))
3806 {
3807 style->bg[GTK_STATE_NORMAL] = *m_backgroundColour.GetColor();
3808 style->base[GTK_STATE_NORMAL] = *m_backgroundColour.GetColor();
3809 style->bg[GTK_STATE_PRELIGHT] = *m_backgroundColour.GetColor();
3810 style->base[GTK_STATE_PRELIGHT] = *m_backgroundColour.GetColor();
3811 style->bg[GTK_STATE_ACTIVE] = *m_backgroundColour.GetColor();
3812 style->base[GTK_STATE_ACTIVE] = *m_backgroundColour.GetColor();
3813 style->bg[GTK_STATE_INSENSITIVE] = *m_backgroundColour.GetColor();
3814 style->base[GTK_STATE_INSENSITIVE] = *m_backgroundColour.GetColor();
3815 }
3816 else
3817 {
3818 // Try to restore the gtk default style. This is still a little
3819 // oversimplified for what is probably really needed here for controls
3820 // other than buttons, but is better than not being able to (re)set a
3821 // control's background colour to default grey and means resetting a
3822 // button to wxSYS_COLOUR_BTNFACE will restore its usual highlighting
3823 // behavior -- RL
3824 GtkStyle *def = gtk_rc_get_style( m_widget );
3825
3826 if (!def)
3827 def = gtk_widget_get_default_style();
3828
3829 style->bg[GTK_STATE_NORMAL] = def->bg[GTK_STATE_NORMAL];
3830 style->base[GTK_STATE_NORMAL] = def->base[GTK_STATE_NORMAL];
3831 style->bg[GTK_STATE_PRELIGHT] = def->bg[GTK_STATE_PRELIGHT];
3832 style->base[GTK_STATE_PRELIGHT] = def->base[GTK_STATE_PRELIGHT];
3833 style->bg[GTK_STATE_ACTIVE] = def->bg[GTK_STATE_ACTIVE];
3834 style->base[GTK_STATE_ACTIVE] = def->base[GTK_STATE_ACTIVE];
3835 style->bg[GTK_STATE_INSENSITIVE] = def->bg[GTK_STATE_INSENSITIVE];
3836 style->base[GTK_STATE_INSENSITIVE] = def->base[GTK_STATE_INSENSITIVE];
3837 }
3838 }
3839 }
3840
3841 void wxWindowGTK::ApplyWidgetStyle()
3842 {
3843 }
3844
3845 //-----------------------------------------------------------------------------
3846 // Pop-up menu stuff
3847 //-----------------------------------------------------------------------------
3848
3849 #if wxUSE_MENUS_NATIVE
3850
3851 extern "C"
3852 void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting )
3853 {
3854 *is_waiting = FALSE;
3855 }
3856
3857 static void SetInvokingWindow( wxMenu *menu, wxWindowGTK *win )
3858 {
3859 menu->SetInvokingWindow( win );
3860 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
3861 while (node)
3862 {
3863 wxMenuItem *menuitem = node->GetData();
3864 if (menuitem->IsSubMenu())
3865 {
3866 SetInvokingWindow( menuitem->GetSubMenu(), win );
3867 }
3868
3869 node = node->GetNext();
3870 }
3871 }
3872
3873 // used to pass the coordinates from wxWindowGTK::DoPopupMenu() to
3874 // wxPopupMenuPositionCallback()
3875 //
3876 // should be safe even in the MT case as the user can hardly popup 2 menus
3877 // simultaneously, can he?
3878 static gint gs_pop_x = 0;
3879 static gint gs_pop_y = 0;
3880
3881 extern "C" void wxPopupMenuPositionCallback( GtkMenu *menu,
3882 gint *x, gint *y,
3883 #ifdef __WXGTK20__
3884 gboolean * WXUNUSED(whatever),
3885 #endif
3886 gpointer WXUNUSED(user_data) )
3887 {
3888 // ensure that the menu appears entirely on screen
3889 GtkRequisition req;
3890 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
3891
3892 wxSize sizeScreen = wxGetDisplaySize();
3893
3894 gint xmax = sizeScreen.x - req.width,
3895 ymax = sizeScreen.y - req.height;
3896
3897 *x = gs_pop_x < xmax ? gs_pop_x : xmax;
3898 *y = gs_pop_y < ymax ? gs_pop_y : ymax;
3899 }
3900
3901 bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
3902 {
3903 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid window") );
3904
3905 wxCHECK_MSG( menu != NULL, FALSE, wxT("invalid popup-menu") );
3906
3907 SetInvokingWindow( menu, this );
3908
3909 menu->UpdateUI();
3910
3911 gs_pop_x = x;
3912 gs_pop_y = y;
3913 ClientToScreen( &gs_pop_x, &gs_pop_y );
3914
3915 bool is_waiting = TRUE;
3916
3917 gtk_signal_connect( GTK_OBJECT(menu->m_menu),
3918 "hide",
3919 GTK_SIGNAL_FUNC(gtk_pop_hide_callback),
3920 (gpointer)&is_waiting );
3921
3922 gtk_menu_popup(
3923 GTK_MENU(menu->m_menu),
3924 (GtkWidget *) NULL, // parent menu shell
3925 (GtkWidget *) NULL, // parent menu item
3926 wxPopupMenuPositionCallback, // function to position it
3927 NULL, // client data
3928 0, // button used to activate it
3929 gs_timeLastClick // the time of activation
3930 );
3931
3932 while (is_waiting)
3933 {
3934 while (gtk_events_pending())
3935 gtk_main_iteration();
3936 }
3937
3938 return TRUE;
3939 }
3940
3941 #endif // wxUSE_MENUS_NATIVE
3942
3943 #if wxUSE_DRAG_AND_DROP
3944
3945 void wxWindowGTK::SetDropTarget( wxDropTarget *dropTarget )
3946 {
3947 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3948
3949 GtkWidget *dnd_widget = GetConnectWidget();
3950
3951 if (m_dropTarget) m_dropTarget->UnregisterWidget( dnd_widget );
3952
3953 if (m_dropTarget) delete m_dropTarget;
3954 m_dropTarget = dropTarget;
3955
3956 if (m_dropTarget) m_dropTarget->RegisterWidget( dnd_widget );
3957 }
3958
3959 #endif // wxUSE_DRAG_AND_DROP
3960
3961 GtkWidget* wxWindowGTK::GetConnectWidget()
3962 {
3963 GtkWidget *connect_widget = m_widget;
3964 if (m_wxwindow) connect_widget = m_wxwindow;
3965
3966 return connect_widget;
3967 }
3968
3969 bool wxWindowGTK::IsOwnGtkWindow( GdkWindow *window )
3970 {
3971 if (m_wxwindow)
3972 return (window == GTK_PIZZA(m_wxwindow)->bin_window);
3973
3974 return (window == m_widget->window);
3975 }
3976
3977 bool wxWindowGTK::SetFont( const wxFont &font )
3978 {
3979 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid window") );
3980
3981 if (!wxWindowBase::SetFont(font))
3982 {
3983 return FALSE;
3984 }
3985
3986 wxColour sysbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
3987 if ( sysbg == m_backgroundColour )
3988 {
3989 m_backgroundColour = wxNullColour;
3990 ApplyWidgetStyle();
3991 m_backgroundColour = sysbg;
3992 }
3993 else
3994 {
3995 ApplyWidgetStyle();
3996 }
3997
3998 return TRUE;
3999 }
4000
4001 void wxWindowGTK::DoCaptureMouse()
4002 {
4003 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4004
4005 GdkWindow *window = (GdkWindow*) NULL;
4006 if (m_wxwindow)
4007 window = GTK_PIZZA(m_wxwindow)->bin_window;
4008 else
4009 window = GetConnectWidget()->window;
4010
4011 wxCHECK_RET( window, _T("CaptureMouse() failed") );
4012
4013 wxCursor* cursor = & m_cursor;
4014 if (!cursor->Ok())
4015 cursor = wxSTANDARD_CURSOR;
4016
4017 gdk_pointer_grab( window, FALSE,
4018 (GdkEventMask)
4019 (GDK_BUTTON_PRESS_MASK |
4020 GDK_BUTTON_RELEASE_MASK |
4021 GDK_POINTER_MOTION_HINT_MASK |
4022 GDK_POINTER_MOTION_MASK),
4023 (GdkWindow *) NULL,
4024 cursor->GetCursor(),
4025 (guint32)GDK_CURRENT_TIME );
4026 g_captureWindow = this;
4027 g_captureWindowHasMouse = TRUE;
4028 }
4029
4030 void wxWindowGTK::DoReleaseMouse()
4031 {
4032 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4033
4034 wxCHECK_RET( g_captureWindow, wxT("can't release mouse - not captured") );
4035
4036 g_captureWindow = (wxWindowGTK*) NULL;
4037
4038 GdkWindow *window = (GdkWindow*) NULL;
4039 if (m_wxwindow)
4040 window = GTK_PIZZA(m_wxwindow)->bin_window;
4041 else
4042 window = GetConnectWidget()->window;
4043
4044 if (!window)
4045 return;
4046
4047 gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
4048 }
4049
4050 /* static */
4051 wxWindow *wxWindowBase::GetCapture()
4052 {
4053 return (wxWindow *)g_captureWindow;
4054 }
4055
4056 bool wxWindowGTK::IsRetained() const
4057 {
4058 return FALSE;
4059 }
4060
4061 void wxWindowGTK::SetScrollbar( int orient, int pos, int thumbVisible,
4062 int range, bool refresh )
4063 {
4064 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4065
4066 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
4067
4068 m_hasScrolling = TRUE;
4069
4070 if (orient == wxHORIZONTAL)
4071 {
4072 float fpos = (float)pos;
4073 float frange = (float)range;
4074 float fthumb = (float)thumbVisible;
4075 if (fpos > frange-fthumb) fpos = frange-fthumb;
4076 if (fpos < 0.0) fpos = 0.0;
4077
4078 if ((fabs(frange-m_hAdjust->upper) < 0.2) &&
4079 (fabs(fthumb-m_hAdjust->page_size) < 0.2))
4080 {
4081 SetScrollPos( orient, pos, refresh );
4082 return;
4083 }
4084
4085 m_oldHorizontalPos = fpos;
4086
4087 m_hAdjust->lower = 0.0;
4088 m_hAdjust->upper = frange;
4089 m_hAdjust->value = fpos;
4090 m_hAdjust->step_increment = 1.0;
4091 m_hAdjust->page_increment = (float)(wxMax(fthumb,0));
4092 m_hAdjust->page_size = fthumb;
4093 }
4094 else
4095 {
4096 float fpos = (float)pos;
4097 float frange = (float)range;
4098 float fthumb = (float)thumbVisible;
4099 if (fpos > frange-fthumb) fpos = frange-fthumb;
4100 if (fpos < 0.0) fpos = 0.0;
4101
4102 if ((fabs(frange-m_vAdjust->upper) < 0.2) &&
4103 (fabs(fthumb-m_vAdjust->page_size) < 0.2))
4104 {
4105 SetScrollPos( orient, pos, refresh );
4106 return;
4107 }
4108
4109 m_oldVerticalPos = fpos;
4110
4111 m_vAdjust->lower = 0.0;
4112 m_vAdjust->upper = frange;
4113 m_vAdjust->value = fpos;
4114 m_vAdjust->step_increment = 1.0;
4115 m_vAdjust->page_increment = (float)(wxMax(fthumb,0));
4116 m_vAdjust->page_size = fthumb;
4117 }
4118
4119 if (orient == wxHORIZONTAL)
4120 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
4121 else
4122 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
4123 }
4124
4125 void wxWindowGTK::SetScrollPos( int orient, int pos, bool WXUNUSED(refresh) )
4126 {
4127 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4128
4129 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
4130
4131 if (orient == wxHORIZONTAL)
4132 {
4133 float fpos = (float)pos;
4134 if (fpos > m_hAdjust->upper - m_hAdjust->page_size) fpos = m_hAdjust->upper - m_hAdjust->page_size;
4135 if (fpos < 0.0) fpos = 0.0;
4136 m_oldHorizontalPos = fpos;
4137
4138 if (fabs(fpos-m_hAdjust->value) < 0.2) return;
4139 m_hAdjust->value = fpos;
4140 }
4141 else
4142 {
4143 float fpos = (float)pos;
4144 if (fpos > m_vAdjust->upper - m_vAdjust->page_size) fpos = m_vAdjust->upper - m_vAdjust->page_size;
4145 if (fpos < 0.0) fpos = 0.0;
4146 m_oldVerticalPos = fpos;
4147
4148 if (fabs(fpos-m_vAdjust->value) < 0.2) return;
4149 m_vAdjust->value = fpos;
4150 }
4151
4152 if (m_wxwindow->window)
4153 {
4154 if (orient == wxHORIZONTAL)
4155 {
4156 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
4157 (GtkSignalFunc) gtk_window_hscroll_callback, (gpointer) this );
4158
4159 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
4160
4161 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
4162 (GtkSignalFunc) gtk_window_hscroll_callback, (gpointer) this );
4163 }
4164 else
4165 {
4166 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
4167 (GtkSignalFunc) gtk_window_vscroll_callback, (gpointer) this );
4168
4169 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
4170
4171 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
4172 (GtkSignalFunc) gtk_window_vscroll_callback, (gpointer) this );
4173 }
4174 }
4175 }
4176
4177 int wxWindowGTK::GetScrollThumb( int orient ) const
4178 {
4179 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4180
4181 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
4182
4183 if (orient == wxHORIZONTAL)
4184 return (int)(m_hAdjust->page_size+0.5);
4185 else
4186 return (int)(m_vAdjust->page_size+0.5);
4187 }
4188
4189 int wxWindowGTK::GetScrollPos( int orient ) const
4190 {
4191 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4192
4193 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
4194
4195 if (orient == wxHORIZONTAL)
4196 return (int)(m_hAdjust->value+0.5);
4197 else
4198 return (int)(m_vAdjust->value+0.5);
4199 }
4200
4201 int wxWindowGTK::GetScrollRange( int orient ) const
4202 {
4203 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4204
4205 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
4206
4207 if (orient == wxHORIZONTAL)
4208 return (int)(m_hAdjust->upper+0.5);
4209 else
4210 return (int)(m_vAdjust->upper+0.5);
4211 }
4212
4213 void wxWindowGTK::ScrollWindow( int dx, int dy, const wxRect* WXUNUSED(rect) )
4214 {
4215 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4216
4217 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
4218
4219 // No scrolling requested.
4220 if ((dx == 0) && (dy == 0)) return;
4221
4222 #ifndef __WXGTK20__
4223 if (!m_updateRegion.IsEmpty())
4224 {
4225 m_updateRegion.Offset( dx, dy );
4226
4227 int cw = 0;
4228 int ch = 0;
4229 GetClientSize( &cw, &ch );
4230 m_updateRegion.Intersect( 0, 0, cw, ch );
4231 }
4232
4233 if (!m_clearRegion.IsEmpty())
4234 {
4235 m_clearRegion.Offset( dx, dy );
4236
4237 int cw = 0;
4238 int ch = 0;
4239 GetClientSize( &cw, &ch );
4240 m_clearRegion.Intersect( 0, 0, cw, ch );
4241 }
4242
4243 m_clipPaintRegion = TRUE;
4244
4245 gtk_pizza_scroll( GTK_PIZZA(m_wxwindow), -dx, -dy );
4246
4247 m_clipPaintRegion = FALSE;
4248 #else
4249
4250 gdk_window_scroll( GTK_PIZZA(m_wxwindow)->bin_window, dx, dy );
4251
4252 GTK_PIZZA(m_wxwindow)->xoffset += dx;
4253 GTK_PIZZA(m_wxwindow)->yoffset += dy;
4254
4255 #endif
4256
4257 }
4258
4259
4260 // Find the wxWindow at the current mouse position, also returning the mouse
4261 // position.
4262 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
4263 {
4264 pt = wxGetMousePosition();
4265 wxWindow* found = wxFindWindowAtPoint(pt);
4266 return found;
4267 }
4268
4269 // Get the current mouse position.
4270 wxPoint wxGetMousePosition()
4271 {
4272 /* This crashes when used within wxHelpContext,
4273 so we have to use the X-specific implementation below.
4274 gint x, y;
4275 GdkModifierType *mask;
4276 (void) gdk_window_get_pointer(NULL, &x, &y, mask);
4277
4278 return wxPoint(x, y);
4279 */
4280
4281 int x, y;
4282 GdkWindow* windowAtPtr = gdk_window_at_pointer(& x, & y);
4283 if (!windowAtPtr)
4284 return wxPoint(-999, -999);
4285
4286 Display *display = GDK_WINDOW_XDISPLAY(windowAtPtr);
4287 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
4288 Window rootReturn, childReturn;
4289 int rootX, rootY, winX, winY;
4290 unsigned int maskReturn;
4291
4292 XQueryPointer (display,
4293 rootWindow,
4294 &rootReturn,
4295 &childReturn,
4296 &rootX, &rootY, &winX, &winY, &maskReturn);
4297 return wxPoint(rootX, rootY);
4298
4299 }
4300
4301 // ----------------------------------------------------------------------------
4302 // wxDCModule
4303 // ----------------------------------------------------------------------------
4304
4305 class wxWinModule : public wxModule
4306 {
4307 public:
4308 bool OnInit();
4309 void OnExit();
4310
4311 private:
4312 DECLARE_DYNAMIC_CLASS(wxWinModule)
4313 };
4314
4315 IMPLEMENT_DYNAMIC_CLASS(wxWinModule, wxModule)
4316
4317 bool wxWinModule::OnInit()
4318 {
4319 // g_eraseGC = gdk_gc_new( GDK_ROOT_PARENT() );
4320 // gdk_gc_set_fill( g_eraseGC, GDK_SOLID );
4321
4322 return TRUE;
4323 }
4324
4325 void wxWinModule::OnExit()
4326 {
4327 if (g_eraseGC)
4328 gdk_gc_unref( g_eraseGC );
4329 }
4330