NCBI C++ ToolKit
wx_utils.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_WX___UTILS__HPP
2 #define GUI_WIDGETS_WX___UTILS__HPP
3 
4 /* $Id: wx_utils.hpp 46860 2021-11-10 22:54:31Z asztalos $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  * A temporary place for various GUI utitlities
33  */
34 
35 #include <gui/gui.hpp>
36 
38 
39 // This header must (at least indirectly) precede any wxWidgets headers.
41 
42 #include <wx/artprov.h>
43 #include <wx/colour.h>
44 #include <wx/utils.h>
45 #include <wx/arrstr.h>
46 #include <wx/event.h>
47 #include <wx/string.h>
48 
49 #include <util/unicode.hpp>
50 
51 
52 class wxMenu;
53 class wxMenuItem;
54 class wxString;
55 class wxMenuBar;
56 class wxRect;
57 class wxWindow;
58 class wxDC;
59 class wxColour;
60 class wxListCtrl;
61 class wxTopLevelWindow;
62 
63 class wxFileArtProvider;
64 class wxImageList;
65 
66 #ifdef __WXMSW__
67  #define wxTR_GBENCH_LINES wxTR_LINES_AT_ROOT
68 #else
69  #define wxTR_GBENCH_LINES wxTR_NO_LINES
70 #endif
71 
73 
74 struct SMenuItemRec;
75 class CMenuItem;
76 class CRgbaColor;
77 
78 
79 NCBI_GUIWIDGETS_WX_EXPORT wxString FindExeFile(const wxString& exeFile);
80 
82 
83 NCBI_GUIWIDGETS_WX_EXPORT void ReportIDError(const string& id_label, bool is_local, const string& title = "Error message");
84 
85 NCBI_GUIWIDGETS_WX_EXPORT void OpenFileBrowser(const wxString& path);
86 
87 NCBI_GUIWIDGETS_WX_EXPORT wxString GetAbsolutePath(const wxString& localpath);
88 
89 class CChecksum;
91 
92 /// Returns id of video configuration to be used for saving settings
94 
95 NCBI_GUIWIDGETS_WX_EXPORT //bool InitDefaultFileArtProvider(wxFileArtProvider* provider);
96  void InitDefaultFileArtProvider(const wxString& dir);
98 
99 // Copy "in" to "out"
100 NCBI_GUIWIDGETS_WX_EXPORT void FromArrayString(const wxArrayString& in, vector<string>& out);
101 NCBI_GUIWIDGETS_WX_EXPORT void ToArrayString(const vector<string>& out, wxArrayString& in);
102 
103 
104 /// Creates a wxMenu object replicating the structure of CMenuItem
105 NCBI_GUIWIDGETS_WX_EXPORT wxMenu* CreateMenu(const CMenuItem* item);
106 
107 /// Find a subitem of the given menu by text
108 NCBI_GUIWIDGETS_WX_EXPORT wxMenuItem* FindSubItem(wxMenu& menu, const wxString& text);
109 
110 /// merges all items form menu_2 into menu_1, preserving the structure if possible
111 NCBI_GUIWIDGETS_WX_EXPORT void Merge(wxMenu& menu_1, const wxMenu& menu_2);
112 
113 /// Removes extra separators (in the begining or at the end of the menu, ot those
114 /// that precede other separators)
116 
117 /// create a copy of the given menu
118 NCBI_GUIWIDGETS_WX_EXPORT wxMenu* CloneMenu(const wxMenu& menu);
119 
120 /// Using default menu item margin width.
121 /// The is a work-around for a wxWidget bug on failing to reset to default
122 /// margin width after using any user-owned bitmap image in menu item.
123 /// The failure will cause the default radio menu item bitmap image disappear.
125 
126 /// Set margin width for menu item.
127 NCBI_GUIWIDGETS_WX_EXPORT void SetMenuItemMarginWidth(wxMenuItem* item, wxBitmap * bmp);
128 
130 {
131 public:
132  CCommandToFocusHandler( wxWindow* window = NULL )
133  : m_Window( window )
134  , mf_Reentry( false )
135  {
136  }
137 
138 protected:
139  void OnCommandEvent( wxCommandEvent& event );
140  void OnMenuEvent( wxCommandEvent& event );
141  void OnUpdateUIEvent( wxUpdateUIEvent& event );
142 
143 private:
144  wxWindow* m_Window;
146 
147  DECLARE_EVENT_TABLE()
148 };
149 
150 class CBoolGuard {
151 public:
152  CBoolGuard( bool& flag ) : m_Flag( flag ) { m_Flag = true; }
153  ~CBoolGuard() { m_Flag = false; }
154 
155  operator bool() { return m_Flag; }
156 
157 private:
158  bool& m_Flag;
159 };
160 
161 inline string ToStdString(const wxString& s)
162 {
163  return string(s.ToAscii());
164 }
165 
166 inline string ToAsciiStdString(const wxString& input)
167 {
168  const wxScopedCharBuffer line(input.ToUTF8());
169  string output = utf8::UTF8ToAsciiString(line, (const utf8::SUnicodeTranslation*) NULL);
170  return output;
171 }
172 
173 inline wxString ToWxString(const string& s)
174 {
175  string clean(s);
176  std::replace_if(clean.begin(), clean.end(),
177  [](const char& c) -> bool { return c < 0 || c > 127; }, '?');
178  return wxString::FromAscii(clean.c_str());
179 }
180 
181 inline wxString ToWxString(const char* s, size_t len)
182 {
183  string clean(s, len);
184  std::replace_if(clean.begin(), clean.end(),
185  [](const char& c) -> bool { return c < 0 || c > 127; }, '?');
186  return wxString::FromAscii(clean.c_str());
187 }
188 
189 inline ostream& operator <<( ostream& os, const wxCharBuffer& buff )
190 {
191  return os << buff.data();
192 }
193 
194 inline const CNcbiDiag& operator <<( const CNcbiDiag& diag, const wxString& line )
195 {
196  return diag << line.ToUTF8().data();
197 }
198 
199 // Conversion functions to save filenames to ASN
200 NCBI_GUIWIDGETS_WX_EXPORT wxString FnToWxString(const string& s);
201 NCBI_GUIWIDGETS_WX_EXPORT string FnToStdString(const wxString& s);
202 
203 /// converts an absolute path to a relative one based on current workspace
204 /// directory
205 NCBI_GUIWIDGETS_WX_EXPORT wxString ToRelativePath(const wxString& base, const wxString& abs_path);
206 /// converts relative path from the current workspace directory to an
207 /// absolute path
208 NCBI_GUIWIDGETS_WX_EXPORT wxString ToAbsolutePath(const wxString& base, const wxString& rel_path);
209 
210 NCBI_GUIWIDGETS_WX_EXPORT string ToString(const wxRect& rc);
211 
212 // get window bounds in screen coordinates
213 NCBI_GUIWIDGETS_WX_EXPORT wxRect GetScreenRect(const wxWindow& win);
214 
215 /// Windows specific function, equivalent of SetWindowText() WinAPI
216 NCBI_GUIWIDGETS_WX_EXPORT void WindowSetText(const wxWindow& win, const wxChar* text);
217 
218 
220  wxColour GetAverage(const wxColor& c1, const wxColor& c2, double ratio);
221 
222 /// Truncate options for drawing text with FLTK API
224  ewxTruncate_Empty, /// truncate text if needed
225  ewxTruncate_Ellipsis, /// truncate text if needed, add "..." if truncated
226  ewxTruncate_EllipsisAlways /// truncate text if needed, add "..." even if not truncated
227 };
228 
229 
230 /// returns number of characters representing a truncated version of "s"
231 /// which fits in the gievn width "w"
232 /// "trunc" controls truncation, if "trunc" == eTruncate_Ellipsis
233 /// "..." is appended to the end of the truncated string.
235 int TruncTextLength(wxDC& dc, const wxString& s, int w,
237 
238 
239 /// truncates given string so that its length is less or equal "w"
240 /// "trunc" controls truncation, if "trunc" == eTruncate_Ellipsis
241 /// "..." is appended to the end of the truncated string.
243 wxString TruncateText(wxDC& dc, const wxString& s, int w,
245 
246 /// divides given "text" into lines, so that every line has width less or equal
247 /// to "w". Returns length of lines in "line_lens".
249  void WrapText(wxDC& dc, const string& text, int w, vector<int>& line_lens);
250 
251 ///////////////////////////////////////////////////////////////////////////////
252 /// CwxSplittingArtProvider - an adapter for old-style image aliases
254  public wxArtProvider
255 {
256  virtual wxBitmap CreateBitmap(const wxArtID& id,
257  const wxArtClient& client,
258  const wxSize& size);
259 };
260 
261 
263 
265 
267  bool NcbiChooseColor(wxWindow* parent, CRgbaColor& color);
268 
270  void SaveWindowRectToRegistry(const wxRect& rc, CRegistryWriteView view);
271 
273  void LoadWindowRectFromRegistry(wxRect& rc, const CRegistryReadView& view);
274 
276  void CorrectWindowRect(wxTopLevelWindow* win, wxRect& rc);
277 
279  wxWindow* FindChildWindowById(long id, wxWindow* parent);
280 
281 /// Sends command event with id 'cmd_id' to window and all its children
283  void BroadcastCommandToChildWindows(wxWindow* window, int cmd_id, int cmd_data=0);
284 
285 /// For OSX Cocoa, puts child window in front of (visually) parent
287  void AddChildWindowAbove(wxFrame* parent, wxWindow* child);
288 /// For OSX Cocoa, puts child window behind the parent
290  void AddChildWindowBelow(wxFrame* parent, wxWindow* child);
291 /// For OSX Cocoa, removes child window connection (for layering) to parent
293  void RemoveChildWindow(wxFrame* parent, wxWindow* child);
294 /// Returns window boundaries for mac (top menubar width and max height)
296  void GetMacDragWindowBounds(wxTopLevelWindow* win, int& menubar_height, int& maxy, int &maxx);
297 /// For mac, return an ID for the current space (of "Spaces" fame)
299  bool GetMacOptionKeyDown();
300 /// Fix a problem on windows where after a dialog overlays an opengl window the opengl
301 /// window may be constrained to just the area under the dialog until some window-changing event.
302 /// Problem only occurs if Aero theme is Not being used.
304  bool DlgGLWinOverlayFix(wxWindow* win);
305 /// Mac always seems to return 72 for screen PPI so use wx function for non-mac and on mac
306 /// divide the displays number of pixels by its physical size to get actual PPI
308  wxSize GetDisplayPPI();
309 
311 bool RunningInsideNCBI();
312 
313 //
314 // This class can be used with wxListCtrl
315 // In case the current platform uses wxGenericListCtrl
316 // CFixGenericListCtrl will allow wxChildFocusEvent to propagate up the windows hierarchy.
317 // In case the current platform doesn't use wxGenericControl this class does nothing.
318 // This partially fixes pane activation issue. GB-726
319 //
320 
322 {
323 public:
324  CFixGenericListCtrl() : m_ListCtrl(0) {}
325  void ConnectToControl(wxListCtrl& listCtrl);
326 
327 private:
328  void OnChildFocus(wxCommandEvent& event);
329 
330  wxListCtrl* m_ListCtrl;
331 };
332 
333 NCBI_GUIWIDGETS_WX_EXPORT wxImageList* CreateCheckboxImages(wxWindow* wnd);
334 
336 
337 #endif // GUI_WIDGETS_WX___UTILS__HPP
static int trunc
Definition: array_out.c:8
#define false
Definition: bool.h:36
#define bool
Definition: bool.h:34
bool & m_Flag
Definition: wx_utils.hpp:158
CBoolGuard(bool &flag)
Definition: wx_utils.hpp:152
CChecksum – Checksum calculator.
Definition: checksum.hpp:302
CCommandToFocusHandler(wxWindow *window=NULL)
Definition: wx_utils.hpp:132
wxListCtrl * m_ListCtrl
Definition: wx_utils.hpp:330
CMenuItem - represents a menu items in IMenu-style menus.
Definition: menu_item.hpp:53
CNcbiDiag –.
Definition: ncbidiag.hpp:924
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
CwxSplittingArtProvider - an adapter for old-style image aliases.
Definition: wx_utils.hpp:255
std::ofstream out("events_result.xml")
main entry point for tests
Workaround for wxWidgets header errors in certain configurations; MUST be included (at least indirect...
thread_local unique_ptr< FtaMsgPost > bmp
Definition: ftaerr.cpp:120
string
Definition: cgiapp.hpp:687
#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
#define NCBI_GUIWIDGETS_WX_EXPORT
Definition: gui_export.h:543
string UTF8ToAsciiString(const char *src, const SUnicodeTranslation *default_translation, const TUnicodeTable *table=NULL, EConversionResult *result=NULL)
Convert UTF8 into ASCII string.
Definition: unicode.cpp:526
n background color
static int input()
int len
static void text(MDB_val *v)
Definition: mdb_dump.c:62
const struct ncbi::grid::netcache::search::fields::SIZE size
std::istream & in(std::istream &in_, double &x_)
static SQLCHAR output[256]
Definition: print.c:5
static CNamedPipeClient * client
SMenuItemRec.
Definition: menu_item.hpp:229
void SetMenuItemMarginWidth(wxMenuItem *item, wxBitmap *bmp)
Set margin width for menu item.
Definition: wx_utils.cpp:709
void OpenFileBrowser(const wxString &path)
Definition: wx_utils.cpp:129
void ReportIDError(const string &id_label, bool is_local, const string &title="Error message")
Definition: wx_utils.cpp:99
void ReportMemoryUsage()
Definition: wx_utils.cpp:116
void GetMacDragWindowBounds(wxTopLevelWindow *win, int &menubar_height, int &maxy, int &maxx)
Returns window boundaries for mac (top menubar width and max height)
Definition: wx_utils.cpp:1239
wxColour ConvertColor(const CRgbaColor &color)
Definition: wx_utils.cpp:997
wxMenu * CreateMenu(const CMenuItem *item)
Creates a wxMenu object replicating the structure of CMenuItem.
Definition: wx_utils.cpp:365
wxString ToRelativePath(const wxString &base, const wxString &abs_path)
converts an absolute path to a relative one based on current workspace directory
Definition: wx_utils.cpp:281
void LoadWindowRectFromRegistry(wxRect &rc, const CRegistryReadView &view)
Definition: wx_utils.cpp:1018
void InitDefaultFileArtProvider(const wxString &dir)
Definition: wx_utils.cpp:323
wxString GetAbsolutePath(const wxString &localpath)
Definition: wx_utils.cpp:188
wxFileArtProvider * GetDefaultFileArtProvider()
Definition: wx_utils.cpp:334
void UseDefaultMarginWidth(wxMenu &menu)
Using default menu item margin width.
Definition: wx_utils.cpp:693
int TruncTextLength(wxDC &dc, const wxString &s, int w, Ewx_Truncate trunc=ewxTruncate_Ellipsis)
returns number of characters representing a truncated version of "s" which fits in the gievn width "w...
Definition: wx_utils.cpp:820
void FromArrayString(const wxArrayString &in, vector< string > &out)
Definition: wx_utils.cpp:343
void Merge(wxMenu &menu_1, const wxMenu &menu_2)
merges all items form menu_2 into menu_1, preserving the structure if possible
Definition: wx_utils.cpp:579
void RemoveChildWindow(wxFrame *parent, wxWindow *child)
For OSX Cocoa, removes child window connection (for layering) to parent.
Definition: wx_utils.cpp:1224
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
void AddChildWindowBelow(wxFrame *parent, wxWindow *child)
For OSX Cocoa, puts child window behind the parent.
Definition: wx_utils.cpp:1209
string GetVideoId()
Returns id of video configuration to be used for saving settings.
Definition: wx_utils.cpp:224
bool RunningInsideNCBI()
Definition: wx_utils.cpp:1335
void BroadcastCommandToChildWindows(wxWindow *window, int cmd_id, int cmd_data=0)
Sends command event with id 'cmd_id' to window and all its children.
Definition: wx_utils.cpp:1145
string GetMD5Digest(const CChecksum &cs)
Definition: wx_utils.cpp:211
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
void ToArrayString(const vector< string > &out, wxArrayString &in)
Definition: wx_utils.cpp:353
wxString FindExeFile(const wxString &exeFile)
Definition: wx_utils.cpp:76
string ToString(const wxRect &rc)
Definition: wx_utils.cpp:773
wxString FnToWxString(const string &s)
Definition: wx_utils.cpp:253
void CorrectWindowRect(wxTopLevelWindow *win, wxRect &rc)
Definition: wx_utils.cpp:1026
wxString ToAbsolutePath(const wxString &base, const wxString &rel_path)
converts relative path from the current workspace directory to an absolute path
Definition: wx_utils.cpp:301
ostream & operator<<(ostream &os, const wxCharBuffer &buff)
Definition: wx_utils.hpp:189
wxRect GetScreenRect(const wxWindow &win)
Definition: wx_utils.cpp:783
string FnToStdString(const wxString &s)
Definition: wx_utils.cpp:268
wxString TruncateText(wxDC &dc, const wxString &s, int w, Ewx_Truncate trunc=ewxTruncate_Ellipsis)
truncates given string so that its length is less or equal "w" "trunc" controls truncation,...
Definition: wx_utils.cpp:878
void WindowSetText(const wxWindow &win, const wxChar *text)
Windows specific function, equivalent of SetWindowText() WinAPI.
Definition: wx_utils.cpp:794
Ewx_Truncate
Truncate options for drawing text with FLTK API.
Definition: wx_utils.hpp:223
@ ewxTruncate_Empty
Definition: wx_utils.hpp:224
@ ewxTruncate_Ellipsis
truncate text if needed
Definition: wx_utils.hpp:225
@ ewxTruncate_EllipsisAlways
truncate text if needed, add "..." if truncated
Definition: wx_utils.hpp:226
wxImageList * CreateCheckboxImages(wxWindow *wnd)
Definition: wx_utils.cpp:1352
void WrapText(wxDC &dc, const string &text, int w, vector< int > &line_lens)
divides given "text" into lines, so that every line has width less or equal to "w".
Definition: wx_utils.cpp:895
wxMenu * CloneMenu(const wxMenu &menu)
create a copy of the given menu
Definition: wx_utils.cpp:559
wxMenuItem * FindSubItem(wxMenu &menu, const wxString &text)
Find a subitem of the given menu by text.
Definition: wx_utils.cpp:426
bool NcbiChooseColor(wxWindow *parent, CRgbaColor &color)
Definition: wx_utils.cpp:1114
wxColour GetAverage(const wxColor &c1, const wxColor &c2, double ratio)
Definition: wx_utils.cpp:810
void AddChildWindowAbove(wxFrame *parent, wxWindow *child)
For OSX Cocoa, puts child window in front of (visually) parent.
Definition: wx_utils.cpp:1194
bool DlgGLWinOverlayFix(wxWindow *win)
Fix a problem on windows where after a dialog overlays an opengl window the opengl window may be cons...
Definition: wx_utils.cpp:1290
string ToAsciiStdString(const wxString &input)
Definition: wx_utils.hpp:166
bool GetMacOptionKeyDown()
For mac, return an ID for the current space (of "Spaces" fame)
Definition: wx_utils.cpp:1274
wxSize GetDisplayPPI()
Mac always seems to return 72 for screen PPI so use wx function for non-mac and on mac divide the dis...
Definition: wx_utils.cpp:1309
wxWindow * FindChildWindowById(long id, wxWindow *parent)
Definition: wx_utils.cpp:1130
void SaveWindowRectToRegistry(const wxRect &rc, CRegistryWriteView view)
Definition: wx_utils.cpp:1009
void CleanupSeparators(wxMenu &menu)
Removes extra separators (in the begining or at the end of the menu, ot those that precede other sepa...
Definition: wx_utils.cpp:668
Modified on Tue Dec 05 02:22:02 2023 by modify_doxy.py rev. 669887