/***************************************************************************
                          quanta.h  -  description
                             -------------------
    begin                : ?? ???  9 13:29:57 EEST 2000
    copyright            : (C) 2000 by Dmitry Poplavsky & Alexander Yakovlev & Eric Laffoon <pdima@users.sourceforge.net,yshurik@linuxfan.com,sequitur@easystreet.com>
                           (C) 2001-2004 by Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef QUANTA_H
#define QUANTA_H

#define QUANTA_PACKAGE "quanta"
#define QUANTA_VERSION "3.5.13"

#define IDS_STATUS      1
#define IDS_INS_OVR     2
#define IDS_MODIFIED    3
#define IDS_STATUS_CLM  4
// Number 5 is used by the debugger, but not added until debugger is activated
#define IDS_STATUS_DEBUGGER  5

#define IDS_DEFAULT     "Ready."

// include files for TQt
#include <tqmap.h>
#include <tqdict.h>
#include <tqvaluelist.h>
#include <tqstrlist.h>
#include <tqptrlist.h>

// include files for KDE
#include <tdeversion.h>
#include <kdockwidget.h>
#include <tdeparts/browserextension.h>
#include <tdeparts/dockmainwindow.h>

#include <tdemdimainfrm.h>

//app includes
#include "dcopwindowmanagerif.h"

// forward declaration
class DCOPSettings;
class DCOPQuanta;

class QuantaPluginInterface;

class QuantaDoc;
class QuantaView;
class QNewDTEPStuff;
class QNewToolbarStuff;
class QNewTemplateStuff;
class QNewScriptStuff;
class QNewDocStuff;

class TQTabWidget;
class TQWidgetStack;
class TQListViewItem;

class TQPopupMenu;

class WHTMLPart;
class TDEHTMLView;

class TDEAction;
class TDEToggleAction;
class TDEProcess;
class TDESelectAction;
class TDERecentFilesAction;
class TDEToolBarPoupAction;
class KTextEdit;

class TagAction;
class Node;
class Parser;
class DocTreeView;
class FilesTreeView;
class ScriptTreeView;
class EnhancedTagAttributeTree;
class Project;
class AnnotationOutput;
class MessageOutput;
class TQDomDocument;
class Document;
class DebuggerManager;
class QuantaInit;
class TDEToolBarPopupAction;
class KTempFile;
namespace KTextEditor
{
  class Mark;
  class View;
}

struct DirInfo;
struct EventAction;
struct ToolbarEntry;


/**
  * The base class for Quanta application windows.
  */
class QuantaApp : public KMdiMainFrm, public DCOPWindowManagerIf
{
  TQ_OBJECT
  

friend class QuantaInit;

public:
  QuantaApp(int mdiMode);
  ~QuantaApp();

  QuantaDoc  *doc() const {return m_doc; }
  TQPopupMenu *tagsMenu() const {return m_tagsMenu;}
  TDEConfig *config() const {return m_config;}

//TODO: check if we really need these "get" methods (and get rid o get)
  MessageOutput *messageOutput() const {return m_messageOutput;}
  MessageOutput *problemOutput() const {return m_problemOutput;}
  AnnotationOutput *annotationOutput() const {return m_annotationOutput;}

  DebuggerManager *debugger() const {return m_debugger;}
  KParts::PartManager *partManager() {return m_partManager;}

  TQWidget* createContainer(TQWidget *parent, int index, const TQDomElement &element, int &id );
  void removeContainer(TQWidget *container, TQWidget *parent, TQDomElement &element, int id );
/** Returns the project's base URL if it exists, the HOME dir if there is no project and no opened document (or the current opened document was not saved yet), and the base URL of the opened document, if it is saved somewhere.

  maps to the same function in Project*/
  KURL projectBaseURL() const;

