1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 #undef SC_DLLIMPLEMENTATION
28 
29 //------------------------------------------------------------------
30 
31 #include "tabbgcolordlg.hxx"
32 #include "scresid.hxx"
33 #include "miscdlgs.hrc"
34 
35 #include <tools/debug.hxx>
36 #include <tools/color.hxx>
37 #include <sfx2/objsh.hxx>
38 #include <svx/xtable.hxx>
39 #include <svx/drawitem.hxx>
40 #include <unotools/pathoptions.hxx>
41 #include <tools/resid.hxx>
42 #include <editeng/editrids.hrc>
43 #include <editeng/eerdll.hxx>
44 
45 #include <boost/scoped_ptr.hpp>
46 
47 //==================================================================
48 
49 #define HDL(hdl) LINK(this,ScTabBgColorDlg,hdl)
50 
ScTabBgColorDlg(Window * pParent,const String & rTitle,const String & rTabBgColorNoColorText,const Color & rDefaultColor,const rtl::OString & sHelpId)51 ScTabBgColorDlg::ScTabBgColorDlg( Window*         pParent,
52                                   const String&   rTitle,
53                                   const String&   rTabBgColorNoColorText,
54                                   const Color&    rDefaultColor,
55                                   const rtl::OString& sHelpId ) :
56     ModalDialog    ( pParent, ScResId( RID_SCDLG_TAB_BG_COLOR ) ),
57     aBorderWin              ( this, ScResId( TAB_BG_COLOR_CT_BORDER ) ),
58     aTabBgColorSet          ( &aBorderWin, ScResId( TAB_BG_COLOR_SET_BGDCOLOR ), this ),
59     aBtnOk                  ( this, ScResId( BTN_OK ) ),
60     aBtnCancel              ( this, ScResId( BTN_CANCEL ) ),
61     aBtnHelp                ( this, ScResId( BTN_HELP ) ),
62     aTabBgColor             ( rDefaultColor ),
63     aTabBgColorNoColorText  ( rTabBgColorNoColorText ),
64     msHelpId                ( sHelpId )
65 
66 {
67     SetHelpId( sHelpId );
68     this->SetText( rTitle );
69     this->SetStyle(GetStyle() | WB_BORDER | WB_STDFLOATWIN | WB_3DLOOK | WB_DIALOGCONTROL | WB_SYSTEMWINDOW | WB_STANDALONE | WB_HIDE);
70 
71     FillColorValueSets_Impl();
72     aTabBgColorSet.SetDoubleClickHdl( HDL(TabBgColorDblClickHdl_Impl) );
73     aBtnOk.SetClickHdl( HDL(TabBgColorOKHdl_Impl) );
74     FreeResource();
75 }
76 
77 //------------------------------------------------------------------------
78 
GetSelectedColor(Color & rColor) const79 void ScTabBgColorDlg::GetSelectedColor( Color& rColor ) const
80 {
81     rColor = this->aTabBgColor;
82 }
83 
~ScTabBgColorDlg()84 ScTabBgColorDlg::~ScTabBgColorDlg()
85 {
86 }
87 
FillColorValueSets_Impl()88 void ScTabBgColorDlg::FillColorValueSets_Impl()
89 {
90     SfxObjectShell* pDocSh = SfxObjectShell::Current();
91     const SfxPoolItem* pItem = NULL;
92     XColorListSharedPtr aColorTable;
93     const Size aSize15x15 = Size( 15, 15 );
94     const Size aSize10x10 = Size( 10, 10 );
95     const Size aSize5x5 = Size( 5, 5 );
96     sal_uInt16 nSelectedItem = 0;
97 
98     DBG_ASSERT( pDocSh, "DocShell not found!" );
99 
100     if ( pDocSh && ( 0 != ( pItem = pDocSh->GetItem(SID_COLOR_TABLE) ) ) )
101     {
102         aColorTable = static_cast< const SvxColorTableItem* >(pItem)->GetColorTable();
103     }
104 
105     if ( !aColorTable.get() )
106     {
107         aColorTable = XPropertyListFactory::CreateSharedXColorList(SvtPathOptions().GetPalettePath());
108     }
109 
110     long nColorCount(0);
111 
112     if ( aColorTable.get() )
113     {
114         nColorCount = aColorTable->Count();
115         Color aColWhite( COL_WHITE );
116         String aStrWhite( EditResId( RID_SVXITEMS_COLOR_WHITE ) );
117 
118         aTabBgColorSet.addEntriesForXColorList(aColorTable);
119     }
120 
121     if(nColorCount)
122     {
123         const WinBits nBits(aTabBgColorSet.GetStyle() | WB_NAMEFIELD | WB_ITEMBORDER | WB_NONEFIELD | WB_3DLOOK | WB_NO_DIRECTSELECT | WB_NOPOINTERFOCUS);
124         aTabBgColorSet.SetText( aTabBgColorNoColorText );
125         aTabBgColorSet.SetStyle( nBits );
126         static sal_Int32 nAdd = 4;
127 
128         // calculate new size of color control as base, derive size of border win
129         const Size aNewSize(aTabBgColorSet.layoutAllVisible(nColorCount));
130         const Size aNewSizeBorderWin(aNewSize.Width() + nAdd, aNewSize.Height() + nAdd);
131 
132         // from that, calculate a new dialog size
133         const Size aCurrentSizeDialog(GetOutputSizePixel());
134         const Size aCurrentSizeBorderWin(aBorderWin.GetOutputSizePixel());
135         const long nOffsetX(aCurrentSizeDialog.Width() - aCurrentSizeBorderWin.Width());
136         const long nOffsetY(aCurrentSizeDialog.Height() - aCurrentSizeBorderWin.Height());
137         const Size aNewSizeDialog(aNewSizeBorderWin.Width() + nOffsetX, aNewSizeBorderWin.Height() + nOffsetY);
138 
139         // also need to adapt pos and size for the three buttons; as a base, take their original
140         // distance from the dialog bottom and get new Y-Pos
141         const long aButtonOffsetFromBottom(aCurrentSizeDialog.Height() - aBtnOk.GetPosPixel().Y());
142         const long aNewButtonY(aNewSizeDialog.Height() - aButtonOffsetFromBottom);
143 
144         // for each button, scale width and x-pos by old/new dialog sizes and re-layout
145         // for Okay-Button
146         const long aNewWidthOkay((aBtnOk.GetSizePixel().Width() * aNewSizeDialog.Width()) / aCurrentSizeDialog.Width());
147         const long aNewPosOkay((aBtnOk.GetPosPixel().X() * aNewSizeDialog.Width()) / aCurrentSizeDialog.Width());
148         const Size aNewSizeOkay(aNewWidthOkay, aBtnOk.GetOutputSizePixel().Height());
149         aBtnOk.SetOutputSizePixel(aNewSizeOkay);
150         aBtnOk.SetPosSizePixel(Point(aNewPosOkay, aNewButtonY), aNewSizeOkay);
151 
152         // for Cancel-Button
153         const long aNewWidthCancel((aBtnCancel.GetSizePixel().Width() * aNewSizeDialog.Width()) / aCurrentSizeDialog.Width());
154         const long aNewPosCancel((aBtnCancel.GetPosPixel().X() * aNewSizeDialog.Width()) / aCurrentSizeDialog.Width());
155         const Size aNewSizeCancel(aNewWidthCancel, aBtnCancel.GetOutputSizePixel().Height());
156         aBtnCancel.SetOutputSizePixel(aNewSizeCancel);
157         aBtnCancel.SetPosSizePixel(Point(aNewPosCancel, aNewButtonY), aNewSizeCancel);
158 
159         // for Help-Button
160         const long aNewWidthHelp((aBtnHelp.GetSizePixel().Width() * aNewSizeDialog.Width()) / aCurrentSizeDialog.Width());
161         const long aNewPosHelp((aBtnHelp.GetPosPixel().X() * aNewSizeDialog.Width()) / aCurrentSizeDialog.Width());
162         const Size aNewSizeHelp(aNewWidthHelp, aBtnHelp.GetOutputSizePixel().Height());
163         aBtnHelp.SetOutputSizePixel(aNewSizeHelp);
164         aBtnHelp.SetPosSizePixel(Point(aNewPosHelp, aNewButtonY), aNewSizeHelp);
165 
166         // set new sizes for color control
167         aTabBgColorSet.SetOutputSizePixel(aNewSize);
168         aTabBgColorSet.SetPosSizePixel(Point(nAdd/2, nAdd/2), aNewSize);
169 
170         // set new size for border win
171         aBorderWin.SetOutputSizePixel(aNewSizeBorderWin);
172 
173         // set new size for dialog itself
174         SetOutputSizePixel(aNewSizeDialog);
175     }
176 
177     aTabBgColorSet.SelectItem(nSelectedItem);
178     aTabBgColorSet.Resize();
179 }
180 
IMPL_LINK(ScTabBgColorDlg,TabBgColorDblClickHdl_Impl,ValueSet *,EMPTYARG)181 IMPL_LINK( ScTabBgColorDlg, TabBgColorDblClickHdl_Impl, ValueSet*, EMPTYARG )
182 /*
183     Handler, called when color selection is changed
184 */
185 {
186     sal_uInt16 nItemId = aTabBgColorSet.GetSelectItemId();
187     Color aColor = nItemId ? ( aTabBgColorSet.GetItemColor( nItemId ) ) : Color( COL_AUTO );
188     aTabBgColor = aColor;
189     EndDialog( sal_True );
190     return 0;
191 }
192 
IMPL_LINK(ScTabBgColorDlg,TabBgColorOKHdl_Impl,OKButton *,EMPTYARG)193 IMPL_LINK( ScTabBgColorDlg, TabBgColorOKHdl_Impl, OKButton*, EMPTYARG )
194 {
195 
196 //    Handler, called when the OK button is pushed
197 
198     sal_uInt16 nItemId = aTabBgColorSet.GetSelectItemId();
199     Color aColor = nItemId ? ( aTabBgColorSet.GetItemColor( nItemId ) ) : Color( COL_AUTO );
200     aTabBgColor = aColor;
201     EndDialog( sal_True );
202     return 0;
203 }
204 
ScTabBgColorValueSet(Control * pParent,const ResId & rResId,ScTabBgColorDlg * pTabBgColorDlg)205 ScTabBgColorDlg::ScTabBgColorValueSet::ScTabBgColorValueSet( Control* pParent, const ResId& rResId, ScTabBgColorDlg* pTabBgColorDlg ) :
206     SvxColorValueSet(pParent, rResId)
207 {
208     aTabBgColorDlg = pTabBgColorDlg;
209 }
210 
KeyInput(const KeyEvent & rKEvt)211 void ScTabBgColorDlg::ScTabBgColorValueSet::KeyInput( const KeyEvent& rKEvt )
212 {
213     switch ( rKEvt.GetKeyCode().GetCode() )
214     {
215         case KEY_SPACE:
216         case KEY_RETURN:
217         {
218             sal_uInt16 nItemId = GetSelectItemId();
219             const Color& aColor = nItemId ? ( GetItemColor( nItemId ) ) : Color( COL_AUTO );
220             aTabBgColorDlg->aTabBgColor = aColor;
221             aTabBgColorDlg->EndDialog(sal_True);
222         }
223         break;
224     }
225     SvxColorValueSet::KeyInput(rKEvt);
226 }
227