[Libreoffice-commits] core.git: sw/inc sw/source

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Fri Oct 12 03:52:47 UTC 2018


 sw/inc/doc.hxx                    |    6 +++-
 sw/inc/fesh.hxx                   |    5 ---
 sw/source/core/docnode/ndtbl1.cxx |   51 +++++++++++++++++++++++---------------
 sw/source/core/frmedt/fetab.cxx   |    4 +-
 sw/source/uibase/shells/tabsh.cxx |    4 ++
 5 files changed, 42 insertions(+), 28 deletions(-)

New commits:
commit ab18c17d70e1dcf5cf9db38256d35e6af479373e
Author:     Justin Luth <justin_luth at sil.org>
AuthorDate: Sat Sep 22 10:15:32 2018 +0300
Commit:     Justin Luth <justin_luth at sil.org>
CommitDate: Fri Oct 12 05:52:21 2018 +0200

    tdf#64242 sw optimal column width, not minimize
    
    Optimize column width: Adjusts the width of the
    selected columns to fit the entire column's content,
    without changing the width of the table.
    Any leftover space is distributed proportionately,
    with thin columns growing slightly,
    and wide columns growing much wider.
    
    Change-Id: I9b8436814fc103d52fdd5ce3d88c6442dbb72d50
    Reviewed-on: https://gerrit.libreoffice.org/60905
    Tested-by: Jenkins
    Reviewed-by: Justin Luth <justin_luth at sil.org>

diff --git a/sw/inc/doc.hxx b/sw/inc/doc.hxx
index bdff80442ebe..ba88d6c88cb9 100644
--- a/sw/inc/doc.hxx
+++ b/sw/inc/doc.hxx
@@ -1460,7 +1460,11 @@ public:
     static bool GetBoxAttr( const SwCursor& rCursor, SfxPoolItem &rToFill );
     void SetBoxAlign( const SwCursor& rCursor, sal_uInt16 nAlign );
     static sal_uInt16 GetBoxAlign( const SwCursor& rCursor );
-    void AdjustCellWidth( const SwCursor& rCursor, bool bBalance );
+    /// Adjusts selected cell widths in such a way, that their content does not need to be wrapped (if possible).
+    /// bBalance evenly re-distributes the available space regardless of content or wrapping.
+    /// bNoShrink keeps table size the same by distributing excesss space proportionately.
+    /// bColumnWidth tests the entire column for content width, not just selected cells.
+    void AdjustCellWidth( const SwCursor& rCursor, const bool bBalance, const bool bNoShrink, const bool bColumnWidth );
 
     SwChainRet Chainable( const SwFrameFormat &rSource, const SwFrameFormat &rDest );
     SwChainRet Chain( SwFrameFormat &rSource, const SwFrameFormat &rDest );
diff --git a/sw/inc/fesh.hxx b/sw/inc/fesh.hxx
index f01fb6d9022a..b9e40d22d7c7 100644
--- a/sw/inc/fesh.hxx
+++ b/sw/inc/fesh.hxx
@@ -707,10 +707,7 @@ public:
     bool IsInRepeatedHeadline() const { return CheckHeadline( true ); }
     bool IsInHeadline() const { return CheckHeadline( false ); }
 
-    /** Adjusts cell widths in such a way, that their content
-     does not need to be wrapped (if possible).
-     bBalance provides for adjustment of selected columns. */
-    void AdjustCellWidth( bool bBalance );
+    void AdjustCellWidth( bool bBalance, const bool bNoShrink, const bool bColumnWidth );
 
     /// Not allowed if only empty cells are selected.
     bool IsAdjustCellWidthAllowed( bool bBalance = false ) const;
