/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include namespace jsdialog { StringMap jsonToStringMap(const char* pJSON) { StringMap aArgs; if (pJSON && pJSON[0] != '\0') { std::stringstream aStream(pJSON); boost::property_tree::ptree aTree; boost::property_tree::read_json(aStream, aTree); for (const auto& rPair : aTree) { aArgs[OUString::fromUtf8(rPair.first.c_str())] = OUString::fromUtf8(rPair.second.get_value(".").c_str()); } } return aArgs; } void SendFullUpdate(sal_uInt64 nWindowId, const OString& rWidget) { weld::Widget* pWidget = JSInstanceBuilder::FindWeldWidgetsMap(nWindowId, rWidget); if (auto pJSWidget = dynamic_cast(pWidget)) pJSWidget->sendFullUpdate(); } bool ExecuteAction(sal_uInt64 nWindowId, const OString& rWidget, StringMap& rData) { weld::Widget* pWidget = JSInstanceBuilder::FindWeldWidgetsMap(nWindowId, rWidget); OUString sControlType = rData["type"]; OUString sAction = rData["cmd"]; if (sControlType == "responsebutton") { if (pWidget == nullptr) { // welded wrapper not found - use response code instead pWidget = JSInstanceBuilder::FindWeldWidgetsMap(nWindowId, "__DIALOG__"); sControlType = "dialog"; sAction = "response"; } else { // welded wrapper for button found - use it sControlType = "pushbutton"; } } if (pWidget != nullptr) { if (sControlType == "tabcontrol") { auto pNotebook = dynamic_cast(pWidget); if (pNotebook) { if (sAction == "selecttab") { OString pageId = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int page = std::atoi(pageId.getStr()); pNotebook->set_current_page(page); return true; } } } else if (sControlType == "combobox") { auto pCombobox = dynamic_cast(pWidget); if (pCombobox) { if (sAction == "selected") { int separatorPos = rData["data"].indexOf(';'); if (separatorPos > 0) { OUString entryPos = rData["data"].copy(0, separatorPos); OString posString = OUStringToOString(entryPos, RTL_TEXTENCODING_ASCII_US); int pos = std::atoi(posString.getStr()); pCombobox->set_active(pos); LOKTrigger::trigger_changed(*pCombobox); return true; } } else if (sAction == "change") { pCombobox->set_entry_text(rData["data"]); LOKTrigger::trigger_changed(*pCombobox); return true; } } } else if (sControlType == "pushbutton") { auto pButton = dynamic_cast(pWidget); if (pButton) { if (sAction == "click") { pButton->clicked(); return true; } } } else if (sControlType == "checkbox") { auto pCheckButton = dynamic_cast(pWidget); if (pCheckButton) { if (sAction == "change") { bool bChecked = rData["data"] == "true"; pCheckButton->set_state(bChecked ? TRISTATE_TRUE : TRISTATE_FALSE); LOKTrigger::trigger_clicked(*static_cast(pCheckButton)); LOKTrigger::trigger_toggled(*static_cast(pCheckButton)); return true; } } } else if (sControlType == "drawingarea") { auto pArea = dynamic_cast(pWidget); if (pArea) { if (sAction == "click") { LOKTrigger::trigger_click(*pArea, Point(10, 10)); return true; } } } else if (sControlType == "spinfield") { auto pSpinField = dynamic_cast(pWidget); if (pSpinField) { if (sAction == "change") { OString sValue = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nValue = std::atoi(sValue.getStr()); pSpinField->set_value(nValue); LOKTrigger::trigger_value_changed(*pSpinField); return true; } if (sAction == "plus") { pSpinField->set_value(pSpinField->get_value() + 1); LOKTrigger::trigger_value_changed(*pSpinField); return true; } else if (sAction == "minus") { pSpinField->set_value(pSpinField->get_value() - 1); LOKTrigger::trigger_value_changed(*pSpinField); return true; } } } else if (sControlType == "toolbox") { auto pToolbar = dynamic_cast(pWidget); if (pToolbar) { if (sAction == "click") { LOKTrigger::trigger_clicked( *pToolbar, OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US)); return true; } } } else if (sControlType == "edit") { auto pEdit = dynamic_cast(pWidget); if (pEdit) { if (sAction == "change") { pEdit->set_text_without_notify(rData["data"]); LOKTrigger::trigger_changed(*pEdit); return true; } } auto pTextView = dynamic_cast(pWidget); if (pTextView) { if (sAction == "change") { pTextView->set_text(rData["data"]); LOKTrigger::trigger_changed(*pTextView); return true; } } } else if (sControlType == "treeview") { auto pTreeView = dynamic_cast(pWidget); if (pTreeView) { if (sAction == "change") { OUString sDataJSON = rtl::Uri::decode( rData["data"], rtl_UriDecodeMechanism::rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8); StringMap aMap(jsonToStringMap( OUStringToOString(sDataJSON, RTL_TEXTENCODING_ASCII_US).getStr())); OString nRowString = OUStringToOString(aMap["row"], RTL_TEXTENCODING_ASCII_US); int nRow = std::atoi(nRowString.getStr()); bool bValue = aMap["value"] == "true"; pTreeView->set_toggle(nRow, bValue ? TRISTATE_TRUE : TRISTATE_FALSE); return true; } else if (sAction == "select") { OString nRowString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); pTreeView->unselect_all(); int nAbsPos = std::atoi(nRowString.getStr()); std::unique_ptr itEntry(pTreeView->make_iterator()); pTreeView->get_iter_abs_pos(*itEntry, nAbsPos); pTreeView->select(*itEntry); pTreeView->set_cursor(*itEntry); LOKTrigger::trigger_changed(*pTreeView); return true; } else if (sAction == "activate") { OString nRowString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nRow = std::atoi(nRowString.getStr()); pTreeView->unselect_all(); pTreeView->select(nRow); pTreeView->set_cursor(nRow); LOKTrigger::trigger_changed(*pTreeView); LOKTrigger::trigger_row_activated(*pTreeView); return true; } else if (sAction == "expand") { OString nRowString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nAbsPos = std::atoi(nRowString.getStr()); std::unique_ptr itEntry(pTreeView->make_iterator()); pTreeView->get_iter_abs_pos(*itEntry, nAbsPos); pTreeView->expand_row(*itEntry); return true; } else if (sAction == "dragstart") { OString nRowString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nRow = std::atoi(nRowString.getStr()); pTreeView->select(nRow); pTreeView->drag_start(); return true; } else if (sAction == "dragend") { pTreeView->drag_end(); return true; } } } else if (sControlType == "iconview") { auto pIconView = dynamic_cast(pWidget); if (pIconView) { if (sAction == "select") { OString nPosString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nPos = std::atoi(nPosString.getStr()); pIconView->select(nPos); LOKTrigger::trigger_changed(*pIconView); return true; } else if (sAction == "activate") { OString nPosString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nPos = std::atoi(nPosString.getStr()); pIconView->select(nPos); LOKTrigger::trigger_changed(*pIconView); LOKTrigger::trigger_item_activated(*pIconView); return true; } } } else if (sControlType == "expander") { auto pExpander = dynamic_cast(pWidget); if (pExpander) { if (sAction == "toggle") { pExpander->set_expanded(!pExpander->get_expanded()); return true; } } } else if (sControlType == "dialog") { auto pDialog = dynamic_cast(pWidget); if (pDialog) { if (sAction == "close") { pDialog->response(RET_CANCEL); return true; } else if (sAction == "response") { OString nResponseString = OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US); int nResponse = std::atoi(nResponseString.getStr()); pDialog->response(nResponse); return true; } } } else if (sControlType == "radiobutton") { auto pRadioButton = dynamic_cast(pWidget); if (pRadioButton) { if (sAction == "change") { bool bChecked = rData["data"] == "true"; pRadioButton->set_state(bChecked ? TRISTATE_TRUE : TRISTATE_FALSE); LOKTrigger::trigger_clicked(*static_cast(pRadioButton)); LOKTrigger::trigger_toggled(*static_cast(pRadioButton)); return true; } } } } return false; } } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */