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