1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
35 SdCustomShowDlg::SdCustomShowDlg(weld::Window
* pWindow
, SdDrawDocument
& rDrawDoc
)
36 : GenericDialogController(pWindow
, u
"modules/simpress/ui/customslideshows.ui"_ustr
, u
"CustomSlideShows"_ustr
)
38 , pCustomShowList(nullptr)
39 , m_xLbCustomShows(m_xBuilder
->weld_tree_view(u
"customshowlist"_ustr
))
40 , m_xBtnNew(m_xBuilder
->weld_button(u
"new"_ustr
))
41 , m_xBtnEdit(m_xBuilder
->weld_button(u
"edit"_ustr
))
42 , m_xBtnRemove(m_xBuilder
->weld_button(u
"delete"_ustr
))
43 , m_xBtnCopy(m_xBuilder
->weld_button(u
"copy"_ustr
))
44 , m_xBtnStartShow(m_xBuilder
->weld_button(u
"startshow"_ustr
))
46 m_xLbCustomShows
->set_size_request(m_xLbCustomShows
->get_approximate_digit_width() * 32,
47 m_xLbCustomShows
->get_height_rows(8));
49 Link
<weld::Button
&,void> aLink( LINK( this, SdCustomShowDlg
, ClickButtonHdl
) );
50 m_xBtnNew
->connect_clicked( aLink
);
51 m_xBtnEdit
->connect_clicked( aLink
);
52 m_xBtnRemove
->connect_clicked( aLink
);
53 m_xBtnCopy
->connect_clicked( aLink
);
54 m_xLbCustomShows
->connect_selection_changed(LINK(this, SdCustomShowDlg
, SelectListBoxHdl
));
56 m_xBtnStartShow
->connect_clicked( LINK( this, SdCustomShowDlg
, StartShowHdl
) ); // for test
58 // get CustomShow list of docs
59 pCustomShowList
= rDoc
.GetCustomShowList();
62 tools::Long nPosToSelect
= pCustomShowList
->GetCurPos();
63 // fill ListBox with CustomShows
64 for( SdCustomShow
* pCustomShow
= pCustomShowList
->First();
65 pCustomShow
!= nullptr;
66 pCustomShow
= pCustomShowList
->Next() )
68 m_xLbCustomShows
->append_text(pCustomShow
->GetName());
70 m_xLbCustomShows
->select(nPosToSelect
);
71 pCustomShowList
->Seek( nPosToSelect
);
77 SdCustomShowDlg::~SdCustomShowDlg()
81 void SdCustomShowDlg::CheckState()
83 int nPos
= m_xLbCustomShows
->get_selected_index();
85 bool bEnable
= nPos
!= -1;
86 m_xBtnEdit
->set_sensitive( bEnable
);
87 m_xBtnRemove
->set_sensitive( bEnable
);
88 m_xBtnCopy
->set_sensitive( bEnable
);
89 m_xBtnStartShow
->set_sensitive(bEnable
);
91 if (bEnable
&& pCustomShowList
)
92 pCustomShowList
->Seek( nPos
);
95 IMPL_LINK( SdCustomShowDlg
, ClickButtonHdl
, weld::Button
&, r
, void )
100 IMPL_LINK( SdCustomShowDlg
, SelectListBoxHdl
, weld::TreeView
&, rListBox
, void )
102 SelectHdl(&rListBox
);
105 void SdCustomShowDlg::SelectHdl(void const *p
)
108 if (p
== m_xBtnNew
.get())
110 std::unique_ptr
<SdCustomShow
> pCustomShow
;
111 SdDefineCustomShowDlg
aDlg(m_xDialog
.get(), rDoc
, pCustomShow
);
112 if (aDlg
.run() == RET_OK
)
116 if( !pCustomShowList
)
117 pCustomShowList
= rDoc
.GetCustomShowList( true );
119 SdCustomShow
* pCustomShowTmp
= pCustomShow
.get();
120 pCustomShowList
->push_back( std::move(pCustomShow
) );
121 pCustomShowList
->Last();
122 m_xLbCustomShows
->append_text( pCustomShowTmp
->GetName() );
123 m_xLbCustomShows
->select_text( pCustomShowTmp
->GetName() );
128 else if( p
== m_xBtnEdit
.get() )
130 int nPos
= m_xLbCustomShows
->get_selected_index();
133 DBG_ASSERT( pCustomShowList
, "pCustomShowList does not exist" );
134 std::unique_ptr
<SdCustomShow
>& pCustomShow
= (*pCustomShowList
)[ nPos
];
135 SdDefineCustomShowDlg
aDlg(m_xDialog
.get(), rDoc
, pCustomShow
);
137 if (aDlg
.run() == RET_OK
)
139 pCustomShowList
->Seek(nPos
);
140 m_xLbCustomShows
->remove(nPos
);
141 m_xLbCustomShows
->insert_text(nPos
, pCustomShow
->GetName());
142 m_xLbCustomShows
->select(nPos
);
147 else if( p
== m_xBtnRemove
.get() )
149 int nPos
= m_xLbCustomShows
->get_selected_index();
152 pCustomShowList
->erase( pCustomShowList
->begin() + nPos
);
153 m_xLbCustomShows
->remove(nPos
);
154 m_xLbCustomShows
->select(nPos
== 0 ? nPos
: nPos
- 1);
158 else if( p
== m_xBtnCopy
.get() )
160 int nPos
= m_xLbCustomShows
->get_selected_index();
163 std::unique_ptr
<SdCustomShow
> pShow(new SdCustomShow( *(*pCustomShowList
)[nPos
] ));
164 OUString
aStr( pShow
->GetName() );
165 OUString
aStrCopy( SdResId( STR_COPY_CUSTOMSHOW
) );
167 sal_Int32 nStrPos
= aStr
.indexOf( aStrCopy
);
171 aStr
+= " (" + aStrCopy
+ OUString::number( nNum
) + ")";
172 nStrPos
= aStr
.indexOf( aStrCopy
);
174 nStrPos
= nStrPos
+ aStrCopy
.getLength();
175 // that we do not access into the nirvana (--> endless loop)
176 if( nStrPos
>= aStr
.getLength() )
178 aStr
+= " " + OUString::number( nNum
);
182 bool bDifferent
= false;
183 //long nPosToSelect = pCustomShowList->GetCurPos();
187 for( SdCustomShow
* pCustomShow
= pCustomShowList
->First();
188 pCustomShow
!= nullptr && bDifferent
;
189 pCustomShow
= pCustomShowList
->Next() )
191 if( aStr
== pCustomShow
->GetName() )
196 // replace number by a number increased by 1
198 const std::optional
<CharClass
>& pCharClass
= rDoc
.GetCharClass();
199 while( pCharClass
->isDigit( aStr
, nStrPos
) )
200 aStr
= aStr
.replaceAt( nStrPos
, 1, u
"" );
201 aStr
= aStr
.subView( 0, nStrPos
) + OUString::number( ++nNum
) + aStr
.subView( nStrPos
);
205 //pCustomShowList->Seek( nPosToSelect );
206 pShow
->SetName( aStr
);
208 auto pShowTmp
= pShow
.get();
209 pCustomShowList
->push_back( std::move(pShow
) );
210 pCustomShowList
->Last();
211 m_xLbCustomShows
->append_text(pShowTmp
->GetName());
212 m_xLbCustomShows
->select_text(pShowTmp
->GetName());
215 else if( p
== m_xLbCustomShows
.get() )
217 int nPos
= m_xLbCustomShows
->get_selected_index();
219 pCustomShowList
->Seek(nPos
);
226 IMPL_LINK_NOARG(SdCustomShowDlg
, StartShowHdl
, weld::Button
&, void)
228 m_xDialog
->response(RET_YES
);
232 bool SdCustomShowDlg::IsCustomShow() const
234 if (!pCustomShowList
->empty())
240 // SdDefineCustomShowDlg
241 SdDefineCustomShowDlg::SdDefineCustomShowDlg(weld::Window
* pWindow
, SdDrawDocument
& rDrawDoc
, std::unique_ptr
<SdCustomShow
>& rpCS
)
242 : GenericDialogController(pWindow
, u
"modules/simpress/ui/definecustomslideshow.ui"_ustr
, u
"DefineCustomSlideShow"_ustr
)
246 , m_xEdtName(m_xBuilder
->weld_entry(u
"customname"_ustr
))
247 , m_xLbPages(m_xBuilder
->weld_tree_view(u
"pages"_ustr
))
248 , m_xBtnAdd(m_xBuilder
->weld_button(u
"add"_ustr
))
249 , m_xBtnRemove(m_xBuilder
->weld_button(u
"remove"_ustr
))
250 , m_xLbCustomPages(m_xBuilder
->weld_tree_view(u
"custompages"_ustr
))
251 , m_xDropTargetHelper(new weld::ReorderingDropTarget(*m_xLbCustomPages
))
252 , m_xBtnOK(m_xBuilder
->weld_button(u
"ok"_ustr
))
254 Link
<weld::Button
&,void> aLink
= LINK( this, SdDefineCustomShowDlg
, ClickButtonHdl
);
255 m_xBtnAdd
->connect_clicked( aLink
);
256 m_xBtnRemove
->connect_clicked( aLink
);
257 m_xEdtName
->connect_changed( LINK( this, SdDefineCustomShowDlg
, ClickButtonEditHdl
) );
258 m_xLbPages
->connect_selection_changed(LINK(this, SdDefineCustomShowDlg
, ClickButtonHdl4
)); // because of status
259 m_xLbCustomPages
->connect_selection_changed(LINK(this, SdDefineCustomShowDlg
, ClickButtonHdl3
)); // because of status
261 m_xBtnOK
->connect_clicked( LINK( this, SdDefineCustomShowDlg
, OKHdl
) );
263 m_xLbPages
->set_selection_mode(SelectionMode::Multiple
);
266 m_xLbPages
->set_size_request(m_xLbPages
->get_approximate_digit_width() * 24, m_xLbPages
->get_height_rows(10));
267 m_xLbCustomPages
->set_size_request(m_xLbPages
->get_approximate_digit_width() * 24, m_xLbCustomPages
->get_height_rows(10));
269 // fill Listbox with page names of Docs
270 for( tools::Long nPage
= 0;
271 nPage
< rDoc
.GetSdPageCount( PageKind::Standard
);
274 SdPage
* pPage
= rDoc
.GetSdPage( static_cast<sal_uInt16
>(nPage
), PageKind::Standard
);
275 m_xLbPages
->append_text(pPage
->GetName());
277 // aLbPages.SelectEntryPos( 0 );
281 aOldName
= rpCustomShow
->GetName();
282 m_xEdtName
->set_text( aOldName
);
284 // fill ListBox with CustomShow pages
285 for( const auto& rpPage
: rpCustomShow
->PagesVector() )
287 m_xLbCustomPages
->append(weld::toId(rpPage
), rpPage
->GetName(), u
""_ustr
);
292 rpCustomShow
.reset(new SdCustomShow
);
293 m_xEdtName
->set_text( SdResId( STR_NEW_CUSTOMSHOW
) );
294 m_xEdtName
->select_region(0, -1);
295 rpCustomShow
->SetName( m_xEdtName
->get_text() );
298 m_xBtnOK
->set_sensitive( false );
302 SdDefineCustomShowDlg::~SdDefineCustomShowDlg()
307 void SdDefineCustomShowDlg::CheckState()
309 bool bPages
= m_xLbPages
->count_selected_rows() > 0;
310 bool bCSPages
= m_xLbCustomPages
->get_selected_index() != -1;
311 bool bCount
= m_xLbCustomPages
->n_children() > 0;
313 m_xBtnOK
->set_sensitive( bCount
);
314 m_xBtnAdd
->set_sensitive( bPages
);
315 m_xBtnRemove
->set_sensitive( bCSPages
);
318 IMPL_LINK( SdDefineCustomShowDlg
, ClickButtonHdl
, weld::Button
&, rWidget
, void )
320 ClickButtonHdl2(&rWidget
);
323 IMPL_LINK( SdDefineCustomShowDlg
, ClickButtonHdl3
, weld::TreeView
&, rWidget
, void )
325 ClickButtonHdl2(&rWidget
);
328 IMPL_LINK( SdDefineCustomShowDlg
, ClickButtonHdl4
, weld::TreeView
&, rListBox
, void )
330 ClickButtonHdl2(&rListBox
);
333 IMPL_LINK( SdDefineCustomShowDlg
, ClickButtonEditHdl
, weld::Entry
&, rEdit
, void )
335 ClickButtonHdl2(&rEdit
);
339 void SdDefineCustomShowDlg::ClickButtonHdl2(void const * p
)
341 if( p
== m_xBtnAdd
.get() )
343 auto aRows
= m_xLbPages
->get_selected_rows();
346 int nPosCP
= m_xLbCustomPages
->get_selected_index();
352 OUString aStr
= m_xLbPages
->get_text(i
);
353 SdPage
* pPage
= rDoc
.GetSdPage(i
, PageKind::Standard
);
354 OUString
sId(weld::toId(pPage
));
355 m_xLbCustomPages
->insert(nPosCP
, aStr
, &sId
, nullptr, nullptr);
356 m_xLbCustomPages
->select(nPosCP
!= -1 ? nPosCP
: m_xLbCustomPages
->n_children() - 1);
364 else if (p
== m_xBtnRemove
.get())
366 int nPos
= m_xLbCustomPages
->get_selected_index();
369 m_xLbCustomPages
->remove(nPos
);
370 m_xLbCustomPages
->select(nPos
== 0 ? nPos
: nPos
- 1);
374 else if( p
== m_xEdtName
.get() )
383 * Checks the page pointer of the Show since entries can be moved and copied
386 void SdDefineCustomShowDlg::CheckCustomShow()
388 bool bDifferent
= false;
391 size_t nCount
= m_xLbCustomPages
->n_children();
392 if (rpCustomShow
->PagesVector().size() != nCount
)
394 rpCustomShow
->PagesVector().clear();
398 // compare page pointer
402 for (const auto& rpPage
: rpCustomShow
->PagesVector())
404 SdPage
* pPage
= weld::fromId
<SdPage
*>(m_xLbCustomPages
->get_id(i
));
407 rpCustomShow
->PagesVector().clear();
416 // set new page pointer
419 for (size_t i
= 0; i
< nCount
; ++i
)
421 SdPage
* pPage
= weld::fromId
<SdPage
*>(m_xLbCustomPages
->get_id(i
));
422 rpCustomShow
->PagesVector().push_back(pPage
);
427 // compare name and set name if necessary
428 OUString
aStr( m_xEdtName
->get_text() );
429 if( rpCustomShow
->GetName() != aStr
)
431 rpCustomShow
->SetName( aStr
);
437 IMPL_LINK_NOARG(SdDefineCustomShowDlg
, OKHdl
, weld::Button
&, void)
440 bool bDifferent
= true;
441 SdCustomShowList
* pCustomShowList
= rDoc
.GetCustomShowList();
442 if( pCustomShowList
)
444 OUString
aName( m_xEdtName
->get_text() );
445 SdCustomShow
* pCustomShow
;
447 tools::Long nPosToSelect
= pCustomShowList
->GetCurPos();
448 for( pCustomShow
= pCustomShowList
->First();
449 pCustomShow
!= nullptr;
450 pCustomShow
= pCustomShowList
->Next() )
452 if( aName
== pCustomShow
->GetName() && aName
!= aOldName
)
455 pCustomShowList
->Seek( nPosToSelect
);
462 m_xDialog
->response(RET_OK
);
466 std::unique_ptr
<weld::MessageDialog
> xWarn(Application::CreateMessageDialog(m_xDialog
.get(),
467 VclMessageType::Warning
, VclButtonsType::Ok
,
468 SdResId(STR_WARN_NAME_DUPLICATE
)));
470 m_xEdtName
->grab_focus();
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */