1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 // include ---------------------------------------------------------------
32 #include <viewlayoutctrl.hxx>
33 
34 #ifndef _STATUS_HXX //autogen
35 #include <vcl/status.hxx>
36 #endif
37 #include <vcl/image.hxx>
38 #include <svl/eitem.hxx>
39 #include <svx/viewlayoutitem.hxx>
40 #ifndef _UTLUI_HRC
41 #include <utlui.hrc>
42 #endif
43 #include <swtypes.hxx>  // fuer Pathfinder
44 
45 // STATIC DATA -----------------------------------------------------------
46 
47 SFX_IMPL_STATUSBAR_CONTROL( SwViewLayoutControl, SvxViewLayoutItem );
48 
49 // -----------------------------------------------------------------------
50 
51 const long nImageWidthSingle = 15;
52 const long nImageWidthAuto = 25;
53 const long nImageWidthBook = 23;
54 const long nImageWidthSum = 63;
55 const long nImageHeight = 11;
56 
57 // -----------------------------------------------------------------------
58 
59 struct SwViewLayoutControl::SwViewLayoutControl_Impl
60 {
61     sal_uInt16      mnState; // 0 = single, 1 = auto, 2 = book, 3 = none
62     Image       maImageSingleColumn;
63     Image       maImageSingleColumn_Active;
64     Image       maImageAutomatic;
65     Image       maImageAutomatic_Active;
66     Image       maImageBookMode;
67     Image       maImageBookMode_Active;
68 };
69 
70 // class SwViewLayoutControl ------------------------------------------
71 
72 SwViewLayoutControl::SwViewLayoutControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
73     SfxStatusBarControl( _nSlotId, _nId, rStb ),
74     mpImpl( new SwViewLayoutControl_Impl )
75 {
76     mpImpl->mnState = 0;
77 
78     const sal_Bool bHC = GetStatusBar().GetSettings().GetStyleSettings().GetHighContrastMode();
79     mpImpl->maImageSingleColumn         = Image( bHC ? SW_RES(IMG_VIEWLAYOUT_SINGLECOLUMN_HC)          : SW_RES(IMG_VIEWLAYOUT_SINGLECOLUMN) );
80     mpImpl->maImageSingleColumn_Active  = Image( bHC ? SW_RES(IMG_VIEWLAYOUT_SINGLECOLUMN_ACTIVE_HC) : SW_RES(IMG_VIEWLAYOUT_SINGLECOLUMN_ACTIVE) );
81     mpImpl->maImageAutomatic            = Image( bHC ? SW_RES(IMG_VIEWLAYOUT_AUTOMATIC_HC)             : SW_RES(IMG_VIEWLAYOUT_AUTOMATIC) );
82     mpImpl->maImageAutomatic_Active     = Image( bHC ? SW_RES(IMG_VIEWLAYOUT_AUTOMATIC_ACTIVE_HC)    : SW_RES(IMG_VIEWLAYOUT_AUTOMATIC_ACTIVE) );
83     mpImpl->maImageBookMode             = Image( bHC ? SW_RES(IMG_VIEWLAYOUT_BOOKMODE_HC)              : SW_RES(IMG_VIEWLAYOUT_BOOKMODE) );
84     mpImpl->maImageBookMode_Active      = Image( bHC ? SW_RES(IMG_VIEWLAYOUT_BOOKMODE_ACTIVE_HC)     : SW_RES(IMG_VIEWLAYOUT_BOOKMODE_ACTIVE) );
85 }
86 
87 // -----------------------------------------------------------------------
88 
89 SwViewLayoutControl::~SwViewLayoutControl()
90 {
91     delete mpImpl;
92 }
93 
94 // -----------------------------------------------------------------------
95 
96 void SwViewLayoutControl::StateChanged( sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
97 {
98     if ( SFX_ITEM_AVAILABLE != eState || pState->ISA( SfxVoidItem ) )
99         GetStatusBar().SetItemText( GetId(), String() );
100     else
101     {
102         DBG_ASSERT( pState->ISA( SvxViewLayoutItem ), "invalid item type" );
103         const sal_uInt16 nColumns  = static_cast<const SvxViewLayoutItem*>( pState )->GetValue();
104         const bool   bBookMode = static_cast<const SvxViewLayoutItem*>( pState )->IsBookMode();
105 
106         // SingleColumn Mode
107         if ( 1 == nColumns )
108             mpImpl->mnState = 0;
109         // Automatic Mode
110         else if ( 0 == nColumns )
111             mpImpl->mnState = 1;
112         // Book Mode
113         else if ( bBookMode && 2 == nColumns )
114             mpImpl->mnState = 2;
115         else
116             mpImpl->mnState = 3;
117     }
118 
119 	if ( GetStatusBar().AreItemsVisible() )
120         GetStatusBar().SetItemData( GetId(), 0 );    // force repaint
121 }
122 
123 // -----------------------------------------------------------------------
124 
125 void SwViewLayoutControl::Paint( const UserDrawEvent& rUsrEvt )
126 {
127     OutputDevice*       pDev =  rUsrEvt.GetDevice();
128     Rectangle           aRect = rUsrEvt.GetRect();
129     Color               aOldLineColor = pDev->GetLineColor();
130     Color               aOldFillColor = pDev->GetFillColor();
131 
132     //pDev->SetLineColor();
133     //pDev->SetFillColor( pDev->GetBackground().GetColor() );
134 
135     const bool bSingleColumn    = 0 == mpImpl->mnState;
136     const bool bAutomatic       = 1 == mpImpl->mnState;
137     const bool bBookMode        = 2 == mpImpl->mnState;
138 
139     const long nXOffset = (aRect.GetWidth()  - nImageWidthSum)/2;
140     const long nYOffset = (aRect.GetHeight() - nImageHeight)/2;
141 
142     aRect.Left() = aRect.Left() + nXOffset;
143     aRect.Top()  = aRect.Top() + nYOffset;
144 
145     // draw single column image:
146     pDev->DrawImage( aRect.TopLeft(), bSingleColumn ? mpImpl->maImageSingleColumn_Active : mpImpl->maImageSingleColumn );
147 
148     // draw automatic image:
149     aRect.Left() += nImageWidthSingle;
150     pDev->DrawImage( aRect.TopLeft(), bAutomatic ? mpImpl->maImageAutomatic_Active       : mpImpl->maImageAutomatic );
151 
152     // draw bookmode image:
153     aRect.Left() += nImageWidthAuto;
154     pDev->DrawImage( aRect.TopLeft(), bBookMode ? mpImpl->maImageBookMode_Active         : mpImpl->maImageBookMode );
155 
156     // draw separators
157     //aRect = rUsrEvt.GetRect();
158     //aRect.Left() += nImageWidth;
159     //aRect.setWidth( 1 );
160     //pDev->DrawRect( aRect );
161     //aRect.Left() += nImageWidth;
162     //pDev->DrawRect( aRect );
163 
164     //pDev->SetLineColor( aOldLineColor );
165     //pDev->SetFillColor( aOldFillColor );
166 }
167 
168 sal_Bool SwViewLayoutControl::MouseButtonDown( const MouseEvent & rEvt )
169 {
170     const Rectangle aRect = getControlRect();
171     const Point aPoint = rEvt.GetPosPixel();
172     const long nXDiff = aPoint.X() - aRect.Left();
173 
174     sal_uInt16 nColumns = 1;
175     bool bBookMode = false;
176 
177     const long nXOffset = (aRect.GetWidth() - nImageWidthSum)/2;
178 
179     if ( nXDiff < nXOffset + nImageWidthSingle )
180     {
181         mpImpl->mnState = 0; // single
182         nColumns = 1;
183     }
184     else if ( nXDiff < nXOffset + nImageWidthSingle + nImageWidthAuto )
185     {
186         mpImpl->mnState = 1; // auto
187         nColumns = 0;
188     }
189     else
190     {
191         mpImpl->mnState = 2; // book
192         nColumns = 2;
193         bBookMode = true;
194     }
195 
196     // commit state change
197     SvxViewLayoutItem aViewLayout( nColumns, bBookMode );
198 
199     ::com::sun::star::uno::Any a;
200     aViewLayout.QueryValue( a );
201 
202     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs( 1 );
203     aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ViewLayout" ));
204     aArgs[0].Value = a;
205 
206     execute( aArgs );
207 
208     return sal_True;
209 }
210