#pragma warning(disable: 4819)

#include "monitor.h"
#include "monitor.xpm"

// global variables
wxString exePath; // MyTcl ½ÇÇà ÆÐ½º
wxString fontFace;
unsigned int fontSize;

class MonitorApp : public wxApp
{
public:
    bool OnInit();
};

DECLARE_APP(MonitorApp)
IMPLEMENT_APP(MonitorApp)

bool MonitorApp::OnInit()
{
	wxMDIParentFrame *mainFrame = new MonitorFrame(NULL,
		 wxID_ANY,
		 wxT("Simple Monitor Program"),
		 wxDefaultPosition,
		 wxSize(800, 600),
		 wxDEFAULT_FRAME_STYLE | wxAUI_GRADIENT_NONE);
    SetTopWindow(mainFrame);
    mainFrame->Show();

    return true;
}

BEGIN_EVENT_TABLE(MonitorFrame, wxMDIParentFrame)
	//EVT_CHILD_FOCUS(MonitorFrame::OnChildFocus)

    EVT_MENU(wxID_ABOUT, MonitorFrame::OnAbout)

    EVT_MENU(wxID_EXIT, MonitorFrame::OnExit)
    EVT_CLOSE(MonitorFrame::OnCloseApp)

	EVT_UPDATE_UI(wxID_ANY, MonitorFrame::OnUpdateUI)
    
	EVT_SIZE(MonitorFrame::OnSize)
    EVT_IDLE(MonitorFrame::OnIdle)
END_EVENT_TABLE()

MonitorFrame::MonitorFrame(wxWindow* parent,
	 wxWindowID id,
	 const wxString& title,
	 const wxPoint& pos,
	 const wxSize& size,
	 long style)
	: wxMDIParentFrame(parent, id, title, pos, size, style)
{
	b_statusBarInit = false;

    m_mgr.SetManagedWindow(this);

	wxInitAllImageHandlers();
	
	{
		wxChar _drv[MAX_PATH], _path[MAX_PATH];
		wxChar fname[MAX_PATH], ext[MAX_PATH], temp[MAX_PATH];

		GetModuleFileName(NULL, temp, 1024);
		_splitpath(temp, _drv, _path, fname, ext);
		// c:/aaaa/ --> c:/aaaa
		wxFileName* path = new wxFileName(wxString::Format("%s%s", _drv, _path));
		exePath = path->GetPath();
		exePath.Replace(wxT("\\"), wxT("/"), true);
	}

	SetFont(GetDockArt()->GetFont(wxAUI_DOCKART_CAPTION_FONT));
	GetDockArt()->SetMetric(wxAUI_DOCKART_GRADIENT_TYPE, 
			wxAUI_GRADIENT_NONE);
	//GetDockArt()->Update();

	unsigned int flag = wxAUI_MGR_ALLOW_ACTIVE_PANE;
	m_mgr.SetFlags(m_mgr.GetFlags() ^ flag);

    //set frame icon
    SetIcon(wxIcon(monitor_xpm));

    // set up default notebook style
    m_notebook_style = wxAUI_NB_DEFAULT_STYLE | 
		wxNO_BORDER | wxAUI_NB_TAB_SPLIT;

    // add menu
    m_menuBar = new wxMenuBar(wxMB_DOCKABLE);
    
	wxMenu* menu = new wxMenu;
	
	wxMenuItem *item;
	
	// -------------
	item = new wxMenuItem(menu, wxID_EXIT, wxT("Exit"));
	menu->Append(item);

    m_menuBar->Append(menu, _("File"));

	// -------------
	menu = new wxMenu;

    menu->Append(wxID_ABOUT, _("About...\tF12"));

    m_menuBar->Append(menu, _("Help"));

    SetMenuBar(m_menuBar);

	// ---------------------
	m_statusBar = new wxStatusBar(this, -1);
	m_statusBar->SetFieldsCount(2);
	int width[2] = { -1, 120 };
	m_statusBar->SetStatusWidths(0, width);
	
	SetStatusBar(m_statusBar);
	
    SetMinSize(wxSize(400,300));

	b_statusBarInit = true;

	// add toolbar
    m_fileToolBar = new wxAuiToolBar(this, 
		wxID_ANY, wxDefaultPosition, wxDefaultSize,
		wxTB_FLAT | wxTB_NODIVIDER);
    m_fileToolBar->SetToolBitmapSize(wxSize(16,16));

    m_fileToolBar->AddTool(wxID_ABOUT, 
			wxT(""), GET_BITMAP("info"), wxT("About"));
    m_fileToolBar->Realize();
    
    m_mgr.AddPane(m_fileToolBar, wxAuiPaneInfo().
		  Name(wxT("file toolbar")).
		  Caption(wxT("File")).
		  ToolbarPane().Top().Row(0).
		  LeftDockable(false).RightDockable(false));
	m_mgr.Update();
    
	// font
	wxFont font = GetDockArt()->GetFont(wxAUI_DOCKART_CAPTION_FONT);
	fontFace = font.GetFaceName();
	fontSize = font.GetPointSize();

	// http://forums.wxwidgets.org/viewtopic.php?t=6556&highlight=sdl
    m_sdl = new SDLFrame(this, this,
		wxID_ANY, wxT("Monitor #1"),
		wxDefaultPosition, wxSize(WIDTH, HEIGHT),
		wxDEFAULT_FRAME_STYLE | 
		wxRESIZE_BORDER | wxNO_FULL_REPAINT_ON_RESIZE);
	//m_sdl->SetClientSize(WIDTH, HEIGHT);
	m_sdl->Init(WIDTH, HEIGHT);
	
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		// error
	}
}

