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

Go to the SVN repository for this file.

1 /* $Id: table_data_listctrl.cpp 32112 2015-01-02 14:35:27Z kuznets $
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: Bob Falk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 
37 #include <algorithm>
38 
40 
41 
42 static const long kDefStyle = wxLC_REPORT | wxLC_VIRTUAL | wxLC_HRULES | wxLC_VRULES;
43 
45  wxWindow* parent,
46  wxWindowID id,
47  const wxPoint& pos,
48  const wxSize& size,
49  long style,
50  const wxValidator& validator,
51  const wxString& name)
52  : wxListCtrl(
53  parent, id, pos, size,
54  (style & ~wxLC_MASK_TYPE) | kDefStyle, validator, name)
55  , m_ImageList(16, 16, TRUE)
56  , m_FontWidth(0)
57 {
58  // Image list adds some (blank) spacing to the left of the first column,
59  // at least on windows even thought I only want images in the column headers.
60  // Anyway, I only specify wxLC_ALIGN_LEFT for the list boxes that need those
61  // column images, so I know when I need to compensate for them in terms of
62  // column width.
63  if (style & wxLC_ALIGN_LEFT) {
65  provider->RegisterFileAlias(wxT("tablelist_import::checked"), wxT("check.png"));
66  provider->RegisterFileAlias(wxT("tablelist_import::skipped"), wxT("track_close.png"));
67 
68  SetImageList(&m_ImageList, wxIMAGE_LIST_SMALL);
69 
70  wxIcon ico = provider->GetIcon(wxT("tablelist_import::checked"));
71  if( ico.IsOk() ){
72  m_ImageList.Add(ico);
73  }
74 
75  ico = provider->GetIcon(wxT("tablelist_import::skipped"));
76  if( ico.IsOk() ){
77  m_ImageList.Add(ico);
78  }
79  }
80 }
81 
82 
83 // wxWidgets RTTI information
84 IMPLEMENT_DYNAMIC_CLASS( CTableDataListCtrl, wxListCtrl )
85 
86 BEGIN_EVENT_TABLE( CTableDataListCtrl, wxListCtrl )
87 
89 
90 
91 void CTableDataListCtrl::InitDataSource(ITableData& table_data)
92 {
93  m_TableData.Reset(&table_data);
94 
95  // Clear current list info
96  ClearAll();
97 
98  if (m_TableData.IsNull()) {
99  SetItemCount(0);
100  }
101  else {
102 
103  int num_rows = (int)m_TableData->GetRowsCount();
104 
105  // First column is a 1-based row number. Its width is based on the
106  // character length of num_rows, so compute that here:
107  //int first_column_char_width = (int)log10((double)num_rows+1) + 1;
108 
109  wxFont f = GetFont();
110  wxClientDC dc(this);
111  dc.SetFont(f);
112  m_FontWidth = dc.GetCharWidth();
113 
114  SetItemCount(num_rows);
115 
116  // Number of rows to review in order to determine a columns (approx) width
117  size_t width_calc_rows = std::min(num_rows, 100);
118  string s;
119 
120  for (size_t col=0; col<m_TableData->GetColsCount(); ++col) {
121  // Get column width in pixels
122  int width = 1;
123  for (size_t row=0; row<width_calc_rows; ++row) {
124  m_TableData->GetStringValue(row, col, s);
125  width = std::max(width, (int)s.length());
126  }
127 
128  // Supporting images messes up size of first column whether image
129  // is displayed or not. in any case, col1 nees to be a bit bigger
130  if (col == 0)
131  width += 2;
132 
133  // Get column width (and add 2 chars to make sure all the
134  // characters show)
135  width = (width+2) * m_FontWidth;
136 
137  // Add a new column or update existing column (if table already has a
138  // column col)
139  if (GetColumnCount() <= (int)col) {
140  InsertColumn(int(col),
141  wxString(ToWxString(m_TableData->GetColumnLabel(col))),
142  wxLIST_FORMAT_LEFT,
143  width);
144  }
145  else {
146  wxListItem item;
147  GetColumn(int(col), item);
148  item.SetWidth(width);
149  item.SetText(ToWxString(m_TableData->GetColumnLabel(col)));
150  SetColumn(int(col), item);
151  }
152  }
153 
154  }
155 
156  // After updating, list may need to add a horizontal scroll bar. Before
157  // adding ScrollList() (in windows) it would not add the horizontal scroll
158  // until I resized the window or manually scrolled vertically. That's why
159  // the ScrollList() hack was added. I also tried SendSizeEvent() but that
160  // didn't work.
161  ScrollList(1, 1);
162  ScrollList(-1, -1);
163  Refresh();
164 }
165 
167 {
168  /*
169  if (!m_ImportedTableData.IsNull()) {
170  if ((size_t)col < m_ImportedTableData->GetColumns().size()) {
171  if (m_ImportedTableData->GetColumns()[col].GetSkipped())
172  return 1;
173  else if (m_ImportedTableData->GetColumns()[col].GetIsCurrent())
174  return 0;
175  }
176  }
177  */
178 
179  return -1;
180 }
181 
183 {
184  // On windows, replace all tabs with 4 spaces to fake the tabs.
185  // On other platforms wxListCtrl shows tabs so there is no need
186  // to do this.
187 #ifdef __WXMSW__
188  wxString tabstr(" ", 1);
189  wxString result;
190  for (size_t i=0; i<str.size(); ++i) {
191  if (str[i] != '\t') {
192  result.append(wxUniChar(str[i]));
193  }
194  else result.append(tabstr);
195  }
196 
197  return result;
198 #else
199  return ToWxString(str);
200 #endif
201 }
202 
203 
204 wxString CTableDataListCtrl::OnGetItemText( long row, long col ) const
205 {
206  if (!m_TableData.IsNull()) {
207  string value;
208  m_TableData->GetStringValue(row, col, value);
209  return ToWxString(value);
210  }
211 
212  return wxString("");
213 }
214 
215 
217 
CTableDataListCtrl -.
CIRef< ITableData > m_TableData
Data table to be rendered in the list.
wxImageList m_ImageList
Holds column icons (shows whether column currently selected or not)
wxString OnGetItemText(long row, long col) const
Get the requested field.
int GetColumnImageID(int col) const
Returns image ID for column headers.
static wxString x_ToWxStringWithTabs(const string &str)
Convert strings with tab characters to wxStrings with 4 spaces for each tab.
virtual void RegisterFileAlias(const wxArtID &anId, const wxArtClient &aClient, const wxSize &aSize, const wxString &aName, long aType=wxBITMAP_TYPE_ANY, int anIndex=-1)
static const char * str(char *buf, int n)
Definition: stats.c:84
static FILE * f
Definition: readconf.c:23
bool IsNull(void) const THROWS_NONE
Check if pointer is null – same effect as Empty().
Definition: ncbiobj.hpp:735
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
END_EVENT_TABLE()
int i
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define TRUE
bool replacment for C indicating true.
Definition: ncbi_std.h:97
T max(T x_, T y_)
T min(T x_, T y_)
#define row(bind, expected)
Definition: string_bind.c:73
static const long kDefStyle
else result
Definition: token2.c:20
wxFileArtProvider * GetDefaultFileArtProvider()
Definition: wx_utils.cpp:334
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Fri Sep 20 14:57:53 2024 by modify_doxy.py rev. 669887