1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <vcl/commandinfoprovider.hxx>
23 #include <vcl/event.hxx>
24 #include <vcl/weld.hxx>
25 #include <vcl/svapp.hxx>
26 
27 #include <algorithm>
28 #include <cstddef>
29 
30 #include <helpids.h>
31 #include <strings.hrc>
32 
33 #include <cfg.hxx>
34 #include <SvxNotebookbarConfigPage.hxx>
35 #include <SvxConfigPageHelper.hxx>
36 #include <dialmgr.hxx>
37 #include <libxml/parser.h>
38 #include <osl/file.hxx>
39 #include <CustomNotebookbarGenerator.hxx>
40 #include <sfx2/notebookbar/SfxNotebookBar.hxx>
41 #include <unotools/configmgr.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <com/sun/star/frame/theUICommandDescription.hpp>
44 #include <com/sun/star/ui/ImageType.hpp>
45 
46 namespace uno = com::sun::star::uno;
47 namespace frame = com::sun::star::frame;
48 namespace lang = com::sun::star::lang;
49 namespace container = com::sun::star::container;
50 namespace beans = com::sun::star::beans;
51 namespace graphic = com::sun::star::graphic;
52 
isCategoryAvailable(const OUString & sClassId,const OUString & sUIItemId,const OUString & sActiveCategory,bool & isCategory)53 static bool isCategoryAvailable(const OUString& sClassId, const OUString& sUIItemId,
54                                 const OUString& sActiveCategory, bool& isCategory)
55 {
56     if (sUIItemId == sActiveCategory)
57         return true;
58     else if ((sClassId == "GtkMenu" || sClassId == "GtkGrid") && sUIItemId != sActiveCategory)
59     {
60         isCategory = false;
61         return false;
62     }
63     return false;
64 }
65 
charToString(const char * cString)66 static OUString charToString(const char* cString)
67 {
68     return OUString(cString, strlen(cString), RTL_TEXTENCODING_UTF8);
69 }
70 
getFileName(const OUString & aFileName)71 static OUString getFileName(const OUString& aFileName)
72 {
73     if (aFileName == "notebookbar.ui")
74         return "Tabbed";
75     else if (aFileName == "notebookbar_compact.ui")
76         return "TabbedCompact";
77     else if (aFileName == "notebookbar_groupedbar_full.ui")
78         return "Groupedbar";
79     else if (aFileName == "notebookbar_groupedbar_compact.ui")
80         return "GroupedbarCompact";
81     else
82         return "None";
83 }
84 
getModuleId(const OUString & sModuleName)85 static OUString getModuleId(const OUString& sModuleName)
86 {
87     if (sModuleName == "Writer")
88         return "com.sun.star.text.TextDocument";
89     else if (sModuleName == "Draw")
90         return "com.sun.star.drawing.DrawingDocument";
91     else if (sModuleName == "Impress")
92         return "com.sun.star.presentation.PresentationDocument";
93     else if (sModuleName == "Calc")
94         return "com.sun.star.sheet.SpreadsheetDocument";
95     else
96         return "None";
97 }
98 
SvxNotebookbarConfigPage(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet & rSet)99 SvxNotebookbarConfigPage::SvxNotebookbarConfigPage(weld::Container* pPage,
100                                                    weld::DialogController* pController,
101                                                    const SfxItemSet& rSet)
102     : SvxConfigPage(pPage, pController, rSet)
103 {
104     m_xCommandCategoryListBox->set_visible(false);
105     m_xDescriptionFieldLb->set_visible(false);
106     m_xSearchEdit->set_visible(false);
107     m_xDescriptionField->set_visible(false);
108     m_xMoveUpButton->set_visible(false);
109     m_xMoveDownButton->set_visible(false);
110     m_xAddCommandButton->set_visible(false);
111     m_xRemoveCommandButton->set_visible(false);
112     m_xLeftFunctionLabel->set_visible(false);
113     m_xSearchLabel->set_visible(false);
114     m_xCategoryLabel->set_visible(false);
115     m_xInsertBtn->set_visible(false);
116     m_xModifyBtn->set_visible(false);
117     m_xResetBtn->set_visible(false);
118     m_xCustomizeLabel->set_visible(false);
119 
120     weld::TreeView& rCommandCategoryBox = m_xFunctions->get_widget();
121     rCommandCategoryBox.hide();
122 
123     m_xContentsListBox.reset(
124         new SvxNotebookbarEntriesListBox(m_xBuilder->weld_tree_view("toolcontents"), this));
125     m_xDropTargetHelper.reset(
126         new SvxConfigPageFunctionDropTarget(*this, m_xContentsListBox->get_widget()));
127     std::vector<int> aWidths;
128     weld::TreeView& rTreeView = m_xContentsListBox->get_widget();
129     Size aSize(m_xFunctions->get_size_request());
130     rTreeView.set_size_request(aSize.Width(), aSize.Height());
131 
132     int nExpectedSize = 16;
133 
134     int nStandardImageColWidth = rTreeView.get_checkbox_column_width();
135     int nMargin = nStandardImageColWidth - nExpectedSize;
136     if (nMargin < 16)
137         nMargin = 16;
138 
139     if (SvxConfigPageHelper::GetImageType() & css::ui::ImageType::SIZE_LARGE)
140         nExpectedSize = 24;
141     else if (SvxConfigPageHelper::GetImageType() & css::ui::ImageType::SIZE_32)
142         nExpectedSize = 32;
143 
144     int nImageColWidth = nExpectedSize + nMargin;
145 
146     aWidths.push_back(nStandardImageColWidth);
147     aWidths.push_back(nImageColWidth);
148     rTreeView.set_column_fixed_widths(aWidths);
149 
150     rTreeView.set_hexpand(true);
151     rTreeView.set_vexpand(true);
152     rTreeView.set_help_id(HID_SVX_CONFIG_NOTEBOOKBAR_CONTENTS);
153     rTreeView.show();
154 }
155 
~SvxNotebookbarConfigPage()156 SvxNotebookbarConfigPage::~SvxNotebookbarConfigPage() {}
157 
DeleteSelectedTopLevel()158 void SvxNotebookbarConfigPage::DeleteSelectedTopLevel() {}
159 
DeleteSelectedContent()160 void SvxNotebookbarConfigPage::DeleteSelectedContent() {}
161 
Init()162 void SvxNotebookbarConfigPage::Init()
163 {
164     m_xTopLevelListBox->clear();
165     m_xContentsListBox->clear();
166     m_xSaveInListBox->clear();
167     CustomNotebookbarGenerator::createCustomizedUIFile();
168     OUString sNotebookbarInterface = getFileName(m_sFileName);
169 
170     OUString sScopeName
171         = utl::ConfigManager::getProductName() + " " + m_sAppName + " -  " + sNotebookbarInterface;
172     OUString sSaveInListBoxID = notebookbarTabScope;
173 
174     m_xSaveInListBox->append(sSaveInListBoxID, sScopeName);
175     m_xSaveInListBox->set_active_id(sSaveInListBoxID);
176 
177     m_xTopLevelListBox->append("NotebookBar", "All Commands");
178     m_xTopLevelListBox->set_active_id("NotebookBar");
179     SelectElement();
180 }
181 
CreateSaveInData(const css::uno::Reference<css::ui::XUIConfigurationManager> & xCfgMgr,const css::uno::Reference<css::ui::XUIConfigurationManager> & xParentCfgMgr,const OUString & aModuleId,bool bDocConfig)182 SaveInData* SvxNotebookbarConfigPage::CreateSaveInData(
183     const css::uno::Reference<css::ui::XUIConfigurationManager>& xCfgMgr,
184     const css::uno::Reference<css::ui::XUIConfigurationManager>& xParentCfgMgr,
185     const OUString& aModuleId, bool bDocConfig)
186 {
187     return static_cast<SaveInData*>(
188         new ToolbarSaveInData(xCfgMgr, xParentCfgMgr, aModuleId, bDocConfig));
189 }
190 
UpdateButtonStates()191 void SvxNotebookbarConfigPage::UpdateButtonStates() {}
192 
QueryReset()193 short SvxNotebookbarConfigPage::QueryReset()
194 {
195     OUString msg = CuiResId(RID_SVXSTR_CONFIRM_TOOLBAR_RESET);
196 
197     OUString saveInName = m_xSaveInListBox->get_active_text();
198 
199     OUString label = SvxConfigPageHelper::replaceSaveInName(msg, saveInName);
200 
201     std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(
202         GetFrameWeld(), VclMessageType::Question, VclButtonsType::YesNo, label));
203     int nValue = xQueryBox->run();
204     if (nValue == RET_YES)
205     {
206         OUString sOriginalUIPath = CustomNotebookbarGenerator::getOriginalUIPath();
207         OUString sCustomizedUIPath = CustomNotebookbarGenerator::getCustomizedUIPath();
208         osl::File::copy(sOriginalUIPath, sCustomizedUIPath);
209         OUString sNotebookbarInterface = getFileName(m_sFileName);
210         Sequence<OUString> sSequenceEntries;
211         CustomNotebookbarGenerator::setCustomizedUIItem(sSequenceEntries, sNotebookbarInterface);
212         OUString sUIPath = "modules/s" + m_sAppName.toAsciiLowerCase() + "/ui/";
213         sfx2::SfxNotebookBar::ReloadNotebookBar(sUIPath);
214     }
215     return nValue;
216 }
217 
InsertEntryIntoNotebookbarTabUI(const OUString & sClassId,const OUString & sUIItemId,const OUString & sUIItemCommand,weld::TreeView & rTreeView,weld::TreeIter & rIter,int nStartCol)218 void SvxConfigPage::InsertEntryIntoNotebookbarTabUI(const OUString& sClassId,
219                                                     const OUString& sUIItemId,
220                                                     const OUString& sUIItemCommand,
221                                                     weld::TreeView& rTreeView,
222                                                     weld::TreeIter& rIter, int nStartCol)
223 {
224     css::uno::Reference<css::container::XNameAccess> m_xCommandToLabelMap,
225         m_xGlobalCommandToLabelMap;
226     uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
227     uno::Reference<container::XNameAccess> xNameAccess(
228         css::frame::theUICommandDescription::get(xContext));
229 
230     uno::Sequence<beans::PropertyValue> aPropSeq, aGlobalPropSeq;
231 
232     xNameAccess->getByName("com.sun.star.text.GlobalDocument") >>= m_xGlobalCommandToLabelMap;
233     xNameAccess->getByName(getModuleId(m_sAppName)) >>= m_xCommandToLabelMap;
234 
235     try
236     {
237         uno::Any aModuleVal = m_xCommandToLabelMap->getByName(sUIItemCommand);
238 
239         aModuleVal >>= aPropSeq;
240     }
241     catch (container::NoSuchElementException&)
242     {
243     }
244 
245     try
246     {
247         uno::Any aGlobalVal = m_xGlobalCommandToLabelMap->getByName(sUIItemCommand);
248         aGlobalVal >>= aGlobalPropSeq;
249     }
250     catch (container::NoSuchElementException&)
251     {
252     }
253 
254     OUString aLabel;
255     for (sal_Int32 i = 0; i < aPropSeq.getLength(); ++i)
256         if (aPropSeq[i].Name == "Name")
257             aPropSeq[i].Value >>= aLabel;
258     if (aLabel.isEmpty())
259         for (sal_Int32 i = 0; i < aGlobalPropSeq.getLength(); ++i)
260             if (aGlobalPropSeq[i].Name == "Name")
261                 aGlobalPropSeq[i].Value >>= aLabel;
262 
263     OUString aName = SvxConfigPageHelper::stripHotKey(aLabel);
264 
265     if (sClassId == "GtkSeparatorMenuItem" || sClassId == "GtkSeparator")
266     {
267         OUString sDataInTree = "--------------------------------------------";
268         rTreeView.set_text(rIter, sDataInTree, nStartCol + 1);
269     }
270     else
271     {
272         if (aName.isEmpty())
273             aName = sUIItemId;
274         auto xImage = GetSaveInData()->GetImage(sUIItemCommand);
275         if (xImage.is())
276             rTreeView.set_image(rIter, xImage, nStartCol);
277         rTreeView.set_text(rIter, aName, nStartCol + 1);
278         rTreeView.set_id(rIter, sUIItemId);
279     }
280 }
281 
getNodeValue(xmlNode * pNodePtr,NotebookbarEntries & aNodeEntries)282 void SvxNotebookbarConfigPage::getNodeValue(xmlNode* pNodePtr, NotebookbarEntries& aNodeEntries)
283 {
284     pNodePtr = pNodePtr->xmlChildrenNode;
285     while (pNodePtr)
286     {
287         if (!(xmlStrcmp(pNodePtr->name, reinterpret_cast<const xmlChar*>("property"))))
288         {
289             xmlChar* UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("name"));
290             if (!(xmlStrcmp(UriValue, reinterpret_cast<const xmlChar*>("visible"))))
291             {
292                 xmlChar* aValue = xmlNodeGetContent(pNodePtr);
293                 const char* cVisibleValue = reinterpret_cast<const char*>(aValue);
294                 aNodeEntries.sVisibleValue = charToString(cVisibleValue);
295                 xmlFree(aValue);
296             }
297             if (!(xmlStrcmp(UriValue, reinterpret_cast<const xmlChar*>("action_name"))))
298             {
299                 xmlChar* aValue = xmlNodeGetContent(pNodePtr);
300                 const char* cActionName = reinterpret_cast<const char*>(aValue);
301                 aNodeEntries.sActionName = charToString(cActionName);
302                 xmlFree(aValue);
303             }
304             xmlFree(UriValue);
305         }
306         pNodePtr = pNodePtr->next;
307     }
308 }
309 
searchNodeandAttribute(std::vector<NotebookbarEntries> & aEntries,std::vector<CategoriesEntries> & aCategoryList,OUString & sActiveCategory,CategoriesEntries & aCurItemEntry,xmlNode * pNodePtr,bool isCategory)310 void SvxNotebookbarConfigPage::searchNodeandAttribute(std::vector<NotebookbarEntries>& aEntries,
311                                                       std::vector<CategoriesEntries>& aCategoryList,
312                                                       OUString& sActiveCategory,
313                                                       CategoriesEntries& aCurItemEntry,
314                                                       xmlNode* pNodePtr, bool isCategory)
315 {
316     pNodePtr = pNodePtr->xmlChildrenNode;
317     while (pNodePtr)
318     {
319         if (pNodePtr->type == XML_ELEMENT_NODE)
320         {
321             const char* cNodeName = reinterpret_cast<const char*>(pNodePtr->name);
322             if (strcmp(cNodeName, "object") == 0)
323             {
324                 OUString sSecondVal;
325 
326                 xmlChar* UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("id"));
327                 const char* cUIItemID = reinterpret_cast<const char*>(UriValue);
328                 OUString sUIItemId = charToString(cUIItemID);
329                 xmlFree(UriValue);
330 
331                 UriValue = xmlGetProp(pNodePtr, reinterpret_cast<const xmlChar*>("class"));
332                 const char* cClassId = reinterpret_cast<const char*>(UriValue);
333                 OUString sClassId = charToString(cClassId);
334                 xmlFree(UriValue);
335 
336                 CategoriesEntries aCategoryEntry;
337                 if (sClassId == "sfxlo-PriorityHBox")
338                 {
339                     aCategoryEntry.sDisplayName = sUIItemId;
340                     aCategoryEntry.sUIItemId = sUIItemId;
341                     aCategoryEntry.sClassType = sClassId;
342                     aCategoryList.push_back(aCategoryEntry);
343 
344                     aCurItemEntry = aCategoryEntry;
345                 }
346                 else if (sClassId == "sfxlo-PriorityMergedHBox")
347                 {
348                     aCategoryEntry.sDisplayName = aCurItemEntry.sDisplayName + " | " + sUIItemId;
349                     aCategoryEntry.sUIItemId = sUIItemId;
350                     aCategoryEntry.sClassType = sClassId;
351 
352                     if (aCurItemEntry.sClassType == sClassId)
353                     {
354                         sal_Int32 rPos = 0;
355                         aCategoryEntry.sDisplayName
356                             = aCurItemEntry.sDisplayName.getToken(rPos, ' ', rPos) + " | "
357                               + sUIItemId;
358                     }
359                     aCategoryList.push_back(aCategoryEntry);
360                     aCurItemEntry = aCategoryEntry;
361                 }
362                 else if (sClassId == "svtlo-ManagedMenuButton")
363                 {
364                     sal_Int32 rPos = 1;
365                     sSecondVal = sUIItemId.getToken(rPos, ':', rPos);
366                     if (!sSecondVal.isEmpty())
367                     {
368                         aCategoryEntry.sDisplayName
369                             = aCurItemEntry.sDisplayName + " | " + sSecondVal;
370                         aCategoryEntry.sUIItemId = sSecondVal;
371                         aCategoryList.push_back(aCategoryEntry);
372                     }
373                 }
374 
375                 NotebookbarEntries nodeEntries;
376                 if (isCategoryAvailable(sClassId, sUIItemId, sActiveCategory, isCategory)
377                     || isCategory)
378                 {
379                     isCategory = true;
380                     if (sClassId == "GtkMenuItem" || sClassId == "GtkToolButton"
381                         || sClassId == "GtkMenuToolButton"
382                         || (sClassId == "svtlo-ManagedMenuButton" && sSecondVal.isEmpty()))
383                     {
384                         nodeEntries.sClassId = sClassId;
385                         nodeEntries.sUIItemId = sUIItemId;
386                         nodeEntries.sDisplayName = sUIItemId;
387 
388                         getNodeValue(pNodePtr, nodeEntries);
389                         aEntries.push_back(nodeEntries);
390                     }
391                     else if (sClassId == "GtkSeparatorMenuItem" || sClassId == "GtkSeparator")
392                     {
393                         nodeEntries.sClassId = sClassId;
394                         nodeEntries.sUIItemId = sUIItemId;
395                         nodeEntries.sDisplayName = "Null";
396                         nodeEntries.sVisibleValue = "Null";
397                         nodeEntries.sActionName = "Null";
398                         aEntries.push_back(nodeEntries);
399                     }
400                     else if (sClassId == "sfxlo-PriorityHBox"
401                              || sClassId == "sfxlo-PriorityMergedHBox"
402                              || sClassId == "svtlo-ManagedMenuButton")
403                     {
404                         nodeEntries.sClassId = sClassId;
405                         nodeEntries.sUIItemId = sUIItemId;
406                         nodeEntries.sDisplayName
407                             = aCategoryList[aCategoryList.size() - 1].sDisplayName;
408                         nodeEntries.sVisibleValue = "Null";
409                         nodeEntries.sActionName = "Null";
410                         aEntries.push_back(nodeEntries);
411                     }
412                 }
413             }
414             searchNodeandAttribute(aEntries, aCategoryList, sActiveCategory, aCurItemEntry,
415                                    pNodePtr, isCategory);
416         }
417         pNodePtr = pNodePtr->next;
418     }
419 }
420 
FillFunctionsList(xmlNodePtr pRootNodePtr,std::vector<NotebookbarEntries> & aEntries,std::vector<CategoriesEntries> & aCategoryList,OUString & sActiveCategory)421 void SvxNotebookbarConfigPage::FillFunctionsList(xmlNodePtr pRootNodePtr,
422                                                  std::vector<NotebookbarEntries>& aEntries,
423                                                  std::vector<CategoriesEntries>& aCategoryList,
424                                                  OUString& sActiveCategory)
425 {
426     CategoriesEntries aCurItemEntry;
427     searchNodeandAttribute(aEntries, aCategoryList, sActiveCategory, aCurItemEntry, pRootNodePtr,
428                            false);
429 }
430 
SelectElement()431 void SvxNotebookbarConfigPage::SelectElement()
432 {
433     OString sUIFileUIPath = CustomNotebookbarGenerator::getSystemPath(
434         CustomNotebookbarGenerator::getCustomizedUIPath());
435     xmlDocPtr pDoc = xmlParseFile(sUIFileUIPath.getStr());
436     if (!pDoc)
437         return;
438     xmlNodePtr pNodePtr = xmlDocGetRootElement(pDoc);
439 
440     std::vector<NotebookbarEntries> aEntries;
441     std::vector<CategoriesEntries> aCategoryList;
442     OUString sActiveCategory = m_xTopLevelListBox->get_active_id();
443     FillFunctionsList(pNodePtr, aEntries, aCategoryList, sActiveCategory);
444 
445     if (m_xTopLevelListBox->get_count() == 1)
446     {
447         for (std::size_t nIdx = 0; nIdx < aCategoryList.size(); nIdx++)
448             m_xTopLevelListBox->append(aCategoryList[nIdx].sUIItemId,
449                                        aCategoryList[nIdx].sDisplayName);
450     }
451     unsigned long nStart = 0;
452     if (aEntries[nStart].sClassId == "sfxlo-PriorityHBox"
453         || aEntries[nStart].sClassId == "sfxlo-PriorityMergedHBox")
454         nStart = 1;
455 
456     std::vector<NotebookbarEntries> aTempEntries;
457     for (std::size_t nIdx = nStart; nIdx < aEntries.size(); nIdx++)
458     {
459         if (aEntries[nIdx].sClassId == "svtlo-ManagedMenuButton")
460         {
461             aTempEntries.push_back(aEntries[nIdx]);
462             std::vector<NotebookbarEntries> aGtkEntries;
463             sal_Int32 rPos = 1;
464             sActiveCategory = aEntries[nIdx].sUIItemId.getToken(rPos, ':', rPos);
465             FillFunctionsList(pNodePtr, aGtkEntries, aCategoryList, sActiveCategory);
466             for (std::size_t Idx = 0; Idx < aGtkEntries.size(); Idx++)
467                 aTempEntries.push_back(aGtkEntries[Idx]);
468             aGtkEntries.clear();
469         }
470         else
471             aTempEntries.push_back(aEntries[nIdx]);
472     }
473 
474     aEntries = aTempEntries;
475     aTempEntries.clear();
476 
477     weld::TreeView& rTreeView = m_xContentsListBox->get_widget();
478     rTreeView.bulk_insert_for_each(
479         aEntries.size(), [this, &rTreeView, &aEntries](weld::TreeIter& rIter, int nIdx) {
480             OUString sId(OUString::number(nIdx));
481             rTreeView.set_id(rIter, sId);
482             if (aEntries[nIdx].sActionName != "Null")
483             {
484                 if (aEntries[nIdx].sVisibleValue == "True")
485                 {
486                     rTreeView.set_toggle(rIter, TRISTATE_TRUE, 0);
487                 }
488                 else
489                 {
490                     rTreeView.set_toggle(rIter, TRISTATE_FALSE, 0);
491                 }
492             }
493             InsertEntryIntoNotebookbarTabUI(aEntries[nIdx].sClassId, aEntries[nIdx].sDisplayName,
494                                             aEntries[nIdx].sActionName, rTreeView, rIter, 1);
495         });
496 
497     aEntries.clear();
498 
499     xmlFreeDoc(pDoc);
500 }
501 
SvxNotebookbarEntriesListBox(std::unique_ptr<weld::TreeView> xParent,SvxConfigPage * pPg)502 SvxNotebookbarEntriesListBox::SvxNotebookbarEntriesListBox(std::unique_ptr<weld::TreeView> xParent,
503                                                            SvxConfigPage* pPg)
504     : SvxMenuEntriesListBox(std::move(xParent), pPg)
505 {
506     m_xControl->connect_toggled(LINK(this, SvxNotebookbarEntriesListBox, CheckButtonHdl));
507     m_xControl->connect_key_press(Link<const KeyEvent&, bool>());
508     m_xControl->connect_key_press(LINK(this, SvxNotebookbarEntriesListBox, KeyInputHdl));
509 }
510 
~SvxNotebookbarEntriesListBox()511 SvxNotebookbarEntriesListBox::~SvxNotebookbarEntriesListBox() {}
512 
EditRegistryFile(const OUString & sUIItemId,const OUString & sSetEntry,const OUString & sNotebookbarInterface)513 static void EditRegistryFile(const OUString& sUIItemId, const OUString& sSetEntry,
514                              const OUString& sNotebookbarInterface)
515 {
516     int nFlag = 0;
517     Sequence<OUString> aOldEntries
518         = CustomNotebookbarGenerator::getCustomizedUIItem(sNotebookbarInterface);
519     Sequence<OUString> aNewEntries(aOldEntries.getLength() + 1);
520     for (int nIdx = 0; nIdx < aOldEntries.getLength(); nIdx++)
521     {
522         sal_Int32 rPos = 0;
523         OUString sFirstValue = aOldEntries[nIdx].getToken(rPos, ',', rPos);
524         if (sFirstValue == sUIItemId)
525         {
526             aOldEntries[nIdx] = sSetEntry;
527             nFlag = 1;
528             break;
529         }
530         aNewEntries[nIdx] = aOldEntries[nIdx];
531     }
532 
533     if (nFlag == 0)
534     {
535         aNewEntries[aOldEntries.getLength()] = sSetEntry;
536         CustomNotebookbarGenerator::setCustomizedUIItem(aNewEntries, sNotebookbarInterface);
537     }
538     else
539     {
540         CustomNotebookbarGenerator::setCustomizedUIItem(aOldEntries, sNotebookbarInterface);
541     }
542 }
543 
ChangedVisibility(int nRow)544 void SvxNotebookbarEntriesListBox::ChangedVisibility(int nRow)
545 {
546     OUString sUIItemId = m_xControl->get_selected_id();
547     OUString sNotebookbarInterface = getFileName(m_pPage->GetFileName());
548 
549     OUString sVisible;
550     if (m_xControl->get_toggle(nRow, 0) == TRISTATE_TRUE)
551         sVisible = "True";
552     else
553         sVisible = "False";
554     OUString sSetEntries = sUIItemId + ",visible," + sVisible;
555     Sequence<OUString> sSeqOfEntries(1);
556     sSeqOfEntries[0] = sSetEntries;
557     EditRegistryFile(sUIItemId, sSetEntries, sNotebookbarInterface);
558     CustomNotebookbarGenerator::modifyCustomizedUIFile(sSeqOfEntries);
559     OUString sUIPath = "modules/s" + m_pPage->GetAppName().toAsciiLowerCase() + "/ui/";
560     sfx2::SfxNotebookBar::ReloadNotebookBar(sUIPath);
561 }
562 
IMPL_LINK(SvxNotebookbarEntriesListBox,CheckButtonHdl,const row_col &,rRowCol,void)563 IMPL_LINK(SvxNotebookbarEntriesListBox, CheckButtonHdl, const row_col&, rRowCol, void)
564 {
565     ChangedVisibility(rRowCol.first);
566 }
567 
IMPL_LINK(SvxNotebookbarEntriesListBox,KeyInputHdl,const KeyEvent &,rKeyEvent,bool)568 IMPL_LINK(SvxNotebookbarEntriesListBox, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
569 {
570     if (rKeyEvent.GetKeyCode() == KEY_SPACE)
571     {
572         int nRow = m_xControl->get_selected_index();
573         m_xControl->set_toggle(
574             nRow, m_xControl->get_toggle(nRow, 0) == TRISTATE_TRUE ? TRISTATE_FALSE : TRISTATE_TRUE,
575             0);
576         ChangedVisibility(nRow);
577         return true;
578     }
579     return SvxMenuEntriesListBox::KeyInputHdl(rKeyEvent);
580 }
581 
582 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
583