summaryrefslogtreecommitdiff
path: root/indra/llui/llfloaterreglistener.cpp
blob: e17f9f4dd61e78616e07e4a9b1c95ee8e00c44d1 (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
/**
 * @file   llfloaterreglistener.cpp
 * @author Nat Goodspeed
 * @date   2009-08-12
 * @brief  Implementation for llfloaterreglistener.
 * 
 * $LicenseInfo:firstyear=2009&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$
 */

// Precompiled header
#include "linden_common.h"
// associated header
#include "llfloaterreglistener.h"
// STL headers
// std headers
// external library headers
// other Linden headers
#include "llfloaterreg.h"
#include "llfloater.h"
#include "llbutton.h"
#include "llluafloater.h"

LLFloaterRegListener::LLFloaterRegListener():
    LLEventAPI("LLFloaterReg",
               "LLFloaterReg listener to (e.g.) show/hide LLFloater instances")
{
    add("getBuildMap",
        "Return on [\"reply\"] data about all registered LLFloaterReg floater names",
        &LLFloaterRegListener::getBuildMap,
        LLSD().with("reply", LLSD()));
    LLSD requiredName;
    requiredName["name"] = LLSD();
    add("showInstance",
        "Ask to display the floater specified in [\"name\"]",
        &LLFloaterRegListener::showInstance,
        requiredName);
    add("hideInstance",
        "Ask to hide the floater specified in [\"name\"]",
        &LLFloaterRegListener::hideInstance,
        requiredName);
    add("toggleInstance",
        "Ask to toggle the state of the floater specified in [\"name\"]",
        &LLFloaterRegListener::toggleInstance,
        requiredName);
    add("instanceVisible",
        "Return on [\"reply\"] an event whose [\"visible\"] indicates the visibility "
        "of the floater specified in [\"name\"]",
        &LLFloaterRegListener::instanceVisible,
        requiredName);
    LLSD requiredNameButton;
    requiredNameButton["name"] = LLSD();
    requiredNameButton["button"] = LLSD();
    add("clickButton",
        "Simulate clicking the named [\"button\"] in the visible floater named in [\"name\"]",
        &LLFloaterRegListener::clickButton,
        requiredNameButton);

    add("showLuaFloater",
        "Open the new floater using XML file specified in [\"xml_path\"] with ID in [\"reqid\"]",
        &LLLuaFloater::showLuaFloater, {llsd::map("xml_path", LLSD(), "reqid", LLSD())});
    add("getFloaterEvents",
        "Return the table of Lua Floater events which are send to the script",
        &LLFloaterRegListener::getLuaFloaterEvents);

    add("getFloaterNames",
        "Return the table of all registered floaters",
        &LLFloaterRegListener::getFloaterNames,
        llsd::map("reply", LLSD()));
}

void LLFloaterRegListener::getBuildMap(const LLSD& event) const
{
    LLSD reply;
    // Build an LLSD map that mirrors sBuildMap. Since we have no good way to
    // represent a C++ callable in LLSD, the only part of BuildData we can
    // store is the filename. For each LLSD map entry, it would be more
    // extensible to store a nested LLSD map containing a single key "file" --
    // but we don't bother, simply storing the string filename instead.
    for (LLFloaterReg::build_map_t::const_iterator mi(LLFloaterReg::sBuildMap.begin()),
                                                   mend(LLFloaterReg::sBuildMap.end());
         mi != mend; ++mi)
    {
        reply[mi->first] = mi->second.mFile;
    }
    // Send the reply to the LLEventPump named in event["reply"].
    sendReply(reply, event);
}

void LLFloaterRegListener::showInstance(const LLSD& event) const
{
    LLFloaterReg::showInstance(event["name"], event["key"], event["focus"]);
}

void LLFloaterRegListener::hideInstance(const LLSD& event) const
{
    LLFloaterReg::hideInstance(event["name"], event["key"]);
}

void LLFloaterRegListener::toggleInstance(const LLSD& event) const
{
    LLFloaterReg::toggleInstance(event["name"], event["key"]);
}

void LLFloaterRegListener::instanceVisible(const LLSD& event) const
{
    sendReply(LLSDMap("visible", LLFloaterReg::instanceVisible(event["name"], event["key"])),
              event);
}


void LLFloaterRegListener::getFloaterNames(const LLSD &event) const
{
    Response response(llsd::map("floaters", LLFloaterReg::getFloaterNames()), event);
}

void LLFloaterRegListener::clickButton(const LLSD& event) const
{
    // If the caller requests a reply, build the reply.
    LLReqID reqID(event);
    LLSD reply(reqID.makeResponse());

    LLFloater* floater = LLFloaterReg::findInstance(event["name"], event["key"]);
    if (! LLFloater::isShown(floater))
    {
        reply["type"]  = "LLFloater";
        reply["name"]  = event["name"];
        reply["key"]   = event["key"];
        reply["error"] = floater? "!isShown()" : "NULL";
    }
    else
    {
        // Here 'floater' points to an LLFloater instance with the specified
        // name and key which isShown().
        LLButton* button = floater->findChild<LLButton>(event["button"]);
        if (! LLButton::isAvailable(button))
        {
            reply["type"]  = "LLButton";
            reply["name"]  = event["button"];
            reply["error"] = button? "!isAvailable()" : "NULL";
        }
        else
        {
            // Here 'button' points to an isAvailable() LLButton child of
            // 'floater' with the specified button name. Pretend to click it.
            button->onCommit();
            // Leave reply["error"] isUndefined(): no error, i.e. success.
        }
    }

    // Send a reply only if caller asked for a reply.
    LLSD replyPump(event["reply"]);
    if (replyPump.isString())       // isUndefined() if absent
    {
        LLEventPumps::instance().obtain(replyPump).post(reply);
    }
}

void LLFloaterRegListener::getLuaFloaterEvents(const LLSD &event) const
{
    Response response(llsd::map("events", LLLuaFloater::getEventsData()), event);
}