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 
TextCharacterSpacingControl(Window * pParent,svx::sidebar::TextPropertyPanel & rPanel,SfxBindings * pBindings)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 }
~TextCharacterSpacingControl()77 TextCharacterSpacingControl::~TextCharacterSpacingControl()
78 {
79 	delete[] mpImg;
80 	delete[] mpImgSel;
81 	delete[] mpStr;
82 	delete[] mpStrTip;
83 }
84 
initial()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 }
ToGetFocus()145 void TextCharacterSpacingControl::ToGetFocus()
146 {
147 	if(!mbVS)
148 		maLBKerning.GrabFocus();
149 	else
150 		maVSSpacing.GrabFocus();
151 }
152 
Rearrange(bool bLBAvailable,bool bAvailable,long nKerning)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( xub_Unicode(' ') );
189 			aStrTip.Append(maStrUnit);		// modify
190 			maVSSpacing.SetItemText(6,aStrTip);
191 		}
192 		else if(mnCustomKern < 0)
193 		{
194 			String aStrTip(maStrCusC) ;		//LAST CUSTOM no tip defect //add
195 			aStrTip.Append( String::CreateFromDouble( (double)-mnCustomKern / 10));
196 			aStrTip.Append( xub_Unicode( ' ' ) );
197 			aStrTip.Append(maStrUnit);		// modify
198 			maVSSpacing.SetItemText( 6, aStrTip );
199 		}
200 		else
201 		{
202 			String aStrTip(maStrCusN) ;		//LAST CUSTOM no tip defect //add
203 			maVSSpacing.SetItemText( 6, aStrTip );
204 		}
205 
206 	}
207 
208 	if(bLBAvailable && bAvailable)
209 	{
210 		maLBKerning.Enable();
211 		maFTSpacing.Enable();
212 
213 		SfxMapUnit eUnit = mrTextPropertyPanel.GetSpaceController().GetCoreMetric();
214         MapUnit eOrgUnit = (MapUnit)eUnit;
215         MapUnit ePntUnit( MAP_POINT );
216         long nBig = maEditKerning.Normalize(nKerning);
217         nKerning = LogicToLogic( nBig, eOrgUnit, ePntUnit );
218 
219         if ( nKerning > 0 )
220         {
221             maFTBy.Enable();
222             maEditKerning.Enable();
223             maEditKerning.SetMax( 9999 );
224             maEditKerning.SetLast( 9999 );
225             maEditKerning.SetValue( nKerning );
226             maLBKerning.SelectEntryPos( SIDEBAR_SPACE_EXPAND );
227 			if(nKerning == 30)
228 			{
229 				maVSSpacing.SelectItem(4);
230 			}
231 			else if(nKerning == 60)
232 			{
233 				maVSSpacing.SelectItem(5);
234 			}
235 			else
236 			{
237                 maVSSpacing.SetNoSelection();
238                 maVSSpacing.SelectItem(0);
239 				mbVS = false;
240 			}
241         }
242         else if ( nKerning < 0 )
243         {
244 			maFTBy.Enable();
245             maEditKerning.Enable();
246             maEditKerning.SetValue( -nKerning );
247             maLBKerning.SelectEntryPos( SIDEBAR_SPACE_CONDENSED );
248             long nMax = mrTextPropertyPanel.GetSelFontSize()/6;
249             maEditKerning.SetMax( maEditKerning.Normalize( nMax ), FUNIT_POINT );
250             maEditKerning.SetLast( maEditKerning.GetMax( maEditKerning.GetUnit() ) );
251 			if( nKerning == -30 )
252 			{
253 				maVSSpacing.SelectItem(1);
254 			}
255 			else if( nKerning == -15 )
256 			{
257 				maVSSpacing.SelectItem(2);
258 			}
259 			else
260 			{
261                 maVSSpacing.SetNoSelection();
262                 maVSSpacing.SelectItem(0);
263 				mbVS = false;
264 			}
265         }
266         else
267         {
268 			maVSSpacing.SelectItem(3);
269 			maLBKerning.SelectEntryPos( SIDEBAR_SPACE_NORMAL );
270 			maFTBy.Disable();
271             maEditKerning.Disable();
272             maEditKerning.SetValue( 0 );
273             maEditKerning.SetMax( 9999 );
274             maEditKerning.SetLast( 9999 );
275         }
276 	}
277 	else if(bLBAvailable && !bAvailable)
278 	{
279 		//modified
280         maVSSpacing.SetNoSelection();
281         maVSSpacing.SelectItem(0);
282 		mbVS = false;
283 		maLBKerning.Enable();
284 		maFTSpacing.Enable();
285 		maLBKerning.SetNoSelection();
286 		maEditKerning.SetText(String());
287 		maEditKerning.Disable();
288 		maFTBy.Disable();
289 	}
290 	else
291 	{
292         maVSSpacing.SetNoSelection();
293         maVSSpacing.SelectItem(0);
294 		mbVS = false;
295 		maEditKerning.SetText(String());
296         maLBKerning.SetNoSelection();
297 		maLBKerning.Disable();
298 		maFTSpacing.Disable();
299         maEditKerning.Disable();
300 		maFTBy.Disable();
301 	}
302 	GetFocus();
303 	maVSSpacing.Format();
304 	maVSSpacing.StartSelection();
305 }
IMPL_LINK(TextCharacterSpacingControl,VSSelHdl,void *,pControl)306 IMPL_LINK(TextCharacterSpacingControl, VSSelHdl, void *, pControl)
307 {
308 	mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
309 
310 	if(pControl == &maVSSpacing)
311 	{
312 		sal_uInt16 iPos = maVSSpacing.GetSelectItemId();
313 		short nKern = 0;
314 		SfxMapUnit eUnit = mrTextPropertyPanel.GetSpaceController().GetCoreMetric();
315 		long nVal = 0;
316 		if(iPos == 1)
317 		{
318 			nVal = LogicToLogic(30, MAP_POINT, (MapUnit)eUnit);
319 			nKern = (short)maEditKerning.Denormalize(nVal);
320 			SvxKerningItem aKernItem(-nKern, SID_ATTR_CHAR_KERNING);
321 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
322 			mrTextPropertyPanel.SetSpacing(-nKern);
323 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
324 		}
325 		else if(iPos == 2)
326 		{
327 			nVal = LogicToLogic(15, MAP_POINT, (MapUnit)eUnit);
328 			nKern = (short)maEditKerning.Denormalize(nVal);
329 			SvxKerningItem aKernItem(-nKern, SID_ATTR_CHAR_KERNING);
330 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
331 			mrTextPropertyPanel.SetSpacing(-nKern);
332 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
333 		}
334 		else if(iPos == 3)
335 		{
336 			SvxKerningItem aKernItem(0, SID_ATTR_CHAR_KERNING);
337 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
338 			mrTextPropertyPanel.SetSpacing(0);
339 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
340 		}
341 		else if(iPos == 4)
342 		{
343 			nVal = LogicToLogic(30, MAP_POINT, (MapUnit)eUnit);
344 			nKern = (short)maEditKerning.Denormalize(nVal);
345 			SvxKerningItem aKernItem(nKern, SID_ATTR_CHAR_KERNING);
346 			mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
347 			mrTextPropertyPanel.SetSpacing(nKern);
348 			mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
349 		}
350 		else if(iPos == 5)
351 		{
352 			nVal = LogicToLogic(60, 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 == 6)
360 		{
361 			//modified
362 			if(mbCusEnable)
363 			{
364 				nVal = LogicToLogic(mnCustomKern, MAP_POINT, (MapUnit)eUnit);
365 				nKern = (short)maEditKerning.Denormalize(nVal);
366 				SvxKerningItem aKernItem(nKern , SID_ATTR_CHAR_KERNING);
367 				mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
368 				mrTextPropertyPanel.SetSpacing(nKern);
369 				mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
370 			}
371 			else
372 			{
373 				maVSSpacing.SetNoSelection();		//add , set no selection and keep the last select item
374 				maVSSpacing.Format();
375 				Invalidate();
376 				maVSSpacing.StartSelection();
377 			}
378 			//modify end
379 		}
380 
381 		if(iPos < 6 || (iPos == 6 && mbCusEnable)) //add
382 			mrTextPropertyPanel.EndSpacingPopupMode();
383 	}
384 
385 
386 
387 	return 0;
388 }
389 
IMPL_LINK(TextCharacterSpacingControl,KerningSelectHdl,ListBox *,EMPTYARG)390 IMPL_LINK(TextCharacterSpacingControl, KerningSelectHdl, ListBox*, EMPTYARG)
391 {
392 	if ( maLBKerning.GetSelectEntryPos() > 0 )
393 	{
394 		maFTBy.Enable();
395 		maEditKerning.Enable();
396 	}
397 	else
398 	{
399 		maEditKerning.SetValue( 0 );
400 		maFTBy.Disable();
401 		maEditKerning.Disable();
402 	}
403 
404     if ( maVSSpacing.GetSelectItemId() > 0 )
405 	{
406         maVSSpacing.SetNoSelection();
407         maVSSpacing.SelectItem(0);
408 		maVSSpacing.Format();
409 		Invalidate();
410 		maVSSpacing.StartSelection();
411 	}
412 	KerningModifyHdl( NULL );
413 	return 0;
414 }
IMPL_LINK(TextCharacterSpacingControl,KerningModifyHdl,MetricField *,EMPTYARG)415 IMPL_LINK(TextCharacterSpacingControl, KerningModifyHdl, MetricField*, EMPTYARG)
416 {
417     if ( maVSSpacing.GetSelectItemId() > 0 )
418 	{
419         maVSSpacing.SetNoSelection();
420         maVSSpacing.SelectItem(0);
421 		maVSSpacing.Format();
422 		Invalidate();
423 		maVSSpacing.StartSelection();
424 	}
425 	sal_uInt16 nPos = maLBKerning.GetSelectEntryPos();
426     short nKern = 0;
427     SfxMapUnit eUnit = mrTextPropertyPanel.GetSpaceController().GetCoreMetric();
428 	mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
429 	if ( nPos == SIDEBAR_SPACE_EXPAND || nPos == SIDEBAR_SPACE_CONDENSED )
430     {
431         long nTmp = static_cast<long>(maEditKerning.GetValue());
432         if ( nPos == SIDEBAR_SPACE_CONDENSED )
433 		{
434 			long nMax =  mrTextPropertyPanel.GetSelFontSize()/6;
435 			maEditKerning.SetMax( maEditKerning.Normalize( nMax ), FUNIT_TWIP );
436 			maEditKerning.SetLast( maEditKerning.GetMax( maEditKerning.GetUnit() ) );
437 			if(nTmp > maEditKerning.GetMax())
438 				nTmp = maEditKerning.GetMax();
439 			mnCustomKern = -nTmp;
440 			long nVal = LogicToLogic( nTmp, MAP_POINT, (MapUnit)eUnit );
441 			nKern = (short)maEditKerning.Denormalize( nVal );
442             nKern *= - 1;
443 		}
444 		else
445 		{
446 			maEditKerning.SetMax( 9999 );
447 			maEditKerning.SetLast( 9999 );
448 			if(nTmp > maEditKerning.GetMax(FUNIT_TWIP))
449 				nTmp = maEditKerning.GetMax(FUNIT_TWIP);
450 			mnCustomKern = nTmp;
451 			long nVal = LogicToLogic( nTmp, MAP_POINT, (MapUnit)eUnit );
452 			nKern = (short)maEditKerning.Denormalize( nVal );
453 		}
454     }
455 	else
456 	{
457 		mnCustomKern = 0;
458 	}
459 	SvxKerningItem aKernItem(nKern, SID_ATTR_CHAR_KERNING);
460 	mpBindings->GetDispatcher()->Execute(SID_ATTR_CHAR_KERNING, SFX_CALLMODE_RECORD, &aKernItem, 0L);
461 	mrTextPropertyPanel.SetSpacing(nKern);
462 	return 0;
463 }
GetLastCustomState()464 short  TextCharacterSpacingControl::GetLastCustomState()
465 {
466 	return mnLastCus;
467 }
GetLastCustomValue()468 long  TextCharacterSpacingControl::GetLastCustomValue()
469 {
470 	return mnCustomKern;
471 }
472 
473 }} // end of namespace sidebar
474