/****************************************************************************
**
** Implementation of the TQTextBrowser class
**
** Created : 990101
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of the widgets module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "tqtextbrowser.h"
#ifndef TQT_NO_TEXTBROWSER
#include "../kernel/tqrichtext_p.h"

#include "tqapplication.h"
#include "tqlayout.h"
#include "tqpainter.h"

#include "tqvaluestack.h"
#include "stdio.h"
#include "tqfile.h"
#include "tqtextstream.h"
#include "tqlayout.h"
#include "tqbitmap.h"
#include "tqtimer.h"
#include "tqimage.h"
#include "tqsimplerichtext.h"
#include "tqdragobject.h"
#include "tqurl.h"
#include "tqcursor.h"

/*!
    \class TQTextBrowser tqtextbrowser.h
    \brief The TQTextBrowser class provides a rich text browser with hypertext navigation.

    \ingroup advanced
    \ingroup helpsystem
    \ingroup text
    \mainclass

    This class extends TQTextEdit (in read-only mode), adding some
    navigation functionality so that users can follow links in
    hypertext documents. The contents of TQTextEdit is set with
    setText(), but TQTextBrowser has an additional function,
    setSource(), which makes it possible to set the text to a named
    document. The name is looked up in the text view's mime source
    factory. If a document name ends with an anchor (for example, "\c
    #anchor"), the text browser automatically scrolls to that position
    (using scrollToAnchor()). When the user clicks on a hyperlink, the
    browser will call setSource() itself, with the link's \c href
    value as argument. You can track the current source by connetion
    to the sourceChanged() signal.

    TQTextBrowser provides backward() and forward() Q_SLOTS which you can
    use to implement Back and Forward buttons. The home() slot sets
    the text to the very first document displayed. The linkClicked()
    signal is emitted when the user clicks a link.

    By using TQTextEdit::setMimeSourceFactory() you can provide your
    own subclass of TQMimeSourceFactory. This makes it possible to
    access data from anywhere, for example from a network or from a
    database. See TQMimeSourceFactory::data() for details.

    If you intend using the mime factory to read the data directly
    from the file system, you may have to specify the encoding for the
    file extension you are using. For example:
    \code
    mimeSourceFactory()->setExtensionType("qml", "text/utf8");
    \endcode
    This is to ensure that the factory is able to resolve the document
    names.

    TQTextBrowser interprets the tags it processes in accordance with
    the default style sheet. Change the style sheet with
    \l{setStyleSheet()}; see TQStyleSheet for details.

    If you want to provide your users with editable rich text use
    TQTextEdit. If you want a text browser without hypertext navigation
    use TQTextEdit, and use TQTextEdit::setReadOnly() to disable
    editing. If you just need to display a small piece of rich text
    use TQSimpleRichText or TQLabel.

    <img src=qtextbrowser-m.png> <img src=qtextbrowser-w.png>
*/

class TQTextBrowserData
{
public:
    TQTextBrowserData():textOrSourceChanged(FALSE) {}

    TQValueStack<TQString> stack;
    TQValueStack<TQString> forwardStack;
    TQString home;
    TQString curmain;
    TQString curmark;

    /*flag necessary to give the linkClicked() signal some meaningful
      semantics when somebody connected to it calls setText() or
      setSource() */
    bool textOrSourceChanged;
};


/*!
    Constructs an empty TQTextBrowser called \a name, with parent \a
    parent.
*/
TQTextBrowser::TQTextBrowser(TQWidget *parent, const char *name)
    : TQTextEdit( parent, name )
{
    setReadOnly( TRUE );
    d = new TQTextBrowserData;

    viewport()->setMouseTracking( TRUE );
}

/*!
    \reimp
*/
TQTextBrowser::~TQTextBrowser()
{
    delete d;
}


/*!
    \property TQTextBrowser::source
    \brief the name of the displayed document.

    This is a TQString::null if no document is displayed or if the
    source is unknown.

    Setting this property uses the mimeSourceFactory() to lookup the
    named document. It also checks for optional anchors and scrolls
    the document accordingly.

    If the first tag in the document is \c{<qt type=detail>}, the
    document is displayed as a popup rather than as new document in
    the browser window itself. Otherwise, the document is displayed
    normally in the text browser with the text set to the contents of
    the named document with setText().

    If you are using the filesystem access capabilities of the mime
    source factory, you must ensure that the factory knows about the
    encoding of specified files; otherwise no data will be available.
    The default factory handles a couple of common file extensions
    such as \c *.html and \c *.txt with reasonable defaults. See
    TQMimeSourceFactory::data() for details.
*/

TQString TQTextBrowser::source() const
{
    if ( d->stack.isEmpty() )
	return TQString::null;
    else
	return d->stack.top();
}

/*!
    \property TQTextBrowser::undoDepth
    \brief This text browser's undo depth.
*/

/*!
    \property TQTextBrowser::overwriteMode
    \brief This text browser's overwrite mode.
*/

