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 <emojicontrol.hxx>
21 #include <emojipopup.hxx>
22 #include <emojiview.hxx>
23 #include <sfx2/thumbnailviewitem.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <comphelper/propertysequence.hxx>
26 #include <comphelper/dispatchcommand.hxx>
27 #include <officecfg/Office/Common.hxx>
28
29 constexpr OStringLiteral FILTER_PEOPLE = "people";
30 constexpr OStringLiteral FILTER_NATURE = "nature";
31 constexpr OStringLiteral FILTER_FOOD = "food";
32 constexpr OStringLiteral FILTER_ACTIVITY = "activity";
33 constexpr OStringLiteral FILTER_TRAVEL = "travel";
34 constexpr OStringLiteral FILTER_OBJECTS = "objects";
35 constexpr OStringLiteral FILTER_SYMBOLS = "symbols";
36 constexpr OStringLiteral FILTER_FLAGS = "flags";
37 constexpr OStringLiteral FILTER_UNICODE9 = "unicode9";
38
39 using namespace com::sun::star;
40
SfxEmojiControl(const EmojiPopup * pControl,weld::Widget * pParent)41 SfxEmojiControl::SfxEmojiControl(const EmojiPopup* pControl, weld::Widget* pParent)
42 : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "sfx/ui/emojicontrol.ui", "emojictrl")
43 , mxPeopleBtn(m_xBuilder->weld_toggle_button(FILTER_PEOPLE))
44 , mxNatureBtn(m_xBuilder->weld_toggle_button(FILTER_NATURE))
45 , mxFoodBtn(m_xBuilder->weld_toggle_button(FILTER_FOOD))
46 , mxActivityBtn(m_xBuilder->weld_toggle_button(FILTER_ACTIVITY))
47 , mxTravelBtn(m_xBuilder->weld_toggle_button(FILTER_TRAVEL))
48 , mxObjectsBtn(m_xBuilder->weld_toggle_button(FILTER_OBJECTS))
49 , mxSymbolsBtn(m_xBuilder->weld_toggle_button(FILTER_SYMBOLS))
50 , mxFlagsBtn(m_xBuilder->weld_toggle_button(FILTER_FLAGS))
51 , mxUnicode9Btn(m_xBuilder->weld_toggle_button(FILTER_UNICODE9))
52 , mxEmojiView(new EmojiView(m_xBuilder->weld_scrolled_window("emoji_win", true)))
53 , mxEmojiWeld(new weld::CustomWeld(*m_xBuilder, "emoji_view", *mxEmojiView))
54 {
55 ConvertLabelToUnicode(*mxPeopleBtn);
56 ConvertLabelToUnicode(*mxNatureBtn);
57 ConvertLabelToUnicode(*mxFoodBtn);
58 ConvertLabelToUnicode(*mxActivityBtn);
59 ConvertLabelToUnicode(*mxTravelBtn);
60 ConvertLabelToUnicode(*mxObjectsBtn);
61 ConvertLabelToUnicode(*mxSymbolsBtn);
62 ConvertLabelToUnicode(*mxFlagsBtn);
63 ConvertLabelToUnicode(*mxUnicode9Btn);
64
65 mxPeopleBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
66 mxNatureBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
67 mxFoodBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
68 mxActivityBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
69 mxTravelBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
70 mxObjectsBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
71 mxSymbolsBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
72 mxFlagsBtn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
73 mxUnicode9Btn->connect_toggled(LINK(this, SfxEmojiControl, ActivatePageHdl));
74
75 mxEmojiView->setItemMaxTextLength(ITEM_MAX_TEXT_LENGTH);
76 mxEmojiView->setItemDimensions(ITEM_MAX_WIDTH, 0, ITEM_MAX_HEIGHT, ITEM_PADDING);
77
78 mxEmojiView->Populate();
79 ActivatePageHdl(*mxPeopleBtn);
80
81 mxEmojiView->setInsertEmojiHdl(LINK(this, SfxEmojiControl, InsertHdl));
82 mxEmojiView->ShowTooltips(true);
83 }
84
GrabFocus()85 void SfxEmojiControl::GrabFocus()
86 {
87 mxEmojiView->GrabFocus();
88 }
89
~SfxEmojiControl()90 SfxEmojiControl::~SfxEmojiControl()
91 {
92 }
93
ConvertLabelToUnicode(weld::ToggleButton & rBtn)94 void SfxEmojiControl::ConvertLabelToUnicode(weld::ToggleButton& rBtn)
95 {
96 OUStringBuffer sHexText;
97 OUString sLabel = rBtn.get_label();
98 sHexText.appendUtf32(sLabel.toUInt32(16));
99 rBtn.set_label(sHexText.toString());
100 }
101
getFilter(const weld::Toggleable & rCurPageId) const102 FILTER_CATEGORY SfxEmojiControl::getFilter(const weld::Toggleable& rCurPageId) const
103 {
104 if (&rCurPageId == mxPeopleBtn.get())
105 return FILTER_CATEGORY::PEOPLE;
106 else if (&rCurPageId == mxNatureBtn.get())
107 return FILTER_CATEGORY::NATURE;
108 else if (&rCurPageId == mxFoodBtn.get())
109 return FILTER_CATEGORY::FOOD;
110 else if (&rCurPageId == mxActivityBtn.get())
111 return FILTER_CATEGORY::ACTIVITY;
112 else if (&rCurPageId == mxTravelBtn.get())
113 return FILTER_CATEGORY::TRAVEL;
114 else if (&rCurPageId == mxObjectsBtn.get())
115 return FILTER_CATEGORY::OBJECTS;
116 else if (&rCurPageId == mxSymbolsBtn.get())
117 return FILTER_CATEGORY::SYMBOLS;
118 else if (&rCurPageId == mxFlagsBtn.get())
119 return FILTER_CATEGORY::FLAGS;
120 else if (&rCurPageId == mxUnicode9Btn.get())
121 return FILTER_CATEGORY::UNICODE9;
122
123 return FILTER_CATEGORY::PEOPLE;
124 }
125
IMPL_LINK(SfxEmojiControl,ActivatePageHdl,weld::Toggleable &,rButton,void)126 IMPL_LINK(SfxEmojiControl, ActivatePageHdl, weld::Toggleable&, rButton, void)
127 {
128 mxPeopleBtn->set_active(&rButton == mxPeopleBtn.get());
129 mxNatureBtn->set_active(&rButton == mxNatureBtn.get());
130 mxFoodBtn->set_active(&rButton == mxFoodBtn.get());
131 mxActivityBtn->set_active(&rButton == mxActivityBtn.get());
132 mxTravelBtn->set_active(&rButton == mxTravelBtn.get());
133 mxObjectsBtn->set_active(&rButton == mxObjectsBtn.get());
134 mxSymbolsBtn->set_active(&rButton == mxSymbolsBtn.get());
135 mxFlagsBtn->set_active(&rButton == mxFlagsBtn.get());
136 mxUnicode9Btn->set_active(&rButton == mxUnicode9Btn.get());
137
138 mxEmojiView->filterItems(ViewFilter_Category(getFilter(rButton)));
139 }
140
IMPL_STATIC_LINK(SfxEmojiControl,InsertHdl,ThumbnailViewItem *,pItem,void)141 IMPL_STATIC_LINK(SfxEmojiControl, InsertHdl, ThumbnailViewItem*, pItem, void)
142 {
143 const OUString& sHexText = pItem->getTitle();
144 sal_uInt32 cEmojiChar = sHexText.toUInt32(16);
145
146 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
147 OUString sFontName(officecfg::Office::Common::Misc::EmojiFont::get(xContext));
148
149 uno::Sequence<beans::PropertyValue> aArgs( comphelper::InitPropertySequence({
150 { "Symbols", uno::Any(OUString(&cEmojiChar, 1)) },
151 // add font settings here
152 { "FontName", uno::Any(sFontName) }
153 }));
154
155 comphelper::dispatchCommand(".uno:InsertSymbol", aArgs);
156 }
157
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
159