NCBI C++ ToolKit
fileartprov.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: fileartprov.cpp 25482 2012-03-27 15:00:20Z kuznets $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Yury Voronov
27  *
28  * File Description: File is supposed to be submitted to wxWidgets main library,
29  * that is why it is written with use of wxWidgets modules only, and in wxWidgets
30  * naming and coding conventions.
31  *
32  */
33 
34 #include <ncbi_pch.hpp>
35 
36 // ---------------------------------------------------------------------------
37 // headers
38 // ---------------------------------------------------------------------------
39 
40 // For compilers that support precompilation, includes "wx.h".
41 #include "wx/wxprec.h"
42 
43 #if defined(__BORLANDC__)
44  #pragma hdrstop
45 #endif
46 
48 #include "wx/hashset.h"
49 #include "wx/filename.h"
50 
51 #ifndef WX_PRECOMP
52  #include "wx/hashmap.h"
53  #include "wx/image.h"
54 #endif
55 
56 
57 // ===========================================================================
58 // implementation
59 // ===========================================================================
60 
61 WX_DECLARE_STRING_HASH_MAP( wxString, wxFileArtNameHash );
62 WX_DECLARE_STRING_HASH_MAP( wxImage, wxFileArtImageHash );
63 
65 {
66 public:
67  static wxString ConstructHashID(
68  const wxArtID& anId, const wxArtClient& aClient = wxART_OTHER, const wxSize& aSize = wxDefaultSize
69  );
70 
71  wxImage GetImage( const wxString& full_id );
72  void PutImage( const wxString& full_id, const wxImage& anImage )
73  {
74  m_ImageHash[full_id] = anImage;
75  }
76 
77  wxString GetName( const wxString& full_id );
78  void PutName( const wxString& full_id, const wxString& aName )
79  {
80  m_NameHash[full_id] = aName;
81  }
82  void Remove( const wxString& full_id )
83  {
84  m_NameHash.erase( full_id );
85  }
86 
87  void Clear()
88  {
89  m_NameHash.clear();
90  }
91 
92 private:
93  wxFileArtNameHash m_NameHash;
94  wxFileArtImageHash m_ImageHash;
95 };
96 
97 wxImage wxFileArtNameAndImageCache::GetImage( const wxString& aFullId )
98 {
99  wxFileArtImageHash::iterator entry = m_ImageHash.find( aFullId );
100 
101  return entry != m_ImageHash.end() ? entry->second : wxImage();
102 }
103 
104 wxString wxFileArtNameAndImageCache::GetName( const wxString& aFullId )
105 {
106  wxFileArtNameHash::iterator entry = m_NameHash.find( aFullId );
107 
108  return entry != m_NameHash.end() ? entry->second : (wxString)wxEmptyString;
109 }
110 
111 
112 /**
113  Creates a string key from three parameters;
114  follows internal code for wxArtProviderCache::ConstructHashID()
115 */
117  const wxArtID& anId, const wxArtClient& aClient, const wxSize& aSize
118 ){
119  wxString str;
120  str.Printf( wxT("%s-%s-%i-%i"), anId.c_str(), aClient.c_str(), aSize.x, aSize.y );
121  return str;
122 }
123 
124 WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, typedef_wxFileArtDirNameSet );
125 
126 class wxFileArtDirNameSet : public typedef_wxFileArtDirNameSet {};
127 
128 IMPLEMENT_DYNAMIC_CLASS(wxFileArtProvider, wxArtProvider)
129 
131 {
132  m_nameCache = new wxFileArtNameAndImageCache();
133  m_DirNameSet = new wxFileArtDirNameSet();
134 
135  wxInitAllImageHandlers();
136 }
137 
138 
140 {
141  delete m_DirNameSet;
142  delete m_nameCache;
143 }
144 
145 int wxFileArtProvider::AddDirectory( wxString aDirName )
146 {
147  wxDir dir( aDirName );
148  if( !dir.IsOpened() ){
149  return -1;
150  }
151 
152  m_DirNameSet->insert( aDirName );
153 
154  return 0;
155 }
156 
158  const wxArtID& anId, const wxArtClient& aClient, const wxSize& aSize,
159  const wxString& aName, long WXUNUSED(aType), int WXUNUSED(anIndex)
160 ){
161  // TODO Add type and index support for images
162 
163  wxString hash_id = wxFileArtNameAndImageCache::ConstructHashID( anId, aClient, aSize );
164 
165  m_nameCache->PutName( hash_id, aName );
166 }
167 
169  const wxArtID& anId, const wxArtClient& aClient, const wxSize& aSize
170 ){
171  wxString hash_id = wxFileArtNameAndImageCache::ConstructHashID( anId, aClient, aSize );
172 
173  m_nameCache->Remove( hash_id );
174 }
175 
177  const wxArtID& anId, const wxArtClient& aClient, const wxSize& aSize
178 ){
179  wxString hash_id = wxFileArtNameAndImageCache::ConstructHashID( anId, aClient, aSize );
180  wxString name = m_nameCache->GetName( hash_id );
181 
182  if( name.IsEmpty() ){
183  hash_id = wxFileArtNameAndImageCache::ConstructHashID( anId, aClient );
184  name = m_nameCache->GetName( hash_id );
185  }
186 
187  if( name.IsEmpty() ){
189  name = m_nameCache->GetName( hash_id );
190  }
191 
192  if( !name.IsEmpty() ){
193 
194  // Check if filename comes with a full directory path
195  // or we need to seek in the art catalog
196  wxFileName wxfname(name);
197  if (!wxfname.DirExists()) {
198 
199  for( wxFileArtDirNameSet::iterator it = m_DirNameSet->begin(); it != m_DirNameSet->end(); ++it ){
200  wxString dir_name = *it;
201 
202  wxDir dir( dir_name );
203  if( !dir.IsOpened() ){
204  continue;
205  }
206 
207  if( dir.HasFiles( name ) ) {
208  wxString sep = wxFileName::GetPathSeparators();
209  name = dir.GetName() + sep[0] + name;
210  break;
211  }
212  }
213  }
214 
215  wxImage image( name );
216  if( image.Ok() ){
217 
218  #if( !defined(__WXMSW__) || wxUSE_WXDIB )
219 
220  if( aSize != wxDefaultSize
221  &&( image.GetWidth() != aSize.x || image.GetHeight() != aSize.y )
222  ){
223  image.Rescale( aSize.x, aSize.y );
224  }
225  #endif
226 
227  return wxBitmap( image );
228  }
229  }
230 
231  return wxNullBitmap;
232 }
wxFileArtImageHash m_ImageHash
Definition: fileartprov.cpp:94
void PutName(const wxString &full_id, const wxString &aName)
Definition: fileartprov.cpp:78
static wxString ConstructHashID(const wxArtID &anId, const wxArtClient &aClient=wxART_OTHER, const wxSize &aSize=wxDefaultSize)
Creates a string key from three parameters; follows internal code for wxArtProviderCache::ConstructHa...
wxString GetName(const wxString &full_id)
void PutImage(const wxString &full_id, const wxImage &anImage)
Definition: fileartprov.cpp:72
wxImage GetImage(const wxString &full_id)
Definition: fileartprov.cpp:97
void Remove(const wxString &full_id)
Definition: fileartprov.cpp:82
wxFileArtNameHash m_NameHash
Definition: fileartprov.cpp:93
virtual void RegisterFileAlias(const wxArtID &anId, const wxArtClient &aClient, const wxSize &aSize, const wxString &aName, long aType=wxBITMAP_TYPE_ANY, int anIndex=-1)
virtual wxBitmap CreateBitmap(const wxArtID &id, const wxArtClient &client, const wxSize &size)
int AddDirectory(wxString aDirName)
wxFileArtDirNameSet * m_DirNameSet
Definition: fileartprov.hpp:95
wxFileArtNameAndImageCache * m_nameCache
Definition: fileartprov.hpp:94
virtual ~wxFileArtProvider()
void RemoveAlias(const wxArtID &anId, const wxArtClient &aClient=wxART_OTHER, const wxSize &aSize=wxDefaultSize)
WX_DECLARE_HASH_SET(wxString, wxStringHash, wxStringEqual, typedef_wxFileArtDirNameSet)
WX_DECLARE_STRING_HASH_MAP(wxString, wxFileArtNameHash)
static const char * str(char *buf, int n)
Definition: stats.c:84
#define wxT(x)
Definition: muParser.cpp:41
Modified on Sat Apr 13 11:45:52 2024 by modify_doxy.py rev. 669887