summaryrefslogtreecommitdiff
path: root/wx+/tooltip.cpp
blob: 3431e339bdd2c6cd8ed0c1e58e5a0de65c80fc65 (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "tooltip.h"
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/statbmp.h>
#include <wx/settings.h>
#include <wx/app.h>
#include <wx+/image_tools.h>

using namespace zen;


class Tooltip::PopupFrameGenerated : public wxFrame
{
public:
    PopupFrameGenerated(wxWindow* parent,
                        wxWindowID id = wxID_ANY,
                        const wxString& title = wxEmptyString,
                        const wxPoint& pos = wxDefaultPosition,
                        const wxSize& size = wxSize( -1, -1 ),
                        long style = wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP | wxSTATIC_BORDER) : wxFrame(parent, id, title, pos, size, style)
    {
        this->SetSizeHints(wxDefaultSize, wxDefaultSize);
        this->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));   //both required: on Ubuntu background is black, foreground white!
        this->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT)); //

        wxBoxSizer* bSizer158;
        bSizer158 = new wxBoxSizer(wxHORIZONTAL);

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

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

        this->SetSizer(bSizer158);
        this->Layout();
        bSizer158->Fit(this);
    }

    wxStaticText* m_staticTextMain;
    wxStaticBitmap* m_bitmapLeft;
};


Tooltip::Tooltip() : tipWindow(new PopupFrameGenerated(nullptr))
{
#if defined FFS_WIN //prevent window stealing focus!
    tipWindow->Disable(); //neither looks good nor works at all on Linux; no visible difference on OS X
#endif
    hide();
}


Tooltip::~Tooltip()
{
    tipWindow->Destroy();
}


void Tooltip::show(const wxString& text, wxPoint mousePos, const wxBitmap* bmp)
{
    const wxBitmap& newBmp = bmp ? *bmp : wxNullBitmap;

    if (!isEqual(tipWindow->m_bitmapLeft->GetBitmap(), newBmp))
    {
        tipWindow->m_bitmapLeft->SetBitmap(newBmp);
        tipWindow->Refresh(); //needed if bitmap size changed!
    }

    if (text != tipWindow->m_staticTextMain->GetLabel())
    {
        tipWindow->m_staticTextMain->SetLabel(text);
        tipWindow->m_staticTextMain->Wrap(600);
    }

    tipWindow->Fit(); //Linux: Fit() seems to be somewhat broken => this needs to be called EVERY time inside show, not only if text or bmp change

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

    if (newPos != tipWindow->GetScreenPosition())
        tipWindow->SetSize(newPos.x, newPos.y, wxDefaultCoord, wxDefaultCoord);
    //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()
{
#ifdef FFS_LINUX
    //on wxGTK the tip window occassionally goes blank and stays that way. This is somehow triggered by wxWindow::Hide() and doesn't seem to be a wxWidgets bug (=> GTK?)
    //apply brute force:
    tipWindow->Destroy();
    tipWindow = new PopupFrameGenerated(nullptr);
#endif

    tipWindow->Hide();
}
bgstack15