Home | History | Annotate | Download | only in gtk_standalone
      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         pview->onKeyEvent(keycode, keyvalue, event->state);
     73     }
     74     return 1;
     75 }
     76 
     77 static void
     78 button_click_cn_en(GtkWidget* button, gpointer data)
     79 {
     80     /*
     81     if (data != NULL) {
     82         CIMIView* pv = (CIMSAView*)data;
     83         unsigned int mask = 0;
     84         pv->switchCN(&mask);
     85         pv->updateWindows(mask);
     86     }
     87     */
     88 }
     89 
     90 static void
     91 button_click_fullhalf_punc(GtkWidget* button, gpointer data)
     92 {
     93     /*
     94     if (data != NULL)
     95       ((CIMSAView*)data)->switchFullPunc(NULL);
     96     */
     97 }
     98 
     99 static void
    100 button_click_fullhalf_simbol(GtkWidget* button, gpointer data)
    101 {
    102     /*
    103     if (data != NULL)
    104         ((CIMSAView*)data)->switchFullSimbol(NULL);
    105     */
    106 }
    107 
    108 CGTKWinHandler::CGTKWinHandler(CIMIView* pv) : mp_view(pv),
    109         CIMIWinHandler(), m_pWin(NULL), m_CandidataArea(NULL), m_PreeditArea(NULL),
    110         m_pLangButton(NULL), m_pPuncButton(NULL), m_pSimbButton(NULL)
    111 {
    112     m_iconv = iconv_open("UTF-8", TWCHAR_ICONV_NAME);
    113     load_images();
    114 }
    115 
    116 CGTKWinHandler::~CGTKWinHandler()
    117 {
    118     iconv_close(m_iconv);
    119     gtk_widget_unref(hanzi_image);
    120     gtk_widget_unref(eng_image);
    121     gtk_widget_unref(cnpunc_image);
    122     gtk_widget_unref(enpunc_image);
    123     gtk_widget_unref(fullwidth_image);
    124     gtk_widget_unref(halfwidth_image);
    125 }
    126 
    127 void
    128 CGTKWinHandler::load_images()
    129 {
    130     if (hanzi_image == NULL) {
    131         hanzi_image = gtk_image_new_from_file(hanzi_image_file_name);
    132         eng_image = gtk_image_new_from_file(eng_image_file_name);
    133         cnpunc_image = gtk_image_new_from_file(cnpunc_image_file_name);
    134         enpunc_image = gtk_image_new_from_file(enpunc_image_file_name);
    135         fullwidth_image = gtk_image_new_from_file(fullwidth_image_file_name);
    136         halfwidth_image = gtk_image_new_from_file(halfwidth_image_file_name);
    137     }
    138     gtk_widget_ref(hanzi_image);
    139     gtk_widget_ref(eng_image);
    140     gtk_widget_ref(cnpunc_image);
    141     gtk_widget_ref(enpunc_image);
    142     gtk_widget_ref(fullwidth_image);
    143     gtk_widget_ref(halfwidth_image);
    144 }
    145 
    146 
    147 void
    148 CGTKWinHandler::updateStatus(int key, int value)
    149 {
    150     switch (key) {
    151     case STATUS_ID_CN:          switchCN( value != 0 ); break;
    152     case STATUS_ID_FULLPUNC:    switchFullPunc( value != 0 ); break;
    153     case STATUS_ID_FULLSIMBOL:  switchFullSimbol( value != 0 ); break;
    154     }
    155 }
    156 
    157 void
    158 CGTKWinHandler::switchCN(bool cn)
    159 {
    160     gtk_container_remove(GTK_CONTAINER(m_pLangButton), (!cn)?hanzi_image:eng_image);
    161     gtk_container_add(GTK_CONTAINER(m_pLangButton), (cn)?hanzi_image:eng_image);
    162     gtk_widget_show_all(m_pLangButton);
    163 }
    164 
    165 void
    166 CGTKWinHandler::switchFullPunc(bool full)
    167 {
    168     gtk_container_remove(GTK_CONTAINER(m_pPuncButton), (!full)?cnpunc_image:enpunc_image);
    169     gtk_container_add(GTK_CONTAINER(m_pPuncButton), (full)?cnpunc_image:enpunc_image);
    170     gtk_widget_show_all(m_pPuncButton);
    171 }
    172 
    173 void
    174 CGTKWinHandler::switchFullSimbol(bool full)
    175 {
    176     gtk_container_remove(GTK_CONTAINER(m_pSimbButton), (!full)?fullwidth_image:halfwidth_image);
    177     gtk_container_add(GTK_CONTAINER(m_pSimbButton), (full)?fullwidth_image:halfwidth_image);
    178     gtk_widget_show_all(m_pSimbButton);
    179 }
    180 
    181 bool
    182 CGTKWinHandler::createWindows()
    183 {
    184     GtkWidget *window;
    185     GtkWidget *chedit;
    186     GtkWidget *button;
    187     GtkWidget *box, *box1;
    188     GtkWidget *img;
    189     GtkWidget *frame;
    190 
    191     m_pWin = window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    192     gtk_container_set_border_width(GTK_CONTAINER(window), 2);
    193     box = gtk_vbox_new(FALSE, 1);
    194     gtk_container_add(GTK_CONTAINER(window), box);
    195 
    196     frame = gtk_frame_new(NULL);
    197     m_CandidataArea = gtk_label_new("/");
    198     gtk_container_add(GTK_CONTAINER(frame), m_CandidataArea);
    199     gtk_label_set_justify(GTK_LABEL(m_CandidataArea), GTK_JUSTIFY_LEFT);
    200     gtk_box_pack_end(GTK_BOX(box), frame, TRUE, TRUE, 1);
    201 
    202     frame = gtk_frame_new(NULL);
    203     m_PreeditArea = gtk_text_view_new();
    204     gtk_text_view_set_editable(GTK_TEXT_VIEW(m_PreeditArea), false);
    205     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(m_PreeditArea), true);
    206     GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (m_PreeditArea));
    207     gtk_text_buffer_set_text(buffer, "", -1);
    208     GtkTextIter ti;
    209     gtk_text_buffer_get_iter_at_offset(buffer, &ti, 0);
    210     gtk_text_buffer_place_cursor(buffer, &ti);
    211 
    212     gtk_container_add(GTK_CONTAINER(frame), m_PreeditArea);
    213     gtk_box_pack_end(GTK_BOX(box), frame, TRUE, TRUE, 1);
    214 
    215     box1 = gtk_hbox_new(FALSE, 1);
    216     gtk_box_pack_start(GTK_BOX(box), box1, TRUE, TRUE, 1);
    217 
    218     frame = gtk_frame_new(NULL);
    219     chedit = gtk_label_new("");
    220     gtk_container_add(GTK_CONTAINER(frame), chedit);
    221     gtk_label_set_justify(GTK_LABEL(chedit), GTK_JUSTIFY_LEFT);
    222     gtk_box_pack_start(GTK_BOX(box1), frame, TRUE, TRUE, 1);
    223 
    224     m_pSimbButton = button = gtk_button_new();
    225     gtk_container_add(GTK_CONTAINER(button), halfwidth_image);
    226     gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 2);
    227     gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_click_fullhalf_simbol), mp_view);
    228 
    229     m_pPuncButton = button = gtk_button_new();
    230     gtk_container_add(GTK_CONTAINER(button), cnpunc_image);
    231     gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 2);
    232     gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_click_fullhalf_punc), mp_view);
    233 
    234     m_pLangButton = button = gtk_button_new();
    235     gtk_container_add(GTK_CONTAINER(button), hanzi_image);
    236     gtk_box_pack_end(GTK_BOX(box1), button, FALSE, FALSE, 2);
    237     gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_click_cn_en), mp_view);
    238 
    239     gtk_window_set_decorated((GtkWindow*)window, FALSE);
    240     gtk_signal_connect(GTK_OBJECT(window), "key_press_event", GTK_SIGNAL_FUNC(key_press_cb), mp_view);
    241     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    242 
    243     g_signal_connect(G_OBJECT(m_pWin), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
    244     gtk_widget_show_all(m_pWin);
    245 
    246     return true;
    247 }
    248 
    249 void
    250 CGTKWinHandler::updatePreedit(const IPreeditString* ppd)
    251 {
    252     TIConvSrcPtr src = (TIConvSrcPtr)(ppd->string());
    253     size_t srclen = (ppd->size()+1)*sizeof(TWCHAR);
    254     char * dst = m_buf;
    255     size_t dstlen = sizeof(m_buf)-1;
    256     iconv(m_iconv, &src, &srclen, &dst, &dstlen);
    257 
    258     //gtk_widget_grab_focus(m_PreeditArea);
    259     GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (m_PreeditArea));
    260     gtk_text_buffer_set_text (buffer, m_buf, -1);
    261     GtkTextIter ti;
    262     gtk_text_buffer_get_iter_at_offset(buffer, &ti, ppd->caret());
    263     gtk_text_buffer_place_cursor(buffer, &ti);
    264 }
    265 
    266 void
    267 CGTKWinHandler::updateCandidates(const ICandidateList* pcl)
    268 {
    269     wstring cand_str;
    270     for (int i=0, sz=pcl->size(); i < sz; ++i) {
    271         const TWCHAR* pcand = pcl->candiString(i);
    272         if (pcand == NULL) break;
    273         cand_str += (i==9)?'0':TWCHAR('1' + i);
    274         cand_str += TWCHAR('.');
    275         cand_str += pcand;
    276         cand_str += TWCHAR(' ');
    277     }
    278 
    279     TIConvSrcPtr src = (TIConvSrcPtr)(cand_str.c_str());
    280     size_t srclen = (cand_str.size()+1)*sizeof(TWCHAR);
    281     char * dst = m_buf;
    282     size_t dstlen = sizeof(m_buf) - 1;
    283     iconv(m_iconv, &src, &srclen, &dst, &dstlen);
    284     gtk_label_set(GTK_LABEL(m_CandidataArea), m_buf);
    285 }
    286 
    287