| 1 |
#include "clBitmap.h" |
| 2 |
#if wxUSE_GUI |
| 3 |
#include <wx/dcscreen.h> |
| 4 |
#include <wx/filename.h> |
| 5 |
#include <wx/window.h> |
| 6 |
#ifdef __WXGTK20__ |
| 7 |
#include <gtk/gtk.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
clBitmap::clBitmap() {} |
| 11 |
|
| 12 |
clBitmap::clBitmap(const wxImage& img, double scale) |
| 13 |
#if wxVERSION_NUMBER >= 3100 |
| 14 |
: wxBitmap(img, -1, scale) |
| 15 |
#else |
| 16 |
: wxBitmap(img, -1) |
| 17 |
#endif |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
clBitmap::~clBitmap() {} |
| 22 |
|
| 23 |
bool clBitmap::LoadFile(const wxString& name, wxBitmapType type) |
| 24 |
{ |
| 25 |
#if defined(__WXMSW__) |
| 26 |
wxFileName filename(name); |
| 27 |
double scale = 1.0; |
| 28 |
if(ShouldLoadHiResImages()) { |
| 29 |
wxFileName hiResFileName = filename; |
| 30 |
wxString hiresName = hiResFileName.GetName(); |
| 31 |
hiresName << "@2x"; |
| 32 |
hiResFileName.SetName(hiresName); |
| 33 |
if(hiResFileName.Exists()) { |
| 34 |
filename = hiResFileName; |
| 35 |
scale = 2.0; |
| 36 |
} |
| 37 |
} |
| 38 |
wxImage img(filename.GetFullPath(), type); |
| 39 |
*this = clBitmap(img, scale); |
| 40 |
return true; |
| 41 |
#else |
| 42 |
return wxBitmap::LoadFile(name, type); |
| 43 |
#endif |
| 44 |
} |
| 45 |
|
| 46 |
bool clBitmap::ShouldLoadHiResImages() |
| 47 |
{ |
| 48 |
static bool once = false; |
| 49 |
static bool shouldLoad = false; |
| 50 |
if(!once) { |
| 51 |
once = true; |
| 52 |
#ifdef __WXGTK__ |
| 53 |
// try the environment variable approach first |
| 54 |
wxString dpiscale = "1.0"; |
| 55 |
if(wxGetEnv("GDK_DPI_SCALE", &dpiscale)) { |
| 56 |
double scale = 1.0; |
| 57 |
if(dpiscale.ToDouble(&scale)) { |
| 58 |
shouldLoad = (scale >= 1.5); |
| 59 |
return shouldLoad; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Try the GTK way |
| 64 |
GdkScreen* screen = gdk_screen_get_default(); |
| 65 |
if(screen) { |
| 66 |
double res = gdk_screen_get_resolution(screen); |
| 67 |
shouldLoad = ((res / 96.) >= 1.5); |
| 68 |
} |
| 69 |
|
| 70 |
#else |
| 71 |
shouldLoad = ((wxScreenDC().GetPPI().y / 96.) >= 1.5); |
| 72 |
#endif |
| 73 |
} |
| 74 |
return shouldLoad; |
| 75 |
} |
| 76 |
|
| 77 |
bool clBitmap::LoadPNGFromMemory(const wxString& name, wxMemoryInputStream& mis, |
| 78 |
std::function<bool(const wxString&, void**, size_t&)> fnGetHiResVersion) |
| 79 |
{ |
| 80 |
void* pData = NULL; |
| 81 |
size_t nLen = 0; |
| 82 |
|
| 83 |
// we will load the @2x version on demand |
| 84 |
if(name.Contains("@2x")) { return false; } |
| 85 |
|
| 86 |
if(ShouldLoadHiResImages()) { |
| 87 |
wxString hiresName = name + "@2x"; |
| 88 |
if(fnGetHiResVersion(hiresName, &pData, nLen)) { |
| 89 |
wxMemoryInputStream m(pData, nLen); |
| 90 |
wxImage img(m, wxBITMAP_TYPE_PNG); |
| 91 |
if(img.IsOk()) { |
| 92 |
*this = clBitmap(img, 2.0); |
| 93 |
return IsOk(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
wxImage img(mis, wxBITMAP_TYPE_PNG); |
| 98 |
*this = clBitmap(img, 1.0); |
| 99 |
return IsOk(); |
| 100 |
} |
| 101 |
|
| 102 |
#endif // LIBCODELITE_WITH_UI |