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 #ifndef INCLUDED_SVX_SOURCE_TBXCTRLS_COLORWINDOW_HXX
21 #define INCLUDED_SVX_SOURCE_TBXCTRLS_COLORWINDOW_HXX
22 
23 #include <svtools/toolbarmenu.hxx>
24 #include <rtl/ustring.hxx>
25 #include <svx/SvxColorValueSet.hxx>
26 #include <svx/Palette.hxx>
27 #include <vcl/lstbox.hxx>
28 #include <vcl/fixed.hxx>
29 
30 #include <functional>
31 
32 namespace com::sun::star::frame { class XFrame; }
33 
34 class PaletteManager;
35 
36 class SVX_DLLPUBLIC ColorStatus
37 {
38     Color maColor;
39     Color maTLBRColor;
40     Color maBLTRColor;
41 public:
42     ColorStatus();
43     ~ColorStatus();
44     void statusChanged( const css::frame::FeatureStateEvent& rEvent );
45     Color GetColor();
46 };
47 
48 class Button;
49 typedef std::function<void(const OUString&, const NamedColor&)> ColorSelectFunction;
50 
51 #define COL_NONE_COLOR    ::Color(0x80, 0xFF, 0xFF, 0xFF)
52 
53 class SVX_DLLPUBLIC SvxColorWindow final : public svtools::ToolbarPopup
54 {
55 private:
56     const sal_uInt16    theSlotId;
57     VclPtr<SvxColorValueSet>   mpColorSet;
58     VclPtr<SvxColorValueSet>   mpRecentColorSet;
59 
60     VclPtr<ListBox>     mpPaletteListBox;
61     VclPtr<PushButton>  mpButtonAutoColor;
62     VclPtr<PushButton>  mpButtonNoneColor;
63     VclPtr<PushButton>  mpButtonPicker;
64     VclPtr<FixedLine>   mpAutomaticSeparator;
65     OUString const      maCommand;
66     Link<const NamedColor&, void> maSelectedLink;
67 
68     VclPtr<vcl::Window> mxParentWindow;
69     std::shared_ptr<PaletteManager> mxPaletteManager;
70     ColorStatus&  mrColorStatus;
71 
72     ColorSelectFunction const maColorSelectFunction;
73     bool const mbReuseParentForPicker;
74 
75     DECL_LINK( SelectHdl, ValueSet*, void );
76     DECL_LINK( SelectPaletteHdl, ListBox&, void);
77     DECL_LINK( AutoColorClickHdl, Button*, void );
78     DECL_LINK( OpenPickerClickHdl, Button*, void );
79 
80     static bool SelectValueSetEntry(SvxColorValueSet* pColorSet, const Color& rColor);
81     static NamedColor GetSelectEntryColor(ValueSet const * pColorSet);
82     NamedColor GetAutoColor() const;
83 
84 public:
85     SvxColorWindow(const OUString& rCommand,
86                    std::shared_ptr<PaletteManager> const & rPaletteManager,
87                    ColorStatus& rColorStatus,
88                    sal_uInt16 nSlotId,
89                    const css::uno::Reference< css::frame::XFrame >& rFrame,
90                    vcl::Window* pParentWindow,
91                    // tdf#118251 When true, reuse pParentWindow as the parent of the color picker
92                    // that appears from the 'custom color' button. When false use the window of
93                    // rFrame. true is helpful when launched from a dialog, false for launched
94                    // from a toolbar
95                    bool bReuseParentForPicker,
96                    ColorSelectFunction const& rColorSelectFunction);
97     virtual ~SvxColorWindow() override;
98     virtual void        dispose() override;
99     void                ShowNoneButton();
100     void                StartSelection();
101     void                SetNoSelection();
102     bool                IsNoSelection() const;
103     void                SelectEntry(const NamedColor& rColor);
104     void                SelectEntry(const Color& rColor);
105     NamedColor          GetSelectEntryColor() const;
106 
107     virtual void        KeyInput( const KeyEvent& rKEvt ) override;
108     virtual void        statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
109 
SetSelectedHdl(const Link<const NamedColor &,void> & rLink)110     void SetSelectedHdl( const Link<const NamedColor&, void>& rLink ) { maSelectedLink = rLink; }
111 };
112 
113 class SVX_DLLPUBLIC MenuOrToolMenuButton
114 {
115 private:
116     // either
117     weld::MenuButton* m_pMenuButton;
118     // or
119     weld::Toolbar* m_pToolbar;
120     OString m_aIdent;
121 public:
122     MenuOrToolMenuButton(weld::MenuButton* pMenuButton);
123     MenuOrToolMenuButton(weld::Toolbar* pToolbar, const OString& rIdent);
124 
125     bool get_active() const;
126     void set_active(bool bActive) const;
127     weld::Widget* get_widget() const;
128 };
129 
130 class SVX_DLLPUBLIC ColorWindow final : public svtools::ToolbarPopupBase
131 {
132 private:
133     std::unique_ptr<weld::Builder> m_xBuilder;
134 
135     const sal_uInt16    theSlotId;
136     weld::Window* const mpParentWindow;
137     MenuOrToolMenuButton maMenuButton;
138     std::shared_ptr<PaletteManager> mxPaletteManager;
139     ColorStatus&  mrColorStatus;
140     ColorSelectFunction const maColorSelectFunction;
141 
142     std::unique_ptr<ColorValueSet> mxColorSet;
143     std::unique_ptr<ColorValueSet> mxRecentColorSet;
144     std::unique_ptr<weld::Container> mxTopLevel;
145     std::unique_ptr<weld::ComboBox> mxPaletteListBox;
146     std::unique_ptr<weld::Button> mxButtonAutoColor;
147     std::unique_ptr<weld::Button> mxButtonNoneColor;
148     std::unique_ptr<weld::Button> mxButtonPicker;
149     std::unique_ptr<weld::Widget> mxAutomaticSeparator;
150     std::unique_ptr<weld::CustomWeld> mxColorSetWin;
151     std::unique_ptr<weld::CustomWeld> mxRecentColorSetWin;
152     weld::Button* mpDefaultButton;
153 
154     DECL_LINK(SelectHdl, SvtValueSet*, void);
155     DECL_LINK(SelectPaletteHdl, weld::ComboBox&, void);
156     DECL_LINK(AutoColorClickHdl, weld::Button&, void);
157     DECL_LINK(OpenPickerClickHdl, weld::Button&, void);
158 
159     static bool SelectValueSetEntry(ColorValueSet* pColorSet, const Color& rColor);
160     static NamedColor GetSelectEntryColor(SvtValueSet const * pColorSet);
161     NamedColor GetAutoColor() const;
162 
163 public:
164     ColorWindow(std::shared_ptr<PaletteManager> const & rPaletteManager,
165                 ColorStatus& rColorStatus,
166                 sal_uInt16 nSlotId,
167                 const css::uno::Reference< css::frame::XFrame >& rFrame,
168                 weld::Window* pParentWindow, const MenuOrToolMenuButton &rMenuButton,
169                 ColorSelectFunction const& rColorSelectFunction);
GetWidget()170     weld::Container* GetWidget() { return mxTopLevel.get(); }
171     virtual ~ColorWindow() override;
172     void                ShowNoneButton();
173     void                SetNoSelection();
174     bool                IsNoSelection() const;
175     void                SelectEntry(const NamedColor& rColor);
176     void                SelectEntry(const Color& rColor);
177     NamedColor          GetSelectEntryColor() const;
178 
179     DECL_LINK(FocusHdl, weld::Widget&, void);
180 
181     virtual void        statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
182 
183 };
184 
185 #endif
186 
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
188