tdf#134331 - Added possibility to sort macros via context menu

The user now has the possibility to sort the entries in various macro
dialogs either alphabetically or in document order using a context menu.

Change-Id: I064947daa9f9bca05cb28e4b2f65ff1c92689ae9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/98783
Tested-by: Jenkins
Reviewed-by: Maxim Monastirsky <momonasmon@gmail.com>
diff --git a/basctl/UIConfig_basicide.mk b/basctl/UIConfig_basicide.mk
index 7593e5a..2c23fbf 100644
--- a/basctl/UIConfig_basicide.mk
+++ b/basctl/UIConfig_basicide.mk
@@ -53,6 +53,7 @@ $(eval $(call gb_UIConfig_add_uifiles,modules/BasicIDE,\
	basctl/uiconfig/basicide/ui/modulepage \
	basctl/uiconfig/basicide/ui/newlibdialog \
	basctl/uiconfig/basicide/ui/organizedialog \
	basctl/uiconfig/basicide/ui/sortmenu \
))

# vim: set noet sw=4 ts=4:
diff --git a/basctl/source/basicide/macrodlg.cxx b/basctl/source/basicide/macrodlg.cxx
index 0ee787b..24cb210 100644
--- a/basctl/source/basicide/macrodlg.cxx
+++ b/basctl/source/basicide/macrodlg.cxx
@@ -39,6 +39,7 @@
#include <sfx2/request.hxx>
#include <sfx2/sfxsids.hrc>
#include <tools/debug.hxx>
#include <vcl/commandevent.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <osl/diagnose.h>
@@ -76,8 +77,6 @@ MacroChooser::MacroChooser(weld::Window* pParnt, const Reference< frame::XFrame 
{
    m_xBasicBox->set_size_request(m_xBasicBox->get_approximate_digit_width() * 30, m_xBasicBox->get_height_rows(18));
    m_xMacroBox->set_size_request(m_xMacroBox->get_approximate_digit_width() * 30, m_xMacroBox->get_height_rows(18));
    // tdf#70813 The macros should be listed alphabetically
    m_xMacroBox->make_sorted();

    m_aMacrosInTxtBaseStr = m_xMacrosInTxt->get_label();

@@ -102,6 +101,7 @@ MacroChooser::MacroChooser(weld::Window* pParnt, const Reference< frame::XFrame 

    m_xMacroBox->connect_row_activated( LINK( this, MacroChooser, MacroDoubleClickHdl ) );
    m_xMacroBox->connect_changed( LINK( this, MacroChooser, MacroSelectHdl ) );
    m_xMacroBox->connect_popup_menu( LINK( this, MacroChooser, ContextMenuHdl ) );

    m_xBasicBox->SetMode( BrowseMode::Modules );

@@ -755,6 +755,35 @@ IMPL_LINK(MacroChooser, ButtonHdl, weld::Button&, rButton, void)
    }
}

IMPL_LINK(MacroChooser, ContextMenuHdl, const CommandEvent&, rCEvt, bool)
{
    if (rCEvt.GetCommand() != CommandEventId::ContextMenu || !m_xMacroBox->n_children())
         return false;

    std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xMacroBox.get(), "modules/BasicIDE/ui/sortmenu.ui"));
    std::unique_ptr<weld::Menu> xPopup(xBuilder->weld_menu("sortmenu"));
    std::unique_ptr<weld::Menu> xDropMenu(xBuilder->weld_menu("sortsubmenu"));
    xDropMenu->set_active("alphabetically", m_xMacroBox->get_sort_order());
    xDropMenu->set_active("properorder", !m_xMacroBox->get_sort_order());

    OString sCommand(xPopup->popup_at_rect(m_xMacroBox.get(), tools::Rectangle(rCEvt.GetMousePosPixel(), Size(1,1))));
    if (sCommand == "alphabetically")
    {
        m_xMacroBox->make_sorted();
    }
    else if (sCommand == "properorder")
    {
        m_xMacroBox->make_unsorted();
        BasicSelectHdl(m_xBasicBox->get_widget());
    }
    else if (!sCommand.isEmpty())
    {
         SAL_WARN("basctl.basicide", "Unknown context menu action: " << sCommand );
    }

    return true;
}

