masterfix DEV300: #i10000# build fix
[LibreOffice.git] / cui / source / options / fontsubs.cxx
blobe252e5763cf8db04c523507975837b3baa44c5fc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 #include <tools/shl.hxx>
31 #include <svtools/ctrltool.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/wrkwin.hxx>
34 #include <svtools/fontsubstconfig.hxx>
35 #include <unotools/sourceviewconfig.hxx>
36 #include "fontsubs.hrc"
37 #include "fontsubs.hxx"
38 #include <dialmgr.hxx>
39 #include "helpid.hrc"
40 #include <cuires.hrc>
42 /*********************************************************************/
43 /* */
44 /* TabPage Fontersetzung */
45 /* */
46 /*********************************************************************/
48 #define CBCOL_FIRST 0
49 #define CBCOL_SECOND 1
50 #define CBCOL_BOTH 2
52 SvxFontSubstTabPage::SvxFontSubstTabPage( Window* pParent,
53 const SfxItemSet& rSet ) :
54 SfxTabPage(pParent, CUI_RES(RID_SVX_FONT_SUBSTITUTION), rSet),
55 aUseTableCB (this, CUI_RES(CB_USETABLE)),
56 aFont1FT (this, CUI_RES(FT_FONT1)),
57 aFont1CB (this, CUI_RES(CB_FONT1)),
58 aFont2FT (this, CUI_RES(FT_FONT2)),
59 aFont2CB (this, CUI_RES(CB_FONT2)),
60 aNewDelTBX (this, CUI_RES(TBX_SUBSTNEWDEL)),
61 aCheckLB (this, CUI_RES(CLB_SUBSTITUTES)),
63 aSourceViewFontsFL (this, CUI_RES(FL_SOURCEVIEW )),
64 aFontNameFT (this, CUI_RES(FT_FONTNAME )),
65 aFontNameLB (this, CUI_RES(LB_FONTNAME )),
66 aNonPropFontsOnlyCB(this, CUI_RES(CB_NONPROP )),
67 aFontHeightFT (this, CUI_RES(FT_FONTHEIGHT )),
68 aFontHeightLB (this, CUI_RES(LB_FONTHEIGHT )),
70 aImageList (CUI_RES(IL_ICON)),
72 sAutomatic (CUI_RES( STR_AUTOMATIC )),
73 pConfig(new SvtFontSubstConfig),
74 pSourceViewConfig(new utl::SourceViewConfig),
76 sHeader1 (CUI_RES( STR_HEADER1 )),
77 sHeader2 (CUI_RES( STR_HEADER2 )),
78 sHeader3 (CUI_RES( STR_HEADER3 )),
79 sHeader4 (CUI_RES( STR_HEADER4 )),
81 sFontGroup ("FontSubstitution"),
82 pCheckButtonData(0)
84 FreeResource();
86 aTextColor = aCheckLB.GetTextColor();
88 for(sal_uInt16 k = 0; k < aNewDelTBX.GetItemCount(); k++)
89 aNewDelTBX.SetItemImage(aNewDelTBX.GetItemId(k),
90 aImageList.GetImage(aNewDelTBX.GetItemId(k)));
92 aNewDelTBX.SetSizePixel( aNewDelTBX.CalcWindowSizePixel() );
94 long nDelta = ( aFont1CB.GetSizePixel().Height() -
95 aNewDelTBX.GetSizePixel().Height() ) / 2;
96 Point aNewPnt = aNewDelTBX.GetPosPixel();
97 aNewPnt.Y() += nDelta;
98 aNewDelTBX.SetPosPixel( aNewPnt );
100 aCheckLB.SetHelpId(HID_OFA_FONT_SUBST_CLB);
101 aCheckLB.SetStyle(aCheckLB.GetStyle()|WB_HSCROLL|WB_VSCROLL);
102 aCheckLB.SetSelectionMode(MULTIPLE_SELECTION);
103 aCheckLB.SortByCol(2);
105 Link aLink(LINK(this, SvxFontSubstTabPage, SelectHdl));
107 aCheckLB.SetSelectHdl(aLink);
108 aUseTableCB.SetClickHdl(aLink);
109 aFont1CB.SetSelectHdl(aLink);
110 aFont1CB.SetModifyHdl(aLink);
111 aFont2CB.SetSelectHdl(aLink);
112 aFont2CB.SetModifyHdl(aLink);
113 aNewDelTBX.SetClickHdl(aLink);
115 aNonPropFontsOnlyCB.SetClickHdl(LINK(this, SvxFontSubstTabPage, NonPropFontsHdl));
117 static long aStaticTabs[] = { 4, 0, 0, 0, 0 };
118 long nW1 = GetTextWidth( sHeader1 );
119 long nW2 = GetTextWidth( sHeader2 );
120 long nMax = Max( nW1, nW2 ) + 6; // width of the longest header + a little offset
121 long nMin = aFontNameFT.LogicToPixel( Size( 30, 0 ), MAP_APPFONT ).Width();
122 nMax = Max( nMax, nMin );
123 const long nDoubleMax = 2*nMax;
124 const long nRest = aCheckLB.GetSizePixel().Width() - nDoubleMax;
125 aStaticTabs[2] = nMax;
126 aStaticTabs[3] = nDoubleMax;
127 aStaticTabs[4] = nDoubleMax + nRest/2;
128 aCheckLB.SvxSimpleTable::SetTabs( aStaticTabs, MAP_PIXEL );
130 String sHeader(sHeader1);
131 String sTab(String::CreateFromAscii("\t"));
132 String sTabSpace(String::CreateFromAscii("\t "));
133 sHeader += sTab;
134 sHeader += sHeader2;
135 sHeader += sTabSpace;
136 sHeader += sHeader3;
137 sHeader += sTabSpace;
138 sHeader += sHeader4;
139 // sHeader += sTabSpace;
140 aCheckLB.InsertHeaderEntry(sHeader);
142 HeaderBar* pBar = aCheckLB.GetTheHeaderBar();
144 HeaderBarItemBits nBits = pBar->GetItemBits(1) | HIB_FIXEDPOS | HIB_FIXED;
145 nBits &= ~HIB_CLICKABLE;
146 pBar->SetItemBits(1, nBits);
147 pBar->SetItemBits(2, nBits);
149 sal_uInt16 nHeight;
150 for(nHeight = 6; nHeight <= 16; nHeight++)
151 aFontHeightLB.InsertEntry(String::CreateFromInt32(nHeight));
152 for(nHeight = 18; nHeight <= 28; nHeight+= 2)
153 aFontHeightLB.InsertEntry(String::CreateFromInt32(nHeight));
154 for(nHeight = 32; nHeight <= 48; nHeight+= 4)
155 aFontHeightLB.InsertEntry(String::CreateFromInt32(nHeight));
156 for(nHeight = 54; nHeight <= 72; nHeight+= 6)
157 aFontHeightLB.InsertEntry(String::CreateFromInt32(nHeight));
158 for(nHeight = 80; nHeight <= 96; nHeight+= 8)
159 aFontHeightLB.InsertEntry(String::CreateFromInt32(nHeight));
162 /*********************************************************************/
163 /* */
164 /*********************************************************************/
166 SvLBoxEntry* SvxFontSubstTabPage::CreateEntry(String& rFont1, String& rFont2)
168 SvLBoxEntry* pEntry = new SvLBoxEntry;
170 if( !pCheckButtonData )
171 pCheckButtonData = new SvLBoxButtonData( &aCheckLB );
173 pEntry->AddItem( new SvLBoxContextBmp( pEntry, 0, Image(), Image(), 0)); // Sonst Puff!
175 pEntry->AddItem( new SvLBoxButton( pEntry,
176 SvLBoxButtonKind_enabledCheckbox, 0,
177 pCheckButtonData ) );
178 pEntry->AddItem( new SvLBoxButton( pEntry,
179 SvLBoxButtonKind_enabledCheckbox, 0,
180 pCheckButtonData ) );
182 pEntry->AddItem( new SvLBoxString( pEntry, 0, rFont1 ) );
183 pEntry->AddItem( new SvLBoxString( pEntry, 0, rFont2 ) );
185 return pEntry;
188 /*********************************************************************/
189 /* */
190 /*********************************************************************/
193 SvxFontSubstTabPage::~SvxFontSubstTabPage()
195 delete pCheckButtonData;
196 delete pConfig;
197 delete pSourceViewConfig;
200 /*********************************************************************/
201 /* */
202 /*********************************************************************/
204 SfxTabPage* SvxFontSubstTabPage::Create( Window* pParent,
205 const SfxItemSet& rAttrSet)
207 return new SvxFontSubstTabPage(pParent, rAttrSet);
210 /*********************************************************************/
211 /* */
212 /*********************************************************************/
214 sal_Bool SvxFontSubstTabPage::FillItemSet( SfxItemSet& )
216 pConfig->ClearSubstitutions();// remove all entries
218 pConfig->Enable(aUseTableCB.IsChecked());
220 SvLBoxEntry* pEntry = aCheckLB.First();
222 while (pEntry)
224 SubstitutionStruct aAdd;
225 aAdd.sFont = aCheckLB.GetEntryText(pEntry, 0);
226 aAdd.sReplaceBy = aCheckLB.GetEntryText(pEntry, 1);
227 aAdd.bReplaceAlways = aCheckLB.IsChecked(pEntry, 0);
228 aAdd.bReplaceOnScreenOnly = aCheckLB.IsChecked(pEntry, 1);
229 pConfig->AddSubstitution(aAdd);
230 pEntry = aCheckLB.Next(pEntry);
232 if(pConfig->IsModified())
233 pConfig->Commit();
234 pConfig->Apply();
235 if(aFontHeightLB.GetSavedValue() != aFontHeightLB.GetSelectEntryPos())
236 pSourceViewConfig->SetFontHeight((sal_Int16)aFontHeightLB.GetSelectEntry().ToInt32());
237 if(aNonPropFontsOnlyCB.GetSavedValue() != aNonPropFontsOnlyCB.IsChecked())
238 pSourceViewConfig->SetShowProportionalFontsOnly(aNonPropFontsOnlyCB.IsChecked());
239 //font name changes cannot be detected by saved values
240 String sFontName;
241 if(aFontNameLB.GetSelectEntryPos())
242 sFontName = aFontNameLB.GetSelectEntry();
243 pSourceViewConfig->SetFontName(sFontName);
245 return sal_False;
248 /*********************************************************************/
249 /* */
250 /*********************************************************************/
253 void SvxFontSubstTabPage::Reset( const SfxItemSet& )
255 aCheckLB.SetUpdateMode(sal_False);
256 aCheckLB.Clear();
258 FontList aFntLst( Application::GetDefaultDevice() );
259 aFont1CB.Fill( &aFntLst );
260 aFont2CB.Fill( &aFntLst );
262 sal_Int32 nCount = pConfig->SubstitutionCount();
263 if (nCount)
264 aUseTableCB.Check(pConfig->IsEnabled());
266 for (sal_Int32 i = 0; i < nCount; i++)
268 const SubstitutionStruct* pSubs = pConfig->GetSubstitution(i);
269 String aTmpStr1(pSubs->sFont);
270 String aTmpStr2(pSubs->sReplaceBy);
271 SvLBoxEntry* pEntry = CreateEntry(aTmpStr1, aTmpStr2);
272 aCheckLB.Insert(pEntry);
273 aCheckLB.CheckEntry(pEntry, 0, pSubs->bReplaceAlways);
274 aCheckLB.CheckEntry(pEntry, 1, pSubs->bReplaceOnScreenOnly);
277 CheckEnable();
278 aCheckLB.SetUpdateMode(sal_True);
280 //fill font name box first
281 aNonPropFontsOnlyCB.Check(pSourceViewConfig->IsShowProportionalFontsOnly());
282 NonPropFontsHdl(&aNonPropFontsOnlyCB);
283 String sFontName(pSourceViewConfig->GetFontName());
284 if(sFontName.Len())
285 aFontNameLB.SelectEntry(sFontName);
286 else
287 aFontNameLB.SelectEntryPos(0);
288 short nFontHeight = pSourceViewConfig->GetFontHeight();
289 aFontHeightLB.SelectEntry(String::CreateFromInt32(nFontHeight));
290 aNonPropFontsOnlyCB.SaveValue();
291 aFontHeightLB.SaveValue();
294 /*********************************************************************/
295 /* */
296 /*********************************************************************/
298 IMPL_LINK(SvxFontSubstTabPage, SelectHdl, Window*, pWin)
300 if (pWin == &aNewDelTBX)
302 SvLBoxEntry* pEntry;
303 // nCol ist behaemmerterweise die nCol'te Textspalte, werden nicht gezaehlt!
304 // Daher als Spalte "0".
305 sal_uLong nPos = aCheckLB.GetEntryPos(aFont1CB.GetText(), 0);
307 switch (aNewDelTBX.GetCurItemId())
309 case BT_SUBSTAPPLY:
311 if (nPos != 0xffffffff)
313 // Eintrag aendern
314 aCheckLB.SetEntryText(aFont2CB.GetText(), nPos, 1);
315 pEntry = aCheckLB.GetEntry(nPos);
317 else
319 // Neuer Eintrag
320 String sFont1 = aFont1CB.GetText();
321 String sFont2 = aFont2CB.GetText();
323 pEntry = CreateEntry(sFont1, sFont2);
324 aCheckLB.Insert(pEntry);
326 aCheckLB.SelectAll(sal_False);
327 aCheckLB.Select(pEntry);
329 break;
331 case BT_SUBSTDELETE:
333 if (nPos != 0xffffffff)
335 pEntry = aCheckLB.FirstSelected();
336 while (pEntry)
338 SvLBoxEntry* pDelEntry = pEntry;
339 pEntry = aCheckLB.NextSelected(pEntry);
340 aCheckLB.RemoveEntry(pDelEntry);
344 break;
348 if (pWin == &aCheckLB)
350 SvLBoxEntry* pEntry = aCheckLB.FirstSelected();
352 if (aCheckLB.NextSelected(pEntry) == 0)
354 aFont1CB.SetText(aCheckLB.GetEntryText(pEntry, 0));
355 aFont2CB.SetText(aCheckLB.GetEntryText(pEntry, 1));
359 if (pWin == &aFont1CB)
361 sal_uLong nPos = aCheckLB.GetEntryPos(aFont1CB.GetText(), 0);
363 if (nPos != 0xffffffff)
365 SvLBoxEntry* pEntry = aCheckLB.GetEntry(nPos);
367 if (pEntry != aCheckLB.FirstSelected())
369 aCheckLB.SelectAll(sal_False);
370 aCheckLB.Select(pEntry);
375 CheckEnable();
377 return 0;
379 /* -----------------------------29.08.2002 11:47------------------------------
381 ---------------------------------------------------------------------------*/
382 IMPL_LINK(SvxFontSubstTabPage, NonPropFontsHdl, CheckBox*, pBox)
384 String sFontName = aFontNameLB.GetSelectEntry();
385 sal_Bool bNonPropOnly = pBox->IsChecked();
386 aFontNameLB.Clear();
387 FontList aFntLst( Application::GetDefaultDevice() );
388 aFontNameLB.InsertEntry(sAutomatic);
389 sal_uInt16 nFontCount = aFntLst.GetFontNameCount();
390 for(sal_uInt16 nFont = 0; nFont < nFontCount; nFont++)
392 const FontInfo& rInfo = aFntLst.GetFontName( nFont );
393 if(!bNonPropOnly || rInfo.GetPitch() == PITCH_FIXED)
394 aFontNameLB.InsertEntry(rInfo.GetName());
396 aFontNameLB.SelectEntry(sFontName);
397 return 0;
399 /*********************************************************************/
400 /* */
401 /*********************************************************************/
403 void SvxFontSubstTabPage::CheckEnable()
405 sal_Bool bEnableAll = aUseTableCB.IsChecked();
407 if (bEnableAll)
409 sal_Bool bApply, bDelete;
411 SvLBoxEntry* pEntry = aCheckLB.FirstSelected();
413 String sEntry = aFont1CB.GetText();
414 sEntry += '\t';
415 sEntry += aFont2CB.GetText();
417 /* if (!aFont1CB.GetText().Len() || !aFont2CB.GetText().Len() ||
418 aFont1CB.GetText() == aFont2CB.GetText() ||
419 aCheckLB.GetEntryPos(sEntry) != 0xffffffff ||
420 (pEntry != 0 && aCheckLB.NextSelected(pEntry) != 0))
421 bApply = sal_False;
422 else
423 bApply = sal_True;*/
425 // Wegen OS/2-Optimierungsfehler (Bug #56267) etwas umstaendlicher:
426 if (!aFont1CB.GetText().Len() || !aFont2CB.GetText().Len())
427 bApply = sal_False;
428 else if(aFont1CB.GetText() == aFont2CB.GetText())
429 bApply = sal_False;
430 else if(aCheckLB.GetEntryPos(sEntry) != 0xffffffff)
431 bApply = sal_False;
432 else if(pEntry != 0 && aCheckLB.NextSelected(pEntry) != 0)
433 bApply = sal_False;
434 else
435 bApply = sal_True;
437 bDelete = pEntry != 0;
439 aNewDelTBX.EnableItem(BT_SUBSTAPPLY, bApply);
440 aNewDelTBX.EnableItem(BT_SUBSTDELETE, bDelete);
443 if (bEnableAll)
445 if (!aCheckLB.IsEnabled())
447 aCheckLB.EnableTable();
448 aCheckLB.SetTextColor(aTextColor);
449 aCheckLB.Invalidate();
450 SelectHdl(&aFont1CB);
453 else
455 if (aCheckLB.IsEnabled())
457 aCheckLB.DisableTable();
458 aCheckLB.SetTextColor(Color(COL_GRAY));
459 aCheckLB.Invalidate();
460 aCheckLB.SelectAll(sal_False);
463 aNewDelTBX.Enable(bEnableAll);
464 aFont1FT.Enable(bEnableAll);
465 aFont1CB.Enable(bEnableAll);
466 aFont2FT.Enable(bEnableAll);
467 aFont2CB.Enable(bEnableAll);
470 /*********************************************************************/
471 /* */
472 /*********************************************************************/
474 void SvxFontSubstCheckListBox::SetTabs()
476 SvxSimpleTable::SetTabs();
477 sal_uInt16 nAdjust = SV_LBOXTAB_ADJUST_RIGHT|SV_LBOXTAB_ADJUST_LEFT|SV_LBOXTAB_ADJUST_CENTER|SV_LBOXTAB_ADJUST_NUMERIC|SV_LBOXTAB_FORCE;
479 SvLBoxTab* pTab = (SvLBoxTab*)aTabs.GetObject(1);
480 pTab->nFlags &= ~nAdjust;
481 pTab->nFlags |= SV_LBOXTAB_PUSHABLE|SV_LBOXTAB_ADJUST_CENTER|SV_LBOXTAB_FORCE;
483 pTab = (SvLBoxTab*)aTabs.GetObject(2);
484 pTab->nFlags &= ~nAdjust;
485 pTab->nFlags |= SV_LBOXTAB_PUSHABLE|SV_LBOXTAB_ADJUST_CENTER|SV_LBOXTAB_FORCE;
487 /* -----------------------------22.05.2002 11:06------------------------------
489 ---------------------------------------------------------------------------*/
490 void SvxFontSubstCheckListBox::KeyInput( const KeyEvent& rKEvt )
492 if(!rKEvt.GetKeyCode().GetModifier() &&
493 KEY_SPACE == rKEvt.GetKeyCode().GetCode())
495 sal_uLong nSelPos = GetModel()->GetAbsPos(GetCurEntry());
496 sal_uInt16 nCol = GetCurrentTabPos() - 1;
497 if ( nCol < 2 )
499 CheckEntryPos( nSelPos, nCol, !IsChecked( nSelPos, nCol ) );
500 CallImplEventListeners( VCLEVENT_CHECKBOX_TOGGLE, (void*)GetEntry( nSelPos ) );
502 else
504 sal_uInt16 nCheck = IsChecked(nSelPos, 1) ? 1 : 0;
505 if(IsChecked(nSelPos, 0))
506 nCheck += 2;
507 nCheck--;
508 nCheck &= 3;
509 CheckEntryPos(nSelPos, 1, 0 != (nCheck & 1));
510 CheckEntryPos(nSelPos, 0, 0 != (nCheck & 2));
513 else
514 SvxSimpleTable::KeyInput(rKEvt);
517 /*********************************************************************/
518 /* */
519 /*********************************************************************/
521 void SvxFontSubstCheckListBox::CheckEntryPos(sal_uLong nPos, sal_uInt16 nCol, sal_Bool bChecked)
523 if ( nPos < GetEntryCount() )
524 SetCheckButtonState(
525 GetEntry(nPos),
526 nCol,
527 bChecked ? SvButtonState( SV_BUTTON_CHECKED ) :
528 SvButtonState( SV_BUTTON_UNCHECKED ) );
531 /*********************************************************************/
532 /* */
533 /*********************************************************************/
535 void SvxFontSubstCheckListBox::CheckEntry(SvLBoxEntry* pEntry, sal_uInt16 nCol, sal_Bool bChecked)
537 if ( pEntry )
538 SetCheckButtonState(
539 pEntry,
540 nCol,
541 bChecked ? SvButtonState( SV_BUTTON_CHECKED ) :
542 SvButtonState( SV_BUTTON_UNCHECKED ) );
545 /*********************************************************************/
546 /* */
547 /*********************************************************************/
549 sal_Bool SvxFontSubstCheckListBox::IsChecked(sal_uLong nPos, sal_uInt16 nCol)
551 return GetCheckButtonState( GetEntry(nPos), nCol ) == SV_BUTTON_CHECKED;
554 /*********************************************************************/
555 /* */
556 /*********************************************************************/
558 sal_Bool SvxFontSubstCheckListBox::IsChecked(SvLBoxEntry* pEntry, sal_uInt16 nCol)
560 return GetCheckButtonState( pEntry, nCol ) == SV_BUTTON_CHECKED;
563 /*********************************************************************/
564 /* */
565 /*********************************************************************/
567 void SvxFontSubstCheckListBox::SetCheckButtonState( SvLBoxEntry* pEntry, sal_uInt16 nCol, SvButtonState eState)
569 SvLBoxButton* pItem = (SvLBoxButton*)(pEntry->GetItem(nCol + 1));
571 DBG_ASSERT(pItem,"SetCheckButton:Item not found");
572 if (((SvLBoxItem*)pItem)->IsA() == SV_ITEM_ID_LBOXBUTTON)
574 switch( eState )
576 case SV_BUTTON_CHECKED:
577 pItem->SetStateChecked();
578 break;
580 case SV_BUTTON_UNCHECKED:
581 pItem->SetStateUnchecked();
582 break;
584 case SV_BUTTON_TRISTATE:
585 pItem->SetStateTristate();
586 break;
588 InvalidateEntry( pEntry );
592 /*********************************************************************/
593 /* */
594 /*********************************************************************/
596 SvButtonState SvxFontSubstCheckListBox::GetCheckButtonState( SvLBoxEntry* pEntry, sal_uInt16 nCol ) const
598 SvButtonState eState = SV_BUTTON_UNCHECKED;
599 SvLBoxButton* pItem = (SvLBoxButton*)(pEntry->GetItem(nCol + 1));
600 DBG_ASSERT(pItem,"GetChButnState:Item not found");
602 if (((SvLBoxItem*)pItem)->IsA() == SV_ITEM_ID_LBOXBUTTON)
604 sal_uInt16 nButtonFlags = pItem->GetButtonFlags();
605 eState = pCheckButtonData->ConvertToButtonState( nButtonFlags );
608 return eState;