clean up PYConfig.h/cc
[platform/upstream/ibus-libpinyin.git] / src / PYEditor.cc
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-pinyin - The Chinese PinYin engine for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #include "PYText.h"
22 #include "PYEditor.h"
23
24 namespace PY {
25
26 Editor::Editor (PinyinProperties & props, Config & config)
27     : m_text (128),
28       m_cursor (0),
29       m_props (props),
30       m_config (config)
31 {
32 }
33
34 Editor::~Editor (void)
35 {
36 }
37
38 gboolean
39 Editor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
40 {
41     modifiers &= (IBUS_CONTROL_MASK |
42                   IBUS_MOD1_MASK |
43                   IBUS_SUPER_MASK |
44                   IBUS_HYPER_MASK |
45                   IBUS_META_MASK);
46     /* ignore key events with some masks */
47     if (modifiers != 0)
48         return TRUE;
49
50     if (keyval >= IBUS_exclam && keyval <= IBUS_asciitilde) {
51         /* char key */
52         m_text.insert (m_cursor++, keyval);
53         update ();
54         return TRUE;
55     }
56     else {
57         /* control key */
58         if (!m_text)
59             return FALSE;
60     }
61
62     switch (keyval) {
63     case IBUS_BackSpace:
64         if (m_cursor > 0) {
65             m_text.erase (--m_cursor, 1);
66             update ();
67         }
68         return TRUE;
69     case IBUS_Delete:
70     case IBUS_KP_Delete:
71         if (m_cursor < m_text.length ()) {
72             m_text.erase (m_cursor, 1);
73             update ();
74         }
75         return TRUE;
76     case IBUS_Left:
77     case IBUS_KP_Left:
78         if (!m_text)
79             return FALSE;
80         if (m_cursor > 0) {
81             m_cursor --;
82             update ();
83         }
84         return TRUE;
85     case IBUS_Right:
86     case IBUS_KP_Right:
87         if (m_cursor < m_text.length ()) {
88             m_cursor ++;
89             update ();
90         }
91         return TRUE;
92     case IBUS_space:
93     case IBUS_Return:
94     case IBUS_KP_Enter:
95         {
96             StaticText text (m_text);
97             commitText (text);
98             reset ();
99         }
100         return TRUE;
101     case IBUS_Escape:
102         reset ();
103         return TRUE;
104     default:
105         return TRUE;
106     }
107 }
108
109 void
110 Editor::reset (void)
111 {
112     gboolean need_update = (m_cursor != 0 || !m_text.empty ());
113     m_cursor = 0;
114     m_text = "";
115     if (need_update)
116         update ();
117 }
118
119 void
120 Editor::pageUp (void)
121 {
122 }
123
124 void
125 Editor::pageDown (void)
126 {
127 }
128
129 void
130 Editor::cursorUp (void)
131 {
132 }
133
134 void
135 Editor::cursorDown (void)
136 {
137 }
138
139 void
140 Editor::candidateClicked (guint index, guint button, guint state)
141 {
142 }
143
144 void
145 Editor::update (void)
146 {
147     if (m_text) {
148         StaticText text (m_text);
149         text.appendAttribute (IBUS_ATTR_TYPE_UNDERLINE, IBUS_ATTR_UNDERLINE_SINGLE, 0, -1);
150         updatePreeditText (text, m_cursor, TRUE);
151     }
152     else {
153         hidePreeditText ();
154     }
155 }
156
157 };
158