Newer
Older
scim-wnn / honoka / src / multiconvertor.cpp
/***************************************************************************
 *   Copyright (C) 2005 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 "multiconvertor.h"

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif

#ifdef HAVE_GETTEXT
  #include <libintl.h>
  #define _(String) dgettext(GETTEXT_PACKAGE,String)
  #define N_(String) (String)
#else
  #define _(String) (String)
  #define N_(String) (String)
  #define bindtextdomain(Package,Directory)
  #define textdomain(domain)
  #define bind_textdomain_codeset(domain,codeset)
#endif

MultiConvertor::MultiConvertor(ConfigPointer cfg , HonokaInstance *i)
 : Convertor(cfg)
{
    instance = i;
}


MultiConvertor::~MultiConvertor()
{
}

bool MultiConvertor::isConnected() {
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        if (!instance->convertors[i]->isConnected())
            return false;
    }
    return true;
}

void MultiConvertor::reset(){
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        instance->convertors[i]->reset();
    }
}

void MultiConvertor::setYomiText(WideString yomi) {
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        instance->convertors[i]->setYomiText(yomi);
    }
}

int MultiConvertor::ren_conversion() {
    if (nuc.find(instance->m_def_convertor) != nuc.end()) return -1;

    def = instance->m_def_convertor;
    texts.clear();
    int b = instance->m_def_convertor->ren_conversion();
    for(unsigned int i = 0;i < b;i ++) {
        ResultList l = instance->m_def_convertor->getResultList(i);
        texts.push_back(l.kouho[0].kanji);
    }
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        if (instance->m_def_convertor != instance->convertors[i]) {
            instance->convertors[i]->ren_conversion();
            for(unsigned int j = 0;j < b;j ++) {
                int bl = instance->m_def_convertor->getResultList(j).Yomi.length();
                int cl = instance->convertors[i]->getResultList(j).Yomi.length();
                if (bl != cl) instance->convertors[i]->resizeRegion(bl - cl);
            }
        }
    }
    setPos(0);
    return b;
}

WideString MultiConvertor::getText() {
    WideString t;
    for(unsigned int i = 0;i < texts.size();i ++) {
        t = t + texts[i];
    }
    return t;
}

int MultiConvertor::setPos(int p){
    int cp = instance->m_def_convertor->setPos(p);
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        if (instance->m_def_convertor != instance->convertors[i])
            instance->convertors[i]->setPos(cp);
    }
    return cp;
}

int MultiConvertor::getPos() {
    return instance->m_def_convertor->getPos();
}

ResultList MultiConvertor::getResultList(int p,ResultType kt){
    results.clear();
    result.kouho.clear();
    ResultList l = instance->m_def_convertor->getResultList(p,kt);
    for(unsigned int i = 0;i < l.kouho.size();i ++) {
        l.kouho[i].label = l.kouho[i].kanji + utf8_mbstowcs(String(" (")) +
            utf8_mbstowcs(instance->m_def_convertor->getName()) + utf8_mbstowcs(String(")"));
    }
    MultiResultList m;
    m.list = l;
    m.convertor = instance->m_def_convertor;
    results.push_back(m);
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        if (instance->convertors[i] != instance->m_def_convertor) {
            m.list = instance->convertors[i]->getResultList(instance->m_def_convertor->getPos(),kt);
            m.convertor = instance->convertors[i];
            results.push_back(m);
        }
    }
    for(unsigned int i = 1;i < results.size();i ++) {
        for(unsigned int j = 0;j < results[i].list.kouho.size();j ++) {
            l.kouho.push_back(ResultEntry(results[i].list.kouho[j].kanji,
                results[i].list.kouho[j].kanji + utf8_mbstowcs(String(" (")) +
                utf8_mbstowcs(results[i].convertor->getPropertyName()) + utf8_mbstowcs(String(")"))));
        }
    }
    result = l;
    return l;
}


bool MultiConvertor::select(int p) {
    if (p < result.kouho.size()) texts[getPos()] = result.kouho[p].kanji;
    return true;
}

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

}

bool MultiConvertor::resizeRegion(int w) {
    //return false;

    // 以下実装中。
    if (!instance->m_def_convertor->resizeRegion(w)) return false;
    texts.clear();
    int p = getPos();
    vector<int> s;
    for(unsigned int i = 0;;i ++) {
        ResultList l = instance->m_def_convertor->getResultList(i);
        if (!l.count()) break;
        s.push_back(l.Yomi.length());
        texts.push_back(l.kouho[0].kanji);
    }
    instance->m_def_convertor->setPos(p);
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (nuc.find(instance->convertors[i]) != nuc.end()) continue;
        if (instance->m_def_convertor != instance->convertors[i]) {
            for(unsigned int j = 0;j < texts.size();j ++) {
                int bl = s[j];
                int cl = instance->convertors[i]->getResultList(j).Yomi.length();
                if (bl != cl) instance->convertors[i]->resizeRegion(bl - cl);
            }
            instance->convertors[i]->setPos(p);
        }
    }
    return true;
}

void MultiConvertor::updateFrequency() {
    // 未実装。
    return;
}

bool MultiConvertor::connect() {
    nuc.clear();
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        if (!instance->convertors[i]->connect()) 
            nuc.insert(instance->convertors[i]);
    }
    return true;
}
void MultiConvertor::disconnect() {
    for(unsigned int i = 0;i < instance->convertors.size();i ++) {
        instance->convertors[i]->disconnect();
    }
    return;
}

int MultiConvertor::getCaretPos() {
    int p = 0;
    for(unsigned int i = 0;i < getPos();i ++) p += texts[i].length();
    return p;
}


String MultiConvertor::getName()
{
    return String("MultiConvertor");
}

String MultiConvertor::getPropertyName()
{
    return instance->m_def_convertor->getPropertyName() + String("(M)");
}