LibreOffice Module basic (master) 1
inputbox.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 <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>
27
28namespace {
29
30class SvRTLInputBox : public weld::GenericDialogController
31{
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;
37
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 );
43
44public:
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; }
48};
49
50}
51
52SvRTLInputBox::SvRTLInputBox(weld::Window* pParent, const OUString& rPrompt,
53 const OUString& rTitle, const OUString& rDefault,
54 tools::Long nXTwips, tools::Long nYTwips)
55 : GenericDialogController(pParent, "svt/ui/inputbox.ui", "InputBox")
56 , m_xEdit(m_xBuilder->weld_entry("entry"))
57 , m_xOk(m_xBuilder->weld_button("ok"))
58 , m_xCancel(m_xBuilder->weld_button("cancel"))
59 , m_xPromptText(m_xBuilder->weld_label("prompt"))
60{
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);
67}
68
69void SvRTLInputBox::InitButtons()
70{
71 m_xOk->connect_clicked(LINK(this,SvRTLInputBox, OkHdl));
72 m_xCancel->connect_clicked(LINK(this,SvRTLInputBox,CancelHdl));
73}
74
75void SvRTLInputBox::PositionDialog(tools::Long nXTwips, tools::Long nYTwips)
76{
77 if( nXTwips != -1 && nYTwips != -1 )
78 {
79 Point aDlgPosApp( nXTwips, nYTwips );
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());
86 }
87}
88
89void SvRTLInputBox::SetPrompt(const OUString& rPrompt)
90{
91 if (rPrompt.isEmpty())
92 return;
93 OUString aText_(convertLineEnd(rPrompt, LINEEND_CR));
94 m_xPromptText->set_label( aText_ );
95}
96
97IMPL_LINK_NOARG( SvRTLInputBox, OkHdl, weld::Button&, void )
98{
99 m_aText = m_xEdit->get_text();
100 m_xDialog->response(RET_OK);
101}
102
103IMPL_LINK_NOARG( SvRTLInputBox, CancelHdl, weld::Button&, void )
104{
105 m_aText.clear();
106 m_xDialog->response(RET_CANCEL);
107}
108
109// Syntax: String InputBox( Prompt, [Title], [Default] [, nXpos, nYpos ] )
110
111void SbRtl_InputBox(StarBASIC *, SbxArray & rPar, bool)
112{
113 sal_uInt32 nArgCount = rPar.Count();
114 if ( nArgCount < 2 )
116 else
117 {
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 )
127 {
128 if ( nArgCount != 6 )
129 {
131 return;
132 }
133 nX = rPar.Get(4)->GetLong();
134 nY = rPar.Get(5)->GetLong();
135 }
136 SvRTLInputBox aDlg(Application::GetDefDialogParent(), rPrompt, aTitle, aDefault, nX, nY);
137 aDlg.run();
138 rPar.Get(0)->PutString(aDlg.GetText());
139 }
140}
141
142/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
static OutputDevice * GetDefaultDevice()
static weld::Window * GetDefDialogParent()
void SetMapMode()
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
Definition: sbx.hxx:95
sal_uInt32 Count() const
Definition: sbxarray.cxx:87
SbxVariable * Get(sal_uInt32)
Definition: sbxarray.cxx:108
OUString GetOUString() const
Definition: sbxvalue.cxx:380
bool PutString(const OUString &)
Definition: sbxvalue.cxx:585
sal_Int32 GetLong() const
Definition: sbxvar.hxx:142
bool IsErr() const
Definition: sbxvar.hxx:123
static void Error(ErrCode, const OUString &rMsg={})
Definition: sb.cxx:1683
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
IMPL_LINK_NOARG(SvRTLInputBox, OkHdl, weld::Button &, void)
Definition: inputbox.cxx:97
void SbRtl_InputBox(StarBASIC *, SbxArray &rPar, bool)
Definition: inputbox.cxx:111
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
long Long
#define ERRCODE_BASIC_BAD_ARGUMENT
Definition: sberrors.hxx:26
RET_OK
RET_CANCEL