File: clBitmap.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (102 lines) | stat: -rw-r--r-- 2,612 bytes parent folder | download | duplicates (3)
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
#include "clBitmap.h"
#if wxUSE_GUI
#include <wx/dcscreen.h>
#include <wx/filename.h>
#include <wx/window.h>
#ifdef __WXGTK20__
#include <gtk/gtk.h>
#endif

clBitmap::clBitmap() {}

clBitmap::clBitmap(const wxImage& img, double scale)
#if wxVERSION_NUMBER >= 3100
    : wxBitmap(img, -1, scale)
#else
    : wxBitmap(img, -1)
#endif
{
}

clBitmap::~clBitmap() {}

bool clBitmap::LoadFile(const wxString& name, wxBitmapType type)
{
#if defined(__WXMSW__)
    wxFileName filename(name);
    double scale = 1.0;
    if(ShouldLoadHiResImages()) {
        wxFileName hiResFileName = filename;
        wxString hiresName = hiResFileName.GetName();
        hiresName << "@2x";
        hiResFileName.SetName(hiresName);
        if(hiResFileName.Exists()) {
            filename = hiResFileName;
            scale = 2.0;
        }
    }
    wxImage img(filename.GetFullPath(), type);
    *this = clBitmap(img, scale);
    return true;
#else
    return wxBitmap::LoadFile(name, type);
#endif
}

bool clBitmap::ShouldLoadHiResImages()
{
    static bool once = false;
    static bool shouldLoad = false;
    if(!once) {
        once = true;
#ifdef __WXGTK__
        // try the environment variable approach first
        wxString dpiscale = "1.0";
        if(wxGetEnv("GDK_DPI_SCALE", &dpiscale)) {
            double scale = 1.0;
            if(dpiscale.ToDouble(&scale)) {
                shouldLoad = (scale >= 1.5);
                return shouldLoad;
            }
        }

        // Try the GTK way
        GdkScreen* screen = gdk_screen_get_default();
        if(screen) {
            double res = gdk_screen_get_resolution(screen);
            shouldLoad = ((res / 96.) >= 1.5);
        }

#else
        shouldLoad = ((wxScreenDC().GetPPI().y / 96.) >= 1.5);
#endif
    }
    return shouldLoad;
}

bool clBitmap::LoadPNGFromMemory(const wxString& name, wxMemoryInputStream& mis,
                                 std::function<bool(const wxString&, void**, size_t&)> fnGetHiResVersion)
{
    void* pData = NULL;
    size_t nLen = 0;

    // we will load the @2x version on demand
    if(name.Contains("@2x")) { return false; }

    if(ShouldLoadHiResImages()) {
        wxString hiresName = name + "@2x";
        if(fnGetHiResVersion(hiresName, &pData, nLen)) {
            wxMemoryInputStream m(pData, nLen);
            wxImage img(m, wxBITMAP_TYPE_PNG);
            if(img.IsOk()) {
                *this = clBitmap(img, 2.0);
                return IsOk();
            }
        }
    }
    wxImage img(mis, wxBITMAP_TYPE_PNG);
    *this = clBitmap(img, 1.0);
    return IsOk();
}

#endif // LIBCODELITE_WITH_UI