Logo Search packages:      
Sourcecode: codeblocks version File versions  Download package

wxsresourcetree.cpp

/*
* This file is part of wxSmith plugin for Code::Blocks Studio
* Copyright (C) 2006-2007  Bartlomiej Swiecki
*
* wxSmith is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wxSmith is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
*
* $Revision: 4850 $
* $Id: wxsresourcetree.cpp 4850 2008-01-29 21:45:49Z byo $
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/contrib/wxSmith/wxsresourcetree.cpp $
*/

#include "wxsresourcetree.h"
#include "wxsresourcetreeitemdata.h"

#include <wx/image.h>
#include <wx/imaglist.h>
#include <wx/intl.h>

#include "configmanager.h"
#include "globals.h"

namespace
{
    class wxsResourceTreeProjectData: public wxsResourceTreeItemData
    {
        public:
            wxsResourceTreeProjectData(wxsProject* Project): m_Project(Project) {}
            wxsProject* m_Project;
            virtual void OnRightClick()
            {

            }

    };
}

wxsResourceTree* wxsResourceTree::m_Singleton = 0;
int wxsResourceTree::m_RootImageId = LoadImage(_T("/images/wxsmith/wxSmith16.png"));
int wxsResourceTree::m_ProjectImageId = LoadImage(_T("/images/codeblocks.png"));
int wxsResourceTree::m_ExternalImageId = LoadImage(_T("/images/wxsmith/deletewidget16.png"));


BEGIN_EVENT_TABLE(wxsResourceTree,wxTreeCtrl)
    EVT_TREE_SEL_CHANGED(wxID_ANY,wxsResourceTree::OnSelect)
    EVT_TREE_ITEM_RIGHT_CLICK(wxID_ANY,wxsResourceTree::OnRightClick)
    EVT_MENU(wxID_ANY,wxsResourceTree::OnPopupMenu)
END_EVENT_TABLE()


00061 wxsResourceTree::wxsResourceTree(wxWindow* Parent)
    : wxTreeCtrl(Parent,-1)
    , m_IsExt(false)
    , m_BlockCount(0)
    , m_Data(0)
{
    m_Singleton = this;
    SetImageList(&GetGlobalImageList());
    Expand(AddRoot(_("Resources"),m_RootImageId));
}

00072 wxsResourceTree::~wxsResourceTree()
{
    if ( m_Singleton == this ) m_Singleton = 0;
}

00077 wxsResourceItemId wxsResourceTree::NewProjectItem(const wxString& ProjectTitle,wxsProject* Project)
{
    wxsResourceItemId Id;
    if ( !m_IsExt )
    {
        Id = AppendItem(GetRootItem(),
            ProjectTitle,m_ProjectImageId,m_ProjectImageId,
            new wxsResourceTreeProjectData(Project));
    }
    else
    {
        Id = InsertItem(GetRootItem(),GetChildrenCount(GetRootItem(),false),
            ProjectTitle,m_ProjectImageId,m_ProjectImageId,
            new wxsResourceTreeProjectData(Project));
    }

    Expand(GetRootItem());
    return Id;
}

00097 wxsResourceItemId wxsResourceTree::ExternalResourcesId()
{
    if ( !m_IsExt )
    {
        m_ExtId = AppendItem(GetRootItem(),
            _("External resources"),m_ExternalImageId,m_ExternalImageId,
            0);
        m_IsExt = true;
    }
    return m_ExtId;
}

00109 void wxsResourceTree::DeleteExternalResourcesId()
{
    if ( m_IsExt )
    {
        Delete(m_ExtId);
        m_IsExt = false;
    }
}

void wxsResourceTree::OnSelect(wxTreeEvent& event)
{
    if ( m_BlockCount ) return;
    wxsResourceTreeItemData* Data = (wxsResourceTreeItemData*)GetItemData(event.GetItem());
    if ( Data ) Data->OnSelect();
}

void wxsResourceTree::OnRightClick(wxTreeEvent& event)
{
    wxsResourceTreeItemData* Data = (wxsResourceTreeItemData*)GetItemData(event.GetItem());
    if ( Data ) Data->OnRightClick();
}

00131 wxImageList& wxsResourceTree::GetGlobalImageList()
{
    static wxImageList List(16,16);
    static bool FirstTime = true;
    if ( FirstTime )
    {
        // Adding some zero-index image
        List.Add(wxBitmap(wxImage(16,16)),*wxBLACK);

        FirstTime = false;
    }
    return List;
}

00145 int wxsResourceTree::LoadImage(const wxString& FileName)
{
    wxBitmap Bmp(cbLoadBitmap(ConfigManager::GetDataFolder()+_T("/")+FileName,wxBITMAP_TYPE_ANY));
    return InsertImage(Bmp);
}

00151 int wxsResourceTree::InsertImage(const wxBitmap& Bitmap)
{
    if ( !Bitmap.Ok() )
    {
        return 0;
    }

    int Index = 0;
    do
    {
        if ( GetFreedList().IsEmpty() )
        {
            return GetGlobalImageList().Add(Bitmap);
        }
        Index = GetFreedList().Last();
        GetFreedList().RemoveAt(GetFreedList().Count()-1);
    }
    while ( Index<=0 || Index>=GetGlobalImageList().GetImageCount() );

    if ( GetGlobalImageList().Replace(Index,Bitmap) )
    {
        return Index;
    }

    return GetGlobalImageList().Add(Bitmap);
}

wxArrayInt& wxsResourceTree::GetFreedList()
{
    static wxArrayInt List;
    return List;
}

00184 void wxsResourceTree::FreeImage(int Index)
{
    GetFreedList().Add(Index);
}

00189 void wxsResourceTree::BlockSelect()
{
    m_BlockCount++;
}

00194 void wxsResourceTree::UnblockSelect()
{
    m_BlockCount--;
}

void wxsResourceTree::PopupMenu(wxMenu* Menu,wxsResourceTreeItemData* ItemData)
{
    m_Data = ItemData;
    wxWindow::PopupMenu(Menu,wxDefaultPosition);
}

void wxsResourceTree::InvalidateItemData(wxsResourceTreeItemData* ItemData)
{
    if ( m_Data == ItemData )
    {
        m_Data = 0;
    }
}

void wxsResourceTree::OnPopupMenu(wxCommandEvent& event)
{
    if ( m_Data )
    {
        if ( m_Data->OnPopup(event.GetId()) )
        {
            return;
        }
    }
    event.Skip();
}

Generated by  Doxygen 1.6.0   Back to index