LibreOffice Module svx (master) 1
hexcolorcontrol.cxx
Go to the documentation of this file.
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
22#include <rtl/character.hxx>
23#include <vcl/svapp.hxx>
24#include <vcl/weld.hxx>
25
26namespace weld
27{
28HexColorControl::HexColorControl(std::unique_ptr<weld::Entry> pEntry)
29 : m_xEntry(std::move(pEntry))
30 , m_nAsyncModifyEvent(nullptr)
31{
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));
36}
37
39{
42}
43
44IMPL_LINK_NOARG(HexColorControl, OnAsyncModifyHdl, void*, void)
45{
46 m_nAsyncModifyEvent = nullptr;
47 m_aModifyHdl.Call(*m_xEntry);
48}
49
50// tdf#123291 resend it async so it arrives after ImplProcessInputHdl has been
51// processed
52IMPL_LINK_NOARG(HexColorControl, ImplProcessModifyHdl, weld::Entry&, void)
53{
54 if (m_nAsyncModifyEvent)
55 Application::RemoveUserEvent(m_nAsyncModifyEvent);
56 m_nAsyncModifyEvent = Application::PostUserEvent(LINK(this, HexColorControl, OnAsyncModifyHdl));
57}
58
60{
61 OUStringBuffer aBuffer;
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);
70}
71
73{
74 sal_Int32 nColor = -1;
75
76 OUString aStr = "#" + m_xEntry->get_text();
77 sal_Int32 nLen = aStr.getLength();
78
79 if (nLen < 7)
80 {
81 static const char* const pNullStr = "000000";
82 aStr += OUString::createFromAscii(&pNullStr[nLen - 1]);
83 }
84
86
87 m_xEntry->set_message_type(nColor != -1 ? weld::EntryMessageType::Normal
89
90 return Color(ColorTransparency, nColor);
91}
92
93IMPL_STATIC_LINK(HexColorControl, ImplProcessInputHdl, OUString&, rTest, bool)
94{
95 const sal_Unicode* pTest = rTest.getStr();
96 sal_Int32 nLen = rTest.getLength();
97
98 OUStringBuffer aFilter(nLen);
99 for (sal_Int32 i = 0; i < nLen; ++i)
100 {
101 if (rtl::isAsciiHexDigit(*pTest))
102 aFilter.append(*pTest);
103 ++pTest;
104 }
105
106 rTest = aFilter.makeStringAndClear();
107 return true;
108}
109}
110
111/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
void SetColor(::Color nColor)
std::unique_ptr< weld::Entry > m_xEntry
HexColorControl(std::unique_ptr< weld::Entry > pEdit)
ImplSVEvent * m_nAsyncModifyEvent
::Color GetColor() const
ColorTransparency
const OUStringLiteral sColor
aStr
int i
IMPL_STATIC_LINK(HexColorControl, ImplProcessInputHdl, OUString &, rTest, bool)
IMPL_LINK_NOARG(CustomWidgetController, DragBeginHdl, weld::DrawingArea &, bool)
sal_uInt16 sal_Unicode
std::unique_ptr< char[]> aBuffer