28#include <wx/graphics.h>
32#define SCROLL_SENSIBILITY 20
37CustomGrid::CustomGrid(wxWindow* parent, wxWindowID
id,
const wxPoint& pos,
38 const wxSize& size,
long style,
const wxString& name)
39 : wxGrid(parent, id, pos, size, style, name) {
41 SetTable(
new wxGridStringTable(0, 0),
true, wxGridSelectRows);
44 EnableGridLines(
true);
45 EnableDragGridSize(
false);
47 EnableDragColMove(
false);
48 EnableDragColSize(
false);
49 EnableDragRowSize(
false);
53 pConf->SetPath(_T(
"/Settings/GRIB"));
54 m_IsDigit = pConf->Read(_T(
"GribDataTableRowPref"), _T(
"XXX"));
56 if (m_IsDigit.Len() != wxString(_T(
"XXX")).Len()) m_IsDigit = _T(
"XXX");
58 for (
unsigned int i = 0; i < m_IsDigit.Len(); i++) {
59 m_NumRow.push_back(wxNOT_FOUND);
60 m_NumRowVal.push_back(std::vector<double>());
64 SetLabelFont(labelfont);
65 wxColour colour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
66 if (colour.Red() > 128) {
67 GetGlobalColor(_T(
"DILG0"), &colour);
68 GetGlobalColor(_T(
"GREEN1"), &m_greenColour);
69 GetGlobalColor(_T(
"DILG1"), &m_greyColour);
71 GetGlobalColor(_T(
"GREEN2"), &m_greenColour);
72 m_greyColour = colour;
74 SetLabelBackgroundColour(colour);
77 GetTextExtent(_T(
"Ab"), &w,
nullptr, 0, 0, &labelfont);
78 double x = (double)w * 6.5;
79 SetRowLabelSize((
int)x);
86 Connect(wxEVT_SCROLLWIN_THUMBTRACK,
87 wxScrollEventHandler(CustomGrid::OnScroll),
nullptr,
this);
88 Connect(wxEVT_SIZE, wxSizeEventHandler(CustomGrid::OnResize),
nullptr,
this);
89 Connect(wxEVT_GRID_LABEL_LEFT_CLICK,
90 wxGridEventHandler(CustomGrid::OnLabeClick),
nullptr,
this);
92 GetGridWindow()->Connect(wxEVT_LEFT_DOWN,
93 wxMouseEventHandler(CustomGrid::OnMouseEvent),
95 GetGridWindow()->Connect(wxEVT_LEFT_UP,
96 wxMouseEventHandler(CustomGrid::OnMouseEvent),
98 GetGridWindow()->Connect(wxEVT_MOTION,
99 wxMouseEventHandler(CustomGrid::OnMouseEvent),
102 m_tRefreshTimer.Connect(wxEVT_TIMER,
103 wxTimerEventHandler(CustomGrid::OnRefreshTimer),
107CustomGrid::~CustomGrid() {
110 pConf->SetPath(_T (
"/Settings/GRIB" ));
111 pConf->Write(_T (
"GribDataTableRowPref" ), m_IsDigit);
117void CustomGrid::DrawColLabel(wxDC& dc,
int col) {
119 dc.SetFont(m_labelFont);
120 if (col == m_gParent->m_pIndex) {
121 dc.SetBrush(wxBrush(m_greenColour, wxBRUSHSTYLE_SOLID));
122 dc.SetPen(wxPen(m_greenColour, 1));
124 dc.SetBrush(wxBrush(m_labelBackgroundColour, wxBRUSHSTYLE_SOLID));
125 dc.SetPen(wxPen(m_labelBackgroundColour, 1));
128 wxRect tRect(GetColLeft(col), 1, GetColWidth(col) - 2, m_colLabelHeight - 2);
129 dc.DrawRectangle(tRect);
131 dc.SetPen(GetDefaultGridLinePen());
132 dc.DrawLine(GetColLeft(col) - 1, 0, GetColRight(col), 0);
134 if (col > -1 && (col == 0 || GetColLabelValue(col).BeforeFirst(
' ') !=
135 GetColLabelValue(col - 1).BeforeFirst(
' ')))
136 dc.SetPen(wxPen(*wxBLACK, 4));
137 dc.DrawLine(GetColLeft(col) - 1, 0, GetColLeft(col) - 1, m_colLabelHeight);
138 if (col == m_numCols - 1) {
139 dc.SetPen(wxPen(*wxBLACK, 4));
140 dc.DrawLine(GetColRight(col), 0, GetColRight(col), m_colLabelHeight);
143 dc.DrawLabel(GetColLabelValue(col), tRect,
144 wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
147void CustomGrid::DrawRowLabel(wxDC& dc,
int row) {
149 dc.SetFont(m_labelFont);
150 dc.SetPen(GetDefaultGridLinePen());
151 dc.SetBrush(wxBrush(m_labelBackgroundColour, wxBRUSHSTYLE_SOLID));
152 int w = dc.GetTextExtent(_T(
"Speed")).x;
153 wxString label1, label2;
154 label1 = GetRowLabelValue(row).BeforeFirst(
',', &label2);
157 if (GetNumberRows() > row + 2 &&
158 label1 == GetRowLabelValue(row + 2).BeforeFirst(
',')) {
160 if (IsRowVisible(row + 2)) label1 = _T(
" ");
163 else if (GetNumberRows() > row + 1 &&
164 label1 == GetRowLabelValue(row + 1).BeforeFirst(
',')) {
167 label1 == GetRowLabelValue(row - 1).BeforeFirst(
',')) {
168 if (!IsRowVisible(row + 1)) label1 = _T(
" ");
172 else if (row > 1 && label1 == GetRowLabelValue(row - 2).BeforeFirst(
',')) {
173 if (IsRowVisible(row - 1)) label1 = _T(
" ");
176 else if (row > 0 && label1 == GetRowLabelValue(row - 1).BeforeFirst(
',')) {
177 if (IsRowVisible(row - 1)) label1 = _T(
" ");
180 wxRect aRect(5, GetRowTop(row), m_rowLabelWidth - w, GetRowHeight(row));
181 dc.DrawLabel(label1, aRect, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
183 wxRect bRect(m_rowLabelWidth - w, GetRowTop(row), w, GetRowHeight(row));
184 dc.SetFont(wxFont(m_labelFont).Scale(0.85));
185 dc.DrawLabel(label2, bRect, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
188 dc.DrawLine(0, GetRowBottom(row) - 1, m_rowLabelWidth,
189 GetRowBottom(row) - 1);
190 dc.DrawLine(0, GetRowTop(row), 0, GetRowBottom(row));
191 dc.DrawLine(m_rowLabelWidth - 1, GetRowTop(row), m_rowLabelWidth - 1,
195void CustomGrid::DrawCornerLabel(wxDC& dc) {
196 dc.SetPen(GetDefaultGridLinePen());
197 dc.SetBrush(wxBrush(m_labelBackgroundColour, wxBRUSHSTYLE_SOLID));
198 wxRect rect(0, 0, m_rowLabelWidth, m_colLabelHeight);
199 dc.DrawRectangle(rect);
201 double hc = m_colLabelHeight;
202 double hb = wxBitmap(now).GetHeight();
203 double scfac = ((hc / hb) * 4) / 4;
204 wxBitmap bmp = m_gParent->GetScaledBitmap(wxBitmap(now), _T(
"now"), scfac);
206 int x = (m_rowLabelWidth - bmp.GetWidth()) / 2;
207 int y = (m_colLabelHeight == bmp.GetHeight())
209 : wxMax(0, (m_colLabelHeight - bmp.GetHeight()) / 2);
210 dc.DrawBitmap(bmp, x, y);
213void CustomGrid::OnScroll(wxScrollEvent& event) {
214 m_tRefreshTimer.Start(10, wxTIMER_ONE_SHOT);
218void CustomGrid::OnRefreshTimer(wxTimerEvent& event) { ForceRefresh(); }
220void CustomGrid::OnResize(wxSizeEvent& event) {
221 m_tRefreshTimer.Start(10, wxTIMER_ONE_SHOT);
225void CustomGrid::OnLabeClick(wxGridEvent& event) {
226 int row =
event.GetRow();
227 int col =
event.GetCol();
228 wxPoint p =
event.GetPosition();
230 if (row == wxNOT_FOUND && event.GetCol() == wxNOT_FOUND) {
232 int frow = 0, fcol = 0;
233 GetFirstVisibleCell(frow, fcol);
235 MakeCellVisible(frow, m_numCols - 1);
236 MakeCellVisible(frow, m_gParent->m_pIndex);
237 }
else if (row != wxNOT_FOUND && col == wxNOT_FOUND) {
238 int idx = GetRowIndex(row);
239 if (idx != wxNOT_FOUND) {
240 if (m_IsDigit.GetChar(idx) ==
'X')
241 m_IsDigit.SetChar(idx,
'.');
243 m_IsDigit.SetChar(idx,
'X');
244 for (
int c = 0; c < m_numCols; c++) {
245 double value = m_NumRowVal[idx][c];
248 if (idx == R_CURRENT && m_IsDigit.GetChar(idx) ==
'X' &&
249 value != GRIB_NOTDEF) {
251 if (value >= 360) value -= 360;
252 if (value < 0) value += 360;
255 row, c,
new CustomRenderer(value, m_IsDigit.GetChar(idx) ==
'X'));
257 m_tRefreshTimer.Start(10, wxTIMER_ONE_SHOT);
262int CustomGrid::GetRowIndex(
int row) {
263 int idx = wxNOT_FOUND;
264 for (
unsigned int i = 0; i < m_NumRow.size(); i++) {
265 if (m_NumRow[i] == row) idx = i;
270void CustomGrid::SetNumericalRow(
int row,
int col,
int datatype,
double value) {
271 m_NumRow[datatype] = row;
272 m_NumRowVal[datatype].push_back(value);
275 if (datatype == R_CURRENT && m_IsDigit.GetChar(datatype) ==
'X' &&
276 value != GRIB_NOTDEF) {
278 if (value >= 360) value -= 360;
279 if (value < 0) value += 360;
282 row, col,
new CustomRenderer(value, m_IsDigit.GetChar(datatype) ==
'X'));
285void CustomGrid::OnMouseEvent(wxMouseEvent& event) {
286 static wxPoint s_pevt;
287 wxPoint pevt =
event.GetPosition();
289 if (!m_bLeftDown && event.LeftIsDown()) {
292 }
else if (m_bLeftDown && !event.LeftIsDown()) {
294 if (HasCapture()) ReleaseMouse();
297 if (event.LeftDown()) s_pevt = pevt;
298 if (event.LeftUp()) {
299 if (HasCapture()) ReleaseMouse();
302 if (event.Dragging()) {
303 int frow, fcol, lrow, lcol;
304 GetFirstVisibleCell(frow, fcol);
305 GetLastVisibleCell(lrow, lcol);
306 if (pevt != s_pevt) {
308 int diff = pevt.x - s_pevt.x;
310 if (diff > SCROLL_SENSIBILITY) {
313 MakeCellVisible(frow, fcol - 1);
318 else if (-diff > SCROLL_SENSIBILITY) {
320 if (lcol < m_numCols - 1) {
321 MakeCellVisible(frow, lcol + 1);
326 diff = pevt.y - s_pevt.y;
327 if (diff > SCROLL_SENSIBILITY) {
330 MakeCellVisible(frow - 1, fcol);
335 else if (-diff > SCROLL_SENSIBILITY) {
337 if (lrow < m_numRows - 1) {
338 MakeCellVisible(lrow + 1, fcol);
339 MakeCellVisible(frow + 1,
345 if (rfh) m_tRefreshTimer.Start(10, wxTIMER_ONE_SHOT);
350bool CustomGrid::IsRowVisible(
int row) {
351 for (
int i = 0; i < m_numCols; i++) {
352 if (IsVisible(row, i,
false))
return true;
358void CustomGrid::GetFirstVisibleCell(
int& frow,
int& fcol) {
361 for (fcol = 0; fcol < m_numCols; fcol++) {
362 for (frow = 0; frow < m_numRows; frow++) {
363 if (IsVisible(frow, fcol)) {
373void CustomGrid::GetLastVisibleCell(
int& lrow,
int& lcol) {
375 lrow = wxMax(m_numRows - 1, 0);
376 for (lcol = wxMax(m_numCols - 1, 0); lcol > -1; lcol--) {
377 for (lrow = m_numRows - 1; lrow > -1; lrow--) {
378 if (IsVisible(lrow, lcol)) {
390void CustomRenderer::Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
391 const wxRect& rect,
int row,
int col,
393 dc.SetPen(wxPen(attr.GetBackgroundColour(), 1));
394 dc.SetBrush(wxBrush(attr.GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
395 dc.DrawRectangle(rect);
396 if (m_IsDigit || m_dDir == GRIB_NOTDEF) {
397 wxString text(wxEmptyString);
398 if (m_dDir != GRIB_NOTDEF) text.Printf(_T(
"%03d%c"), (
int)m_dDir, 0x00B0);
399 dc.DrawLabel(text, rect,
400 wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
402 double si = sin((m_dDir - 90) * M_PI / 180.);
403 double co = cos((m_dDir - 90) * M_PI / 180.);
405 int i = rect.GetTopLeft().x + (rect.GetWidth() / 2);
406 int j = rect.GetTopLeft().y + (rect.GetHeight() / 2);
408 int arrowSize = rect.GetHeight() - 3;
409 int dec = -arrowSize / 2;
411#if wxUSE_GRAPHICS_CONTEXT
412 wxGraphicsContext* gdc;
413 wxClientDC* cdc =
new wxClientDC(
dynamic_cast<wxWindow*
>(&grid));
414 cdc =
dynamic_cast<wxClientDC*
>(&dc);
416 gdc = wxGraphicsContext::Create(*cdc);
423 for (
int c = 0; c < grid.GetNumberCols(); c++) {
424 for (r = 0; r < grid.GetNumberRows(); r++) {
425 if (grid.IsVisible(r, c)) {
427 i -= (c * grid.GetColSize(0));
428 j -= (r * grid.GetRowHeight(0));
435 gdc->SetPen(wxPen(attr.GetTextColour(), 3));
436 gdc->SetBrush(wxBrush(attr.GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
438 double ii, jj, kk, ll;
439 GetArrowsPoints(si, co, i, j, dec, 0, dec + arrowSize, 0, ii, jj, kk, ll);
440 gdc->StrokeLine(ii, jj, kk, ll);
441 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, 3, ii, jj, kk, ll);
442 gdc->StrokeLine(ii, jj, kk, ll);
443 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, -3, ii, jj, kk, ll);
444 gdc->StrokeLine(ii, jj, kk, ll);
449 dc.SetPen(wxPen(attr.GetTextColour(), 3));
450 double ii, jj, kk, ll;
451 GetArrowsPoints(si, co, i, j, dec, 0, dec + arrowSize, 0, ii, jj, kk, ll);
452 dc.DrawLine((
int)ii, (
int)jj, (
int)kk, (
int)ll);
453 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, 3, ii, jj, kk, ll);
454 dc.DrawLine((
int)ii, (
int)jj, (
int)kk, (
int)ll);
455 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, -3, ii, jj, kk, ll);
456 dc.DrawLine((
int)ii, (
int)jj, (
int)kk, (
int)ll);
461void CustomRenderer::GetArrowsPoints(
double si,
double co,
int di,
int dj,
462 int i,
int j,
int k,
int l,
double& ii,
463 double& jj,
double& kk,
double& ll) {
464 ii = (i * co - j * si + 0.5) + di;
465 jj = (i * si + j * co + 0.5) + dj;
466 kk = (k * co - l * si + 0.5) + di;
467 ll = (k * si + l * co + 0.5) + dj;
Specialized Grid Control for GRIB Data Display.
GRIB Data Table View and Export Interface.
wxFileConfig * GetOCPNConfigObject()
Gets OpenCPN's configuration object.
wxFont GetOCPNGUIScaledFont_PlugIn(wxString item)
Gets a uniquely scaled font copy for responsive UI elements.
OpenGL Platform Abstraction Layer.