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(
"/Settings/GRIB");
54 m_IsDigit = pConf->Read(
"GribDataTableRowPref",
"XXX");
56 if (m_IsDigit.Len() != wxString(
"XXX").Len()) m_IsDigit =
"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(
"DILG0", &colour);
68 GetGlobalColor(
"GREEN1", &m_greenColour);
69 GetGlobalColor(
"DILG1", &m_greyColour);
71 GetGlobalColor(
"GREEN2", &m_greenColour);
72 m_greyColour = colour;
74 SetLabelBackgroundColour(colour);
77 GetTextExtent(
"Ab", &w,
nullptr,
nullptr,
nullptr, &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(
"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 =
" ";
163 else if (GetNumberRows() > row + 1 &&
164 label1 == GetRowLabelValue(row + 1).BeforeFirst(
',')) {
167 label1 == GetRowLabelValue(row - 1).BeforeFirst(
',')) {
168 if (!IsRowVisible(row + 1)) label1 =
" ";
172 else if (row > 1 && label1 == GetRowLabelValue(row - 2).BeforeFirst(
',')) {
173 if (IsRowVisible(row - 1)) label1 =
" ";
176 else if (row > 0 && label1 == GetRowLabelValue(row - 1).BeforeFirst(
',')) {
177 if (IsRowVisible(row - 1)) label1 =
" ";
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),
"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();
229 if (row == wxNOT_FOUND && event.GetCol() == wxNOT_FOUND) {
231 int frow = 0, fcol = 0;
232 GetFirstVisibleCell(frow, fcol);
234 MakeCellVisible(frow, m_numCols - 1);
235 MakeCellVisible(frow, m_gParent->m_pIndex);
236 }
else if (row != wxNOT_FOUND && col == wxNOT_FOUND) {
237 int idx = GetRowIndex(row);
238 if (idx != wxNOT_FOUND) {
239 if (m_IsDigit.GetChar(idx) ==
'X')
240 m_IsDigit.SetChar(idx,
'.');
242 m_IsDigit.SetChar(idx,
'X');
243 for (
int c = 0; c < m_numCols; c++) {
244 double value = m_NumRowVal[idx][c];
247 if (idx == R_CURRENT && m_IsDigit.GetChar(idx) ==
'X' &&
248 value != GRIB_NOTDEF) {
250 if (value >= 360) value -= 360;
251 if (value < 0) value += 360;
254 row, c,
new CustomRenderer(value, m_IsDigit.GetChar(idx) ==
'X'));
256 m_tRefreshTimer.Start(10, wxTIMER_ONE_SHOT);
261int CustomGrid::GetRowIndex(
int row) {
262 int idx = wxNOT_FOUND;
263 for (
unsigned i = 0; i < m_NumRow.size(); i++) {
264 if (m_NumRow[i] == row) idx =
static_cast<int>(i);
269void CustomGrid::SetNumericalRow(
int row,
int col,
int datatype,
double value) {
270 m_NumRow[datatype] = row;
271 m_NumRowVal[datatype].push_back(value);
274 if (datatype == R_CURRENT && m_IsDigit.GetChar(datatype) ==
'X' &&
275 value != GRIB_NOTDEF) {
277 if (value >= 360) value -= 360;
278 if (value < 0) value += 360;
281 row, col,
new CustomRenderer(value, m_IsDigit.GetChar(datatype) ==
'X'));
284void CustomGrid::OnMouseEvent(wxMouseEvent& event) {
285 static wxPoint s_pevt;
286 wxPoint pevt =
event.GetPosition();
288 if (!m_bLeftDown && event.LeftIsDown()) {
291 }
else if (m_bLeftDown && !event.LeftIsDown()) {
293 if (HasCapture()) ReleaseMouse();
296 if (event.LeftDown()) s_pevt = pevt;
297 if (event.LeftUp()) {
298 if (HasCapture()) ReleaseMouse();
301 if (event.Dragging()) {
302 int frow, fcol, lrow, lcol;
303 GetFirstVisibleCell(frow, fcol);
304 GetLastVisibleCell(lrow, lcol);
305 if (pevt != s_pevt) {
307 int diff = pevt.x - s_pevt.x;
309 if (diff > SCROLL_SENSIBILITY) {
312 MakeCellVisible(frow, fcol - 1);
317 else if (-diff > SCROLL_SENSIBILITY) {
319 if (lcol < m_numCols - 1) {
320 MakeCellVisible(frow, lcol + 1);
325 diff = pevt.y - s_pevt.y;
326 if (diff > SCROLL_SENSIBILITY) {
329 MakeCellVisible(frow - 1, fcol);
334 else if (-diff > SCROLL_SENSIBILITY) {
336 if (lrow < m_numRows - 1) {
337 MakeCellVisible(lrow + 1, fcol);
338 MakeCellVisible(frow + 1,
344 if (rfh) m_tRefreshTimer.Start(10, wxTIMER_ONE_SHOT);
349bool CustomGrid::IsRowVisible(
int row) {
350 for (
int i = 0; i < m_numCols; i++) {
351 if (IsVisible(row, i,
false))
return true;
357void CustomGrid::GetFirstVisibleCell(
int& frow,
int& fcol) {
360 for (fcol = 0; fcol < m_numCols; fcol++) {
361 for (frow = 0; frow < m_numRows; frow++) {
362 if (IsVisible(frow, fcol)) {
372void CustomGrid::GetLastVisibleCell(
int& lrow,
int& lcol) {
374 lrow = wxMax(m_numRows - 1, 0);
375 for (lcol = wxMax(m_numCols - 1, 0); lcol > -1; lcol--) {
376 for (lrow = m_numRows - 1; lrow > -1; lrow--) {
377 if (IsVisible(lrow, lcol)) {
389void CustomRenderer::Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
390 const wxRect& rect,
int row,
int col,
392 dc.SetPen(wxPen(attr.GetBackgroundColour(), 1));
393 dc.SetBrush(wxBrush(attr.GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
394 dc.DrawRectangle(rect);
395 if (m_IsDigit || m_dDir == GRIB_NOTDEF) {
396 wxString text(wxEmptyString);
397 if (m_dDir != GRIB_NOTDEF) text.Printf(
"%03d%c", (
int)m_dDir, 0x00B0);
398 dc.DrawLabel(text, rect,
399 wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
401 double si = sin((m_dDir - 90) * M_PI / 180.);
402 double co = cos((m_dDir - 90) * M_PI / 180.);
404 int i = rect.GetTopLeft().x + (rect.GetWidth() / 2);
405 int j = rect.GetTopLeft().y + (rect.GetHeight() / 2);
407 int arrowSize = rect.GetHeight() - 3;
408 int dec = -arrowSize / 2;
410#if wxUSE_GRAPHICS_CONTEXT
411 wxGraphicsContext* gdc;
412 auto* cdc =
new wxClientDC(
dynamic_cast<wxWindow*
>(&grid));
413 cdc =
dynamic_cast<wxClientDC*
>(&dc);
415 gdc = wxGraphicsContext::Create(*cdc);
422 for (
int c = 0; c < grid.GetNumberCols(); c++) {
423 for (r = 0; r < grid.GetNumberRows(); r++) {
424 if (grid.IsVisible(r, c)) {
426 i -= (c * grid.GetColSize(0));
427 j -= (r * grid.GetRowHeight(0));
434 gdc->SetPen(wxPen(attr.GetTextColour(), 3));
435 gdc->SetBrush(wxBrush(attr.GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
437 double ii, jj, kk, ll;
438 GetArrowsPoints(si, co, i, j, dec, 0, dec + arrowSize, 0, ii, jj, kk, ll);
439 gdc->StrokeLine(ii, jj, kk, ll);
440 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, 3, ii, jj, kk, ll);
441 gdc->StrokeLine(ii, jj, kk, ll);
442 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, -3, ii, jj, kk, ll);
443 gdc->StrokeLine(ii, jj, kk, ll);
448 dc.SetPen(wxPen(attr.GetTextColour(), 3));
449 double ii, jj, kk, ll;
450 GetArrowsPoints(si, co, i, j, dec, 0, dec + arrowSize, 0, ii, jj, kk, ll);
451 dc.DrawLine((
int)ii, (
int)jj, (
int)kk, (
int)ll);
452 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, 3, ii, jj, kk, ll);
453 dc.DrawLine((
int)ii, (
int)jj, (
int)kk, (
int)ll);
454 GetArrowsPoints(si, co, i, j, dec - 3, 0, dec + 5, -3, ii, jj, kk, ll);
455 dc.DrawLine((
int)ii, (
int)jj, (
int)kk, (
int)ll);
460void CustomRenderer::GetArrowsPoints(
double si,
double co,
int di,
int dj,
461 int i,
int j,
int k,
int l,
double& ii,
462 double& jj,
double& kk,
double& ll) {
463 ii = (i * co - j * si + 0.5) + di;
464 jj = (i * si + j * co + 0.5) + dj;
465 kk = (k * co - l * si + 0.5) + di;
466 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.