/*!
    \property TQTextBrowser::modified
    \brief Whether the contents have been modified.
*/

/*!
    \property TQTextBrowser::readOnly
    \brief Whether the contents are read only.
*/

/*!
    \property TQTextBrowser::undoRedoEnabled
    \brief Whether undo and redo are enabled.
*/



/*!
    Reloads the current set source.
*/

void TQTextBrowser::reload()
{
    TQString s = d->curmain;
    d->curmain = "";
    setSource( s );
}


void TQTextBrowser::setSource(const TQString& name)
{
#ifndef TQT_NO_CURSOR
    if ( isVisible() )
	tqApp->setOverrideCursor( Qt::WaitCursor );
#endif
    d->textOrSourceChanged = TRUE;
    TQString source = name;
    TQString mark;
    int hash = name.find('#');
    if ( hash != -1) {
	source = name.left( hash );
	mark = name.mid( hash+1 );
    }

    if ( source.left(5) == "file:" )
	source = source.mid(6);

    TQString url = mimeSourceFactory()->makeAbsolute( source, context() );
    TQString txt;
    bool dosettext = FALSE;

    if ( !source.isEmpty() && url != d->curmain ) {
	const TQMimeSource* m =
		    TQT_TQMIMESOURCE_CONST(mimeSourceFactory()->data( source, context() ));
	if ( !m ){
	    qWarning("TQTextBrowser: no mimesource for %s", source.latin1() );
	}
	else {
	    if ( !TQTextDrag::decode( m, txt ) ) {
		qWarning("TQTextBrowser: cannot decode %s", source.latin1() );
	    }
	}
	if ( isVisible() ) {
	    TQString firstTag = txt.left( txt.find( '>' ) + 1 );
	    if ( firstTag.left( 3 ) == "<qt" && firstTag.contains( "type" ) && firstTag.contains( "detail" ) ) {
		popupDetail( txt, TQCursor::pos() );
#ifndef TQT_NO_CURSOR
		tqApp->restoreOverrideCursor();
#endif
		return;
	    }
	}

	d->curmain = url;
	dosettext = TRUE;
    }

    d->curmark = mark;

    if ( !mark.isEmpty() ) {
	url += "#";
	url += mark;
    }
    if ( !d->home )
	d->home = url;

    if ( d->stack.isEmpty() || d->stack.top() != url)
	d->stack.push( url );

    int stackCount = (int)d->stack.count();
    if ( d->stack.top() == url )
	stackCount--;
    emit backwardAvailable( stackCount > 0 );
    stackCount = (int)d->forwardStack.count();
    if ( d->forwardStack.isEmpty() || d->forwardStack.top() == url )
	stackCount--;
    emit forwardAvailable( stackCount > 0 );

    if ( dosettext )
	TQTextEdit::setText( txt, url );

    if ( !mark.isEmpty() )
	scrollToAnchor( mark );
    else
	setContentsPos( 0, 0 );

#ifndef TQT_NO_CURSOR
    if ( isVisible() )
	tqApp->restoreOverrideCursor();
#endif

    emit sourceChanged( url );
}

/*!
    \fn void TQTextBrowser::backwardAvailable(bool available)

    This signal is emitted when the availability of backward()
    changes. \a available is FALSE when the user is at home();
    otherwise it is TRUE.
*/

/*!
    \fn void TQTextBrowser::forwardAvailable(bool available)

    This signal is emitted when the availability of forward() changes.
    \a available is TRUE after the user navigates backward() and FALSE
    when the user navigates or goes forward().
*/

/*!
    \fn void TQTextBrowser::sourceChanged( const TQString& src)

    This signal is emitted when the mime source has changed, \a src
    being the new source.

    Source changes happen both programmatically when calling
    setSource(), forward(), backword() or home() or when the user
    clicks on links or presses the equivalent key sequences.
*/

/*!  \fn void TQTextBrowser::highlighted (const TQString &link)

    This signal is emitted when the user has selected but not
    activated a link in the document. \a link is the value of the \c
    href i.e. the name of the target document.
*/

/*!
    \fn void TQTextBrowser::linkClicked( const TQString& link)

    This signal is emitted when the user clicks a link. The \a link is
    the value of the \c href i.e. the name of the target document.

    The \a link will be the absolute location of the document, based
    on the value of the anchor's href tag and the current context of
    the document.

    \sa anchorClicked(), context()
*/

/*!
    \fn void TQTextBrowser::anchorClicked( const TQString& name, const TQString &link)

    This signal is emitted when the user clicks an anchor. The \a link is
    the value of the \c href i.e. the name of the target document.  The \a name
    is the name of the anchor.

    \sa linkClicked()
*/

/*!
    Changes the document displayed to the previous document in the
    list of documents built by navigating links. Does nothing if there
    is no previous document.

    \sa forward(), backwardAvailable()
*/
void TQTextBrowser::backward()
{
    if ( d->stack.count() <= 1)
	return;
    d->forwardStack.push( d->stack.pop() );
    setSource( d->stack.pop() );
    emit forwardAvailable( TRUE );
}

