1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/*
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
*
* This program 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; version 2 of the
* License.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef _WIN32
#include <glib.h>
#include <algorithm>
#endif
#include "base/file_utilities.h"
#include "icon_manager.h"
#include "common.h"
#ifdef __APPLE__
#include "mforms/app.h"
#endif
/**
* @file icon_manager.cpp
* @brief
*/
using namespace bec;
IconManager::IconManager()
{
gchar *tmp= g_get_current_dir();
_basedir= tmp;
g_free(tmp);
_next_id= 1;
/* do not hardcode stuff
add_search_path(".");
add_search_path("./images");
add_search_path("./images/grt");
add_search_path("./images/grt/structs");
add_search_path("./images/icons");
*/
}
void IconManager::set_basedir(const std::string &basedir)
{
_basedir= basedir;
}
IconManager *IconManager::get_instance()
{
static IconManager inst;
return &inst;
}
static std::string get_icon_file_for_size(const std::string &aicon_file, IconSize size,
const std::string &extra_qualifier)
{
std::string file;
std::string icon_file= aicon_file;
if (!extra_qualifier.empty())
icon_file= replace_string(icon_file, "$", extra_qualifier + ".$");
if (icon_file.find('$')!=std::string::npos)
{
// strip the .png
file= icon_file.substr(0, icon_file.rfind('$'));
switch (size)
{
case Icon11:
file+="11x11";
break;
case Icon12:
file+="12x12";
break;
case Icon16:
file+="16x16";
break;
case Icon24:
file+="24x24";
break;
case Icon32:
file+="32x32";
break;
case Icon48:
file+="48x48";
break;
case Icon64:
file+="64x64";
break;
}
file+= icon_file.substr(icon_file.rfind('$')+1);
}
else
file= icon_file;
return file;
}
std::string IconManager::get_icon_path(const std::string &file)
{
boost::unordered_map<std::string, std::string>::const_iterator it = _icon_paths.find(file);
if (it != _icon_paths.end())
return it->second;
for (std::vector<std::string>::const_iterator i= _search_path.begin();
i != _search_path.end(); ++i)
{
std::string path= _basedir + G_DIR_SEPARATOR + *i + G_DIR_SEPARATOR + file;
#ifdef __APPLE__
std::string mac_path;
if (mforms::App::get()->backing_scale_factor() > 1)
{
mac_path = base::strip_extension(path) + "_mac@2x.png";
if (g_file_test(mac_path.c_str(), G_FILE_TEST_EXISTS))
{
_icon_paths[file]= mac_path;
return mac_path;
}
mac_path = base::strip_extension(path) + "@2x.png";
if (g_file_test(mac_path.c_str(), G_FILE_TEST_EXISTS))
{
_icon_paths[file]= mac_path;
return mac_path;
}
}
mac_path = base::strip_extension(path) + "_mac.png";
if (g_file_test(mac_path.c_str(), G_FILE_TEST_EXISTS))
{
_icon_paths[file]= mac_path;
return mac_path;
}
#endif
if (g_file_test(path.c_str(), G_FILE_TEST_EXISTS))
{
_icon_paths.insert(std::make_pair(file, path));
return path;
}
}
_icon_paths.insert(std::make_pair(file, ""));
return "";
}
IconId IconManager::get_icon_id(const std::string &icon_file, IconSize size,
const std::string &extra_qualifier)
{
std::map<std::string,IconId>::iterator it;
std::string file= get_icon_file_for_size(icon_file, size, extra_qualifier);
if ((it= _icon_ids.find(file)) != _icon_ids.end())
{
return it->second;
}
_icon_files[_next_id]= file;
_icon_ids[file]= _next_id;
return _next_id++;
}
IconId IconManager::get_icon_id(const grt::ObjectRef &object, IconSize size,
const std::string &extra_qualifier)
{
return get_icon_id(object.get_metaclass(), size, extra_qualifier);
}
IconId IconManager::get_icon_id(grt::MetaClass *metaclass, IconSize size,
const std::string &extra_qualifier)
{
grt::MetaClass *parent, *gstruct;
std::string file, path;
parent= metaclass;
do
{
gstruct= parent;
file= gstruct->get_attribute("icon");
if (file.empty())
file= std::string(gstruct->name())+".$.png";
file= get_icon_file_for_size(file, size, extra_qualifier);
path= get_icon_path(file);
parent= gstruct->parent();
} while (path.empty() && parent);
std::map<std::string,IconId>::iterator it;
if ((it= _icon_ids.find(file)) != _icon_ids.end())
{
return it->second;
}
_icon_files[_next_id]= file;
_icon_ids[file]= _next_id;
return _next_id++;
}
std::string IconManager::get_icon_file(IconId icon)
{
if (icon == 0)
return "";
return _icon_files[icon];
}
std::string IconManager::get_icon_path(IconId icon)
{
std::string file= get_icon_file(icon);
if (file.empty())
return "";
return get_icon_path(file);
}
void IconManager::add_search_path(const std::string &path)
{
std::string npath;
#ifdef _WIN32
npath= replace_string(path, "/", G_DIR_SEPARATOR_S);
#else
npath= path;
#endif
if (std::find(_search_path.begin(), _search_path.end(), npath) == _search_path.end()
&& g_file_test((_basedir + G_DIR_SEPARATOR + npath).c_str(), G_FILE_TEST_IS_DIR))
_search_path.push_back(npath);
}
|