/***************************************************************************
 *   Copyright (C) 2003-2005 by liuspider                                  *
 *   liuspider@users.sourceforge.net                                       *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
#include "scimlistbox.h"
#include <qvariant.h>
#include <qlayout.h>
#include <qlabel.h>

#include <qwidget.h>
#include <qsize.h>

ScimListBox::ScimListBox(QWidget *parent, const char *name, bool _isVertical)
    : ScimDragableFrame(parent, parent, name), candidatesLbl(0), curSelection(0)
{
    setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum, 0, 0) );

    QBoxLayout::Direction defaultLayoutDirection = QBoxLayout::LeftToRight;

    if(_isVertical)
        defaultLayoutDirection = QBoxLayout::TopToBottom;

    layout = new QBoxLayout( this, defaultLayoutDirection, 3, 2, "defaultLayout");
    layout->setResizeMode( QLayout::FreeResize );
    enableMouse = false;
    candidatesLbl = new ScimStringListItem(this);

    layout->addWidget(candidatesLbl);
}

void ScimListBox::setEnableHighlight(bool b)
{
    enableHighlight = b;

//     for(int i = 0; i < MAX_CANDIDATES; i++)
//         if(candidatesLbl[i])
//             candidatesLbl[i]->setMouseTracking(enableHighlight);
//     if(!enableHighlight && candidatesLbl[curSelection])
//       candidatesLbl[curSelection]->setHighlight(false);
}
void ScimListBox::setEnableMouse(bool b)
{
  //FIXME
//     enableMouse = b;
}
void ScimListBox::setVertical(bool b)
{
    QBoxLayout::Direction defaultLayoutDirection = QBoxLayout::LeftToRight;
    if(b)
        defaultLayoutDirection = QBoxLayout::TopToBottom;
    layout->setDirection(defaultLayoutDirection);
}
void ScimListBox::updateHighlight(int _newsel)
{
//     if(enableMouse && enableHighlight)
//     {
//       candidatesLbl[curSelection]->setHighlight(false);
//       candidatesLbl[_newsel]->setHighlight(true);
      curSelection = _newsel;
//     }
}
void ScimListBox::updateSelection(int i)
{
    emit( itemSelected( i ) );
}
unsigned ScimListBox::updateContent( const QStringList & labellist,
                                 const QStringList & lookupstringlist,
                                 QValueList<scim::AttributeList> & attrslist )
{
    static int max_width = screen.width() / 3;
    unsigned allitems = lookupstringlist.size();
    unsigned counter = 0;
    QString separator, labelSeparator;
    if(QBoxLayout::LeftToRight == layout->direction()) {
        separator = " ";
        labelSeparator = ".";
    } else {
        separator = "\n";
        labelSeparator = ". ";
    }
    QString cache;
    scim::AttributeList attrs;
    int highlight_start = 0;
    static int cachedWidth = 0, curwidth;
    if(enableMouse)
    {
      //TODO
    }
    else
    {
        scim::Attribute highlight;
        highlight.set_type(scim::SCIM_ATTR_DECORATE);
        highlight.set_value(scim::SCIM_ATTR_DECORATE_HIGHLIGHT);
        bool insertHighlight = false;
        QFontMetrics fm( fontMetrics() );
        cachedWidth = 0;
        for(; counter < allitems; counter++)
        {
            insertHighlight = ((uint)curSelection == counter);

            if(insertHighlight) {
              highlight_start = cache.length();
              highlight.set_start(highlight_start);
            }

            cache += labellist[counter] + labelSeparator;
            if(insertHighlight)
            {
              highlight.set_length(cache.length() +
                lookupstringlist[counter].length() - highlight_start);
              attrs.push_back(highlight);
            }
//             std::cout << attrslist[i].size() << " sub \n";
            for(size_t j = 0; j < attrslist[counter].size(); j++)
            {
              attrslist[counter][j].set_start(attrslist[counter][j].get_start() + cache.length());
              attrs.push_back(attrslist[counter][j]);
            }
            if(layout->direction() != QBoxLayout::TopToBottom)
            {
                curwidth = fm.width(lookupstringlist[counter] + separator);
                if(cachedWidth + curwidth > max_width)
                    break;
                else
                    cachedWidth += curwidth;
            }

            cache += lookupstringlist[counter] + separator;
        }
    }

    candidatesLbl->setText(cache, attrs);
    return counter;
}

ScimListBox::~ScimListBox()
{
}


#include "scimlistbox.moc"
