--- sw/inc/doc.hxx.orig	2007-04-06 10:17:36.000000000 +0800
+++ sw/inc/doc.hxx	2007-04-06 10:17:52.000000000 +0800
@@ -1954,6 +1954,10 @@ public:
     }
     void SetApplyWorkaroundForB6375613( bool p_bApplyWorkaroundForB6375613 );
     // <--
+    
+    //Update all the page masters
+    SW_DLLPUBLIC void SetDefaultPageMode(bool bSquaredPageMode);
+    SW_DLLPUBLIC sal_Bool IsSquaredPageMode() const;    
 };
 
 
--- sw/inc/swmodule.hxx.orig	2007-04-06 10:19:13.000000000 +0800
+++ sw/inc/swmodule.hxx	2007-04-06 10:19:24.000000000 +0800
@@ -192,6 +192,9 @@ public:
 	void ApplyUserMetric( FieldUnit eMetric, BOOL bWeb );
 	SW_DLLPUBLIC void ApplyFldUpdateFlags(sal_Int32 nFldFlags);
 	SW_DLLPUBLIC void ApplyLinkMode(sal_Int32 nNewLinkMode);
+    
+    //default page mode for text grid 
+    SW_DLLPUBLIC void ApplyDefaultPageMode(sal_Bool bIsSquaredPageMode);
 
 	// ConfigItems erzeugen
 	SwModuleOptions*    GetModuleConfig()		{ return pModuleConfig;}
--- sw/inc/tgrditem.hxx.orig	2007-04-06 10:20:35.000000000 +0800
+++ sw/inc/tgrditem.hxx	2007-04-19 15:48:33.000000000 +0800
@@ -64,6 +64,10 @@ class SW_DLLPUBLIC SwTextGridItem : publ
 	BOOL bPrintGrid;
 	BOOL bDisplayGrid;
 
+	//for textgrid enhancement
+    sal_uInt16 nBaseWidth;
+    BOOL bSnapToChars;
+    BOOL bSquaredMode;
 public:
 	SwTextGridItem();
 	virtual ~SwTextGridItem();
@@ -107,6 +111,21 @@ public:
     BOOL IsDisplayGrid() const { return bDisplayGrid; }
     BOOL GetDisplayGrid() const { return bDisplayGrid; }
     void SetDisplayGrid( BOOL bNew ) { bDisplayGrid = bNew; }
+    
+    //for textgrid enhancement
+    sal_uInt16 GetBaseWidth() const { return bSquaredMode? nBaseHeight : nBaseWidth;}
+    void SetBaseWidth( sal_uInt16 nNew ) { nBaseWidth = nNew; }
+    
+    BOOL IsSnapToChars() const { return bSnapToChars; }
+    BOOL GetSnapToChars() const { return bSnapToChars; }
+    void SetSnapToChars( BOOL bNew ) { bSnapToChars = bNew; }
+    
+    BOOL IsSquaredMode() const { return bSquaredMode; }
+    BOOL GetSquaredMode() const { return bSquaredMode; }
+    void SetSquaredMode( BOOL bNew ) { bSquaredMode = bNew; }
+	void SwitchPaperMode(BOOL bNew );
+	
+	void Init();
 };
 
 inline const SwTextGridItem &SwAttrSet::GetTextGrid(BOOL bInP) const
--- sw/inc/unomid.h.orig	2007-04-06 10:20:55.000000000 +0800
+++ sw/inc/unomid.h	2007-04-06 10:21:54.000000000 +0800
@@ -138,6 +138,9 @@
 #define MID_GRID_RUBY_BELOW     5
 #define MID_GRID_PRINT          6
 #define MID_GRID_DISPLAY        7
+#define MID_GRID_BASEWIDTH      8
+#define MID_GRID_SNAPTOCHARS    9
+#define MID_GRID_SQUARE_MODE    10
 
 //SwEnvItem
 #define MID_ENV_ADDR_TEXT       1
--- sw/inc/unoprnms.hxx.orig	2007-04-06 10:21:09.000000000 +0800
+++ sw/inc/unoprnms.hxx	2007-04-06 10:22:11.000000000 +0800
@@ -779,7 +779,11 @@ enum SwPropNameIds
 /* 0714 */  UNO_NAME_DIALOG_LIBRARIES,
 /* 0715 */  UNO_NAME_HEADER_ROW_COUNT,
 /* 0716 */  UNO_NAME_INPUT,
-/* 0716 */  SW_PROPNAME_END
+// --> 2007-03-27 text grid enhancement for better CJK support
+/* 0717 */  UNO_NAME_GRID_BASE_WIDTH,
+/* 0718 */  UNO_NAME_GRID_SNAP_TO_CHARS,
+/* 0719 */  UNO_NAME_GRID_SQUARE_PAGE_MODE,
+/* 0720 */  SW_PROPNAME_END
 };
 
 
