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