use insert function instead of for loop
[LibreOffice.git] / desktop / source / deployment / gui / license_dialog.cxx
blob24bf5e350d650077aef64842c573bd4352267d47
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 .
21 #include <comphelper/unwrapargs.hxx>
22 #include <vcl/event.hxx>
23 #include <vcl/idle.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/threadex.hxx>
26 #include <vcl/weld.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include "license_dialog.hxx"
31 #include <functional>
32 #include <string_view>
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
37 namespace dp_gui {
39 namespace {
41 struct LicenseDialogImpl : public weld::GenericDialogController
43 bool m_bLicenseRead;
44 Idle m_aResized;
45 AutoTimer m_aRepeat;
47 std::unique_ptr<weld::Label> m_xFtHead;
48 std::unique_ptr<weld::Widget> m_xArrow1;
49 std::unique_ptr<weld::Widget> m_xArrow2;
50 std::unique_ptr<weld::TextView> m_xLicense;
51 std::unique_ptr<weld::Button> m_xDown;
52 std::unique_ptr<weld::Button> m_xAcceptButton;
53 std::unique_ptr<weld::Button> m_xDeclineButton;
55 void PageDown();
56 DECL_LINK(ScrollTimerHdl, Timer*, void);
57 DECL_LINK(ScrolledHdl, weld::TextView&, void);
58 DECL_LINK(ResizedHdl, Timer*, void);
59 DECL_LINK(CancelHdl, weld::Button&, void);
60 DECL_LINK(AcceptHdl, weld::Button&, void);
61 DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
62 DECL_STATIC_LINK(LicenseDialogImpl, KeyReleaseHdl, const KeyEvent&, bool);
63 DECL_LINK(MousePressHdl, const MouseEvent&, bool);
64 DECL_LINK(MouseReleaseHdl, const MouseEvent&, bool);
65 DECL_LINK(SizeAllocHdl, const Size&, void);
67 LicenseDialogImpl(weld::Window * pParent,
68 std::u16string_view sExtensionName,
69 const OUString & sLicenseText);
71 bool IsEndReached() const;
76 LicenseDialogImpl::LicenseDialogImpl(
77 weld::Window * pParent,
78 std::u16string_view sExtensionName,
79 const OUString & sLicenseText)
80 : GenericDialogController(pParent, u"desktop/ui/licensedialog.ui"_ustr, u"LicenseDialog"_ustr)
81 , m_bLicenseRead(false)
82 , m_aResized("desktop LicenseDialogImpl m_aResized")
83 , m_aRepeat("LicenseDialogImpl m_aRepeat")
84 , m_xFtHead(m_xBuilder->weld_label(u"head"_ustr))
85 , m_xArrow1(m_xBuilder->weld_widget(u"arrow1"_ustr))
86 , m_xArrow2(m_xBuilder->weld_widget(u"arrow2"_ustr))
87 , m_xLicense(m_xBuilder->weld_text_view(u"textview"_ustr))
88 , m_xDown(m_xBuilder->weld_button(u"down"_ustr))
89 , m_xAcceptButton(m_xBuilder->weld_button(u"ok"_ustr))
90 , m_xDeclineButton(m_xBuilder->weld_button(u"cancel"_ustr))
92 m_xArrow1->show();
93 m_xArrow2->hide();
95 m_xLicense->connect_size_allocate(LINK(this, LicenseDialogImpl, SizeAllocHdl));
96 m_xLicense->set_size_request(m_xLicense->get_approximate_digit_width() * 72,
97 m_xLicense->get_height_rows(21));
99 m_xLicense->set_text(sLicenseText);
100 m_xFtHead->set_label(m_xFtHead->get_label() + "\n" + sExtensionName);
102 m_xAcceptButton->connect_clicked( LINK(this, LicenseDialogImpl, AcceptHdl) );
103 m_xDeclineButton->connect_clicked( LINK(this, LicenseDialogImpl, CancelHdl) );
105 m_xLicense->connect_vadjustment_changed(LINK(this, LicenseDialogImpl, ScrolledHdl));
106 m_xDown->connect_mouse_press(LINK(this, LicenseDialogImpl, MousePressHdl));
107 m_xDown->connect_mouse_release(LINK(this, LicenseDialogImpl, MouseReleaseHdl));
108 m_xDown->connect_key_press(LINK(this, LicenseDialogImpl, KeyInputHdl));
109 m_xDown->connect_key_release(LINK(this, LicenseDialogImpl, KeyReleaseHdl));
111 m_aRepeat.SetTimeout(Application::GetSettings().GetMouseSettings().GetButtonRepeat());
112 m_aRepeat.SetInvokeHandler(LINK(this, LicenseDialogImpl, ScrollTimerHdl));
114 m_aResized.SetPriority(TaskPriority::LOWEST);
115 m_aResized.SetInvokeHandler(LINK(this, LicenseDialogImpl, ResizedHdl));
118 IMPL_LINK_NOARG(LicenseDialogImpl, SizeAllocHdl, const Size&, void)
120 m_aResized.Start();
123 IMPL_LINK_NOARG(LicenseDialogImpl, AcceptHdl, weld::Button&, void)
125 m_xDialog->response(RET_OK);
128 IMPL_LINK_NOARG(LicenseDialogImpl, CancelHdl, weld::Button&, void)
130 m_xDialog->response(RET_CANCEL);
133 bool LicenseDialogImpl::IsEndReached() const
135 return m_xLicense->vadjustment_get_value() + m_xLicense->vadjustment_get_page_size() >= m_xLicense->vadjustment_get_upper();
138 IMPL_LINK_NOARG(LicenseDialogImpl, ScrolledHdl, weld::TextView&, void)
140 if (IsEndReached())
142 m_xDown->set_sensitive(false);
143 m_aRepeat.Stop();
145 if (!m_bLicenseRead)
147 m_xAcceptButton->set_sensitive(true);
148 m_xAcceptButton->grab_focus();
149 m_xArrow1->hide();
150 m_xArrow2->show();
151 m_bLicenseRead = true;
154 else
155 m_xDown->set_sensitive(true);
158 void LicenseDialogImpl::PageDown()
160 m_xLicense->vadjustment_set_value(m_xLicense->vadjustment_get_value() +
161 m_xLicense->vadjustment_get_page_size());
162 ScrolledHdl(*m_xLicense);
165 IMPL_LINK(LicenseDialogImpl, KeyInputHdl, const KeyEvent&, rKEvt, bool)
167 vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
168 if (aKeyCode.GetCode() == KEY_RETURN || aKeyCode.GetCode() == KEY_SPACE)
169 PageDown();
170 return false;
173 IMPL_LINK_NOARG(LicenseDialogImpl, ResizedHdl, Timer*, void)
175 ScrolledHdl(*m_xLicense);
178 IMPL_LINK_NOARG(LicenseDialogImpl, ScrollTimerHdl, Timer*, void)
180 PageDown();
183 IMPL_STATIC_LINK_NOARG(LicenseDialogImpl, KeyReleaseHdl, const KeyEvent&, bool)
185 return false;
188 IMPL_LINK_NOARG(LicenseDialogImpl, MousePressHdl, const MouseEvent&, bool)
190 PageDown();
191 m_aRepeat.Start();
192 return false;
195 IMPL_LINK_NOARG(LicenseDialogImpl, MouseReleaseHdl, const MouseEvent&, bool)
197 m_aRepeat.Stop();
198 return false;
201 LicenseDialog::LicenseDialog( Sequence<Any> const& args,
202 Reference<XComponentContext> const& )
204 comphelper::unwrapArgs( args, m_parent, m_sExtensionName, m_sLicenseText );
207 // XServiceInfo
208 OUString LicenseDialog::getImplementationName()
210 return u"com.sun.star.comp.deployment.ui.LicenseDialog"_ustr;
213 sal_Bool LicenseDialog::supportsService( const OUString& ServiceName )
215 return cppu::supportsService(this, ServiceName);
218 css::uno::Sequence< OUString > LicenseDialog::getSupportedServiceNames()
220 return { u"com.sun.star.deployment.ui.LicenseDialog"_ustr };
224 // XExecutableDialog
226 void LicenseDialog::setTitle( OUString const & )
230 sal_Int16 LicenseDialog::execute()
232 return vcl::solarthread::syncExecute(
233 std::bind(&LicenseDialog::solar_execute, this));
236 sal_Int16 LicenseDialog::solar_execute()
238 LicenseDialogImpl dlg(Application::GetFrameWeld(m_parent), m_sExtensionName, m_sLicenseText);
239 return dlg.run();
242 } // namespace dp_gui
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */