2020-07-02 11:01:54 +02:00
|
|
|
/* -*- 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 <jsdialog/jsdialogbuilder.hxx>
|
|
|
|
#include <vcl/weld.hxx>
|
|
|
|
#include <vcl/jsdialog/executor.hxx>
|
|
|
|
#include <sal/log.hxx>
|
2020-10-30 10:51:08 +01:00
|
|
|
#include <rtl/uri.hxx>
|
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
2020-07-02 11:01:54 +02:00
|
|
|
|
|
|
|
namespace jsdialog
|
|
|
|
{
|
2020-10-30 10:51:08 +01:00
|
|
|
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<std::string>(".").c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aArgs;
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:01:54 +02:00
|
|
|
bool ExecuteAction(sal_uInt64 nWindowId, const OString& rWidget, StringMap& rData)
|
|
|
|
{
|
|
|
|
weld::Widget* pWidget = JSInstanceBuilder::FindWeldWidgetsMap(nWindowId, rWidget);
|
|
|
|
|
|
|
|
if (pWidget != nullptr)
|
|
|
|
{
|
|
|
|
OUString sControlType = rData["type"];
|
|
|
|
OUString sAction = rData["cmd"];
|
|
|
|
|
|
|
|
if (sControlType == "tabcontrol")
|
|
|
|
{
|
|
|
|
auto pNotebook = dynamic_cast<weld::Notebook*>(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<weld::ComboBox*>(pWidget);
|
|
|
|
if (pCombobox)
|
|
|
|
{
|
|
|
|
if (sAction == "selected")
|
|
|
|
{
|
|
|
|
int separatorPos = rData["data"].indexOf(';');
|
2020-11-03 11:32:21 +01:00
|
|
|
if (separatorPos > 0)
|
2020-07-02 11:01:54 +02:00
|
|
|
{
|
|
|
|
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<weld::Button*>(pWidget);
|
|
|
|
if (pButton)
|
|
|
|
{
|
|
|
|
if (sAction == "click")
|
|
|
|
{
|
|
|
|
pButton->clicked();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 12:24:49 +01:00
|
|
|
else if (sControlType == "checkbox")
|
|
|
|
{
|
|
|
|
auto pCheckButton = dynamic_cast<weld::CheckButton*>(pWidget);
|
|
|
|
if (pCheckButton)
|
|
|
|
{
|
|
|
|
if (sAction == "change")
|
|
|
|
{
|
|
|
|
bool bChecked = rData["data"] == "true";
|
|
|
|
pCheckButton->set_state(bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
|
2020-12-15 15:59:36 +01:00
|
|
|
LOKTrigger::trigger_clicked(*static_cast<weld::Button*>(pCheckButton));
|
2020-11-02 12:24:49 +01:00
|
|
|
LOKTrigger::trigger_toggled(*static_cast<weld::ToggleButton*>(pCheckButton));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 11:01:54 +02:00
|
|
|
else if (sControlType == "drawingarea")
|
|
|
|
{
|
|
|
|
auto pArea = dynamic_cast<weld::DrawingArea*>(pWidget);
|
|
|
|
if (pArea)
|
|
|
|
{
|
|
|
|
if (sAction == "click")
|
|
|
|
{
|
|
|
|
LOKTrigger::trigger_click(*pArea, Point(10, 10));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sControlType == "spinfield")
|
|
|
|
{
|
|
|
|
auto pSpinField = dynamic_cast<weld::SpinButton*>(pWidget);
|
|
|
|
if (pSpinField)
|
|
|
|
{
|
2020-12-09 06:28:34 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-07-02 11:01:54 +02:00
|
|
|
if (sAction == "plus")
|
|
|
|
{
|
|
|
|
pSpinField->set_value(pSpinField->get_value() + 1);
|
2020-12-09 06:28:34 +01:00
|
|
|
LOKTrigger::trigger_value_changed(*pSpinField);
|
2020-07-02 11:01:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (sAction == "minus")
|
|
|
|
{
|
|
|
|
pSpinField->set_value(pSpinField->get_value() - 1);
|
2020-12-09 06:28:34 +01:00
|
|
|
LOKTrigger::trigger_value_changed(*pSpinField);
|
2020-07-02 11:01:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sControlType == "toolbox")
|
|
|
|
{
|
|
|
|
auto pToolbar = dynamic_cast<weld::Toolbar*>(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<weld::Entry*>(pWidget);
|
|
|
|
if (pEdit)
|
|
|
|
{
|
|
|
|
if (sAction == "change")
|
|
|
|
{
|
|
|
|
pEdit->set_text(rData["data"]);
|
|
|
|
LOKTrigger::trigger_changed(*pEdit);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 14:44:24 +02:00
|
|
|
|
|
|
|
auto pTextView = dynamic_cast<weld::TextView*>(pWidget);
|
|
|
|
if (pTextView)
|
|
|
|
{
|
|
|
|
if (sAction == "change")
|
|
|
|
{
|
|
|
|
pTextView->set_text(rData["data"]);
|
|
|
|
LOKTrigger::trigger_changed(*pTextView);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 11:01:54 +02:00
|
|
|
}
|
2020-10-30 10:51:08 +01:00
|
|
|
else if (sControlType == "treeview")
|
|
|
|
{
|
2020-12-17 11:02:40 -04:00
|
|
|
auto pTreeView = dynamic_cast<JSTreeView*>(pWidget);
|
2020-10-30 10:51:08 +01:00
|
|
|
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);
|
|
|
|
int nRow = std::atoi(nRowString.getStr());
|
|
|
|
|
2020-12-07 09:49:01 +01:00
|
|
|
pTreeView->unselect(pTreeView->get_selected_index());
|
2020-10-30 10:51:08 +01:00
|
|
|
pTreeView->select(nRow);
|
2020-12-07 09:49:01 +01:00
|
|
|
pTreeView->set_cursor(nRow);
|
2020-11-20 12:23:45 +01:00
|
|
|
LOKTrigger::trigger_changed(*pTreeView);
|
2021-01-13 09:13:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (sAction == "activate")
|
|
|
|
{
|
|
|
|
OString nRowString
|
|
|
|
= OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US);
|
|
|
|
int nRow = std::atoi(nRowString.getStr());
|
|
|
|
|
|
|
|
pTreeView->unselect(pTreeView->get_selected_index());
|
|
|
|
pTreeView->select(nRow);
|
|
|
|
pTreeView->set_cursor(nRow);
|
|
|
|
LOKTrigger::trigger_changed(*pTreeView);
|
|
|
|
LOKTrigger::trigger_row_activated(*pTreeView);
|
2020-11-18 10:12:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-12-17 11:02:40 -04:00
|
|
|
else if (sAction == "expand")
|
|
|
|
{
|
|
|
|
OString nRowString
|
|
|
|
= OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US);
|
|
|
|
int nAbsPos = std::atoi(nRowString.getStr());
|
|
|
|
std::unique_ptr<weld::TreeIter> itEntry(pTreeView->make_iterator());
|
|
|
|
pTreeView->get_iter_abs_pos(*itEntry, nAbsPos);
|
|
|
|
pTreeView->expand_row(*itEntry);
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-18 10:12:38 +01:00
|
|
|
else if (sAction == "dragstart")
|
|
|
|
{
|
|
|
|
OString nRowString
|
|
|
|
= OUStringToOString(rData["data"], RTL_TEXTENCODING_ASCII_US);
|
|
|
|
int nRow = std::atoi(nRowString.getStr());
|
|
|
|
|
|
|
|
pTreeView->select(nRow);
|
2020-12-17 11:02:40 -04:00
|
|
|
pTreeView->drag_start();
|
2020-11-18 10:12:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (sAction == "dragend")
|
|
|
|
{
|
2020-12-17 11:02:40 -04:00
|
|
|
pTreeView->drag_end();
|
2020-11-18 10:12:38 +01:00
|
|
|
return true;
|
2020-10-30 10:51:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 09:47:33 +01:00
|
|
|
else if (sControlType == "iconview")
|
|
|
|
{
|
|
|
|
auto pIconView = dynamic_cast<weld::IconView*>(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 14:32:04 +01:00
|
|
|
else if (sControlType == "expander")
|
|
|
|
{
|
|
|
|
auto pExpander = dynamic_cast<weld::Expander*>(pWidget);
|
|
|
|
if (pExpander)
|
|
|
|
{
|
|
|
|
if (sAction == "toggle")
|
|
|
|
{
|
|
|
|
pExpander->set_expanded(!pExpander->get_expanded());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 12:14:35 +01:00
|
|
|
else if (sControlType == "dialog")
|
|
|
|
{
|
|
|
|
auto pDialog = dynamic_cast<weld::Dialog*>(pWidget);
|
|
|
|
if (pDialog)
|
|
|
|
{
|
|
|
|
if (sAction == "close")
|
|
|
|
{
|
|
|
|
pDialog->response(RET_CANCEL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 09:19:03 +01:00
|
|
|
else if (sControlType == "radiobutton")
|
|
|
|
{
|
|
|
|
auto pRadioButton = dynamic_cast<weld::RadioButton*>(pWidget);
|
|
|
|
if (pRadioButton)
|
|
|
|
{
|
|
|
|
if (sAction == "change")
|
|
|
|
{
|
|
|
|
bool bChecked = rData["data"] == "true";
|
|
|
|
pRadioButton->set_state(bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
|
|
|
|
LOKTrigger::trigger_clicked(*static_cast<weld::Button*>(pRadioButton));
|
|
|
|
LOKTrigger::trigger_toggled(*static_cast<weld::ToggleButton*>(pRadioButton));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 11:01:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|