bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / dlg / custsdlg.cxx
blobb18a6e05e5d2f9152eafce9c740164dd104e70e8
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 .
20 #include <custsdlg.hxx>
22 #include <strings.hrc>
23 #include <sdresid.hxx>
25 #include <drawdoc.hxx>
26 #include <sdpage.hxx>
27 #include <cusshow.hxx>
28 #include <customshowlist.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/weld.hxx>
31 #include <unotools/charclass.hxx>
32 #include <tools/debug.hxx>
34 // SdCustomShowDlg
35 SdCustomShowDlg::SdCustomShowDlg(weld::Window* pWindow, SdDrawDocument& rDrawDoc)
36 : GenericDialogController(pWindow, "modules/simpress/ui/customslideshows.ui", "CustomSlideShows")
37 , rDoc(rDrawDoc)
38 , pCustomShowList(nullptr)
39 , bModified(false)
40 , m_xLbCustomShows(m_xBuilder->weld_tree_view("customshowlist"))
41 , m_xCbxUseCustomShow(m_xBuilder->weld_check_button("usecustomshows"))
42 , m_xBtnNew(m_xBuilder->weld_button("new"))
43 , m_xBtnEdit(m_xBuilder->weld_button("edit"))
44 , m_xBtnRemove(m_xBuilder->weld_button("delete"))
45 , m_xBtnCopy(m_xBuilder->weld_button("copy"))
46 , m_xBtnHelp(m_xBuilder->weld_button("help"))
47 , m_xBtnStartShow(m_xBuilder->weld_button("startshow"))
48 , m_xBtnOK(m_xBuilder->weld_button("ok"))
50 m_xLbCustomShows->set_size_request(m_xLbCustomShows->get_approximate_digit_width() * 32,
51 m_xLbCustomShows->get_height_rows(8));
53 Link<weld::Button&,void> aLink( LINK( this, SdCustomShowDlg, ClickButtonHdl ) );
54 m_xBtnNew->connect_clicked( aLink );
55 m_xBtnEdit->connect_clicked( aLink );
56 m_xBtnRemove->connect_clicked( aLink );
57 m_xBtnCopy->connect_clicked( aLink );
58 m_xCbxUseCustomShow->connect_clicked( aLink );
59 m_xLbCustomShows->connect_changed( LINK( this, SdCustomShowDlg, SelectListBoxHdl ) );
61 m_xBtnStartShow->connect_clicked( LINK( this, SdCustomShowDlg, StartShowHdl ) ); // for test
63 // get CustomShow list of docs
64 pCustomShowList = rDoc.GetCustomShowList();
65 if( pCustomShowList )
67 long nPosToSelect = pCustomShowList->GetCurPos();
68 // fill ListBox with CustomShows
69 for( SdCustomShow* pCustomShow = pCustomShowList->First();
70 pCustomShow != nullptr;
71 pCustomShow = pCustomShowList->Next() )
73 m_xLbCustomShows->append_text(pCustomShow->GetName());
75 m_xLbCustomShows->select(nPosToSelect);
76 pCustomShowList->Seek( nPosToSelect );
79 m_xCbxUseCustomShow->set_active(pCustomShowList && rDoc.getPresentationSettings().mbCustomShow);
81 CheckState();
84 SdCustomShowDlg::~SdCustomShowDlg()
88 void SdCustomShowDlg::CheckState()
90 int nPos = m_xLbCustomShows->get_selected_index();
92 bool bEnable = nPos != -1;
93 m_xBtnEdit->set_sensitive( bEnable );
94 m_xBtnRemove->set_sensitive( bEnable );
95 m_xBtnCopy->set_sensitive( bEnable );
96 m_xCbxUseCustomShow->set_sensitive( bEnable );
97 m_xBtnStartShow->set_sensitive(true);
99 if (bEnable && pCustomShowList)
100 pCustomShowList->Seek( nPos );
103 IMPL_LINK( SdCustomShowDlg, ClickButtonHdl, weld::Button&, r, void )
105 SelectHdl(&r);
108 IMPL_LINK( SdCustomShowDlg, SelectListBoxHdl, weld::TreeView&, rListBox, void )
110 SelectHdl(&rListBox);
113 void SdCustomShowDlg::SelectHdl(void const *p)
115 // new CustomShow
116 if (p == m_xBtnNew.get())
118 std::unique_ptr<SdCustomShow> pCustomShow;
119 SdDefineCustomShowDlg aDlg(m_xDialog.get(), rDoc, pCustomShow);
120 if (aDlg.run() == RET_OK)
122 if( pCustomShow )
124 if( !pCustomShowList )
125 pCustomShowList = rDoc.GetCustomShowList( true );
127 SdCustomShow* pCustomShowTmp = pCustomShow.get();
128 pCustomShowList->push_back( std::move(pCustomShow) );
129 pCustomShowList->Last();
130 m_xLbCustomShows->append_text( pCustomShowTmp->GetName() );
131 m_xLbCustomShows->select_text( pCustomShowTmp->GetName() );
134 if (aDlg.IsModified())
135 bModified = true;
138 // edit CustomShow
139 else if( p == m_xBtnEdit.get() )
141 int nPos = m_xLbCustomShows->get_selected_index();
142 if (nPos != -1)
144 DBG_ASSERT( pCustomShowList, "pCustomShowList does not exist" );
145 std::unique_ptr<SdCustomShow>& pCustomShow = (*pCustomShowList)[ nPos ];
146 SdDefineCustomShowDlg aDlg(m_xDialog.get(), rDoc, pCustomShow);
148 if (aDlg.run() == RET_OK)
150 pCustomShowList->Seek(nPos);
151 m_xLbCustomShows->remove(nPos);
152 m_xLbCustomShows->insert_text(nPos, pCustomShow->GetName());
153 m_xLbCustomShows->select(nPos);
154 if (aDlg.IsModified())
155 bModified = true;
159 // delete CustomShow
160 else if( p == m_xBtnRemove.get() )
162 int nPos = m_xLbCustomShows->get_selected_index();
163 if (nPos != -1)
165 pCustomShowList->erase( pCustomShowList->begin() + nPos );
166 m_xLbCustomShows->remove(nPos);
167 m_xLbCustomShows->select(nPos == 0 ? nPos : nPos - 1);
168 bModified = true;
171 // copy CustomShow
172 else if( p == m_xBtnCopy.get() )
174 int nPos = m_xLbCustomShows->get_selected_index();
175 if (nPos != -1)
177 std::unique_ptr<SdCustomShow> pShow(new SdCustomShow( *(*pCustomShowList)[nPos] ));
178 OUString aStr( pShow->GetName() );
179 OUString aStrCopy( SdResId( STR_COPY_CUSTOMSHOW ) );
181 sal_Int32 nStrPos = aStr.indexOf( aStrCopy );
182 sal_Int32 nNum = 1;
183 if( nStrPos < 0 )
185 aStr = aStr + " (" + aStrCopy + OUString::number( nNum ) + ")";
186 nStrPos = aStr.indexOf( aStrCopy );
188 nStrPos = nStrPos + aStrCopy.getLength();
189 // that we do not access into the nirvana (--> endless loop)
190 if( nStrPos >= aStr.getLength() )
192 aStr = aStr + " " + OUString::number( nNum );
195 // check name...
196 bool bDifferent = false;
197 //long nPosToSelect = pCustomShowList->GetCurPos();
198 while( !bDifferent )
200 bDifferent = true;
201 for( SdCustomShow* pCustomShow = pCustomShowList->First();
202 pCustomShow != nullptr && bDifferent;
203 pCustomShow = pCustomShowList->Next() )
205 if( aStr == pCustomShow->GetName() )
206 bDifferent = false;
208 if( !bDifferent )
210 // replace number by a number increased by 1
212 const CharClass* pCharClass = rDoc.GetCharClass();
213 while( pCharClass->isDigit( aStr, nStrPos ) )
214 aStr = aStr.replaceAt( nStrPos, 1, "" );
215 aStr = aStr.copy( 0, nStrPos) + OUString::number( ++nNum ) + aStr.copy( nStrPos);
219 //pCustomShowList->Seek( nPosToSelect );
220 pShow->SetName( aStr );
222 auto pShowTmp = pShow.get();
223 pCustomShowList->push_back( std::move(pShow) );
224 pCustomShowList->Last();
225 m_xLbCustomShows->append_text(pShowTmp->GetName());
226 m_xLbCustomShows->select_text(pShowTmp->GetName());
228 bModified = true;
231 else if( p == m_xLbCustomShows.get() )
233 int nPos = m_xLbCustomShows->get_selected_index();
234 if (nPos != -1)
235 pCustomShowList->Seek(nPos);
237 bModified = true;
239 else if( p == m_xCbxUseCustomShow.get() )
241 bModified = true;
244 CheckState();
247 // StartShow-Hdl
248 IMPL_LINK_NOARG(SdCustomShowDlg, StartShowHdl, weld::Button&, void)
250 m_xDialog->response(RET_YES);
253 // CheckState
254 bool SdCustomShowDlg::IsCustomShow() const
256 return (m_xCbxUseCustomShow->get_sensitive() && m_xCbxUseCustomShow->get_active());
259 // SdDefineCustomShowDlg
260 SdDefineCustomShowDlg::SdDefineCustomShowDlg(weld::Window* pWindow, SdDrawDocument& rDrawDoc, std::unique_ptr<SdCustomShow>& rpCS)
261 : GenericDialogController(pWindow, "modules/simpress/ui/definecustomslideshow.ui", "DefineCustomSlideShow")
262 , rDoc(rDrawDoc)
263 , rpCustomShow(rpCS)
264 , bModified(false)
265 , m_xEdtName(m_xBuilder->weld_entry("customname"))
266 , m_xLbPages(m_xBuilder->weld_tree_view("pages"))
267 , m_xBtnAdd(m_xBuilder->weld_button("add"))
268 , m_xBtnRemove(m_xBuilder->weld_button("remove"))
269 , m_xLbCustomPages(m_xBuilder->weld_tree_view("custompages"))
270 , m_xBtnOK(m_xBuilder->weld_button("ok"))
271 , m_xBtnCancel(m_xBuilder->weld_button("cancel"))
272 , m_xBtnHelp(m_xBuilder->weld_button("help"))
274 Link<weld::Button&,void> aLink = LINK( this, SdDefineCustomShowDlg, ClickButtonHdl );
275 m_xBtnAdd->connect_clicked( aLink );
276 m_xBtnRemove->connect_clicked( aLink );
277 m_xEdtName->connect_changed( LINK( this, SdDefineCustomShowDlg, ClickButtonEditHdl ) );
278 m_xLbPages->connect_changed( LINK( this, SdDefineCustomShowDlg, ClickButtonHdl4 ) ); // because of status
279 m_xLbCustomPages->connect_changed( LINK( this, SdDefineCustomShowDlg, ClickButtonHdl3 ) ); // because of status
281 m_xBtnOK->connect_clicked( LINK( this, SdDefineCustomShowDlg, OKHdl ) );
283 m_xLbPages->set_selection_mode(SelectionMode::Multiple);
285 // shape 'em a bit
286 m_xLbPages->set_size_request(m_xLbPages->get_approximate_digit_width() * 24, m_xLbPages->get_height_rows(10));
287 m_xLbCustomPages->set_size_request(m_xLbPages->get_approximate_digit_width() * 24, m_xLbCustomPages->get_height_rows(10));
289 // fill Listbox with page names of Docs
290 for( long nPage = 0;
291 nPage < rDoc.GetSdPageCount( PageKind::Standard );
292 nPage++ )
294 SdPage* pPage = rDoc.GetSdPage( static_cast<sal_uInt16>(nPage), PageKind::Standard );
295 m_xLbPages->append_text(pPage->GetName());
297 // aLbPages.SelectEntryPos( 0 );
299 if( rpCustomShow )
301 aOldName = rpCustomShow->GetName();
302 m_xEdtName->set_text( aOldName );
304 // fill ListBox with CustomShow pages
305 for( const auto& rpPage : rpCustomShow->PagesVector() )
307 m_xLbCustomPages->append(OUString::number(reinterpret_cast<sal_uInt64>(rpPage)), rpPage->GetName(), "");
310 else
312 rpCustomShow.reset(new SdCustomShow);
313 m_xEdtName->set_text( SdResId( STR_NEW_CUSTOMSHOW ) );
314 m_xEdtName->select_region(0, -1);
315 rpCustomShow->SetName( m_xEdtName->get_text() );
318 m_xBtnOK->set_sensitive( false );
319 CheckState();
322 SdDefineCustomShowDlg::~SdDefineCustomShowDlg()
326 // CheckState
327 void SdDefineCustomShowDlg::CheckState()
329 bool bPages = m_xLbPages->count_selected_rows() > 0;
330 bool bCSPages = m_xLbCustomPages->get_selected_index() != -1;
331 bool bCount = m_xLbCustomPages->n_children() > 0;
333 m_xBtnOK->set_sensitive( bCount );
334 m_xBtnAdd->set_sensitive( bPages );
335 m_xBtnRemove->set_sensitive( bCSPages );
338 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonHdl, weld::Button&, rWidget, void )
340 ClickButtonHdl2(&rWidget);
343 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonHdl3, weld::TreeView&, rWidget, void )
345 ClickButtonHdl2(&rWidget);
348 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonHdl4, weld::TreeView&, rListBox, void )
350 ClickButtonHdl2(&rListBox);
353 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonEditHdl, weld::Entry&, rEdit, void )
355 ClickButtonHdl2(&rEdit);
358 // ButtonHdl()
359 void SdDefineCustomShowDlg::ClickButtonHdl2(void const * p)
361 if( p == m_xBtnAdd.get() )
363 auto aRows = m_xLbPages->get_selected_rows();
364 if (!aRows.empty())
366 int nPosCP = m_xLbCustomPages->get_selected_index();
367 if (nPosCP != -1)
368 ++nPosCP;
370 for (auto i : aRows)
372 OUString aStr = m_xLbPages->get_text(i);
373 SdPage* pPage = rDoc.GetSdPage(i, PageKind::Standard);
374 OUString sId(OUString::number(reinterpret_cast<sal_uInt64>(pPage)));
375 m_xLbCustomPages->insert(nPosCP, aStr, &sId, nullptr, nullptr);
376 m_xLbCustomPages->select(nPosCP != -1 ? nPosCP : m_xLbCustomPages->n_children() - 1);
378 if (nPosCP != -1)
379 ++nPosCP;
381 bModified = true;
384 else if (p == m_xBtnRemove.get())
386 int nPos = m_xLbCustomPages->get_selected_index();
387 if (nPos != -1)
389 m_xLbCustomPages->remove(nPos);
390 m_xLbCustomPages->select(nPos == 0 ? nPos : nPos - 1);
391 bModified = true;
394 else if( p == m_xEdtName.get() )
396 bModified = true;
399 CheckState();
403 * Checks the page pointer of the Show since entries can be moved and copied
404 * by TreeLB.
406 void SdDefineCustomShowDlg::CheckCustomShow()
408 bool bDifferent = false;
410 // compare count
411 size_t nCount = m_xLbCustomPages->n_children();
412 if (rpCustomShow->PagesVector().size() != nCount)
414 rpCustomShow->PagesVector().clear();
415 bDifferent = true;
418 // compare page pointer
419 if( !bDifferent )
421 size_t i = 0;
422 for (const auto& rpPage : rpCustomShow->PagesVector())
424 SdPage* pPage = reinterpret_cast<SdPage*>(m_xLbCustomPages->get_id(i).toUInt64());
425 if (rpPage != pPage)
427 rpCustomShow->PagesVector().clear();
428 bDifferent = true;
429 break;
432 ++i;
436 // set new page pointer
437 if( bDifferent )
439 for (size_t i = 0; i < nCount; ++i)
441 SdPage* pPage = reinterpret_cast<SdPage*>(m_xLbCustomPages->get_id(i).toUInt64());
442 rpCustomShow->PagesVector().push_back(pPage);
444 bModified = true;
447 // compare name and set name if necessary
448 OUString aStr( m_xEdtName->get_text() );
449 if( rpCustomShow->GetName() != aStr )
451 rpCustomShow->SetName( aStr );
452 bModified = true;
456 // OK-Hdl
457 IMPL_LINK_NOARG(SdDefineCustomShowDlg, OKHdl, weld::Button&, void)
459 // check name...
460 bool bDifferent = true;
461 SdCustomShowList* pCustomShowList = rDoc.GetCustomShowList();
462 if( pCustomShowList )
464 OUString aName( m_xEdtName->get_text() );
465 SdCustomShow* pCustomShow;
467 long nPosToSelect = pCustomShowList->GetCurPos();
468 for( pCustomShow = pCustomShowList->First();
469 pCustomShow != nullptr;
470 pCustomShow = pCustomShowList->Next() )
472 if( aName == pCustomShow->GetName() && aName != aOldName )
473 bDifferent = false;
475 pCustomShowList->Seek( nPosToSelect );
478 if( bDifferent )
480 CheckCustomShow();
482 m_xDialog->response(RET_OK);
484 else
486 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(m_xDialog.get(),
487 VclMessageType::Warning, VclButtonsType::Ok,
488 SdResId(STR_WARN_NAME_DUPLICATE)));
489 xWarn->run();
490 m_xEdtName->grab_focus();
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */