NCBI C++ ToolKit
generic_panel.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: generic_panel.cpp 43609 2019-08-08 16:12:53Z filippov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Colleen Bollin, Andrea Asztalos
27  */
28 
29 
30 #include <ncbi_pch.hpp>
31 
37 #include <wx/sizer.h>
38 #include <wx/filedlg.h>
39 #include <wx/bmpbuttn.h>
40 #include <wx/hyperlink.h>
42 
44 
45 /*!
46  * CGenericPanel type definition
47  */
48 
49  IMPLEMENT_DYNAMIC_CLASS(CGenericPanel, wxPanel)
50 
51 
52 /*!
53  * CGenericPanel event table definition
54  */
55 
56  BEGIN_EVENT_TABLE(CGenericPanel, wxPanel)
57  EVT_BUTTON(ID_GENERIC_REPORT_CLOSE, CGenericPanel::OnCloseButton)
58  EVT_BUTTON(ID_GEN_RPT_REFRESH, CGenericPanel::OnGenRptRefreshClick)
59  EVT_BUTTON(ID_GENERIC_REPORT_FIND, CGenericPanel::OnGenericReportFindClick)
60  EVT_BUTTON(ID_GENERIC_REPORT_CLOSE2, CGenericPanel::OnCloseButton)
61  EVT_BUTTON(ID_GEN_RPT_REFRESH2, CGenericPanel::OnGenRptRefreshClick2)
62  EVT_BUTTON(ID_GENERIC_REPORT_FIND2, CGenericPanel::OnGenericReportFindClick2)
63  EVT_BUTTON(ID_GENERIC_REPORT_EXPORT, CGenericPanel::OnGenericReportExportClick)
64  EVT_TEXT_ENTER(ID_GENERIC_REPORT_TEXT_FIND, CGenericPanel::OnGenericReportFindClick)
65  EVT_TEXT_ENTER(ID_GENERIC_REPORT_TEXT_FIND2, CGenericPanel::OnGenericReportFindClick2)
67 
68 
69 /*!
70  * CGenericPanel constructors
71  */
72 
74  : m_Workbench(0), m_Simple(false)
75 {
76  Init();
77 }
78 
79 CGenericPanel::CGenericPanel(wxWindow* parent, IWorkbench* workbench, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
80  : m_Workbench(workbench), m_Simple(false)
81 {
82  Init();
83  Create(parent, id, pos, size, style);
84 }
85 
86 
87 CGenericPanel::CGenericPanel(wxWindow* parent, bool simple, IWorkbench* workbench, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
88  : m_Workbench(workbench), m_Simple(simple)
89 {
90  Init();
91  Create(parent, id, pos, size, style);
92 }
93 
94 
95 /*!
96  * CGenericPanel creator
97  */
98 
99 bool CGenericPanel::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
100 {
101 ////@begin CGenericPanel creation
102  wxPanel::Create( parent, id, pos, size, style );
103  CreateControls();
104  if (GetSizer())
105  {
106  GetSizer()->SetSizeHints(this);
107  }
108  Centre();
109 ////@end CGenericPanel creation
110  return true;
111 }
112 
113 
114 /*!
115  * CGenericPanel destructor
116  */
117 
119 {
120  if (m_Refresh) {
121  delete m_Refresh;
122  }
123 }
124 
125 
126 /*!
127  * Member initialisation
128  */
129 
131 {
132  m_RTCtrl = NULL;
133  m_FindText = NULL;
134  m_RefreshBtn = NULL;
135  m_FindText2 = NULL;
137  m_Refresh = NULL;
138  m_PrevFindPos = 0;
139  m_PrevFindText = "";
140  m_move_to_top = false;
142  m_busy = false;
143  m_HelpButton = NULL;
144 }
145 
146 
147 /*!
148  * Control creation for CGenericPanel
149  */
150 
152 {
153  CGenericPanel* itemDialog1 = this;
154 
155  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
156  itemDialog1->SetSizer(itemBoxSizer2);
157 
158  wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
159  itemBoxSizer2->Add(itemBoxSizer5, 0, wxGROW|wxALL, 5);
160 
161  wxArrayString strings;
162  strings.Add(_("Match case"));
163  strings.Add(_("Do not match case"));
164  m_FindModeCtrl = new wxChoice(itemDialog1, wxID_ANY, wxDefaultPosition, wxDefaultSize, strings);
165  itemBoxSizer5->Add(m_FindModeCtrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
166  m_FindModeCtrl->SetSelection(1);
167 
168  m_FindText2 = new wxTextCtrl(itemDialog1, ID_GENERIC_REPORT_TEXT_FIND2, wxEmptyString, wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER);
169  itemBoxSizer5->Add(m_FindText2, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
170 
171  wxBitmapButton* itemButton9 = new wxBitmapButton(itemDialog1, ID_GENERIC_REPORT_FIND2, wxArtProvider::GetBitmap(wxT("text_panel::search")));
172  itemBoxSizer5->Add(itemButton9, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
173 
174  if (!m_Simple) {
175  m_RefreshBtn2 = new wxButton(itemDialog1, ID_GEN_RPT_REFRESH2, _("Refresh Top"), wxDefaultPosition, wxDefaultSize, 0);
176  itemBoxSizer5->Add(m_RefreshBtn2, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
177 
178  wxButton* itemButton10 = new wxButton(itemDialog1, ID_GENERIC_REPORT_CLOSE2, _("Close"), wxDefaultPosition, wxDefaultSize, 0);
179  itemBoxSizer5->Add(itemButton10, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
180 
181  itemBoxSizer5->AddStretchSpacer();
182  wxButton* itemButton11 = new wxButton(itemDialog1, ID_GENERIC_REPORT_EXPORT, _("Export..."), wxDefaultPosition, wxDefaultSize, 0);
183  itemBoxSizer5->Add(itemButton11, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
184  }
185 
186  m_RTCtrl = new CGenTextCtrl(itemDialog1, ID_RICHTEXTCTRL, wxEmptyString, wxDefaultPosition, wxSize(800, 400), wxWANTS_CHARS | wxTE_MULTILINE | wxTE_RICH | wxHSCROLL);
187  // m_RTCtrl->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Consolas")));
189  SetLineSpacing(5);
190  itemBoxSizer2->Add(m_RTCtrl, 1, wxGROW|wxALL, 5);
191 
192  if (!m_Simple) {
193  wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
194  itemBoxSizer2->Add(itemBoxSizer4, 0, wxGROW|wxALL, 5);
195 
196  m_FindText = new wxTextCtrl( itemDialog1, ID_GENERIC_REPORT_TEXT_FIND, wxEmptyString, wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER);
197  itemBoxSizer4->Add(m_FindText, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
198 
199  wxBitmapButton* itemButton5 = new wxBitmapButton(itemDialog1, ID_GENERIC_REPORT_FIND, wxArtProvider::GetBitmap(wxT("text_panel::search")));
200  itemBoxSizer4->Add(itemButton5, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
201 
202  m_RefreshBtn = new wxButton(itemDialog1, ID_GEN_RPT_REFRESH, _("Refresh"), wxDefaultPosition, wxDefaultSize, 0);
203  itemBoxSizer4->Add(m_RefreshBtn, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
204 
205  wxButton* itemButton8 = new wxButton(itemDialog1, ID_GENERIC_REPORT_CLOSE, _("Close"), wxDefaultPosition, wxDefaultSize, 0);
206  itemBoxSizer4->Add(itemButton8, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
207 
208  m_HelpButton = new wxHyperlinkCtrl( itemDialog1, wxID_HELP, _("Help"), wxEmptyString, wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE );
209  m_HelpButton->SetForegroundColour(wxColour(192, 192, 192));
210  itemBoxSizer4->Add(m_HelpButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
211 
212  m_RefreshBtn->Show(false);
213  m_RefreshBtn2->Show(false);
214  m_HelpButton->Hide();
215  }
216 }
217 
218 void CGenericPanel::SetHelpUrl(const wxString &url)
219 {
220  m_HelpButton->SetURL(url);
221  m_HelpButton->Show(!url.IsEmpty());
222 }
223 
225 {
226  wxFont font = CTextPanel::GetFont(CGuiRegistry::GetInstance().GetInt("GBENCH.TextView.FontSize", 1));
227  m_RTCtrl->SetInitialSize(wxSize(800*font.GetPointSize()/10,400));
228  m_RTCtrl->SetFont(font);
229 }
230 
231 
232 /*!
233  * Should we show tooltips?
234  */
235 
237 {
238  return true;
239 }
240 
241 /*!
242  * Get bitmap resources
243  */
244 
245 wxBitmap CGenericPanel::GetBitmapResource(const wxString& name)
246 {
247  wxUnusedVar(name);
248  return wxNullBitmap;
249 }
250 
251 /*!
252  * Get icon resources
253  */
254 
255 wxIcon CGenericPanel::GetIconResource(const wxString& name)
256 {
257  wxUnusedVar(name);
258  return wxNullIcon;
259 }
260 
261 
263 {
264  wxTextAttr attr(m_RTCtrl->GetDefaultStyle());
265  attr.SetLineSpacing(spacing);
266  m_RTCtrl->SetDefaultStyle(attr);
267 }
268 
269 
270 void CGenericPanel::SetText(const wxString& text)
271 {
273  Freeze();
274  m_RTCtrl->ChangeValue(text);
275  if (m_move_to_top)
276  {
278  m_move_to_top = false;
279  }
280  else
281  {
283  }
284  Thaw();
285  if (m_busy)
286  {
287  m_busy = false;
288  wxEndBusyCursor();
289  }
290 }
291 
292 
294 {
295  if (m_Refresh) {
296  delete m_Refresh;
297  }
298  m_Refresh = refresh;
299  if (refresh) {
300  m_RefreshBtn->Show(true);
301  m_RefreshBtn2->Show(true);
302  } else {
303  m_RefreshBtn->Show(false);
304  m_RefreshBtn2->Show(false);
305  }
306 }
307 
309 {
310  if (!m_busy)
311  {
312  m_busy = true;
313  wxBeginBusyCursor();
314  }
315 
316  if (m_Refresh) {
317  m_Refresh->RefreshText(this);
318  }
319 }
320 
321 
322 void CGenericPanel::OnGenRptRefreshClick(wxCommandEvent& event)
323 {
324  m_move_to_top = false;
325  RefreshData();
326 }
327 
328 void CGenericPanel::OnGenRptRefreshClick2(wxCommandEvent& event)
329 {
330  m_move_to_top = true;
331  RefreshData();
332 }
333 
334 void CGenericPanel::OnGenericReportFindClick(wxCommandEvent& event)
335 {
336  string find = ToStdString(m_FindText->GetValue());
337  if (NStr::IsBlank(find)) {
338  return;
339  }
340  string text = ToStdString(m_RTCtrl->GetValue());
341 
342  if (find != m_PrevFindText) {
343  // looking for new text
344  m_PrevFindPos = 0;
345  }
346 
347  long val = NStr::Find(text, find, static_cast<NStr::ECase>(m_FindModeCtrl->GetSelection()), NStr::eForwardSearch, m_PrevFindPos);
348  if (val == string::npos && m_PrevFindPos > 0) {
349  m_PrevFindPos = 0;
350  val = NStr::Find(text, find, static_cast<NStr::ECase>(m_FindModeCtrl->GetSelection()), NStr::eForwardSearch, m_PrevFindPos);
351  }
352 
353  if (val != string::npos) {
354  m_RTCtrl->SetSelection(val, val + find.length());
356  m_PrevFindPos++;
357  }
358 
359  m_PrevFindText = find;
360 }
361 
362 void CGenericPanel::OnGenericReportFindClick2(wxCommandEvent& event)
363 {
364  string find = ToStdString(m_FindText2->GetValue());
365  if (NStr::IsBlank(find)) {
366  return;
367  }
368  string text = ToStdString(m_RTCtrl->GetValue());
369 
370  if (find != m_PrevFindText) {
371  // looking for new text
372  m_PrevFindPos = 0;
373  }
374 
375  long val = NStr::Find(text, find, static_cast<NStr::ECase>(m_FindModeCtrl->GetSelection()), NStr::eForwardSearch, m_PrevFindPos);
376  if (val == string::npos && m_PrevFindPos > 0) {
377  m_PrevFindPos = 0;
378  val = NStr::Find(text, find, static_cast<NStr::ECase>(m_FindModeCtrl->GetSelection()), NStr::eForwardSearch, m_PrevFindPos);
379  }
380 
381  if (val != string::npos) {
382  m_RTCtrl->SetSelection(val, val + find.length());
384  m_PrevFindPos++;
385  }
386 
387  m_PrevFindText = find;
388 }
389 
390 
391 void CGenericPanel::OnCloseButton(wxCommandEvent& event)
392 {
393  GetParent()->Destroy();
394 }
395 
396 void CGenericPanel::OnGenericReportExportClick(wxCommandEvent& event)
397 {
398  wxFileDialog dlg(this, wxT("Select a file"), m_WorkDir, wxT(""),
400  wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
401 
402  if (dlg.ShowModal() != wxID_OK)
403  return;
404 
405  wxString path = dlg.GetPath();
406  if (!path.IsEmpty()) {
407  CNcbiOfstream os(path.fn_str(), ios::out);
408  os << m_RTCtrl->GetValue();
409  }
410 }
411 
412 BEGIN_EVENT_TABLE(CGenTextCtrl, wxTextCtrl)
413 EVT_ACTIVATE(CGenTextCtrl::OnActivate)
414 EVT_KILL_FOCUS(CGenTextCtrl::OnKillFocus)
415 EVT_SET_FOCUS(CGenTextCtrl::OnSetFocus)
416 EVT_CHILD_FOCUS(CGenTextCtrl::OnSetChildFocus)
417 EVT_SCROLLWIN(CGenTextCtrl::OnScroll)
418 EVT_SCROLLWIN_TOP(CGenTextCtrl::OnScrollWinTop)
419 EVT_IDLE(CGenTextCtrl::OnIdle)
421 
422 
423 void CGenTextCtrl::OnKillFocus(wxFocusEvent& event)
424 {
425  StorePosition();
426  event.Skip();
427 }
428 
429 void CGenTextCtrl::OnSetFocus(wxFocusEvent& event)
430 {
431  event.Skip();
432  m_safe_to_store = false;
433  CallAfter(&CGenTextCtrl::RestorePosition);
434 }
435 
436 void CGenTextCtrl::OnSetChildFocus(wxChildFocusEvent& event)
437 {
438  event.Skip();
439  m_safe_to_store = false;
440  CallAfter(&CGenTextCtrl::RestorePosition);
441 }
442 
443 void CGenTextCtrl::OnActivate(wxActivateEvent& event)
444 {
445  if (event.GetActive())
446  {
447  event.Skip();
448  m_safe_to_store = false;
449  CallAfter(&CGenTextCtrl::RestorePosition);
450  }
451  else
452  {
453  event.Skip();
454  }
455 }
456 
457 void CGenTextCtrl::OnScroll(wxScrollWinEvent& event)
458 {
459  event.Skip();
460  StorePosition();
461 }
462 
463 void CGenTextCtrl::OnIdle(wxIdleEvent&)
464 {
465  StorePosition();
466 }
467 
469 {
470  wxTextCtrl::ShowPosition(pos);
471  CallAfter(&CGenTextCtrl::StorePosition);
472 }
473 
474 void CGenTextCtrl::OnScrollWinTop(wxScrollWinEvent& event)
475 {
476  event.StopPropagation();
477 }
478 
480 {
481  wxTextCtrl::ShowPosition(m_scroll_pos);
482  m_safe_to_store = true;
483 }
484 
486 {
487  if (m_safe_to_store)
488  HitTest(GetClientRect().GetTopLeft(), &m_scroll_pos);
489 }
490 
492 
static wxString GetDialogFilter(EFileType fileType)
void OnKillFocus(wxFocusEvent &event)
void OnScrollWinTop(wxScrollWinEvent &event)
void OnScroll(wxScrollWinEvent &event)
void RestorePosition()
void StorePosition()
void ShowPosition(long pos)
void OnSetFocus(wxFocusEvent &event)
void OnActivate(wxActivateEvent &event)
void OnIdle(wxIdleEvent &)
void OnSetChildFocus(wxChildFocusEvent &event)
void OnGenericReportExportClick(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_GENERIC_REPORT_EXPORT
wxTextCtrl * m_FindText
void OnGenericReportFindClick(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_GENERIC_REPORT_FIND
void SetHelpUrl(const wxString &url)
wxTextCtrl * m_FindText2
bool Create(wxWindow *parent, wxWindowID id=ID_CGENERICPANEL, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxTAB_TRAVERSAL)
Creation.
void SetText(const wxString &text)
void OnGenRptRefreshClick2(wxCommandEvent &event)
void OnCloseButton(wxCommandEvent &event)
void SetFontAsTextPanel()
void OnGenericReportFindClick2(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_GENERIC_REPORT_FIND2
wxButton * m_RefreshBtn2
CGenTextCtrl * m_RTCtrl
~CGenericPanel()
Destructor.
void SetRefresh(IRefreshCntrl *refresh)
wxChoice * m_FindModeCtrl
void SetLineSpacing(int spacing)
static bool ShowToolTips()
Should we show tooltips?
void OnGenRptRefreshClick(wxCommandEvent &event)
wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_GEN_RPT_REFRESH
void CreateControls()
Creates the controls and sizers.
wxHyperlinkCtrl * m_HelpButton
wxButton * m_RefreshBtn
wxString m_WorkDir
wxIcon GetIconResource(const wxString &name)
Retrieves icon resources.
wxBitmap GetBitmapResource(const wxString &name)
Retrieves bitmap resources.
void Init()
Initialises member variables.
CGenericPanel()
Constructors.
IRefreshCntrl * m_Refresh
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
static wxFont GetFont(int size)
Definition: text_panel.cpp:174
virtual void RefreshText(CGenericPanel *text)=0
IWorkbench is the central interface in the application framework.
Definition: workbench.hpp:113
#define _(proto)
Definition: ct_nlmzip_i.h:78
std::ofstream out("events_result.xml")
main entry point for tests
#define false
Definition: bool.h:36
static void Init(void)
Definition: cursor6.c:76
static const char *const strings[]
Definition: utf8.c:21
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
static bool IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
static SIZE_TYPE Find(const CTempString str, const CTempString pattern, ECase use_case=eCase, EDirection direction=eForwardSearch, SIZE_TYPE occurrence=0)
Find the pattern in the string.
Definition: ncbistr.cpp:2891
ECase
Which type of string comparison.
Definition: ncbistr.hpp:1204
@ eForwardSearch
Search in a forward direction.
Definition: ncbistr.hpp:1946
END_EVENT_TABLE()
static void text(MDB_val *v)
Definition: mdb_dump.c:62
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
static static static wxID_ANY
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
Modified on Fri May 03 15:47:58 2024 by modify_doxy.py rev. 669887