| 1 | // Aseprite UI Library |
| 2 | // Copyright (C) 2021 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2018 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef UI_TOOLTIPS_H_INCLUDED |
| 9 | #define UI_TOOLTIPS_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "ui/base.h" |
| 13 | #include "ui/popup_window.h" |
| 14 | #include "ui/timer.h" |
| 15 | #include "ui/window.h" |
| 16 | |
| 17 | #include <map> |
| 18 | #include <memory> |
| 19 | |
| 20 | namespace ui { |
| 21 | |
| 22 | class Display; |
| 23 | class TextBox; |
| 24 | class TipWindow; |
| 25 | |
| 26 | class TooltipManager : public Widget { |
| 27 | public: |
| 28 | TooltipManager(); |
| 29 | ~TooltipManager(); |
| 30 | |
| 31 | void addTooltipFor(Widget* widget, const std::string& text, int arrowAlign = 0); |
| 32 | void removeTooltipFor(Widget* widget); |
| 33 | |
| 34 | protected: |
| 35 | bool onProcessMessage(Message* msg) override; |
| 36 | void onInitTheme(InitThemeEvent& ev) override; |
| 37 | |
| 38 | private: |
| 39 | void onTick(); |
| 40 | |
| 41 | struct TipInfo { |
| 42 | std::string text; |
| 43 | int arrowAlign; |
| 44 | |
| 45 | TipInfo() { } |
| 46 | TipInfo(const std::string& text, int arrowAlign) |
| 47 | : text(text), arrowAlign(arrowAlign) { |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | typedef std::map<Widget*, TipInfo> Tips; |
| 52 | Tips m_tips; // All tips. |
| 53 | std::unique_ptr<TipWindow> m_tipWindow; // Frame to show tooltips. |
| 54 | std::unique_ptr<Timer> m_timer; // Timer to control the tooltip delay. |
| 55 | struct { |
| 56 | Widget* widget; |
| 57 | TipInfo tipInfo; |
| 58 | } m_target; |
| 59 | }; |
| 60 | |
| 61 | class TipWindow : public PopupWindow { |
| 62 | public: |
| 63 | TipWindow(const std::string& text = "" ); |
| 64 | |
| 65 | Style* arrowStyle() { return m_arrowStyle; } |
| 66 | void setArrowStyle(Style* style) { m_arrowStyle = style; } |
| 67 | |
| 68 | int arrowAlign() const { return m_arrowAlign; } |
| 69 | const gfx::Rect& target() const { return m_target; } |
| 70 | |
| 71 | void setCloseOnKeyDown(bool state); |
| 72 | |
| 73 | // Returns false there is no enough screen space to show the |
| 74 | // window. |
| 75 | bool pointAt(int arrowAlign, |
| 76 | const gfx::Rect& target, |
| 77 | const ui::Display* display); |
| 78 | |
| 79 | void adjustTargetFrom(const ui::Display* targetDisplay); |
| 80 | |
| 81 | TextBox* textBox() const { return m_textBox; } |
| 82 | |
| 83 | protected: |
| 84 | bool onProcessMessage(Message* msg) override; |
| 85 | void onPaint(PaintEvent& ev) override; |
| 86 | void onBuildTitleLabel() override; |
| 87 | |
| 88 | private: |
| 89 | Style* m_arrowStyle; |
| 90 | int m_arrowAlign; |
| 91 | gfx::Rect m_target; |
| 92 | bool m_closeOnKeyDown; |
| 93 | TextBox* m_textBox; |
| 94 | }; |
| 95 | |
| 96 | } // namespace ui |
| 97 | |
| 98 | #endif |
| 99 | |