2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6
* The contents of this file are subject to the terms of either the GNU Lesser
7
* General Public License Version 2.1 only ("LGPL") or the Common Development and
8
* Distribution License ("CDDL")(collectively, the "License"). You may not use this
9
* file except in compliance with the License. You can obtain a copy of the CDDL at
10
* http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11
* http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12
* specific language governing permissions and limitations under the License. When
13
* distributing the software, include this License Header Notice in each file and
14
* include the full text of the License in the License file as well as the
17
* NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19
* For Covered Software in this distribution, this License shall be governed by the
20
* laws of the State of California (excluding conflict-of-law provisions).
21
* Any litigation relating to this License shall be subject to the jurisdiction of
22
* the Federal Courts of the Northern District of California and the state courts
23
* of the State of California, with venue lying in Santa Clara County, California.
27
* If you wish your version of this file to be governed by only the CDDL or only
28
* the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29
* include this software in this distribution under the [CDDL or LGPL Version 2.1]
30
* license." If you don't indicate a single choice of license, a recipient has the
31
* option to distribute your version of this file under either the CDDL or the LGPL
32
* Version 2.1, or to extend the choice of license to its licensees as provided
33
* above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34
* Version 2 license, then the option applies only if the new code is made subject
35
* to such option by the copyright holder.
42
#include "imi_gtkwin.h"
44
#include "imi_uiobjects.h"
47
const char *CGTKWinHandler::hanzi_image_file_name = "images/han.png";
48
const char *CGTKWinHandler::eng_image_file_name = "images/eng.png";
49
const char *CGTKWinHandler::cnpunc_image_file_name = "images/cnpunc.png";
50
const char *CGTKWinHandler::enpunc_image_file_name = "images/enpunc.png";
51
const char *CGTKWinHandler::fullwidth_image_file_name = "images/fullwidth.png";
52
const char *CGTKWinHandler::halfwidth_image_file_name = "images/halfwidth.png";
54
GtkWidget *CGTKWinHandler::hanzi_image = NULL;
55
GtkWidget *CGTKWinHandler::eng_image = NULL;
56
GtkWidget *CGTKWinHandler::cnpunc_image = NULL;
57
GtkWidget *CGTKWinHandler::enpunc_image = NULL;
58
GtkWidget *CGTKWinHandler::fullwidth_image = NULL;
59
GtkWidget *CGTKWinHandler::halfwidth_image = NULL;
63
key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
66
unsigned keycode = event->keyval;
67
unsigned keyvalue = event->keyval;
68
if (keyvalue < 0x20 && keyvalue > 0x7E)
71
CIMIView* pview = (CIMIView*)(data);
72
CKeyEvent key_event (keycode, keyvalue, event->state);
73
pview->onKeyEvent(key_event);
79
button_click_cn_en(GtkWidget* button, gpointer data)
83
CIMIView* pv = (CIMSAView*)data;
84
unsigned int mask = 0;
86
pv->updateWindows(mask);
92
button_click_fullhalf_punc(GtkWidget* button, gpointer data)
96
((CIMSAView*)data)->switchFullPunc(NULL);
101
button_click_fullhalf_simbol(GtkWidget* button, gpointer data)
105
((CIMSAView*)data)->switchFullSimbol(NULL);
109
CGTKWinHandler::CGTKWinHandler(CIMIView* pv)
110
: mp_view(pv), m_pWin(NULL), m_CandidataArea(NULL), m_PreeditArea(NULL),
111
m_pLangButton(NULL), m_pPuncButton(NULL), m_pSimbButton(NULL)
113
m_iconv = iconv_open("UTF-8", TWCHAR_ICONV_NAME);
117
CGTKWinHandler::~CGTKWinHandler()
119
iconv_close(m_iconv);
120
g_object_unref(hanzi_image);
121
g_object_unref(eng_image);
122
g_object_unref(cnpunc_image);
123
g_object_unref(enpunc_image);
124
g_object_unref(fullwidth_image);
125
g_object_unref(halfwidth_image);
129
CGTKWinHandler::load_images()
131
if (hanzi_image == NULL) {
132
hanzi_image = gtk_image_new_from_file(hanzi_image_file_name);
133
eng_image = gtk_image_new_from_file(eng_image_file_name);
134
cnpunc_image = gtk_image_new_from_file(cnpunc_image_file_name);
135
enpunc_image = gtk_image_new_from_file(enpunc_image_file_name);
136
fullwidth_image = gtk_image_new_from_file(fullwidth_image_file_name);
137
halfwidth_image = gtk_image_new_from_file(halfwidth_image_file_name);
139
g_object_ref(hanzi_image);
140
g_object_ref(eng_image);
141
g_object_ref(cnpunc_image);
142
g_object_ref(enpunc_image);
143
g_object_ref(fullwidth_image);
144
g_object_ref(halfwidth_image);
149
CGTKWinHandler::updateStatus(int key, int value)
152
case STATUS_ID_CN: switchCN( value != 0 ); break;
153
case STATUS_ID_FULLPUNC: switchFullPunc( value != 0 ); break;
154
case STATUS_ID_FULLSYMBOL: switchFullSimbol( value != 0 ); break;
159
CGTKWinHandler::switchCN(bool cn)
161
gtk_container_remove(GTK_CONTAINER(m_pLangButton), (!cn)?hanzi_image:eng_image);
162
gtk_container_add(GTK_CONTAINER(m_pLangButton), (cn)?hanzi_image:eng_image);
163
gtk_widget_show_all(m_pLangButton);
167
CGTKWinHandler::switchFullPunc(bool full)
169
gtk_container_remove(GTK_CONTAINER(m_pPuncButton), (!full)?cnpunc_image:enpunc_image);
170
gtk_container_add(GTK_CONTAINER(m_pPuncButton), (full)?cnpunc_image:enpunc_image);
171
gtk_widget_show_all(m_pPuncButton);
175
CGTKWinHandler::switchFullSimbol(bool full)
177
gtk_container_remove(GTK_CONTAINER(m_pSimbButton), (!full)?fullwidth_image:halfwidth_image);
178
gtk_container_add(GTK_CONTAINER(m_pSimbButton), (full)?fullwidth_image:halfwidth_image);
179
gtk_widget_show_all(m_pSimbButton);
183
CGTKWinHandler::createWindows()
188
GtkWidget *box, *box1;
192
m_pWin = window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
193
gtk_container_set_border_width(GTK_CONTAINER(window), 2);
194
box = gtk_vbox_new(FALSE, 1);
195
gtk_container_add(GTK_CONTAINER(window), box);
197
frame = gtk_frame_new(NULL);
198
m_CandidataArea = gtk_label_new("这里显示候选字/词");
199
gtk_container_add(GTK_CONTAINER(frame), m_CandidataArea);
200
gtk_label_set_justify(GTK_LABEL(m_CandidataArea), GTK_JUSTIFY_LEFT);
201
gtk_box_pack_end(GTK_BOX(box), frame, TRUE, TRUE, 1);
203
frame = gtk_frame_new(NULL);
204
m_PreeditArea = gtk_text_view_new();
205
gtk_text_view_set_editable(GTK_TEXT_VIEW(m_PreeditArea), false);
206
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(m_PreeditArea), true);
207
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (m_PreeditArea));
208
gtk_text_buffer_set_text(buffer, "请输入拼音", -1);
210
gtk_text_buffer_get_iter_at_offset(buffer, &ti, 0);
211
gtk_text_buffer_place_cursor(buffer, &ti);
213
gtk_container_add(GTK_CONTAINER(frame), m_PreeditArea);
214
gtk_box_pack_end(GTK_BOX(box), frame, TRUE, TRUE, 1);
216
box1 = gtk_hbox_new(FALSE, 1);
217
gtk_box_pack_start(GTK_BOX(box), box1, TRUE, TRUE, 1);
219
frame = gtk_frame_new(NULL);
220
chedit = gtk_label_new("最佳整句候选结果");
221
gtk_container_add(GTK_CONTAINER(frame), chedit);
222
gtk_label_set_justify(GTK_LABEL(chedit), GTK_JUSTIFY_LEFT);
223
gtk_box_pack_start(GTK_BOX(box1), frame, TRUE, TRUE, 1);
225
m_pSimbButton = button = gtk_button_new();
226
gtk_container_add(GTK_CONTAINER(button), halfwidth_image);
227
gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 2);
228
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button_click_fullhalf_simbol), mp_view);
230
m_pPuncButton = button = gtk_button_new();
231
gtk_container_add(GTK_CONTAINER(button), cnpunc_image);
232
gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 2);
233
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button_click_fullhalf_punc), mp_view);
235
m_pLangButton = button = gtk_button_new();
236
gtk_container_add(GTK_CONTAINER(button), hanzi_image);
237
gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 2);
238
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button_click_cn_en), mp_view);
240
gtk_window_set_decorated((GtkWindow*)window, FALSE);
241
g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(key_press_cb), mp_view);
242
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
244
g_signal_connect(G_OBJECT(m_pWin), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
245
gtk_widget_show_all(m_pWin);
251
CGTKWinHandler::updatePreedit(const IPreeditString* ppd)
253
TIConvSrcPtr src = (TIConvSrcPtr)(ppd->string());
254
size_t srclen = (ppd->size()+1)*sizeof(TWCHAR);
256
size_t dstlen = sizeof(m_buf)-1;
257
iconv(m_iconv, &src, &srclen, &dst, &dstlen);
259
//gtk_widget_grab_focus(m_PreeditArea);
260
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (m_PreeditArea));
261
gtk_text_buffer_set_text (buffer, m_buf, -1);
263
gtk_text_buffer_get_iter_at_offset(buffer, &ti, ppd->caret());
264
gtk_text_buffer_place_cursor(buffer, &ti);
268
CGTKWinHandler::updateCandidates(const ICandidateList* pcl)
271
for (int i=0, sz=pcl->size(); i < sz; ++i) {
272
const TWCHAR* pcand = pcl->candiString(i);
273
if (pcand == NULL) break;
274
cand_str += (i==9)?'0':TWCHAR('1' + i);
275
cand_str += TWCHAR('.');
277
cand_str += TWCHAR(' ');
280
TIConvSrcPtr src = (TIConvSrcPtr)(cand_str.c_str());
281
size_t srclen = (cand_str.size()+1)*sizeof(TWCHAR);
283
size_t dstlen = sizeof(m_buf) - 1;
284
iconv(m_iconv, &src, &srclen, &dst, &dstlen);
285
gtk_label_set_text(GTK_LABEL(m_CandidataArea), m_buf);