--- sw/source/core/layout/flowfrm.cxx.orig	2007-04-06 10:25:22.000000000 +0800
+++ sw/source/core/layout/flowfrm.cxx	2007-04-06 10:28:05.000000000 +0800
@@ -1600,7 +1600,9 @@ SwTwips SwFlowFrm::CalcUpperSpace( const
 
     // OD 2004-03-12 #i11860# - consider value of new parameter <_bConsiderGrid>
     // and use new method <GetUpperSpaceAmountConsideredForPageGrid(..)>
-    if ( _bConsiderGrid )
+
+	//consider grid in square page mode
+    if ( _bConsiderGrid && rThis.GetUpper()->GetFmt()->GetDoc()->IsSquaredPageMode() )
     {
         nUpper += _GetUpperSpaceAmountConsideredForPageGrid( nUpper );
     }
--- sw/source/core/layout/pagechg.cxx.orig	2007-04-06 10:25:38.000000000 +0800
+++ sw/source/core/layout/pagechg.cxx	2007-04-06 10:28:22.000000000 +0800
@@ -176,7 +176,8 @@ void SwBodyFrm::Format( const SwBorderAt
             long nBorder = 0;
             if( GRID_LINES_CHARS == pGrid->GetGridType() )
             {
-                nBorder = nSize % pGrid->GetBaseHeight();
+                //for textgrid refactor
+                nBorder = nSize % pGrid->GetBaseWidth();
                 nSize -= nBorder;
                 nBorder /= 2;
             }
--- sw/source/core/layout/paintfrm.cxx.orig	2007-04-06 10:25:55.000000000 +0800
+++ sw/source/core/layout/paintfrm.cxx	2007-04-06 10:28:51.000000000 +0800
@@ -4746,6 +4746,7 @@ void SwPageFrm::PaintGrid( OutputDevice*
                 BOOL bGrid = pGrid->GetRubyTextBelow();
                 BOOL bCell = GRID_LINES_CHARS == pGrid->GetGridType();
                 long nGrid = pGrid->GetBaseHeight();
+                long nGridWidth = pGrid->GetBaseWidth(); //for textgrid refactor
                 long nRuby = pGrid->GetRubyHeight();
                 long nSum = nGrid + nRuby;
                 const Color *pCol = &pGrid->GetColor();
@@ -4889,10 +4890,11 @@ void SwPageFrm::PaintGrid( OutputDevice*
                     SwTwips nY = nOrig + nSum *( (aInter.Top()-nOrig)/nSum );
                     SwRect aTmp( Point( aInter.Left(), nY ),
                                 Size( aInter.Width(), 1 ) );
-                    SwTwips nX = aGrid.Left() + nGrid *
-                                ( ( aInter.Left() - aGrid.Left() )/ nGrid );
+                    //for textgrid refactor
+                    SwTwips nX = aGrid.Left() + nGridWidth *
+                        ( ( aInter.Left() - aGrid.Left() )/ nGridWidth );
                     if( nX < aInter.Left() )
-                        nX += nGrid;
+                        nX += nGridWidth;
                     SwTwips nGridRight = aGrid.Left() + aGrid.Width();
                     BOOL bLeft = aGrid.Left() >= aInter.Left();
                     BOOL bRight = nGridRight <= nRight;
@@ -4914,7 +4916,7 @@ void SwPageFrm::PaintGrid( OutputDevice*
                                     while( aVert.Left() <= nRight )
                                     {
                                         PaintBorderLine(rRect,aVert,this,pCol);
-                                        aVert.Pos().X() += nGrid;
+                                        aVert.Pos().X() += nGridWidth;	//for textgrid refactor
                                     }
                                 }
                                 else if ( bBorder )
@@ -4971,7 +4973,7 @@ void SwPageFrm::PaintGrid( OutputDevice*
                                     while( aVert.Left() <= nRight )
                                     {
                                         PaintBorderLine( rRect, aVert, this, pCol);
-                                        aVert.Pos().X() += nGrid;
+                                        aVert.Pos().X() += nGridWidth;	//for textgrid refactor
                                     }
                                 }
                                 else if( bBorder )
--- sw/source/core/text/itrform2.cxx.orig	2007-04-06 10:31:50.000000000 +0800
+++ sw/source/core/text/itrform2.cxx	2007-04-06 10:32:40.000000000 +0800
@@ -474,7 +474,7 @@ void SwTxtFormatter::BuildPortions( SwTx
                               GRID_LINES_CHARS == pGrid->GetGridType();
 
     const USHORT nGridWidth = bHasGrid ?
-                              pGrid->GetBaseHeight() : 0;
+                                pGrid->GetBaseWidth() : 0;	//for textgrid refactor
 
     // used for grid mode only:
     // the pointer is stored, because after formatting of non-asian text,
--- sw/source/core/text/itrtxt.cxx.orig	2007-04-06 10:32:04.000000000 +0800
+++ sw/source/core/text/itrtxt.cxx	2007-04-06 10:32:48.000000000 +0800
@@ -359,9 +359,12 @@ USHORT SwTxtCursor::AdjustBaseLine( cons
             {
                 // Portions which are bigger than on grid distance are
                 // centered inside the whole line.
-                const USHORT nLineNetto = ( nPorHeight > nGridWidth ) ?
-                                            rLine.Height() - nRubyHeight :
-                                            nGridWidth;
+                
+                //for text refactor
+                const USHORT nLineNetto =  rLine.Height() - nRubyHeight;
+                //const USHORT nLineNetto = ( nPorHeight > nGridWidth ) ?
+                 //                           rLine.Height() - nRubyHeight :
+                 //                           nGridWidth;
                 nOfst += ( nLineNetto - nPorHeight ) / 2;
                 if ( bRubyTop )
                     nOfst += nRubyHeight;
--- sw/source/core/text/txtfly.cxx.orig	2007-04-06 10:32:26.000000000 +0800
+++ sw/source/core/text/txtfly.cxx	2007-04-06 10:32:56.000000000 +0800
@@ -682,7 +682,7 @@ void SwTxtFormatter::CalcFlyWidth( SwTxt
                                     (pBody->*fnRect->fnGetPrtLeft)() :
                                     (pPageFrm->*fnRect->fnGetPrtLeft)();
 
-            const USHORT nGridWidth = pGrid->GetBaseHeight();
+            const USHORT nGridWidth = pGrid->GetBaseWidth();	//for textgrid refactor
 
             SwTwips nStartX = GetLeftMargin();
             if ( bVert )
--- sw/source/core/txtnode/fntcache.cxx.orig	2007-04-24 13:26:22.000000000 +0800
+++ sw/source/core/txtnode/fntcache.cxx	2007-04-24 11:58:26.000000000 +0800
@@ -114,6 +114,18 @@
 #include <svtools/accessibilityoptions.hxx>
 #endif
 
+#ifndef _DOC_HXX
+#include <doc.hxx>
+#endif
+#ifndef _SVX_FHGTITEM_HXX
+#include <svx/fhgtitem.hxx>
+#endif
+#ifndef _SWDOCSH_HXX
+#include <docsh.hxx>
+#endif
+#ifndef _POOLFMT_HRC
+#include <poolfmt.hrc>
+#endif
 // Enable this to use the helpclass SwRVPMark
 #if OSL_DEBUG_LEVEL > 1
 #ifndef _RVP_MARK_HXX
@@ -136,6 +148,18 @@ MapMode* SwFntObj::pPixMap = NULL;
 OutputDevice* SwFntObj::pPixOut = NULL;
 
 extern USHORT UnMapDirection( USHORT nDir, const BOOL bVertFormat );
+USHORT GetDefaultFontHeight( SwDrawTextInfo &rInf )
+{
+    SwDocShell* pDocShell = rInf.GetShell()->GetDoc()->GetDocShell();
+    SfxStyleSheetBasePool* pBasePool = pDocShell->GetStyleSheetPool();
+    
+    String aString(SW_RES(STR_POOLCOLL_STANDARD));
+    
+    SfxStyleSheetBase* pStyle = pBasePool->Find( aString, (SfxStyleFamily)SFX_STYLE_FAMILY_PARA );
+    SfxItemSet& aTmpSet = pStyle->GetItemSet();
+    SvxFontHeightItem &aDefaultFontItem = (SvxFontHeightItem&)aTmpSet.Get(RES_CHRATR_CJK_FONTSIZE); 
+    return aDefaultFontItem.GetHeight();
+}
 
 #ifdef _RVP_MARK_HXX
 
@@ -1074,16 +1098,18 @@ void SwFntObj::DrawText( SwDrawTextInfo 
 
 
     //
-    // ASIAN LINE AND CHARACTER GRID MODE START
+    // ASIAN LINE AND CHARACTER GRID MODE START: snap to characters
     //
 
     if ( rInf.GetFrm() && rInf.SnapToGrid() && rInf.GetFont() &&
          SW_CJK == rInf.GetFont()->GetActual() )
     {
         GETGRID( rInf.GetFrm()->FindPageFrm() )
-        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars())
         {
-            const USHORT nGridWidth = pGrid->GetBaseHeight();
+            //for textgrid refactor
+            //const USHORT nGridWidth = pGrid->GetBaseHeight();
+            const USHORT nGridWidth = pGrid->GetBaseWidth();
             sal_Int32* pKernArray = new sal_Int32[rInf.GetLen()];
 
             if ( pPrinter )
@@ -1114,7 +1140,7 @@ void SwFntObj::DrawText( SwDrawTextInfo 
             {
             case SwScriptInfo::NONE :
                 aPos.X() += ( nWidthPerChar - nCharWidth ) / 2;
-                nNextFix = nCharWidth / 2;
+                nNextFix = nCharWidth / 2;                    
                 break;
             case SwScriptInfo::SPECIAL_RIGHT :
                 nNextFix = nHalfWidth;
@@ -1160,7 +1186,148 @@ void SwFntObj::DrawText( SwDrawTextInfo 
             return;
         }
     }
+    
+    // For text grid refactor
+    // ASIAN LINE AND CHARACTER GRID MODE START: not snap to characters
+    //
+    if ( rInf.GetFrm() && rInf.SnapToGrid() && rInf.GetFont() )
+    {
+        GETGRID( rInf.GetFrm()->FindPageFrm() )
+        
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
+        {
+            const USHORT  nDefaultFontHeight = GetDefaultFontHeight( rInf );
+            
+            USHORT  nGridWidthAdd = pGrid->GetBaseWidth();
+            if( SW_LATIN == rInf.GetFont()->GetActual() )
+                nGridWidthAdd = ( nGridWidthAdd - nDefaultFontHeight ) / 2;
+            else
+                nGridWidthAdd = nGridWidthAdd - nDefaultFontHeight;
 
+            sal_Int32*  pKernArray = new sal_Int32[rInf.GetLen()];
+
+            if ( pPrinter )
+                pPrinter->GetTextArray( rInf.GetText(), pKernArray,
+                rInf.GetIdx(), rInf.GetLen() );
+            else
+                rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray,
+                rInf.GetIdx(), rInf.GetLen() );
+            if ( bSwitchH2V )
+                rInf.GetFrm()->SwitchHorizontalToVertical( aPos );
+            if ( rInf.GetSpace() || rInf.GetKanaComp())
+            {
+                short nSpaceAdd = rInf.GetSpace();
+                sal_Bool bSpecialJust = sal_False;
+                if ( rInf.GetFont() && rInf.GetLen() )
+                {
+                    const SwScriptInfo* pSI = rInf.GetScriptInfo();
+                    const BYTE nActual = rInf.GetFont()->GetActual();
+                    ///Kana Compression
+                    if( SW_CJK == nActual && rInf.GetKanaComp() &&
+                        pSI && pSI->CountCompChg() &&
+                        lcl_IsMonoSpaceFont( *(rInf.GetpOut()) ) )
+                    {
+                        pSI->Compress( pKernArray,rInf.GetIdx(), rInf.GetLen(),
+                            rInf.GetKanaComp(), (USHORT)aFont.GetSize().Height(),&aPos );
+                        bSpecialJust = sal_True;
+                    }
+                    ///Asian Justification
+                    if ( ( SW_CJK == nActual || SW_LATIN == nActual ) && nSpaceAdd )
+                    {
+                        LanguageType aLang = rInf.GetFont()->GetLanguage( SW_CJK );
+                        if ( LANGUAGE_KOREAN != aLang && LANGUAGE_KOREAN_JOHAB != aLang)
+                        {
+                            long nSpaceSum = nSpaceAdd;
+                            for ( USHORT nI = 0; nI < rInf.GetLen(); ++nI )
+                            {
+                                pKernArray[ nI ] += nSpaceSum;
+                                nSpaceSum += nSpaceAdd;
+                            }
+                            bSpecialJust = sal_True;
+                            nSpaceAdd = 0;
+                        }
+                    }
+                    long nGridAddSum = nGridWidthAdd;
+                    for(xub_StrLen i = 0; i < rInf.GetLen(); i++,nGridAddSum += nGridWidthAdd )
+                    {
+                        pKernArray[i] += nGridAddSum;
+                    }
+                    long nKernSum = rInf.GetKern();
+                    if ( bSpecialJust || rInf.GetKern() )
+                    {
+                        for( xub_StrLen i = 0; i < rInf.GetLen(); i++, nKernSum += rInf.GetKern() )
+                        {
+                            if ( CH_BLANK == rInf.GetText().GetChar(rInf.GetIdx()+i) )
+                                nKernSum += nSpaceAdd;
+                            pKernArray[i] += nKernSum;
+                        }
+                        ///With through/uderstr. Grouped style requires a blank at the end
+                        ///of a text edition special measures:
+                        if( bPaintBlank && rInf.GetLen() && (CH_BLANK ==
+                            rInf.GetText().GetChar( rInf.GetIdx() + rInf.GetLen() - 1) ) )
+                        {
+                            ///If it concerns a singular, underlined space acts,
+                            ///we must spend two:
+                            if( 1 == rInf.GetLen() )
+                            {
+                                pKernArray[0] = rInf.GetWidth() + nSpaceAdd;
+                                rInf.GetOut().DrawTextArray( aPos, rInf.GetText(),
+                                    pKernArray, rInf.GetIdx(), 1 );
+                            }
+                            else
+                            {
+                                pKernArray[ rInf.GetLen() - 2] += nSpaceAdd;
+                                rInf.GetOut().DrawTextArray( aPos, rInf.GetText(),
+                                    pKernArray, rInf.GetIdx(), rInf.GetLen() );
+                            }
+                        }
+                        else
+                        {
+                            rInf.GetOut().DrawTextArray( aPos, rInf.GetText(),
+                                pKernArray, rInf.GetIdx(), rInf.GetLen() );
+                        }
+                    }
+                    else
+                    {
+                        Point aTmpPos( aPos );
+                        xub_StrLen i;
+                        xub_StrLen j = 0;
+                        long nSpaceSum = 0;
+                        for( i = 0; i < rInf.GetLen(); i++ )
+                        {
+                            if( CH_BLANK == rInf.GetText().GetChar( rInf.GetIdx() + i) )
+                            {
+                                nSpaceSum += nSpaceAdd;
+                                if( j < i)
+                                    rInf.GetOut().DrawText( aTmpPos, rInf.GetText(),
+                                    rInf.GetIdx() + j, i - j );
+                                j = i + 1;
+                                pKernArray[i] = pKernArray[i] + nSpaceSum;
+                                aTmpPos.X() = aPos.X() + pKernArray[ i ] + nKernSum;
+                            }
+                        }
+                        if( j < i )
+                            rInf.GetOut().DrawText( aTmpPos, rInf.GetText(),
+                            rInf.GetIdx() +j , i - j );
+                    }
+                }
+            }
+            else
+            {
+                //long nKernAdd = rInf.GetKern();
+		long nKernAdd = 0;
+                long nGridAddSum = nGridWidthAdd + nKernAdd;
+                for(xub_StrLen i = 0; i < rInf.GetLen(); i++,nGridAddSum += nGridWidthAdd + nKernAdd )
+                {
+                    pKernArray[i] += nGridAddSum;
+                }
+                rInf.GetOut().DrawTextArray( aPos, rInf.GetText(),
+                    pKernArray, rInf.GetIdx(), rInf.GetLen() );
+            }
+            delete[] pKernArray;
+            return;
+        }
+    }
 
     //
     // DIRECT PAINTING WITHOUT SCREEN ADJUSTMENT
@@ -1922,9 +2089,9 @@ Size SwFntObj::GetTextSize( SwDrawTextIn
          SW_CJK == rInf.GetFont()->GetActual() )
     {
         GETGRID( rInf.GetFrm()->FindPageFrm() )
-        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() )
         {
-            const USHORT nGridWidth = pGrid->GetBaseHeight();
+            const USHORT nGridWidth = pGrid->GetBaseWidth();	//for textgrid refactor
 
             OutputDevice* pOutDev;
 
@@ -1953,6 +2120,39 @@ Size SwFntObj::GetTextSize( SwDrawTextIn
                             1;
 
             aTxtSize.Width() = i * nGridWidth * nLn;
+            rInf.SetKanaDiff( 0 );
+            return aTxtSize;
+        }
+    }
+    
+    //for textgrid refactor
+    if ( rInf.GetFrm() && nLn && rInf.SnapToGrid() && rInf.GetFont())
+    {
+        GETGRID( rInf.GetFrm()->FindPageFrm() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
+        {
+            const USHORT nDefaultFontHeight = GetDefaultFontHeight( rInf );
+
+            USHORT nGridWidthAdd = pGrid->GetBaseWidth();
+            if( SW_LATIN == rInf.GetFont()->GetActual() )
+                nGridWidthAdd = ( nGridWidthAdd - nDefaultFontHeight ) / 2;
+            else
+                nGridWidthAdd = nGridWidthAdd - nDefaultFontHeight;
+            OutputDevice* pOutDev;
+            if ( pPrinter )
+            {
+                if( !pPrtFont->IsSameInstance( pPrinter->GetFont() ) )
+                    pPrinter->SetFont(*pPrtFont);
+                pOutDev = pPrinter;
+            }
+            else
+                pOutDev = rInf.GetpOut();
+            aTxtSize.Width() = pOutDev->GetTextWidth( rInf.GetText(), rInf.GetIdx(), nLn );
+            aTxtSize.Height() = pOutDev->GetTextHeight() +
+                                GetFontLeading( rInf.GetShell(), rInf.GetOut() );
+            aTxtSize.Width() += (nLn) * long( nGridWidthAdd ); 
+            //if ( rInf.GetKern() && nLn )
+            //    aTxtSize.Width() += ( nLn ) * long( rInf.GetKern() );
 
             rInf.SetKanaDiff( 0 );
             return aTxtSize;
@@ -2175,9 +2375,9 @@ xub_StrLen SwFntObj::GetCrsrOfst( SwDraw
          rInf.GetFont() && SW_CJK == rInf.GetFont()->GetActual() )
     {
         GETGRID( rInf.GetFrm()->FindPageFrm() )
-        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() )
         {
-            const USHORT nGridWidth = pGrid->GetBaseHeight();
+            const USHORT nGridWidth = pGrid->GetBaseWidth();	//for textgrid refactor
 
             long nWidthPerChar = pKernArray[ rInf.GetLen() - 1 ] / rInf.GetLen();
 
@@ -2195,6 +2395,37 @@ xub_StrLen SwFntObj::GetCrsrOfst( SwDraw
             return nCnt;
         }
     }
+    
+    //for textgrid refactor
+    if ( rInf.GetFrm() && rInf.GetLen() && rInf.SnapToGrid() &&
+        rInf.GetFont() )
+    {
+        GETGRID( rInf.GetFrm()->FindPageFrm() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
+        {
+
+            const USHORT nDefaultFontHeight = GetDefaultFontHeight( rInf );
+
+            USHORT nGridWidthAdd = pGrid->GetBaseWidth();
+            if( SW_LATIN == rInf.GetFont()->GetActual() )   
+                nGridWidthAdd = ( nGridWidthAdd - nDefaultFontHeight ) / 2;
+            else
+                nGridWidthAdd = nGridWidthAdd - nDefaultFontHeight;
+            
+            long nKernAdd = rInf.GetKern();
+            for(xub_StrLen j = 0; j < rInf.GetLen(); j++)
+            {
+                long nScr = pKernArray[ j ] + ( nSpaceAdd + nGridWidthAdd  ) * ( j + 1 );
+                if( nScr >= rInf.GetOfst())
+                {
+                    nCnt = j;
+                    break;
+                }
+            }
+            delete[] pKernArray;
+            return nCnt;
+        }
+    }
 
     sal_uInt16 nItrMode = ::com::sun::star::i18n::CharacterIteratorMode::SKIPCELL;
     sal_Int32 nDone = 0;
@@ -2402,9 +2633,9 @@ xub_StrLen SwFont::GetTxtBreak( SwDrawTe
          rInf.GetFont() && SW_CJK == rInf.GetFont()->GetActual() )
     {
         GETGRID( rInf.GetFrm()->FindPageFrm() )
-        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() )
         {
-            const USHORT nGridWidth = pGrid->GetBaseHeight();
+            const USHORT nGridWidth = pGrid->GetBaseWidth();	//for textgrid refactor
 
             sal_Int32* pKernArray = new sal_Int32[rInf.GetLen()];
             rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray,
@@ -2429,6 +2660,34 @@ xub_StrLen SwFont::GetTxtBreak( SwDrawTe
             return nTxtBreak + rInf.GetIdx();
         }
     }
+    
+    //for text grid enhancement
+    if ( rInf.GetFrm() && nLn && rInf.SnapToGrid() && rInf.GetFont() )
+    {
+        GETGRID( rInf.GetFrm()->FindPageFrm() )
+        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
+        {
+            const USHORT nDefaultFontHeight = GetDefaultFontHeight( rInf );
+
+            USHORT nGridWidthAdd = pGrid->GetBaseWidth();
+            if( SW_LATIN == rInf.GetFont()->GetActual() )
+                nGridWidthAdd = ( nGridWidthAdd - nDefaultFontHeight ) / 2 ;
+            else
+                nGridWidthAdd = nGridWidthAdd - nDefaultFontHeight;
+
+            sal_Int32* pKernArray = new sal_Int32[rInf.GetLen()];
+            rInf.GetOut().GetTextArray( rInf.GetText(), pKernArray,
+                                            rInf.GetIdx(), rInf.GetLen() );
+            long nCurrPos = pKernArray[nTxtBreak] + nGridWidthAdd;
+            while( nTxtBreak < rInf.GetLen() && nTextWidth >= nCurrPos)
+            {
+                nTxtBreak++;
+                nCurrPos = pKernArray[nTxtBreak] + nGridWidthAdd * ( nTxtBreak + 1 );
+            }
+            delete[] pKernArray;
+            return nTxtBreak + rInf.GetIdx();
+        }
+    }
 
     if( aSub[nActual].IsCapital() && nLn )
 		nTxtBreak = GetCapitalBreak( rInf.GetShell(), rInf.GetpOut(),
--- sw/source/ui/app/docshini.cxx.orig	2007-04-06 10:46:13.000000000 +0800
+++ sw/source/ui/app/docshini.cxx	2007-04-19 15:53:11.000000000 +0800
@@ -227,6 +227,8 @@
 #include <fmtfollowtextflow.hxx>
 #endif
 
+// text grid
+#include <tgrditem.hxx>
 
 using namespace ::com::sun::star::i18n;
 using namespace ::com::sun::star::lang;
@@ -911,6 +913,14 @@ void SwDocShell::SubInitNew()
     aDfltSet.Put( SvxColorItem( Color( COL_AUTO ), RES_CHRATR_COLOR ) );
 
 	pDoc->SetDefault( aDfltSet );
+
+	//default page mode for text grid
+	if(!bWeb)
+	{
+		sal_Bool bSquaredPageMode = SW_MOD()->GetUsrPref(FALSE)->IsSquaredPageMode();
+		pDoc->SetDefaultPageMode( bSquaredPageMode );
+	}
+	
 	pDoc->ResetModified();
 }
 
--- sw/source/ui/app/swmodul1.cxx.orig	2007-04-06 10:46:56.000000000 +0800
+++ sw/source/ui/app/swmodul1.cxx	2007-04-06 10:47:11.000000000 +0800
@@ -669,6 +669,14 @@
                GetUsrPref(sal_False);
        pUsrPref->SetUpdateLinkMode(nNewLinkMode);
 }
+
+void SwModule::ApplyDefaultPageMode(sal_Bool bIsSquaredPageMode)
+{
+    if(!pUsrPref)
+        GetUsrPref(sal_False);
+    pUsrPref->SetDefaultPageMode(bIsSquaredPageMode);
+}
+
 /* ---------------------------------------------------------------------------

  ---------------------------------------------------------------------------*/
--- sw/source/ui/config/optload.cxx.orig	2007-04-06 10:48:25.000000000 +0800
+++ sw/source/ui/config/optload.cxx	2007-04-19 15:53:46.000000000 +0800
@@ -105,6 +105,11 @@
 #include <SwStyleNameMapper.hxx>
 #endif
 
+#include <doc.hxx>
+#ifndef _SVTOOLS_CJKOPTIONS_HXX
+#include <svtools/cjkoptions.hxx>
+#endif
+
 /* -----------------22.10.98 15:12-------------------
  *
  * --------------------------------------------------*/
@@ -125,6 +130,7 @@ SwLoadOptPage::SwLoadOptPage( Window* pP
     aMetricFT			( this, SW_RES( FT_METRIC ) ),
     aTabFT				( this, SW_RES( FT_TAB ) ),
 	aTabMF				( this,	SW_RES( MF_TAB ) ),
+    aUseSquaredPageMode        ( this, SW_RES( CB_USE_SQUARE_PAGE_MODE ) ),
 
 	pWrtShell	( NULL ),
     nLastTab	( 0 ),
@@ -164,6 +170,10 @@ SwLoadOptPage::SwLoadOptPage( Window* pP
         aTabFT.Hide();
 		aTabMF.Hide();
 	}
+	
+	SvtCJKOptions aCJKOptions;
+	if(!aCJKOptions.IsAsianTypographyEnabled())
+		aUseSquaredPageMode.Hide();
 }
 
 /*-----------------18.01.97 12.43-------------------
@@ -250,6 +260,19 @@ BOOL __EXPORT SwLoadOptPage::FillItemSet
 					(USHORT)aTabMF.Denormalize(aTabMF.GetValue(FUNIT_TWIP))));
         bRet = TRUE;
     }
+    
+    sal_Bool bIsSquaredPageModeFlag = aUseSquaredPageMode.IsChecked();
+    if ( bIsSquaredPageModeFlag != aUseSquaredPageMode.GetSavedValue() )
+    {
+        pMod->ApplyDefaultPageMode( bIsSquaredPageModeFlag );
+        if ( pWrtShell )
+        {
+            SwDoc* pDoc = pWrtShell->GetDoc();
+            pDoc->SetDefaultPageMode( bIsSquaredPageModeFlag );
+            pWrtShell->SetModified();
+        }
+        bRet = TRUE;
+    }
 
     return bRet;
 }
@@ -317,6 +340,14 @@ void __EXPORT SwLoadOptPage::Reset( cons
 	{
 		bHTMLMode = 0 != (((const SfxUInt16Item*)pItem)->GetValue() & HTMLMODE_ON);
 	}
+    
+	//default page mode loading
+	if(pWrtShell)
+	{
+    	sal_Bool bSquaredPageMode = pWrtShell->GetDoc()->IsSquaredPageMode();
+		aUseSquaredPageMode.Check( bSquaredPageMode );
+   	 	aUseSquaredPageMode.SaveValue();
+	}
 }
 /*-----------------13.01.97 14.44-------------------
 	Metric des Deftabstops umschalten
--- sw/source/ui/config/optload.hrc.orig	2007-04-06 10:48:40.000000000 +0800
+++ sw/source/ui/config/optload.hrc	2007-04-06 10:49:58.000000000 +0800
@@ -48,6 +48,7 @@
 #define FT_METRIC				20
 #define FT_TAB					21
 #define MF_TAB					22
+#define CB_USE_SQUARE_PAGE_MODE 23
 
 // SwCaptionOptPage -----------------------------
 
--- sw/source/ui/config/optload.src.orig	2007-04-06 10:48:56.000000000 +0800
+++ sw/source/ui/config/optload.src	2007-04-19 15:54:00.000000000 +0800
@@ -159,6 +159,13 @@ TabPage TP_OPTLOAD_PAGE
 		First = 50 ;
 		Last = 9999 ;
 	};
+    CheckBox CB_USE_SQUARE_PAGE_MODE
+    {
+        Pos = MAP_APPFONT ( 12 , 111) ;
+        Size = MAP_APPFONT ( 248 , 10 ) ;
+        Text [ en-US ] = "Use square page mode for text grid";
+        Text [ zh-CN ] = "使用稿纸模式";
+    };
 };
 /**************************************************************************/
 /*                                                                        */
--- sw/source/ui/config/usrpref.cxx.orig	2007-04-06 10:49:09.000000000 +0800
+++ sw/source/ui/config/usrpref.cxx	2007-04-06 10:50:38.000000000 +0800
@@ -88,7 +88,8 @@ SwMasterUsrPref::SwMasterUsrPref(BOOL bW
     nLinkUpdateMode(0),
     bIsHScrollMetricSet(sal_False),
     bIsVScrollMetricSet(sal_False),
-    nDefTab( MM50 * 4 )
+    nDefTab( MM50 * 4 ),
+    bIsSquaredPageMode(sal_False)
 {
     MeasurementSystem eSystem = GetAppLocaleData().getMeasurementSystemEnum();
     eUserMetric = MEASURE_METRIC == eSystem ? FUNIT_CM : FUNIT_INCH;
@@ -273,9 +274,10 @@ Sequence<OUString> SwLayoutViewConfig::G
         "Zoom/Type",                        //12
         "Other/MeasureUnit",                //13
         "Other/TabStop",                    //14
-        "Window/IsVerticalRulerRight"       //15
+        "Window/IsVerticalRulerRight",      //15
+        "Other/IsSquaredPageMode"        //16
     };
-    const int nCount = bWeb ? 14 : 16;
+    const int nCount = bWeb ? 14 : 17;
     Sequence<OUString> aNames(nCount);
 	OUString* pNames = aNames.getArray();
 	for(int i = 0; i < nCount; i++)
@@ -340,8 +342,9 @@ void SwLayoutViewConfig::Commit()
             case 13: pValues[nProp] <<= (sal_Int32)rParent.GetMetric(); break;// "Other/MeasureUnit",
             case 14: pValues[nProp] <<= static_cast<sal_Int32>(TWIP_TO_MM100(rParent.GetDefTab())); break;// "Other/TabStop",
             case 15: bSet = rParent.IsVRulerRight(); break;// "Window/IsVerticalRulerRight",
+            case 16: bSet = rParent.IsSquaredPageMode(); break;// "Other/IsSquaredPageMode",
         }
-        if(nProp < 8 || nProp == 10)
+        if(nProp < 8 || nProp == 10 || nProp == 16)
 			pValues[nProp].setValue(&bSet, ::getBooleanCppuType());
 	}
 	PutProperties(aNames, aValues);
@@ -361,7 +364,7 @@ void SwLayoutViewConfig::Load()
 		{
 			if(pValues[nProp].hasValue())
 			{
-                sal_Bool bSet = nProp < 8 || nProp == 10 ? *(sal_Bool*)pValues[nProp].getValue() : sal_False;
+                sal_Bool bSet = nProp < 8 || nProp == 10 || nProp == 16? *(sal_Bool*)pValues[nProp].getValue() : sal_False;
                 switch(nProp)
 				{
                     case  0: rParent.SetCrossHair(bSet); break;// "Line/Guide",
@@ -412,6 +415,7 @@ void SwLayoutViewConfig::Load()
 					}
 					break;// "Other/TabStop",
                     case 15: rParent.SetVRulerRight(bSet); break;// "Window/IsVerticalRulerRight",
+                    case 16: rParent.SetDefaultPageMode(bSet); break;// "Other/IsSquaredPageMode",
                 }
 			}
 		}
--- sw/source/ui/inc/optload.hxx.orig	2007-04-06 10:53:00.000000000 +0800
+++ sw/source/ui/inc/optload.hxx	2007-04-06 10:53:40.000000000 +0800
@@ -93,6 +93,7 @@ private:
     ListBox     aMetricLB;
     FixedText   aTabFT;
 	MetricField aTabMF;
+    CheckBox    aUseSquaredPageMode;
 
 	SwWrtShell*	pWrtShell;
     sal_Bool    bHTMLMode;
--- sw/source/ui/inc/pggrid.hxx.orig	2007-04-06 10:53:11.000000000 +0800
+++ sw/source/ui/inc/pggrid.hxx	2007-04-06 10:57:03.000000000 +0800
@@ -60,6 +60,7 @@ class SwTextGridPage: public SfxTabPage
     RadioButton     aNoGridRB;
     RadioButton     aLinesGridRB;
     RadioButton     aCharsGridRB;
+    CheckBox        aSnapToCharsCB;
 
     SwPageGridExample   aExampleWN;
 
@@ -74,6 +75,9 @@ class SwTextGridPage: public SfxTabPage
     FixedText       aCharsPerLineFT;
     NumericField    aCharsPerLineNF;
 
+    FixedText       aCharWidthFT;
+    MetricField     aCharWidthMF;    
+    
     FixedText       aRubySizeFT;
     MetricField     aRubySizeMF;
 
@@ -86,12 +90,13 @@ class SwTextGridPage: public SfxTabPage
     FixedText       aColorFT;
     ColorListBox    aColorLB;
 
-    Window*         aControls[16];
+    Window*         aControls[18];
 
     sal_Int32       m_nRubyUserValue;
     sal_Bool        m_bRubyUserValue;
     Size            m_aPageSize;
     sal_Bool        m_bVertical;
+	sal_Bool		m_bSquaredMode;
 
     SwTextGridPage(Window *pParent, const SfxItemSet &rSet);
     ~SwTextGridPage();
@@ -100,7 +105,8 @@ class SwTextGridPage: public SfxTabPage
     void PutGridItem(SfxItemSet& rSet);
 
     DECL_LINK(GridTypeHdl, RadioButton*);
-    DECL_LINK(CharSizeChangedHdl, SpinField*);
+    DECL_LINK(CharorLineChangedHdl, SpinField*);
+    DECL_LINK(TextSizeChangedHdl, SpinField*);
     DECL_LINK(GridModifyHdl, void*);
     DECL_LINK(DisplayGridHdl, CheckBox*);
 public:
--- sw/source/ui/inc/usrpref.hxx.orig	2007-04-06 10:53:28.000000000 +0800
+++ sw/source/ui/inc/usrpref.hxx	2007-04-06 10:54:20.000000000 +0800
@@ -160,6 +160,8 @@ class SwMasterUsrPref : public SwViewOpt
 
 
 	sal_Int32	nDefTab;			//default tab stop distance
+    
+    sal_Bool    bIsSquaredPageMode; //default page mode for text grid
 
 public:
 	SwMasterUsrPref(BOOL bWeb);
@@ -269,6 +271,15 @@ public:
                     if(!bNoModify)
                         aLayoutConfig.SetModified();
                 }
+    
+    //default page mode for text grid
+    sal_Bool    IsSquaredPageMode() const {return bIsSquaredPageMode;}
+    void        SetDefaultPageMode( sal_Bool bVal, sal_Bool bNoModify = sal_False ) 
+                { 
+                    bIsSquaredPageMode = bVal;
+                    if(!bNoModify)
+                        aLayoutConfig.SetModified(); 
+                }
 
 };
 
--- sw/source/ui/misc/pggrid.hrc.orig	2007-04-06 10:57:50.000000000 +0800
+++ sw/source/ui/misc/pggrid.hrc	2007-04-06 10:58:49.000000000 +0800
@@ -55,3 +55,6 @@
 #define CB_PRINT            20
 #define FT_COLOR            21
 #define LB_COLOR            22
+#define CB_SNAPTOCHARS      23
+#define FT_CHARWIDTH        24
+#define MF_CHARWIDTH        25
--- sw/source/ui/misc/pggrid.src.orig	2007-04-24 13:27:47.000000000 +0800
+++ sw/source/ui/misc/pggrid.src	2007-04-24 11:58:47.000000000 +0800
@@ -69,6 +69,13 @@ TabPage TP_TEXTGRID_PAGE
         Size = MAP_APPFONT ( 158 , 10 ) ;
         Text [ en-US ] = "Grid (lines and characters)" ;
 	};
+    CheckBox        CB_SNAPTOCHARS
+    {
+        Pos = MAP_APPFONT ( 14 , 53 ) ;
+        Size = MAP_APPFONT ( 158 , 10 ) ;
+        Text [ en-US ] = "~Snap to characters" ;
+        Text [ zh-CN ] = "文字对齐字符网格" ;
+    };
     Window          WN_EXAMPLE
     {
         Pos = MAP_APPFONT ( 176 , 6 ) ;
@@ -76,7 +83,7 @@ TabPage TP_TEXTGRID_PAGE
     };
     FixedLine       FL_LAYOUT
     {
-        Pos = MAP_APPFONT ( 6 , 56 ) ;
+        Pos = MAP_APPFONT ( 6 , 69 ) ;
         Size = MAP_APPFONT ( 248 , 8 ) ;
         Text [ en-US ] = "Grid layout" ;
 	};
@@ -84,13 +91,13 @@ TabPage TP_TEXTGRID_PAGE
 	};
     FixedText       FT_LINESPERPAGE
     {
-        Pos = MAP_APPFONT ( 12 , 69 ) ;
+        Pos = MAP_APPFONT ( 12 , 82 ) ;
         Size = MAP_APPFONT ( 59 , 8 ) ;
         Text [ en-US ] = "Lines per page" ;
 	};
     NumericField    NF_LINESPERPAGE
     {
-        Pos = MAP_APPFONT ( 74 , 67 ) ;
+        Pos = MAP_APPFONT ( 74 , 80 ) ;
         Size = MAP_APPFONT ( 25 , 12 ) ;
         Border = TRUE ;
         TabStop = TRUE ;
@@ -107,13 +114,13 @@ TabPage TP_TEXTGRID_PAGE
     };
     FixedText       FT_TEXTSIZE
     {
-        Pos = MAP_APPFONT ( 133 , 69 ) ;
+        Pos = MAP_APPFONT ( 133 , 82 ) ;
         Size = MAP_APPFONT ( 80 , 8 ) ;
         Text [ en-US ] = "Max. base text size" ;
 	};
     MetricField     MF_TEXTSIZE
     {
-        Pos = MAP_APPFONT ( 216 , 67 ) ;
+        Pos = MAP_APPFONT ( 216 , 80 ) ;
         Size = MAP_APPFONT ( 35 , 12 ) ;
         Border = TRUE ;
         TabStop = TRUE ;
@@ -123,7 +130,7 @@ TabPage TP_TEXTGRID_PAGE
 		Spin = TRUE ;
 		Minimum = 1 ;
 		Maximum = 5600 ;
-        DecimalDigits = 1 ;
+        DecimalDigits = 2 ;
         Unit = FUNIT_POINT ;
 		First = 10 ;
 		Last = 5600 ;
@@ -131,13 +138,13 @@ TabPage TP_TEXTGRID_PAGE
     };
     FixedText       FT_CHARSPERLINE
     {
-        Pos = MAP_APPFONT ( 12 , 85 ) ;
+        Pos = MAP_APPFONT ( 12 , 98 ) ;
         Size = MAP_APPFONT ( 59 , 8 ) ;
         Text [ en-US ] = "Characters per line" ;
 	};
     NumericField    NF_CHARSPERLINE
     {
-        Pos = MAP_APPFONT ( 74 , 83 ) ;
+        Pos = MAP_APPFONT ( 74 , 96 ) ;
         Size = MAP_APPFONT ( 25 , 12 ) ;
         Border = TRUE ;
         TabStop = TRUE ;
@@ -152,44 +159,68 @@ TabPage TP_TEXTGRID_PAGE
         Last = 233 ;
 		SpinSize = 1 ;
     };
-    FixedText       FT_RUBYSIZE
+    FixedText       FT_CHARWIDTH
     {
-        Pos = MAP_APPFONT ( 133 , 85 ) ;
+        Pos = MAP_APPFONT ( 133 , 98 ) ;
         Size = MAP_APPFONT ( 80 , 8 ) ;
-        Text [ en-US ] = "Max. Ruby text size" ;
+        Text [ en-US ] = "Character ~width" ;
+        Text [ zh-CN ] = "字符跨度" ;
 	};
-    MetricField     MF_RUBYSIZE
+    MetricField     MF_CHARWIDTH
     {
-        Pos = MAP_APPFONT ( 216 , 83 ) ;
+        Pos = MAP_APPFONT ( 216 , 96 ) ;
         Size = MAP_APPFONT ( 35 , 12 ) ;
         Border = TRUE ;
         TabStop = TRUE ;
 		Left = TRUE ;
 		Repeat = TRUE ;
 		Spin = TRUE ;
-		Minimum = 1 ;
+		Minimum = 0 ;
 		Maximum = 5600 ;
-        DecimalDigits = 1 ;
+        DecimalDigits = 2 ;
         Unit = FUNIT_POINT;
-		First = 10 ;
+		First = 0 ;
 		Last = 5600 ;
         SpinSize = 5 ;
     };
+    FixedText       FT_RUBYSIZE
+    {
+        Pos = MAP_APPFONT ( 133 , 98 ) ;
+        Size = MAP_APPFONT ( 80 , 8 ) ;
+        Text [ en-US ] = "Max. Ruby text size" ;
+    };
+    MetricField     MF_RUBYSIZE
+    {
+        Pos = MAP_APPFONT ( 216 , 96 ) ;
+        Size = MAP_APPFONT ( 35 , 12 ) ;
+        Border = TRUE ;
+        TabStop = TRUE ;
+        Left = TRUE ;
+        Repeat = TRUE ;
+        Spin = TRUE ;
+        Minimum = 0 ;
+        Maximum = 5600 ;
+        DecimalDigits = 2 ;
+        Unit = FUNIT_POINT;
+        First = 0 ;
+        Last = 5600 ;
+        SpinSize = 5 ;
+    };
     CheckBox        CB_RUBYBELOW
     {
-        Pos = MAP_APPFONT ( 14 , 99 ) ;
-        Size = MAP_APPFONT ( 242 , 10 ) ;
+        Pos = MAP_APPFONT ( 14 , 112 ) ;
+        Size = MAP_APPFONT ( 110 , 10 ) ;
         Text [ en-US ] = "Ruby text below/left from base text" ;
 	};
     FixedLine       FL_DISPLAY
     {
-        Pos = MAP_APPFONT ( 6 , 115 ) ;
+        Pos = MAP_APPFONT ( 6 , 128 ) ;
         Size = MAP_APPFONT ( 248 , 8 ) ;
         Text [ en-US ] = "Grid display" ;
 	};
     CheckBox        CB_DISPLAY
     {
-        Pos = MAP_APPFONT ( 12 , 126 ) ;
+        Pos = MAP_APPFONT ( 12 , 139 ) ;
         Size = MAP_APPFONT ( 112 , 10 ) ;
         Text [ en-US ] = "Display grid" ;
 	};
@@ -200,7 +231,7 @@ TabPage TP_TEXTGRID_PAGE
 	};
     CheckBox        CB_PRINT
     {
-        Pos = MAP_APPFONT ( 18 , 141 ) ;
+        Pos = MAP_APPFONT ( 18 , 154 ) ;
         Size = MAP_APPFONT ( 106 , 10 ) ;
         Text [ en-US ] = "Print grid" ;
 	};
@@ -208,13 +239,13 @@ TabPage TP_TEXTGRID_PAGE
 	};
     FixedText       FT_COLOR
     {
-        Pos = MAP_APPFONT ( 133 , 126 ) ;
+        Pos = MAP_APPFONT ( 133 , 139 ) ;
         Size = MAP_APPFONT ( 55 , 8 ) ;
         Text [ en-US ] = "Grid color" ;
 	};
     ListBox         LB_COLOR
     {
-        Pos = MAP_APPFONT ( 191 , 124 ) ;
+        Pos = MAP_APPFONT ( 191 , 137 ) ;
         Size = MAP_APPFONT ( 60 , 50 ) ;
         TabStop = TRUE;
         Border = TRUE;
--- sw/source/core/layout/atrfrm.cxx.orig	2007-07-26 14:26:13.000000000 +0800
+++ sw/source/core/layout/atrfrm.cxx	2007-07-26 14:38:24.000000000 +0800
@@ -274,6 +274,10 @@
 #endif
 // <--
 
+#ifndef _SVTOOLS_CJKOPTIONS_HXX
+#include <svtools/cjkoptions.hxx>
+#endif
+
 using namespace ::com::sun::star;
 using namespace ::rtl;
 
@@ -2279,7 +2283,8 @@ BOOL SwFmtLineNumber::PutValue( const un
 SwTextGridItem::SwTextGridItem()
     : SfxPoolItem( RES_TEXTGRID ), aColor( COL_LIGHTGRAY ), nLines( 20 ),
       nBaseHeight( 400 ), nRubyHeight( 200 ), eGridType( GRID_NONE ),
-      bRubyTextBelow( 0 ), bPrintGrid( 1 ), bDisplayGrid( 1 )
+      bRubyTextBelow( 0 ), bPrintGrid( 1 ), bDisplayGrid( 1 ),
+      nBaseWidth(400), bSnapToChars( 1 ), bSquaredMode(1)
 {
 }
 
@@ -2297,7 +2302,10 @@ int SwTextGridItem::operator==( const Sf
            bRubyTextBelow == ((SwTextGridItem&)rAttr).GetRubyTextBelow() &&
            bDisplayGrid == ((SwTextGridItem&)rAttr).GetDisplayGrid() &&
            bPrintGrid == ((SwTextGridItem&)rAttr).GetPrintGrid() &&
-           aColor == ((SwTextGridItem&)rAttr).GetColor();
+           aColor == ((SwTextGridItem&)rAttr).GetColor() && 
+           GetBaseWidth() == ((SwTextGridItem&)rAttr).GetBaseWidth() && 
+           bSnapToChars == ((SwTextGridItem&)rAttr).GetSnapToChars() &&
+           bSquaredMode == ((SwTextGridItem&)rAttr).GetSquaredMode();
 }
 
 SfxPoolItem* SwTextGridItem::Clone( SfxItemPool* pPool ) const
@@ -2315,6 +2323,9 @@ SwTextGridItem& SwTextGridItem::operator
     bRubyTextBelow = rCpy.GetRubyTextBelow();
     bPrintGrid = rCpy.GetPrintGrid();
     bDisplayGrid = rCpy.GetDisplayGrid();
+    nBaseWidth = rCpy.GetBaseWidth();
+    bSnapToChars = rCpy.GetSnapToChars();
+    bSquaredMode = rCpy.GetSquaredMode();
 
 	return *this;
 }
@@ -2345,6 +2356,11 @@ BOOL SwTextGridItem::QueryValue( uno::An
                         "This value needs TWIPS-MM100 conversion" );
             rVal <<= (sal_Int32) TWIP_TO_MM100(nBaseHeight);
             break;
+        case MID_GRID_BASEWIDTH:
+            DBG_ASSERT( (nMemberId & CONVERT_TWIPS) != 0,
+                        "This value needs TWIPS-MM100 conversion" );
+            rVal <<= (sal_Int32) TWIP_TO_MM100(nBaseWidth);
+            break;
         case MID_GRID_RUBYHEIGHT:
             DBG_ASSERT( (nMemberId & CONVERT_TWIPS) != 0,
                         "This value needs TWIPS-MM100 conversion" );
@@ -2368,6 +2384,12 @@ BOOL SwTextGridItem::QueryValue( uno::An
                     break;
             }
             break;
+        case MID_GRID_SNAPTOCHARS:
+            rVal.setValue( &bSnapToChars, ::getBooleanCppuType() );
+            break;
+        case MID_GRID_SQUARE_MODE:
+            rVal.setValue( &bSquaredMode, ::getBooleanCppuType() );
+            break;
         default:
             DBG_ERROR("Unknown SwTextGridItem member");
             bRet = FALSE;
@@ -2410,6 +2432,7 @@ BOOL SwTextGridItem::PutValue( const uno
             SetDisplayGrid( *(sal_Bool*)rVal.getValue() );
             break;
         case MID_GRID_BASEHEIGHT:
+        case MID_GRID_BASEWIDTH:
         case MID_GRID_RUBYHEIGHT:
         {
             DBG_ASSERT( (nMemberId & CONVERT_TWIPS) != 0,
@@ -2420,6 +2443,8 @@ BOOL SwTextGridItem::PutValue( const uno
             if( bRet && (nTmp >= 0) && ( nTmp <= USHRT_MAX) )
                 if( (nMemberId & ~CONVERT_TWIPS) == MID_GRID_BASEHEIGHT )
                     SetBaseHeight( (USHORT)nTmp );
+                else if( (nMemberId & ~CONVERT_TWIPS) == MID_GRID_BASEWIDTH )
+                    SetBaseWidth( (USHORT)nTmp );
                 else
                     SetRubyHeight( (USHORT)nTmp );
             else
@@ -2448,6 +2473,12 @@ BOOL SwTextGridItem::PutValue( const uno
                 }
             }
             break;
+        case MID_GRID_SNAPTOCHARS:
+            SetSnapToChars( *(sal_Bool*)rVal.getValue() );
+            break;
+        case MID_GRID_SQUARE_MODE:
+            SetSquaredMode( *(sal_Bool*)rVal.getValue() );
+            break;
         default:
             DBG_ERROR("Unknown SwTextGridItem member");
             bRet = FALSE;
@@ -2456,6 +2487,75 @@ BOOL SwTextGridItem::PutValue( const uno
     return bRet;
 }
 
+void SwTextGridItem::SwitchPaperMode(BOOL bNew)
+{
+	if( bNew == bSquaredMode )
+	{
+		//same paper mode, not switch
+		return;
+	}
+
+	// use default value when grid is disable
+	if( eGridType == GRID_NONE )
+	{
+		bSquaredMode = bNew;
+		Init();
+		return;
+	}
+
+	if( bSquaredMode )
+	{
+		//switch from "squared mode" to "standard mode"
+		nBaseWidth = nBaseHeight;
+		nBaseHeight = nBaseHeight + nRubyHeight;
+		nRubyHeight = 0;
+	}
+	else
+	{
+		//switch from "standard mode" to "squared mode"
+		nRubyHeight = nBaseHeight/3;
+		nBaseHeight = nBaseHeight - nRubyHeight;
+		nBaseWidth = nBaseHeight;
+	}
+	bSquaredMode = !bSquaredMode;
+}
+
+void SwTextGridItem::Init()
+{
+	if( bSquaredMode )
+	{
+		nLines = 20;
+		nBaseHeight = 400;
+		nRubyHeight = 200;
+		eGridType = GRID_NONE;
+		bRubyTextBelow = 0;
+		bPrintGrid = 1;
+		bDisplayGrid = 1;
+		bSnapToChars = 1;
+		nBaseWidth = 400;
+	}
+	else
+	{
+		nLines = 44;
+		nBaseHeight = 312;
+		nRubyHeight = 0;
+		eGridType = GRID_NONE;
+		bRubyTextBelow = 0;
+		bPrintGrid = 1;
+		bDisplayGrid = 1;
+		nBaseWidth = 210;
+		bSnapToChars = 1;
+
+		//default grid type is line only in CJK env
+		//disable this function due to type area change 
+		//if grid type change.
+		//if(SvtCJKOptions().IsAsianTypographyEnabled())
+		//{
+		//	bDisplayGrid = 0;
+		//	eGridType = GRID_LINES_ONLY;
+		//}
+	}
+}
 // class SwHeaderAndFooterEatSpacingItem
 
 SfxPoolItem* SwHeaderAndFooterEatSpacingItem::Clone( SfxItemPool* pPool ) const
--- sw/source/core/doc/docdesc.cxx.orig	2007-07-26 14:26:12.000000000 +0800
+++ sw/source/core/doc/docdesc.cxx	2007-07-26 14:32:18.000000000 +0800
@@ -157,6 +157,8 @@
 #include <SwUndoPageDesc.hxx>
 #include <headerfooterhelper.hxx>
 
+#include <tgrditem.hxx>
+
 using namespace com::sun::star;
 
 static void lcl_DefaultPageFmt( sal_uInt16 nPoolFmtId,
@@ -1068,3 +1070,36 @@ void SwDoc::CheckDefaultPageFmt()
             lcl_DefaultPageFmt( rDesc.GetPoolFmtId(), rDesc.GetMaster(), rDesc.GetLeft() );
     }
 }
+
+void SwDoc::SetDefaultPageMode(bool bSquaredPageMode)
+{
+	if( bSquaredPageMode == IsSquaredPageMode() )
+		return;
+
+	const SwTextGridItem& rGrid =
+					(const SwTextGridItem&)GetDefault( RES_TEXTGRID );
+	SwTextGridItem aNewGrid = rGrid;
+	aNewGrid.SetSquaredMode(bSquaredPageMode);
+	aNewGrid.Init();
+	SetDefault(aNewGrid);
+	
+    for ( USHORT i = 0; i < GetPageDescCnt(); ++i )
+    {
+        SwPageDesc& rDesc = _GetPageDesc( i );
+
+        SwFrmFmt& rMaster = rDesc.GetMaster();
+		SwFrmFmt& rLeft = rDesc.GetLeft();
+        
+        SwTextGridItem aGrid((SwTextGridItem&)rMaster.GetAttr(RES_TEXTGRID));
+        aGrid.SwitchPaperMode( bSquaredPageMode );
+		rMaster.SetAttr(aGrid);
+		rLeft.SetAttr(aGrid);
+    }
+}
+
+sal_Bool SwDoc::IsSquaredPageMode() const
+{
+	const SwTextGridItem& rGrid =
+						(const SwTextGridItem&)GetDefault( RES_TEXTGRID );
+	return rGrid.IsSquaredMode();					
+}
--- sw/source/ui/misc/pggrid.cxx.orig	2007-07-26 14:26:14.000000000 +0800
+++ sw/source/ui/misc/pggrid.cxx	2007-07-26 14:44:32.000000000 +0800
@@ -69,6 +69,9 @@
 #ifndef _SVX_LRSPITEM_HXX
 #include <svx/lrspitem.hxx>
 #endif
+#ifndef _SVX_ULSPITEM_HXX
+#include <svx/ulspitem.hxx>
+#endif
 #ifndef _SVX_BOXITEM_HXX
 #include <svx/boxitem.hxx>
 #endif
@@ -83,6 +86,11 @@
 #endif
 #include <pggrid.hrc>
 
+#include "wrtsh.hxx"
+#include "doc.hxx"
+#include "uiitems.hxx"
+#include "swmodule.hxx"
+#include "view.hxx"
 /*-- 06.02.2002 15:25:39---------------------------------------------------
 
   -----------------------------------------------------------------------*/
@@ -92,6 +100,7 @@ SwTextGridPage::SwTextGridPage(Window *p
     aNoGridRB               (this, SW_RES(RB_NOGRID          )),
     aLinesGridRB            (this, SW_RES(RB_LINESGRID       )),
     aCharsGridRB            (this, SW_RES(RB_CHARSGRID       )),
+    aSnapToCharsCB          (this, SW_RES(CB_SNAPTOCHARS     )),
     aExampleWN              (this, SW_RES(WN_EXAMPLE         )),
     aLayoutFL               (this, SW_RES(FL_LAYOUT          )),
     aLinesPerPageFT         (this, SW_RES(FT_LINESPERPAGE    )),
@@ -100,6 +109,8 @@ SwTextGridPage::SwTextGridPage(Window *p
     aTextSizeMF             (this, SW_RES(MF_TEXTSIZE        )),
     aCharsPerLineFT         (this, SW_RES(FT_CHARSPERLINE    )),
     aCharsPerLineNF         (this, SW_RES(NF_CHARSPERLINE    )),
+    aCharWidthFT            (this, SW_RES(FT_CHARWIDTH       )),
+    aCharWidthMF            (this, SW_RES(MF_CHARWIDTH       )),
     aRubySizeFT             (this, SW_RES(FT_RUBYSIZE        )),
     aRubySizeMF             (this, SW_RES(MF_RUBYSIZE        )),
     aRubyBelowCB            (this, SW_RES(CB_RUBYBELOW       )),
@@ -111,7 +122,8 @@ SwTextGridPage::SwTextGridPage(Window *p
     m_nRubyUserValue(0),
     m_bRubyUserValue(sal_False),
     m_aPageSize(MM50, MM50),
-    m_bVertical(sal_False)
+    m_bVertical(sal_False),
+    m_bSquaredMode(sal_False)
 {
     FreeResource();
 
@@ -122,24 +134,37 @@ SwTextGridPage::SwTextGridPage(Window *p
     aControls[4] =&aTextSizeMF;
     aControls[5] =&aCharsPerLineFT;
     aControls[6] =&aCharsPerLineNF;
-    aControls[7] =&aRubySizeFT;
-    aControls[8] =&aRubySizeMF;
-    aControls[9] =&aRubyBelowCB;
-    aControls[10] =&aDisplayFL;
-    aControls[11] =&aDisplayCB;
-    aControls[12] =&aPrintCB;
-    aControls[13] =&aColorFT;
-    aControls[14] =&aColorLB;
-    aControls[15] =0;
+    aControls[7] =&aCharWidthFT;
+    aControls[8] =&aCharWidthMF;
+    aControls[9] =&aRubySizeFT;
+    aControls[10] =&aRubySizeMF;    
+    aControls[11] =&aRubyBelowCB;
+    aControls[12] =&aDisplayFL;
+    aControls[13] =&aDisplayCB;
+    aControls[14] =&aPrintCB;
+    aControls[15] =&aColorFT;
+    aControls[16] =&aColorLB;
+    aControls[17] =0;
 
-    Link aLink = LINK(this, SwTextGridPage, CharSizeChangedHdl);
+    Link aLink = LINK(this, SwTextGridPage, CharorLineChangedHdl);
     aCharsPerLineNF.SetUpHdl(aLink);
-    aTextSizeMF.SetUpHdl(aLink);
     aCharsPerLineNF.SetDownHdl(aLink);
-    aTextSizeMF.SetDownHdl(aLink);
     aCharsPerLineNF.SetLoseFocusHdl(aLink);
-    aTextSizeMF.SetLoseFocusHdl(aLink);
-
+    aLinesPerPageNF.SetUpHdl(aLink);
+    aLinesPerPageNF.SetDownHdl(aLink);
+    aLinesPerPageNF.SetLoseFocusHdl(aLink);
+    
+    Link aSizeLink = LINK(this, SwTextGridPage, TextSizeChangedHdl);
+    aTextSizeMF.SetUpHdl(aSizeLink);
+    aTextSizeMF.SetDownHdl(aSizeLink);
+    aTextSizeMF.SetLoseFocusHdl(aSizeLink);
+    aRubySizeMF.SetUpHdl(aSizeLink);
+    aRubySizeMF.SetDownHdl(aSizeLink);
+    aRubySizeMF.SetLoseFocusHdl(aSizeLink); 
+    aCharWidthMF.SetUpHdl(aSizeLink);
+    aCharWidthMF.SetDownHdl(aSizeLink);
+    aCharWidthMF.SetLoseFocusHdl(aSizeLink);
+    
     Link aGridTypeHdl = LINK(this, SwTextGridPage, GridTypeHdl);
     aNoGridRB   .SetClickHdl(aGridTypeHdl);
     aLinesGridRB.SetClickHdl(aGridTypeHdl);
@@ -148,14 +173,8 @@ SwTextGridPage::SwTextGridPage(Window *p
     Link aModifyLk = LINK(this, SwTextGridPage, GridModifyHdl);
     aColorLB.SetSelectHdl(aModifyLk);
     aPrintCB.SetClickHdl(aModifyLk);
-    aRubyBelowCB.SetClickHdl(aModifyLk);
+    aRubyBelowCB.SetClickHdl(aModifyLk);   
 
-    aLinesPerPageNF.SetUpHdl(aModifyLk);
-    aRubySizeMF.SetUpHdl(aModifyLk);
-    aLinesPerPageNF.SetDownHdl(aModifyLk);
-    aRubySizeMF.SetDownHdl(aModifyLk);
-    aLinesPerPageNF.SetLoseFocusHdl(aModifyLk);
-    aRubySizeMF.SetLoseFocusHdl(aModifyLk);
 
     aDisplayCB.SetClickHdl(LINK(this, SwTextGridPage, DisplayGridHdl));
 
@@ -169,6 +188,34 @@ SwTextGridPage::SwTextGridPage(Window *p
 		aColorLB.InsertEntry( aColor, sName );
 	}
 	aColorLB.SetUpdateMode( TRUE );
+	//Get the default paper mode
+	SwView *pView   = ::GetActiveView();
+	if( pView )
+	{
+		SwWrtShell* pSh = pView->GetWrtShellPtr();
+		if( pSh )
+		{
+			m_bSquaredMode = pSh->GetDoc()->IsSquaredPageMode();
+		}
+	}
+	if( m_bSquaredMode )
+	{
+		aRubySizeFT.Show();
+		aRubySizeMF.Show();
+		aRubyBelowCB.Show();
+		aSnapToCharsCB.Hide();
+		aCharWidthFT.Hide();
+		aCharWidthMF.Hide();
+	}
+	else
+	{
+		aRubySizeFT.Hide();
+		aRubySizeMF.Hide();
+		aRubyBelowCB.Hide();
+		aSnapToCharsCB.Show();
+		aCharWidthFT.Show();
+		aCharWidthMF.Show();
+	}
 }
 /*-- 06.02.2002 15:25:40---------------------------------------------------
 
@@ -196,7 +243,9 @@ BOOL    SwTextGridPage::FillItemSet(SfxI
         aTextSizeMF.GetSavedValue().ToInt32() != aTextSizeMF.GetValue()||
         aCharsPerLineNF.GetSavedValue().ToInt32()
            != aCharsPerLineNF.GetValue()||
+        aSnapToCharsCB.GetSavedValue() != aSnapToCharsCB.IsChecked() ||
         aRubySizeMF.GetSavedValue().ToInt32() != aRubySizeMF.GetValue()||
+        aCharWidthMF.GetSavedValue().ToInt32() != aCharWidthMF.GetValue()||
         aRubyBelowCB.GetSavedValue() != aRubyBelowCB.IsChecked()||
         aDisplayCB.GetSavedValue() != aDisplayCB.IsChecked()||
         aPrintCB.GetSavedValue() != aPrintCB.IsChecked()||
@@ -225,11 +274,13 @@ void    SwTextGridPage::Reset(const SfxI
         }
         pButton->Check();
         GridTypeHdl(pButton);
+        aSnapToCharsCB.Check(rGridItem.IsSnapToChars());
         aLinesPerPageNF.SetValue(rGridItem.GetLines());
         m_nRubyUserValue = rGridItem.GetBaseHeight();
         m_bRubyUserValue = sal_True;
         aTextSizeMF.SetValue(aTextSizeMF.Normalize(m_nRubyUserValue), FUNIT_TWIP);
         aRubySizeMF.SetValue(aRubySizeMF.Normalize(rGridItem.GetRubyHeight()), FUNIT_TWIP);
+        aCharWidthMF.SetValue(aCharWidthMF.Normalize(rGridItem.GetBaseWidth()), FUNIT_TWIP);
         aRubyBelowCB.Check(rGridItem.IsRubyTextBelow());
         aDisplayCB.Check(rGridItem.IsDisplayGrid());
         aPrintCB.Check(rGridItem.IsPrintGrid());
@@ -239,10 +290,12 @@ void    SwTextGridPage::Reset(const SfxI
 
     aNoGridRB.SaveValue();
     aLinesGridRB.SaveValue();
+    aSnapToCharsCB.SaveValue();
     aLinesPerPageNF.SaveValue();
     aTextSizeMF.SaveValue();
-    aCharsPerLineNF.SaveValue();
+	aCharsPerLineNF.SaveValue();
     aRubySizeMF.SaveValue();
+    aCharWidthMF.SaveValue();
     aRubyBelowCB.SaveValue();
     aDisplayCB.SaveValue();
     aPrintCB.SaveValue();
@@ -275,12 +328,15 @@ void SwTextGridPage::PutGridItem(SfxItem
         SwTextGridItem aGridItem;
         aGridItem.SetGridType(aNoGridRB.IsChecked() ? GRID_NONE :
             aLinesGridRB.IsChecked() ? GRID_LINES_ONLY : GRID_LINES_CHARS );
+        aGridItem.SetSnapToChars(aSnapToCharsCB.IsChecked());
         aGridItem.SetLines(aLinesPerPageNF.GetValue());
         aGridItem.SetBaseHeight(
             m_bRubyUserValue ? m_nRubyUserValue :
                 aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP)));
         aGridItem.SetRubyHeight(aRubySizeMF.Denormalize(aRubySizeMF.GetValue(FUNIT_TWIP)));
+        aGridItem.SetBaseWidth(aCharWidthMF.Denormalize(aCharWidthMF.GetValue(FUNIT_TWIP)));
         aGridItem.SetRubyTextBelow(aRubyBelowCB.IsChecked());
+        aGridItem.SetSquaredMode(m_bSquaredMode);
         aGridItem.SetDisplayGrid(aDisplayCB.IsChecked());
         aGridItem.SetPrintGrid(aPrintCB.IsChecked());
         aGridItem.SetColor(aColorLB.GetSelectEntryColor());
@@ -292,7 +348,7 @@ void SwTextGridPage::PutGridItem(SfxItem
 void SwTextGridPage::UpdatePageSize(const SfxItemSet& rSet)
 {
     if( SFX_ITEM_UNKNOWN !=  rSet.GetItemState( RES_FRAMEDIR, TRUE ))
-	{
+    {
         const SvxFrameDirectionItem& rDirItem =
                     (const SvxFrameDirectionItem&)rSet.Get(RES_FRAMEDIR);
         m_bVertical = rDirItem.GetValue() == FRMDIR_VERT_TOP_RIGHT||
@@ -305,13 +361,16 @@ void SwTextGridPage::UpdatePageSize(cons
                                             SID_ATTR_PAGE_SIZE);
         const SvxLRSpaceItem& rLRSpace = (const SvxLRSpaceItem&)rSet.Get(
                                                             RES_LR_SPACE );
+        const SvxULSpaceItem& rULSpace = (const SvxULSpaceItem&)rSet.Get(
+                                                            RES_UL_SPACE );                                                    
         const SvxBoxItem& rBox = (const SvxBoxItem&) rSet.Get(RES_BOX);
-        sal_Int32 nDistance = rLRSpace.GetLeft() + rLRSpace.GetRight();
+        sal_Int32 nDistanceLR = rLRSpace.GetLeft() + rLRSpace.GetRight();
+        sal_Int32 nDistanceUL = rULSpace.GetUpper() + rULSpace.GetLower();
 
-        sal_Int32 nValue1 = rSize.GetSize().Height() - nDistance -
+        sal_Int32 nValue1 = rSize.GetSize().Height() - nDistanceUL -
                 rBox.GetDistance(BOX_LINE_TOP) -
                                     rBox.GetDistance(BOX_LINE_BOTTOM);
-        sal_Int32 nValue2 = rSize.GetSize().Width() - nDistance -
+        sal_Int32 nValue2 = rSize.GetSize().Width() - nDistanceLR -
                 rBox.GetDistance(BOX_LINE_LEFT) -
                                     rBox.GetDistance(BOX_LINE_RIGHT);
         if(m_bVertical)
@@ -328,7 +387,20 @@ void SwTextGridPage::UpdatePageSize(cons
         sal_Int32 nTextSize = m_bRubyUserValue ?
                     m_nRubyUserValue :
                         aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP));
-        aCharsPerLineNF.SetValue(m_aPageSize.Width() / nTextSize);
+        
+        if ( m_bSquaredMode )
+        {
+            aCharsPerLineNF.SetValue(m_aPageSize.Width() / nTextSize);
+        }
+        else
+        {
+            sal_Int32 nTextWidth = aCharWidthMF.Denormalize(aCharWidthMF.GetValue(FUNIT_TWIP));
+            aLinesPerPageNF.SetValue(m_aPageSize.Height() / nTextSize);
+            if (nTextWidth)
+                aCharsPerLineNF.SetValue(m_aPageSize.Width() / nTextWidth); 
+            else
+                aCharsPerLineNF.SetValue( 45 );
+        }
     }
 }
 /* -----------------------------06.02.2002 15:24------------------------------
@@ -344,21 +416,86 @@ USHORT* SwTextGridPage::GetRanges()
 /* -----------------------------08.02.2002 10:56------------------------------
 
  ---------------------------------------------------------------------------*/
-IMPL_LINK(SwTextGridPage, CharSizeChangedHdl, SpinField*, pField)
+IMPL_LINK(SwTextGridPage, CharorLineChangedHdl, SpinField*, pField)
 {
-    if(&aCharsPerLineNF == pField)
+    //if in squared mode
+    if ( m_bSquaredMode )
     {
-        long nWidth = m_aPageSize.Width() / aCharsPerLineNF.GetValue();
-        aTextSizeMF.SetValue(aTextSizeMF.Normalize(nWidth), FUNIT_TWIP);
-        //prevent rounding errors in the MetricField by saving the used value
-        m_nRubyUserValue = nWidth;
-        m_bRubyUserValue = sal_True;
+        if(&aCharsPerLineNF == pField)
+        {
+            long nWidth = m_aPageSize.Width() / aCharsPerLineNF.GetValue();
+            aTextSizeMF.SetValue(aTextSizeMF.Normalize(nWidth), FUNIT_TWIP);
+            //prevent rounding errors in the MetricField by saving the used value
+            m_nRubyUserValue = nWidth;
+            m_bRubyUserValue = sal_True;
+            
+			//set maximum line per page
+            {
+                sal_Int32 nMaxLines = m_aPageSize.Height() /
+                    (   aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP)) +
+                        aRubySizeMF.Denormalize(aRubySizeMF.GetValue(FUNIT_TWIP)));
+                aLinesPerPageNF.SetMax(nMaxLines);
+            }
+        }
+    }
+    else//in normal mode
+    {
+        if(&aLinesPerPageNF == pField)
+        {
+            long nHeight = m_aPageSize.Height() / aLinesPerPageNF.GetValue();
+            aTextSizeMF.SetValue(aTextSizeMF.Normalize(nHeight), FUNIT_TWIP);
+            aRubySizeMF.SetValue(0, FUNIT_TWIP);
+            
+            m_nRubyUserValue = nHeight;
+            m_bRubyUserValue = sal_True;
+        }
+        else if (&aCharsPerLineNF == pField)
+        {
+            long nWidth = m_aPageSize.Width() / aCharsPerLineNF.GetValue();
+            aCharWidthMF.SetValue(aCharWidthMF.Normalize(nWidth), FUNIT_TWIP);
+        }
+    }
+    GridModifyHdl(0);
+    return 0;
+}
+/* -----------------------------04.09.2006 15:46------------------------------
+ ---------------------------------------------------------------------------*/
+IMPL_LINK(SwTextGridPage, TextSizeChangedHdl, SpinField*, pField)
+{
+    //if in squared mode
+    if( m_bSquaredMode )
+    {
+        if (&aTextSizeMF == pField)
+        {
+            sal_Int32 nTextSize = aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP));
+            aCharsPerLineNF.SetValue(m_aPageSize.Width() / nTextSize);
+            m_bRubyUserValue = sal_False;
+        }
+		//set maximum line per page
+        {
+            sal_Int32 nMaxLines = m_aPageSize.Height() /
+                (   aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP)) +
+                    aRubySizeMF.Denormalize(aRubySizeMF.GetValue(FUNIT_TWIP)));
+            aLinesPerPageNF.SetMax(nMaxLines);
+        }
     }
     else
     {
-        sal_Int32 nTextSize = aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP));
-        aCharsPerLineNF.SetValue(m_aPageSize.Width() / nTextSize);
-        m_bRubyUserValue = sal_False;
+        if (&aTextSizeMF == pField)
+        {
+            sal_Int32 nTextSize = aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP));
+            aLinesPerPageNF.SetValue(m_aPageSize.Height() / nTextSize);
+            m_bRubyUserValue = sal_False;
+        }
+        else if (&aCharWidthMF == pField)
+        {
+            sal_Int32 nTextWidth = aCharWidthMF.Denormalize(aCharWidthMF.GetValue(FUNIT_TWIP));
+            if (nTextWidth)
+                aCharsPerLineNF.SetValue(m_aPageSize.Width() / nTextWidth); 
+            else
+                aCharsPerLineNF.SetValue( 45 );
+        }
+        //rubySize is disabled
     }
     GridModifyHdl(0);
     return 0;
@@ -375,7 +512,19 @@ IMPL_LINK(SwTextGridPage, GridTypeHdl, R
     //one special case
     if(bEnable)
         DisplayGridHdl(&aDisplayCB);
-
+	
+	bEnable = &aCharsGridRB == pButton;
+	aSnapToCharsCB.Enable(bEnable);
+	
+	bEnable = &aLinesGridRB == pButton;
+	if(bEnable && !m_bSquaredMode )
+	{
+		aCharsPerLineFT.Enable(sal_False);
+		aCharsPerLineNF.Enable(sal_False);
+		aCharWidthFT.Enable(sal_False);
+		aCharWidthMF.Enable(sal_False);
+	}
+	
     GridModifyHdl(0);
     return 0;
 }
@@ -392,11 +541,6 @@ IMPL_LINK(SwTextGridPage, DisplayGridHdl
  ---------------------------------------------------------------------------*/
 IMPL_LINK(SwTextGridPage, GridModifyHdl, void*, EMPTYARG)
 {
-    //set maximum for Lines per page
-    sal_Int32 nMaxLines = m_aPageSize.Height() /
-                (   aTextSizeMF.Denormalize(aTextSizeMF.GetValue(FUNIT_TWIP)) +
-                    aRubySizeMF.Denormalize(aRubySizeMF.GetValue(FUNIT_TWIP)));
-    aLinesPerPageNF.SetMax(nMaxLines);
     const SfxItemSet& rOldSet = GetItemSet();
     SfxItemSet aSet(rOldSet);
     const SfxItemSet* pExSet = GetTabDialog()->GetExampleSet();
