/////////////////////////////////////////////////////////////////////////////
// Name:        commandlistbox.cpp
// Purpose:     wxCommandListBox
// Author:      Francesco Montorsi
// Modified by:
// Created:     15/06/2006 21:21:22
// RCS-ID:      $Id: commandlistbox.cpp,v 1.2 2007/01/23 23:05:41 frm Exp $
// Copyright:   (c) 2006 Francesco Montorsi
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////


// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "guicmn/commandlistbox.h"



// ----------------------------------------------------------------------------
// wxCommandListBox
// ----------------------------------------------------------------------------
/*
wxColour g_formatColours[wxPCF_MAX] =
{
    wxColour(128, 128, 128),        // borland
    wxColour(220, 220, 220),        // msvc
    wxColour(250, 200, 200)         // watcom
            
    wxPCF_WATCOM = 4,
    wxPCF_MINGW = 8,
    wxPCF_MSYS = 16,

    // unix main formats
    wxPCF_GNUMAKEFILE = 32,
    wxPCF_AUTOCONF = 64,

            wxPCF_MAX = 7*
}; */

wxCommandListBox::wxCommandListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
                                   const wxSize& size, long style, const wxString& name)
{
    // create base class
    wxHtmlListBox::Create(parent, id, pos, size, style|wxSUNKEN_BORDER, name);
    //SetSelectionBackground(wxColour(wxT("DARK GREEN")));
    SetMargins(0, 0);

    m_buildsys = wxPBST_INVALID;
    m_formats = 0;
}

wxString wxCommandListBox::OnGetItem(size_t n) const
{
    size_t idx = m_index[n];
    long fmt = m_arr[idx].GetCondition().GetFormats() & m_formats;
    wxString formats = m_formats == 0 ? 
        wxPackageCompilerFormatList2String(m_arr[idx].GetCondition().GetFormats()) :
        wxPackageCompilerFormatList2String(fmt);

    wxString path = m_arr[idx].GetWorkingPath();
    if (path.IsEmpty())
        path = wxT("package root");

    wxColour clr;
    int c = ( fmt * 1000 ) % 255;
    if (m_formats == 0)
        clr = *wxWHITE;     // filter by format is disabled
    else
        clr.Set(c*3, c*2, c*3);   // FIXME: looks bad !!

    return wxString::Format(
            wxT("<table><tr><td width='100%%' bgcolor='%s'>Command: %s<br>In path: %s")
            wxT("<div align='right'>(<tt>%s</tt>)</div></td></tr></table>"),
            clr.GetAsString(wxC2S_HTML_SYNTAX).c_str(),
            m_arr[idx].GetCommand().c_str(),
            m_arr[idx].GetWorkingPath().c_str(),
            formats.c_str());
}

wxColour wxCommandListBox::GetSelectedTextBgColour(const wxColour& colBg, size_t n) const
{
    size_t idx = m_index[n];
    long fmt = m_arr[idx].GetCondition().GetFormats() & m_formats;

    int c = ( fmt * 1000 ) % 255;
    if (m_formats == 0)
        return *wxWHITE;     // filter by format is disabled
    else
        return wxColour(c*3, c*2, c*3);   // FIXME: looks bad !!
}

wxColour wxCommandListBox::GetSelectedTextColour(const wxColour& colBg, size_t n) const
{
    return *wxBLACK;
}

bool wxCommandListBox::TransferDataToWindow()
{
    if (!wxHtmlListBox::TransferDataToWindow())
        return false;

    m_index.Clear();
    for (size_t i=0; i<m_arr.GetCount(); i++)
    {
        // NB: here we won't do the check on the platforms supported by the
        //     command as we want to list all commands, even for platforms != current.
        if ((m_formats != 0 && !m_arr[i].GetCondition().IsValidForCompilers(m_formats)) ||
            (m_buildsys != wxPBST_INVALID && !m_arr[i].GetCondition().IsValidForBuildSys(m_buildsys)))
            continue;   // skip this

        m_index.Add(i);
    }

    SetItemCount(m_index.GetCount());
    SetSelection(-1);
    RefreshAll();

    return true;
}

void wxCommandListBox::DeleteSelected()
{
    size_t idx = m_index[GetSelection()];
    m_arr.RemoveAt(idx);
    TransferDataToWindow();
}

