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 
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>
25 
26 namespace weld
27 {
28 HexColorControl::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 
44 IMPL_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
52 IMPL_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;
62  sax::Converter::convertColor(aBuffer, nColor);
63  OUString sColor = aBuffer.makeStringAndClear().copy(1);
64  if (sColor == m_xEntry->get_text())
65  return;
66  m_xEntry->set_text(sColor);
67 }
68 
70 {
71  sal_Int32 nColor = -1;
72 
73  OUString aStr = "#" + m_xEntry->get_text();
74  sal_Int32 nLen = aStr.getLength();
75 
76  if (nLen < 7)
77  {
78  static const char* const pNullStr = "000000";
79  aStr += OUString::createFromAscii(&pNullStr[nLen - 1]);
80  }
81 
82  sax::Converter::convertColor(nColor, aStr);
83 
84  m_xEntry->set_message_type(nColor != -1 ? weld::EntryMessageType::Normal
86 
87  return Color(nColor);
88 }
89 
90 IMPL_STATIC_LINK(HexColorControl, ImplProcessInputHdl, OUString&, rTest, bool)
91 {
92  const sal_Unicode* pTest = rTest.getStr();
93  sal_Int32 nLen = rTest.getLength();
94 
95  OUStringBuffer aFilter(nLen);
96  for (sal_Int32 i = 0; i < nLen; ++i)
97  {
98  if (rtl::isAsciiHexDigit(*pTest))
99  aFilter.append(*pTest);
100  ++pTest;
101  }
102 
103  rTest = aFilter.makeStringAndClear();
104  return true;
105 }
106 }
107 
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetColor(::Color nColor)
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
IMPL_LINK_NOARG(CustomWidgetController, DragBeginHdl, weld::DrawingArea &, bool)
sal_uInt16 sal_Unicode
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
int i
ImplSVEvent * m_nAsyncModifyEvent
IMPL_STATIC_LINK(HexColorControl, ImplProcessInputHdl, OUString &, rTest, bool)
std::unique_ptr< char[]> aBuffer
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
HexColorControl(std::unique_ptr< weld::Entry > pEdit)
::Color GetColor() const
std::unique_ptr< weld::Entry > m_xEntry
aStr