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 
13 #include <wx/wx.h>
14 
15 
16 
17 #include "WindowAccessible.h"
18 
19 #if wxUSE_ACCESSIBILITY
20 
21 class ReadOnlyTextAx final : public WindowAccessible
22 {
23 public:
ReadOnlyTextAx(wxWindow * window)24    ReadOnlyTextAx(wxWindow * window)
25    :  WindowAccessible(window)
26    {
27    }
28 
~ReadOnlyTextAx()29    ~ReadOnlyTextAx()
30    {
31    }
32 
GetRole(int childId,wxAccRole * role)33    wxAccStatus GetRole(int childId, wxAccRole *role)
34    {
35       *role = wxROLE_SYSTEM_STATICTEXT;
36 
37       return wxACC_OK;
38    }
39 
GetState(int childId,long * state)40    wxAccStatus GetState(int childId, long *state)
41    {
42       auto w = GetWindow();
43       *state = wxACC_STATE_SYSTEM_FOCUSABLE | wxACC_STATE_SYSTEM_READONLY;
44       *state |= ( w == wxWindow::FindFocus() ? wxACC_STATE_SYSTEM_FOCUSED : 0 );
45 
46       return wxACC_OK;
47    }
48 
GetValue(int childId,wxString * strValue)49    wxAccStatus GetValue(int childId, wxString* strValue)
50    {
51       *strValue = GetWindow()->GetLabel();
52 
53       return wxACC_OK;
54    }
55 };
56 
57 #endif
58 
59 class ReadOnlyText final : public wxControl
60 {
61 public:
62    ReadOnlyText(wxWindow *parent,
63                 wxWindowID id,
64                 const wxString &value,
65                 const wxPoint &pos = wxDefaultPosition,
66                 const wxSize &size = wxDefaultSize,
67                 long style = wxBORDER_NONE)
wxControl(parent,id,pos,size,style)68    :  wxControl(parent, id, pos, size, style)
69    {
70 #if wxUSE_ACCESSIBILITY
71       SetAccessible(safenew ReadOnlyTextAx(this));
72 #endif
73       SetInitialSize(size);
74 
75       Bind(wxEVT_SET_FOCUS, [&](wxFocusEvent &event)
76       {
77          SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
78          SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
79          Refresh();
80          event.Skip();
81       });
82 
83       Bind(wxEVT_KILL_FOCUS, [&](wxFocusEvent &event)
84       {
85          SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
86          SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
87          Refresh();
88          event.Skip();
89       });
90 
91       Bind(wxEVT_PAINT, [&](wxPaintEvent & WXUNUSED(event))
92       {
93          wxPaintDC dc(this);
94 
95          wxRect rect = GetClientRect();
96          if (!IsEnabled())
97          {
98             // draw shadow of the text
99             dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT));
100             wxRect rectShadow = rect;
101             rectShadow.Offset(1, 1);
102             dc.DrawLabel(GetLabel(), rectShadow, GetAlignment());
103             dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));
104          }
105          dc.DrawLabel(GetLabel(), rect, GetAlignment());
106       });
107     };
108 
~ReadOnlyText()109    ~ReadOnlyText()
110    {
111    };
112 
DoGetBestClientSize()113    wxSize DoGetBestClientSize() const override
114    {
115        wxClientDC dc(wxConstCast(this, ReadOnlyText));
116 
117        return dc.GetMultiLineTextExtent(GetLabel());
118    }
119 
GetValue()120    wxString GetValue()
121    {
122       return GetLabel();
123    }
124 
SetValue(const wxString & value)125    void SetValue(const wxString &value)
126    {
127       SetLabel(value);
128       Refresh();
129    }
130 
SetValue(const TranslatableString & value)131    void SetValue(const TranslatableString &value)
132    {
133       SetValue(value.Translation());
134    }
135 };
136 
137 #endif // __AUDACITY_READONLYTEXT__
138