1 // Copyright (c) Charles J. Cliffe
2 // SPDX-License-Identifier: GPL-2.0+
3 
4 #include "DemodLabelDialog.h"
5 
6 #include "wx/clipbrd.h"
7 #include <sstream>
8 #include "CubicSDR.h"
9 
wxBEGIN_EVENT_TABLE(DemodLabelDialog,wxDialog)10 wxBEGIN_EVENT_TABLE(DemodLabelDialog, wxDialog)
11 EVT_CHAR_HOOK(DemodLabelDialog::OnChar)
12 EVT_SHOW(DemodLabelDialog::OnShow)
13 wxEND_EVENT_TABLE()
14 
15 DemodLabelDialog::DemodLabelDialog(wxWindow * parent, wxWindowID id, const wxString & title,
16         DemodulatorInstancePtr demod, const wxPoint & position,
17         const wxSize & size, long style) :
18         wxDialog(parent, id, title, position, size, style) {
19 
20     wxString labelStr;
21 
22     //by construction, is always != nullptr
23     activeDemod = demod;
24 
25     labelStr = activeDemod->getDemodulatorUserLabel();
26 
27     if (labelStr.empty()) {
28         //propose a default value...
29         labelStr = activeDemod->getDemodulatorType();
30     }
31 
32 
33     dialogText = new wxTextCtrl(this, wxID_LABEL_INPUT, labelStr, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
34     dialogText->SetFont(wxFont(15, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
35 
36     // Set the textControl width to the [title + 100%] or the [content +100%],
37 // whichever's the greater.
38     int textCtrlX = dialogText->GetTextExtent(labelStr).GetWidth();
39     int titleX = this->GetTextExtent(title).GetWidth();
40     dialogText->SetMinSize(wxSize(max(int(2.0 * titleX), int(2.0 * textCtrlX)), -1));
41 
42     auto* dialogsizer = new wxBoxSizer(wxALL);
43     dialogsizer->Add(dialogText, wxSizerFlags(1).Expand().Border(wxALL, 5));
44     SetSizerAndFit(dialogsizer);
45     Centre();
46 
47     dialogText->SetSelection(-1, -1);
48 }
49 
50 
OnChar(wxKeyEvent & event)51 void DemodLabelDialog::OnChar(wxKeyEvent& event) {
52     int c = event.GetKeyCode();
53 
54     //we support 16 bit strings for user labels internally.
55     wxString strValue = dialogText->GetValue();
56 
57     switch (c) {
58     case WXK_RETURN:
59     case WXK_NUMPAD_ENTER:
60 
61         //No need to display the demodulator type twice if the user do not change the default value...
62         //when comparing getDemodulatorType() std::string, take care of "upgrading" it to wxString which will
63         //try to its best...
64         if (strValue != wxString(activeDemod->getDemodulatorType())) {
65             activeDemod->setDemodulatorUserLabel(strValue.ToStdWstring());
66         }
67         else {
68             activeDemod->setDemodulatorUserLabel(L"");
69         }
70         wxGetApp().getBookmarkMgr().updateActiveList();
71         Close();
72         break;
73     case WXK_ESCAPE:
74         Close();
75         break;
76     }
77 
78     if (event.ControlDown() && c == 'V') {
79         // Alter clipboard contents to remove unwanted chars
80         wxTheClipboard->Open();
81         wxTextDataObject data;
82         wxTheClipboard->GetData(data);
83         std::wstring clipText = data.GetText().ToStdWstring();
84         wxTheClipboard->SetData(new wxTextDataObject(clipText));
85         wxTheClipboard->Close();
86         event.Skip();
87     }
88     else if (c == WXK_RIGHT || c == WXK_LEFT || event.ControlDown()) {
89         event.Skip();
90 
91     }
92     else {
93 #if defined(__linux__) || defined(__FreeBSD__)
94         dialogText->OnChar(event);
95         event.Skip();
96 #else
97         event.DoAllowNextEvent();
98 #endif
99     }
100 }
101 
OnShow(wxShowEvent & event)102 void DemodLabelDialog::OnShow(wxShowEvent &event) {
103 
104     dialogText->SetFocus();
105     dialogText->SetSelection(-1, -1);
106 	event.Skip();
107 }
108