summaryrefslogtreecommitdiff
path: root/indra/newview/llsetkeybinddialog.cpp
blob: 806c70aa03784a3a65f01aa703b19d58f752f395 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/** 
 * @file llsetkeybinddialog.cpp
 * @brief LLSetKeyBindDialog class implementation.
 *
 * $LicenseInfo:firstyear=2019&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2019, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llsetkeybinddialog.h"

#include "llbutton.h"
#include "llcheckboxctrl.h"
#include "lleventtimer.h"
#include "llfloaterreg.h"
#include "llfocusmgr.h"
#include "llkeyconflict.h"

class LLSetKeyBindDialog::Updater : public LLEventTimer
{
public:

    typedef boost::function<void(MASK)> callback_t;

    Updater(callback_t cb, F32 period, MASK mask)
        :LLEventTimer(period),
        mMask(mask),
        mCallback(cb)
    {
        mEventTimer.start();
    }

    virtual ~Updater(){}

protected:
    BOOL tick()
    {
        mCallback(mMask);
        // Deletes itseft after execution
        return TRUE;
    }

private:
    MASK mMask;
    callback_t mCallback;
};

bool LLSetKeyBindDialog::sRecordKeys = false;

LLSetKeyBindDialog::LLSetKeyBindDialog(const LLSD& key)
    : LLModalDialog(key),
    pParent(NULL),
    mKeyFilterMask(DEFAULT_KEY_FILTER),
    pUpdater(NULL)
{
}

LLSetKeyBindDialog::~LLSetKeyBindDialog()
{
}

//virtual
BOOL LLSetKeyBindDialog::postBuild()
{
    childSetAction("SetEmpty", onBlank, this);
    childSetAction("Default", onDefault, this);
    childSetAction("Cancel", onCancel, this);
    getChild<LLUICtrl>("Cancel")->setFocus(TRUE);

    pCheckBox = getChild<LLCheckBoxCtrl>("ignore_masks");
    pDesription = getChild<LLTextBase>("descritption");

    gFocusMgr.setKeystrokesOnly(TRUE);

    return TRUE;
}

//virtual
void LLSetKeyBindDialog::onOpen(const LLSD& data)
{
     sRecordKeys = true;
     LLModalDialog::onOpen(data);
}

//virtual
void LLSetKeyBindDialog::onClose(bool app_quiting)
{
    sRecordKeys = false;
    if (pParent)
    {
        pParent->onCancelKeyBind();
        pParent = NULL;
    }
    if (pUpdater)
    {
        // Doubleclick timer has't fired, delete it
        delete pUpdater;
        pUpdater = NULL;
    }
    LLModalDialog::onClose(app_quiting);
}

//virtual
void LLSetKeyBindDialog::draw()
{
    LLRect local_rect;
    drawFrustum(local_rect, this, (LLView*)getDragHandle(), hasFocus());
    LLModalDialog::draw();
}

void LLSetKeyBindDialog::setParent(LLKeyBindResponderInterface* parent, LLView* frustum_origin, U32 key_mask)
{
    pParent = parent;
    setFrustumOrigin(frustum_origin);
    mKeyFilterMask = key_mask;

    std::string input;
    if ((key_mask & ALLOW_MOUSE) != 0)
    {
        input = getString("mouse");
    }
    if ((key_mask & ALLOW_KEYS) != 0)
    {
        if (!input.empty())
        {
            input += ", ";
        }
        input += getString("keyboard");
    }
    pDesription->setText(getString("basic_description"));
    pDesription->setTextArg("[INPUT]", input);

    bool can_ignore_masks = (key_mask & CAN_IGNORE_MASKS) != 0;
    pCheckBox->setVisible(can_ignore_masks);
    pCheckBox->setValue(false);
}

// static
bool LLSetKeyBindDialog::recordKey(KEY key, MASK mask)
{
    if (sRecordKeys)
    {
        LLSetKeyBindDialog* dialog = LLFloaterReg::getTypedInstance<LLSetKeyBindDialog>("keybind_dialog", LLSD());
        if (dialog && dialog->getVisible())
        {
            return dialog->recordAndHandleKey(key, mask);
        }
        else
        {
            LL_WARNS() << "Key recording was set despite no open dialog" << LL_ENDL;
            sRecordKeys = false;
        }
    }
    return false;
}

bool LLSetKeyBindDialog::recordAndHandleKey(KEY key, MASK mask)
{
    if ((key == 'Q' && mask == MASK_CONTROL)
        || key == KEY_ESCAPE)
    {
        sRecordKeys = false;
        closeFloater();
        return true;
    }

    if (key == KEY_DELETE)
    {
        setKeyBind(CLICK_NONE, KEY_NONE, MASK_NONE, false);
        sRecordKeys = false;
        closeFloater();
        return false;
    }

    // forbidden keys
    if (key == KEY_NONE
        || key == KEY_RETURN
        || key == KEY_BACKSPACE)
    {
        return false;
    }

    if ((mKeyFilterMask & ALLOW_MASKS) == 0
        && (key == KEY_CONTROL || key == KEY_SHIFT || key == KEY_ALT))
    {
        // mask by themself are not allowed
        return false;
    }
    else if ((mKeyFilterMask & ALLOW_KEYS) == 0)
    {
        // basic keys not allowed
        return false;
    }
    else if ((mKeyFilterMask & ALLOW_MASK_KEYS) == 0 && mask != 0)
    {
        // masked keys not allowed
        return false;
    }

    if (LLKeyConflictHandler::isReservedByMenu(key, mask))
    {
        pDesription->setText(getString("reserved_by_menu"));
        pDesription->setTextArg("[KEYSTR]", LLKeyboard::stringFromAccelerator(mask,key));
        return true;
    }

    setKeyBind(CLICK_NONE, key, mask, pCheckBox->getValue().asBoolean());
    sRecordKeys = false;
    closeFloater();
    return true;
}

BOOL LLSetKeyBindDialog::handleAnyMouseClick(S32 x, S32 y, MASK mask, EMouseClickType clicktype, BOOL down)
{
    BOOL result = FALSE;
    if (!pParent)
    {
        // we already processed 'down' event, this is 'up', consume
        closeFloater();
        result = TRUE;
    }
    if (!result && clicktype == CLICK_LEFT)
    {
        // try handling buttons first
        if (down)
        {
            result = LLView::handleMouseDown(x, y, mask);
        }
        else
        {
            result = LLView::handleMouseUp(x, y, mask);
        }
        if (result)
        {
            setFocus(TRUE);
            gFocusMgr.setKeystrokesOnly(TRUE);
        }
        // ignore selection related combinations
        else if (down && (mask & (MASK_SHIFT | MASK_CONTROL)) == 0)
        {
            // this can be a double click, wait a bit;
            if (!pUpdater)
            {
                // Note: default doubleclick time is 500ms, but can stretch up to 5s
                pUpdater = new Updater(boost::bind(&onClickTimeout, this, _1), 0.7f, mask);
                result = TRUE;
            }
        }
    }

    if (!result
        && (clicktype != CLICK_LEFT) // subcases were handled above
        && ((mKeyFilterMask & ALLOW_MOUSE) != 0)
        && (clicktype != CLICK_RIGHT || mask != 0) // reassigning menu button is not supported
        && ((mKeyFilterMask & ALLOW_MASK_MOUSE) != 0 || mask == 0)) // reserved for selection
    {
        setKeyBind(clicktype, KEY_NONE, mask, pCheckBox->getValue().asBoolean());
        result = TRUE;
        if (!down)
        {
            // wait for 'up' event before closing
            // alternative: set pUpdater
            closeFloater();
        }
    }

    return result;
}

//static
void LLSetKeyBindDialog::onCancel(void* user_data)
{
    LLSetKeyBindDialog* self = (LLSetKeyBindDialog*)user_data;
    self->closeFloater();
}

//static
void LLSetKeyBindDialog::onBlank(void* user_data)
{
    LLSetKeyBindDialog* self = (LLSetKeyBindDialog*)user_data;
    // tmp needs 'no key' button
    self->setKeyBind(CLICK_NONE, KEY_NONE, MASK_NONE, false);
    self->closeFloater();
}

//static
void LLSetKeyBindDialog::onDefault(void* user_data)
{
    LLSetKeyBindDialog* self = (LLSetKeyBindDialog*)user_data;
    if (self->pParent)
    {
        self->pParent->onDefaultKeyBind();
        self->pParent = NULL;
    }
    self->closeFloater();
}

//static
void LLSetKeyBindDialog::onClickTimeout(void* user_data, MASK mask)
{
    LLSetKeyBindDialog* self = (LLSetKeyBindDialog*)user_data;

    // timer will delete itself after timeout
    self->pUpdater = NULL;

    self->setKeyBind(CLICK_LEFT, KEY_NONE, mask, self->pCheckBox->getValue().asBoolean());
    self->closeFloater();
}

void LLSetKeyBindDialog::setKeyBind(EMouseClickType click, KEY key, MASK mask, bool ignore)
{
    if (pParent)
    {
        pParent->onSetKeyBind(click, key, mask, ignore);
        pParent = NULL;
    }
}