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