1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 11954 $
6  * $Id: loghacker.cpp 11954 2020-01-09 18:26:46Z fuscated $
7  * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/loghacker/loghacker.cpp $
8  */
9 
10 #include "sdk.h"
11 #ifndef CB_PRECOMP
12 #include <wx/arrstr.h>
13 #include <wx/intl.h>
14 #include <wx/listbox.h>
15 #include <wx/stattext.h>
16 #include <wx/sizer.h>
17 #include <wx/string.h>
18 #include <wx/textctrl.h>
19 #include "configmanager.h"
20 #include "logmanager.h"
21 #endif
22 #include "configurationpanel.h"
23 #include "loghacker.h"
24 
25 const int id_text = wxNewId();
26 const int id_channel = wxNewId();
27 const int id_logger = wxNewId();
28 
29 namespace
30 {
31     PluginRegistrant<LogHacker> reg(_T("LogHacker"));
32 }
33 
34 
LogHacker()35 LogHacker::LogHacker()
36 {
37 }
38 
~LogHacker()39 LogHacker::~LogHacker()
40 {
41 }
42 
OnAttach()43 void LogHacker::OnAttach()
44 {
45 }
46 
OnRelease(bool)47 void LogHacker::OnRelease(bool /*appShutDown*/)
48 {
49 }
50 
51 class CfgPanel: public cbConfigurationPanel
52 {
53     ConfigManagerContainer::StringToStringMap c2l;
54     ConfigManagerContainer::StringToStringMap c2f;
55 
56     wxListBox*    channel;
57     wxListBox*    logger;
58     wxTextCtrl*    filename;
59 
60     //DECLARE_EVENT_TABLE
61 public:
62 
63     void Create(wxWindow* parent);
64 
GetTitle() const65     virtual wxString GetTitle() const { return _T("Log Hacker"); };
GetBitmapBaseName() const66     virtual wxString GetBitmapBaseName() const { return _T(""); };
67 
68     virtual void OnApply();
OnCancel()69     virtual void OnCancel(){};
70 
71     virtual void Change();
72 };
73 
74 
75 //BEGIN_EVENT_TABLE
76 //   add list control change event ---> Change()
77 //END_EVENT_TABLE
78 
79 
Create(wxWindow * parent)80 void CfgPanel::Create(wxWindow* parent)
81 {
82     wxPanel::Create(parent,-1);
83 
84     wxArrayString channelStrings;
85 
86     int slot = 0;
87     while (slot < LogManager::max_logs)
88     {
89         const wxString t = LogManager::Get()->Slot(slot).title;
90 
91         if (!!t)
92             channelStrings.Add(t);
93 
94         ++slot;
95     }
96 
97     wxArrayString loggerStrings = LogManager::Get()->ListAvailable();
98     loggerStrings.Insert(_T("<application default>"), 0);
99 
100     wxFlexGridSizer* flex = new wxFlexGridSizer(3, 2, 0, 0);
101     flex->AddGrowableRow(1);
102     flex->AddGrowableCol(0);
103     flex->AddGrowableCol(1);
104 
105     wxStaticText* txt;
106     txt = new wxStaticText(this, wxID_STATIC, _("Log Source"));
107     flex->Add(txt, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP, 5);
108     txt = new wxStaticText(this, wxID_STATIC, _("Associated Logger"));
109     flex->Add(txt, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxTOP, 5);
110 
111     channel = new wxListBox(this, id_channel, wxDefaultPosition, wxDefaultSize, channelStrings, wxLB_SINGLE );
112     logger  = new wxListBox(this, id_logger,  wxDefaultPosition, wxDefaultSize, loggerStrings,  wxLB_SINGLE );
113     flex->Add(channel, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
114     flex->Add(logger,  0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
115 
116     channel->SetSelection(0);
117     logger->SetSelection(0);
118 
119     // spacer
120     flex->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);
121 
122     filename = new wxTextCtrl(this, id_text);
123     flex->Add(filename, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
124 
125     SetSizer(flex);
126 }
127 
128 
129 
GetConfigurationPanel(wxWindow * parent)130 cbConfigurationPanel* LogHacker::GetConfigurationPanel(wxWindow* parent)
131 {
132     CfgPanel *p = new CfgPanel();
133     p->Create(parent);
134     return p;
135 }
136 
137 
138 
Change()139 void CfgPanel::Change()
140 {
141     wxString c = channel->GetStringSelection();
142     wxString l = logger->GetStringSelection();
143     filename->Enable(LogManager::Get()->FilenameRequired(l));
144 
145     if (l.at(0) == _T('<'))  // "<application default>"
146         l.Empty();
147 
148 //  blah... do something, update the maps according to what is selected
149 }
150 
151 
OnApply()152 void CfgPanel::OnApply()
153 {
154     LogManager *m = LogManager::Get();
155 
156     for (ConfigManagerContainer::StringToStringMap::iterator i = c2l.begin(); i != c2l.end(); ++i)
157     {
158         if (!!i->second.IsEmpty())
159         {
160             int slot = 0;
161             while (slot <= LogManager::max_logs && m->Slot(slot).title != i->second)
162                 ++slot;
163 
164             m->SetLog(m->New(i->second), slot);
165         }
166     }
167 
168     // since we use ConfigManagerContainer::StringToStringMap, we could store whatever settings we have like:
169     //   ConfigManager::Get()->Write(_T("/loghacker/c2l"), c2l);
170     //   ConfigManager::Get()->Write(_T("/loghacker/c2f"), c2f);
171 }
172