1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#ifndef GtkInputMethodFilter_h
21#define GtkInputMethodFilter_h
22
23#include "GRefPtrGtk.h"
24#include "IntRect.h"
25#include <gdk/gdk.h>
26#include <wtf/text/WTFString.h>
27
28typedef struct _GtkIMContext GtkIMContext;
29typedef struct _GtkWidget GtkWidget;
30
31namespace WebCore {
32
33class GtkInputMethodFilter {
34public:
35    GtkInputMethodFilter();
36    ~GtkInputMethodFilter();
37
38    bool filterKeyEvent(GdkEventKey*);
39    void notifyMouseButtonPress();
40    void notifyFocusedIn();
41    void notifyFocusedOut();
42    void resetContext();
43    void setEnabled(bool);
44
45    void handleCommit(const char* compositionString);
46    void handlePreeditChanged();
47    void handlePreeditStart();
48    void handlePreeditEnd();
49
50    void confirmComposition();
51    void cancelContextComposition();
52    void updatePreedit();
53    void setCursorRect(const IntRect& location);
54
55    GtkIMContext* context() { return m_context.get(); }
56
57    enum EventFakedForComposition {
58        EventFaked,
59        EventNotFaked
60    };
61
62protected:
63    enum ResultsToSend {
64        Preedit = 1 << 1,
65        Composition = 1 << 2,
66        PreeditAndComposition = Preedit + Composition
67    };
68
69    void setWidget(GtkWidget*);
70    virtual bool canEdit() = 0;
71    virtual bool sendSimpleKeyEvent(GdkEventKey*, WTF::String eventString = String(), EventFakedForComposition = EventNotFaked) = 0;
72    virtual bool sendKeyEventWithCompositionResults(GdkEventKey*, ResultsToSend = PreeditAndComposition, EventFakedForComposition = EventNotFaked) = 0;
73    virtual void confirmCompositionText(String composition) = 0;
74    virtual void confirmCurrentComposition() = 0;
75    virtual void cancelCurrentComposition() = 0;
76    virtual void setPreedit(String, int cursorOffset) = 0;
77
78    WTF::String m_confirmedComposition;
79    WTF::String m_preedit;
80    int m_cursorOffset;
81
82private:
83    void sendCompositionAndPreeditWithFakeKeyEvents(ResultsToSend);
84
85    GRefPtr<GtkIMContext> m_context;
86    GtkWidget* m_widget;
87    bool m_enabled;
88    bool m_composingTextCurrently;
89    bool m_filteringKeyEvent;
90    bool m_preeditChanged;
91    bool m_preventNextCommit;
92    bool m_justSentFakeKeyUp;
93    unsigned int m_lastFilteredKeyPressCodeWithNoResults;
94    IntPoint m_lastCareLocation;
95};
96
97} // namespace WebCore
98
99#endif // GtkInputMethodFilter_h
100