Back to index

nux  3.0.0
InputMethodIBus.h
Go to the documentation of this file.
00001 /*
00002 * Copyright 2010 Inalogic® Inc.
00003 *
00004 * This program is free software: you can redistribute it and/or modify it
00005 * under the terms of the GNU Lesser General Public License, as
00006 * published by the  Free Software Foundation; either version 2.1 or 3.0
00007 * of the License.
00008 *
00009 * This program is distributed in the hope that it will be useful, but
00010 * WITHOUT ANY WARRANTY; without even the implied warranties of
00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012 * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013 * License for more details.
00014 *
00015 * You should have received a copy of both the GNU Lesser General Public
00016 * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017 *
00018 * Authored by: Brandon Schaefer <brandontschaefer@gmail.com>
00019 *              Jay Taoko <jaytaoko@inalogic.com>
00020 *
00021 */
00022 
00023 
00024 #ifndef INPUTMETHODIBUS_H
00025 #define INPUTMETHODIBUS_H
00026 
00027 #include <Nux/TextEntry.h>
00028 #include <ibus.h>
00029 
00030 namespace nux
00031 {
00032 
00033   class IBusIMEContext;
00034   class TextEntry;
00035 
00036   // FIXME This class should be reworked to replace the mouse_state
00037   // with the hardware key_code.
00038   class KeyEvent
00039   {
00040   public:
00041 
00042     KeyEvent(NuxEventType type,
00043       unsigned int key_sym,
00044       unsigned int key_code, unsigned int event_flags, const char* character)
00045       : type_(type)
00046       , key_sym_(key_sym)
00047       , key_code_(key_code)
00048       , key_modifiers_(event_flags)
00049       , character_(character ? character : "")
00050     {
00051     }
00052 
00053     NuxEventType type() const {return type_;}
00054     unsigned int key_sym() const {return key_sym_;}
00055     unsigned int key_code() const {return key_code_;}
00056     unsigned int flags() const {return key_modifiers_;}
00057     std::string character() const {return character_;}
00058 
00059     bool IsShiftDown() const { return (key_modifiers_ & KEY_MODIFIER_SHIFT) != 0; }
00060     bool IsControlDown() const { return (key_modifiers_ & KEY_MODIFIER_CTRL) != 0; }
00061     bool IsCapsLockDown() const { return (key_modifiers_ & KEY_MODIFIER_CAPS_LOCK) != 0; }
00062     bool IsAltDown() const { return (key_modifiers_ & KEY_MODIFIER_ALT) != 0; }
00063 
00064   private:
00065     EventType type_;
00066     unsigned int key_sym_;
00067     unsigned int key_code_;
00068     unsigned int key_modifiers_;
00069     std::string character_;
00070 
00071     KeyEvent(const KeyEvent&);
00072     void operator = (const KeyEvent&);
00073   };
00074 
00075   // Used for passing data to ProcessKeyEventDone function()
00076   class ProcessKeyEventData
00077   {
00078   public:
00079     ProcessKeyEventData(IBusIMEContext* context,
00080       const KeyEvent& event)
00081       : context(context)
00082       , event(event.type(), event.key_sym(), event.key_code(), event.flags(), event.character().c_str())
00083     {
00084 
00085     }
00086     IBusIMEContext* context;
00087     KeyEvent event;
00088   };
00089 
00090   // Implements IMEContext to integrate ibus input method framework
00091   class IBusIMEContext
00092   {
00093   public:
00094     explicit IBusIMEContext(TextEntry* text_entry);
00095     virtual ~IBusIMEContext();
00096 
00097     // views::IMEContext implementations:
00098     virtual void Focus();
00099     virtual void Blur();
00100     virtual void Reset();
00101     virtual bool FilterKeyEvent(const KeyEvent& event);
00102     virtual void SetSurrounding(const std::wstring& text, int cursor_pos);
00103 
00104     bool IsConnected() const;
00105     bool IsHotkeyEvent(EventType type, unsigned long keysym, unsigned long modifiers) const;
00106 
00107   protected:
00108     static std::vector<Event> ParseIBusHotkeys(const gchar** keybindings);
00109 
00110   private:
00111     void CreateContext();
00112     void DestroyContext();
00113 
00114     void UpdateCursorLocation();
00115     static void UpdateHotkeys();
00116 
00117     // Event handlers for IBusBus:
00118     //CHROMEG_CALLBACK_0(IBusIMEContext, void, OnConnected, IBusBus*);
00119     static void OnConnected_(IBusBus* bus, void* data) {nuxDebugMsg("***IBusIMEContext::OnConnected***"); static_cast<IBusIMEContext*>(data)->OnConnected(bus);}
00120     void OnConnected(IBusBus *bus);
00121 
00122     //CHROMEG_CALLBACK_0(IBusIMEContext, void, OnDisconnected, IBusBus*);
00123     static void OnDisconnected_(IBusBus* bus, void* data) {static_cast<IBusIMEContext*>(data)->OnDisconnected(bus);}
00124     void OnDisconnected(IBusBus *bus);
00125 
00126     static void OnConfigChanged_(IBusConfig* config, gchar* section, gchar* name, GVariant* value, gpointer data) {static_cast<IBusIMEContext*>(data)->OnConfigChanged(config, section, name, value);}
00127     void OnConfigChanged(IBusConfig* config, gchar* section, gchar* name, GVariant* value);
00128 
00129 //     // Event handlers for IBusIMEContext:
00130     //CHROMEG_CALLBACK_1(IBusIMEContext, void, OnCommitText, IBusInputContext*, IBusText*);
00131     static void OnCommitText_(IBusInputContext* context, IBusText* text, void* data) {static_cast<IBusIMEContext*>(data)->OnCommitText(context, text);}
00132     void OnCommitText(IBusInputContext *context, IBusText* text);
00133 
00134     //CHROMEG_CALLBACK_3(IBusIMEContext, void, OnUpdatePreeditText, IBusInputContext*, IBusText*, guint, gboolean);
00135     static void OnUpdatePreeditText_(IBusInputContext* context, IBusText* text, guint cursor_pos, gboolean visible, void* data) {reinterpret_cast<IBusIMEContext*>(data)->OnUpdatePreeditText(context, text, cursor_pos, visible);}
00136     void OnUpdatePreeditText(IBusInputContext *context, IBusText* text, guint cursor_pos, gboolean visible);
00137 
00138     //CHROMEG_CALLBACK_0(IBusIMEContext, void, OnShowPreeditText, IBusInputContext*);
00139     static void OnShowPreeditText_(IBusInputContext* context, void* data) {static_cast<IBusIMEContext*>(data)->OnShowPreeditText(context);}
00140     void OnShowPreeditText(IBusInputContext *context);
00141 
00142 //     CHROMEG_CALLBACK_0(IBusIMEContext, void, OnHidePreeditText, IBusInputContext*);
00143     static void OnHidePreeditText_(IBusInputContext* context, void* data) {static_cast<IBusIMEContext*>(data)->OnHidePreeditText(context);}
00144     void OnHidePreeditText(IBusInputContext *context);
00145 
00146 //     CHROMEG_CALLBACK_0(IBusIMEContext, void, OnEnable, IBusInputContext*);
00147     static void OnEnable_(IBusInputContext* context, void* data) {static_cast<IBusIMEContext*>(data)->OnEnable(context);}
00148     void OnEnable(IBusInputContext *context);
00149 
00150 //     CHROMEG_CALLBACK_0(IBusIMEContext, void, OnDisable, IBusInputContext*);
00151     static void OnDisable_(IBusInputContext* context, void* data) {static_cast<IBusIMEContext*>(data)->OnDisable(context);}
00152     void OnDisable(IBusInputContext *context);
00153 
00154 //     CHROMEG_CALLBACK_0(IBusIMEContext, void, OnDestroy, IBusInputContext*);
00155     static void OnDestroy_(IBusInputContext* context, void* data) {static_cast<IBusIMEContext*>(data)->OnDestroy(context);}
00156     void OnDestroy(IBusInputContext *context);
00157 
00158     static void ProcessKeyEventDone(IBusInputContext* context,
00159       GAsyncResult* res,
00160       ProcessKeyEventData* data);
00161 
00162     TextEntry* text_entry_;
00163     IBusInputContext* context_;
00164     bool is_focused_;
00165 
00166     static IBusBus* bus_;
00167     static std::vector<Event> hotkeys_;
00168 
00169     IBusIMEContext(const IBusIMEContext&);
00170     void operator = (const IBusIMEContext&);
00171   };
00172 
00173 }
00174 #endif  // INPUTMETHODIBUS_H
00175