LibreOffice Module sw (master)  1
tmplctrl.cxx
Go to the documentation of this file.
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 <svl/style.hxx>
21 #include <vcl/menu.hxx>
22 #include <svl/stritem.hxx>
23 #include <sfx2/dispatch.hxx>
24 #include <sfx2/viewfrm.hxx>
25 #include <vcl/commandevent.hxx>
26 #include <vcl/status.hxx>
27 
28 #include <swtypes.hxx>
29 #include <strings.hrc>
30 
31 #include <wrtsh.hxx>
32 #include <view.hxx>
33 #include <swmodule.hxx>
34 #include <cmdid.h>
35 #include <docsh.hxx>
36 #include <tmplctrl.hxx>
37 
39 
40 namespace {
41 
42 class SwTemplatePopup_Impl : public PopupMenu
43 {
44 public:
45  SwTemplatePopup_Impl();
46 
47  sal_uInt16 GetCurId() const { return nCurId; }
48 
49 private:
50  sal_uInt16 nCurId;
51 
52  virtual void Select() override;
53 };
54 
55 }
56 
57 SwTemplatePopup_Impl::SwTemplatePopup_Impl() :
58  PopupMenu(),
59  nCurId(USHRT_MAX)
60 {
61 }
62 
63 void SwTemplatePopup_Impl::Select()
64 {
65  nCurId = GetCurItemId();
66 }
67 
69  sal_uInt16 _nId,
70  StatusBar& rStb ) :
71  SfxStatusBarControl( _nSlotId, _nId, rStb )
72 {
73  GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_TMPLCTRL_HINT));
74 }
75 
77 {
78 }
79 
81  sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
82 {
83  const SfxStringItem* pItem = nullptr;
84  if (SfxItemState::DEFAULT == eState && (pItem = dynamic_cast<const SfxStringItem*>(pState)))
85  {
86  sTemplate = pItem->GetValue();
88  }
89  else
90  GetStatusBar().SetItemText(GetId(), OUString());
91 }
92 
94 {
95 }
96 
98 {
99  if ( rCEvt.GetCommand() != CommandEventId::ContextMenu ||
100  GetStatusBar().GetItemText( GetId() ).isEmpty())
101  return;
102 
104  {
105  SwView* pView = ::GetActiveView();
106  SwWrtShell *const pWrtShell(pView ? pView->GetWrtShellPtr() : nullptr);
107  if (nullptr != pWrtShell &&
108  !pWrtShell->SwCursorShell::HasSelection()&&
109  !pWrtShell->IsSelFrameMode() &&
110  !pWrtShell->IsObjSelected())
111  {
112  SfxStyleSheetBasePool* pPool = pView->GetDocShell()->
113  GetStyleSheetPool();
114  auto xIter = pPool->CreateIterator(SfxStyleFamily::Page);
115  if (xIter->Count() > 1)
116  {
117  sal_uInt16 nCount = 0;
118  SfxStyleSheetBase* pStyle = xIter->First();
119  while( pStyle )
120  {
121  aPop->InsertItem( ++nCount, pStyle->GetName() );
122  pStyle = xIter->Next();
123  }
124 
125  aPop->Execute( &GetStatusBar(), rCEvt.GetMousePosPixel());
126  const sal_uInt16 nCurrId = aPop->GetCurId();
127  if( nCurrId != USHRT_MAX)
128  {
129  // looks a bit awkward, but another way is not possible
130  pStyle = xIter->operator[]( nCurrId - 1 );
131  SfxStringItem aStyle( FN_SET_PAGE_STYLE, pStyle->GetName() );
132  pWrtShell->GetView().GetViewFrame()->GetDispatcher()->ExecuteList(
134  SfxCallMode::SLOT|SfxCallMode::RECORD,
135  { &aStyle });
136  }
137  }
138  }
139  }
140 }
141 
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwTemplateControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: tmplctrl.cxx:68
virtual std::unique_ptr< SfxStyleSheetIterator > CreateIterator(SfxStyleFamily, SfxStyleSearchBits nMask=SfxStyleSearchBits::All)
#define FN_SET_PAGE_STYLE
Definition: cmdid.h:338
SwView * GetActiveView()
Definition: swmodul1.cxx:116
const OUString & GetItemText(sal_uInt16 nItemId) const
virtual ~SwTemplateControl() override
Definition: tmplctrl.cxx:76
SFX_IMPL_STATUSBAR_CONTROL(SwTemplateControl, SfxStringItem)
void SetItemText(sal_uInt16 nItemId, const OUString &rText, int nCharsWidth=-1)
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
const OUString & GetName() const
int nCount
sal_uInt16 GetId() const
const OUString & GetValue() const
CommandEventId GetCommand() const
virtual void Paint(const UserDrawEvent &rEvt) override
Definition: tmplctrl.cxx:93
OUString SwResId(const char *pId)
Definition: swmodule.cxx:165
SwDocShell * GetDocShell()
Definition: view.cxx:1108
const Point & GetMousePosPixel() const
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
SwWrtShell * GetWrtShellPtr() const
Definition: view.hxx:399
SfxItemState
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
StatusBar & GetStatusBar() const
virtual void StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: tmplctrl.cxx:80
OUString sTemplate
Definition: tmplctrl.hxx:39
virtual void Command(const CommandEvent &rCEvt) override
Definition: tmplctrl.cxx:97
Definition: view.hxx:144
virtual void Select()