| 1 | /* Copyright 2026 Maxim Iorsh <[email protected]> |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | #pragma once |
| 28 | |
| 29 | #include <functional> |
| 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include <gtkmm.h> |
| 34 | |
| 35 | namespace ff::widgets { |
| 36 | |
| 37 | class Histogram : public Gtk::DrawingArea { |
| 38 | public: |
| 39 | Histogram(); |
| 40 | |
| 41 | void set_values(const std::vector<int>& values); |
| 42 | void set_bar_width(int width_px); |
| 43 | void set_moving_average_window(int window_size); |
| 44 | void set_lower_bound(int lower_bound); |
| 45 | |
| 46 | // int bar_index argument is the actual bar X-value, not the index in the |
| 47 | // bars vector. |
| 48 | void set_tooltip_text_callback( |
| 49 | std::function<std::string(int bar_index, double average)> |
| 50 | tooltip_text_callback); |
| 51 | void set_bar_click_callback( |
| 52 | std::function<void(int bar_index, bool shift_pressed)> |
| 53 | bar_click_callback); |
| 54 | |
| 55 | protected: |
| 56 | bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override; |
| 57 | |
| 58 | private: |
| 59 | // Retrieve the actual bar X-value, which may be negative. |
| 60 | bool get_bar_index(int x, int& index) const; |
| 61 | |
| 62 | bool on_button_press_event(GdkEventButton* event); |
| 63 | bool on_query_tooltip_event(int x, int y, bool keyboard_tooltip, |
| 64 | const Glib::RefPtr<Gtk::Tooltip>& tooltip); |
| 65 | void update_size_request(); |
| 66 | double draw_axis(const Cairo::RefPtr<Cairo::Context>& cr, int width, |
| 67 | int height); |
| 68 | void draw_axis_tick(const Cairo::RefPtr<Cairo::Context>& cr, double axis_y, |
| 69 | int index); |
| 70 | void draw_bars(const Cairo::RefPtr<Cairo::Context>& cr, double bar_base); |
| 71 | void draw_moving_average(const Cairo::RefPtr<Cairo::Context>& cr, |
| 72 | double bar_base); |
| 73 | |
| 74 | std::vector<int> values_; |
| 75 | std::vector<double> avg_values_; |
| 76 | int bar_width_px_ = 10; |
| 77 | int moving_average_window_ = 1; |
| 78 | int lower_bound_ = 0; |
| 79 | std::function<std::string(int bar_index, double average)> tooltip_text_cb_; |
| 80 | std::function<void(int bar_index, bool shift_pressed)> bar_click_cb_; |
| 81 | }; |
| 82 | |
| 83 | } // namespace ff::widgets |