OpenCPN Partial API docs
Loading...
Searching...
No Matches
std_icon.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2025 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/ *
16 **************************************************************************/
17
24#ifndef STD_ICON_H
25#define STD_ICON_H
26
27#include <string>
28
29#include <wx/bitmap.h>
30
31#include "observable_evtvar.h"
32
37class StdIcon {
38public:
46 StdIcon(const wxWindow* parent, const std::string& svg_file,
47 EventVar& color_change_evt, bool touch);
48
50 const wxBitmap& GetBitmap();
51
52private:
53 ObsListener m_color_change_lstnr;
54 EventVar& m_color_change_evt;
55 bool m_is_night;
56 wxBitmap m_day_bitmap;
57 wxBitmap m_night_bitmap;
58 wxBitmap& m_bitmap;
59};
60
61#endif // STD_ICON_H
62
69#include "model/svg_utils.h"
70#include "model/std_icon.h"
71
72static wxBitmap InvertColors(const wxBitmap& original) {
73 wxImage image = original.ConvertToImage();
74 unsigned char* data = image.GetData();
75 unsigned char* p_idata = data;
76 for (int i = 0; i < image.GetSize().y; i++) {
77 for (int j = 0; j < image.GetSize().x; j++) {
78 unsigned char v = *p_idata;
79 v = 255 - v;
80 *p_idata++ = v;
81 v = *p_idata;
82 v = 255 - v;
83 *p_idata++ = v;
84 v = *p_idata;
85 v = 255 - v;
86 *p_idata++ = v;
87 }
88 }
89 return wxBitmap(image);
90}
91
92StdIcon::StdIcon(const wxWindow* parent, const std::string& svg_file,
93 EventVar& color_change_evt, bool touch)
94 : m_color_change_evt(color_change_evt),
95 m_is_night(false),
96 m_day_bitmap(LoadSvgStdIcon(svg_file, parent, touch)),
97 m_night_bitmap(InvertColors(m_day_bitmap)),
98 m_bitmap(m_day_bitmap) {
99 m_color_change_lstnr.Init(m_color_change_evt, [&](const ObservedEvt& ev) {
100 m_bitmap = ev.GetInt() == 0 ? m_night_bitmap : m_day_bitmap;
101 });
102}
103
104const wxBitmap& StdIcon::GetBitmap() { return m_bitmap; }
Generic event handling between MVC Model and Controller based on a shared EventVar variable.
Define an action to be performed when a KeyProvider is notified.
Definition observable.h:257
void Init(const KeyProvider &kp, const std::function< void(ObservedEvt &ev)> &action)
Initiate an object yet not listening.
Definition observable.h:295
Custom event class for OpenCPN's notification system.
Small monochrome SVG icon scaled to the height of a char.
Definition std_icon.cpp:37
StdIcon(const wxWindow *parent, const std::string &svg_file, EventVar &color_change_evt, bool touch)
Create an icon.
Definition std_icon.cpp:92
const wxBitmap & GetBitmap()
Return bitmap, either day or inverted night variant.
Definition std_icon.cpp:104
A common variable shared between producer and consumer which supports Listen() and Notify().
Class StdIcon, typically a small monochrome svg icon.
wxBitmap LoadSvgStdIcon(const std::string &svg_file, const wxWindow *w, bool touch)
Load an svg icon from standard path, roughly scaled to the height of a char.
SVG utilities.