/////////////////////////////////////////////////////////////////////////////
// Name:        extcmdprogresspanel.h
// Purpose:     wxExtCmdProgressPanel definition
// Author:      Julian Smart, Francesco Montorsi
// Modified by:
// Created:     15/06/2006 21:21:31
// RCS-ID:      $Id: extcmdprogresspanel.h,v 1.4 2007/01/28 15:56:10 frm Exp $
// Copyright:   (c) 2004 Francesco Montorsi
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////

#ifndef _PMEXTCMDPROGRESSPANEL_H_
#define _PMEXTCMDPROGRESSPANEL_H_

/*!
 * Includes
 */

#include "wxp/package.h"
#include "wx/process.h"
#include "wx/timer.h"
#include "wx/gauge.h"

#include "guipm/progresspanel.h"


/*!
 * Control identifiers
 */

////@begin control identifiers
#define SYMBOL_WXEXTCMDPROGRESSPANEL_STYLE 0
#define SYMBOL_WXEXTCMDPROGRESSPANEL_TITLE _("Dialog")
#define SYMBOL_WXEXTCMDPROGRESSPANEL_IDNAME wxID_ANY
#define SYMBOL_WXEXTCMDPROGRESSPANEL_SIZE wxSize(400, 300)
#define SYMBOL_WXEXTCMDPROGRESSPANEL_POSITION wxDefaultPosition
#define ID_EXTCMD_COMMANDS 10000
#define ID_EXTCMD_GAUGE 10001
#define ID_EXTCMD_NOTEBOOK 10003
#define ID_EXTCMD_OUTPUT 10006
#define ID_EXTCMD_OUTPUTERR 10002
////@end control identifiers

#define SYMBOL_WXEXTCMDPROGRESSPANEL_NAME wxT("Build")
#define ID_EXTCMD_PROCESS  10015
#define ID_EXTCMD_CURRPROCESS_PREFIX  wxT("=> ")


// ----------------------------------------------------------------------------
// wxExtCmdProgressPanel
// ----------------------------------------------------------------------------

class wxExtCmdProgressPanel: public wxProgressPanel
{
    DECLARE_DYNAMIC_CLASS( wxExtCmdProgressPanel )
    DECLARE_EVENT_TABLE()

public:
    /// Constructors
    wxExtCmdProgressPanel( );
    wxExtCmdProgressPanel( wxWindow* parent,
                           wxWindowID id = SYMBOL_WXEXTCMDPROGRESSPANEL_IDNAME,
                           const wxPoint& pos = SYMBOL_WXEXTCMDPROGRESSPANEL_POSITION,
                           const wxSize& size = SYMBOL_WXEXTCMDPROGRESSPANEL_SIZE,
                           long style = SYMBOL_WXEXTCMDPROGRESSPANEL_STYLE,
                           const wxString &name = SYMBOL_WXEXTCMDPROGRESSPANEL_NAME);

    /// Creation
    bool Create( wxWindow* parent, wxWindowID id = SYMBOL_WXEXTCMDPROGRESSPANEL_IDNAME,
                 const wxPoint& pos = SYMBOL_WXEXTCMDPROGRESSPANEL_POSITION,
                 const wxSize& size = SYMBOL_WXEXTCMDPROGRESSPANEL_SIZE,
                 long style = SYMBOL_WXEXTCMDPROGRESSPANEL_STYLE,
                 const wxString &name = SYMBOL_WXEXTCMDPROGRESSPANEL_NAME);

    /// Creates the controls and sizers
    void CreateControls();


    virtual ~wxExtCmdProgressPanel()
    {
        // if not stopped, stop now
        Stop();
    }


public:     // event handlers

    void OnIdle(wxIdleEvent &ev);
    void OnTimer(wxTimerEvent& event);
    void OnProcessTerminate(wxProcessEvent &ev);
    void OnUpdateUI(wxUpdateUIEvent &);

public:

    void SetStage(wxPackageBuildSystemStage stage)
        { m_stage=stage; }

    void SetPackage(const wxPackage &pkg);

    void Start();
    void Stop();

    bool IsProcessing() const
        { return m_timerIdleWakeUp.IsRunning() || m_currProcess != NULL; }

    wxString GetLogData() const;
    wxString GetLogLabel() const;

    bool IsSuccessful() const
        { return m_nExitCode == 0; }

    void ClearControls();

protected:      // internal utilities

    void SelectCurrProcess();
    void DeselectCurrProcess();

    void OnProcessStart();

    void UpdateCachedCommands();

    wxString GetCmdString(size_t n) const;
    wxString GetCmdPath(size_t n) const;

protected:      // controls

    wxListBox* m_pCommandList;
    wxTextCtrl* m_pOutput;
    wxTextCtrl* m_pOutputErr;
    wxGauge *m_pGauge;

protected:      // other variables

    wxPackage m_pkg;
    wxPackageBuildSystemStage m_stage;

    //! The cached array of *valid* commands for the m_pkg already substituted.
    wxPackageCommandArray m_cmd;

    wxTimer m_timerIdleWakeUp;
    wxDateTime m_start;

protected:      // running process

    wxProcess *m_currProcess;
    int m_nCurrProcess;
    int m_pid;
    int m_nExitCode;
};

#endif
    // _PMEXTCMDPROGRESSPANEL_H_