diff --git a/sw/source/core/docnode/ndtbl1.cxx b/sw/source/core/docnode/ndtbl1.cxx
index 70ca0acd17b4..fcec43de7db4 100644
--- a/sw/source/core/docnode/ndtbl1.cxx
+++ b/sw/source/core/docnode/ndtbl1.cxx
@@ -1373,29 +1373,29 @@ static void lcl_CalcSubColValues( std::vector<sal_uInt16> &rToFill, const SwTabC
 }
 
 /**
- * Retrievs new values to set the TabCols.
+ * Retrieves new values to set the TabCols.
  *
  * We do not iterate over the TabCols' entries, but over the gaps that describe Cells.
+ * We set TabCol entries for which we did not calculate Cells to 0.
  *
- * @param bWishValues == true:     We calculate the desired value of all affected
- *                                 Cells for the current Selection/current Cell.
- *                                 If more Cells are within a Column, the highest
- *                                 desired value is returned.
- *                                 We set TabCol entries for which we did not calculate
- *                                 Cells to 0.
- *
- * @param bWishValues == false:     The Selection is expanded vertically.
- *                                  We calculate the minimum value for every
- *                                  Column in the TabCols that intersects with the
- *                                  Selection.
+ * @param bWishValues == true:     Calculate the desired width of the content
+ *                                 The highest desired value is returned.
+ * @param bWishValues == false:    Calculate the minimum width of the content
+ * @param bColumnWidth == false:   We calculate the desired value of all affected
+ *                                 Cells for the current Selection only.
+ * @param bColumnWidth == true:     The Selection is expanded vertically.
+ *                                  We calculate the wish/minimum value for
+ *                                  each cell in every Column that intersects
+ *                                  with the Selection.
  */
 static void lcl_CalcColValues( std::vector<sal_uInt16> &rToFill, const SwTabCols &rCols,
                            const SwLayoutFrame *pStart, const SwLayoutFrame *pEnd,
-                           bool bWishValues )
+                           bool bWishValues,
+                           bool bColumnWidth )
 {
     SwSelUnions aUnions;
     ::MakeSelUnions( aUnions, pStart, pEnd,
-                    bWishValues ? SwTableSearchType::NONE : SwTableSearchType::Col );
+                    bColumnWidth ? SwTableSearchType::Col : SwTableSearchType::NONE );
 
     for ( auto &rU : aUnions )
     {
@@ -1474,7 +1474,10 @@ static void lcl_CalcColValues( std::vector<sal_uInt16> &rToFill, const SwTabCols
     }
 }
 
-void SwDoc::AdjustCellWidth( const SwCursor& rCursor, bool bBalance )
+void SwDoc::AdjustCellWidth( const SwCursor& rCursor,
+                             const bool bBalance,
+                             const bool bNoShrink,
+                             const bool bColumnWidth )
 {
     // Check whether the current Cursor has it's Point/Mark in a Table
     SwContentNode* pCntNd = rCursor.GetPoint()->nNode.GetNode().GetContentNode();
@@ -1502,7 +1505,7 @@ void SwDoc::AdjustCellWidth( const SwCursor& rCursor, bool bBalance )
     std::vector<sal_uInt16> aWish(aTabCols.Count() + 1);
     std::vector<sal_uInt16> aMins(aTabCols.Count() + 1);
 
-    ::lcl_CalcColValues( aWish, aTabCols, pStart, pEnd, true  );
+    ::lcl_CalcColValues( aWish, aTabCols, pStart, pEnd, true, bColumnWidth );
 
     // It's more robust if we calculate the minimum values for the whole Table
     const SwTabFrame *pTab = pStart->ImplFindTabFrame();
@@ -1510,12 +1513,13 @@ void SwDoc::AdjustCellWidth( const SwCursor& rCursor, bool bBalance )
     pEnd   = const_cast<SwLayoutFrame*>(pTab->FindLastContent()->GetUpper());
     while( !pEnd->IsCellFrame() )
         pEnd = pEnd->GetUpper();
-    ::lcl_CalcColValues( aMins, aTabCols, pStart, pEnd, false );
+    ::lcl_CalcColValues( aMins, aTabCols, pStart, pEnd, false, /*bColumnWidth=*/true );
 
     sal_uInt16 nSelectedWidth = 0, nCols = 0;
-    if( bBalance )
+    float fTotalWish = 0;
+    if ( bBalance || bNoShrink )
     {
-        // Find the combined size of the selected columns, and distribute evenly
+        // Find the combined size of the selected columns
         for ( size_t i = 0; i <= aTabCols.Count(); ++i )
         {
             if ( aWish[i] )
@@ -1528,10 +1532,12 @@ void SwDoc::AdjustCellWidth( const SwCursor& rCursor, bool bBalance )
                     nSelectedWidth += aTabCols[i] - aTabCols[i-1];
                 ++nCols;
             }
+            fTotalWish += aWish[i];
         }
         const sal_uInt16 nEqualWidth = nSelectedWidth / nCols;
+        // bBalance: Distribute the width evenly
         for (sal_uInt16 & rn : aWish)
-            if ( rn )
+            if ( rn && bBalance )
                 rn = nEqualWidth;
     }
 
