// @topic T02330 GUI Window Demo
// @brief CAppWindow class implementation

#include "CAppWindow.h"
#include "train.h"

CAppWindow::CAppWindow( Train& train )
    :
    Fl_Double_Window( 451, 110 ),
    m_train( train ),
    vect_original_track( BOX_ARRAY_SIZE, FL_GREEN ),
    vect_current_display( BOX_ARRAY_SIZE, FL_GREEN )
{
    // The following code was origianlly generated by FLTK Fluid:
    Fl_Button* o = new Fl_Button(40, 60, 75, 25, "Animate");
    o->callback((Fl_Callback*)cb_btn_animate, this);

    int idx = 0;
    for ( idx = 0; idx < BOX_ARRAY_SIZE; ++idx ) {
        out_box[ idx ] = new Fl_Output(
            OUTBOX_TOP_LEFT_X + idx * OUTBOX_WIDTH,
            OUTBOX_TOP_LEFT_Y,
            OUTBOX_WIDTH,
            OUTBOX_HEIGHT
            );
            out_box[ idx ]->color( vect_current_display[ idx ] );
    }
    end();
    resizable( this );
}

// invoked by the operating system when the user clicks the button
void CAppWindow::cb_btn_animate(Fl_Widget* btn, void* pwin)
{
    CAppWindow* pAppWindow = static_cast< CAppWindow*>( pwin );
    pAppWindow->btn_animate();
}

// button re-entry point
void CAppWindow::btn_animate()
{
    // Let business objects perform animation steps:
    m_train.move( vect_original_track, vect_current_display );

    // update colors of output boxes:
    int idx = 0;
    for ( idx = 0; idx < BOX_ARRAY_SIZE; ++idx ) {
        out_box[ idx ]->color( vect_current_display[ idx ] );
    }

    // redraw the window:
    Fl::redraw();
}