~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxSmith/wxsresourcefactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of wxSmith plugin for Code::Blocks Studio
 
3
* Copyright (C) 2006-2007  Bartlomiej Swiecki
 
4
*
 
5
* wxSmith is free software; you can redistribute it and/or modify
 
6
* it under the terms of the GNU General Public License as published by
 
7
* the Free Software Foundation; either version 3 of the License, or
 
8
* (at your option) any later version.
 
9
*
 
10
* wxSmith is distributed in the hope that it will be useful,
 
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
* GNU General Public License for more details.
 
14
*
 
15
* You should have received a copy of the GNU General Public License
 
16
* along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
 
17
*
 
18
* $Revision: 4850 $
 
19
* $Id: wxsresourcefactory.cpp 4850 2008-01-29 21:45:49Z byo $
 
20
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/contrib/wxSmith/wxsresourcefactory.cpp $
 
21
*/
 
22
 
 
23
#include "wxsresourcefactory.h"
 
24
 
 
25
// TODO: Support dynamic loading / unloading of factories
 
26
 
 
27
wxsResourceFactory* wxsResourceFactory::m_UpdateQueue = 0;
 
28
wxsResourceFactory* wxsResourceFactory::m_Initialized = 0;
 
29
wxsResourceFactory::HashT wxsResourceFactory::m_Hash;
 
30
wxString wxsResourceFactory::m_LastExternalName;
 
31
wxsResourceFactory* wxsResourceFactory::m_LastExternalFactory = 0;
 
32
bool wxsResourceFactory::m_AllAttached = false;
 
33
 
 
34
wxsResourceFactory::wxsResourceFactory()
 
35
{
 
36
    m_Next = m_UpdateQueue;
 
37
    m_UpdateQueue = this;
 
38
    m_Attached = false;
 
39
}
 
40
 
 
41
wxsResourceFactory::~wxsResourceFactory()
 
42
{
 
43
}
 
44
 
 
45
void wxsResourceFactory::InitializeFromQueue()
 
46
{
 
47
    while ( m_UpdateQueue )
 
48
    {
 
49
        wxsResourceFactory* NextFactory = m_UpdateQueue->m_Next;
 
50
        m_UpdateQueue->Initialize();
 
51
        m_UpdateQueue = NextFactory;
 
52
    }
 
53
}
 
54
 
 
55
inline void wxsResourceFactory::Initialize()
 
56
{
 
57
    for ( int i=OnGetCount(); i-->0; )
 
58
    {
 
59
        wxString Name;
 
60
        wxString GUI;
 
61
        OnGetInfo(i,Name,GUI);
 
62
        ResourceInfo& Info = m_Hash[Name];
 
63
        Info.m_Factory = this;
 
64
        Info.m_Number = i;
 
65
        Info.m_GUI = GUI;
 
66
        if ( Info.m_MenuId < 0 )
 
67
        {
 
68
            Info.m_MenuId = wxNewId();
 
69
        }
 
70
    }
 
71
 
 
72
    m_Next = m_Initialized;
 
73
    m_Initialized = this;
 
74
 
 
75
    if ( m_AllAttached )
 
76
    {
 
77
        OnAttach();
 
78
        m_Attached = true;
 
79
    }
 
80
}
 
81
 
 
82
wxsResource* wxsResourceFactory::Build(const wxString& ResourceType,wxsProject* Project)
 
83
{
 
84
    InitializeFromQueue();
 
85
    ResourceInfo& Info = m_Hash[ResourceType];
 
86
    if ( !Info.m_Factory )
 
87
    {
 
88
        return 0;
 
89
    }
 
90
    return Info.m_Factory->OnCreate(Info.m_Number,Project);
 
91
}
 
92
 
 
93
bool wxsResourceFactory::CanHandleExternal(const wxString& FileName)
 