  /** Returns the project (if there is one loaded) or global default encoding. */
  TQString defaultEncoding();
  /** Returns the interface number for the currently active editor. */
  int currentEditorIfNum() const;
  /** Return the URL of the currently active document */
  TQString currentURL() const;
  /** Return the URL of the currently project */
  TQString projectURL() const;
  /** Return the list of opened URLs and their editor interface numbers*/
  TQStringList openedURLs() const;
  TQString saveCurrentFile();
  /**
   * Sets the DTEP for the current document.
   * @param dtepName the name (nickname or full name) of the DTEP
   * @param convert  if true, converts the !DOCTYPE line to the new DTEP
   */
  void setDtep(const TQString& dtepName, bool convert);
  TQStringList tagAreas(const TQString& name, bool includeCoordinates, bool skipFoundContent) const;
  TQString documentFolderForURL(const TQString &url);
  TQString urlWithPreviewPrefix(const TQString &url);
  void addFileToProject(const TQString &url);
  void addFolderToProject(const TQString &url);
  void uploadURL(const TQString &url, const TQString& profile, bool markOnly);  /** Capture DCOP signals from KXsldbgPart or similar plugin */
  void newCursorPosition(const TQString &file, int lineNumber, int columnNumber);
  void newDebuggerPosition(const TQString &file, int lineNumber);
  void openFile(const TQString &file, int lineNumber, int columnNumber);

  /** reparse current document and initialize node. */
  void reparse(bool force);

  bool structTreeVisible() const;
  bool previewVisible() const {return m_previewVisible;}

  //return the old Cursor position
  void oldCursorPos(uint &line, uint &col) {line = oldCursorLine; col = oldCursorCol;}

  TQStringList selectors(const TQString& tag);
  TQStringList idSelectors();
  WHTMLPart *documentationPart() {return m_htmlPartDoc;}
  /** Show the toolbar which is in url. If it was not loaded yet, it loads the
      toolbar from the file */
  void showToolbarFile(const KURL &url);

  void setParserEnabled(bool enabled) {m_parserEnabled = enabled;}

    /** tabs for left panel */
  DocTreeView *dTab;
  EnhancedTagAttributeTree *aTab;

/**
   * Show a TagDialog of Node tag, with attrs attr.
   * @param tag The name of the new Node to create.
   * @param attr The string containing the attrs of the new Node to create.
   * @return Returns a new Node created according to the contents of the TagDialog.
   */
  Node *showTagDialogAndReturnNode(const TQString &tag, const TQString &attr = TQString());
  /** Returns the baseURL of the document. */
  KURL baseURL();

  void startIdleTimer();

  /** Called when a document was closed. Resets some variables. */
  void slotFileClosed(Document *w);
  void setTabToolTip(TQWidget *w, const TQString& toolTipStr);

  void createPreviewPart();
  void createDocPart();

  /**
   * This is a pool for actions waiting for being applied (VPL only yet).
   * For example, whee a user presses the bold action, it isn't immediately applied on the document.
   * If the user presses a key right away the character is inserted inside the tags for the queued actions.
   * If the user changes the place of the cursor, the actions waiting for being inserted are removed.
   */
  TQStringList const& tagActionPool() const {return m_tagActionPool;}
  void insertTagActionPoolItem(TQString const& action_item);
  void removeTagActionPoolItem(TQString const& action_item);
  void removeAllTagActionPoolItems() {m_tagActionPool.clear();}
  /** Updates the structure and attribute treeview. */
  void updateTreeViews();
  void setTitle(const TQString&);

  TQPtrList<TagAction> const& tagActions() const {return m_tagActions;}

  /** Clicked word or selected text for context sensitive menu in editor */
  TQString popupWord;

signals: // Signals
  /** signal used to hide the splash screen */
  void showSplash(bool);
  // trees need reload because hidden files option changed
  void reloadAllTrees();

  /** Emitted when some kind of event that can have associated actions has happened. */
  void eventHappened(const TQString&, const TQString&, const TQString& );

