Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / app / AppIconControl.cxx
blobbd94d52c564ba509e913fdb96396c73877ea998c
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 "AppIconControl.hxx"
21 #include <core_resource.hxx>
22 #include <strings.hrc>
23 #include <bitmaps.hlst>
24 #include <sfx2/thumbnailviewitem.hxx>
25 #include <vcl/bitmapex.hxx>
26 #include <vcl/event.hxx>
27 #include <vcl/i18nhelp.hxx>
28 #include <vcl/mnemonic.hxx>
29 #include <vcl/settings.hxx>
30 #include <vcl/svapp.hxx>
31 #include <callbacks.hxx>
32 #include <AppElementType.hxx>
34 namespace dbaui
36 class OApplicationIconControlDropTarget final : public DropTargetHelper
38 private:
39 OApplicationIconControl& m_rControl;
41 public:
42 OApplicationIconControlDropTarget(OApplicationIconControl& rControl)
43 : DropTargetHelper(rControl.GetDrawingArea()->get_drop_target())
44 , m_rControl(rControl)
48 virtual sal_Int8 AcceptDrop(const AcceptDropEvent& rEvt) override
50 return m_rControl.AcceptDrop(rEvt);
53 virtual sal_Int8 ExecuteDrop(const ExecuteDropEvent& rEvt) override
55 return m_rControl.ExecuteDrop(rEvt);
59 OApplicationIconControl::OApplicationIconControl(std::unique_ptr<weld::ScrolledWindow> xScroll)
60 : ThumbnailView(std::move(xScroll), nullptr)
61 , m_pActionListener(nullptr)
62 , m_nMaxWidth(0)
63 , m_nMaxHeight(0)
65 mnVItemSpace = 6; // row spacing
66 mbSelectOnFocus = false;
67 DrawMnemonics(true);
70 void OApplicationIconControl::Fill()
72 static constexpr struct CategoryDescriptor
74 TranslateId pLabelResId;
75 ElementType eType;
76 OUString aImageResId;
77 } aCategories[] = { { RID_STR_TABLES_CONTAINER, E_TABLE, BMP_TABLEFOLDER_TREE_L },
78 { RID_STR_QUERIES_CONTAINER, E_QUERY, BMP_QUERYFOLDER_TREE_L },
79 { RID_STR_FORMS_CONTAINER, E_FORM, BMP_FORMFOLDER_TREE_L },
80 { RID_STR_REPORTS_CONTAINER, E_REPORT, BMP_REPORTFOLDER_TREE_L } };
82 for (const CategoryDescriptor& aCategorie : aCategories)
84 // E_TABLE is 0, but 0 means void so use id of enum + 1
85 std::unique_ptr<ThumbnailViewItem> xItem(
86 new ThumbnailViewItem(*this, aCategorie.eType + 1));
87 xItem->mbBorder = false;
88 xItem->maPreview1 = BitmapEx(aCategorie.aImageResId);
89 const Size& rSize = xItem->maPreview1.GetSizePixel();
90 m_nMaxWidth = std::max(m_nMaxWidth, rSize.Width());
91 m_nMaxHeight = std::max(m_nMaxHeight, rSize.Height());
92 xItem->maTitle = DBA_RES(aCategorie.pLabelResId);
93 m_nMaxWidth = std::max<tools::Long>(m_nMaxWidth, GetTextWidth(xItem->maTitle));
94 AppendItem(std::move(xItem));
97 const int nMargin = 12;
98 const int nWidthRequest = m_nMaxWidth + 2 * nMargin;
99 set_size_request(nWidthRequest, -1);
100 // we expect a Resize at which point we'll set the item sizes based on our final size
103 ElementType OApplicationIconControl::GetSelectedItem() const
105 for (const auto& rItem : mItemList)
107 if (!rItem->mbSelected)
108 continue;
109 return static_cast<ElementType>(rItem->mnId - 1);
111 return E_NONE;
114 void OApplicationIconControl::createIconAutoMnemonics(MnemonicGenerator& rMnemonics)
116 for (const auto& rItem : mItemList)
117 rMnemonics.RegisterMnemonic(rItem->maTitle);
119 // exchange texts with generated mnemonics
120 for (auto& rItem : mItemList)
121 rItem->maTitle = rMnemonics.CreateMnemonic(rItem->maTitle);
124 void OApplicationIconControl::Resize()
126 // fill the full width of the allocated area and give two lines of space to
127 // center the title in
128 setItemDimensions(GetOutputSizePixel().Width(), m_nMaxHeight, GetTextHeight() * 2, 0);
129 ThumbnailView::Resize();
132 bool OApplicationIconControl::IsMnemonicChar(sal_Unicode cChar, ElementType& rType) const
134 bool bRet = false;
136 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
137 for (const auto& rItem : mItemList)
139 if (rI18nHelper.MatchMnemonic(rItem->maTitle, cChar))
141 bRet = true;
142 rType = static_cast<ElementType>(rItem->mnId - 1);
143 break;
147 return bRet;
150 bool OApplicationIconControl::DoKeyShortCut(const KeyEvent& rKEvt)
152 bool bMod2 = rKEvt.GetKeyCode().IsMod2();
153 sal_Unicode cChar = rKEvt.GetCharCode();
154 ElementType eType(E_NONE);
155 if (bMod2 && cChar && IsMnemonicChar(cChar, eType))
157 // shortcut is clicked
158 deselectItems();
159 SelectItem(eType + 1);
160 return true;
163 return false;
166 bool OApplicationIconControl::KeyInput(const KeyEvent& rKEvt)
168 return DoKeyShortCut(rKEvt) || ThumbnailView::KeyInput(rKEvt);
171 void OApplicationIconControl::SetDrawingArea(weld::DrawingArea* pDrawingArea)
173 ThumbnailView::SetDrawingArea(pDrawingArea);
174 m_xDropTarget.reset(new OApplicationIconControlDropTarget(*this));
177 sal_Int8 OApplicationIconControl::AcceptDrop(const AcceptDropEvent& rEvt)
179 sal_Int8 nDropOption = DND_ACTION_NONE;
180 if (m_pActionListener)
182 sal_uInt16 nEntry = GetItemId(rEvt.maPosPixel);
183 if (nEntry)
185 deselectItems();
186 SelectItem(nEntry);
187 nDropOption
188 = m_pActionListener->queryDrop(rEvt, m_xDropTarget->GetDataFlavorExVector());
191 return nDropOption;
194 sal_Int8 OApplicationIconControl::ExecuteDrop(const ExecuteDropEvent& rEvt)
196 if (m_pActionListener)
197 m_pActionListener->executeDrop(rEvt);
198 return DND_ACTION_NONE;
201 OApplicationIconControl::~OApplicationIconControl() {}
203 void OApplicationIconControl::GetFocus()
205 ThumbnailView::GetFocus();
206 Invalidate(); // redraw focus rect
209 void OApplicationIconControl::LoseFocus()
211 ThumbnailView::LoseFocus();
212 Invalidate(); // redraw focus rect
215 tools::Rectangle OApplicationIconControl::GetFocusRect()
217 if (HasFocus())
219 // Get the last selected item in the list
220 for (tools::Long i = mFilteredItemList.size() - 1; i >= 0; --i)
222 ThumbnailViewItem* pItem = mFilteredItemList[i];
223 if (pItem->isSelected())
225 tools::Rectangle aRet(pItem->getDrawArea());
226 aRet.AdjustLeft(THUMBNAILVIEW_ITEM_CORNER);
227 aRet.AdjustTop(1);
228 aRet.AdjustRight(-THUMBNAILVIEW_ITEM_CORNER);
229 aRet.AdjustBottom(-2);
230 return aRet;
234 return tools::Rectangle();
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */