bump product version to 6.3.0.0.beta1
[LibreOffice.git] / cui / source / customize / SvxToolbarConfigPage.cxx
blob178a9c851ac316c7cfd092a4807d0646600a59c1
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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <cassert>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <typeinfo>
28 #include <vcl/commandinfoprovider.hxx>
29 #include <vcl/event.hxx>
30 #include <vcl/help.hxx>
31 #include <vcl/weld.hxx>
32 #include <vcl/decoview.hxx>
33 #include <vcl/virdev.hxx>
34 #include <vcl/settings.hxx>
35 #include <vcl/svapp.hxx>
37 #include <sfx2/app.hxx>
38 #include <sfx2/sfxdlg.hxx>
39 #include <sfx2/viewfrm.hxx>
40 #include <sfx2/viewsh.hxx>
41 #include <sfx2/msg.hxx>
42 #include <sfx2/msgpool.hxx>
43 #include <sfx2/minfitem.hxx>
44 #include <sfx2/objsh.hxx>
45 #include <sfx2/request.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sfx2/sfxsids.hrc>
48 #include <svl/stritem.hxx>
49 #include <svtools/miscopt.hxx>
50 #include <tools/diagnose_ex.h>
52 #include <algorithm>
53 #include <helpids.h>
54 #include <strings.hrc>
56 #include <acccfg.hxx>
57 #include <cfg.hxx>
58 #include <SvxToolbarConfigPage.hxx>
59 #include <SvxConfigPageHelper.hxx>
60 #include <dialmgr.hxx>
62 #include <comphelper/processfactory.hxx>
63 #include <unotools/configmgr.hxx>
64 #include <com/sun/star/embed/ElementModes.hpp>
65 #include <com/sun/star/embed/FileSystemStorageFactory.hpp>
66 #include <com/sun/star/frame/UnknownModuleException.hpp>
67 #include <com/sun/star/frame/XFrames.hpp>
68 #include <com/sun/star/frame/XLayoutManager.hpp>
69 #include <com/sun/star/frame/FrameSearchFlag.hpp>
70 #include <com/sun/star/frame/ModuleManager.hpp>
71 #include <com/sun/star/frame/XController.hpp>
72 #include <com/sun/star/frame/Desktop.hpp>
73 #include <com/sun/star/graphic/GraphicProvider.hpp>
74 #include <com/sun/star/io/IOException.hpp>
75 #include <com/sun/star/lang/IllegalAccessException.hpp>
76 #include <com/sun/star/ui/ItemType.hpp>
77 #include <com/sun/star/ui/ItemStyle.hpp>
78 #include <com/sun/star/ui/ImageManager.hpp>
79 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
80 #include <com/sun/star/ui/XUIConfiguration.hpp>
81 #include <com/sun/star/ui/XUIConfigurationListener.hpp>
82 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
83 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
84 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
85 #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
86 #include <com/sun/star/ui/XUIElement.hpp>
87 #include <com/sun/star/ui/UIElementType.hpp>
88 #include <com/sun/star/ui/ImageType.hpp>
89 #include <com/sun/star/ui/theWindowStateConfiguration.hpp>
90 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
91 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
92 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
93 #include <com/sun/star/util/thePathSettings.hpp>
95 #include <dlgname.hxx>
97 SvxToolbarConfigPage::SvxToolbarConfigPage(TabPageParent pParent, const SfxItemSet& rSet)
98 : SvxConfigPage(pParent, rSet)
100 m_xGearBtn = m_xBuilder->weld_menu_button("toolbargearbtn");
101 m_xGearBtn->show();
102 m_xContainer->set_help_id(HID_SVX_CONFIG_TOOLBAR);
104 m_xContentsListBox.reset(new SvxToolbarEntriesListBox(m_xBuilder->weld_tree_view("toolcontents"), this));
105 std::vector<int> aWidths;
106 weld::TreeView& rTreeView = m_xContentsListBox->get_widget();
107 Size aSize(m_xFunctions->get_size_request());
108 rTreeView.set_size_request(aSize.Width(), aSize.Height());
110 int nExpectedSize = 16;
112 int nStandardImageColWidth = rTreeView.get_checkbox_column_width();
113 int nMargin = nStandardImageColWidth - nExpectedSize;
114 if (nMargin < 16)
115 nMargin = 16;
117 if (SvxConfigPageHelper::GetImageType() & css::ui::ImageType::SIZE_LARGE)
118 nExpectedSize = 24;
119 else if (SvxConfigPageHelper::GetImageType() & css::ui::ImageType::SIZE_32)
120 nExpectedSize = 32;
122 int nImageColWidth = nExpectedSize + nMargin;
124 aWidths.push_back(nStandardImageColWidth);
125 aWidths.push_back(nImageColWidth);
126 rTreeView.set_column_fixed_widths(aWidths);
128 rTreeView.set_hexpand(true);
129 rTreeView.set_vexpand(true);
130 rTreeView.set_help_id( HID_SVX_CONFIG_TOOLBAR_CONTENTS );
131 rTreeView.show();
133 rTreeView.connect_changed(
134 LINK( this, SvxToolbarConfigPage, SelectToolbarEntry ) );
136 rTreeView.connect_model_changed(LINK(this, SvxToolbarConfigPage, ListModifiedHdl));
138 m_xTopLevelListBox->set_help_id ( HID_SVX_TOPLEVELLISTBOX );
139 m_xSaveInListBox->set_help_id( HID_SVX_SAVE_IN );
140 m_xMoveUpButton->set_help_id( HID_SVX_UP_TOOLBAR_ITEM );
141 m_xMoveDownButton->set_help_id( HID_SVX_DOWN_TOOLBAR_ITEM );
142 m_xDescriptionField->set_help_id ( HID_SVX_DESCFIELD );
144 m_xCommandCategoryListBox->connect_changed(
145 LINK( this, SvxToolbarConfigPage, SelectCategory ) );
147 m_xGearBtn->connect_selected(
148 LINK( this, SvxToolbarConfigPage, GearHdl ) );
150 m_xMoveUpButton->connect_clicked( LINK( this, SvxToolbarConfigPage, MoveHdl) );
151 m_xMoveDownButton->connect_clicked( LINK( this, SvxToolbarConfigPage, MoveHdl) );
152 // Always enable Up and Down buttons
153 // added for issue i53677 by shizhoubo
154 m_xMoveDownButton->set_sensitive(true);
155 m_xMoveUpButton->set_sensitive(true);
157 m_xAddCommandButton->connect_clicked( LINK( this, SvxToolbarConfigPage, AddCommandHdl ) );
158 m_xRemoveCommandButton->connect_clicked( LINK( this, SvxToolbarConfigPage, RemoveCommandHdl ) );
160 m_xInsertBtn->connect_selected(
161 LINK( this, SvxToolbarConfigPage, InsertHdl ) );
162 m_xModifyBtn->connect_selected(
163 LINK( this, SvxToolbarConfigPage, ModifyItemHdl ) );
164 m_xResetBtn->connect_clicked(
165 LINK( this, SvxToolbarConfigPage, ResetToolbarHdl ) );
167 // "Insert Submenu" is irrelevant to the toolbars
168 m_xInsertBtn->remove_item("insertsubmenu");
170 // Gear menu's "Move" action is irrelevant to the toolbars
171 m_xGearBtn->set_item_sensitive("toolbar_gear_move", false);
173 // default toolbar to select is standardbar unless a different one
174 // has been passed in
175 m_aURLToSelect = ITEM_TOOLBAR_URL;
176 m_aURLToSelect += "standardbar";
178 const SfxPoolItem* pItem =
179 rSet.GetItem( rSet.GetPool()->GetWhich( SID_CONFIG ) );
181 if ( pItem )
183 OUString text = static_cast<const SfxStringItem*>(pItem)->GetValue();
184 if (text.startsWith( ITEM_TOOLBAR_URL ))
186 m_aURLToSelect = text.copy( 0 );
191 IMPL_LINK_NOARG(SvxToolbarConfigPage, ListModifiedHdl, weld::TreeView&, void)
193 // regenerate with the current ordering within the list
194 SvxEntries* pEntries = GetTopLevelSelection()->GetEntries();
195 pEntries->clear();
197 for (int i = 0; i < m_xContentsListBox->n_children(); ++i)
198 pEntries->push_back(reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(i).toInt64()));
200 GetSaveInData()->SetModified();
201 GetTopLevelSelection()->SetModified();
203 SvxConfigEntry* pToolbar = GetTopLevelSelection();
204 if ( pToolbar )
205 static_cast<ToolbarSaveInData*>(GetSaveInData())->ApplyToolbar( pToolbar );
208 SvxToolbarConfigPage::~SvxToolbarConfigPage()
210 disposeOnce();
213 void SvxToolbarConfigPage::dispose()
215 for (int i = 0, nCount = m_xSaveInListBox->get_count(); i < nCount; ++i)
217 ToolbarSaveInData* pData =
218 reinterpret_cast<ToolbarSaveInData*>(m_xSaveInListBox->get_id(i).toInt64());
219 delete pData;
221 m_xSaveInListBox->clear();
223 SvxConfigPage::dispose();
226 void SvxToolbarConfigPage::DeleteSelectedTopLevel()
228 const sal_Int32 nSelectionPos = m_xTopLevelListBox->get_active();
229 ToolbarSaveInData* pSaveInData = static_cast<ToolbarSaveInData*>( GetSaveInData() );
230 pSaveInData->RemoveToolbar( GetTopLevelSelection() );
232 int nCount = m_xTopLevelListBox->get_count();
233 if (nCount > 1)
235 // select next entry after the one being deleted
236 // selection position is indexed from 0 so need to
237 // subtract one from the entry count
238 if (nSelectionPos != nCount - 1)
240 m_xTopLevelListBox->set_active(nSelectionPos + 1);
242 else
244 m_xTopLevelListBox->set_active(nSelectionPos - 1);
246 SelectElement();
248 // and now remove the entry
249 m_xTopLevelListBox->remove(nSelectionPos);
251 else
253 ReloadTopLevelListBox();
257 void SvxToolbarConfigPage::DeleteSelectedContent()
259 int nActEntry = m_xContentsListBox->get_selected_index();
261 if (nActEntry != -1)
263 // get currently selected entry
264 SvxConfigEntry* pEntry =
265 reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(nActEntry).toInt64());
267 SvxConfigEntry* pToolbar = GetTopLevelSelection();
269 // remove entry from the list for this toolbar
270 SvxConfigPageHelper::RemoveEntry( pToolbar->GetEntries(), pEntry );
272 // remove toolbar entry from UI
273 m_xContentsListBox->remove(nActEntry);
275 // delete data for toolbar entry
276 delete pEntry;
278 static_cast<ToolbarSaveInData*>(GetSaveInData())->ApplyToolbar( pToolbar );
279 UpdateButtonStates();
281 // if this is the last entry in the toolbar and it is a user
282 // defined toolbar pop up a dialog asking the user if they
283 // want to delete the toolbar
284 if ( m_xContentsListBox->n_children() == 0 &&
285 GetTopLevelSelection()->IsDeletable() )
287 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(GetDialogFrameWeld(),
288 VclMessageType::Question, VclButtonsType::YesNo,
289 CuiResId(RID_SXVSTR_CONFIRM_DELETE_TOOLBAR)));
290 if (xQueryBox->run() == RET_YES)
292 DeleteSelectedTopLevel();
298 IMPL_LINK( SvxToolbarConfigPage, MoveHdl, weld::Button&, rButton, void )
300 MoveEntry(&rButton == m_xMoveUpButton.get());
303 void SvxToolbarConfigPage::MoveEntry( bool bMoveUp )
305 SvxConfigPage::MoveEntry( bMoveUp );
307 // Apply change to currently selected toolbar
308 SvxConfigEntry* pToolbar = GetTopLevelSelection();
309 if ( pToolbar )
310 static_cast<ToolbarSaveInData*>(GetSaveInData())->ApplyToolbar( pToolbar );
311 else
313 SAL_WARN( "cui.customize", "SvxToolbarConfigPage::MoveEntry(): no entry" );
314 UpdateButtonStates();
318 void SvxToolbarConfigPage::Init()
320 // ensure that the UI is cleared before populating it
321 m_xTopLevelListBox->clear();
322 m_xContentsListBox->clear();
324 ReloadTopLevelListBox();
326 sal_Int32 nPos = 0;
327 if ( !m_aURLToSelect.isEmpty() )
329 for (sal_Int32 i = 0, nCount = m_xTopLevelListBox->get_count(); i < nCount; ++i)
331 SvxConfigEntry* pData =
332 reinterpret_cast<SvxConfigEntry*>(m_xTopLevelListBox->get_id(i).toInt64());
334 if ( pData->GetCommand().equals( m_aURLToSelect ) )
336 nPos = i;
337 break;
341 // in future select the default toolbar: Standard
342 m_aURLToSelect = ITEM_TOOLBAR_URL;
343 m_aURLToSelect += "standardbar";
346 m_xTopLevelListBox->set_active(nPos);
347 SelectElement();
349 m_xCommandCategoryListBox->Init(
350 comphelper::getProcessComponentContext(),
351 m_xFrame,
352 vcl::CommandInfoProvider::GetModuleIdentifier(m_xFrame));
353 m_xCommandCategoryListBox->categorySelected(m_xFunctions.get(), OUString(), GetSaveInData());
356 SaveInData* SvxToolbarConfigPage::CreateSaveInData(
357 const css::uno::Reference< css::ui::XUIConfigurationManager >& xCfgMgr,
358 const css::uno::Reference< css::ui::XUIConfigurationManager >& xParentCfgMgr,
359 const OUString& aModuleId,
360 bool bDocConfig )
362 return static_cast< SaveInData* >(
363 new ToolbarSaveInData( xCfgMgr, xParentCfgMgr, aModuleId, bDocConfig ));
366 IMPL_LINK_NOARG(SvxToolbarConfigPage, SelectToolbarEntry, weld::TreeView&, void)
368 UpdateButtonStates();
371 IMPL_LINK( SvxToolbarConfigPage, GearHdl, const OString&, rIdent, void )
373 SvxConfigEntry* pCurrentToolbar = GetTopLevelSelection();
375 if (rIdent == "toolbar_gear_add")
377 OUString prefix = CuiResId( RID_SVXSTR_NEW_TOOLBAR );
379 OUString aNewName =
380 SvxConfigPageHelper::generateCustomName( prefix, GetSaveInData()->GetEntries() );
382 OUString aNewURL =
383 SvxConfigPageHelper::generateCustomURL( GetSaveInData()->GetEntries() );
385 SvxNewToolbarDialog aNameDialog(GetDialogFrameWeld(), aNewName);
387 // Reflect the actual m_xSaveInListBox into the new toolbar dialog
388 for (int i = 0, nCount = m_xSaveInListBox->get_count(); i < nCount; ++i)
389 aNameDialog.m_xSaveInListBox->append_text(m_xSaveInListBox->get_text(i));
391 aNameDialog.m_xSaveInListBox->set_active(m_xSaveInListBox->get_active());
393 if (aNameDialog.run() == RET_OK)
395 aNewName = aNameDialog.GetName();
397 // Where to save the new toolbar? (i.e. Modulewise or documentwise)
398 int nInsertPos = aNameDialog.m_xSaveInListBox->get_active();
400 ToolbarSaveInData* pData =
401 reinterpret_cast<ToolbarSaveInData*>(
402 m_xSaveInListBox->get_id(nInsertPos).toInt64() );
404 if ( GetSaveInData() != pData )
406 m_xSaveInListBox->set_active(nInsertPos);
407 SelectSaveInLocation(*m_xSaveInListBox);
410 SvxConfigEntry* pToolbar =
411 new SvxConfigEntry( aNewName, aNewURL, true, false );
413 pToolbar->SetUserDefined();
414 pToolbar->SetMain();
416 pData->CreateToolbar( pToolbar );
418 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pToolbar)));
419 m_xTopLevelListBox->append(sId, pToolbar->GetName());
420 m_xTopLevelListBox->set_active_id(sId);
421 SelectElement();
423 pData->SetModified();
426 else if (rIdent == "toolbar_gear_delete")
428 if ( pCurrentToolbar && pCurrentToolbar->IsDeletable() )
430 DeleteSelectedTopLevel();
431 UpdateButtonStates();
434 else if (rIdent == "toolbar_gear_rename")
436 sal_Int32 nSelectionPos = m_xTopLevelListBox->get_active();
437 SvxConfigEntry* pToolbar =
438 reinterpret_cast<SvxConfigEntry*>(m_xTopLevelListBox->get_id(nSelectionPos).toInt64());
439 ToolbarSaveInData* pSaveInData = static_cast<ToolbarSaveInData*>( GetSaveInData() );
441 //Rename the toolbar
442 OUString sCurrentName( SvxConfigPageHelper::stripHotKey( pToolbar->GetName() ) );
443 OUString sDesc = CuiResId( RID_SVXSTR_LABEL_NEW_NAME );
445 SvxNameDialog aNameDialog(GetDialogFrameWeld(), sCurrentName, sDesc);
446 aNameDialog.set_help_id(HID_SVX_CONFIG_RENAME_TOOLBAR);
447 aNameDialog.set_title(CuiResId(RID_SVXSTR_RENAME_TOOLBAR));
449 if ( aNameDialog.run() == RET_OK )
451 OUString sNewName = aNameDialog.GetName();
453 if (sCurrentName == sNewName)
454 return;
456 pToolbar->SetName( sNewName );
457 pSaveInData->ApplyToolbar( pToolbar );
459 // have to use remove and insert to change the name
460 m_xTopLevelListBox->remove(nSelectionPos);
461 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pToolbar)));
462 m_xTopLevelListBox->insert(nSelectionPos, sNewName, &sId, nullptr, nullptr);
463 m_xTopLevelListBox->set_active_id(sId);
466 else if (rIdent == "toolbar_gear_iconOnly" || rIdent == "toolbar_gear_textOnly" || rIdent == "toolbar_gear_iconAndText")
468 ToolbarSaveInData* pSaveInData = static_cast<ToolbarSaveInData*>( GetSaveInData() );
470 if (pCurrentToolbar == nullptr || pSaveInData == nullptr)
472 SAL_WARN("cui.customize", "NULL toolbar or savein data");
473 return;
476 sal_Int32 nStyle = 0;
477 if (rIdent == "toolbar_gear_iconOnly")
478 nStyle = 0;
479 else if (rIdent == "toolbar_gear_textOnly")
480 nStyle = 1;
481 else if (rIdent == "toolbar_gear_iconAndText")
482 nStyle = 2;
484 pCurrentToolbar->SetStyle( nStyle );
485 pSaveInData->SetSystemStyle( m_xFrame, pCurrentToolbar->GetCommand(), nStyle );
487 SelectElement();
489 else
491 //This block should never be reached
492 SAL_WARN("cui.customize", "Unknown gear menu option: " << rIdent);
493 return;
497 IMPL_LINK_NOARG( SvxToolbarConfigPage, SelectCategory, weld::ComboBox&, void )
499 OUString aSearchTerm(m_xSearchEdit->get_text());
501 m_xCommandCategoryListBox->categorySelected(m_xFunctions.get(), aSearchTerm, GetSaveInData());
504 IMPL_LINK_NOARG( SvxToolbarConfigPage, AddCommandHdl, weld::Button&, void )
506 AddFunction();
509 IMPL_LINK_NOARG( SvxToolbarConfigPage, RemoveCommandHdl, weld::Button&, void )
511 DeleteSelectedContent();
514 IMPL_LINK(SvxToolbarConfigPage, InsertHdl, const OString&, rIdent, void)
516 if (rIdent == "insertseparator")
518 // Get the currently selected toolbar
519 SvxConfigEntry* pToolbar = GetTopLevelSelection();
521 SvxConfigEntry* pNewEntryData = new SvxConfigEntry;
522 pNewEntryData->SetUserDefined();
524 int nPos = InsertEntry(pNewEntryData, -1);
525 InsertEntryIntoUI(pNewEntryData, nPos, 1);
527 static_cast<ToolbarSaveInData*>( GetSaveInData())->ApplyToolbar( pToolbar );
529 UpdateButtonStates();
531 else
533 //This block should never be reached
534 SAL_WARN("cui.customize", "Unknown insert option: " << rIdent);
535 return;
539 IMPL_LINK(SvxToolbarConfigPage, ModifyItemHdl, const OString&, rIdent, void)
541 bool bNeedsApply = false;
543 // get currently selected toolbar
544 SvxConfigEntry* pToolbar = GetTopLevelSelection();
546 if (rIdent.isEmpty() || pToolbar == nullptr)
548 SAL_WARN("cui.customize", "No toolbar selected, or empty rIdent!");
549 return;
552 if (rIdent == "renameItem")
554 int nActEntry = m_xContentsListBox->get_selected_index();
555 SvxConfigEntry* pEntry =
556 reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(nActEntry).toInt64());
558 OUString aNewName( SvxConfigPageHelper::stripHotKey( pEntry->GetName() ) );
559 OUString aDesc = CuiResId( RID_SVXSTR_LABEL_NEW_NAME );
561 SvxNameDialog aNameDialog(GetDialogFrameWeld(), aNewName, aDesc);
562 aNameDialog.set_help_id(HID_SVX_CONFIG_RENAME_TOOLBAR_ITEM);
563 aNameDialog.set_title(CuiResId(RID_SVXSTR_RENAME_TOOLBAR));
565 if (aNameDialog.run() == RET_OK)
567 aNewName = aNameDialog.GetName();
569 if( aNewName.isEmpty() ) // tdf#80758 - Accelerator character ("~") is passed as
570 pEntry->SetName( "~" ); // the button name in case of empty values.
571 else
572 pEntry->SetName( aNewName );
574 m_xContentsListBox->set_text(nActEntry, aNewName, 2);
575 bNeedsApply = true;
578 else if (rIdent == "changeIcon")
580 int nActEntry = m_xContentsListBox->get_selected_index();
581 SvxConfigEntry* pEntry =
582 reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(nActEntry).toInt64());
584 SvxIconSelectorDialog aIconDialog(GetDialogFrameWeld(),
585 GetSaveInData()->GetImageManager(),
586 GetSaveInData()->GetParentImageManager());
588 if (aIconDialog.run() == RET_OK)
590 css::uno::Reference< css::graphic::XGraphic > newgraphic =
591 aIconDialog.GetSelectedIcon();
593 if ( newgraphic.is() )
595 css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > >
596 aGraphicSeq( 1 );
598 css::uno::Sequence<OUString> aURLSeq { pEntry->GetCommand() };
600 if ( !pEntry->GetBackupGraphic().is() )
602 css::uno::Reference< css::graphic::XGraphic > backup =
603 SvxConfigPageHelper::GetGraphic(GetSaveInData()->GetImageManager(),
604 aURLSeq[0]);
606 if ( backup.is() )
608 pEntry->SetBackupGraphic(backup);
612 aGraphicSeq[ 0 ] = newgraphic;
615 GetSaveInData()->GetImageManager()->replaceImages(
616 SvxConfigPageHelper::GetImageType(), aURLSeq, aGraphicSeq );
618 m_xContentsListBox->remove(nActEntry);
620 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pEntry)));
621 m_xContentsListBox->insert(nActEntry, sId);
622 m_xContentsListBox->set_toggle(nActEntry, pEntry->IsVisible() ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
623 InsertEntryIntoUI(pEntry, nActEntry, 1);
625 m_xContentsListBox->select(nActEntry);
626 m_xContentsListBox->scroll_to_row(nActEntry);
628 GetSaveInData()->PersistChanges(
629 GetSaveInData()->GetImageManager() );
631 catch ( css::uno::Exception& e)
633 SAL_WARN("cui.customize", "Error replacing image: " << e);
638 else if (rIdent == "resetIcon")
640 int nActEntry = m_xContentsListBox->get_selected_index();
641 SvxConfigEntry* pEntry =
642 reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(nActEntry).toInt64());
644 css::uno::Reference< css::graphic::XGraphic > backup =
645 pEntry->GetBackupGraphic();
647 css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > >
648 aGraphicSeq( 1 );
649 aGraphicSeq[ 0 ] = backup;
651 css::uno::Sequence<OUString> aURLSeq { pEntry->GetCommand() };
655 GetSaveInData()->GetImageManager()->replaceImages(
656 SvxConfigPageHelper::GetImageType(), aURLSeq, aGraphicSeq );
658 m_xContentsListBox->remove(nActEntry);
660 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pEntry)));
661 m_xContentsListBox->insert(nActEntry, sId);
662 m_xContentsListBox->set_toggle(nActEntry, pEntry->IsVisible() ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
663 InsertEntryIntoUI(pEntry, nActEntry, 1);
665 m_xContentsListBox->select(nActEntry);
666 m_xContentsListBox->scroll_to_row(nActEntry);
668 // reset backup in entry
669 pEntry->SetBackupGraphic(
670 css::uno::Reference< css::graphic::XGraphic >() );
672 GetSaveInData()->PersistChanges(
673 GetSaveInData()->GetImageManager() );
675 catch ( css::uno::Exception& e )
677 SAL_WARN("cui.customize", "Error resetting image: " << e);
680 else if (rIdent == "restoreItem")
682 int nActEntry = m_xContentsListBox->get_selected_index();
683 SvxConfigEntry* pEntry =
684 reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(nActEntry).toInt64());
686 ToolbarSaveInData* pSaveInData =
687 static_cast<ToolbarSaveInData*>( GetSaveInData() );
689 OUString aSystemName =
690 pSaveInData->GetSystemUIName( pEntry->GetCommand() );
692 if ( !pEntry->GetName().equals( aSystemName ) )
694 pEntry->SetName( aSystemName );
695 m_xContentsListBox->set_text(
696 nActEntry, SvxConfigPageHelper::stripHotKey(aSystemName), 2);
697 bNeedsApply = true;
700 css::uno::Sequence<OUString> aURLSeq { pEntry->GetCommand() };
704 GetSaveInData()->GetImageManager()->removeImages(
705 SvxConfigPageHelper::GetImageType(), aURLSeq );
707 // reset backup in entry
708 pEntry->SetBackupGraphic(
709 css::uno::Reference< css::graphic::XGraphic >() );
711 GetSaveInData()->PersistChanges(
712 GetSaveInData()->GetImageManager() );
714 m_xContentsListBox->remove(nActEntry);
716 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pEntry)));
717 m_xContentsListBox->insert(nActEntry, sId);
718 m_xContentsListBox->set_toggle(nActEntry,
719 pEntry->IsVisible() ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
720 InsertEntryIntoUI(pEntry, nActEntry, 1);
722 m_xContentsListBox->select(nActEntry);
723 m_xContentsListBox->scroll_to_row(nActEntry);
725 bNeedsApply = true;
727 catch ( css::uno::Exception& e )
729 SAL_WARN("cui.customize", "Error restoring image: " << e);
732 else
734 //This block should never be reached
735 SAL_WARN("cui.customize", "Unknown insert option: " << rIdent);
736 return;
739 if ( bNeedsApply )
741 static_cast<ToolbarSaveInData*>( GetSaveInData())->ApplyToolbar( pToolbar );
742 UpdateButtonStates();
746 IMPL_LINK_NOARG(SvxToolbarConfigPage, ResetToolbarHdl, weld::Button&, void)
748 sal_Int32 nSelectionPos = m_xTopLevelListBox->get_active();
750 SvxConfigEntry* pToolbar =
751 reinterpret_cast<SvxConfigEntry*>(m_xTopLevelListBox->get_id(nSelectionPos).toInt64());
753 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(GetDialogFrameWeld(),
754 VclMessageType::Question, VclButtonsType::YesNo,
755 CuiResId(RID_SVXSTR_CONFIRM_RESTORE_DEFAULT)));
756 if (xQueryBox->run() == RET_YES)
758 ToolbarSaveInData* pSaveInData =
759 static_cast<ToolbarSaveInData*>(GetSaveInData());
761 pSaveInData->RestoreToolbar( pToolbar );
763 SelectElement();
767 void SvxToolbarConfigPage::UpdateButtonStates()
769 SvxConfigEntry* pToolbar = GetTopLevelSelection();
770 int selection = m_xContentsListBox->get_selected_index();
772 bool bIsSeparator =
773 selection != -1 && reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(selection).toInt64())->IsSeparator();
774 bool bIsValidSelection =
775 !(m_xContentsListBox->n_children() == 0 || selection == -1);
777 m_xMoveUpButton->set_sensitive( bIsValidSelection );
778 m_xMoveDownButton->set_sensitive( bIsValidSelection );
780 m_xRemoveCommandButton->set_sensitive( bIsValidSelection );
782 m_xModifyBtn->set_sensitive( bIsValidSelection && !bIsSeparator );
784 // Handle the gear button
785 // "toolbar_gear_add" option is always enabled
786 m_xGearBtn->set_item_sensitive("toolbar_gear_delete", pToolbar && pToolbar->IsDeletable());
787 m_xGearBtn->set_item_sensitive("toolbar_gear_rename", pToolbar && pToolbar->IsRenamable());
790 short SvxToolbarConfigPage::QueryReset()
792 OUString msg = CuiResId( RID_SVXSTR_CONFIRM_TOOLBAR_RESET );
794 OUString saveInName = m_xSaveInListBox->get_active_text();
796 OUString label = SvxConfigPageHelper::replaceSaveInName( msg, saveInName );
798 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(GetDialogFrameWeld(),
799 VclMessageType::Question, VclButtonsType::YesNo,
800 label));
801 return xQueryBox->run();
804 void SvxToolbarConfigPage::SelectElement()
806 m_xContentsListBox->clear();
808 SvxConfigEntry* pToolbar = GetTopLevelSelection();
809 if ( pToolbar == nullptr )
811 //TODO: Disable related buttons
812 m_xInsertBtn->set_sensitive( false );
813 m_xResetBtn->set_sensitive( false );
814 m_xGearBtn->set_sensitive( false );
816 return;
818 else
820 m_xInsertBtn->set_sensitive(true);
821 m_xResetBtn->set_sensitive(true);
822 m_xGearBtn->set_sensitive(true);
825 switch (pToolbar->GetStyle())
827 case 0:
829 m_xGearBtn->set_item_active("toolbar_gear_iconOnly", true);
830 break;
832 case 1:
834 m_xGearBtn->set_item_active("toolbar_gear_textOnly", true);
835 break;
837 case 2:
839 m_xGearBtn->set_item_active("toolbar_gear_iconAndText", true);
840 break;
844 int i = 0;
845 SvxEntries* pEntries = pToolbar->GetEntries();
846 for (auto const& entry : *pEntries)
848 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(entry)));
849 m_xContentsListBox->insert(i, sId);
850 if (entry->IsBinding() && !entry->IsSeparator())
851 m_xContentsListBox->set_toggle(i, entry->IsVisible() ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
852 InsertEntryIntoUI(entry, i, 1);
853 ++i;
856 UpdateButtonStates();
859 void SvxToolbarConfigPage::AddFunction(int nTarget, bool bFront)
861 SvxConfigEntry* pToolbar = GetTopLevelSelection();
863 if (pToolbar == nullptr)
864 return;
866 // Add the command to the contents listbox of the selected toolbar
867 int nNewLBEntry =
868 SvxConfigPage::AddFunction(nTarget, bFront, true/*bAllowDuplicates*/);
870 if (nNewLBEntry == -1)
871 return;
873 SvxConfigEntry* pEntry = reinterpret_cast<SvxConfigEntry*>(m_xContentsListBox->get_id(nNewLBEntry).toInt64());
875 if ( pEntry->IsBinding() ) //TODO sep ?
877 pEntry->SetVisible(true);
878 m_xContentsListBox->set_toggle(nNewLBEntry, TRISTATE_TRUE, 0);
881 InsertEntryIntoUI(pEntry, nNewLBEntry, 1);
883 // Changes are not visible on the toolbar until this point
884 // TODO: Figure out a way to show the changes on the toolbar, but revert if
885 // the dialog is closed by pressing "Cancel"
886 // get currently selected toolbar and apply change
887 if ( pToolbar != nullptr )
889 static_cast<ToolbarSaveInData*>( GetSaveInData() )->ApplyToolbar( pToolbar );
893 SvxToolbarEntriesListBox::SvxToolbarEntriesListBox(std::unique_ptr<weld::TreeView> xParent, SvxToolbarConfigPage* pPg)
894 : SvxMenuEntriesListBox(std::move(xParent), pPg)
896 m_xControl->connect_toggled(LINK(this, SvxToolbarEntriesListBox, CheckButtonHdl));
897 m_xControl->connect_key_press(Link<const KeyEvent&, bool>()); //acknowledge we first remove the old one
898 m_xControl->connect_key_press(LINK(this, SvxToolbarEntriesListBox, KeyInputHdl)); // then add the new one
901 SvxToolbarEntriesListBox::~SvxToolbarEntriesListBox()
905 void SvxToolbarEntriesListBox::ChangedVisibility(int nRow)
907 SvxConfigEntry* pEntryData =
908 reinterpret_cast<SvxConfigEntry*>(m_xControl->get_id(nRow).toInt64());
910 if (pEntryData->IsBinding())
912 pEntryData->SetVisible(m_xControl->get_toggle(nRow, 0) == TRISTATE_TRUE);
914 SvxConfigEntry* pToolbar = pPage->GetTopLevelSelection();
916 ToolbarSaveInData* pToolbarSaveInData = static_cast<ToolbarSaveInData*>(
917 pPage->GetSaveInData() );
919 pToolbarSaveInData->ApplyToolbar( pToolbar );
923 IMPL_LINK(SvxToolbarEntriesListBox, CheckButtonHdl, const row_col&, rRowCol, void)
925 ChangedVisibility(rRowCol.first);
928 IMPL_LINK(SvxToolbarEntriesListBox, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
930 // space key will change visibility of toolbar items
931 if ( rKeyEvent.GetKeyCode() == KEY_SPACE )
933 int nRow = m_xControl->get_selected_index();
934 SvxConfigEntry* pEntryData = reinterpret_cast<SvxConfigEntry*>(m_xControl->get_id(nRow).toInt64());
935 if (pEntryData->IsBinding() && !pEntryData->IsSeparator())
937 m_xControl->set_toggle(nRow, m_xControl->get_toggle(nRow, 0) == TRISTATE_TRUE ? TRISTATE_FALSE : TRISTATE_TRUE, 0);
938 ChangedVisibility(nRow);
940 return true;
942 return SvxMenuEntriesListBox::KeyInputHdl(rKeyEvent);
945 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */