summaryrefslogtreecommitdiff
path: root/wx+/tooltip.cpp
blob: 01b5ead401e6afaf6c1b658057a38c604ccce631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#include "tooltip.h"
#include <zen/zstring.h>
#include <wx/dialog.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/statbmp.h>
#include <wx/settings.h>
#include <wx/app.h>
#include "image_tools.h"
#include "bitmap_button.h"
#include "dc.h"
    #include <gtk/gtk.h>

using namespace zen;


namespace
{
const int TIP_WINDOW_OFFSET_DIP = 30;
}


class Tooltip::TooltipDlgGenerated : public wxDialog
{
public:
    TooltipDlgGenerated(wxWindow* parent) : //Suse Linux/X11: needs parent window, else there are z-order issues
        wxDialog(parent, wxID_ANY, L"" /*title*/, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER /*style*/)
        //wxSIMPLE_BORDER side effect: removes title bar on KDE
    {
        SetSizeHints(wxDefaultSize, wxDefaultSize);
        SetExtraStyle(this->GetExtraStyle() | wxWS_EX_TRANSIENT);
        SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));   //both required: on Ubuntu background is black, foreground white!
        SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT)); //

        wxBoxSizer* bSizer158 = new wxBoxSizer(wxHORIZONTAL);
        bitmapLeft_ = new wxStaticBitmap(this, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0);
        bSizer158->Add(bitmapLeft_, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5);

        staticTextMain_ = new wxStaticText(this, wxID_ANY, wxString(), wxDefaultPosition, wxDefaultSize, 0);
        bSizer158->Add(staticTextMain_, 0, wxALL | wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL, 5);

        SetSizer(bSizer158);

    }

    bool AcceptsFocus() const override { return false; } //any benefit?

    wxStaticText* staticTextMain_ = nullptr;
    wxStaticBitmap* bitmapLeft_   = nullptr;
};


void Tooltip::show(const wxString& text, wxPoint mousePos, const wxImage* img)
{
    if (!tipWindow_)
        tipWindow_ = new TooltipDlgGenerated(&parent_); //ownership passed to parent

    const wxImage& newImg = img ? *img : wxNullImage;

    const bool imgChanged = !newImg.IsSameAs(lastUsedImg_);
    const bool txtChanged = text != lastUsedText_;

    if (imgChanged)
    {
        lastUsedImg_ = newImg;
        setImage(*tipWindow_->bitmapLeft_, newImg);
        // tipWindow_->Refresh(); //needed if bitmap size changed!    ->???
    }

    if (txtChanged)
    {
        lastUsedText_ = text;
        {
            tipWindow_->staticTextMain_->SetLabelText(text);
            tipWindow_->staticTextMain_->Wrap(dipToWxsize(600));
        }
    }

    if (imgChanged || txtChanged)
        //tipWindow_->Dimensions(); -> apparently not needed!?
        tipWindow_->GetSizer()->SetSizeHints(tipWindow_); //~=Fit() + SetMinSize()
#ifdef __WXGTK3__
    //GTK3 size calculation requires visible window: https://github.com/wxWidgets/wxWidgets/issues/16088
    //=> call wxWindow::Show() to "execute"
#endif

    const wxPoint newPos = wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ?
                           mousePos - wxPoint(dipToWxsize(TIP_WINDOW_OFFSET_DIP) + tipWindow_->GetSize().GetWidth(), 0) :
                           mousePos + wxPoint(dipToWxsize(TIP_WINDOW_OFFSET_DIP),                                    0);

    if (newPos != tipWindow_->GetScreenPosition())
        tipWindow_->Move(newPos);
    //attention!!! possible endless loop: mouse pointer must NOT be within tipWindow!
    //else it will trigger a wxEVT_LEAVE_WINDOW on middle grid which will hide the window, causing the window to be shown again via this method, etc.

    if (!tipWindow_->IsShown())
        tipWindow_->Show();
}


void Tooltip::hide()
{
    if (tipWindow_)
    {
#if GTK_MAJOR_VERSION == 2 //the tooltip sometimes turns blank or is not shown again after it was hidden: e.g. drag-selection on middle grid
        //=> no such issues on GTK3!
        tipWindow_->Destroy(); //apply brute force:
        tipWindow_ = nullptr;  //
        lastUsedImg_ = wxNullImage;

#elif GTK_MAJOR_VERSION == 3
        tipWindow_->Hide();
#else
#error unknown GTK version!
#endif
    }
}
bgstack15