File: LogWindow.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (250 lines) | stat: -rw-r--r-- 6,362 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**********************************************************************

Audacity: A Digital Audio Editor

LogWindow.cpp

Paul Licameli split from AudacityLogger.cpp

**********************************************************************/
#include "LogWindow.h"

#include <optional>
#include <wx/filedlg.h>
#include <wx/frame.h>
#include <wx/icon.h>
#include <wx/settings.h>
#include <wx/textctrl.h>
#include <wx/weakref.h>

#include "AudacityLogger.h"
#include "AudacityMessageBox.h"
#include "FileNames.h"
#include "Internat.h"
#include "MemoryX.h"
#include "Prefs.h"
#include "SelectFile.h"
#include "ShuttleGui.h"

#include "AudacityLogoAlpha.xpm"

// If wxLogWindow is used and initialized before the Mac's "root" window, then
//     Audacity may crash when terminating.  It's not fully understood why this occurs
//     but it probably has to do with the order of deletion.  However, deferring the
//     creation of the log window until it is actually shown circumvents the problem.
enum
{
   LoggerID_Save = wxID_HIGHEST + 1,
   LoggerID_Clear,
   LoggerID_Close
};

namespace {
Destroy_ptr<wxFrame> sFrame;
wxWeakRef<wxTextCtrl> sText;

struct LogWindowUpdater : public PrefsListener
{
   // PrefsListener implementation
   void UpdatePrefs() override;
};
// Unique PrefsListener can't be statically constructed before the application
// object initializes, so use optional
std::optional<LogWindowUpdater> pUpdater;

void OnCloseWindow(wxCloseEvent & e);
void OnClose(wxCommandEvent & e);
void OnClear(wxCommandEvent & e);
void OnSave(wxCommandEvent & e);
}

void LogWindow::Show(bool show)
{
   // Hide the frame if created, otherwise do nothing
   if (!show) {
      if (sFrame) {
         sFrame->Show(false);
      }
      return;
   }

   // If the frame already exists, refresh its contents and show it
   auto pLogger = AudacityLogger::Get();
   if (sFrame) {
      if (!sFrame->IsShown() && sText) {
         if (pLogger)
            sText->ChangeValue(pLogger->GetBuffer());
         sText->SetInsertionPointEnd();
         sText->ShowPosition(sText->GetLastPosition());
      }
      sFrame->Show();
      sFrame->Raise();
      return;
   }

   // This is the first use, so create the frame
   Destroy_ptr<wxFrame> frame
      { safenew wxFrame(NULL, wxID_ANY, _("Audacity Log")) };
   frame->SetName(frame->GetTitle());
   frame->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));

   // loads either the XPM or the windows resource, depending on the platform
   {
#if !defined(__WXMAC__) && !defined(__WXX11__)
#if defined(__WXMSW__)
      wxIcon ic{wxICON(AudacityLogo)};
#elif defined(__WXGTK__)
      wxIcon ic{wxICON(AudacityLogoAlpha)};
#else
      wxIcon ic{};
      ic.CopyFromBitmap(theTheme.Bitmap(bmpAudacityLogo48x48));
#endif
      frame->SetIcon(ic);
#endif
   }

   // Log text
   ShuttleGui S(frame.get(), eIsCreating);

   S.Style(wxNO_BORDER | wxTAB_TRAVERSAL).Prop(true).StartPanel();
   {
      S.StartVerticalLay(true);
      {
         sText = S.Style(wxTE_MULTILINE | wxHSCROLL | wxTE_READONLY | wxTE_RICH)
            .AddTextWindow({});
         
         // Populated TextWindow created above
         if (pLogger) *sText << pLogger->GetBuffer();

         S.AddSpace(0, 5);
         S.StartHorizontalLay(wxALIGN_CENTER, 0);
         {
            S.AddSpace(10, 0);
            S.Id(LoggerID_Save).AddButton(XXO("&Save..."));
            S.Id(LoggerID_Clear).AddButton(XXO("Cl&ear"));
            S.Id(LoggerID_Close).AddButton(XXO("&Close"));
            S.AddSpace(10, 0);
         }
         S.EndHorizontalLay();
         S.AddSpace(0, 3);
      }
      S.EndVerticalLay();
   }
   S.EndPanel();

   // Give a place for the menu help text to go
   // frame->CreateStatusBar();

   frame->Layout();

   // Hook into the frame events
   frame->Bind(wxEVT_CLOSE_WINDOW, OnCloseWindow );

   frame->Bind(   wxEVT_COMMAND_MENU_SELECTED, OnSave, LoggerID_Save);
   frame->Bind(   wxEVT_COMMAND_MENU_SELECTED, OnClear, LoggerID_Clear);
   frame->Bind(   wxEVT_COMMAND_MENU_SELECTED, OnClose, LoggerID_Close);
   frame->Bind(   wxEVT_COMMAND_BUTTON_CLICKED, OnSave, LoggerID_Save);
   frame->Bind(   wxEVT_COMMAND_BUTTON_CLICKED, OnClear, LoggerID_Clear);
   frame->Bind(   wxEVT_COMMAND_BUTTON_CLICKED, OnClose, LoggerID_Close);

   sFrame = std::move( frame );

   sFrame->Show();

   if (pLogger)
      pLogger->Flush();

   // Also create the listeners
   if (!pUpdater)
      pUpdater.emplace();

   if (pLogger) {
      pLogger->SetListener([]{
         if (auto pLogger = AudacityLogger::Get()) {
            if (sFrame && sFrame->IsShown()) {
               if (sText)
                  sText->ChangeValue(pLogger->GetBuffer());
               return true;
            }
         }
         return false;
      });

      // Initial flush populates sText
      pLogger->Flush();
   }
}

void LogWindow::Destroy()
{
   sFrame.reset();
}

namespace {
void OnCloseWindow(wxCloseEvent & WXUNUSED(e))
{
#if defined(__WXMAC__)
   // On the Mac, destroy the window rather than hiding it since the
   // log menu will override the root windows menu if there is no
   // project window open.
   sFrame.reset();
#else
   sFrame->Show(false);
#endif
}

void OnClose(wxCommandEvent & WXUNUSED(e))
{
   wxCloseEvent dummy;
   OnCloseWindow(dummy);
}

void OnClear(wxCommandEvent & WXUNUSED(e))
{
   auto pLogger = AudacityLogger::Get();
   if (pLogger)
      pLogger->ClearLog();
}

void OnSave(wxCommandEvent & WXUNUSED(e))
{
   wxString fName = _("log.txt");

   fName = SelectFile(FileNames::Operation::Export,
      XO("Save log to:"),
      wxEmptyString,
      fName,
      wxT("txt"),
      { FileNames::TextFiles },
      wxFD_SAVE | wxFD_OVERWRITE_PROMPT | wxRESIZE_BORDER,
      sFrame.get());

   if (fName.empty()) {
      return;
   }

   if (!(sText && sText->SaveFile(fName))) {
      AudacityMessageBox(
         XO("Couldn't save log to file: %s").Format( fName ),
         XO("Warning"),
         wxICON_EXCLAMATION,
         sFrame.get());
      return;
   }
}

void LogWindowUpdater::UpdatePrefs()
{
   //! Re-create the non-modal window in case of change of preferred language
   if (sFrame) {
      bool shown = sFrame->IsShown();
      if (shown) {
         LogWindow::Show(false);
      }
      sFrame.reset();
      if (shown) {
         LogWindow::Show(true);
      }
   }
}
}