@@ -1545,10 +1551,15 @@ void SwDoc::AdjustCellWidth( const SwCursor& rCursor, bool bBalance )
     // The first column's desired width would be discarded as it would cause
     // the Table's width to exceed the maximum width.
     const sal_uInt16 nEqualWidth = (aTabCols.GetRight() - aTabCols.GetLeft()) / (aTabCols.Count() + 1);
+    const sal_Int16 nTablePadding = nSelectedWidth - fTotalWish;
     for ( int k = 0; k < 2; ++k )
     {
         for ( size_t i = 0; i <= aTabCols.Count(); ++i )
         {
+            // bNoShrink: distribute excess space proportionately on pass 2.
+            if ( bNoShrink && k && nTablePadding > 0 && fTotalWish > 0 )
+                aWish[i] += round( aWish[i] / fTotalWish * nTablePadding );
+
             // First pass is primarily a shrink pass. Give all columns a chance
             //    to grow by requesting the maximum width as "balanced".
             // Second pass is a first-come, first-served chance to max out.
diff --git a/sw/source/core/frmedt/fetab.cxx b/sw/source/core/frmedt/fetab.cxx
index e51f1e4e96e1..ece9442cc5d4 100644
--- a/sw/source/core/frmedt/fetab.cxx
+++ b/sw/source/core/frmedt/fetab.cxx
@@ -1107,7 +1107,7 @@ bool SwFEShell::CheckHeadline( bool bRepeat ) const
     return bRet;
 }
 
-void SwFEShell::AdjustCellWidth( bool bBalance )
+void SwFEShell::AdjustCellWidth( bool bBalance, const bool bNoShrink, const bool bColumnWidth )
 {
     SET_CURR_SHELL( this );
     StartAllAction();
@@ -1117,7 +1117,7 @@ void SwFEShell::AdjustCellWidth( bool bBalance )
     TableWait aWait(std::numeric_limits<size_t>::max(), nullptr,
                   *GetDoc()->GetDocShell());
 
-    GetDoc()->AdjustCellWidth( *getShellCursor( false ), bBalance );
+    GetDoc()->AdjustCellWidth( *getShellCursor( false ), bBalance, bNoShrink, bColumnWidth );
     EndAllActionAndCall();
 }
 
diff --git a/sw/source/uibase/shells/tabsh.cxx b/sw/source/uibase/shells/tabsh.cxx
index a849013d9dbb..336f46d3a7d0 100644
--- a/sw/source/uibase/shells/tabsh.cxx
+++ b/sw/source/uibase/shells/tabsh.cxx
@@ -770,13 +770,15 @@ void SwTableShell::Execute(SfxRequest &rReq)
         case FN_TABLE_BALANCE_CELLS:
         {
             bool bBalance = (FN_TABLE_BALANCE_CELLS == nSlot);
+            const bool bNoShrink = FN_TABLE_ADJUST_CELLS == nSlot;
+            const bool bSelectedWidth = SID_TABLE_MINIMAL_COLUMN_WIDTH == nSlot;
             if ( rSh.IsAdjustCellWidthAllowed(bBalance) )
             {
                 {
                     // remove actions to make a valid table selection
                     UnoActionRemoveContext aRemoveContext(rSh.GetDoc());
                 }
-                rSh.AdjustCellWidth(bBalance);
+                rSh.AdjustCellWidth(bBalance, bNoShrink, !bSelectedWidth);
             }
             bCallDone = true;
             break;


More information about the Libreoffice-commits mailing list