/*
 *  Copyright (c) 2002-2003 Jesper K. Pedersen 
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/
#ifdef TQT_ONLY
  #include "compat.h"
  #include "images.h"
#else
  #include 
  #include 
  #include 
  #include "regexpbuttons.moc"
#endif
#include "regexpbuttons.h"
#include "dcbutton.h"
#include 
#include 
#include 
#include 
#include 
#include "regexpconverter.h"
RegExpButtons::RegExpButtons( TQWidget *tqparent, const char *name )
  : TQDockWindow( TQDockWindow::InDock, tqparent, name), _keepMode(false)
{
  TQBoxLayout *tqlayout = boxLayout();
  _grp = new TQButtonGroup(this);
  _grp->hide();
  _grp->setExclusive( true );
  _mapper = new TQSignalMapper( TQT_TQOBJECT(this), "RegExpButtons::_mapper" );
  connect( _mapper, TQT_SIGNAL( mapped(int) ), this, TQT_SIGNAL( clicked(int) ) );
  // The "select" button.
  _selectBut = new TQToolButton( this);
#ifdef TQT_ONLY
  TQPixmap pix;
  pix.convertFromImage( qembed_findImage( "select" ) );
#else
  TQPixmap pix = KGlobal::iconLoader()->loadIcon(locate("data", TQString::tqfromLatin1("kregexpeditor/pics/select.png") ), KIcon::Toolbar );
#endif
  _selectBut->setPixmap( pix );
  tqlayout->addWidget( _selectBut );
  _grp->insert(_selectBut);
  _selectBut->setToggleButton( true );
  connect( _selectBut, TQT_SIGNAL(clicked()), TQT_SIGNAL(doSelect()));
  connect( _selectBut, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotSetNonKeepMode()) );
  TQToolTip::add( _selectBut, i18n("Selection tool"));
  TQWhatsThis::add( _selectBut, i18n("This will change the state of the editor to selection state."
                             "In this state you will not be inserting regexp items, but instead select them. "
                             "To select a number of items, press down the left mouse button and drag it over the items.
"
                             "When you have selected a number of items, you may use cut/copy/paste. These functions are "
                             "found in the right mouse button menu.
"));
  // Insert buttons.
  DoubleClickButton* but;
  but = insert(TEXT, "text", i18n("Text"),
               i18n( "This will insert a text field, where you may write text. The text you write will "
                     "be matched literally. (i.e. you do not need to escape any characters)" ) );
  tqlayout->addWidget( but );
  but = insert(CHARSET, "characters", i18n("A single character specified in a range"),
               i18n("This will match a single character from a predefined range."
                    "When you insert this widget a dialog box will appear, which lets you specify "
                    "which characters this regexp item will match.
") );
  tqlayout->addWidget( but );
  but = insert(DOT, "anychar", i18n("Any character"),
               i18n("This will match any single character") );
  tqlayout->addWidget( but );
  but = insert(REPEAT, "repeat", i18n("Repeated content"),
               i18n("This regexp item will repeat the regexp items it surrounds "
                    "a specified number of times."
                    "The number of times to repeat may be specified using ranges. e.g. You may specify "
                    "that it should match from 2 to 4 times, that it should match exactly 5 times, or "
                    "that it should match at least one time.
"
                    "Examples:
"
                    "If you specify that it should match any time, and the content it surrounds "
                    "is abc, then this regexp item will match the empty string, "
                    "the string abc, the string abcabc, the string abcabcabcabc, "
                    "etc.
") );
  tqlayout->addWidget( but );
  but = insert(ALTN, "altn", i18n("Alternatives"),
               i18n("This regexp item will match any of its alternatives.
"
                    "You specify alternatives by placing regexp items on top of "
                    "each other inside this widget.") );
  tqlayout->addWidget( but );
  but = insert(COMPOUND,  "compound", i18n("Compound regexp"),
               i18n("This regexp item serves two purposes:"
                    "- It makes it possible for you to collapse a huge regexp item into "
                    "a small box. This makes it easier for you to get an overview of large "
                    "regexp items. This is especially useful if you load a predefined regexp item "
                    "you perhaps don't care about the inner workings of.") );
  tqlayout->addWidget( but );
  but = insert(BEGLINE,  "begline", i18n("Beginning of line"),
               i18n("This will match the beginning of a line.") );
  tqlayout->addWidget( but );
  but = insert(ENDLINE,  "endline", i18n("End of line"),
               i18n("This will match the end of a line.") );
  tqlayout->addWidget( but );
  _wordBoundary = insert(WORDBOUNDARY,  "wordboundary", i18n("Word boundary"),
                         i18n("This asserts a word boundary (This part does not actually match any characters)") );
  tqlayout->addWidget( _wordBoundary );
  _nonWordBoundary = insert(NONWORDBOUNDARY,  "nonwordboundary", i18n("Non Word boundary"),
                            i18n("This asserts a non-word boundary "
                                 "(This part does not actually match any characters)") );
  tqlayout->addWidget( _nonWordBoundary );
  _posLookAhead = insert(POSLOOKAHEAD,  "poslookahead", i18n("Positive Look Ahead"),
                         i18n("This asserts a regular expression (This part does not actually match any characters). "
                              "You can only use this at the end of a regular expression.") );
  tqlayout->addWidget( _posLookAhead );
  _negLookAhead = insert(NEGLOOKAHEAD,  "neglookahead", i18n("Negative Look Ahead"),
                         i18n("This asserts a regular expression that must not match "
                              "(This part does not actually match any characters). "
                              "You can only use this at the end of a regular expression.") );
  tqlayout->addWidget( _negLookAhead );
}
DoubleClickButton* RegExpButtons::insert(RegExpType tp, const char* name, TQString tooltip, TQString whatsthis)
{
#ifdef TQT_ONLY
    TQPixmap pix;
    pix.convertFromImage( qembed_findImage( TQString::tqfromLatin1( name ) ) );
#else
    TQPixmap pix = KGlobal::iconLoader()->loadIcon(locate("data", TQString::tqfromLatin1("kregexpeditor/pics/")+TQString::tqfromLatin1(name) +
                                                       TQString::tqfromLatin1(".png") ), KIcon::Toolbar );
#endif
  DoubleClickButton* but = new DoubleClickButton( pix, this, "RegExpButtons::but");
  _mapper->setMapping( TQT_TQOBJECT(but), tp );
  connect( but, TQT_SIGNAL( clicked() ), _mapper, TQT_SLOT( map() ) );
  connect( but, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotSetNonKeepMode() ) );
  connect( but, TQT_SIGNAL( doubleClicked() ), this, TQT_SLOT( slotSetKeepMode() ) );
  _grp->insert( but );
  but->setToggleButton( true );
  TQToolTip::add( but, tooltip );
  TQWhatsThis::add( but, whatsthis );
  return but;
}
void RegExpButtons::slotUnSelect()
{
  if ( _grp->selected() ) {
    TQToolButton *pb = static_cast(_grp->selected());
    if (pb) {
      pb->setOn( false );
    }
  }
}
void RegExpButtons::slotSetKeepMode( )
{
  _keepMode = true;
}
void RegExpButtons::slotSetNonKeepMode( )
{
  _keepMode = false;
}
void RegExpButtons::slotSelectNewAction()
{
  if ( ! _keepMode ) {
    emit doSelect();
    _grp->setButton(_grp->id(_selectBut));
  }
}
void RegExpButtons::setFeatures( int features )
{
    _wordBoundary->setShown( features & RegExpConverter::WordBoundary );
    _nonWordBoundary->setShown( features & RegExpConverter::NonWordBoundary );
    _posLookAhead->setShown( features & RegExpConverter::PosLookAhead );
    _negLookAhead->setShown( features & RegExpConverter::NegLookAhead );
}