Skip to content
PyZyInputContext.h 3.35 KiB
Newer Older
/* vim:set et ts=4 sts=4:
 *
 * ibus-pinyin - The Chinese PinYin engine for IBus
 *
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 *
 * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef __INPUT_CONTEXT_H_
#define __INPUT_CONTEXT_H_

#include <string>
#include <vector>

namespace PyZy {

class Config;

enum CandidateType {
    NORMAL_PHRASE,
    USER_PHRASE,
    SPECIAL_PHRASE,
};

struct Candidate {
    std::string text;
    CandidateType type;
};

// lower 8bits of vkey_code should be zero.
enum vkey_code {
    VKEY_FIRST = 1 << 8,

    VKEY_COMMIT = (10 << 8),
    VKEY_RESET = (11 << 8),

    VKEY_CURSOR_RIGHT = (20 << 8),
    VKEY_CURSOR_LEFT = (21 << 8),
    VKEY_CURSOR_RIGHT_BY_WORD = (22 << 8),
    VKEY_CURSOR_LEFT_BY_WORD = (23 << 8),
    VKEY_CURSOR_TO_BEGIN = (24 << 8),
    VKEY_CURSOR_TO_END = (25 << 8),

    VKEY_CANDIDATE_SELECT = (30 << 8),
    VKEY_CANDIDATE_FOCUS = (31 << 8),
    VKEY_CANDIDATE_RESET = (32 << 8),

    VKEY_PAGE_PREVIOUS = (40 << 8),
    VKEY_PAGE_NEXT = (41 << 8),
    VKEY_PAGE_BEGIN = (42 << 8),
    VKEY_PAGE_END = (43 << 8),

    VKEY_DELETE_CHARACTER_BEFORE = (50 << 8),
    VKEY_DELETE_CHARACTER_AFTER = (51 << 8),
    VKEY_DELETE_WORD_BEFORE = (52 << 8),
    VKEY_DELETE_WORD_AFTER = (53 << 8),

    VKEY_BOPOMOFO_SELECT_MODE = (60 << 8),
};

class InputContext {
public:
    virtual ~InputContext (void) { }

    class Observer {
    public:
        virtual ~Observer () { }
        virtual void commitText (const std::string &commit_text) = 0;
        virtual void preeditTextChanged () = 0;
        virtual void auxiliaryTextChanged () = 0;
        virtual void lookupTableChanged () = 0;
    };

    enum InputType {
        FULL_PINYIN,
        DOUBLE_PINYIN,
        BOPOMOFO,
    };

    /* static functions */
    static void init ();
    static void init (const std::string & user_data_dir);
    static void finalize ();
    static InputContext * create (InputContext::InputType type,
                                 Config & config,
                                 InputContext::Observer * observer);

    virtual bool processKeyEvent (unsigned short key_event) = 0;
    virtual void update (void) = 0;
    virtual void commit (void) = 0;
    virtual void reset (void) = 0;

    virtual std::string selectedText (void) const = 0;
    virtual std::string conversionText (void) const = 0;
    virtual std::string restText (void) const = 0;
    virtual std::string auxiliaryText (void) const = 0;
    virtual std::vector<Candidate> candidates () const = 0;
    virtual std::string inputText () const = 0;
    virtual unsigned int cursor () const = 0;
    virtual unsigned int focusedCandidate () const = 0;
    virtual unsigned int page () const = 0;
};

}; // namespace PyZy

#endif  // __INPUT_CONTEXT_H_