Audacity 3.2.0
UserImage.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 UserImage.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#include "UserImage.h"
12
13#include <memory>
14
15#include <wx/dcbuffer.h>
16#include <wx/graphics.h>
17
19{
20
21UserImage::UserImage(wxWindow* parent, const wxSize& size)
22 : wxPanelWrapper(parent, wxID_ANY, wxDefaultPosition, size, wxBORDER_NONE)
23{
24 SetBackgroundStyle(wxBG_STYLE_PAINT);
25
26 SetMinSize(size);
27 SetMaxSize(size);
28
29 Bind(wxEVT_PAINT, [this](auto&) { OnPaint(); });
30}
31
32void UserImage::SetBitmap(const wxBitmap& bitmap)
33{
34 mBitmap = bitmap;
35 Refresh(true);
36}
37
38void UserImage::SetBitmap(const wxString& path)
39{
40 wxImage image(path);
41
42 if (image.IsOk())
43 {
44 mBitmap = wxBitmap(image);
45 Refresh(true);
46 }
47}
48
50{
51 wxAutoBufferedPaintDC dc(this);
52 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
53
54 gc->SetInterpolationQuality(wxINTERPOLATION_BEST);
55
56 const wxSize size = GetSize();
57
58 gc->SetBrush(wxBrush(GetBackgroundColour()));
59 gc->DrawRectangle(0, 0, size.x, size.y);
60
61 if (!mBitmap.IsOk())
62 {
63 gc->SetBrush(wxBrush(wxColour(255, 255, 255, 255)));
64 gc->DrawEllipse(0, 0, size.x, size.y);
65 }
66 else
67 {
68 const wxSize resultingImageSize { std::min(size.x, mBitmap.GetWidth()),
69 std::min(size.y, mBitmap.GetHeight()) };
70
71 gc->DrawBitmap(
72 mBitmap,
73 (size.x - resultingImageSize.x) / 2,
74 (size.y - resultingImageSize.y) / 2,
75 resultingImageSize.x,
76 resultingImageSize.y);
77
78 auto path = gc->CreatePath();
79
80 path.MoveToPoint(0, 0);
81 path.AddLineToPoint(size.x / 2, 0);
82 path.AddArc(size.x / 2, size.y / 2, size.x / 2, 3 * M_PI_2, M_PI, false);
83 path.CloseSubpath();
84
85 path.MoveToPoint(size.x, 0);
86 path.AddLineToPoint(size.x, size.y / 2);
87 path.AddArc(size.x / 2, size.y / 2, size.x / 2, 0, 3 * M_PI_2, false);
88 path.CloseSubpath();
89
90 path.MoveToPoint(size.x, size.y);
91 path.AddLineToPoint(size.x / 2, size.y);
92 path.AddArc(size.x / 2, size.y / 2, size.x / 2, M_PI_2, 0, false);
93 path.CloseSubpath();
94
95 path.MoveToPoint(0, size.y);
96 path.AddLineToPoint(0, size.y / 2);
97 path.AddArc(size.x / 2, size.y / 2, size.x / 2, M_PI, M_PI_2, false);
98 path.CloseSubpath();
99
100 gc->DrawPath(path);
101 }
102}
103
104} // namespace audacity::cloud::audiocom
wxImage(22, 22)
int min(int a, int b)
#define M_PI_2
Definition: Distortion.cpp:25
#define M_PI
Definition: Distortion.cpp:22
UserImage(wxWindow *parent, const wxSize &size)
Definition: UserImage.cpp:21
void SetBitmap(const wxBitmap &bitmap)
Definition: UserImage.cpp:32