  void toolbarRemoved(const TQString&);

  void showMessage(const TQString&, bool);
  void clearMessages();

public slots:
  void slotFileNew();
  void slotFileOpen();
  void slotFileOpen(const KURL &url);
  void slotFileOpen(const KURL &url, const TQString &encoding);
  void slotFileOpen(const KURL &url, const TQString &encoding, bool readOnly);
  void slotFileOpen(const KURL::List &urls, const TQString& encoding);
  void slotFileSave();
  bool slotFileSaveAs(QuantaView *viewToSave = 0L);
  void slotFileSaveAsLocalTemplate();
  void slotFileSaveAsProjectTemplate();
  void slotFileSaveSelectionAsLocalTemplate();
  void slotFileSaveSelectionAsProjectTemplate();
  void slotFileSaveAll();
  void slotFileReload(QuantaView *view = 0L);
  void slotFileReloadAll();
  /** Close the document specified in the parameter if it's opened */
  void slotFileClose(const KURL &url);
  void slotFileCloseAll();
  void slotFileQuit();

  void slotOpenFileInPreview(const KURL &url);
  void slotImageOpen(const KURL &url);
  void slotFileOpenRecent(const KURL &url);
  void slotInsertTag(const KURL&, DirInfo);

  void slotEditFindInFiles();
  /// open url in documentation window
  void openDoc(const TQString& url);

  void slotContextHelp();

  void slotBack();
  void slotForward();

  void statusBarTimeout();
  /** Shows the message in the status bar.
       WARNING: Don't use in place where nothing should happen until the function
       exits (like in startup code, DTD reading, etc.) as it calls processEvents() and
       unexpected things may happen. */
  void slotStatusMsg(const TQString &text);

  void slotNewStatus();
  void slotNewLineColumn();
//  void slotUpdateStatus(TQWidget*);FIXME:

  /** repaint preview */
  void slotRepaintPreview();
  /** toggles showing the preview */
  void slotToggleShowPreview();
  /** Shows the preview widget and repaints the preview or
        hides the preview widget and restores the original document
  */
  void slotShowPreviewWidget(bool show);
  /** Called from the view manager when the status of the preview must change:
        - hide if the preview was in the editor area
        - update if the preview is in a toolview
   */
  void slotChangePreviewStatus();
  /** Called when the preview widget got or lost the focus */
  void slotPreviewHasFocus(bool focus);

  void slotShowMessagesView();
  void slotShowProblemsView();
  void slotShowAnnotationView();

  void slotContextMenuAboutToShow();

  /** options slots */
  void slotShowMenuBar();
  void slotPreviewOptions();
  void slotOptions();
  void slotOptionsConfigureKeys();
  void slotOptionsConfigureToolbars();
  void slotNewToolbarConfig();
  /** Configure toolbars, show defaultToolbar by default */
  void slotConfigureToolbars(const TQString& defaultToolbar = TQString());
  void slotOptionsConfigureActions();

  void setCursorPosition(int row, int col );
  void gotoFileAndLine(const TQString& filename, int line, int column);

  void selectArea(int line1, int col1, int line2, int col2);