void MacroChooser::UpdateFields()
{
    auto nMacroEntry = m_xMacroBox->get_selected_index();
diff --git a/basctl/source/basicide/macrodlg.hxx b/basctl/source/basicide/macrodlg.hxx
index 0d23947..0e50ee5 100644
--- a/basctl/source/basicide/macrodlg.hxx
+++ b/basctl/source/basicide/macrodlg.hxx
@@ -58,6 +58,7 @@ private:
    DECL_LINK(MacroDoubleClickHdl, weld::TreeView&, bool);
    DECL_LINK(BasicSelectHdl, weld::TreeView&, void);
    DECL_LINK(EditModifyHdl, weld::Entry&, void);
    DECL_LINK(ContextMenuHdl, const CommandEvent&, bool);
    DECL_LINK(ButtonHdl, weld::Button&, void);

    void                CheckButtons();
diff --git a/basctl/uiconfig/basicide/ui/sortmenu.ui b/basctl/uiconfig/basicide/ui/sortmenu.ui
new file mode 100644
index 0000000..c82c5ab
--- /dev/null
+++ b/basctl/uiconfig/basicide/ui/sortmenu.ui
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface domain="basctl">
  <requires lib="gtk+" version="3.18"/>
  <object class="GtkMenu" id="sortmenu">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkMenuItem">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes" context="sortmenu|macrosort">_Sorting</property>
        <property name="use_underline">True</property>
        <child type="submenu">
          <object class="GtkMenu" id="sortsubmenu">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkRadioMenuItem" id="alphabetically">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes" context="sortmenu|alphabetically">_Alphabetically</property>
                <property name="use_underline">True</property>
                <property name="draw_as_radio">True</property>
                <property name="group">properorder</property>
              </object>
            </child>
            <child>
              <object class="GtkRadioMenuItem" id="properorder">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes" context="sortmenu|properorder">_Proper order</property>
                <property name="draw_as_radio">True</property>
                <property name="use_underline">True</property>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>
\ No newline at end of file
diff --git a/cui/source/customize/cfgutil.cxx b/cui/source/customize/cfgutil.cxx
index 13203e0..f1c5cce 100644
--- a/cui/source/customize/cfgutil.cxx
+++ b/cui/source/customize/cfgutil.cxx
@@ -49,6 +49,7 @@
#include <osl/diagnose.h>
#include <dialmgr.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/commandevent.hxx>
#include <vcl/commandinfoprovider.hxx>
#include <vcl/help.hxx>
#include <vcl/svapp.hxx>
@@ -1105,6 +1106,7 @@ SvxScriptSelectorDialog::SvxScriptSelectorDialog(
            LINK( this, SvxScriptSelectorDialog, SelectHdl ) );
    m_xCommands->connect_changed( LINK( this, SvxScriptSelectorDialog, SelectHdl ) );
    m_xCommands->connect_row_activated( LINK( this, SvxScriptSelectorDialog, FunctionDoubleClickHdl ) );
    m_xCommands->connect_popup_menu( LINK( this, SvxScriptSelectorDialog, ContextMenuHdl ) );

    m_xOKButton->connect_clicked( LINK( this, SvxScriptSelectorDialog, ClickHdl ) );
    m_xCancelButton->connect_clicked( LINK( this, SvxScriptSelectorDialog, ClickHdl ) );
@@ -1145,6 +1147,36 @@ IMPL_LINK_NOARG(SvxScriptSelectorDialog, FunctionDoubleClickHdl, weld::TreeView&
    return true;
}

IMPL_LINK(SvxScriptSelectorDialog, ContextMenuHdl, const CommandEvent&, rCEvt, bool)
{
    weld::TreeView& xTreeView = m_xCommands->get_widget();
    if (rCEvt.GetCommand() != CommandEventId::ContextMenu || !xTreeView.n_children())
         return false;

    std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(&xTreeView, "modules/BasicIDE/ui/sortmenu.ui"));
    std::unique_ptr<weld::Menu> xPopup(xBuilder->weld_menu("sortmenu"));
    std::unique_ptr<weld::Menu> xDropMenu(xBuilder->weld_menu("sortsubmenu"));
    xDropMenu->set_active("alphabetically", xTreeView.get_sort_order());
    xDropMenu->set_active("properorder", !xTreeView.get_sort_order());

    OString sCommand(xPopup->popup_at_rect(&xTreeView, tools::Rectangle(rCEvt.GetMousePosPixel(), Size(1,1))));
    if (sCommand == "alphabetically")
    {
        xTreeView.make_sorted();
    }
    else if (sCommand == "properorder")
    {
        xTreeView.make_unsorted();
        m_xCategories->GroupSelected();
    }
    else if (!sCommand.isEmpty())
    {
         SAL_WARN("cui.customize", "Unknown context menu action: " << sCommand );
    }

    return true;
}

// Check if command is selected and enable the OK button accordingly
// Grab the help text for this id if available and update the description field
void
diff --git a/cui/source/inc/cfgutil.hxx b/cui/source/inc/cfgutil.hxx
index b9ee134..c4d50e2 100644
--- a/cui/source/inc/cfgutil.hxx
+++ b/cui/source/inc/cfgutil.hxx
@@ -111,6 +111,7 @@ public:
    CuiConfigFunctionListBox(std::unique_ptr<weld::TreeView> xTreeView);
    void set_sensitive(bool bSensitive) { m_xTreeView->set_sensitive(bSensitive); }
    void connect_changed(const Link<weld::TreeView&, void>& rLink) { m_xTreeView->connect_changed(rLink); }
    void connect_popup_menu(const Link<const CommandEvent&, bool>& rLink) { m_xTreeView->connect_popup_menu(rLink); }
    void connect_row_activated(const Link<weld::TreeView&, bool>& rLink) { m_xTreeView->connect_row_activated(rLink); }
    void freeze() { m_xTreeView->freeze(); }
    void thaw() { m_xTreeView->thaw(); }
@@ -245,6 +246,7 @@ class SvxScriptSelectorDialog : public weld::GenericDialogController
    DECL_LINK(ClickHdl, weld::Button&, void);
    DECL_LINK(SelectHdl, weld::TreeView&, void);
    DECL_LINK(FunctionDoubleClickHdl, weld::TreeView&, bool);
    DECL_LINK(ContextMenuHdl, const CommandEvent&, bool);

    void                            UpdateUI();