~ubuntu-branches/ubuntu/raring/sunpinyin/raring

« back to all changes in this revision

Viewing changes to wrapper/gtk_standalone/imi_gtkwin.cpp

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
 
5
 *
 
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
 
15
 * following notice:
 
16
 *
 
17
 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
 
18
 * (CDDL)
 
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.
 
24
 *
 
25
 * Contributor(s):
 
26
 *
 
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.
 
36
 */
 
37
 
 
38
#ifdef HAVE_CONFIG_H
 
39
#include <config.h>
 
40
#endif
 
41
 
 
42
#include "imi_gtkwin.h"
 
43
#include "imi_view.h"
 
44
#include "imi_uiobjects.h"
 
45
 
 
46
 
 
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";
 
53
 
 
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;
 
60
 
 
61
 
 
62
static gint
 
63
key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
 
64
{
 
65
    if (data != NULL) {
 
66
        unsigned keycode = event->keyval;
 
67
        unsigned keyvalue = event->keyval;
 
68
        if (keyvalue < 0x20 && keyvalue > 0x7E)
 
69
            keyvalue = 0;
 
70
 
 
71
        CIMIView* pview = (CIMIView*)(data);
 
72
        CKeyEvent key_event (keycode, keyvalue, event->state);
 
73
        pview->onKeyEvent(key_event);
 
74
    }
 
75
    return 1;
 
76
}
 
77
 
 
78
static void
 
79
button_click_cn_en(GtkWidget* button, gpointer data)
 
80
{
 
81
    /*
 
82
    if (data != NULL) {
 
83
        CIMIView* pv = (CIMSAView*)data;
 
84
        unsigned int mask = 0;
 
85
        pv->switchCN(&mask);
 
86
        pv->updateWindows(mask);
 
87
    }
 
88
    */
 
89
}
 
90
 
 
91
static void
 
92
button_click_fullhalf_punc(GtkWidget* button, gpointer data)
 
93
{
 
94
    /*
 
95
    if (data != NULL)
 
96
      ((CIMSAView*)data)->switchFullPunc(NULL);
 
97
    */
 
98
}
 
99
 
 
100
static void
 
101
button_click_fullhalf_simbol(GtkWidget* button, gpointer data)
 
102
{
 
103
    /*
 
104
    if (data != NULL)
 
105
        ((CIMSAView*)data)->switchFullSimbol(NULL);
 
106
    */
 
107
}
 
108
 
 
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)
 
112
{
 
113
    m_iconv = iconv_open("UTF-8", TWCHAR_ICONV_NAME);
 
114
    load_images();
 
115
}
 
116
 
 
117
CGTKWinHandler::~CGTKWinHandler()
 
118
{
 
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);
 
126
}
 
127
 
 
128
void
 
129
CGTKWinHandler::load_images()
 
130
{
 
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);
 
138
    }
 
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);
 
145
}
 
146
 
 
147
 
 
148
void
 
149
CGTKWinHandler::updateStatus(int key, int value)
 
150
{
 
151
    switch (key) {
 
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;
 
155
    }
 
156
}
 
157
 
 
158
void
 
159
CGTKWinHandler::switchCN(bool cn)
 
160
{
 
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);
 
164
}
 
165
 
 
166
void
 
167
CGTKWinHandler::switchFullPunc(bool full)
 
168
{
 
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);
 
172
}
 
173
 
 
174
void
 
175
CGTKWinHandler::switchFullSimbol(bool full)
 
176
{
 
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);
 
180
}
 
181
 
 
182
bool
 
183
CGTKWinHandler::createWindows()
 
184
{
 
185
    GtkWidget *window;
 
186
    GtkWidget *chedit;
 
187
    GtkWidget *button;
 
188
    GtkWidget *box, *box1;
 
189
    GtkWidget *img;
 
190
    GtkWidget *frame;
 
191
 
 
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);
 
196
 
 
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);
 
202
 
 
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);
 
209
    GtkTextIter ti;
 
210
    gtk_text_buffer_get_iter_at_offset(buffer, &ti, 0);
 
211
    gtk_text_buffer_place_cursor(buffer, &ti);
 
212
 
 
213
    gtk_container_add(GTK_CONTAINER(frame), m_PreeditArea);
 
214
    gtk_box_pack_end(GTK_BOX(box), frame, TRUE, TRUE, 1);
 
215
 
 
216
    box1 = gtk_hbox_new(FALSE, 1);
 
217
    gtk_box_pack_start(GTK_BOX(box), box1, TRUE, TRUE, 1);
 
218
 
 
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);
 
224
 
 
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);
 
229
 
 
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);
 
234
 
 
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);
 
239
 
 
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);
 
243
 
 
244
    g_signal_connect(G_OBJECT(m_pWin), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
 
245
    gtk_widget_show_all(m_pWin);
 
246
 
 
247
    return true;
 
248
}
 
249
 
 
250
void
 
251
CGTKWinHandler::updatePreedit(const IPreeditString* ppd)
 
252
{
 
253
    TIConvSrcPtr src = (TIConvSrcPtr)(ppd->string());
 
254
    size_t srclen = (ppd->size()+1)*sizeof(TWCHAR);
 
255
    char * dst = m_buf;
 
256
    size_t dstlen = sizeof(m_buf)-1;
 
257
    iconv(m_iconv, &src, &srclen, &dst, &dstlen);
 
258
 
 
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);
 
262
    GtkTextIter ti;
 
263
    gtk_text_buffer_get_iter_at_offset(buffer, &ti, ppd->caret());
 
264
    gtk_text_buffer_place_cursor(buffer, &ti);
 
265
}
 
266
 
 
267
void
 
268
CGTKWinHandler::updateCandidates(const ICandidateList* pcl)
 
269
{
 
270
    wstring cand_str;
 
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('.');
 
276
        cand_str += pcand;
 
277
        cand_str += TWCHAR(' ');
 
278
    }
 
279
 
 
280
    TIConvSrcPtr src = (TIConvSrcPtr)(cand_str.c_str());
 
281
    size_t srclen = (cand_str.size()+1)*sizeof(TWCHAR);
 
282
    char * dst = m_buf;
 
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);
 
286
}
 
287