/* ----------------------------------------------------------------------- */
/*                                                                         */
/* [klt.h]                      Klt                                   */
/*                                                                            */
/* -------------------------------------------------------------------------- */
/*                                                                            */
/* Copyright (c) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>            */
/*                                                                            */
/* This file is part of Klt, an X terminal.                               */
/*                                                                            */
/* The material contained in here more or less directly orginates from        */
/* kvt, which is copyright (c) 1996 by Matthias Ettrich <ettrich@kde.org>     */
/*                                                                            */
/* -------------------------------------------------------------------------- */

#ifndef KONSOLE_H
#define KONSOLE_H


#include <kmainwindow.h>
#include <kdialogbase.h>
#include <ksimpleconfig.h>
#include <kaction.h>
#include <kpopupmenu.h>
#include <keditcl.h>

#include <kwinmodule.h>

#include <qstrlist.h>
#include <qintdict.h>
#include <qptrdict.h>

#include "TEPty.h"
#include "TEWidget.h"
#include "TEmuVt102.h"
#include "session.h"
#include "schema.h"
#include "klt_child.h"

#include "kltiface.h"

#undef PACKAGE
#undef VERSION
#define PACKAGE "klt"
#define VERSION "1.2"

class KRootPixmap;
class QCheckBox;
class KltFind;

// Defined in main.C
const char *klt_shell(QStrList &args);

class Klt : public KMainWindow, virtual public KltIface
{
    Q_OBJECT

    friend class KltSessionManaged;
public:

  Klt(int isrestored );
  Klt(const QString &pgm, const QStrList &args,int isrestored );
  ~Klt();
  void setColLin(int columns, int lines);
  void setAutoClose(bool on);
  void initSessionFont(int fontNo);
  void initSessionTitle(const QString &_title);
  void initSessionKeyTab(const QString &keyTab);
	void initFullScreen();
  void initMasterMode(bool on);
  void newSession(const QString &program, const QStrList &args, const QString &term, const QString &icon, const QString &cwd);
  void setSchema(const QString & path);
  
  void enableFullScripting(bool b);

  void run();
  void setDefaultSession(const QString &filename);
  void showTipOnStart();


  // Additional functions for DCOP
  int sessionCount() { return sessions.count(); }

  QString currentSession();
  QString newSession(const QString &type);
  QString sessionId(const int position);

  void activateSession(const QString& sessionId);
  void feedAllSessions(const QString &text);
  void sendAllSessions(const QString &text);

  KURL baseURL() const;

  virtual bool processDynamic(const QCString &fun, const QByteArray &data, QCString& replyType, QByteArray &replyData);
  virtual QCStringList functionsDynamic();

public slots:
  void activateSession(const int position);

  void makeGUI();
  QString newSession();

protected:

 bool queryClose();
 bool resizeEvent();
 bool queryExit();
 void saveProperties(KConfig* config);
 void readProperties(KConfig* config);

 

private slots:
  void currentDesktopChanged(int desk);
  void slotBackgroundChanged(int desk);
  void configureRequest(TEWidget*,int,int,int);
  void activateSession();
  void activateSession(TESession*);
  void closeCurrentSession();
  void doneChild(KltChild*, TESession*);
  void doneSession(TESession*);
  void slotCouldNotClose();
  void schema_menu_activated(int item);
  void pixmap_menu_activated(int item);
  void keytab_menu_activated(int item);
  void schema_menu_check();
  void attachSession(TESession*);
  void detachSession();
  void newSession(int kind);
  void updateSchemaMenu();
  void updateKeytabMenu();

  void changeColumns(int);
  void changeLines(int);
  void notifySessionState(TESession* session,int state);
  void notifySize(int,int);
  void updateTitle();
  void activateMenu();
  void activateStatusBar(const QString &msg);
  void clearStatusBar();
  void setSchema(int n);
  void sendSignal(int n);
  void slotClearTerminal();
  void slotResetClearTerminal();
  void slotToggleMenubar();
  void slotToggleMasterMode();
  void slotSelectBell();
  void slotSelectAutoresize();
  void slotSelectSize();
  void slotSelectFont();
  void loadScreenSessions();

  void slotSaveSettings();
  void slotSaveSessionsProfile();
  void slotConfigureKeys();
  void slotConfigure();
  void reparseConfiguration();

  void clearAllListenToKeyPress();
  void restoreAllListenToKeyPress();
  void newSession( const QString&, const QString& );

  void fontNotFound();
  void showTip();

  void listSessions();
  void switchToSession1() { activateSession(1); }
  void switchToSession2() { activateSession(2); }
  void switchToSession3() { activateSession(3); }
  void switchToSession4() { activateSession(4); }
  void switchToSession5() { activateSession(5); }
  void switchToSession6() { activateSession(6); }
  void switchToSession7() { activateSession(7); }
  void switchToSession8() { activateSession(8); }
  void switchToSession9() { activateSession(9); }
  void switchToSession10() { activateSession(10); }
  void switchToSession11() { activateSession(11); }
  void switchToSession12() { activateSession(12); }

