Newer
Older
scim-wnn / scim-wnn / src / anthyconversion.cpp
/***************************************************************************
 *   Copyright (C) 2004 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.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.             *
 ***************************************************************************/
#include "anthyconversion.h"

#ifdef HAVE_LIBANTHY


AnthyConversion::AnthyConversion(ConfigPointer cfg) : Convertor(cfg)
{
    m_iconv.set_encoding ("EUC-JP");
    pos = 0;
    anthy_init();
    context = anthy_create_context();
}


AnthyConversion::~AnthyConversion()
{
}

bool AnthyConversion::isConnected() {
    return(true);
}

void AnthyConversion::reset(){
    anthy_init();
    context = anthy_create_context();
    return;
}

void AnthyConversion::setYomiText(WideString yomi) {
    yomiText = yomi;
}

int AnthyConversion::ren_conversion() {
    String y;
    m_iconv.convert(y,yomiText);
    anthy_set_string(context,y.c_str());
    struct anthy_conv_stat stat;
    anthy_get_stat(context,&stat);
    pos = 0;
    buildResult();
    
    return(stat.nr_segment);
}

WideString AnthyConversion::getText() {
    WideString text;
    for(unsigned int i = 0;i < convResult.size();i ++) {
        text += convResult[i].kouho[convResult[i].pos];
    }
    return(text);
}

int AnthyConversion::setPos(int p){
    if ((p < convResult.size()) && (p >= 0)) pos = p;
    return(pos);
}

int AnthyConversion::getPos() {
    return(pos);
}

ResultList AnthyConversion::getResultList(int p,int kt){
    if (p == -1) p = pos;
    if ((kt != 0) || (p >= convResult.size())) return(ResultList());
    return(convResult[p]);
}

ResultList AnthyConversion::getYosokuList(const WideString &str) {
    return(ResultList());
}

bool AnthyConversion::select(int p) {
    if (p < convResult[pos].count) {
        convResult[pos].pos = p;
    }
}

AttributeList AnthyConversion::getAttributeList() {
    AttributeList attr;
    int l = 0;
    for(unsigned int i = 0;i < convResult.size();i ++) {
        if (pos == i) {
            Attribute a(l,convResult[i].kouho[convResult[i].pos].length(),SCIM_ATTR_DECORATE,SCIM_ATTR_DECORATE_REVERSE);
            attr.push_back(a);
        }
        l += convResult[i].kouho[convResult[i].pos].length();
    }
    return(attr);
}

bool AnthyConversion::resizeRegion(int w) {
    if ((convResult[pos].kouho[convResult[pos].pos].length() + w) < 1) return(false);
    if ((pos >= (convResult.size() - 1)) && (w > 0)) return(false);
    anthy_resize_segment(context,pos,w);
    buildResult();
    return(true);
}

void AnthyConversion::updateFrequency() {
    return;
}

bool AnthyConversion::connect() {
    return(true);
}
void AnthyConversion::disconnect() {
    return;
}

int AnthyConversion::getCaretPos() {
    int l = 0;
    for(unsigned int i = 0;i < convResult.size();i ++) {
        if (pos == i) return(l);
        l += convResult[i].kouho[convResult[i].pos].length();
    }
    return(0);
}

void AnthyConversion::buildResult() {
    struct anthy_conv_stat stat;
    anthy_get_stat(context,&stat);
    convResult.clear();
    for(int i = 0;i < stat.nr_segment;i ++) {
        ResultList l;
        l.kType = 0;
        struct anthy_segment_stat sstat;
        anthy_get_segment_stat(context,0,&sstat);
        l.count = sstat.nr_candidate;
        l.pos = 0;
        for(int j = 0;j < sstat.nr_candidate;j ++) {
            char res[256];
            int c = anthy_get_segment(context,i,j,0,0);
            if (c > 255) c = 255;
            anthy_get_segment(context,i,j,res,c + 1);
            WideString w;
            m_iconv.convert(w,String(res));
            l.kouho.push_back(w);
        }
        convResult.push_back(l);
    }
}

#endif