+
+static bool
+wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
+ wxWindowGTK *win,
+ GdkEventKey *gdk_event)
+{
+ // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
+ // but only event->keyval which is quite useless to us, so remember
+ // the last character from GDK_KEY_PRESS and reuse it as last resort
+ //
+ // NB: should be MT-safe as we're always called from the main thread only
+ static struct
+ {
+ KeySym keysym;
+ long keycode;
+ } s_lastKeyPress = { 0, 0 };
+
+ KeySym keysym = gdk_event->keyval;
+
+ wxLogTrace(TRACE_KEYS, _T("Key %s event: keysym = %ld"),
+ event.GetEventType() == wxEVT_KEY_UP ? _T("release")
+ : _T("press"),
+ keysym);
+
+ long key_code = wxTranslateKeySymToWXKey(keysym, FALSE /* !isChar */);
+
+ if ( !key_code )
+ {
+ // do we have the translation or is it a plain ASCII character?
+ if ( (gdk_event->length == 1) || wxIsAsciiKeysym(keysym) )
+ {
+ // we should use keysym if it is ASCII as X does some translations
+ // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
+ // which we don't want here (but which we do use for OnChar())
+ if ( !wxIsAsciiKeysym(keysym) )
+ {
+ keysym = (KeySym)gdk_event->string[0];
+ }
+
+ // we want to always get the same key code when the same key is
+ // pressed regardless of the state of the modifies, i.e. on a
+ // standard US keyboard pressing '5' or '%' ('5' key with
+ // Shift) should result in the same key code in OnKeyDown():
+ // '5' (although OnChar() will get either '5' or '%').
+ //
+ // to do it we first translate keysym to keycode (== scan code)
+ // and then back but always using the lower register
+ Display *dpy = (Display *)wxGetDisplay();
+ KeyCode keycode = XKeysymToKeycode(dpy, keysym);
+
+ wxLogTrace(TRACE_KEYS, _T("\t-> keycode %d"), keycode);
+
+ KeySym keysymNormalized = XKeycodeToKeysym(dpy, keycode, 0);
+
+ // use the normalized, i.e. lower register, keysym if we've
+ // got one
+ key_code = keysymNormalized ? keysymNormalized : keysym;
+
+ // as explained above, we want to have lower register key codes
+ // normally but for the letter keys we want to have the upper ones
+ //
+ // NB: don't use XConvertCase() here, we want to do it for letters
+ // only
+ key_code = toupper(key_code);
+ }
+ else // non ASCII key, what to do?
+ {
+ // by default, ignore it
+ key_code = 0;
+
+ // but if we have cached information from the last KEY_PRESS
+ if ( gdk_event->type == GDK_KEY_RELEASE )
+ {
+ // then reuse it
+ if ( keysym == s_lastKeyPress.keysym )
+ {
+ key_code = s_lastKeyPress.keycode;
+ }
+ }
+ }
+
+ if ( gdk_event->type == GDK_KEY_PRESS )
+ {
+ // remember it to be reused for KEY_UP event later
+ s_lastKeyPress.keysym = keysym;
+ s_lastKeyPress.keycode = key_code;
+ }
+ }
+
+ wxLogTrace(TRACE_KEYS, _T("\t-> wxKeyCode %ld"), key_code);
+
+ // sending unknown key events doesn't really make sense
+ if ( !key_code )
+ return FALSE;
+
+ // now fill all the other fields
+ wxFillOtherKeyEventFields(event, win, gdk_event);
+
+ event.m_keyCode = key_code;
+
+ return TRUE;
+}
+
+
+#ifdef __WXGTK20__
+struct wxGtkIMData
+{
+ GtkIMContext *context;
+ GdkEventKey *lastKeyEvent;
+
+ wxGtkIMData()
+ {
+ context = gtk_im_multicontext_new();
+ lastKeyEvent = NULL;
+ }
+ ~wxGtkIMData()
+ {
+ g_object_unref(context);
+ }
+};
+#endif
+
+static gint gtk_window_key_press_callback( GtkWidget *widget,
+ GdkEventKey *gdk_event,
+ wxWindow *win )
+{
+ DEBUG_MAIN_THREAD
+
+ if (g_isIdle)
+ wxapp_install_idle_handler();
+
+ if (!win->m_hasVMT)
+ return FALSE;
+ if (g_blockEventsOnDrag)
+ return FALSE;
+
+#ifdef __WXGTK20__
+ // We have to pass key press events through GTK+'s Input Method context
+ // object in order to get correct characters. By doing so, we loose the
+ // ability to let other GTK+'s handlers (namely, widgets' default signal
+ // handlers) handle the signal by returning false from this callback.
+ // Because GTK+ sends the events to parent widgets as well, we can't
+ // afford loosing it, otherwise native widgets inserted into wxPanel
+ // would break in subtle ways (e.g. spacebar would no longer toggle
+ // wxCheckButton's state). Therefore, we only pass the event to IM if it
+ // originated in this window's widget, which we detect by checking if we've
+ // seen the same event before (no events from children are lost this way,
+ // because gtk_window_key_press_callback is installed for native controls
+ // as well and the wxKeyEvent it creates propagates upwards).
+ static GdkEventKey s_lastEvent;
+
+ bool useIM = (win->m_imData != NULL) &&
+ memcmp(gdk_event, &s_lastEvent, sizeof(GdkEventKey)) != 0;
+
+ s_lastEvent = *gdk_event;
+#endif
+
+ wxKeyEvent event( wxEVT_KEY_DOWN );
+ if ( !wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
+ {
+ // unknown key pressed, ignore (the event would be useless anyhow)
+#ifdef __WXGTK20__
+ if ( useIM )
+ {
+ // it may be useful for the input method, though:
+ win->m_imData->lastKeyEvent = gdk_event;
+ bool ret = gtk_im_context_filter_keypress(win->m_imData->context,
+ gdk_event);
+ win->m_imData->lastKeyEvent = NULL;
+ return ret;
+ }
+#endif
+ return FALSE;
+ }
+
+ // Emit KEY_DOWN event
+ bool ret = win->GetEventHandler()->ProcessEvent( event );
+
+#if wxUSE_ACCEL
+ if (!ret)
+ {
+ wxWindowGTK *ancestor = win;
+ while (ancestor)
+ {
+ int command = ancestor->GetAcceleratorTable()->GetCommand( event );
+ if (command != -1)
+ {
+ wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
+ ret = ancestor->GetEventHandler()->ProcessEvent( command_event );
+ break;
+ }
+ if (ancestor->IsTopLevel())
+ break;
+ ancestor = ancestor->GetParent();
+ }
+ }
+#endif // wxUSE_ACCEL
+
+ // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
+ // will only be sent if it is not in an accelerator table.
+ if (!ret)
+ {
+#ifdef __WXGTK20__
+ if (useIM)
+ {
+ // In GTK 2.0, we need to hand over the key event to an input method
+ // and the IM will emit a "commit" event containing the actual utf8
+ // character. In that case the EVT_CHAR events will be sent from
+ // there.
+ win->m_imData->lastKeyEvent = gdk_event;
+ if ( gtk_im_context_filter_keypress(win->m_imData->context,
+ gdk_event) )
+ {
+ win->m_imData->lastKeyEvent = NULL;
+ wxLogTrace(TRACE_KEYS, _T("Key event intercepted by IM"));
+ return TRUE;
+ }
+ else
+ win->m_imData->lastKeyEvent = NULL;
+ }
+#endif
+
+ long key_code;
+ KeySym keysym = gdk_event->keyval;
+ // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
+ key_code = wxTranslateKeySymToWXKey(keysym, TRUE /* isChar */);
+ if ( !key_code )
+ {
+ if ( gdk_event->length == 1 )
+ {
+ key_code = (unsigned char)gdk_event->string[0];
+ }
+ else if ( wxIsAsciiKeysym(keysym) )
+ {
+ // ASCII key
+ key_code = (unsigned char)keysym;
+ }
+ }
+
+ if ( key_code )
+ {
+ wxLogTrace(TRACE_KEYS, _T("Char event: %ld"), key_code);
+
+ event.m_keyCode = key_code;
+
+ // Implement OnCharHook by checking ancesteror top level windows
+ wxWindow *parent = win;
+ while (parent && !parent->IsTopLevel())
+ parent = parent->GetParent();
+ if (parent)
+ {
+ event.SetEventType( wxEVT_CHAR_HOOK );
+ ret = parent->GetEventHandler()->ProcessEvent( event );
+ }
+
+ if (!ret)
+ {
+ event.SetEventType(wxEVT_CHAR);
+ ret = win->GetEventHandler()->ProcessEvent( event );
+ }
+ }
+ }
+
+ // win is a control: tab can be propagated up
+ if ( !ret &&
+ ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) &&
+// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
+// have this style, yet choose not to process this particular TAB in which
+// case TAB must still work as a navigational character
+// JS: enabling again to make consistent with other platforms
+// (with wxTE_PROCESS_TAB you have to call Navigate to get default
+// navigation behaviour)
+#if wxUSE_TEXTCTRL
+ (! (win->HasFlag(wxTE_PROCESS_TAB) && win->IsKindOf(CLASSINFO(wxTextCtrl)) )) &&
+#endif
+ win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
+ {
+ wxNavigationKeyEvent new_event;
+ new_event.SetEventObject( win->GetParent() );
+ // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
+ new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
+ // CTRL-TAB changes the (parent) window, i.e. switch notebook page
+ new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
+ new_event.SetCurrentFocus( win );
+ ret = win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
+ }
+
+ // generate wxID_CANCEL if <esc> has been pressed (typically in dialogs)
+ if ( !ret &&
+ (gdk_event->keyval == GDK_Escape) )