Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / miscdlgs / scuiautofmt.cxx
blob7a2fbbd0cd45ac25cd5a3e6c3c6c8a1c67d3a79d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #undef SC_DLLIMPLEMENTATION
25 //------------------------------------------------------------------
27 #include "scitems.hxx"
28 #include <svx/algitem.hxx>
29 #include <editeng/boxitem.hxx>
30 #include <editeng/brushitem.hxx>
31 #include <editeng/contouritem.hxx>
32 #include <editeng/colritem.hxx>
33 #include <editeng/crossedoutitem.hxx>
34 #include <editeng/fontitem.hxx>
35 #include <editeng/postitem.hxx>
36 #include <editeng/shdditem.hxx>
37 #include <editeng/udlnitem.hxx>
38 #include <editeng/wghtitem.hxx>
39 #include <svl/zforlist.hxx>
40 #include <vcl/msgbox.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <sfx2/sfxresid.hxx>
43 #include "sc.hrc"
44 #include "scmod.hxx"
45 #include "attrib.hxx"
46 #include "zforauto.hxx"
47 #include "global.hxx"
48 #include "globstr.hrc"
49 #include "autoform.hxx"
50 #include "strindlg.hxx"
51 #include "miscdlgs.hrc"
52 #include "scuiautofmt.hxx"
53 #include "scresid.hxx"
54 #include "document.hxx"
56 //========================================================================
57 // AutoFormat-Dialog:
59 ScAutoFormatDlg::ScAutoFormatDlg( Window* pParent,
60 ScAutoFormat* pAutoFormat,
61 const ScAutoFormatData* pSelFormatData,
62 ScDocument* pDoc ) :
64 ModalDialog ( pParent, ScResId( RID_SCDLG_AUTOFORMAT ) ),
66 aFlFormat ( this, ScResId( FL_FORMAT ) ),
67 aLbFormat ( this, ScResId( LB_FORMAT ) ),
68 pWndPreview ( new ScAutoFmtPreview( this, ScResId( WND_PREVIEW ), pDoc ) ),
69 aBtnOk ( this, ScResId( BTN_OK ) ),
70 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
71 aBtnHelp ( this, ScResId( BTN_HELP ) ),
72 aBtnAdd ( this, ScResId( BTN_ADD ) ),
73 aBtnRemove ( this, ScResId( BTN_REMOVE ) ),
74 aBtnMore ( this, ScResId( BTN_MORE ) ),
75 aFlFormatting ( this, ScResId( FL_FORMATTING ) ),
76 aBtnNumFormat ( this, ScResId( BTN_NUMFORMAT ) ),
77 aBtnBorder ( this, ScResId( BTN_BORDER ) ),
78 aBtnFont ( this, ScResId( BTN_FONT ) ),
79 aBtnPattern ( this, ScResId( BTN_PATTERN ) ),
80 aBtnAlignment ( this, ScResId( BTN_ALIGNMENT ) ),
81 aBtnAdjust ( this, ScResId( BTN_ADJUST ) ),
82 aBtnRename ( this, ScResId( BTN_RENAME ) ),
83 aStrTitle ( ScResId( STR_ADD_TITLE ) ),
84 aStrLabel ( ScResId( STR_ADD_LABEL ) ),
85 aStrClose ( ScResId( STR_BTN_CLOSE ) ),
86 aStrDelTitle ( ScResId( STR_DEL_TITLE ) ),
87 aStrDelMsg ( ScResId( STR_DEL_MSG ) ) ,
88 aStrRename ( ScResId( STR_RENAME_TITLE ) ),
90 pFormat ( pAutoFormat ),
91 pSelFmtData ( pSelFormatData ),
92 nIndex ( 0 ),
93 bCoreDataChanged( false ),
94 bFmtInserted ( false )
96 Init();
97 ScAutoFormat::iterator it = pFormat->begin();
98 pWndPreview->NotifyChange(it->second);
99 FreeResource();
102 //------------------------------------------------------------------------
104 ScAutoFormatDlg::~ScAutoFormatDlg()
106 delete pWndPreview;
109 //------------------------------------------------------------------------
111 void ScAutoFormatDlg::Init()
113 aLbFormat .SetSelectHdl( LINK( this, ScAutoFormatDlg, SelFmtHdl ) );
114 aBtnNumFormat.SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
115 aBtnBorder .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
116 aBtnFont .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
117 aBtnPattern .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
118 aBtnAlignment.SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
119 aBtnAdjust .SetClickHdl ( LINK( this, ScAutoFormatDlg, CheckHdl ) );
120 aBtnAdd .SetClickHdl ( LINK( this, ScAutoFormatDlg, AddHdl ) );
121 aBtnRemove .SetClickHdl ( LINK( this, ScAutoFormatDlg, RemoveHdl ) );
122 aBtnOk .SetClickHdl ( LINK( this, ScAutoFormatDlg, CloseHdl ) );
123 aBtnCancel .SetClickHdl ( LINK( this, ScAutoFormatDlg, CloseHdl ) );
124 aBtnRename .SetClickHdl ( LINK( this, ScAutoFormatDlg, RenameHdl ) );
125 aLbFormat .SetDoubleClickHdl( LINK( this, ScAutoFormatDlg, DblClkHdl ) );
127 aBtnMore.AddWindow( &aBtnRename );
128 aBtnMore.AddWindow( &aBtnNumFormat );
129 aBtnMore.AddWindow( &aBtnBorder );
130 aBtnMore.AddWindow( &aBtnFont );
131 aBtnMore.AddWindow( &aBtnPattern );
132 aBtnMore.AddWindow( &aBtnAlignment );
133 aBtnMore.AddWindow( &aBtnAdjust );
134 aBtnMore.AddWindow( &aFlFormatting );
136 ScAutoFormat::const_iterator it = pFormat->begin(), itEnd = pFormat->end();
137 for (; it != itEnd; ++it)
138 aLbFormat.InsertEntry(it->second->GetName());
140 if (pFormat->size() == 1)
141 aBtnRemove.Disable();
143 aLbFormat.SelectEntryPos( 0 );
144 aBtnRename.Disable();
145 aBtnRemove.Disable();
147 nIndex = 0;
148 UpdateChecks();
150 if ( !pSelFmtData )
152 aBtnAdd.Disable();
153 aBtnRemove.Disable();
154 bFmtInserted = true;
158 //------------------------------------------------------------------------
160 void ScAutoFormatDlg::UpdateChecks()
162 const ScAutoFormatData* pData = pFormat->findByIndex(nIndex);
164 aBtnNumFormat.Check( pData->GetIncludeValueFormat() );
165 aBtnBorder .Check( pData->GetIncludeFrame() );
166 aBtnFont .Check( pData->GetIncludeFont() );
167 aBtnPattern .Check( pData->GetIncludeBackground() );
168 aBtnAlignment.Check( pData->GetIncludeJustify() );
169 aBtnAdjust .Check( pData->GetIncludeWidthHeight() );
172 //------------------------------------------------------------------------
173 // Handler:
174 //---------
176 IMPL_LINK( ScAutoFormatDlg, CloseHdl, PushButton *, pBtn )
178 if ( pBtn == &aBtnOk || pBtn == &aBtnCancel )
180 if ( bCoreDataChanged )
181 ScGlobal::GetOrCreateAutoFormat()->Save();
183 EndDialog( (pBtn == &aBtnOk) ? RET_OK : RET_CANCEL );
185 return 0;
188 //------------------------------------------------------------------------
190 IMPL_LINK_NOARG_INLINE_START(ScAutoFormatDlg, DblClkHdl)
192 if ( bCoreDataChanged )
193 ScGlobal::GetOrCreateAutoFormat()->Save();
195 EndDialog( RET_OK );
196 return 0;
198 IMPL_LINK_NOARG_INLINE_END(ScAutoFormatDlg, DblClkHdl)
200 //------------------------------------------------------------------------
202 IMPL_LINK( ScAutoFormatDlg, CheckHdl, Button *, pBtn )
204 ScAutoFormatData* pData = pFormat->findByIndex(nIndex);
205 bool bCheck = ((CheckBox*)pBtn)->IsChecked();
207 if ( pBtn == &aBtnNumFormat )
208 pData->SetIncludeValueFormat( bCheck );
209 else if ( pBtn == &aBtnBorder )
210 pData->SetIncludeFrame( bCheck );
211 else if ( pBtn == &aBtnFont )
212 pData->SetIncludeFont( bCheck );
213 else if ( pBtn == &aBtnPattern )
214 pData->SetIncludeBackground( bCheck );
215 else if ( pBtn == &aBtnAlignment )
216 pData->SetIncludeJustify( bCheck );
217 else if ( pBtn == &aBtnAdjust )
218 pData->SetIncludeWidthHeight( bCheck );
220 if ( !bCoreDataChanged )
222 aBtnCancel.SetText( aStrClose );
223 bCoreDataChanged = true;
226 pWndPreview->NotifyChange( pData );
228 return 0;
231 //------------------------------------------------------------------------
233 IMPL_LINK_NOARG(ScAutoFormatDlg, AddHdl)
235 if ( !bFmtInserted && pSelFmtData )
237 OUString aStrStandard( SfxResId(STR_STANDARD) );
238 OUString aFormatName;
239 ScStringInputDlg* pDlg;
240 bool bOk = false;
242 while ( !bOk )
244 pDlg = new ScStringInputDlg( this,
245 aStrTitle,
246 aStrLabel,
247 aFormatName,
248 HID_SC_ADD_AUTOFMT, HID_SC_AUTOFMT_NAME );
250 if ( pDlg->Execute() == RET_OK )
252 aFormatName = pDlg->GetInputString();
254 if ( !aFormatName.isEmpty() && !aFormatName.equals(aStrStandard) )
256 ScAutoFormatData* pNewData
257 = new ScAutoFormatData( *pSelFmtData );
259 pNewData->SetName( aFormatName );
260 bFmtInserted = pFormat->insert(pNewData);
262 if ( bFmtInserted )
264 ScAutoFormat::const_iterator it = pFormat->find(pNewData);
265 ScAutoFormat::const_iterator itBeg = pFormat->begin();
266 size_t nPos = std::distance(itBeg, it);
267 aLbFormat.InsertEntry(aFormatName, nPos);
268 aLbFormat.SelectEntry( aFormatName );
269 aBtnAdd.Disable();
271 if ( !bCoreDataChanged )
273 aBtnCancel.SetText( aStrClose );
274 bCoreDataChanged = true;
277 SelFmtHdl( 0 );
278 bOk = sal_True;
280 else
281 delete pNewData;
285 if ( !bFmtInserted )
287 sal_uInt16 nRet = ErrorBox( this,
288 WinBits( WB_OK_CANCEL | WB_DEF_OK),
289 ScGlobal::GetRscString(STR_INVALID_AFNAME)
290 ).Execute();
292 bOk = ( nRet == RET_CANCEL );
295 else
296 bOk = sal_True;
298 delete pDlg;
302 return 0;
305 //------------------------------------------------------------------------
307 IMPL_LINK_NOARG(ScAutoFormatDlg, RemoveHdl)
309 if ( (nIndex > 0) && (aLbFormat.GetEntryCount() > 0) )
311 OUString aMsg( aStrDelMsg.getToken( 0, '#' ) );
313 aMsg += aLbFormat.GetSelectEntry();
314 aMsg += aStrDelMsg.getToken( 1, '#' );
316 if ( RET_YES ==
317 QueryBox( this, WinBits( WB_YES_NO | WB_DEF_YES ), aMsg ).Execute() )
319 aLbFormat.RemoveEntry( nIndex );
320 aLbFormat.SelectEntryPos( nIndex-1 );
322 if ( nIndex-1 == 0 )
323 aBtnRemove.Disable();
325 if ( !bCoreDataChanged )
327 aBtnCancel.SetText( aStrClose );
328 bCoreDataChanged = true;
331 ScAutoFormat::iterator it = pFormat->begin();
332 std::advance(it, nIndex);
333 pFormat->erase(it);
334 nIndex--;
336 SelFmtHdl( 0 );
340 SelFmtHdl( 0 );
342 return 0;
345 IMPL_LINK_NOARG(ScAutoFormatDlg, RenameHdl)
347 sal_Bool bOk = false;
348 while( !bOk )
351 OUString aFormatName = aLbFormat.GetSelectEntry();
352 OUString aEntry;
354 ScStringInputDlg* pDlg = new ScStringInputDlg( this,
355 aStrRename,
356 aStrLabel,
357 aFormatName,
358 HID_SC_REN_AFMT_DLG, HID_SC_REN_AFMT_NAME );
359 if( pDlg->Execute() == RET_OK )
361 bool bFmtRenamed = false;
362 aFormatName = pDlg->GetInputString();
364 if (!aFormatName.isEmpty())
366 ScAutoFormat::iterator it = pFormat->begin(), itEnd = pFormat->end();
367 for (; it != itEnd; ++it)
369 aEntry = it->second->GetName();
370 if (aFormatName.equals(aEntry))
371 break;
373 if (it == itEnd)
375 // Format mit dem Namen noch nicht vorhanden, also
376 // umbenennen
378 aLbFormat.RemoveEntry(nIndex );
379 const ScAutoFormatData* p = pFormat->findByIndex(nIndex);
380 ScAutoFormatData* pNewData
381 = new ScAutoFormatData(*p);
383 it = pFormat->begin();
384 std::advance(it, nIndex);
385 pFormat->erase(it);
387 pNewData->SetName( aFormatName );
389 pFormat->insert(pNewData);
391 aLbFormat.SetUpdateMode(false);
392 aLbFormat.Clear();
393 for (it = pFormat->begin(); it != itEnd; ++it)
395 aEntry = it->second->GetName();
396 aLbFormat.InsertEntry( aEntry );
399 aLbFormat.SetUpdateMode(true);
400 aLbFormat.SelectEntry( aFormatName);
402 if ( !bCoreDataChanged )
404 aBtnCancel.SetText( aStrClose );
405 bCoreDataChanged = true;
409 SelFmtHdl( 0 );
410 bOk = true;
411 bFmtRenamed = true;
414 if( !bFmtRenamed )
416 bOk = RET_CANCEL == ErrorBox( this,
417 WinBits( WB_OK_CANCEL | WB_DEF_OK),
418 ScGlobal::GetRscString(STR_INVALID_AFNAME)
419 ).Execute();
422 else
423 bOk = true;
424 delete pDlg;
427 return 0;
430 //------------------------------------------------------------------------
432 IMPL_LINK_NOARG(ScAutoFormatDlg, SelFmtHdl)
434 nIndex = aLbFormat.GetSelectEntryPos();
435 UpdateChecks();
437 if ( nIndex == 0 )
439 aBtnRename.Disable();
440 aBtnRemove.Disable();
442 else
444 aBtnRename.Enable();
445 aBtnRemove.Enable();
448 ScAutoFormatData* p = pFormat->findByIndex(nIndex);
449 pWndPreview->NotifyChange(p);
451 return 0;
454 //------------------------------------------------------------------------
456 OUString ScAutoFormatDlg::GetCurrFormatName()
458 const ScAutoFormatData* p = pFormat->findByIndex(nIndex);
459 return p ? p->GetName() : OUString();
462 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */