summaryrefslogtreecommitdiff
path: root/wx+/popup_dlg.cpp
blob: d7d1960ebae8b4a5ce3b617a4f0b9a51af2929ce (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// **************************************************************************
// * 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 "popup_dlg.h"
#include <wx/app.h>
#include <wx+/mouse_move_dlg.h>
#include <wx+/std_button_order.h>
#include <wx+/font_size.h>
#include <wx+/image_resources.h>
#include "popup_dlg_generated.h"

using namespace zen;

namespace
{
void setAsStandard(wxButton& btn)
{
    btn.SetDefault();
    btn.SetFocus();
}


void setBestInitialSize(wxTextCtrl& ctrl, const wxString& text, wxSize maxSize)
{
    const int scrollbarWidth = 30;
    if (maxSize.x <= scrollbarWidth) //implicitly checks for non-zero, too!
        return;
    maxSize.x -= scrollbarWidth;

    int bestWidth = 0;
    int rowCount  = 0;
    int rowHeight = 0;

    auto evalLineExtent = [&](const wxSize& sz) -> bool //return true when done
    {
        if (sz.x > bestWidth)
            bestWidth = std::min(maxSize.x, sz.x);

        rowCount += (sz.x + maxSize.x - 1) / maxSize.x; //integer round up: consider line-wraps!
        rowHeight = std::max(rowHeight, sz.y); //all rows *should* have same height

        return rowCount * rowHeight >= maxSize.y;
    };

    for (auto it = text.begin();;)
    {
        auto itEnd = std::find(it, text.end(), L'\n');
        wxString line(it, itEnd);
        if (line.empty())
            line = L" "; //GetTextExtent() returns (0, 0) for empty strings!

        wxSize sz = ctrl.GetTextExtent(line); //exactly gives row height, but does *not* consider newlines
        if (evalLineExtent(sz))
            break;

        if (itEnd == text.end())
            break;
        it = itEnd + 1;
    }

#if defined ZEN_WIN || defined ZEN_LINUX
    const int rowGap = 0;
#elif defined ZEN_MAC
    const int rowGap = 1;
#endif
    const wxSize bestSize(bestWidth + scrollbarWidth, std::min(rowCount * (rowHeight + rowGap), maxSize.y));
    ctrl.SetMinSize(bestSize); //alas, SetMinClientSize() is just not working!
}
}


class zen::StandardPopupDialog : public PopupDialogGenerated
{
public:
    StandardPopupDialog(wxWindow* parent, DialogInfoType type, const PopupDialogCfg& cfg) :
        PopupDialogGenerated(parent),
        checkBoxValue_(cfg.checkBoxValue)
    {
#ifdef ZEN_WIN
        new zen::MouseMoveWindow(*this); //allow moving main dialog by clicking (nearly) anywhere...; ownership passed to "this"
#endif
        wxString titleTmp = cfg.title;
        switch (type)
        {
            case DialogInfoType::INFO:
                //"information" is meaningless as caption text!
                //confirmation doesn't use info icon
                //m_bitmapMsgType->Hide();
                //m_bitmapMsgType->SetSize(30, -1);
                //m_bitmapMsgType->SetBitmap(getResourceImage(L"msg_info"));
                break;
            case DialogInfoType::WARNING:
                if (titleTmp.empty()) titleTmp = _("Warning");
                m_bitmapMsgType->SetBitmap(getResourceImage(L"msg_warning"));
                break;
            case DialogInfoType::ERROR2:
                if (titleTmp.empty()) titleTmp = _("Error");
                m_bitmapMsgType->SetBitmap(getResourceImage(L"msg_error"));
                break;
        }

        if (titleTmp.empty())
            SetTitle(wxTheApp->GetAppDisplayName());
        else
        {
            if (parent && parent->IsShownOnScreen())
                SetTitle(titleTmp);
            else
                SetTitle(wxTheApp->GetAppDisplayName() + L" - " + titleTmp);
        }

        const wxSize maxSize(500, 380);

        assert(!cfg.textMain.empty() || !cfg.textDetail.empty());
        if (!cfg.textMain.empty())
        {
            setMainInstructionFont(*m_staticTextMain);
            m_staticTextMain->SetLabel(cfg.textMain);
            m_staticTextMain->Wrap(maxSize.GetWidth()); //call *after* SetLabel()
        }
        else
            m_staticTextMain->Hide();

        if (!cfg.textDetail.empty())
        {
            const wxString& text = L"\n" + cfg.textDetail + L"\n"; //add empty top/bottom lines *instead* of using border space!
            setBestInitialSize(*m_textCtrlTextDetail, text, maxSize);
            m_textCtrlTextDetail->ChangeValue(text);
        }
        else
            m_textCtrlTextDetail->Hide();

        if (checkBoxValue_)
        {
            assert(contains(cfg.checkBoxLabel, L"&"));
            m_checkBoxCustom->SetLabel(cfg.checkBoxLabel);
            m_checkBoxCustom->SetValue(*checkBoxValue_);
        }
        else
            m_checkBoxCustom->Hide();

        Connect(wxEVT_CHAR_HOOK, wxKeyEventHandler(StandardPopupDialog::OnKeyPressed), nullptr, this);
    }

private:
    virtual void OnClose (wxCloseEvent&   event) override { EndModal(static_cast<int>(ConfirmationButton3::CANCEL)); }
    virtual void OnCancel(wxCommandEvent& event) override { EndModal(static_cast<int>(ConfirmationButton3::CANCEL)); }

    void OnKeyPressed(wxKeyEvent& event)
    {
        const int keyCode = event.GetKeyCode();
        if (keyCode == WXK_ESCAPE) //handle case where cancel button is hidden!
        {
            EndModal(static_cast<int>(ConfirmationButton3::CANCEL));
            return;
        }
        event.Skip();
    }

    virtual void OnButtonAffirmative(wxCommandEvent& event) override
    {
        if (checkBoxValue_)
            * checkBoxValue_ = m_checkBoxCustom->GetValue();
        EndModal(static_cast<int>(ConfirmationButton3::DO_IT));
    }

    virtual void OnButtonNegative(wxCommandEvent& event) override
    {
        if (checkBoxValue_)
            * checkBoxValue_ = m_checkBoxCustom->GetValue();
        EndModal(static_cast<int>(ConfirmationButton3::DONT_DO_IT));
    }

    bool* checkBoxValue_;
};


namespace
{
class NotificationDialog : public StandardPopupDialog
{
public:
    NotificationDialog(wxWindow* parent, DialogInfoType type, const PopupDialogCfg& cfg) :
        StandardPopupDialog(parent, type, cfg)
    {
        m_buttonAffirmative->SetLabel(_("Close")); //UX Guide: use "Close" for errors, warnings and windows in which users can't make changes (no ampersand!)
        m_buttonNegative->Hide();
        m_buttonCancel->Hide();

        //set std order after button visibility was set
        setStandardButtonOrder(*bSizerStdButtons, StdButtons().setAffirmative(m_buttonAffirmative));
        setAsStandard(*m_buttonAffirmative);
        GetSizer()->SetSizeHints(this); //~=Fit() + SetMinSize()
    }
};


class ConfirmationDialog : public StandardPopupDialog
{
public:
    ConfirmationDialog(wxWindow* parent, DialogInfoType type, const PopupDialogCfg& cfg, const wxString& labelDoIt) :
        StandardPopupDialog(parent, type, cfg)
    {
        assert(contains(labelDoIt, L"&"));
        m_buttonAffirmative->SetLabel(labelDoIt);
        m_buttonNegative->Hide();

        //set std order after button visibility was set
        setStandardButtonOrder(*bSizerStdButtons, StdButtons().setAffirmative(m_buttonAffirmative).setCancel(m_buttonCancel));
        setAsStandard(*m_buttonAffirmative);
        GetSizer()->SetSizeHints(this); //~=Fit() + SetMinSize()
    }
};
}

class zen::ConfirmationDialog3 : public StandardPopupDialog
{
public:
    ConfirmationDialog3(wxWindow* parent, DialogInfoType type, const PopupDialogCfg3& cfg, const wxString& labelDoIt, const wxString& labelDontDoIt) :
        StandardPopupDialog(parent, type, cfg.pdCfg),
        buttonToDisableWhenChecked(cfg.buttonToDisableWhenChecked)
    {
        assert(contains(labelDoIt,     L"&"));
        assert(contains(labelDontDoIt, L"&"));
        m_buttonAffirmative->SetLabel(labelDoIt);
        m_buttonNegative   ->SetLabel(labelDontDoIt);

        //m_buttonAffirmative->SetId(wxID_IGNORE); -> setting id after button creation breaks "mouse snap to" functionality
        //m_buttonNegative   ->SetId(wxID_RETRY);  -> also wxWidgets docs seem to hide some info: "Normally, the identifier should be provided on creation and should not be modified subsequently."

        updateGui();

        //set std order after button visibility was set
        setStandardButtonOrder(*bSizerStdButtons, StdButtons().setAffirmative(m_buttonAffirmative).setNegative(m_buttonNegative).setCancel(m_buttonCancel));
        setAsStandard(*m_buttonAffirmative);
        GetSizer()->SetSizeHints(this); //~=Fit() + SetMinSize()
    }

private:
    virtual void OnCheckBoxClick(wxCommandEvent& event) override { updateGui(); event.Skip(); }

    void updateGui()
    {
        switch (buttonToDisableWhenChecked)
        {
            case ConfirmationButton3::DO_IT:
                m_buttonAffirmative->Enable(!m_checkBoxCustom->GetValue());
                break;
            case ConfirmationButton3::DONT_DO_IT:
                m_buttonNegative->Enable(!m_checkBoxCustom->GetValue());
                break;
            case ConfirmationButton3::CANCEL:
                break;
        }
    }

    const ConfirmationButton3 buttonToDisableWhenChecked;
};

//########################################################################################

void zen::showNotificationDialog(wxWindow* parent, DialogInfoType type, const PopupDialogCfg& cfg)
{
    NotificationDialog dlg(parent, type, cfg);
    dlg.ShowModal();
}


ConfirmationButton zen::showConfirmationDialog(wxWindow* parent, DialogInfoType type, const PopupDialogCfg& cfg, const wxString& labelDoIt)
{
    ConfirmationDialog dlg(parent, type, cfg, labelDoIt);
    return static_cast<ConfirmationButton>(dlg.ShowModal());
}


ConfirmationButton3 zen::showConfirmationDialog3(wxWindow* parent, DialogInfoType type, const PopupDialogCfg3& cfg, const wxString& labelDoIt, const wxString& labelDontDoIt)
{
    ConfirmationDialog3 dlg(parent, type, cfg, labelDoIt, labelDontDoIt);
    return static_cast<ConfirmationButton3>(dlg.ShowModal());
}
bgstack15