/*
 * Copyright (C) 2006 Wim Dumon, Koen Deforche
 *
 * See the LICENSE file for terms of use.
 */

#include <iostream>
#include <vector>
#include <string>

#include <boost/format.hpp>

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WDialog>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WGridLayout>
#include <Wt/WRadioButton>
#include <Wt/WButtonGroup>
#include <Wt/WTextArea>
#include <Wt/WHBoxLayout>
#include <Wt/WVBoxLayout>
#include <Wt/WTabWidget>
#include <Wt/WSignalMapper>



using namespace Wt;

/*
 * test application using tabs inside a dialog
 */



    
class DialogDemo : public WApplication
{
public:
    DialogDemo(const WEnvironment& env);
    //    bool readConfiguration(std::string filename);
    
    ~DialogDemo();

private:
    WGridLayout *ppiGrid_;
    WGridLayout *rhiGrid_;
    WGridLayout *surGrid_;

    WHBoxLayout *hbox1_;
    WHBoxLayout *hbox2_;
    WVBoxLayout *vbox1_;

    
    WContainerWidget *outer_;
    WContainerWidget *scanCW_;
    WContainerWidget *hbox2CW_;

    WContainerWidget *ppiContainer_;
    WContainerWidget *rhiContainer_;
    WContainerWidget *surContainer_;
    WLineEdit *nameEdit_;
    WButtonGroup *ppiButtonGroup_;
    WButtonGroup *rhiButtonGroup_;
    WButtonGroup *surButtonGroup_;
    WText *showTip_;

    WContainerWidget *ShowTab(char *title);
    void ShowDialog();
    

};


/*
 * The env argument contains information about the new session, and
 * the initial request. It must be passed to the WApplication
 * constructor so it is typically also an argument for your custom
 * application constructor.
 */
DialogDemo::DialogDemo(const WEnvironment& env) :
        WApplication(env) 
{
    setTitle("Tab Dialog Test ");                  // application title


    outer_ = new WContainerWidget(root());
    vbox1_ = new WVBoxLayout();
    WPushButton *dtest = new WPushButton("push me");
    dtest->clicked().connect(SLOT(this, DialogDemo::ShowDialog));
    
    vbox1_->addWidget(dtest, 0, Wt::AlignTop);
    
    outer_->setLayout(vbox1_, AlignTop | AlignJustify);
    

}
void DialogDemo::ShowDialog()
{
    WDialog dialog;
    dialog.setWindowTitle("Dialog Tab Test");
    
    WVBoxLayout *contentsLayout = new WVBoxLayout();
    dialog.contents()->setLayout(contentsLayout);
    dialog.contents()->resize(WLength(), 500);
    
    WTabWidget *tw = new WTabWidget();
    tw->setStyleClass("tabwidget");
    tw->addTab(ShowTab("Equal Angles"), "Equal Angles", WTabWidget::PreLoading);
    tw->addTab(ShowTab("Equal Resolution"), "Equal Resolution", WTabWidget::PreLoading);
    tw->addTab(ShowTab("Rotate"), "Rotate", WTabWidget::PreLoading);

    contentsLayout->addWidget(tw, 0, AlignLeft | AlignTop);

    if ( dialog.exec() == WDialog::Accepted) {
        std::cerr << "dialog accepted\n";
    }
    
}

DialogDemo::~DialogDemo() 
{
}










WContainerWidget *DialogDemo::ShowTab(char *title)
{
    WContainerWidget *container = new WContainerWidget(NULL);
    container->resize(WLength(), 400);
    WVBoxLayout *contentsLayout = new WVBoxLayout(container);
    WGridLayout *grid = new WGridLayout();
    
    contentsLayout->addLayout(grid,1, Wt::AlignTop);
    WText *titlew = new WText(title);
    grid->addWidget(titlew, 0, 2, 1, 2);
    


    return container;
}





WApplication *createApplication(const WEnvironment& env)
{
    /*
     * You could read information from the environment to decide whether
     * the user has permission to start a new application
     */
    DialogDemo *r = new DialogDemo(env);
    //    r->readConfiguration("/tmp/radar_ctl.txt");
    return r;
}

int main(int argc, char **argv)
{
    /*
     * Your main method may set up some shared resources, but should then
     * start the server application (FastCGI or httpd) that starts listening
     * for requests, and handles all of the application life cycles.
     *
     * The last argument to WRun specifies the function that will instantiate
     * new application objects. That function is executed when a new user surfs
     * to the Wt application, and after the library has negotiated browser
     * support. The function should return a newly instantiated application
     * object.
     */
    return WRun(argc, argv, &createApplication);
}
 
