/*
 * File:	panel.cc
 * Purpose:	Panel/panel item tests
 * Author:	Julian Smart
 * Created:	1995
 * Updated:	
 * Copyright:	(c) 1995, AIAI, University of Edinburgh
 */

static const char sccsid[] = "%W% %G%";

#pragma hdrfile "c:\c\wx\develop\src\msw\wx.sym"

#include "wx.h"
#pragma hdrstop
#include "panel.h"

MyFrame   *frame = NULL;
wxFrame  *subframe = NULL;
wxMenuBar *menu_bar = NULL;
wxItem *item1 = NULL;
wxItem *item2 = NULL;

// This statement initialises the whole application
MyApp     myApp;

#if USE_GAUGE
int gaugePos = 0;
wxGauge *gauge1 = NULL;
#endif

wxButton *bitmapButton = NULL;
wxMessage *bitmapMessage = NULL;
wxBitmap *currentBitmap = NULL;
wxBitmap *backgroundBitmap = NULL;

// The `main program' equivalent, creating the windows and returning the
// main frame
wxFrame *MyApp::OnInit(void)
{
  backgroundBitmap = new wxBitmap("marble.bmp", wxBITMAP_TYPE_BMP);
  
  // Create the main frame window
  frame = new MyFrame(NULL, "Panel test", 0, 0, 430, 340, wxSDI|wxDEFAULT_FRAME);

  // Give it an icon
#ifdef wx_msw
  wxIcon *test_icon = new wxIcon("aiai_icn");
  frame->SetIcon(test_icon);
#endif

  // Make a menubar
  wxMenu *file_menu = new wxMenu;

  file_menu->Append(PANELS_LOAD_BITMAP, "&Load background bitmap (.BMP)");
  file_menu->Append(PANELS_QUIT, "&Quit");


  wxMenu *help_menu = new wxMenu;
  help_menu->Append(PANELS_ABOUT, "&About");

  menu_bar = new wxMenuBar;

  menu_bar->Append(file_menu, "&File");
  menu_bar->Append(help_menu, "&Help");

  // Associate the menu bar with the frame
  frame->SetMenuBar(menu_bar);

  // Make a panel
  frame->panel = new MyPanel(frame, 0, 0, 400, 400, wxUSER_COLOURS);

  frame->panel->SetLabelPosition(wxVERTICAL);
  frame->panel->SetBackground(wxWHITE_BRUSH);

  wxColour col1("RED");  
  wxColour col2("CYAN");
#if USE_GAUGE
  frame->panel->NewLine();
  frame->panel->SetLabelPosition(wxVERTICAL);
  gauge1 = new wxGauge(frame->panel, NULL, 60, 80, 120, 250, 50, wxHORIZONTAL);
  gauge1->SetShadowWidth(3);
  gauge1->SetButtonColour(&col1);
  gauge1->SetBackgroundColour(&col2);
  frame->panel->NewLine();
#endif
  wxButton *but1 = new wxButton(frame->panel, (wxFunction)&button_proc, "Continue", 50, 205, 100, 50);
  wxButton *but2 = new wxButton(frame->panel, (wxFunction)&button_proc, "Exit", 250, 205, 100, 50);

  frame->Centre(wxBOTH);
  frame->Show(TRUE);

  // Return the main frame window
  return frame;
}

// Define my frame constructor
MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h, long style):
  wxFrame(frame, title, x, y, w, h, style)
{
  panel = NULL;
}

// Intercept menu commands
void MyFrame::OnMenuCommand(int id)
{
  switch (id)
  {
    case PANELS_LOAD_BITMAP:
    {
      char *s = wxFileSelector("Load bitmap", NULL, NULL, NULL, "*.*");
      if (s)
      {
        char *f = copystring(s);
        wxBitmap *bitmap = new wxBitmap(f, wxBITMAP_TYPE_BMP);
        delete[] f;
        if (bitmap)
	{
          if (backgroundBitmap)
            delete backgroundBitmap;
          backgroundBitmap = bitmap;
          panel->Refresh();
        }
      }
     
      break;
    }
    case PANELS_QUIT:
    {
      OnClose();
      delete this;
      break;
    }
    case PANELS_ABOUT:
    {
      (void)wxMessageBox("Panel Test\nAuthor: Julian Smart J.Smart@ed.ac.uk\nAIAI (c) 1995", "About Panel Test");
      break;
    }
  }
}
// Define the behaviour for the frame closing
// - must delete all frames except for the main one.
Bool MyFrame::OnClose(void)
{
  return TRUE;
}

void MyFrame::OnSize(int w, int h)
{
  wxFrame::OnSize(w, h);
}

void quit_proc(wxButton& but, wxEvent& event)
{
  wxDialogBox *box = (wxDialogBox *)but.GetParent();
  box->Show(FALSE);
  delete box;
}

// Gets some user input, and sets the status line
void button_proc(wxButton& but, wxEvent& event)
{
#if USE_GAUGE
  gaugePos += 5;
  if (gaugePos > 50)
    gaugePos = 0;
  gauge1->SetValue(gaugePos);
#endif
}

// Put some text into the text window
void list_proc(wxListBox& list, wxEvent& event)
{
}

void GenericOk(wxButton& but, wxEvent& event)
{
  wxDialogBox *dialog = (wxDialogBox *)but.GetParent();

  dialog->Show(FALSE);
}

MyPanel::MyPanel(wxFrame *frame, int x, int y, int w, int h, long style):
  wxPanel(frame, x, y, w, h, style)
{
}

void MyPanel::OnPaint(void)
{
  wxDC *dc = GetDC();
  if (!backgroundBitmap)
    return;

  wxMemoryDC memDC;

  memDC.SelectObject(backgroundBitmap);
  if (backgroundBitmap->GetColourMap())
  {
    memDC.SetColourMap(backgroundBitmap->GetColourMap());
    dc->SetColourMap(backgroundBitmap->GetColourMap());
  }
    
  dc->Blit(0, 0, backgroundBitmap->GetWidth(), backgroundBitmap->GetHeight(), &memDC, 0, 0);
  memDC.SelectObject(NULL);

  // Paint any static items
  wxPanel::OnPaint();
}
