/***************************************************************************
                          multilineeditdlg.cpp  -  description
                             -------------------
    begin                : Sam Jan 11 2003
    copyright            : (C) 2003 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "multilineeditdlg.h"
#include "tokendialog.h"

// KDE includes
#include <tdeaction.h>
#include <kcolordialog.h>
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <tdespell.h>
#include <dstextedit.h>
#include <tdetoolbar.h>
#include <kcombobox.h>

// TQt includes
#include <tqdockarea.h>
#include <tqregexp.h>

MultiLineEditor::MultiLineEditor( TokenProvider* token, TQWidget *parent, const char *name )
    : TQWidget( parent, name ), m_token( token )
{
    TQVBoxLayout* layout = new TQVBoxLayout( this, 6, 6 );

//    ksc = new KSpellConfig( this );

    editor = new DSTextEdit( this );
    editor->setTextFormat( TQt::RichText );
    //editor->setText( text, "" );
    editor->setFocus();

    TQDockArea* area = new TQDockArea( TQt::Horizontal, TQDockArea::Normal, this );
    toolBar = new TDEToolBar( area );
    tool2Bar = new TDEToolBar( area );
    tool3Bar = new TDEToolBar( area );
    
    setupActions();

    layout->addWidget( area );
    layout->addWidget( editor );

}

MultiLineEditor::~MultiLineEditor()
{
}


void MultiLineEditor::setupActions()
{
    ac = new TDEActionCollection( this );

    TDEAction* action_export = new TDEAction( i18n("Export"), "fileexport", 0, this,  TQ_SLOT( save() ), ac );

    //
    // Edit Actions
    //
    TDEAction *action_undo = KStdAction::undo( editor, TQ_SLOT( undo() ), ac );
    action_undo->setEnabled( false );
    connect( editor, TQ_SIGNAL( undoAvailable(bool) ), action_undo, TQ_SLOT( setEnabled(bool) ) );

    
    TDEAction *action_redo = KStdAction::redo( editor, TQ_SLOT( redo() ), ac );
    action_redo->setEnabled( false );
    connect( editor, TQ_SIGNAL( redoAvailable(bool) ), action_redo, TQ_SLOT( setEnabled(bool) ) );

    TDEAction *action_cut = KStdAction::cut( editor, TQ_SLOT( cut() ), ac );
    action_cut->setEnabled( false );
    connect( editor, TQ_SIGNAL( copyAvailable(bool) ), action_cut, TQ_SLOT( setEnabled(bool) ) );

    TDEAction *action_copy = KStdAction::copy( editor, TQ_SLOT( copy() ), ac );
    action_copy->setEnabled( false );
    connect( editor, TQ_SIGNAL( copyAvailable(bool) ), action_copy, TQ_SLOT( setEnabled(bool) ) );

    TDEAction* action_paste = KStdAction::paste( editor, TQ_SLOT( paste() ), ac );

    //
    // Character Formatting
    //
    action_bold = new TDEToggleAction( i18n("&Bold"), "format-text-bold", CTRL+Key_B, ac, "format_bold" );
    connect( action_bold, TQ_SIGNAL( toggled(bool) ), editor, TQ_SLOT( setBold(bool) ) );

    action_italic = new TDEToggleAction( i18n("&Italic"), "format-text-italic", CTRL+Key_I, ac, "format_italic" );
    connect( action_italic, TQ_SIGNAL( toggled(bool) ), editor, TQ_SLOT( setItalic(bool) ) );

    action_underline = new TDEToggleAction( i18n("&Underline"), "format-text-underline", CTRL+Key_U, ac, "format_underline" );
    connect( action_underline, TQ_SIGNAL( toggled(bool) ), editor, TQ_SLOT( setUnderline(bool) ) );

    TDEAction* action_color = new TDEAction( i18n("Text &Color..."), "colorpicker", 0, this,  TQ_SLOT( formatColor() ), ac, "format_color" );

    //
    // Font
    //
    action_font = new TDEFontAction( i18n("&Font"), 0, ac, "format_font" );
    connect( action_font, TQ_SIGNAL( activated( const TQString & ) ), editor, TQ_SLOT( setFamily( const TQString & ) ) );

    action_font_size = new TDEFontSizeAction( i18n("Font &Size"), 0, ac, "format_font_size" );
    connect( action_font_size, TQ_SIGNAL( fontSizeChanged(int) ), editor, TQ_SLOT( setPointSize(int) ) );

    //
    // Alignment
    //
    action_align_left = new TDEToggleAction( i18n("Align &Left"), "format-text-direction-ltr", 0, ac, "format_align_left" );
    connect( action_align_left, TQ_SIGNAL( toggled(bool) ), this,  TQ_SLOT( setAlignLeft(bool) ) );

    action_align_center = new TDEToggleAction( i18n("Align &Center"), "text_center", 0, ac, "format_align_center" );
    connect( action_align_center, TQ_SIGNAL( toggled(bool) ), this,  TQ_SLOT( setAlignCenter(bool) ) );

    action_align_right = new TDEToggleAction( i18n("Align &Right"), "format-text-direction-rtl", 0, ac, "format_align_right" );
    connect( action_align_right, TQ_SIGNAL( toggled(bool) ), this,  TQ_SLOT( setAlignRight(bool) ) );

    action_align_justify = new TDEToggleAction( i18n("&Justify"), "text_block", 0, ac, "format_align_justify" );
    connect( action_align_justify, TQ_SIGNAL( toggled(bool) ), this,  TQ_SLOT( setAlignJustify(bool) ) );

    action_align_left->setExclusiveGroup( "alignment" );
    action_align_center->setExclusiveGroup( "alignment" );
    action_align_right->setExclusiveGroup( "alignment" );
    action_align_justify->setExclusiveGroup( "alignment" );

    //TDEAction* action_spell = KStdAction::spelling( this, TQ_SLOT( checkSpelling() ), ac );

    TDEAction* textDataAct = new TDEAction( i18n("Insert &Data Field"), "contents", 0, this,  TQ_SLOT( insertNewField() ), ac, "text_data_act");        action_export->plug( toolBar );
    
    toolBar->insertSeparator();
    action_undo->plug( toolBar );
    action_redo->plug( toolBar );
    toolBar->insertSeparator();
    action_cut->plug( toolBar );
    action_copy->plug( toolBar );
    action_paste->plug( toolBar );
    toolBar->insertSeparator();    
    action_bold->plug( toolBar );
    action_italic->plug( toolBar );
    action_underline->plug( toolBar );
    toolBar->insertSeparator();
           
    action_font->plug( tool2Bar );
    action_font_size->plug( tool2Bar );
    action_color->plug( tool2Bar );
    tool2Bar->insertSeparator();    
    action_align_left->plug( tool2Bar );
    action_align_center->plug( tool2Bar );
    action_align_right->plug( tool2Bar );
    action_align_justify->plug( tool2Bar );

    textDataAct->plug( tool3Bar );
    
    //
    // Setup enable/disable
    //
    updateActions();

    connect( editor, TQ_SIGNAL( currentFontChanged( const TQFont & ) ), this,  TQ_SLOT( updateFont() ) );
    connect( editor, TQ_SIGNAL( currentFontChanged( const TQFont & ) ), this,  TQ_SLOT( updateCharFmt() ) );
    connect( editor, TQ_SIGNAL( cursorPositionChanged( int,int ) ), this,  TQ_SLOT( updateAligment() ) );
}

TQString MultiLineEditor::text()
{
    return editor->text();
}

void MultiLineEditor::setText( const TQString & t )
{
    editor->setText( t );
}

void MultiLineEditor::updateCharFmt()
{
    action_bold->setChecked( editor->bold() );
    action_italic->setChecked( editor->italic() );
    action_underline->setChecked( editor->underline() );
}

void MultiLineEditor::updateAligment()
{
    int align = editor->alignment();

    switch ( align ) {
        case AlignRight:
            action_align_right->setChecked( true );
            break;
        case AlignCenter:
            action_align_center->setChecked( true );
            break;
        case AlignLeft:
            action_align_left->setChecked( true );
            break;
        case AlignJustify:
            action_align_justify->setChecked( true );
            break;
        default:
            break;
    }
}

void MultiLineEditor::updateFont()
{
    if ( editor->pointSize() > 0 )
        action_font_size->setFontSize( editor->pointSize() );
    action_font->setFont( editor->family() );
}

void MultiLineEditor::updateActions()
{
    updateCharFmt();
    updateAligment();
    updateFont();
}

void MultiLineEditor::formatColor()
{
    TQColor col;

    int s = KColorDialog::getColor( col, editor->color(), editor );
    if ( s != TQDialog::Accepted )
        return;

    editor->setColor( col );
}

void MultiLineEditor::setAlignLeft( bool yes )
{
    if ( yes )
        editor->setAlignment( AlignLeft );
}

void MultiLineEditor::setAlignRight( bool yes )
{
    if ( yes )
        editor->setAlignment( AlignRight );
}

void MultiLineEditor::setAlignCenter( bool yes )
{
    if ( yes )
        editor->setAlignment( AlignCenter );
}

void MultiLineEditor::setAlignJustify( bool yes )
{
    if ( yes )
        editor->setAlignment( AlignJustify );
}

void MultiLineEditor::insertNewField()
{
    TokenDialog dlg( m_token, this,  "dlg" );
    if( dlg.exec() == TQDialog::Accepted )
        editor->insert( dlg.token() );
}

void MultiLineEditor::checkSpelling()
{
// FIXME: check and clean up spellchecking (Zsolt, 2008-04-07)
/*
    TQString s;
    if ( editor->hasSelectedText() )
        s = editor->selectedText();
    else
        s = editor->text();

    spell = new KSpell( this, i18n("Spell Checking"), 0, 0, 0, true, true, KSpell::HTML );
    spell->setAutoDelete( true );
    spell->check( s, true );
    connect( spell, TQ_SIGNAL( done(const TQString &) ), this,  TQ_SLOT( spellCheckDone(const TQString &) ) );

    spell->cleanUp();
*/
}

void MultiLineEditor::spellCheckDone(const TQString & buffer)
{
    editor->setText( buffer );
    spell->cleanUp();
}

void MultiLineEditor::save()
{
    TQString name = KFileDialog::getSaveFileName ( NULL, "*", this );
    if( name.isEmpty() )
        return;

    TQFile file( name );
    if ( file.open( IO_WriteOnly ) ) {
        TQTextStream ts( &file );
        ts << editor->text();
    }
}


#include "multilineeditdlg.moc"