MonitorFrame::~MonitorFrame()
{
	//Sleep(100);
    m_mgr.UnInit();
}

void MonitorFrame::Example()
{
	//Uint32 *raw_pixels;
	//raw_pixels = (Uint32 *) m_sdl->m_panel->GetSurface()->pixels;

	SDL_Surface *surface;
	surface = m_sdl->GetSurface();
	
	int noise;
	int carry;
	int counter;
	int seed = 0x12345;
	unsigned int *pixels = (unsigned int *)malloc(
			HEIGHT * WIDTH * sizeof(unsigned int));
	while(1)
	{
		for (int counter = 0; counter < WIDTH*HEIGHT; counter++)
		{
			noise = seed;
			noise >>= 3;
			noise ^= seed;
			carry = noise & 1;
			noise >>= 1;
			seed >>= 1;
			seed |= (carry << 30);
			noise &= 0xFF;
			pixels[counter] = (noise << 16) | (noise << 8) | noise;
		}

		m_sdl->Lock();
		m_sdl->Update(pixels);
		
		Refresh();
		Update();
		wxYield();
		
		m_sdl->Unlock();
	}

	free(pixels);
}

void MonitorFrame::OnIdle(wxIdleEvent &) 
{
	Example();
}

void MonitorFrame::OnSize(wxSizeEvent& event)
{
	if ( b_statusBarInit )
	{
		wxRect rect;
		m_statusBar->GetFieldRect(5, rect);
		wxPoint pos;
		pos.x = rect.x+20;
		pos.y = rect.y;
		//m_aniCtrl->SetPosition(pos);
	}
}

wxAuiDockArt* MonitorFrame::GetDockArt()
{
    return m_mgr.GetArtProvider();
}

void MonitorFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event))
{
	//m_mgr.Update();
	//Example();
}

void MonitorFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
	wxMessageBox("a");
}

void MonitorFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
	m_mgr.UnInit();
	exit(0);
}

void MonitorFrame::OnCloseApp(wxCloseEvent& WXUNUSED(event))
{
	m_mgr.UnInit();
	exit(0);
}
