diff options
author | Caolán McNamara <caolanm@redhat.com> | 2020-02-06 10:59:19 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2020-02-06 18:17:02 +0100 |
commit | 97a5da96159f28de0b846de7dd94d2880251e8b3 (patch) | |
tree | 091f4334ff184047376240a4deb5bfb9b0da5b1e /sd/source | |
parent | bf2f1adfd2e4725518147657c6b57eb5697a29d5 (diff) |
weld SvxMetricField
split off SdPagesField as its not really a MetricSpinButton and can
be a SpinButton with a custom output function.
while I'm at it use ngettext to provide better plurals for the output.
Change-Id: Idc9ce0513c9fce60c8713c7361fd40f0dbd01f64
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88100
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'sd/source')
-rw-r--r-- | sd/source/ui/app/sdmod.cxx | 5 | ||||
-rw-r--r-- | sd/source/ui/dlg/diactrl.cxx | 112 | ||||
-rw-r--r-- | sd/source/ui/inc/diactrl.hxx | 21 |
3 files changed, 96 insertions, 42 deletions
diff --git a/sd/source/ui/app/sdmod.cxx b/sd/source/ui/app/sdmod.cxx index 0a10c237501f..077ab4b259bd 100644 --- a/sd/source/ui/app/sdmod.cxx +++ b/sd/source/ui/app/sdmod.cxx @@ -85,6 +85,11 @@ OUString SdResId(const char* pId) return Translate::get(pId, SD_MOD()->GetResLocale()); } +OUString SdResId(const char* pId, int nCardinality) +{ + return Translate::nget(pId, nCardinality, SD_MOD()->GetResLocale()); +} + // Dtor SdModule::~SdModule() { diff --git a/sd/source/ui/dlg/diactrl.cxx b/sd/source/ui/dlg/diactrl.cxx index d05f4f1f16a0..37507c3b4e5b 100644 --- a/sd/source/ui/dlg/diactrl.cxx +++ b/sd/source/ui/dlg/diactrl.cxx @@ -17,6 +17,7 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <vcl/field.hxx> #include <svl/intitem.hxx> #include <vcl/toolbox.hxx> @@ -34,54 +35,95 @@ using namespace ::com::sun::star; SFX_IMPL_TOOLBOX_CONTROL( SdTbxCtlDiaPages, SfxUInt16Item ) +namespace +{ + OUString format_number(int nSlides) + { + OUString aSlides(SdResId(STR_SLIDES, nSlides)); + return aSlides.replaceFirst("%1", OUString::number(nSlides)); + } +} + // SdPagesField SdPagesField::SdPagesField( vcl::Window* pParent, - const uno::Reference< frame::XFrame >& rFrame ) : - SvxMetricField ( pParent, rFrame ), - m_xFrame ( rFrame ) + const uno::Reference< frame::XFrame >& rFrame ) + : InterimItemWindow(pParent, "modules/simpress/ui/pagesfieldbox.ui", "PagesFieldBox") + , m_xWidget(m_xBuilder->weld_spin_button("pagesfield")) + , m_xFrame(rFrame) { - OUString aStr( SdResId( STR_SLIDE_PLURAL ) ); - SetCustomUnitText( aStr ); - - // set size - aStr += "XXX"; - Size aSize( GetTextWidth( aStr )+20, GetTextHeight()+6 ); + // set parameter of MetricFields + m_xWidget->set_digits(0); + m_xWidget->set_range(1, 15); + m_xWidget->set_increments(1, 5); + m_xWidget->connect_value_changed(LINK(this, SdPagesField, ModifyHdl)); + m_xWidget->connect_output(LINK(this, SdPagesField, OutputHdl)); + m_xWidget->connect_input(LINK(this, SdPagesField, spin_button_input)); + m_xWidget->connect_key_press(LINK(this, SdPagesField, KeyInputHdl)); + + auto width = std::max(m_xWidget->get_pixel_size(format_number(1)).Width(), + m_xWidget->get_pixel_size(format_number(15)).Width()); + int chars = ceil(width / m_xWidget->get_approximate_digit_width()); + m_xWidget->set_width_chars(chars); + + SetSizePixel(m_xWidget->get_preferred_size()); +} - SetSizePixel( aSize ); +IMPL_LINK(SdPagesField, KeyInputHdl, const KeyEvent&, rKEvt, bool) +{ + return ChildKeyInput(rKEvt); +} - // set parameter of MetricFields - SetUnit( FieldUnit::CUSTOM ); - SetMin( 1 ); - SetFirst( 1 ); - SetMax( 15 ); - SetLast( 15 ); - SetSpinSize( 1 ); - SetDecimalDigits( 0 ); - Show(); +void SdPagesField::dispose() +{ + m_xWidget.reset(); + InterimItemWindow::dispose(); } SdPagesField::~SdPagesField() { + disposeOnce(); +} + +void SdPagesField::set_sensitive(bool bSensitive) +{ + Enable(bSensitive); + m_xWidget->set_sensitive(bSensitive); + if (!bSensitive) + m_xWidget->set_text(""); } void SdPagesField::UpdatePagesField( const SfxUInt16Item* pItem ) { - if( pItem ) + if (pItem) + m_xWidget->set_value(pItem->GetValue()); + else + m_xWidget->set_text(OUString()); +} + +IMPL_STATIC_LINK(SdPagesField, OutputHdl, weld::SpinButton&, rSpinButton, void) +{ + rSpinButton.set_text(format_number(rSpinButton.get_value())); +} + +IMPL_LINK(SdPagesField, spin_button_input, int*, result, bool) +{ + const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper(); + double fResult(0.0); + bool bRet = MetricFormatter::TextToValue(m_xWidget->get_text(), fResult, 0, m_xWidget->get_digits(), rLocaleData, FieldUnit::NONE); + if (bRet) { - long nValue = static_cast<long>(pItem->GetValue()); - SetValue( nValue ); - if( nValue == 1 ) - SetCustomUnitText( SdResId( STR_SLIDE_SINGULAR ) ); - else - SetCustomUnitText( SdResId( STR_SLIDE_PLURAL ) ); + if (fResult > SAL_MAX_INT32) + fResult = SAL_MAX_INT32; + else if (fResult < SAL_MIN_INT32) + fResult = SAL_MIN_INT32; + *result = fResult; } - else - SetText( OUString() ); + return bRet; } -void SdPagesField::Modify() +IMPL_LINK_NOARG(SdPagesField, ModifyHdl, weld::SpinButton&, void) { - SfxUInt16Item aItem( SID_PAGES_PER_ROW, static_cast<sal_uInt16>(GetValue()) ); + SfxUInt16Item aItem(SID_PAGES_PER_ROW, m_xWidget->get_value()); ::uno::Any a; ::uno::Sequence< ::beans::PropertyValue > aArgs( 1 ); @@ -110,12 +152,11 @@ void SdTbxCtlDiaPages::StateChanged( sal_uInt16, if ( eState == SfxItemState::DISABLED ) { - pFld->Disable(); - pFld->SetText( OUString() ); + pFld->set_sensitive(false); } else { - pFld->Enable(); + pFld->set_sensitive(true); const SfxUInt16Item* pItem = nullptr; if ( eState == SfxItemState::DEFAULT ) @@ -130,7 +171,10 @@ void SdTbxCtlDiaPages::StateChanged( sal_uInt16, VclPtr<vcl::Window> SdTbxCtlDiaPages::CreateItemWindow( vcl::Window* pParent ) { - return VclPtrInstance<SdPagesField>( pParent, m_xFrame ).get(); + VclPtr<SdPagesField> pWindow = VclPtr<SdPagesField>::Create(pParent, m_xFrame); + pWindow->Show(); + + return pWindow; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sd/source/ui/inc/diactrl.hxx b/sd/source/ui/inc/diactrl.hxx index 908731a80fb1..f9260ef4303a 100644 --- a/sd/source/ui/inc/diactrl.hxx +++ b/sd/source/ui/inc/diactrl.hxx @@ -20,7 +20,7 @@ #ifndef INCLUDED_SD_SOURCE_UI_INC_DIACTRL_HXX #define INCLUDED_SD_SOURCE_UI_INC_DIACTRL_HXX -#include <svx/itemwin.hxx> +#include <svx/InterimItemWindow.hxx> #include <sfx2/tbxctrl.hxx> namespace com { namespace sun { namespace star { namespace frame { class XFrame; } } } } @@ -28,17 +28,22 @@ class SfxUInt16Item; // SdPagesField: -class SdPagesField : public SvxMetricField +class SdPagesField final : public InterimItemWindow { private: - css::uno::Reference< css::frame::XFrame > m_xFrame; -protected: - virtual void Modify() override; + std::unique_ptr<weld::SpinButton> m_xWidget; + css::uno::Reference<css::frame::XFrame> m_xFrame; + + DECL_LINK(ModifyHdl, weld::SpinButton&, void); + DECL_STATIC_LINK(SdPagesField, OutputHdl, weld::SpinButton&, void); + DECL_LINK(spin_button_input, int* result, bool); + DECL_LINK(KeyInputHdl, const KeyEvent&, bool); public: - SdPagesField( vcl::Window* pParent, - const css::uno::Reference< css::frame::XFrame >& rFrame ); - virtual ~SdPagesField() override; + SdPagesField(vcl::Window* pParent, const css::uno::Reference<css::frame::XFrame>& rFrame); + virtual void dispose() override; + void set_sensitive(bool bSensitive); + virtual ~SdPagesField() override; void UpdatePagesField( const SfxUInt16Item* pItem ); }; |