/*!
    Changes the document displayed to the next document in the list of
    documents built by navigating links. Does nothing if there is no
    next document.

    \sa backward(), forwardAvailable()
*/
void TQTextBrowser::forward()
{
    if ( d->forwardStack.isEmpty() )
	return;
    setSource( d->forwardStack.pop() );
    emit forwardAvailable( !d->forwardStack.isEmpty() );
}

/*!
    Changes the document displayed to be the first document the
    browser displayed.
*/
void TQTextBrowser::home()
{
    if (!d->home.isNull() )
	setSource( d->home );
}

/*!
    The event \a e is used to provide the following keyboard shortcuts:
    \table
    \header \i Keypress	    \i Action
    \row \i Alt+Left Arrow  \i \l backward()
    \row \i Alt+Right Arrow \i \l forward()
    \row \i Alt+Up Arrow    \i \l home()
    \endtable
*/
void TQTextBrowser::keyPressEvent( TQKeyEvent * e )
{
    if ( e->state() & TQt::AltButton ) {
	switch (e->key()) {
	case Qt::Key_Right:
	    forward();
	    return;
	case Qt::Key_Left:
	    backward();
	    return;
	case Qt::Key_Up:
	    home();
	    return;
	}
    }
    TQTextEdit::keyPressEvent(e);
}

class TQTextDetailPopup : public TQWidget
{
public:
    TQTextDetailPopup()
	: TQWidget ( 0, "automatic TQText detail widget", (TQt::WidgetFlags)((TQt::WidgetFlags)WType_Popup | TQt::WDestructiveClose) )
	{
	}

protected:

    void mousePressEvent( TQMouseEvent*)
	{
	close();
	}
};


void TQTextBrowser::popupDetail( const TQString& contents, const TQPoint& pos )
{

    const int shadowWidth = 6;   // also used as '5' and '6' and even '8' below
    const int vMargin = 8;
    const int hMargin = 12;

    TQWidget* popup = new TQTextDetailPopup;
    popup->setBackgroundMode( TQt::NoBackground );

    TQSimpleRichText* doc = new TQSimpleRichText( contents, popup->font() );
    doc->adjustSize();
    TQRect r( 0, 0, doc->width(), doc->height() );

    int w = r.width() + 2*hMargin;
    int h = r.height() + 2*vMargin;

    popup->resize( w + shadowWidth, h + shadowWidth );

    // okay, now to find a suitable location
    //###### we need a global fancy popup positioning somewhere
    popup->move(pos - popup->rect().center());
    if (popup->tqgeometry().right() > TQApplication::desktop()->width())
	popup->move( TQApplication::desktop()->width() - popup->width(),
		     popup->y() );
    if (popup->tqgeometry().bottom() > TQApplication::desktop()->height())
	popup->move( popup->x(),
		     TQApplication::desktop()->height() - popup->height() );
    if ( popup->x() < 0 )
	popup->move( 0, popup->y() );
    if ( popup->y() < 0 )
	popup->move( popup->x(), 0 );


    popup->show();

    // now for super-clever shadow stuff.  super-clever mostly in
    // how many window system problems it skirts around.

    TQPainter p( popup );
    p.setPen( TQApplication::tqpalette().active().foreground() );
    p.drawRect( 0, 0, w, h );
    p.setPen( TQApplication::tqpalette().active().mid() );
    p.setBrush( TQColor( 255, 255, 240 ) );
    p.drawRect( 1, 1, w-2, h-2 );
    p.setPen( Qt::black );

    doc->draw( &p, hMargin, vMargin, r, popup->tqcolorGroup(), 0 );
    delete doc;

    p.drawPoint( w + 5, 6 );
    p.drawLine( w + 3, 6,
		w + 5, 8 );
    p.drawLine( w + 1, 6,
		w + 5, 10 );
    int i;
    for( i=7; i < h; i += 2 )
	p.drawLine( w, i,
		    w + 5, i + 5 );
    for( i = w - i + h; i > 6; i -= 2 )
	p.drawLine( i, h,
		    i + 5, h + 5 );
    for( ; i > 0 ; i -= 2 )
	p.drawLine( 6, h + 6 - i,
		    i + 5, h + 5 );
}

/*!
    \fn void TQTextBrowser::setText( const TQString &txt )

    \overload

    Sets the text to \a txt.
*/

/*!
    \reimp
*/

void TQTextBrowser::setText( const TQString &txt, const TQString &context )
{
    d->textOrSourceChanged = TRUE;
    d->curmark = "";
    d->curmain = "";
    TQTextEdit::setText( txt, context );
}

void TQTextBrowser::emitHighlighted( const TQString &s )
{
    emit highlighted( s );
}

void TQTextBrowser::emitLinkClicked( const TQString &s )
{
    d->textOrSourceChanged = FALSE;
    emit linkClicked( s );
    if ( !d->textOrSourceChanged )
	setSource( s );
}

#endif  // TQT_NO_TEXTBROWSER
