/***************************************************************************
 *   Copyright (C) 2003 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)
{
    setFrameShape( QFrame::Box );
    setFrameShadow( QFrame::Raised );
    setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding, 0, 0) );

    QBoxLayout::Direction defaultLayoutDirection = QBoxLayout::LeftToRight;
    
    if(_isVertical)
        defaultLayoutDirection = QBoxLayout::TopToBottom;

    layout = new QBoxLayout( this, defaultLayoutDirection, 3, 2, "defaultLayout");
    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 )
{
    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;
    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() );

        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]);
            }
            cache += lookupstringlist[counter] + separator;
            if(layout->direction() != QBoxLayout::TopToBottom)
              if(fm.width(cache) > screen.width()/3)
                break;
        }
    }
    candidatesLbl->setText(cache, attrs);
    adjustSize();
    return counter + 1;
}

ScimListBox::~ScimListBox()
{
}


#include "scimlistbox.moc"
