/* -*- 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 "ParaSpacingWindow.hxx"
#include <editeng/editids.hrc>
#include <editeng/lrspitem.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/app.hxx>
#include <sfx2/viewfrm.hxx>
#include <svl/itempool.hxx>

using namespace svx;

#define DEFAULT_VALUE          0
#define MAX_DURCH             5670
#define MAX_SW                  1709400
#define MAX_SC_SD               116220200
#define NEGA_MAXVALUE          -10000000

// ParaULSpacingWindow

ParaULSpacingWindow::ParaULSpacingWindow(vcl::Window* pParent)
    : InterimItemWindow(pParent, "svx/ui/paraulspacing.ui", "ParaULSpacingWindow")
    , m_eUnit(MapUnit::MapTwip)
{
    m_xAboveSpacing = std::make_unique<SvxRelativeField>(m_xBuilder->weld_metric_spin_button("aboveparaspacing", FieldUnit::CM));
    m_xBelowSpacing = std::make_unique<SvxRelativeField>(m_xBuilder->weld_metric_spin_button("belowparaspacing", FieldUnit::CM));
    m_xAboveContainer = m_xBuilder->weld_container("above");
    m_xBelowContainer = m_xBuilder->weld_container("below");

    Link<weld::MetricSpinButton&,void> aLink = LINK(this, ParaULSpacingWindow, ModifySpacingHdl);
    m_xAboveSpacing->connect_value_changed(aLink);
    m_xBelowSpacing->connect_value_changed(aLink);

    /// set the initial values of max width
    m_xAboveSpacing->set_max(m_xAboveSpacing->normalize(MAX_DURCH), FieldUnit::CM);
    m_xBelowSpacing->set_max(m_xBelowSpacing->normalize(MAX_DURCH), FieldUnit::CM);
}

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

void ParaULSpacingWindow::dispose()
{
    m_xAboveSpacing.reset();
    m_xBelowSpacing.reset();
    m_xAboveContainer.reset();
    m_xBelowContainer.reset();

    InterimItemWindow::dispose();
}

void ParaULSpacingWindow::SetUnit(FieldUnit eUnit)
{
    m_xAboveSpacing->SetFieldUnit(eUnit);
    m_xBelowSpacing->SetFieldUnit(eUnit);

    SfxItemPool &rPool = SfxGetpApp()->GetPool();
    sal_uInt16 nWhich = rPool.GetWhich(SID_ATTR_PARA_ULSPACE);
    m_eUnit = rPool.GetMetric(nWhich);

    m_xAboveSpacing->set_max(m_xAboveSpacing->normalize(MAX_DURCH), MapToFieldUnit(m_eUnit));
    m_xBelowSpacing->set_max(m_xBelowSpacing->normalize(MAX_DURCH), MapToFieldUnit(m_eUnit));
}

void ParaULSpacingWindow::SetValue(const SvxULSpaceItem* pItem)
{
    sal_Int64 nVal = pItem->GetUpper();
    nVal = m_xAboveSpacing->normalize(nVal);
    m_xAboveSpacing->set_value(nVal, FieldUnit::MM_100TH);

    nVal = pItem->GetLower();
    nVal = m_xBelowSpacing->normalize(nVal);
    m_xBelowSpacing->set_value(nVal, FieldUnit::MM_100TH);
}

IMPL_LINK_NOARG(ParaULSpacingWindow, ModifySpacingHdl, weld::MetricSpinButton&, void)
{
    SfxDispatcher* pDisp = SfxViewFrame::Current()->GetBindings().GetDispatcher();
    if(pDisp)
    {
        SvxULSpaceItem aMargin(SID_ATTR_PARA_ULSPACE);
        aMargin.SetUpper(m_xAboveSpacing->GetCoreValue(m_eUnit));
        aMargin.SetLower(m_xBelowSpacing->GetCoreValue(m_eUnit));
        pDisp->ExecuteList(SID_ATTR_PARA_ULSPACE, SfxCallMode::RECORD, {&aMargin});
    }
}

// ParaAboveSpacingWindow

ParaAboveSpacingWindow::ParaAboveSpacingWindow(vcl::Window* pParent)
    : ParaULSpacingWindow(pParent)
{
    m_xAboveContainer->show();
    m_xBelowContainer->hide();

    SetSizePixel(get_preferred_size());
}

void ParaAboveSpacingWindow::GetFocus()
{
    if (m_xAboveSpacing)
        m_xAboveSpacing->grab_focus();
    ParaULSpacingWindow::GetFocus();
}

// ParaBelowSpacingWindow

ParaBelowSpacingWindow::ParaBelowSpacingWindow(vcl::Window* pParent)
    : ParaULSpacingWindow(pParent)
{
    m_xAboveContainer->hide();
    m_xBelowContainer->show();

    SetSizePixel(get_preferred_size());
}

void ParaBelowSpacingWindow::GetFocus()
{
    if (m_xBelowSpacing)
        m_xBelowSpacing->grab_focus();
    ParaULSpacingWindow::GetFocus();
}

// ParaLRSpacingWindow

ParaLRSpacingWindow::ParaLRSpacingWindow(vcl::Window* pParent)
    : InterimItemWindow(pParent, "svx/ui/paralrspacing.ui", "ParaLRSpacingWindow")
    , m_eUnit(MapUnit::MapTwip)
{
    m_xBeforeSpacing = std::make_unique<SvxRelativeField>(m_xBuilder->weld_metric_spin_button("beforetextindent", FieldUnit::CM));
    m_xAfterSpacing = std::make_unique<SvxRelativeField>(m_xBuilder->weld_metric_spin_button("aftertextindent", FieldUnit::CM));
    m_xFLSpacing = std::make_unique<SvxRelativeField>(m_xBuilder->weld_metric_spin_button("firstlineindent", FieldUnit::CM));
    m_xBeforeContainer = m_xBuilder->weld_container("before");
    m_xAfterContainer = m_xBuilder->weld_container("after");
    m_xFirstLineContainer = m_xBuilder->weld_container("firstline");

    Link<weld::MetricSpinButton&,void> aLink = LINK(this, ParaLRSpacingWindow, ModifySpacingHdl);
    m_xBeforeSpacing->connect_value_changed(aLink);
    m_xAfterSpacing->connect_value_changed(aLink);
    m_xFLSpacing->connect_value_changed(aLink);

    /// set the initial values of max width
    m_xBeforeSpacing->set_min(NEGA_MAXVALUE, FieldUnit::MM_100TH);
    m_xAfterSpacing->set_min(NEGA_MAXVALUE, FieldUnit::MM_100TH);
    m_xFLSpacing->set_min(NEGA_MAXVALUE, FieldUnit::MM_100TH);
}

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

void ParaLRSpacingWindow::dispose()
{
    m_xBeforeSpacing.reset();
    m_xAfterSpacing.reset();
    m_xFLSpacing.reset();
    m_xBeforeContainer.reset();
    m_xAfterContainer.reset();
    m_xFirstLineContainer.reset();

    InterimItemWindow::dispose();
}

void ParaLRSpacingWindow::SetContext(const vcl::EnumContext& eContext)
{
    m_aContext = eContext;
}

void ParaLRSpacingWindow::SetValue(SfxItemState eState, const SfxPoolItem* pState)
{
    switch(m_aContext.GetCombinedContext_DI())
    {

    case CombinedEnumContext(Application::WriterVariants, Context::DrawText):
    case CombinedEnumContext(Application::WriterVariants, Context::Annotation):
    case CombinedEnumContext(Application::Calc, Context::DrawText):
    case CombinedEnumContext(Application::DrawImpress, Context::DrawText):
    case CombinedEnumContext(Application::DrawImpress, Context::Draw):
    case CombinedEnumContext(Application::DrawImpress, Context::TextObject):
    case CombinedEnumContext(Application::DrawImpress, Context::Graphic):
    case CombinedEnumContext(Application::DrawImpress, Context::Table):
        {
            m_xBeforeSpacing->set_min(DEFAULT_VALUE, FieldUnit::NONE);
            m_xAfterSpacing->set_min(DEFAULT_VALUE, FieldUnit::NONE);
            m_xFLSpacing->set_min(DEFAULT_VALUE, FieldUnit::NONE);
        }
        break;
    case CombinedEnumContext(Application::WriterVariants, Context::Default):
    case CombinedEnumContext(Application::WriterVariants, Context::Text):
    case CombinedEnumContext(Application::WriterVariants, Context::Table):
        {
            m_xBeforeSpacing->set_min(NEGA_MAXVALUE, FieldUnit::MM_100TH);
            m_xAfterSpacing->set_min(NEGA_MAXVALUE, FieldUnit::MM_100TH);
            m_xFLSpacing->set_min(NEGA_MAXVALUE, FieldUnit::MM_100TH);
        }
        break;
    }

    if(pState && eState >= SfxItemState::DEFAULT)
    {
        m_xBeforeSpacing->set_sensitive(true);
        m_xAfterSpacing->set_sensitive(true);
        m_xFLSpacing->set_sensitive(true);

        const SvxLRSpaceItem* pSpace = static_cast<const SvxLRSpaceItem*>(pState);
        long aTxtLeft = pSpace->GetTextLeft();
        long aTxtRight = pSpace->GetRight();
        long aTxtFirstLineOfst = pSpace->GetTextFirstLineOffset();

        aTxtLeft = m_xBeforeSpacing->normalize(aTxtLeft);

        if(m_aContext.GetCombinedContext_DI() != CombinedEnumContext(Application::WriterVariants, Context::Text)
             && m_aContext.GetCombinedContext_DI() != CombinedEnumContext(Application::WriterVariants, Context::Default)
             && m_aContext.GetCombinedContext_DI() != CombinedEnumContext(Application::WriterVariants, Context::Table))
        {
            m_xFLSpacing->set_min(aTxtLeft*-1, FieldUnit::MM_100TH);
        }

        aTxtRight = m_xAfterSpacing->normalize(aTxtRight);

        switch(m_aContext.GetCombinedContext_DI())
        {
        case CombinedEnumContext(Application::WriterVariants, Context::DrawText):
        case CombinedEnumContext(Application::WriterVariants, Context::Text):
        case CombinedEnumContext(Application::WriterVariants, Context::Default):
        case CombinedEnumContext(Application::WriterVariants, Context::Table):
        case CombinedEnumContext(Application::WriterVariants, Context::Annotation):
            {
                m_xBeforeSpacing->set_max(MAX_SW - aTxtRight, FieldUnit::MM_100TH);
                m_xAfterSpacing->set_max(MAX_SW - aTxtLeft, FieldUnit::MM_100TH);
                m_xFLSpacing->set_max(MAX_SW - aTxtLeft - aTxtRight, FieldUnit::MM_100TH);
            }
            break;
        case CombinedEnumContext(Application::DrawImpress, Context::DrawText):
        case CombinedEnumContext(Application::DrawImpress, Context::Draw):
        case CombinedEnumContext(Application::DrawImpress, Context::Table):
        case CombinedEnumContext(Application::DrawImpress, Context::TextObject):
        case CombinedEnumContext(Application::DrawImpress, Context::Graphic):
            {
                m_xBeforeSpacing->set_max(MAX_SC_SD - aTxtRight, FieldUnit::MM_100TH);
                m_xAfterSpacing->set_max(MAX_SC_SD - aTxtLeft, FieldUnit::MM_100TH);
                m_xFLSpacing->set_max(MAX_SC_SD - aTxtLeft - aTxtRight, FieldUnit::MM_100TH);
            }
        }

        m_xBeforeSpacing->set_value(aTxtLeft, FieldUnit::MM_100TH);
        m_xAfterSpacing->set_value(aTxtRight, FieldUnit::MM_100TH);

        aTxtFirstLineOfst = m_xFLSpacing->normalize(aTxtFirstLineOfst);
        m_xFLSpacing->set_value(aTxtFirstLineOfst, FieldUnit::MM_100TH);
    }
    else if(eState == SfxItemState::DISABLED)
    {
        m_xBeforeSpacing->set_sensitive(false);
        m_xAfterSpacing->set_sensitive(false);
        m_xFLSpacing->set_sensitive(false);
    }
    else
    {
        m_xBeforeSpacing->set_text("");
        m_xAfterSpacing->set_text("");
        m_xFLSpacing->set_text("");
    }
}

void ParaLRSpacingWindow::SetUnit(FieldUnit eUnit)
{
    m_xBeforeSpacing->SetFieldUnit(eUnit);
    m_xAfterSpacing->SetFieldUnit(eUnit);
    m_xFLSpacing->SetFieldUnit(eUnit);

    SfxItemPool &rPool = SfxGetpApp()->GetPool();
    sal_uInt16 nWhich = rPool.GetWhich(SID_ATTR_PARA_LRSPACE);
    m_eUnit = rPool.GetMetric(nWhich);
}

IMPL_LINK_NOARG(ParaLRSpacingWindow, ModifySpacingHdl, weld::MetricSpinButton&, void)
{
    SfxDispatcher* pDisp = SfxViewFrame::Current()->GetBindings().GetDispatcher();
    if(pDisp)
    {
        SvxLRSpaceItem aMargin(SID_ATTR_PARA_LRSPACE);
        aMargin.SetTextLeft(m_xBeforeSpacing->GetCoreValue(m_eUnit));
        aMargin.SetRight(m_xAfterSpacing->GetCoreValue(m_eUnit));
        aMargin.SetTextFirstLineOffset(m_xFLSpacing->GetCoreValue(m_eUnit));

        pDisp->ExecuteList(SID_ATTR_PARA_LRSPACE, SfxCallMode::RECORD, {&aMargin});
    }
}

// ParaLeftSpacingWindow

ParaLeftSpacingWindow::ParaLeftSpacingWindow(vcl::Window* pParent)
    : ParaLRSpacingWindow(pParent)
{
    m_xBeforeContainer->show();
    m_xAfterContainer->hide();
    m_xFirstLineContainer->hide();

    SetSizePixel(get_preferred_size());
}

void ParaLeftSpacingWindow::GetFocus()
{
    if (m_xBeforeSpacing)
        m_xBeforeSpacing->grab_focus();
    ParaLRSpacingWindow::GetFocus();
}

// ParaRightSpacingWindow

ParaRightSpacingWindow::ParaRightSpacingWindow(vcl::Window* pParent)
    : ParaLRSpacingWindow(pParent)
{
    m_xBeforeContainer->hide();
    m_xAfterContainer->show();
    m_xFirstLineContainer->hide();

    SetSizePixel(get_preferred_size());
}

void ParaRightSpacingWindow::GetFocus()
{
    if (m_xAfterSpacing)
        m_xAfterSpacing->grab_focus();
    ParaLRSpacingWindow::GetFocus();
}

// ParaFirstLineSpacingWindow

ParaFirstLineSpacingWindow::ParaFirstLineSpacingWindow(vcl::Window* pParent)
    : ParaLRSpacingWindow(pParent)
{
    m_xBeforeContainer->hide();
    m_xAfterContainer->hide();
    m_xFirstLineContainer->show();

    SetSizePixel(get_preferred_size());
}

void ParaFirstLineSpacingWindow::GetFocus()
{
    if (m_xFLSpacing)
        m_xFLSpacing->grab_focus();
    ParaLRSpacingWindow::GetFocus();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
