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 #include "precompiled_svx.hxx"
23 #include "TextCharacterSpacingControl.hxx"
24 #include "TextPropertyPanel.hrc"
25 #include <sfx2/sidebar/ResourceDefinitions.hrc>
26 #include <svx/dialogs.hrc>
27 #include <svx/dialmgr.hxx>
28 #include <unotools/viewoptions.hxx>
29 #include <editeng/kernitem.hxx>
30 #include <sfx2/bindings.hxx>
31 #include <sfx2/dispatch.hxx>
32 #include <sfx2/sidebar/Theme.hxx>
33 
34 namespace svx { namespace sidebar {
35 
36 TextCharacterSpacingControl::TextCharacterSpacingControl (
37     Window* pParent,
38     svx::sidebar::TextPropertyPanel& rPanel,
39     SfxBindings* pBindings)
40 :	PopupControl( pParent,SVX_RES(RID_POPUPPANEL_TEXTPAGE_SPACING))
41 ,	mrTextPropertyPanel(rPanel)
42 ,	mpBindings(pBindings)
43 ,	maVSSpacing		(ValueSetWithTextControl::IMAGE_TEXT,this, SVX_RES(VS_SPACING))
44 ,	maLastCus		(this, SVX_RES(FT_LASTCUSTOM))
45 //,	maBorder		(this, SVX_RES(CT_BORDER))
46 ,	maFTSpacing		(this, SVX_RES(FT_SPACING))
47 ,	maLBKerning		(this, SVX_RES(LB_KERNING))
48 ,	maFTBy			(this, SVX_RES(FT_BY))
49 ,	maEditKerning	(this, SVX_RES(ED_KERNING))
50 
51 ,	mpImg			(NULL)
52 ,	mpImgSel		(NULL)
53 ,	mpStr			(NULL)
54 ,	mpStrTip		(NULL)
55 
56 ,	maImgCus		(SVX_RES(IMG_CUSTOM))
57 ,	maImgCusGrey	(SVX_RES(IMG_CUSTOM_GRAY))
58 ,	maStrCus		(SVX_RES(STR_CUSTOM))
59 ,	maStrCusE		(SVX_RES(STR_CUSTOM_E_TIP))	//add
60 ,	maStrCusC		(SVX_RES(STR_CUSTOM_C_TIP))	//add
61 ,	maStrCusN		(SVX_RES(STR_NORMAL_TIP))	//add
62 ,	maStrUnit		(SVX_RES(STR_PT))			//add
63 
64 ,	mnCustomKern(0)
65 ,	mnLastCus ( SPACING_NOCUSTOM )
66 ,	mbCusEnable(false)
67 ,	mbVS(true)
68 {
69 	initial();
70 	FreeResource();
71 	Link aLink = LINK(this, TextCharacterSpacingControl, KerningSelectHdl);
72 	maLBKerning.SetSelectHdl(aLink);
73 	aLink =LINK(this, TextCharacterSpacingControl, KerningModifyHdl);
74 	maEditKerning.SetModifyHdl(aLink);
75 
76 }
77 TextCharacterSpacingControl::~TextCharacterSpacingControl()
78 {
79 	delete[] mpImg;
80 	delete[] mpImgSel;
81 	delete[] mpStr;
82 	delete[] mpStrTip;
83 }
84 
85 void TextCharacterSpacingControl::initial()
86 {
87 	maVSSpacing.SetStyle( maVSSpacing.GetStyle()| WB_3DLOOK |  WB_NO_DIRECTSELECT  );
88 	{
89 		maVSSpacing.SetControlBackground(GetSettings().GetStyleSettings().GetHighContrastMode()?
90 		GetSettings().GetStyleSettings().GetMenuColor():
91 		sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Paint_PanelBackground ));
92 		maVSSpacing.SetColor(GetSettings().GetStyleSettings().GetHighContrastMode()?
93 		GetSettings().GetStyleSettings().GetMenuColor():
94 		sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Paint_PanelBackground ));
95 		maVSSpacing.SetBackground(GetSettings().GetStyleSettings().GetHighContrastMode()?
96 		GetSettings().GetStyleSettings().GetMenuColor():
97 		sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Paint_PanelBackground ));
98 		maFTSpacing.SetBackground(GetSettings().GetStyleSettings().GetHighContrastMode()?
99 		GetSettings().GetStyleSettings().GetMenuColor():
100 		sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Paint_PanelBackground ));
101 		maFTBy.SetBackground(GetSettings().GetStyleSettings().GetHighContrastMode()?
102 		GetSettings().GetStyleSettings().GetMenuColor():
103 		sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Paint_PanelBackground ));
104 	}
105 	mpImg = new Image[5];
106 	mpImg[0] = Image(SVX_RES(IMG_VERY_TIGHT));
107 	mpImg[1] = Image(SVX_RES(IMG_TIGHT));
108 	mpImg[2] = Image(SVX_RES(IMG_NORMAL));
109 	mpImg[3] = Image(SVX_RES(IMG_LOOSE));
110 	mpImg[4] = Image(SVX_RES(IMG_VERY_LOOSE));
111 
112 	mpImgSel = new Image[5];
113 	mpImgSel[0] = Image(SVX_RES(IMG_VERY_TIGHT_S));
114 	mpImgSel[1] = Image(SVX_RES(IMG_TIGHT_S));
115 	mpImgSel[2] = Image(SVX_RES(IMG_NORMAL_S));
116 	mpImgSel[3] = Image(SVX_RES(IMG_LOOSE_S));
117 	mpImgSel[4] = Image(SVX_RES(IMG_VERY_LOOSE_S));
118 
119 	mpStr = new XubString[5];
120 	mpStr[0] = XubString(SVX_RES(STR_VERY_TIGHT));
121 	mpStr[1] = XubString(SVX_RES(STR_TIGHT));
122 	mpStr[2] = XubString(SVX_RES(STR_NORMAL));
123 	mpStr[3] = XubString(SVX_RES(STR_LOOSE));
124 	mpStr[4] = XubString(SVX_RES(STR_VERY_LOOSE));
125 
126 
127 	mpStrTip = new XubString[5];
128 	mpStrTip[0] = XubString(SVX_RES(STR_VERY_TIGHT_TIP));
129 	mpStrTip[1] = XubString(SVX_RES(STR_TIGHT_TIP));
130 	mpStrTip[2] = XubString(SVX_RES(STR_NORMAL_TIP));
131 	mpStrTip[3] = XubString(SVX_RES(STR_LOOSE_TIP));
132 	mpStrTip[4] = XubString(SVX_RES(STR_VERY_LOOSE_TIP));
133 
134 	for (int i=0;i<5;i++)
135 		maVSSpacing.AddItem(mpImg[i], &mpImgSel[i],mpStr[i],&mpStrTip[i]);
136 
137     maVSSpacing.AddItem( maImgCus, 0, maStrCus, 0 );
138 
139     maVSSpacing.SetNoSelection();
140 	Link aLink = LINK(this, TextCharacterSpacingControl,VSSelHdl );
141 	maVSSpacing.SetSelectHdl(aLink);
142 	maVSSpacing.StartSelection();
143 	maVSSpacing.Show();
144 }
145 void TextCharacterSpacingControl::ToGetFocus()
146 {
147 	if(!mbVS)
148 		maLBKerning.GrabFocus();
149 	else
150 		maVSSpacing.GrabFocus();
151 }
152 
153 void TextCharacterSpacingControl::Rearrange(bool bLBAvailable,bool bAvailable, long nKerning)
154 {
155 	mbVS = true;
156     maVSSpacing.SetNoSelection();
157 	SvtViewOptions aWinOpt( E_WINDOW, SIDEBAR_SPACING_GLOBAL_VALUE );
158     if ( aWinOpt.Exists() )
159 	{
160 		::com::sun::star::uno::Sequence < ::com::sun::star::beans::NamedValue > aSeq = aWinOpt.GetUserData();
161 		::rtl::OUString aTmp;
162 		if ( aSeq.getLength())
163 			aSeq[0].Value >>= aTmp;
164 
165 		String aWinData( aTmp );
166 		mnCustomKern = aWinData.ToInt32();
167 		mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
168 		mbCusEnable = true;
169 	}
170 	else
171 	{
172 		mnLastCus = SPACING_NOCUSTOM;
173 		mbCusEnable = false;
174 	}
175 
176 	if( !mnLastCus )
177 	{
178 		maVSSpacing.ReplaceItemImages(6, maImgCusGrey,0);
179 	}
180 	else
181 	{
182 		//set custom tips
183 		maVSSpacing.ReplaceItemImages(6, maImgCus,0);
184 		if(mnCustomKern > 0)
185 		{
186 			String aStrTip( maStrCusE);   //LAST CUSTOM no tip defect //add
187 			aStrTip.Append( String::CreateFromDouble( (double)mnCustomKern / 10));
188 			aStrTip.Append(maStrUnit);		// modify
189 			maVSSpacing.SetItemText(6,aStrTip);
190 		}
191 		else if(mnCustomKern < 0)
192 		{
193 			String aStrTip(maStrCusC) ;		//LAST CUSTOM no tip defect //add
194 			aStrTip.Append( String::CreateFromDouble( (double)-mnCustomKern / 10));
195 			aStrTip.Append(maStrUnit);		// modify
196 			maVSSpacing.SetItemText( 6, aStrTip );
197 		}
198 		else
199 		{
200 			String aStrTip(maStrCusN) ;		//LAST CUSTOM no tip defect //add
201 			maVSSpacing.SetItemText( 6, aStrTip );
202 		}
203 
204 	}
205 
206 	if(bLBAvailable && bAvailable)
207 	{
208 		maLBKerning.Enable();
209 		maFTSpacing.Enable();
210 
211 		SfxMapUnit eUnit = mrTextPropertyPanel.GetSpaceController().GetCoreMetric();
212         MapUnit eOrgUnit = (MapUnit)eUnit;
213         MapUnit ePntUnit( MAP_POINT );
214         long nBig = maEditKerning.Normalize(nKerning);
215         nKerning = LogicToLogic( nBig, eOrgUnit, ePntUnit );
216 
217         if ( nKerning > 0 )
218         {
219             maFTBy.Enable();
220             maEditKerning.Enable();
221             maEditKerning.SetMax( 9999 );
222             maEditKerning.SetLast( 9999 );
223             maEditKerning.SetValue( nKerning );
224             maLBKerning.SelectEntryPos( SIDEBAR_SPACE_EXPAND );
225 			if(nKerning == 30)
226 			{
227 				maVSSpacing.SelectItem(4);
228 			}
229 			else if(nKerning == 60)
230 			{
231 				maVSSpacing.SelectItem(5);
232 			}
233 			else
234 			{
235                 maVSSpacing.SetNoSelection();
236                 maVSSpacing.SelectItem(0);
237 				mbVS = false;
238 			}
239         }
240         else if ( nKerning < 0 )
241         {
242 			maFTBy.Enable();
243             maEditKerning.Enable();
244             maEditKerning.SetValue( -nKerning );
245             maLBKerning.SelectEntryPos( SIDEBAR_SPACE_CONDENSED );
246             long nMax = mrTextPropertyPanel.GetSelFontSize()/6;
247             maEditKerning.SetMax( maEditKerning.Normalize( nMax ), FUNIT_POINT );
248             maEditKerning.SetLast( maEditKerning.GetMax( maEditKerning.GetUnit() ) );
249 			if( nKerning == -30 )
250 			{
251 				maVSSpacing.SelectItem(1);
252 			}
253 			else if( nKerning == -15 )
254 			{
255 				maVSSpacing.SelectItem(2);
256 			}
257 			else
258 			{
259                 maVSSpacing.SetNoSelection();
260                 maVSSpacing.SelectItem(0);
261 				mbVS = false;
262 			}
263         }
264         else
265         {
266 			maVSSpacing.SelectItem(3);
267 			maLBKerning.SelectEntryPos( SIDEBAR_SPACE_NORMAL );
268 			maFTBy.Disable();
269             maEditKerning.Disable();
270             maEditKerning.SetValue( 0 );
271             maEditKerning.SetMax( 9999 );
272             maEditKerning.SetLast( 9999 );
273         }
274 	}
275 	else if(bLBAvailable && !bAvailable)
276 	{
277 		//modified
278         maVSSpacing.SetNoSelection();
279         maVSSpacing.SelectItem(0);
280 		mbVS = false;
281 		maLBKerning.Enable();
282 		maFTSpacing.Enable();
283 		maLBKerning.SetNoSelection();
284 		maEditKerning.SetText(String());
285 		maEditKerning.Disable();
286 		maFTBy.Disable();
287 	}
288 	else
289 	{
290         maVSSpacing.SetNoSelection();
291         maVSSpacing.SelectItem(0);
292 		mbVS = false;
293 		maEditKerning.SetText(String());
294         maLBKerning.SetNoSelection();
295 		maLBKerning.Disable();
296 		maFTSpacing.Disable();
297         maEditKerning.Disable();
298 		maFTBy.Disable();
299 	}
300 	GetFocus();
301 	maVSSpacing.Format();
302 	maVSSpacing.StartSelection();
303 }
304 IMPL_LINK(TextCharacterSpacingControl, VSSelHdl, void *, pControl)
305 {
306 	mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
307 
308 	if(pControl == &maVSSpacing)
309 	{
310 		sal_uInt16 iPos = maVSSpacing.GetSelectItemId();
311 		short nKern = 0;
312 		SfxMapUnit eUnit = mrTextPropertyPanel.GetSpaceController().GetCoreMetric();
313 		long nVal = 0;
314 		if(iPos == 1)
315 		{
316 			nVal = LogicToLogic(30, MAP_POINT, (MapUnit)eUnit);
317 			nKern = (short)maEditKerning.Denormalize(nVal);
318 			SvxKerningItem aKernItem(-nKern, SID_ATTR_CHAR_KERNING);
319 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
320 			mrTextPropertyPanel.SetSpacing(-nKern);
321 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
322 		}
323 		else if(iPos == 2)
324 		{
325 			nVal = LogicToLogic(15, MAP_POINT, (MapUnit)eUnit);
326 			nKern = (short)maEditKerning.Denormalize(nVal);
327 			SvxKerningItem aKernItem(-nKern, SID_ATTR_CHAR_KERNING);
328 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
329 			mrTextPropertyPanel.SetSpacing(-nKern);
330 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
331 		}
332 		else if(iPos == 3)
333 		{
334 			SvxKerningItem aKernItem(0, SID_ATTR_CHAR_KERNING);
335 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
336 			mrTextPropertyPanel.SetSpacing(0);
337 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
338 		}
339 		else if(iPos == 4)
340 		{
341 			nVal = LogicToLogic(30, MAP_POINT, (MapUnit)eUnit);
342 			nKern = (short)maEditKerning.Denormalize(nVal);
343 			SvxKerningItem aKernItem(nKern, SID_ATTR_CHAR_KERNING);
344 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
345 			mrTextPropertyPanel.SetSpacing(nKern);
346 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
347 		}
348 		else if(iPos == 5)
349 		{
350 			nVal = LogicToLogic(60, MAP_POINT, (MapUnit)eUnit);
351 			nKern = (short)maEditKerning.Denormalize(nVal);
352 			SvxKerningItem aKernItem(nKern, SID_ATTR_CHAR_KERNING);
353 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
354 			mrTextPropertyPanel.SetSpacing(nKern);
355 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
356 		}
357 		else if(iPos == 6)
358 		{
359 			//modified
360 			if(mbCusEnable)
361 			{
362 				nVal = LogicToLogic(mnCustomKern, MAP_POINT, (MapUnit)eUnit);
363 				nKern = (short)maEditKerning.Denormalize(nVal);
364 				SvxKerningItem aKernItem(nKern , SID_ATTR_CHAR_KERNING);
365 				mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
366 				mrTextPropertyPanel.SetSpacing(nKern);
367 				mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
368 			}
369 			else
370 			{
371 				maVSSpacing.SetNoSelection();		//add , set no selection and keep the last select item
372 				maVSSpacing.Format();
373 				Invalidate();
374 				maVSSpacing.StartSelection();
375 			}
376 			//modify end
377 		}
378 
379 		if(iPos < 6 || (iPos == 6 && mbCusEnable)) //add
380 			mrTextPropertyPanel.EndSpacingPopupMode();
381 	}
382 
383 
384 
385 	return 0;
386 }
387 
388 IMPL_LINK(TextCharacterSpacingControl, KerningSelectHdl, ListBox*, EMPTYARG)
389 {
390 	if ( maLBKerning.GetSelectEntryPos() > 0 )
391 	{
392 		maFTBy.Enable();
393 		maEditKerning.Enable();
394 	}
395 	else
396 	{
397 		maEditKerning.SetValue( 0 );
398 		maFTBy.Disable();
399 		maEditKerning.Disable();
400 	}
401 
402     if ( maVSSpacing.GetSelectItemId() > 0 )
403 	{
404         maVSSpacing.SetNoSelection();
405         maVSSpacing.SelectItem(0);
406 		maVSSpacing.Format();
407 		Invalidate();
408 		maVSSpacing.StartSelection();
409 	}
410 	KerningModifyHdl( NULL );
411 	return 0;
412 }
413 IMPL_LINK(TextCharacterSpacingControl, KerningModifyHdl, MetricField*, EMPTYARG)
414 {
415     if ( maVSSpacing.GetSelectItemId() > 0 )
416 	{
417         maVSSpacing.SetNoSelection();
418         maVSSpacing.SelectItem(0);
419 		maVSSpacing.Format();
420 		Invalidate();
421 		maVSSpacing.StartSelection();
422 	}
423 	sal_uInt16 nPos = maLBKerning.GetSelectEntryPos();
424     short nKern = 0;
425     SfxMapUnit eUnit = mrTextPropertyPanel.GetSpaceController().GetCoreMetric();
426 	mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
427 	if ( nPos == SIDEBAR_SPACE_EXPAND || nPos == SIDEBAR_SPACE_CONDENSED )
428     {
429         long nTmp = static_cast<long>(maEditKerning.GetValue());
430         if ( nPos == SIDEBAR_SPACE_CONDENSED )
431 		{
432 			long nMax =  mrTextPropertyPanel.GetSelFontSize()/6;
433 			maEditKerning.SetMax( maEditKerning.Normalize( nMax ), FUNIT_TWIP );
434 			maEditKerning.SetLast( maEditKerning.GetMax( maEditKerning.GetUnit() ) );
435 			if(nTmp > maEditKerning.GetMax())
436 				nTmp = maEditKerning.GetMax();
437 			mnCustomKern = -nTmp;
438 			long nVal = LogicToLogic( nTmp, MAP_POINT, (MapUnit)eUnit );
439 			nKern = (short)maEditKerning.Denormalize( nVal );
440             nKern *= - 1;
441 		}
442 		else
443 		{
444 			maEditKerning.SetMax( 9999 );
445 			maEditKerning.SetLast( 9999 );
446 			if(nTmp > maEditKerning.GetMax(FUNIT_TWIP))
447 				nTmp = maEditKerning.GetMax(FUNIT_TWIP);
448 			mnCustomKern = nTmp;
449 			long nVal = LogicToLogic( nTmp, MAP_POINT, (MapUnit)eUnit );
450 			nKern = (short)maEditKerning.Denormalize( nVal );
451 		}
452     }
453 	else
454 	{
455 		mnCustomKern = 0;
456 	}
457 	SvxKerningItem aKernItem(nKern, SID_ATTR_CHAR_KERNING);
458 	mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
459 	mrTextPropertyPanel.SetSpacing(nKern);
460 	return 0;
461 }
462 short  TextCharacterSpacingControl::GetLastCustomState()
463 {
464 	return mnLastCus;
465 }
466 long  TextCharacterSpacingControl::GetLastCustomValue()
467 {
468 	return mnCustomKern;
469 }
470 
471 }} // end of namespace sidebar
472