  /** Insert the content of the url into the current document. */
  void slotInsertFile(const KURL& url);
  /** Sends a toolbar in mail. */
  void slotSendToolbar();
  /** Removes a user toolbar from the toolbars. */
  bool slotRemoveToolbar();
  /** Adds a new, empty toolbar. */
  void slotAddToolbar();
  /** Saves a toolbar as local specific. */
  void slotSaveLocalToolbar();
  /** Saves a toolbar as project specific. */
  void slotSaveProjectToolbar();
  /** Loads the toolbars for dtd named dtdName and unload the ones belonging to oldDtdName. */
  void slotLoadToolbarForDTD(const TQString& dtdName);
  /** Load an user toolbar from the disk. */
  void slotLoadToolbarFile(const KURL& url);
  /** Load an user toolbar from the disk. */
  void slotLoadToolbar();
  /** Load a global toolbar from the disk. */
  void slotLoadGlobalToolbar();
  /** Remove the toolbar named "name". */
  bool slotRemoveToolbar(const TQString& name);
  /** Rename the toolbar named "name". */
  void slotRenameToolbar(const TQString& name);
  /** Rename the toolbar. */
  void slotRenameToolbar();
  /** Delete an action */
  void slotDeleteAction(TDEAction *action);
  /** Remove the action from toolbar*/
  void slotRemoveAction(const TQString&, const TQString& actionName);
  /** Edit the action */
  void slotEditAction(const TQString&);
  /** Creates a new, empty action */
  void slotNewAction();
  /** Creates a script action for a_scriptURL using the a_interpreter as the script
  interpreter application */
  void slotAssignActionToScript(const KURL&a_scriptURL, const TQString& a_interpreter);

  /** Change the DTD/DTEP of the current document. */
  void slotChangeDTD();
  /** Edit a DTD/DTEP description. */
  void slotEditDTD();
  /** Called when the user explicetly requested the completion box.*/
  void slotShowCompletion();
  /** Called when the user explicetly requested the completion hint.*/
  void slotShowCompletionHint();
  /** Show or hide the groups for dtepName in the structure tree.
    The special value of "clear" for dtepName means show groups
    for all DTEPs found in the document.*/
  void slotShowGroupsForDTEP(const TQString& dtepName, bool show);
  /** Build the project specific toolbar menu. */
  void slotBuildPrjToolbarsMenu();

  void slotReparse();
  void slotForceReparse();

  void slotExpandAbbreviation();

  /** Format the code of the document */
  void slotCodeFormatting();
  /** Show the Document Properties Dialog */
  void slotDocumentProperties();
  /** Show the Document Properties Dialog
   * If forceInsertionOfMinimalTree and the user haven't modified something in the dialog,
   * it will anyway insert a Minimal tree (HTML, BODY, ...)
   */
  void documentProperties(bool forceInsertionOfMinimalTree = false);
  /** No descriptions */
  void slotAutosaveTimer();

  void slotHideSplash() {emit showSplash(false);}

  void slotConvertCase();

  /** Reload the tree of the StructTreeView */
  void slotReloadStructTreeView(bool groupOnly = false);

  void slotReportBug();
  /** registers a new part in the partmanager */
  void slotNewPart(KParts::Part *newPart, bool setActiv);

  void slotUploadFile(QuantaView *view=0L);
  void slotDeleteFile(QuantaView *view=0L);

  /** Called when the CVS command working on files was executed successfully. */
  void slotCVSCommandExecuted(const TQString &command, const TQStringList &files);

  /** Called when the preview or documentation part is deleted. */
  void slotHTMLPartDeleted(TQObject *object);

  void slotRefreshActiveWindow();

  bool slotEnableIdleTimer(bool enable);

//Overridden KMdiMainFrm slots
  virtual void closeAllViews();
  virtual void closeActiveView();
  virtual void switchToToplevelMode();
  virtual void switchToChildframeMode();
  virtual void switchToIDEAlMode();
  virtual void switchToTabPageMode();
  /** appends all visible user toolbar urls to the list */
  void slotGetUserToolbarFiles(KURL::List *list);

