Update git submodules
[LibreOffice.git] / basic / source / runtime / inputbox.cxx
blob134c1a0759c70f124bf6d1f129535fa2ba2fd414
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 <basic/sberrors.hxx>
21 #include <tools/lineend.hxx>
22 #include <vcl/outdev.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/weld.hxx>
25 #include <rtlproto.hxx>
26 #include <memory>
28 namespace {
30 class SvRTLInputBox : public weld::GenericDialogController
32 std::unique_ptr<weld::Entry> m_xEdit;
33 std::unique_ptr<weld::Button> m_xOk;
34 std::unique_ptr<weld::Button> m_xCancel;
35 std::unique_ptr<weld::Label> m_xPromptText;
36 OUString m_aText;
38 void PositionDialog( tools::Long nXTwips, tools::Long nYTwips );
39 void InitButtons();
40 void SetPrompt(const OUString& rPrompt);
41 DECL_LINK( OkHdl, weld::Button&, void );
42 DECL_LINK( CancelHdl, weld::Button&, void );
44 public:
45 SvRTLInputBox(weld::Window* pParent, const OUString& rPrompt, const OUString& rTitle,
46 const OUString& rDefault, tools::Long nXTwips, tools::Long nYTwips );
47 OUString const & GetText() const { return m_aText; }
52 SvRTLInputBox::SvRTLInputBox(weld::Window* pParent, const OUString& rPrompt,
53 const OUString& rTitle, const OUString& rDefault,
54 tools::Long nXTwips, tools::Long nYTwips)
55 : GenericDialogController(pParent, u"svt/ui/inputbox.ui"_ustr, u"InputBox"_ustr)
56 , m_xEdit(m_xBuilder->weld_entry(u"entry"_ustr))
57 , m_xOk(m_xBuilder->weld_button(u"ok"_ustr))
58 , m_xCancel(m_xBuilder->weld_button(u"cancel"_ustr))
59 , m_xPromptText(m_xBuilder->weld_label(u"prompt"_ustr))
61 PositionDialog( nXTwips, nYTwips );
62 InitButtons();
63 SetPrompt(rPrompt);
64 m_xDialog->set_title(rTitle);
65 m_xEdit->set_text(rDefault);
66 m_xEdit->select_region(0, -1);
69 void SvRTLInputBox::InitButtons()
71 m_xOk->connect_clicked(LINK(this,SvRTLInputBox, OkHdl));
72 m_xCancel->connect_clicked(LINK(this,SvRTLInputBox,CancelHdl));
75 void SvRTLInputBox::PositionDialog(tools::Long nXTwips, tools::Long nYTwips)
77 if( nXTwips != -1 && nYTwips != -1 )
79 Point aDlgPosApp( nXTwips, nYTwips );
80 OutputDevice* pDefaultDevice = Application::GetDefaultDevice();
81 pDefaultDevice->Push(vcl::PushFlags::MAPMODE);
82 pDefaultDevice->SetMapMode(MapMode( MapUnit::MapAppFont));
83 aDlgPosApp = pDefaultDevice->LogicToPixel(aDlgPosApp, MapMode(MapUnit::MapTwip));
84 pDefaultDevice->Pop();
85 m_xDialog->window_move(aDlgPosApp.X(), aDlgPosApp.Y());
89 void SvRTLInputBox::SetPrompt(const OUString& rPrompt)
91 if (rPrompt.isEmpty())
92 return;
93 OUString aText_(convertLineEnd(rPrompt, LINEEND_CR));
94 m_xPromptText->set_label( aText_ );
97 IMPL_LINK_NOARG( SvRTLInputBox, OkHdl, weld::Button&, void )
99 m_aText = m_xEdit->get_text();
100 m_xDialog->response(RET_OK);
103 IMPL_LINK_NOARG( SvRTLInputBox, CancelHdl, weld::Button&, void )
105 m_aText.clear();
106 m_xDialog->response(RET_CANCEL);
109 // Syntax: String InputBox( Prompt, [Title], [Default] [, nXpos, nYpos ] )
111 void SbRtl_InputBox(StarBASIC *, SbxArray & rPar, bool)
113 sal_uInt32 nArgCount = rPar.Count();
114 if ( nArgCount < 2 )
115 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
116 else
118 OUString aTitle;
119 OUString aDefault;
120 sal_Int32 nX = -1, nY = -1; // center
121 const OUString& rPrompt = rPar.Get(1)->GetOUString();
122 if (nArgCount > 2 && !rPar.Get(2)->IsErr())
123 aTitle = rPar.Get(2)->GetOUString();
124 if (nArgCount > 3 && !rPar.Get(3)->IsErr())
125 aDefault = rPar.Get(3)->GetOUString();
126 if ( nArgCount > 4 )
128 if ( nArgCount != 6 )
130 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
131 return;
133 nX = rPar.Get(4)->GetLong();
134 nY = rPar.Get(5)->GetLong();
136 SvRTLInputBox aDlg(Application::GetDefDialogParent(), rPrompt, aTitle, aDefault, nX, nY);
137 aDlg.run();
138 rPar.Get(0)->PutString(aDlg.GetText());
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */