#include "wx/wxprec.h"

// APPLICATION
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

// FENETRE
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

    // EVENEMENTS
    void OnOpen(wxCommandEvent& event);
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

    virtual void OnSize(wxSizeEvent& event);

private:
    // EVENEMENTS
    DECLARE_EVENT_TABLE()
    wxStaticBitmap *bitmap;
    wxScrolledWindow *scrollPane;
};

// CONSTANTES POUR EVENEMENTS
enum
{
    // menu items
    Menu_Open,
    Menu_Quit,
    Menu_About
};

// TABLE D'EVENEMENTS
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(Menu_Open,  MyFrame::OnOpen)
    EVT_MENU(Menu_Quit,  MyFrame::OnQuit)
    EVT_MENU(Menu_About, MyFrame::OnAbout)
END_EVENT_TABLE()

// CREATION DE L'APPLICATION
IMPLEMENT_APP(MyApp)

// PROGRAMME
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame("Bitmap Viewer",
                                 wxDefaultPosition, wxSize(450, 340));

    frame->CenterOnScreen();
    frame->Show(TRUE);

    return TRUE;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
       : wxFrame((wxFrame *) NULL, -1, title, pos, size)
{
    //SetIcon(wxIcon(wxString("main.ico"), wxBITMAP_TYPE_ICO));
    SetIcon(wxIcon("mainIcon"));

    wxMenu *menuHelp = new wxMenu();
    menuHelp->Append(Menu_About, "&About...\tCtrl-A", ";p");

    wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
    menuFile->Append(Menu_Open, "&Open...\tCtrl-O", "Open a bitmap picture");
    menuFile->Append(Menu_Quit, "E&xit\tAlt-X", "Quit this program");

    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");

    SetMenuBar(menuBar);

    CreateStatusBar(1);
    SetStatusText("<No Bitmap>");

    scrollPane = new wxScrolledWindow(this,
                                      -1, wxDefaultPosition,
                                      wxDefaultSize, wxHSCROLL | wxVSCROLL);
    bitmap = new wxStaticBitmap(scrollPane, -1, wxBitmap("mainBitmap"));
}


// GESTIONNAIRES EVENEMENTS
void MyFrame::OnSize(wxSizeEvent& event)
{
  bitmap->Move(wxPoint(0, 0));
}

void MyFrame::OnOpen(wxCommandEvent& event)
{
    wxFileDialog *dialog = new wxFileDialog(this, "Choose a bitmap", "", "",
                                            "BMP files (*.bmp)|*.bmp", wxOPEN);
    if (dialog->ShowModal() == wxID_OK)
    {
      SetStatusText(dialog->GetFilename());
      wxBitmap bmp(dialog->GetFilename(), wxBITMAP_TYPE_BMP);
      scrollPane->SetScrollbars(5, 5, bmp.GetWidth() / 5, bmp.GetHeight() / 5);
      bitmap->SetBitmap(bmp);
    }
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
    Close(TRUE);
}

void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg;
    msg << "Bitmap Viewer\n" << "(C)2002 Romain Guy - 05/16/2002";

    wxMessageBox(msg, "About...", wxOK | wxICON_INFORMATION, this);
}