  // Get saved position of dock
  KDockWidget::DockPosition prevDockPosition(TQWidget* dock, KDockWidget::DockPosition def);

protected slots:
  void slotDockWidgetHasUndocked(KDockWidget *widget);
  void slotPreviewBeingClosed();
  /** Show or hide the DTD toolbar */
  void slotToggleDTDToolbar(bool show);
  /** Loads a DTEP*/
  void slotLoadDTEP();
  /** Sends a DTEP in email */
  void slotEmailDTEP();
  /** Downloads a DTEP from the main server */
  void slotDownloadDTEP();
  /** Uploads a DTEP to the main server */
  void slotUploadDTEP();
  /** Downloads a toolbar from the main server */
  void slotDownloadToolbar();
  /** Uploads a toolbar to the main server */
  void slotUploadToolbar();
  /** Enable/Disable Smart Tag Insertion */
  void slotSmartTagInsertion();
  /** Downloads a template from the main server */
  void slotDownloadTemplate();
  /** Uploads a template to the main server */
  void slotUploadTemplate(const TQString &fileName);
  /** Downloads a script from the main server */
  void slotDownloadScript();
  /** Uploads a script to the main server */
  void slotUploadScript(const TQString &fileName);
  /** Downloads a documentation from the main server */
  void slotDownloadDoc();
  /** Shows tip of the day */
  void slotHelpTip();
  void slotOpenFileUnderCursor();
  void slotUploadOpenedFiles();
  /** Called after there was no user activity - cursor movement - for xx ms*/
  void slotIdleTimerExpired();
  void slotShowNoFramesPreview();
  /** Get script output */
  void slotGetScriptOutput(TDEProcess*, char*, int);
  /** Get script error */
  void slotGetScriptError(TDEProcess*, char*, int);
  /** Notify when process exits */
  void slotProcessExited(TDEProcess*);
  /** External app execution timeout handling */
  void slotProcessTimeout();

  /** connected to the part manager, activates a new part */
  void slotActivePartChanged(KParts::Part * );

  void slotTagMail();
  void slotTagQuickList();
  void slotTagEditTable();
  void slotTagColor();
  void slotTagDate();
  void slotTagSelect();
  /** Add the starting and closing text for a
  user specified tag. */
  void slotTagMisc();
  void slotEditCurrentTag();
  void slotSelectTagArea();
  void slotSelectTagArea(Node *node);

  void slotInsertCSS();
  void slotFrameWizard();
  void slotViewInKFM();
  void slotViewInLynx();

  void slotPasteHTMLQuoted();
  void slotPasteURLEncoded();
  void slotInsertChar();

//Edit
  void slotUndo ();
  void slotRedo ();
  void slotCut();
  void slotCopy();
  void slotPaste();

  void slotShowSourceEditor();
  void slotShowVPLAndSourceEditor();
  void slotShowVPLOnly();
  void slotTabDragged(TQWidget *widget);
  void slotTabMoved(int from, int to);
  void slotTabAboutToMove(int from, int to);
  void slotAnnotate();

protected:
  /** Create a DTEP tarball which can be uploaded or sent in email. Returns
  *   the name of the created file or TQString() if creation has failed.
  */
  TQString createDTEPTarball();

  /** Create a toolbar tarball which can be uploaded or sent in email. Returns
  *   the name of the created file or TQString() if creation has failed.
  */
  TQString createToolbarTarball();

  /** Ask for save all the modified user toolbars. */
  bool removeToolbars();
  /** Returns true if all toolbars are hidden, false otherwise. */
  bool allToolbarsHidden() const;
  /** No descriptions */
  virtual void focusInEvent(TQFocusEvent*);
  void saveOptions();

  virtual bool queryClose();
  void saveAsTemplate (bool projectTemplate, bool selectionOnly = false);
  /** Saves a toolbar as local or project specific. */
  bool saveToolbar(bool localToolbar = true, const TQString& toolbarToSave = TQString(), const KURL& destURL = KURL());
  /** Saves the toolbar and the actions. Returns the name of the actions file*/
  KURL saveToolbarToFile(const TQString& toolbarName,const KURL& destFile);
  /** Makes the tabwidget look and behave like we want. If closeButtonsOnly is true,
  only the close button behavior is changed. */
  void initTabWidget(bool closeButtonsOnly = false);

  void dropEvent(TQDropEvent *ev);
  void dragEnterEvent ( TQDragEnterEvent * );

  void resetDockLayout();

