Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / dialog / hexcolorcontrol.cxx
blobfbf6b9ea66d4076476f8dab97fc54ca25af1df32
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 <sax/tools/converter.hxx>
21 #include <svx/hexcolorcontrol.hxx>
22 #include <rtl/character.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/weld.hxx>
26 namespace weld
28 HexColorControl::HexColorControl(std::unique_ptr<weld::Entry> pEntry)
29 : m_xEntry(std::move(pEntry))
30 , m_nAsyncModifyEvent(nullptr)
32 m_xEntry->set_max_length(6);
33 m_xEntry->set_width_chars(6);
34 m_xEntry->connect_insert_text(LINK(this, HexColorControl, ImplProcessInputHdl));
35 m_xEntry->connect_changed(LINK(this, HexColorControl, ImplProcessModifyHdl));
38 HexColorControl::~HexColorControl()
40 if (m_nAsyncModifyEvent)
41 Application::RemoveUserEvent(m_nAsyncModifyEvent);
44 IMPL_LINK_NOARG(HexColorControl, OnAsyncModifyHdl, void*, void)
46 m_nAsyncModifyEvent = nullptr;
47 m_aModifyHdl.Call(*m_xEntry);
50 // tdf#123291 resend it async so it arrives after ImplProcessInputHdl has been
51 // processed
52 IMPL_LINK_NOARG(HexColorControl, ImplProcessModifyHdl, weld::Entry&, void)
54 if (m_nAsyncModifyEvent)
55 Application::RemoveUserEvent(m_nAsyncModifyEvent);
56 m_nAsyncModifyEvent = Application::PostUserEvent(LINK(this, HexColorControl, OnAsyncModifyHdl));
59 void HexColorControl::SetColor(Color nColor)
61 OUStringBuffer aBuffer;
62 sax::Converter::convertColor(aBuffer, nColor);
63 OUString sColor = aBuffer.makeStringAndClear().copy(1);
64 if (sColor == m_xEntry->get_text())
65 return;
66 int nStartPos, nEndPos;
67 m_xEntry->get_selection_bounds(nStartPos, nEndPos);
68 m_xEntry->set_text(sColor);
69 m_xEntry->select_region(nStartPos, nEndPos);
72 Color HexColorControl::GetColor() const
74 sal_Int32 nColor = -1;
76 OUString aStr = "#" + m_xEntry->get_text();
77 sal_Int32 nLen = aStr.getLength();
79 if (nLen < 7)
81 static const char* const pNullStr = "000000";
82 aStr += OUString::createFromAscii(&pNullStr[nLen - 1]);
85 sax::Converter::convertColor(nColor, aStr);
87 m_xEntry->set_message_type(nColor != -1 ? weld::EntryMessageType::Normal
88 : weld::EntryMessageType::Error);
90 return Color(ColorTransparency, nColor);
93 IMPL_STATIC_LINK(HexColorControl, ImplProcessInputHdl, OUString&, rTest, bool)
95 const sal_Unicode* pTest = rTest.getStr();
96 sal_Int32 nLen = rTest.getLength();
98 OUStringBuffer aFilter(nLen);
99 for (sal_Int32 i = 0; i < nLen; ++i)
101 if (rtl::isAsciiHexDigit(*pTest))
102 aFilter.append(*pTest);
103 ++pTest;
106 rTest = aFilter.makeStringAndClear();
107 return true;
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */