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

Go to the SVN repository for this file.

1 /* $Id: non_ascii_replacement_dlg.cpp 47479 2023-05-02 13:24:02Z ucko $
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: Igor Filippov
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
35 
36 
38 
39 
40 
41 
42 static wxString GetContext(const wxString &str, int pos)
43 {
44  if (str.Length() <= 20)
45  return str;
46  pos -= 10;
47  if (pos < 0)
48  pos = 0;
49  if (pos + 20 <= str.Length())
50  return str.Mid(pos,20);
51  return str.Mid(str.Length() - 20);
52 }
53 
54 static void TestWindowForNonAsciiText(wxWindow *win, map<TUnicodeSymbol, string> &nonAsciiChars, map<TUnicodeSymbol, set<wxString> > &nonAsciiContext)
55 {
56  if (!win)
57  return;
58  wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl *>(win);
59  if (text_ctrl)
60  {
61  wxString str = text_ctrl->GetValue();
62  int pos = 0;
63  for (wxString::const_iterator i = str.begin(); i != str.end(); ++i)
64  {
65  wxUniChar u = *i;
66  if (!u.IsAscii())
67  {
68  TUnicodeSymbol v = u.GetValue();
69  nonAsciiChars[v] = CDoiLookup::GetSpecialCharacterReplacement(v);
70  nonAsciiContext[v].insert(GetContext(str,pos));
71  }
72  pos++;
73  }
74  }
75  else
76  {
77  for (wxWindowList::iterator node = win->GetChildren().begin(); node != win->GetChildren().end(); ++node)
78  {
79  wxWindow *w = *node;
80  TestWindowForNonAsciiText(w, nonAsciiChars, nonAsciiContext);
81  }
82  }
83 }
84 
85 static void FixWindowForNonAsciiText(wxWindow *win, map<TUnicodeSymbol, string> &nonAsciiChars)
86 {
87  if (!win)
88  return;
89  wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl *>(win);
90  if (text_ctrl)
91  {
92  wxString str = text_ctrl->GetValue();
93  wxString new_str;
94  bool modified = false;
95  for (wxString::const_iterator i = str.begin(); i != str.end(); ++i)
96  {
97  wxUniChar u = *i;
98  if (!u.IsAscii())
99  {
100  TUnicodeSymbol v = u.GetValue();
101  string replacement = nonAsciiChars[v];
102  new_str << replacement;
103  modified = true;
104  }
105  else
106  {
107  new_str << u;
108  }
109  }
110  if (modified)
111  text_ctrl->SetValue(new_str);
112  }
113  else
114  {
115  for (wxWindowList::iterator node = win->GetChildren().begin(); node != win->GetChildren().end(); ++node)
116  {
117  wxWindow *w = *node;
118  FixWindowForNonAsciiText(w, nonAsciiChars);
119  }
120  }
121 }
122 
123 void TestForNonAsciiText(wxWindow *win)
124 {
125  map<TUnicodeSymbol, string> nonAsciiChars;
126  map<TUnicodeSymbol, set<wxString> > nonAsciiContext;
127 
128  TestWindowForNonAsciiText(win, nonAsciiChars, nonAsciiContext);
129  if (!nonAsciiChars.empty())
130  {
131  CNonAsciiCharacterReplacement dlg(NULL, nonAsciiChars, nonAsciiContext);
132  dlg.ShowModal();
133  nonAsciiChars = dlg.GetReplacementMap();
134  FixWindowForNonAsciiText(win, nonAsciiChars);
135  }
136 }
137 
138 
139 /*
140  * CNonAsciiCharacterReplacement type definition
141  */
142 
143 IMPLEMENT_DYNAMIC_CLASS( CNonAsciiCharacterReplacement, wxDialog )
144 
145 
146 /*
147  * CNonAsciiCharacterReplacement event table definition
148  */
149 
150 BEGIN_EVENT_TABLE( CNonAsciiCharacterReplacement, wxDialog )
151 
152 ////@begin CNonAsciiCharacterReplacement event table entries
153 ////@end CNonAsciiCharacterReplacement event table entries
154 
156 
157 
158 /*
159  * CNonAsciiCharacterReplacement constructors
160  */
161 
163 {
164  Init();
165 }
166 
168  wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
169  : m_NonAsciiChars(nonAsciiChars), m_NonAsciiContext(nonAsciiContext)
170 {
171  Init();
172  Create(parent, id, caption, pos, size, style);
173 }
174 
175 
176 /*
177  * CNonAsciiCharacterReplacement creator
178  */
179 
180 bool CNonAsciiCharacterReplacement::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
181 {
182 ////@begin CNonAsciiCharacterReplacement creation
183  SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
184  wxDialog::Create( parent, id, caption, pos, size, style );
185 
186  CreateControls();
187  if (GetSizer())
188  {
189  GetSizer()->SetSizeHints(this);
190  }
191  Centre();
192 ////@end CNonAsciiCharacterReplacement creation
193  return true;
194 }
195 
196 
197 /*
198  * CNonAsciiCharacterReplacement destructor
199  */
200 
202 {
203 ////@begin CNonAsciiCharacterReplacement destruction
204 ////@end CNonAsciiCharacterReplacement destruction
205 }
206 
207 
208 /*
209  * Member initialisation
210  */
211 
213 {
214 ////@begin CNonAsciiCharacterReplacement member initialisation
216 ////@end CNonAsciiCharacterReplacement member initialisation
217 }
218 
219 
220 /*
221  * Control creation for CNonAsciiCharacterReplacement
222  */
223 
225 {
226 ////@begin CNonAsciiCharacterReplacement content construction
227  // Generated by DialogBlocks, 20/01/2016 17:14:51 (unregistered)
228 
229  CNonAsciiCharacterReplacement* itemDialog1 = this;
230 
231  wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
232  itemDialog1->SetSizer(itemBoxSizer2);
233 
234  wxStaticText* itemStaticText3 = new wxStaticText( itemDialog1, wxID_STATIC, _("You may not include special characters in the text.If you do not choose replacement characters, these special characters will be replaced with '#'."), wxDefaultPosition, wxDefaultSize, 0 );
235  itemStaticText3->Wrap(270);
236  itemBoxSizer2->Add(itemStaticText3, 0, wxALIGN_LEFT|wxALL, 5);
237 
238  wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
239  itemBoxSizer2->Add(itemBoxSizer4, 0, 0, 0);
240 
241  wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Character"), wxDefaultPosition, wxDefaultSize, 0 );
242  itemBoxSizer4->Add(itemStaticText5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
243 
244  wxStaticText* itemStaticText6 = new wxStaticText( itemDialog1, wxID_STATIC, _("Replacement"), wxDefaultPosition, wxDefaultSize, 0 );
245  itemBoxSizer4->Add(itemStaticText6, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
246 
247  wxStaticText* itemStaticText7 = new wxStaticText( itemDialog1, wxID_STATIC, _("Contexts"), wxDefaultPosition, wxDefaultSize, 0 );
248  itemBoxSizer4->Add(itemStaticText7, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
249 
250  m_ScrolledWindow = new wxScrolledWindow( itemDialog1, ID_SCROLLEDWINDOW, wxDefaultPosition, wxSize(300, 100), wxSUNKEN_BORDER|wxVSCROLL );
251  itemBoxSizer2->Add(m_ScrolledWindow, 0, wxGROW|wxALL, 5);
252  m_ScrolledWindow->SetScrollbars(0, 1, 0, 0);
253  wxBoxSizer *scrollSizer = new wxBoxSizer(wxVERTICAL);
254  m_ScrolledWindow->SetSizer(scrollSizer);
255 
257  {
258  CNonAsciiReplacementPanel *panel = new CNonAsciiReplacementPanel(m_ScrolledWindow, wxUniChar(i->first), i->second, m_NonAsciiContext[i->first]);
259  scrollSizer->Add(panel, 0, wxALIGN_LEFT, 0);
260  }
261 
262  m_ScrolledWindow->FitInside();
263 
264  wxButton* itemButton10 = new wxButton( itemDialog1, wxID_OK, _("Replace"), wxDefaultPosition, wxDefaultSize, 0 );
265  itemBoxSizer2->Add(itemButton10, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
266 
267 ////@end CNonAsciiCharacterReplacement content construction
268 }
269 
270 
272 {
274  return m_NonAsciiChars;
275 }
276 
278 {
279  if (m_ScrolledWindow)
280  {
282  for (wxWindowList::iterator node = m_ScrolledWindow->GetChildren().begin(); node != m_ScrolledWindow->GetChildren().end(); ++node)
283  {
284  wxWindow *w = *node;
285  CNonAsciiReplacementPanel *panel = dynamic_cast<CNonAsciiReplacementPanel*>(w);
286  if (panel)
287  {
288  pair<TUnicodeSymbol, string> p = panel->GetReplacement();
289  m_NonAsciiChars[p.first] = p.second;
290  }
291  }
292  }
293  return wxDialog::TransferDataFromWindow();
294 }
295 
296 /*
297  * Should we show tooltips?
298  */
299 
301 {
302  return true;
303 }
304 
305 /*
306  * Get bitmap resources
307  */
308 
309 wxBitmap CNonAsciiCharacterReplacement::GetBitmapResource( const wxString& name )
310 {
311  // Bitmap retrieval
312 ////@begin CNonAsciiCharacterReplacement bitmap retrieval
313  wxUnusedVar(name);
314  return wxNullBitmap;
315 ////@end CNonAsciiCharacterReplacement bitmap retrieval
316 }
317 
318 /*
319  * Get icon resources
320  */
321 
323 {
324  // Icon retrieval
325 ////@begin CNonAsciiCharacterReplacement icon retrieval
326  wxUnusedVar(name);
327  return wxNullIcon;
328 ////@end CNonAsciiCharacterReplacement icon retrieval
329 }
330 
331 
332 
333 /*
334  * CNonAsciiReplacementPanel type definition
335  */
336 
338 
339 
340 /*
341  * CNonAsciiReplacementPanel event table definition
342  */
343 
344 BEGIN_EVENT_TABLE( CNonAsciiReplacementPanel, wxPanel )
345 
346 ////@begin CNonAsciiReplacementPanel event table entries
347 ////@end CNonAsciiReplacementPanel event table entries
348 
350 
351 
352 /*
353  * CNonAsciiReplacementPanel constructors
354  */
355 
357 {
358  Init();
359 }
360 
361 CNonAsciiReplacementPanel::CNonAsciiReplacementPanel( wxWindow* parent, wxUniChar orig, const string &replacement, const set<wxString> &contexts,
362  wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
363  : m_Orig(orig), m_ReplacementStr(wxString(replacement)), m_Contexts(contexts)
364 {
365  Init();
366  Create( parent, id, pos, size, style );
367 }
368 
369 
370 /*
371  * CNonAsciiReplacementPanel creator
372  */
373 
374 bool CNonAsciiReplacementPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
375 {
376 ////@begin CNonAsciiReplacementPanel creation
377  wxPanel::Create( parent, id, pos, size, style );
378 
379  CreateControls();
380  Centre();
381 ////@end CNonAsciiReplacementPanel creation
382  return true;
383 }
384 
385 
386 /*
387  * CNonAsciiReplacementPanel destructor
388  */
389 
391 {
392 ////@begin CNonAsciiReplacementPanel destruction
393 ////@end CNonAsciiReplacementPanel destruction
394 }
395 
396 
397 /*
398  * Member initialisation
399  */
400 
402 {
403 ////@begin CNonAsciiReplacementPanel member initialisation
404  m_Char = NULL;
406 ////@end CNonAsciiReplacementPanel member initialisation
407 }
408 
409 
410 /*
411  * Control creation for CNonAsciiReplacementPanel
412  */
413 
415 {
416 ////@begin CNonAsciiReplacementPanel content construction
417  // Generated by DialogBlocks, 20/01/2016 17:26:08 (unregistered)
418 
419  CNonAsciiReplacementPanel* itemPanel2 = this;
420 
421  wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
422  itemPanel2->SetSizer(itemBoxSizer3);
423 
424  m_Char = new wxStaticText( itemPanel2, wxID_STATIC, m_Orig, wxDefaultPosition, wxSize(30,-1), 0 );
425  itemBoxSizer3->Add(m_Char, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxLEFT|wxTOP, 5);
426 
427  m_Replacement = new wxTextCtrl( itemPanel2, wxID_ANY, m_ReplacementStr, wxDefaultPosition, wxDefaultSize, 0 );
428  itemBoxSizer3->Add(m_Replacement, 0, wxALIGN_TOP|wxRIGHT|wxTOP, 5);
429 
430  wxArrayString array_context;
431  for (set<wxString>::const_iterator i = m_Contexts.begin(); i != m_Contexts.end(); ++i)
432  array_context.Add(*i);
433  wxChoice* itemChoice6 = new wxChoice( itemPanel2, wxID_ANY, wxDefaultPosition, wxSize(120,-1), array_context, 0 );
434  itemBoxSizer3->Add(itemChoice6, 0, wxALIGN_TOP|wxRIGHT|wxTOP, 5);
435  itemChoice6->SetSelection(0);
436 ////@end CNonAsciiReplacementPanel content construction
437 }
438 
439 
440 /*
441  * Should we show tooltips?
442  */
443 
445 {
446  return true;
447 }
448 
449 /*
450  * Get bitmap resources
451  */
452 
453 wxBitmap CNonAsciiReplacementPanel::GetBitmapResource( const wxString& name )
454 {
455  // Bitmap retrieval
456 ////@begin CNonAsciiReplacementPanel bitmap retrieval
457  wxUnusedVar(name);
458  return wxNullBitmap;
459 ////@end CNonAsciiReplacementPanel bitmap retrieval
460 }
461 
462 /*
463  * Get icon resources
464  */
465 
466 wxIcon CNonAsciiReplacementPanel::GetIconResource( const wxString& name )
467 {
468  // Icon retrieval
469 ////@begin CNonAsciiReplacementPanel icon retrieval
470  wxUnusedVar(name);
471  return wxNullIcon;
472 ////@end CNonAsciiReplacementPanel icon retrieval
473 }
474 
475 pair<TUnicodeSymbol, string> CNonAsciiReplacementPanel::GetReplacement()
476 {
477  TUnicodeSymbol orig = m_Char->GetLabel().Last().GetValue();
478  string replacement = m_Replacement->GetValue().ToStdString();
479  pair<TUnicodeSymbol, string> result(orig, replacement);
480  return result;
481 }
482 
static string GetSpecialCharacterReplacement(TUnicodeSymbol ch)
Definition: doi_lookup.cpp:68
wxIcon GetIconResource(const wxString &name)
Retrieves icon resources.
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &caption=_("non-ascii character replacement"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(400, 300), long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxTAB_TRAVERSAL)
Creation.
static bool ShowToolTips()
Should we show tooltips?
map< TUnicodeSymbol, string > GetReplacementMap()
map< TUnicodeSymbol, string > m_NonAsciiChars
void Init()
Initialises member variables.
void CreateControls()
Creates the controls and sizers.
map< TUnicodeSymbol, set< wxString > > m_NonAsciiContext
wxBitmap GetBitmapResource(const wxString &name)
Retrieves bitmap resources.
void Init()
Initialises member variables.
void CreateControls()
Creates the controls and sizers.
static bool ShowToolTips()
Should we show tooltips?
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX)
pair< TUnicodeSymbol, string > GetReplacement()
wxBitmap GetBitmapResource(const wxString &name)
Retrieves bitmap resources.
wxIcon GetIconResource(const wxString &name)
Retrieves icon resources.
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
bool empty() const
Definition: map.hpp:149
void clear()
Definition: map.hpp:169
Definition: set.hpp:45
#define _(proto)
Definition: ct_nlmzip_i.h:78
static void Init(void)
Definition: cursor6.c:76
IMPLEMENT_CLASS(CFloatingFrame, CFloatingFrameBaseClass) const static long kFloatFrameStyle
CFloatingFrame.
#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
Uint4 TUnicodeSymbol
Unicode character.
Definition: ncbistr.hpp:141
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is orig
END_EVENT_TABLE()
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
void TestForNonAsciiText(wxWindow *win)
static wxString GetContext(const wxString &str, int pos)
static void FixWindowForNonAsciiText(wxWindow *win, map< TUnicodeSymbol, string > &nonAsciiChars)
static void TestWindowForNonAsciiText(wxWindow *win, map< TUnicodeSymbol, string > &nonAsciiChars, map< TUnicodeSymbol, set< wxString > > &nonAsciiContext)
#define ID_SCROLLEDWINDOW
static static static wxID_ANY
static const char * str(char *buf, int n)
Definition: stats.c:84
else result
Definition: token2.c:20
Modified on Thu Nov 30 04:54:28 2023 by modify_doxy.py rev. 669887