94
{
 
95
    InitializeFromQueue();
 
96
    for ( wxsResourceFactory* Factory = m_Initialized; Factory; Factory=Factory->m_Next )
 
97
    {
 
98
        if ( Factory->OnCanHandleExternal(FileName) )
 
99
        {
 
100
            m_LastExternalName = FileName;
 
101
            m_LastExternalFactory = Factory;
 
102
            return true;
 
103
        }
 
104
    }
 
105
    m_LastExternalName = wxEmptyString;
 
106
    m_LastExternalFactory = 0;
 
107
    return false;
 
108
}
 
109
 
 
110
wxsResource* wxsResourceFactory::BuildExternal(const wxString& FileName)
 
111
{
 
112
    InitializeFromQueue();
 
113
    if ( m_LastExternalFactory && (m_LastExternalName==FileName) )
 
114
    {
 
115
        return m_LastExternalFactory->OnBuildExternal(FileName);
 
116
    }
 
117
    for ( wxsResourceFactory* Factory = m_Initialized; Factory; Factory=Factory->m_Next )
 
118
    {
 
119
        wxsResource* Res = Factory->OnBuildExternal(FileName);
 
120
        if ( Res ) return Res;
 
121
    }
 
122
    return 0;
 
123
}
 
124
 
 
125
void wxsResourceFactory::BuildSmithMenu(wxMenu* menu)
 
126
{
 
127
    InitializeFromQueue();
 
128
    for ( HashT::iterator i=m_Hash.begin(); i!=m_Hash.end(); ++i )
 
129
    {
 
130
        if ( i->second.m_Factory == 0 ) continue;
 
131
        wxString MenuEntry = _T("Add ") + i->first;
 
132
        menu->Append(i->second.m_MenuId,MenuEntry);
 
133
    }
 
134
}
 
135
 
 
136
bool wxsResourceFactory::NewResourceMenu(int Id,wxsProject* Project)
 
137
{
 
138
    for ( HashT::iterator i=m_Hash.begin(); i!=m_Hash.end(); ++i )
 
139
    {
 
140
        if ( i->second.m_Factory == 0 ) continue;
 
141
        if ( i->second.m_MenuId == Id )
 
142
        {
 
143
            return i->second.m_Factory->OnNewWizard(i->second.m_Number,Project);
 
144
        }
 
145
    }
 
146
    return false;
 
147
}
 
148
 
 
149
int wxsResourceFactory::ResourceTreeIcon(const wxString& ResourceType)
 
150
{
 
151
    InitializeFromQueue();
 
152
    ResourceInfo& Info = m_Hash[ResourceType];
 
153
    if ( !Info.m_Factory )
 
154
    {
 
155
        return -1;
 
156
    }
 
157
    return Info.m_Factory->OnResourceTreeIcon(Info.m_Number);
 
158
}
 
159
 
 
160
void wxsResourceFactory::OnAttachAll()
 
161
{
 
162
    if ( m_AllAttached ) return;
 
163
    InitializeFromQueue();
 
164
    for ( wxsResourceFactory* Factory = m_Initialized; Factory; Factory = Factory->m_Next )
 
165
    {
 
166
        if ( !Factory->m_Attached )
 
167
        {
 
168
            Factory->OnAttach();
 
169
            Factory->m_Attached = true;
 
170
        }
 
171
    }
 
172
    m_AllAttached = true;
 
173
}
 
174
 
 
175
void wxsResourceFactory::OnReleaseAll()
 
176
{
 
177
    if ( !m_AllAttached ) return;
 
178
    InitializeFromQueue();
 
179
    for ( wxsResourceFactory* Factory = m_Initialized; Factory; Factory = Factory->m_Next )
 
180
    {
 
181
        if ( Factory->m_Attached )
 
182
        {
 
183
            Factory->OnRelease();
 
184
            Factory->m_Attached = false;
 
185
        }
 
186
    }
 
187
    m_AllAttached = false;
 
188
}