split SvxFmAbsRecWin up to make a reusable piece

Change-Id: I3c9992d39f7dd45ac354b8e4087bacfd9715b234
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99246
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/include/svx/recorditemwindow.hxx b/include/svx/recorditemwindow.hxx
new file mode 100644
index 0000000..d49db97
--- /dev/null
+++ b/include/svx/recorditemwindow.hxx
@@ -0,0 +1,37 @@
/* -*- 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/.
 */

#pragma once

#include <vcl/InterimItemWindow.hxx>
#include <svx/svxdllapi.h>

class SVXCORE_DLLPUBLIC RecordItemWindow : public InterimItemWindow
{
public:
    RecordItemWindow(vcl::Window* _pParent);
    virtual void dispose() override;
    virtual ~RecordItemWindow() override;

    void set_text(const OUString& rText) { m_xWidget->set_text(rText); }

private:
    virtual void PositionFired(sal_Int64 nRecord) = 0;

    std::unique_ptr<weld::Entry> m_xWidget;

    DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
    DECL_LINK(ActivatedHdl, weld::Entry&, bool);
    // for invalidating our content when losing the focus
    DECL_LINK(FocusOutHdl, weld::Widget&, void);

    void FirePosition(bool bForce);
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/svx/Library_svxcore.mk b/svx/Library_svxcore.mk
index 73d22ca..bd9cf30 100644
--- a/svx/Library_svxcore.mk
+++ b/svx/Library_svxcore.mk
@@ -469,6 +469,7 @@ $(eval $(call gb_Library_add_exception_objects,svxcore,\
    svx/source/form/navigatortree \
    svx/source/form/navigatortreemodel \
    svx/source/form/ParseContext \
    svx/source/form/recorditemwindow \
    svx/source/form/sdbdatacolumn \
    svx/source/form/sqlparserclient \
    svx/source/form/typemap \
diff --git a/svx/source/form/recorditemwindow.cxx b/svx/source/form/recorditemwindow.cxx
new file mode 100644
index 0000000..26920ee
--- /dev/null
+++ b/svx/source/form/recorditemwindow.cxx
@@ -0,0 +1,89 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <vcl/event.hxx>
#include <tbxform.hxx>

RecordItemWindow::RecordItemWindow(vcl::Window* pParent)
    : InterimItemWindow(pParent, "svx/ui/absrecbox.ui", "AbsRecBox")
    , m_xWidget(m_xBuilder->weld_entry("entry"))
{
    InitControlBase(m_xWidget.get());

    m_xWidget->connect_key_press(LINK(this, RecordItemWindow, KeyInputHdl));
    m_xWidget->connect_activate(LINK(this, RecordItemWindow, ActivatedHdl));
    m_xWidget->connect_focus_out(LINK(this, RecordItemWindow, FocusOutHdl));

    SetSizePixel(m_xWidget->get_preferred_size());
}

void RecordItemWindow::dispose()
{
    m_xWidget.reset();
    InterimItemWindow::dispose();
}

RecordItemWindow::~RecordItemWindow() { disposeOnce(); }

void RecordItemWindow::FirePosition(bool _bForce)
{
    if (!_bForce && !m_xWidget->get_value_changed_from_saved())
        return;

    sal_Int64 nRecord = m_xWidget->get_text().toInt64();
    if (nRecord < 1)
        nRecord = 1;

    PositionFired(nRecord);

    m_xWidget->save_value();
}

IMPL_LINK_NOARG(RecordItemWindow, FocusOutHdl, weld::Widget&, void) { FirePosition(false); }

IMPL_LINK(RecordItemWindow, KeyInputHdl, const KeyEvent&, rKEvt, bool)
{
    vcl::KeyCode aCode = rKEvt.GetKeyCode();
    bool bUp = (aCode.GetCode() == KEY_UP);
    bool bDown = (aCode.GetCode() == KEY_DOWN);

    if (!aCode.IsShift() && !aCode.IsMod1() && !aCode.IsMod2() && (bUp || bDown))
    {
        sal_Int64 nRecord = m_xWidget->get_text().toInt64();
        if (bUp)
            ++nRecord;
        else
            --nRecord;
        if (nRecord < 1)
            nRecord = 1;
        m_xWidget->set_text(OUString::number(nRecord));
        return true;
    }

    return ChildKeyInput(rKEvt);
}

IMPL_LINK_NOARG(RecordItemWindow, ActivatedHdl, weld::Entry&, bool)
{
    if (!m_xWidget->get_text().isEmpty())
        FirePosition(true);
    return true;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/form/tbxform.cxx b/svx/source/form/tbxform.cxx
index 26ff4ad..97aac28 100644
--- a/svx/source/form/tbxform.cxx
+++ b/svx/source/form/tbxform.cxx
@@ -34,39 +34,13 @@ using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;

SvxFmAbsRecWin::SvxFmAbsRecWin(vcl::Window* pParent, SfxToolBoxControl* pController)
    : InterimItemWindow(pParent, "svx/ui/absrecbox.ui", "AbsRecBox")
    , m_xWidget(m_xBuilder->weld_entry("entry"))
    : RecordItemWindow(pParent)
    , m_pController(pController)
{
    InitControlBase(m_xWidget.get());

    m_xWidget->connect_key_press(LINK(this, SvxFmAbsRecWin, KeyInputHdl));
    m_xWidget->connect_activate(LINK(this, SvxFmAbsRecWin, ActivatedHdl));
    m_xWidget->connect_focus_out(LINK(this, SvxFmAbsRecWin, FocusOutHdl));

    SetSizePixel(m_xWidget->get_preferred_size());
}

void SvxFmAbsRecWin::dispose()
void SvxFmAbsRecWin::PositionFired(sal_Int64 nRecord)
{
    m_xWidget.reset();
    InterimItemWindow::dispose();
}

SvxFmAbsRecWin::~SvxFmAbsRecWin()
{
    disposeOnce();
}

void SvxFmAbsRecWin::FirePosition( bool _bForce )
{
    if (!_bForce && !m_xWidget->get_value_changed_from_saved())
        return;

    sal_Int64 nRecord = m_xWidget->get_text().toInt64();
    if (nRecord < 1)
        nRecord = 1;

    SfxInt32Item aPositionParam( FN_PARAM_1, static_cast<sal_Int32>(nRecord) );

    Any a;
@@ -77,42 +51,6 @@ void SvxFmAbsRecWin::FirePosition( bool _bForce )
    m_pController->Dispatch( ".uno:AbsoluteRecord",
                             aArgs );
    m_pController->updateStatus();

    m_xWidget->save_value();
}

IMPL_LINK_NOARG(SvxFmAbsRecWin, FocusOutHdl, weld::Widget&, void)
{
    FirePosition( false );
}

IMPL_LINK(SvxFmAbsRecWin, KeyInputHdl, const KeyEvent&, rKEvt, bool)
{
    vcl::KeyCode aCode = rKEvt.GetKeyCode();
    bool bUp = (aCode.GetCode() == KEY_UP);
    bool bDown = (aCode.GetCode() == KEY_DOWN);

    if (!aCode.IsShift() && !aCode.IsMod1() && !aCode.IsMod2() && (bUp || bDown))
    {
        sal_Int64 nRecord = m_xWidget->get_text().toInt64();
        if (bUp)
            ++nRecord;
        else
            --nRecord;
        if (nRecord < 1)
            nRecord = 1;
        m_xWidget->set_text(OUString::number(nRecord));
        return true;
    }

    return ChildKeyInput(rKEvt);
}

IMPL_LINK_NOARG(SvxFmAbsRecWin, ActivatedHdl, weld::Entry&, bool)
{
    if (!m_xWidget->get_text().isEmpty())
        FirePosition( true );
    return true;
}

SFX_IMPL_TOOLBOX_CONTROL( SvxFmTbxCtlAbsRec, SfxInt32Item );
diff --git a/svx/source/inc/tbxform.hxx b/svx/source/inc/tbxform.hxx
index 153fc0c..a167b51 100644
--- a/svx/source/inc/tbxform.hxx
+++ b/svx/source/inc/tbxform.hxx
@@ -20,25 +20,15 @@
#define INCLUDED_SVX_SOURCE_INC_TBXFORM_HXX

#include <sfx2/tbxctrl.hxx>
#include <vcl/InterimItemWindow.hxx>
#include <svx/recorditemwindow.hxx>

class SvxFmAbsRecWin final : public InterimItemWindow
class SvxFmAbsRecWin final : public RecordItemWindow
{
public:
    SvxFmAbsRecWin( vcl::Window* _pParent, SfxToolBoxControl* _pController );
    virtual void dispose() override;
    virtual ~SvxFmAbsRecWin() override;

    void set_text(const OUString& rText) { m_xWidget->set_text(rText); }

private:
    std::unique_ptr<weld::Entry> m_xWidget;

    DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
    DECL_LINK(ActivatedHdl, weld::Entry&, bool);
    DECL_LINK(FocusOutHdl, weld::Widget&, void); // for invalidating our content when losing the focus

    void FirePosition( bool _bForce );
    virtual void PositionFired(sal_Int64 nRecord) override;

    SfxToolBoxControl*  m_pController;
};