/****************************************************************************
    Kalkulon - A programmable calculator for programmers

    This file is part of the Kalkulon GUI.

    If you find this source code useful, your are encouraged to use it in your own projects.
    Please give me credit!

    COPYRIGHT by Juergen Holetzeck 2003 - 2007
    e-mail:		contact@kalkulon.de
    homepage:	www.kalkulon.de
****************************************************************************/

#ifndef KALKULONDLG_H
#define KALKULONDLG_H

#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QShortcut>

#include "ui_kalkulon.h"
#include "SyntaxHighlighter.h"

#include "../core/kalkuloncore.h"

class MyCallback;

class CKalkulonDlg : public QMainWindow, public Ui::CKalkulonDlg
{
    Q_OBJECT

public:
    CKalkulonDlg(QWidget *parent = 0);

    void print(QString text, bool input = true, bool lf = true);

private slots:
    void on_calcBtn_clicked();
    void on_shiftBtn_clicked();
    void on_pinBtn_clicked();
    void on_zoominBtn_clicked();
    void on_zoomoutBtn_clicked();
    void on_menuBtn_clicked();
    void on_inputWin_cursorPositionChanged();
    void on_inputWin_textChanged();

    void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
    void shortcutCopyLastResultActivated();
    void openHelp();
    void openOptionsDlg();
    void about();
    void clearAll();

private:
// overloaded methods
    virtual void keyPressEvent(QKeyEvent* event);
    virtual bool eventFilter(QObject *target, QEvent *event);
    virtual void closeEvent(QCloseEvent *event);

// private methods
    void printBase(double d);
    void printLabel();
    void doCalculation(QString expr);
    void lockInput(bool lock = true);
    void clearInput();

    void writeSettings();
    void readSettings();

    // UI stuff
    void setupUIpart2();			// part of construction
    void createActionsAndMenus();	// part of construction

    void setNewPointSize();
    void execMenu();
    void doBracketMatching();

// private attributes
    SyntaxHighlighter *shlOutput_, *shlInput_, *shlBase_;

    bool isExpanded_;
    bool isPinned_;

    bool calculationIsRunning_;

    // attributes for doBracketMatching
    bool doBracketMatchingIsRunning_;
    bool inputIsMarked_; // remember if some bracket etc. is marked
    int  lastCursorPos_; // remember last cursor position before textChanged

    QRect iniRect_;			// initial geometry at application start
    int fontsize_;			// font point size for all widgets
    QString bgcolor_;		// background color for read-only style widgets

    QStringList inputs_;	// input history
    int idxInput_;			// current index of input history
    QString lastResult;     //remember last Kalkulon result ("out") for copying to clipboard

// Actions
    QAction *openHelpAction_;
    QAction *clearAllAction_;
    QAction *openOptionsDlgAction_;
    QAction *aboutAction_;
    QAction *aboutQtAction_;
// Menues
    QMenu *mainMenu_;

//shortcuts
    QShortcut *shortcutCopyLastResult;

// Tray Icon
    QSystemTrayIcon *trayIcon;

// private consts
    static const int MAXPOINTSIZE = 18;
    static const int MINPOINTSIZE = 4;

// Kalkulon core
    Kalkulon::CArithParser		parser_;
    Kalkulon::CRPNStackEval		evaluator_;

    MyCallback* mycallback_;
};


class MyCallback:public Kalkulon::Callback
{
    public:
        MyCallback(CKalkulonDlg* pDlg ):pDlg_(pDlg) {};
        void print(const char * str, bool lf) {pDlg_->print(str, true, lf); };
        void processEvents() {qApp->processEvents(); };
    private:
        CKalkulonDlg* pDlg_;
};


#endif //KALKULONDLG_H
