summaryrefslogtreecommitdiff
path: root/wx+/std_button_order.h
blob: 71fc23349d939036865c69ff0a4135da09a5bce6 (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
// **************************************************************************
// * 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        *
// **************************************************************************

#ifndef STD_BUTTON_ORDER_H_18347032147831732143214
#define STD_BUTTON_ORDER_H_18347032147831732143214

#include <wx/sizer.h>
#include <wx/button.h>

namespace zen
{
struct StdButtons
{
    StdButtons() : btnYes(nullptr), btnNo(nullptr), btnCancel(nullptr) {}
    StdButtons& setAffirmative (wxButton* btn) { btnYes    = btn; return *this; }
    StdButtons& setNegative    (wxButton* btn) { btnNo     = btn; return *this; }
    StdButtons& setCancel      (wxButton* btn) { btnCancel = btn; return *this; }

    wxButton* btnYes;
    wxButton* btnNo;
    wxButton* btnCancel;
};

void setStandardButtonOrder(wxBoxSizer& sizer, const StdButtons& buttons = StdButtons());
//sizer width will change! => call wxWindow::Fit and wxWindow::Layout











//--------------- impelementation -------------------------------------------
inline
void setStandardButtonOrder(wxBoxSizer& sizer, const StdButtons& buttons)
{
    assert(sizer.GetOrientation() == wxHORIZONTAL);

    StdButtons buttonsTmp = buttons;

    auto detach = [&](wxButton*& btn)
    {
        if (btn)
        {
            assert(btn->GetContainingSizer() == &sizer);
            if (btn->IsShown())
            {
                bool rv = sizer.Detach(btn);
                assert(rv);
                if (!rv)
                    btn = nullptr;
            }
            else
                btn = nullptr;
        }
    };

    detach(buttonsTmp.btnYes);
    detach(buttonsTmp.btnNo);
    detach(buttonsTmp.btnCancel);

#if defined ZEN_WIN
    //Windows User Experience Interaction Guidelines: http://msdn.microsoft.com/en-us/library/windows/desktop/aa511453.aspx#sizing
    const int spaceH    = 6;  //OK
    const int spaceRimH = 10; //OK
    const int spaceRimV = 8;  //compromise; consider additional top row from static line; exact values: top 8, bottom 9
#elif defined ZEN_LINUX
    //GNOME Human Interface Guidelines: https://developer.gnome.org/hig-book/3.2/hig-book.html#alert-spacing
    const int spaceH    = 6;  //OK
    const int spaceRimH = 12; //OK
    const int spaceRimV = 12; //OK
#elif defined ZEN_MAC
    //OS X Human Interface Guidelines: http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AppleHIGuidelines/Windows/Windows.html
    const int spaceH    = 14; //OK
    const int spaceRimH = 20; //OK
    const int spaceRimV = 11; //compromise; custom button height (of 30 pix) is considered, although the button is drawn smaller; otherwise 15 seems to have been optimal
#endif

    bool firstButtonSet = false;
    auto attach = [&](wxButton* btn)
    {
        if (btn)
        {
            if (firstButtonSet)
                sizer.Add(spaceH, 0);
            else
                firstButtonSet = true;
            sizer.Add(btn, 0, wxTOP | wxBOTTOM | wxALIGN_CENTER_VERTICAL, spaceRimV);
            assert(btn->GetSize().GetHeight() == 30); //see comment for OS X above
        }
    };

    //set border on left considering existing items
    if (sizer.GetChildren().GetCount() > 0) //for yet another retarded reason wxWidgets will have wxSizer::GetItem(0) cause an assert rather than just return nullptr as documented
        if (wxSizerItem* item = sizer.GetItem(static_cast<size_t>(0)))
        {
            assert(item->GetBorder() <= spaceRimV); //pragmatic check: other controls in the sizer should not have a larger border
            int flag = item->GetFlag();
            if (flag & wxLEFT)
            {
                flag &= ~wxLEFT;
                item->SetFlag(flag);
            }
            sizer.Insert(static_cast<size_t>(0), spaceRimH, 0);
        }

    sizer.Add(spaceRimH, 0);
#if defined ZEN_WIN
    attach(buttonsTmp.btnYes);
    attach(buttonsTmp.btnNo);
    attach(buttonsTmp.btnCancel);

#elif defined ZEN_LINUX
    attach(buttonsTmp.btnNo);
    attach(buttonsTmp.btnCancel);
    attach(buttonsTmp.btnYes);

#elif defined ZEN_MAC
    if (buttonsTmp.btnNo)
    {
        attach(buttonsTmp.btnNo);
        sizer.Add(42 - spaceH, 0); //OS X Human Interface Guidelines: "position it at least 24 pixels away from the “safe” buttons" -> however 42 is used in practice!
    }
    attach(buttonsTmp.btnCancel);
    attach(buttonsTmp.btnYes);
#endif
    sizer.Add(spaceRimH, 0);

    assert(buttonsTmp.btnCancel || buttonsTmp.btnYes); //OS X: there should be at least one button following the gap after the "dangerous" no-button
}
}

#endif //STD_BUTTON_ORDER_H_18347032147831732143214
bgstack15