⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scim_tomoe.cpp

📁 手写输入
💻 CPP
字号:
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- *//* * Copyright (c) 2005 Hiroyuki Ikezoe <poincare@ikezoe.net> * Copyright (c) 2005 Takuro Ashie <ashie@homa.ne.jp> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * $Id: scim_tomoe.cpp,v 1.38 2006/12/07 03:59:48 makeinu Exp $ * *//* * The original code is scim_input_pad.cpp in scim-input-pad-0.1.0. * Copyright (C) 2005 James Su <suzhe@tsinghua.org.cn> */#include <wctype.h>#include <gtk/gtk.h>#define Uses_SCIM_UTILITY#define Uses_SCIM_OBJECT#define Uses_SCIM_POINTER#define Uses_SCIM_EVENT#define Uses_SCIM_HELPER#define Uses_SCIM_CONFIG_BASE#include <vector>#include <scim.h>#ifdef HAVE_CONFIG_H  #include <config.h>#endif#include <glib/gi18n-lib.h>#include "scim_tomoe_prefs.h"#include "scim_tomoe_prefs_widget.h"#include <tomoe-gtk.h>#define scim_module_init tomoe_LTX_scim_module_init#define scim_module_exit tomoe_LTX_scim_module_exit#define scim_helper_module_number_of_helpers tomoe_LTX_scim_helper_module_number_of_helpers#define scim_helper_module_get_helper_info tomoe_LTX_scim_helper_module_get_helper_info#define scim_helper_module_run_helper tomoe_LTX_scim_helper_module_run_helper#define SCIM_TOMOE_ICON (SCIM_ICONDIR "/scim-tomoe.png")using namespace scim;static void       slot_exit                   (const HelperAgent   *agent,                                               int                  ic,                                               const String        &uuid);static void       slot_update_screen          (const HelperAgent   *agent,                                               int                  ic,                                               const String        &uuid,                                               int                  screen);static void       run                         (const String        &display,                                               const ConfigPointer &config);static void       on_candidate_selected       (TomoeWindow         *window,                                               gpointer             user_data);static void       on_space_button_clicked     (GtkButton           *button,                                               gpointer             user_data);static void       on_backspace_button_clicked (GtkButton           *button,                                               gpointer             user_data);static void       on_enter_button_clicked     (GtkButton           *button,                                               gpointer             user_data);static void       on_notebook_switch_page     (GtkNotebook         *notebook,                                                GtkNotebookPage     *page,                                               guint                page_num,                                               gpointer             user_data);static HelperAgent  helper_agent;static GtkWidget   *main_window = NULL;static gint         main_window_xpos = 0;static gint         main_window_ypos = 0;static ConfigPointer m_config = NULL;static HelperInfo  helper_info (String (SCIM_TOMOE_UUID),                                "",                                String (SCIM_TOMOE_ICON),                                "",                                SCIM_HELPER_STAND_ALONE |                                SCIM_HELPER_NEED_SCREEN_INFO);//Module Interfaceextern "C" {    void scim_module_init (void)    {        bindtextdomain (GETTEXT_PACKAGE, SCIM_TOMOE_LOCALEDIR);        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");        helper_info.name = String (_("Handwriting recognition"));        helper_info.description = String (            _("A front-end for handwriting recognition engine"));    }    void scim_module_exit (void)    {    }    unsigned int scim_helper_module_number_of_helpers (void)    {        return 1;    }    bool scim_helper_module_get_helper_info (unsigned int idx, HelperInfo &info)    {        if (idx == 0) {            info = helper_info;             return true;        }        return false;    }    void scim_helper_module_run_helper (const String &uuid,                                        const ConfigPointer &config,                                        const String &display)    {        SCIM_DEBUG_MAIN(1) << "tomoe_LTX_scim_helper_module_run_helper ()\n";        if (uuid == SCIM_TOMOE_UUID) {            run (display, config);        }        SCIM_DEBUG_MAIN(1) << "exit tomoe_LTX_scim_helper_module_run_helper ()\n";    }}static gbooleanhelper_agent_input_handler (GIOChannel *source,                            GIOCondition condition,                            gpointer user_data){    if (condition == G_IO_IN) {        HelperAgent *agent = static_cast<HelperAgent *> (user_data);        if (agent && agent->has_pending_event ())            agent->filter_event ();    } else if (condition == G_IO_ERR || condition == G_IO_HUP) {        gtk_main_quit ();    }    return TRUE;}static voidslot_exit (const HelperAgent *agent, int ic, const String &uuid){    gtk_main_quit ();}static voidslot_update_screen (const HelperAgent *agent, int ic,                    const String &uuid, int screen){    if (gdk_display_get_n_screens (gdk_display_get_default ()) > screen) {        GdkScreen *scr = gdk_display_get_screen (gdk_display_get_default (),                                                 screen);        if (scr && main_window)            gtk_window_set_screen (GTK_WINDOW (main_window), scr);    }}static voidslot_trigger_property (const HelperAgent *agent, int ic,                       const String &uuid, const String &property){    if (property == "/Tomoe") {        if (GTK_WIDGET_VISIBLE (main_window)) {            gtk_window_get_position (GTK_WINDOW (main_window),                                     &main_window_xpos,                                     &main_window_ypos);            gtk_widget_hide (main_window);        } else {            gtk_window_move (GTK_WINDOW (main_window),                             main_window_xpos,                             main_window_ypos);            gtk_widget_show (main_window);        }    }}void run (const String &display, const ConfigPointer &config){    char **argv = new char * [4];    int    argc = 3;    argv [0] = "tomoe";    argv [1] = "--display";    argv [2] = const_cast<char *> (display.c_str ());    argv [3] = 0;     setenv ("DISPLAY", display.c_str (), 1);    gtk_init (&argc, &argv);    tomoe_gtk_init ();    helper_agent.signal_connect_exit (slot (slot_exit));	helper_agent.signal_connect_update_screen (slot (slot_update_screen));    helper_agent.signal_connect_trigger_property (slot (slot_trigger_property));    int fd = helper_agent.open_connection (helper_info, display);    GIOChannel *ch = g_io_channel_unix_new (fd);    if (fd >= 0 && ch) {        Property prop ("/Tomoe", _("Tomoe"),                       SCIM_TOMOE_ICON,                       _("Show/Hide Tomoe."));	    PropertyList props;	    props.push_back (prop);	    helper_agent.register_properties (props);        g_io_add_watch (ch, G_IO_IN,                        helper_agent_input_handler,                        (gpointer) &helper_agent);        g_io_add_watch (ch, G_IO_ERR,                        helper_agent_input_handler,                        (gpointer) &helper_agent);        g_io_add_watch (ch, G_IO_HUP,                        helper_agent_input_handler,                        (gpointer) &helper_agent);    }    GtkWidget *window = tomoe_window_new ();    gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);    gtk_window_set_accept_focus (GTK_WINDOW (window), FALSE);    GdkPixbuf *icon = gdk_pixbuf_new_from_file (SCIM_TOMOE_ICON, NULL);    if (icon) {        gtk_window_set_icon (GTK_WINDOW (window), icon);        g_object_unref (icon);    }    gtk_widget_show (window);    main_window = window;    // add software keyboard buttons    TomoeWindow *tomoe = TOMOE_WINDOW (window);    GtkWidget *separator, *button;    separator = gtk_hseparator_new ();    GtkWidget *hw_page = tomoe_window_get_handwriting_page (TOMOE_WINDOW (tomoe));    GtkWidget *button_area = tomoe_handwriting_get_button_area (TOMOE_HANDWRITING (hw_page));    gtk_box_pack_start (GTK_BOX (button_area), separator,                        FALSE, FALSE, 4);    gtk_widget_show (separator);    button = gtk_button_new_with_mnemonic (_("Space"));    gtk_box_pack_start (GTK_BOX (button_area), button,                        FALSE, FALSE, 4);    g_signal_connect (G_OBJECT (button), "pressed",                      G_CALLBACK (on_space_button_clicked),                      (gpointer) tomoe);    gtk_widget_show (button);    button = gtk_button_new_with_mnemonic (_("BackSpace"));    gtk_box_pack_start (GTK_BOX (button_area), button,                        FALSE, FALSE, 4);    g_signal_connect (G_OBJECT (button), "pressed",                      G_CALLBACK (on_backspace_button_clicked),                      (gpointer) tomoe);    gtk_widget_show (button);    button = gtk_button_new_with_mnemonic (_("Enter"));    gtk_box_pack_start (GTK_BOX (button_area), button,                        FALSE, FALSE, 4);    g_signal_connect (G_OBJECT (button), "pressed",                      G_CALLBACK (on_enter_button_clicked),                      (gpointer) tomoe);    gtk_widget_show (button);    // for getting focus     GtkWidget *notebook = tomoe_window_get_notebook (TOMOE_WINDOW (tomoe));;    g_signal_connect (G_OBJECT (notebook), "switch-page",                      G_CALLBACK (on_notebook_switch_page), window);    // create preferences widget    GtkWidget *prefs_widget = scim_tomoe_prefs_widget_new (tomoe, config);    gtk_widget_show (prefs_widget);    // add preference page    tomoe_window_append_page (        tomoe, prefs_widget,         gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU),        _("Preferences for scim-tomoe"));    // connect to the window's signals    g_signal_connect (G_OBJECT (window), "destroy",                      G_CALLBACK (gtk_main_quit),                      NULL);    g_signal_connect (G_OBJECT (window), "selected",                      G_CALLBACK (on_candidate_selected),                      prefs_widget);    // run    gtk_main ();    tomoe_gtk_quit ();}static voidon_candidate_selected (TomoeWindow *window, gpointer user_data){    ScimTomoePrefsWidget *prefs_widget = SCIM_TOMOE_PREFS_WIDGET (user_data);    if (helper_agent.get_connection_number () < 0) return;    const gchar *str = tomoe_window_get_selected_char (window);    if (str && *str) {        helper_agent.commit_string (-1, "", scim::utf8_mbstowcs (str));        bool value;        value = prefs_widget->config->read (            String (SCIM_TOMOE_CONFIG_CLEAR_ON_SELECT),            SCIM_TOMOE_CONFIG_CLEAR_ON_SELECT_DEFAULT);        if (value) {            TomoeHandwriting *hw;            TomoeCanvas *canvas;            hw = TOMOE_HANDWRITING (tomoe_window_get_handwriting_page (window));            canvas = TOMOE_CANVAS (tomoe_handwriting_get_canvas (hw));            tomoe_canvas_clear (TOMOE_CANVAS (canvas));        }    }}static voidon_space_button_clicked (GtkButton *button, gpointer user_data){    if (helper_agent.get_connection_number () < 0) return;    KeyEvent key (SCIM_KEY_space, 0);    KeyEvent key_release (SCIM_KEY_space, SCIM_KEY_ReleaseMask);    helper_agent.forward_key_event (-1, "", key);    helper_agent.forward_key_event (-1, "", key_release);}static voidon_backspace_button_clicked (GtkButton *button, gpointer user_data){    if (helper_agent.get_connection_number () < 0) return;    KeyEvent key (SCIM_KEY_BackSpace, 0);    KeyEvent key_release (SCIM_KEY_BackSpace, SCIM_KEY_ReleaseMask);    helper_agent.forward_key_event (-1, "", key);    helper_agent.forward_key_event (-1, "", key_release);}static voidon_enter_button_clicked (GtkButton *button, gpointer user_data){    if (helper_agent.get_connection_number () < 0) return;    KeyEvent key (SCIM_KEY_Return, 0);    KeyEvent key_release (SCIM_KEY_Return, SCIM_KEY_ReleaseMask);    helper_agent.forward_key_event (-1, "", key);    helper_agent.forward_key_event (-1, "", key_release);}static voidon_notebook_switch_page (GtkNotebook *notebook, GtkNotebookPage *page,                         guint page_num, gpointer user_data){    g_return_if_fail (GTK_WINDOW (user_data));    if (page_num == 1) {        gtk_window_set_accept_focus (GTK_WINDOW (user_data), TRUE);    } else {        gtk_window_set_accept_focus (GTK_WINDOW (user_data), FALSE);        gtk_window_set_focus (GTK_WINDOW (user_data), NULL);    }}/*vi:ts=4:nowrap:ai:expandtab*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -