PageRenderTime 41ms CodeModel.GetById 22ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lluilistener.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 101 lines | 49 code | 9 blank | 43 comment | 2 complexity | b6f3049e47e3ad8958a9b2d39eaa9357 MD5 | raw file
  1/**
  2 * @file   lluilistener.cpp
  3 * @author Nat Goodspeed
  4 * @date   2009-08-18
  5 * @brief  Implementation for lluilistener.
  6 * 
  7 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29// Precompiled header
 30#include "llviewerprecompiledheaders.h"
 31// associated header
 32#include "lluilistener.h"
 33// STL headers
 34// std headers
 35// external library headers
 36// other Linden headers
 37#include "llui.h" // getRootView(), resolvePath()
 38#include "lluictrl.h"
 39#include "llerror.h"
 40
 41
 42LLUIListener::LLUIListener():
 43    LLEventAPI("UI",
 44               "LLUICtrl::CommitCallbackRegistry listener.\n"
 45               "Capable of invoking any function (with parameter) you can specify in XUI.")
 46{
 47    add("call",
 48        "Invoke the operation named by [\"function\"], passing [\"parameter\"],\n"
 49        "as if from a user gesture on a menu -- or a button click.",
 50        &LLUIListener::call,
 51        LLSD().with("function", LLSD()));
 52
 53    add("getValue",
 54        "For the UI control identified by the path in [\"path\"], return the control's\n"
 55        "current value as [\"value\"] reply.",
 56        &LLUIListener::getValue,
 57        LLSDMap("path", LLSD())("reply", LLSD()));
 58}
 59
 60void LLUIListener::call(const LLSD& event) const
 61{
 62    LLUICtrl::commit_callback_t* func =
 63        LLUICtrl::CommitCallbackRegistry::getValue(event["function"]);
 64    if (! func)
 65    {
 66        // This API is intended for use by a script. It's a fire-and-forget
 67        // API: we provide no reply. Therefore, a typo in the script will
 68        // provide no feedback whatsoever to that script. To rub the coder's
 69        // nose in such an error, crump rather than quietly ignoring it.
 70        LL_ERRS("LLUIListener") << "function '" << event["function"] << "' not found" << LL_ENDL;
 71    }
 72    else
 73    {
 74        // Interestingly, view_listener_t::addMenu() (addCommit(),
 75        // addEnable()) constructs a commit_callback_t callable that accepts
 76        // two parameters but discards the first. Only the second is passed to
 77        // handleEvent(). Therefore we feel completely safe passing NULL for
 78        // the first parameter.
 79        (*func)(NULL, event["parameter"]);
 80    }
 81}
 82
 83void LLUIListener::getValue(const LLSD&event) const
 84{
 85    LLSD reply = LLSD::emptyMap();
 86
 87    const LLView* root = LLUI::getRootView();
 88    const LLView* view = LLUI::resolvePath(root, event["path"].asString());
 89    const LLUICtrl* ctrl(dynamic_cast<const LLUICtrl*>(view));
 90
 91    if (ctrl) 
 92    {
 93        reply["value"] = ctrl->getValue();
 94    }
 95    else
 96    {
 97        // *TODO: ??? return something indicating failure to resolve
 98    }
 99    
100    sendReply(reply, event);
101}