  void biggerFont();
  void smallerFont();

private:
  KSimpleConfig *defaultSession();
  QString newSession(KSimpleConfig *co, QString pgm = QString::null, const QStrList &args = QStrList(), const QString &_term = QString::null, const QString &_icon = QString::null, const QString &_title = QString::null, const QString &_cwd = QString::null,const QString &_font = QString::null );
  void readProperties(KConfig *config, const QString &schema, bool globalConfigOnly);
  void applySettingsToGUI();
  void makeBasicGUI();
  void runSession(TESession* s);
  void addSession(TESession* s);
  void setColorPixmaps();

  void setSchema(ColorSchema* s);
  void setFont(int fontno);

  void buildSessionMenus();
  void addSessionCommand(const QString & path);
  void loadSessionCommands();
  void addScreenSession(const QString & path, const QString & socket);
  void resetScreenSessions();

  QPtrDict<TESession> action2session;
  QPtrDict<KRadioAction> session2action;
  QPtrList<TESession> sessions;
  QPtrList<KltChild> detached;
  QIntDict<KSimpleConfig> no2command;
  QIntDict<KTempFile> no2tempFile;
  QIntDict<QString> no2filename;
  KSimpleConfig* m_defaultSession;
  QString m_defaultSessionFilename;

  TEWidget*      te;
  TESession*     se;
  TESession*     se_previous;
  TESession*     m_initialSession;
  ColorSchemaList* colors;

  KRootPixmap*   rootxpm;
  KWinModule*    kWinModule;

  KMenuBar*   menubar;
  KStatusBar* statusbar;

  KPopupMenu* m_session;
  KPopupMenu* m_edit;
  KPopupMenu* m_view;
  KPopupMenu* m_options;
  KPopupMenu* m_schema;
  KPopupMenu* m_keytab;
  KPopupMenu* m_signals;
  KPopupMenu* m_help;
  KPopupMenu* m_rightButton;

  KToggleAction *masterMode;
  KToggleAction *showMenubar;

  KSelectAction *selectSize;
  KSelectAction *selectFont;
  KSelectAction *selectBell;
  KSelectAction *selectAutoresize;

  KAction       *m_detachSession;
  KAction       *m_moveSessionLeft;
  KAction       *m_moveSessionRight;

  KAction       *m_copyClipboard;
  KAction       *m_pasteClipboard;
  KAction       *m_clearTerminal;
  KAction       *m_resetClearTerminal;
  KAction       *m_clearAllSessionHistories;
  KAction       *m_saveProfile;
  KAction       *m_closeSession;
  KAction       *m_quit;

  KActionCollection *m_shortcuts;

  KltFind* m_finddialog;
  bool         m_find_first;
  bool         m_find_found;
  QString      m_find_pattern;

  int cmd_serial;
  int cmd_first_screen;
  int         n_keytab;
  int         n_defaultKeytab;
  int         n_font;
  int         n_defaultFont; // font as set in config to use as default for new sessions
  int         n_scroll;
  int         n_bell;
  int         n_autoresize;
  int         n_render;
  int         curr_schema; // current schema no
  int         wallpaperSource;
  int         sessionIdCounter;
  int         monitorSilenceSeconds;

  QString     s_schema;
  QString     s_kconfigSchema;
  QString     s_word_seps;			// characters that are considered part of a word
  QString     pmPath; // pixmap path
  QString     dropText;
  QString     fontNotFound_par;
  QFont       defaultFont;
  QSize       defaultSize;

  QRect       _saveGeometry;

  QTimer      m_closeTimeout;

  bool        b_framevis:1;
  bool        m_menuCreated:1;
  bool        skip_exit_query:1;
  bool        b_warnQuit:1;
  bool        isRestored:1;
  bool        b_allowResize:1;
  bool        b_addToUtmp:1;
  bool        b_xonXoff:1;

  bool        b_histEnabled:1;
  bool        b_fullScripting:1;
  unsigned int m_histSize;
};

class QSpinBox;

class SizeDialog : public KDialogBase
{
    Q_OBJECT
public:
  SizeDialog(unsigned int const columns,
             unsigned int const lines,
             QWidget *parent);

public slots:
  void slotDefault();

  unsigned int columns() const;
  unsigned int lines() const;

protected:
  QSpinBox*  m_columns;
  QSpinBox*  m_lines;
};

class KltFind : public KEdFind
{
    Q_OBJECT
public:
  KltFind( QWidget *parent = 0, const char *name=0, bool modal=true );
  bool reg_exp() const;

private slots:
  void slotEditRegExp();

private:
  QCheckBox*    m_asRegExp;
  QDialog*      m_editorDialog;
  QPushButton*  m_editRegExp;
};

#endif
