Audacity 3.2.0
ReadOnlyText.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 ReadOnlyText.h
6
7**********************************************************************/
8
9#ifndef __AUDACITY_READONLYTEXT__
10#define __AUDACITY_READONLYTEXT__
11
12#include <wx/wx.h>
13
14
15
16#include "WindowAccessible.h"
17
18#if wxUSE_ACCESSIBILITY
19
20class ReadOnlyTextAx final : public WindowAccessible
21{
22public:
23 ReadOnlyTextAx(wxWindow * window)
24 : WindowAccessible(window)
25 {
26 }
27
28 ~ReadOnlyTextAx()
29 {
30 }
31
32 wxAccStatus GetRole(int childId, wxAccRole *role)
33 {
34 *role = wxROLE_SYSTEM_STATICTEXT;
35
36 return wxACC_OK;
37 }
38
39 wxAccStatus GetState(int childId, long *state)
40 {
41 auto w = GetWindow();
42 *state = wxACC_STATE_SYSTEM_FOCUSABLE | wxACC_STATE_SYSTEM_READONLY;
43 *state |= ( w == wxWindow::FindFocus() ? wxACC_STATE_SYSTEM_FOCUSED : 0 );
44
45 return wxACC_OK;
46 }
47
48 wxAccStatus GetValue(int childId, wxString* strValue)
49 {
50 *strValue = GetWindow()->GetLabel();
51
52 return wxACC_OK;
53 }
54};
55
56#endif
57
58class ReadOnlyText final : public wxControl
59{
60public:
61 ReadOnlyText(wxWindow *parent,
62 wxWindowID id,
63 const wxString &value,
64 const wxPoint &pos = wxDefaultPosition,
65 const wxSize &size = wxDefaultSize,
66 long style = wxBORDER_NONE)
67 : wxControl(parent, id, pos, size, style)
68 {
69#if wxUSE_ACCESSIBILITY
70 SetAccessible(safenew ReadOnlyTextAx(this));
71#endif
72 SetInitialSize(size);
73
74 Bind(wxEVT_SET_FOCUS, [&](wxFocusEvent &event)
75 {
76 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
77 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
78 Refresh();
79 event.Skip();
80 });
81
82 Bind(wxEVT_KILL_FOCUS, [&](wxFocusEvent &event)
83 {
84 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
85 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
86 Refresh();
87 event.Skip();
88 });
89
90 Bind(wxEVT_PAINT, [&](wxPaintEvent & WXUNUSED(event))
91 {
92 wxPaintDC dc(this);
93
94 wxRect rect = GetClientRect();
95 if (!IsEnabled())
96 {
97 // draw shadow of the text
98 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT));
99 wxRect rectShadow = rect;
100 rectShadow.Offset(1, 1);
101 dc.DrawLabel(GetLabel(), rectShadow, GetAlignment());
102 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));
103 }
104 dc.DrawLabel(GetLabel(), rect, GetAlignment());
105 });
106 };
107
109 {
110 };
111
112 wxSize DoGetBestClientSize() const override
113 {
114 wxClientDC dc(wxConstCast(this, ReadOnlyText));
115
116 return dc.GetMultiLineTextExtent(GetLabel());
117 }
118
119 wxString GetValue()
120 {
121 return GetLabel();
122 }
123
124 void SetValue(const wxString &value)
125 {
126 SetLabel(value);
127 Refresh();
128 }
129
130 void SetValue(const TranslatableString &value)
131 {
132 SetValue(value.Translation());
133 }
134};
135
136#endif // __AUDACITY_READONLYTEXT__
#define safenew
Definition: MemoryX.h:10
int id
void SetValue(const wxString &value)
Definition: ReadOnlyText.h:124
void SetValue(const TranslatableString &value)
Definition: ReadOnlyText.h:130
wxString GetValue()
Definition: ReadOnlyText.h:119
wxSize DoGetBestClientSize() const override
Definition: ReadOnlyText.h:112
ReadOnlyText(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxBORDER_NONE)
Definition: ReadOnlyText.h:61
Holds a msgid for the translation catalog; may also bind format arguments.
wxString Translation() const
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
std::unique_ptr< WindowPlacement > FindFocus()
Find the window that is accepting keyboard input, if any.
Definition: BasicUI.h:383