  ScriptTreeView *scriptTab;

private:
  FilesTreeView *fTab;
  ToolbarEntry *toolbarByURL(const KURL& url);
  /** Message output window */
  MessageOutput *m_messageOutput;
  MessageOutput *m_problemOutput;
  AnnotationOutput *m_annotationOutput;
  KMdiToolViewAccessor* m_messageOutputView;
  KMdiToolViewAccessor* m_problemsOutputView;
  KMdiToolViewAccessor* m_annotationOutputView;
  KMdiToolViewAccessor* m_previewToolView;
  KMdiToolViewAccessor* m_documentationToolView;
  Document *m_previewedDocument;

  // Debugger
  DebuggerManager *m_debugger;

  QuantaPluginInterface *m_pluginInterface;

  TQPopupMenu *m_tagsMenu;

  // config
  TDEConfig *m_config;

   /** HTML class for preview */
  WHTMLPart *m_htmlPart;
  WHTMLPart *m_htmlPartDoc;

  // DOC & VIEW
  QuantaDoc  *m_doc;

  /** parsered tree of document  */
  TQTimer *statusbarTimer;

  // ACTIONS
  TDERecentFilesAction *projectToolbarFiles;

  TDEToggleAction *showVPLAction, *showSourceAction, *showVPLSourceAction, *showDTDToolbar;
  TDEToolBarPopupAction *showPreviewAction;

  TDEAction *saveAction, *saveAllAction;

  TDEAction *editTagAction, *selectTagAreaAction;

  TQDomDocument* m_actions;

  TQPtrList<KTextEditor::Mark> markList;

  int currentPageIndex;
  uint userToolbarsCount;
  bool previewCopyMade;
  KTempFile *previewTmpFile;

  uint cursorLine;
  uint cursorCol;
  uint oldCursorLine;
  uint oldCursorCol;
  bool m_previewVisible;
  bool m_noFramesPreview;
  bool m_parserEnabled; ///< enables/disables reparsing. If false, even a forced reparse is ignored (used when opening multiple files)

  TQString m_scriptOutput;

  TQStringList m_tagActionPool;
  TQPtrList<TagAction> m_tagActions;

protected: // Protected attributes
  /** Timer to refresh the structure tree. */
  TQTimer *refreshTimer;
  /** Timer to detect idle periods. Every time the cursor moves the timer is
  restarted.*/
  TQTimer *idleTimer;
  /** The toolbars for this DTD are currently shown to the user. */
  TQString currentToolbarDTD;
  KDockWidget *m_oldTreeViewWidget;
  /** The ids of the widgets visible before doing the preview/documentation browsing */
  TQValueList<int> previousWidgetList;

  /* Store the old shortcuts from the local quantaui.rc */
  TQMap<TQString, TQString> oldShortcuts;
  KURL urlUnderCursor;
  TQTimer *autosaveTimer;
  DCOPSettings *dcopSettings;
  DCOPQuanta *dcopQuanta;
  KParts::PartManager *m_partManager;  ///< the pointer to the part manager
  TQGuardedPtr<KTextEditor::View> m_oldKTextEditor;  ///< remembers the last activated GUI
  QNewDTEPStuff *m_newDTEPStuff;
  QNewToolbarStuff *m_newToolbarStuff;
  QNewTemplateStuff *m_newTemplateStuff;
  QNewScriptStuff *m_newScriptStuff;
  QNewDocStuff *m_newDocStuff;
  TQDict<ToolbarEntry> m_toolbarList;

public: //TODO: check if it's worth to make a read method for them
  TDERecentFilesAction *fileRecent;
  /** True when the whole quanta is initialized. */
  bool quantaStarted;
  bool m_loopStarted; //true if an internal event loop has been started
  bool m_idleTimerEnabled;
  QuantaInit *m_quantaInit;  ///< the pointer to all the init stuff
};

#endif // QUANTA_H
