summaryrefslogtreecommitdiff
path: root/indra/llui/lluictrl.h
blob: 8cd99509179bce92dc12e5c61f2e0ed1640c4a01 (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
338
339
340
341
/**
 * @file lluictrl.h
 * @author James Cook, Richard Nelson, Tom Yedwab
 * @brief Abstract base class for UI controls
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, 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$
 */

#ifndef LL_LLUICTRL_H
#define LL_LLUICTRL_H

//#include "llboost.h"
#include "llrect.h"
#include "llsd.h"
#include <boost/function.hpp>
#include <boost/signals2.hpp>

#include "llinitparam.h"
#include "llview.h"
#include "llviewmodel.h"        // *TODO move dependency to .cpp file
#include "llsearchablecontrol.h"

const bool TAKE_FOCUS_YES = true;
const bool TAKE_FOCUS_NO  = false;
const S32 DROP_SHADOW_FLOATER = 5;

class LLUICtrl
    : public LLView, public boost::signals2::trackable
{
public:
    typedef boost::function<void (LLUICtrl* ctrl, const LLSD& param)> commit_callback_t;
    typedef boost::signals2::signal<void (LLUICtrl* ctrl, const LLSD& param)> commit_signal_t;
    // *TODO: add xml support for this type of signal in the future
    typedef boost::signals2::signal<void (LLUICtrl* ctrl, S32 x, S32 y, MASK mask)> mouse_signal_t;

    typedef boost::function<bool (LLUICtrl* ctrl, const LLSD& param)> enable_callback_t;
    typedef boost::signals2::signal<bool (LLUICtrl* ctrl, const LLSD& param), boost_boolean_combiner> enable_signal_t;

    struct CallbackParam : public LLInitParam::Block<CallbackParam>
    {
        Ignored                 name;

        Optional<std::string>   function_name;
        Optional<LLSD>          parameter;

        Optional<std::string>   control_name;

        CallbackParam();
    };

    struct CommitCallbackParam : public LLInitParam::Block<CommitCallbackParam, CallbackParam >
    {
        Optional<commit_callback_t> function;
    };

    // also used for visible callbacks
    struct EnableCallbackParam : public LLInitParam::Block<EnableCallbackParam, CallbackParam >
    {
        Optional<enable_callback_t> function;
    };

    struct EnableControls : public LLInitParam::ChoiceBlock<EnableControls>
    {
        Alternative<std::string> enabled;
        Alternative<std::string> disabled;

        EnableControls();
    };
    struct ControlVisibility : public LLInitParam::ChoiceBlock<ControlVisibility>
    {
        Alternative<std::string> visible;
        Alternative<std::string> invisible;

        ControlVisibility();
    };
    struct Params : public LLInitParam::Block<Params, LLView::Params>
    {
        Optional<std::string>           label;
        Optional<bool>                  tab_stop,
                                        chrome,
                                        requests_front;
        Optional<LLSD>                  initial_value;

        Optional<CommitCallbackParam>   init_callback,
                                        commit_callback;
        Optional<EnableCallbackParam>   validate_callback;

        Optional<CommitCallbackParam>   mouseenter_callback,
                                        mouseleave_callback;

        Optional<std::string>           control_name;
        Optional<EnableControls>        enabled_controls;
        Optional<ControlVisibility>     controls_visibility;

        // font params
        Optional<const LLFontGL*>       font;
        Optional<LLFontGL::HAlign>      font_halign;
        Optional<LLFontGL::VAlign>      font_valign;

        // cruft from LLXMLNode implementation
        Ignored                         type,
                                        length;

        Params();
    };

    enum ETypeTransparency
    {
        TT_DEFAULT,
        TT_ACTIVE,      // focused floater
        TT_INACTIVE,    // other floaters
        TT_FADING,      // fading toast
    };
    /*virtual*/ ~LLUICtrl();

    void initFromParams(const Params& p);
protected:
    friend class LLUICtrlFactory;
    static const Params& getDefaultParams();
    LLUICtrl(const Params& p = getDefaultParams(),
             const LLViewModelPtr& viewmodel=LLViewModelPtr(new LLViewModel));

    commit_signal_t::slot_type initCommitCallback(const CommitCallbackParam& cb);
    enable_signal_t::slot_type initEnableCallback(const EnableCallbackParam& cb);

    // We need this virtual so we can override it with derived versions
    virtual LLViewModel* getViewModel() const;
    // We shouldn't ever need to set this directly
    //virtual void    setViewModel(const LLViewModelPtr&);

    /*virtual*/ bool    postBuild() override;

public:
    // LLView interface
    /*virtual*/ bool    setLabelArg( const std::string& key, const LLStringExplicit& text ) override;
    /*virtual*/ bool    isCtrl() const override;
    /*virtual*/ void    onMouseEnter(S32 x, S32 y, MASK mask) override;
    /*virtual*/ void    onMouseLeave(S32 x, S32 y, MASK mask) override;
    /*virtual*/ bool    canFocusChildren() const override;
    /*virtual*/ bool    handleMouseDown(S32 x, S32 y, MASK mask) override;
    /*virtual*/ bool    handleMouseUp(S32 x, S32 y, MASK mask) override;
    /*virtual*/ bool    handleRightMouseDown(S32 x, S32 y, MASK mask) override;
    /*virtual*/ bool    handleRightMouseUp(S32 x, S32 y, MASK mask) override;
    /*virtual*/ bool    handleDoubleClick(S32 x, S32 y, MASK mask) override;

    // From LLFocusableElement
    /*virtual*/ void    setFocus( bool b ) override;
    /*virtual*/ bool    hasFocus() const override;

    // New virtuals


    // Return NULL by default (overrride if the class has the appropriate interface)
    virtual class LLCtrlSelectionInterface* getSelectionInterface();
    virtual class LLCtrlListInterface* getListInterface();
    virtual class LLCtrlScrollInterface* getScrollInterface();

    bool setControlValue(const LLSD& value);
    void setControlVariable(LLControlVariable* control);
    virtual void setControlName(const std::string& control, LLView *context = NULL);
    void removeControlVariable();

    LLControlVariable* getControlVariable() { return mControlVariable; }

    void setEnabledControlVariable(LLControlVariable* control);
    void setDisabledControlVariable(LLControlVariable* control);
    void setMakeVisibleControlVariable(LLControlVariable* control);
    void setMakeInvisibleControlVariable(LLControlVariable* control);

    void setFunctionName(const std::string& function_name);

    virtual void    setTentative(bool b);
    virtual bool    getTentative() const;
    virtual void    setValue(const LLSD& value);
    virtual LLSD    getValue() const;
    /// When two widgets are displaying the same data (e.g. during a skin
    /// change), share their ViewModel.
    virtual void    shareViewModelFrom(const LLUICtrl& other);

    virtual bool    setTextArg(  const std::string& key, const LLStringExplicit& text );
    virtual void    setIsChrome(bool is_chrome);

    virtual bool    acceptsTextInput() const; // Defaults to false

    // A control is dirty if the user has modified its value.
    // Editable controls should override this.
    virtual bool    isDirty() const; // Defauls to false
    virtual void    resetDirty(); //Defaults to no-op

    // Call appropriate callback
    virtual void    onCommit();

    // Default to no-op:
    virtual void    onTabInto();

    // Clear any user-provided input (text in a text editor, checked checkbox,
    // selected radio button, etc.).  Defaults to no-op.
    virtual void    clear();

    virtual void    setColor(const LLUIColor& color);

    // Ansariel: Changed to virtual. We might want to change the transparency ourself!
    virtual F32 getCurrentTransparency();

    void                setTransparencyType(ETypeTransparency type);
    ETypeTransparency   getTransparencyType() const {return mTransparencyType;}

    bool    focusNextItem(bool text_entry_only);
    bool    focusPrevItem(bool text_entry_only);
    bool    focusFirstItem(bool prefer_text_fields = false, bool focus_flash = true );

    // Non Virtuals
    LLHandle<LLUICtrl> getHandle() const { return getDerivedHandle<LLUICtrl>(); }
    bool            getIsChrome() const;

    void            setTabStop( bool b );
    bool            hasTabStop() const;

    LLUICtrl*       getParentUICtrl() const;

    // return true if help topic found by crawling through parents -
    // topic then put in help_topic_out
    bool                    findHelpTopic(std::string& help_topic_out);

    boost::signals2::connection setCommitCallback(const CommitCallbackParam& cb);
    boost::signals2::connection setValidateCallback(const EnableCallbackParam& cb);

    boost::signals2::connection setCommitCallback( const commit_signal_t::slot_type& cb );
    boost::signals2::connection setValidateCallback( const enable_signal_t::slot_type& cb );

    boost::signals2::connection setMouseEnterCallback( const commit_signal_t::slot_type& cb );
    boost::signals2::connection setMouseLeaveCallback( const commit_signal_t::slot_type& cb );

    boost::signals2::connection setMouseDownCallback( const mouse_signal_t::slot_type& cb );
    boost::signals2::connection setMouseUpCallback( const mouse_signal_t::slot_type& cb );
    boost::signals2::connection setRightMouseDownCallback( const mouse_signal_t::slot_type& cb );
    boost::signals2::connection setRightMouseUpCallback( const mouse_signal_t::slot_type& cb );

    boost::signals2::connection setDoubleClickCallback( const mouse_signal_t::slot_type& cb );

    // *TODO: Deprecate; for backwards compatability only:
    boost::signals2::connection setCommitCallback( boost::function<void (LLUICtrl*,void*)> cb, void* data);
    boost::signals2::connection setValidateBeforeCommit( boost::function<bool (const LLSD& data)> cb );

    LLUICtrl* findRootMostFocusRoot();

    class LLTextInputFilter : public LLQueryFilter, public LLSingleton<LLTextInputFilter>
    {
        LLSINGLETON_EMPTY_CTOR(LLTextInputFilter);
        /*virtual*/ filterResult_t operator() (const LLView* const view, const viewList_t & children) const override
        {
            return filterResult_t(view->isCtrl() && static_cast<const LLUICtrl *>(view)->acceptsTextInput(), true);
        }
    };

    template <typename F, typename DERIVED> class CallbackRegistry : public LLRegistrySingleton<std::string, F, DERIVED >
    {};

    class CommitCallbackRegistry : public CallbackRegistry<commit_callback_t, CommitCallbackRegistry>
    {
        LLSINGLETON_EMPTY_CTOR(CommitCallbackRegistry);
    };
    // the enable callback registry is also used for visiblity callbacks
    class EnableCallbackRegistry : public CallbackRegistry<enable_callback_t, EnableCallbackRegistry>
    {
        LLSINGLETON_EMPTY_CTOR(EnableCallbackRegistry);
    };

protected:

    static bool controlListener(const LLSD& newvalue, LLHandle<LLUICtrl> handle, std::string type);

    commit_signal_t*        mCommitSignal;
    enable_signal_t*        mValidateSignal;

    commit_signal_t*        mMouseEnterSignal;
    commit_signal_t*        mMouseLeaveSignal;

    mouse_signal_t*     mMouseDownSignal;
    mouse_signal_t*     mMouseUpSignal;
    mouse_signal_t*     mRightMouseDownSignal;
    mouse_signal_t*     mRightMouseUpSignal;

    mouse_signal_t*     mDoubleClickSignal;

    LLViewModelPtr  mViewModel;

    LLControlVariable* mControlVariable;
    boost::signals2::connection mControlConnection;
    LLControlVariable* mEnabledControlVariable;
    boost::signals2::connection mEnabledControlConnection;
    LLControlVariable* mDisabledControlVariable;
    boost::signals2::connection mDisabledControlConnection;
    LLControlVariable* mMakeVisibleControlVariable;
    boost::signals2::connection mMakeVisibleControlConnection;
    LLControlVariable* mMakeInvisibleControlVariable;
    boost::signals2::connection mMakeInvisibleControlConnection;

    std::string mFunctionName;

    static F32 sActiveControlTransparency;
    static F32 sInactiveControlTransparency;

    /*virtual*/ void addInfo(LLSD & info) override;

private:

    bool            mIsChrome;
    bool            mRequestsFront;
    bool            mTabStop;
    bool            mTentative;

    ETypeTransparency mTransparencyType;
};

// Build time optimization, generate once in .cpp file
#ifndef LLUICTRL_CPP
extern template class LLUICtrl* LLView::getChild<class LLUICtrl>(
    std::string_view name, bool recurse) const;
#endif

#endif  // LL_LLUICTRL_H