Audacity  3.0.3
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 
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:
24  ReadOnlyTextAx(wxWindow * window)
25  : WindowAccessible(window)
26  {
27  }
28 
29  ~ReadOnlyTextAx()
30  {
31  }
32 
33  wxAccStatus GetRole(int childId, wxAccRole *role)
34  {
35  *role = wxROLE_SYSTEM_STATICTEXT;
36 
37  return wxACC_OK;
38  }
39 
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 
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)
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 
110  {
111  };
112 
113  wxSize DoGetBestClientSize() const override
114  {
115  wxClientDC dc(wxConstCast(this, ReadOnlyText));
116 
117  return dc.GetMultiLineTextExtent(GetLabel());
118  }
119 
120  wxString GetValue()
121  {
122  return GetLabel();
123  }
124 
125  void SetValue(const wxString &value)
126  {
127  SetLabel(value);
128  Refresh();
129  }
130 
131  void SetValue(const TranslatableString &value)
132  {
133  SetValue(value.Translation());
134  }
135 };
136 
137 #endif // __AUDACITY_READONLYTEXT__
ReadOnlyText
Definition: ReadOnlyText.h:60
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
ReadOnlyText::DoGetBestClientSize
wxSize DoGetBestClientSize() const override
Definition: ReadOnlyText.h:113
ReadOnlyText::SetValue
void SetValue(const wxString &value)
Definition: ReadOnlyText.h:125
ReadOnlyText::SetValue
void SetValue(const TranslatableString &value)
Definition: ReadOnlyText.h:131
ReadOnlyText::~ReadOnlyText
~ReadOnlyText()
Definition: ReadOnlyText.h:109
WindowAccessible
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
id
int id
Definition: WaveTrackControls.cpp:591
WindowAccessible.h
TranslatableString::Translation
wxString Translation() const
Definition: TranslatableString.h:79
ReadOnlyText::ReadOnlyText
ReadOnlyText(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxBORDER_NONE)
Definition: ReadOnlyText.h:62
safenew
#define safenew
Definition: MemoryX.h:10
ReadOnlyText::GetValue
wxString GetValue()
Definition: ReadOnlyText.h:120