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 <svx/SvxPresetListBox.hxx>
21 #include <svx/xtable.hxx>
22 #include <vcl/commandevent.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/settings.hxx>
25 #include <vcl/image.hxx>
26 #include <vcl/menu.hxx>
27 #include <vcl/popupmenuwindow.hxx>
28 
SvxPresetListBox(std::unique_ptr<weld::ScrolledWindow> pWindow)29 SvxPresetListBox::SvxPresetListBox(std::unique_ptr<weld::ScrolledWindow> pWindow)
30     : SvtValueSet(std::move(pWindow))
31     , aIconSize(60, 64)
32 {
33     SetEdgeBlending(true);
34     SetExtraSpacing(4);
35 }
36 
Resize()37 void SvxPresetListBox::Resize()
38 {
39     DrawLayout();
40     WinBits aWinBits(GetStyle());
41     aWinBits |= WB_VSCROLL;
42     SetStyle(aWinBits);
43     SvtValueSet::Resize();
44 }
45 
Command(const CommandEvent & rEvent)46 bool SvxPresetListBox::Command(const CommandEvent& rEvent)
47 {
48     if (rEvent.GetCommand() != CommandEventId::ContextMenu)
49         return CustomWidgetController::Command(rEvent);
50     const sal_uInt16 nIndex = GetSelectedItemId();
51     if(nIndex > 0)
52     {
53         std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetDrawingArea(), "svx/ui/presetmenu.ui"));
54         std::unique_ptr<weld::Menu> xMenu(xBuilder->weld_menu("menu"));
55         OnMenuItemSelected(xMenu->popup_at_rect(GetDrawingArea(), tools::Rectangle(rEvent.GetMousePosPixel(), Size(1,1))));
56         return true;
57     }
58     return false;
59 }
60 
DrawLayout()61 void SvxPresetListBox::DrawLayout()
62 {
63     SetColCount(getColumnCount());
64     SetLineCount(5);
65 }
66 
67 template< typename ListType, typename EntryType >
FillPresetListBoxImpl(ListType & pList,sal_uInt32 nStartIndex)68 void SvxPresetListBox::FillPresetListBoxImpl(ListType & pList, sal_uInt32 nStartIndex)
69 {
70     const Size aSize( GetIconSize() );
71     BitmapEx aBitmap;
72     for(long nIndex = 0; nIndex < pList.Count(); nIndex++, nStartIndex++)
73     {
74         aBitmap = pList.GetBitmapForPreview(nIndex, aSize);
75         EntryType* pItem = static_cast<EntryType*>( pList.Get(nIndex) );
76         InsertItem(nStartIndex, Image(aBitmap), pItem->GetName());
77     }
78 }
79 
FillPresetListBox(XGradientList & pList,sal_uInt32 nStartIndex)80 void SvxPresetListBox::FillPresetListBox(XGradientList& pList, sal_uInt32 nStartIndex)
81 {
82     FillPresetListBoxImpl< XGradientList, XGradientEntry>( pList, nStartIndex );
83 }
84 
FillPresetListBox(XHatchList & pList,sal_uInt32 nStartIndex)85 void SvxPresetListBox::FillPresetListBox(XHatchList& pList, sal_uInt32 nStartIndex)
86 {
87     FillPresetListBoxImpl< XHatchList, XHatchEntry>( pList, nStartIndex );
88 }
89 
FillPresetListBox(XBitmapList & pList,sal_uInt32 nStartIndex)90 void SvxPresetListBox::FillPresetListBox(XBitmapList& pList, sal_uInt32 nStartIndex)
91 {
92     FillPresetListBoxImpl< XBitmapList, XBitmapEntry >( pList, nStartIndex );
93 }
94 
FillPresetListBox(XPatternList & pList,sal_uInt32 nStartIndex)95 void SvxPresetListBox::FillPresetListBox(XPatternList& pList, sal_uInt32 nStartIndex)
96 {
97     FillPresetListBoxImpl< XPatternList, XBitmapEntry >( pList, nStartIndex );
98 }
99 
OnMenuItemSelected(const OString & rIdent)100 void SvxPresetListBox::OnMenuItemSelected(const OString& rIdent)
101 {
102     if (rIdent == "rename")
103         maRenameHdl.Call(this);
104     else if (rIdent == "delete")
105         maDeleteHdl.Call(this);
106 }
107 
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
109