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/svapp.hxx>
23 #include <vcl/weld.hxx>
24 #include <vcl/window.hxx>
25 #include <rtlproto.hxx>
26 #include <memory>
27 
28 namespace {
29 
30 class 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 
44 public:
45     SvRTLInputBox(weld::Window* pParent, const OUString& rPrompt, const OUString& rTitle,
46         const OUString& rDefault, tools::Long nXTwips, tools::Long nYTwips );
GetText() const47     OUString const & GetText() const { return m_aText; }
48 };
49 
50 }
51 
SvRTLInputBox(weld::Window * pParent,const OUString & rPrompt,const OUString & rTitle,const OUString & rDefault,tools::Long nXTwips,tools::Long nYTwips)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, "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 
InitButtons()69 void SvRTLInputBox::InitButtons()
70 {
71     m_xOk->connect_clicked(LINK(this,SvRTLInputBox, OkHdl));
72     m_xCancel->connect_clicked(LINK(this,SvRTLInputBox,CancelHdl));
73 }
74 
PositionDialog(tools::Long nXTwips,tools::Long nYTwips)75 void SvRTLInputBox::PositionDialog(tools::Long nXTwips, tools::Long nYTwips)
76 {
77     if( nXTwips != -1 && nYTwips != -1 )
78     {
79         Point aDlgPosApp( nXTwips, nYTwips );
80         OutputDevice* pDefaultDevice = Application::GetDefaultDevice();
81         pDefaultDevice->Push(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 
SetPrompt(const OUString & rPrompt)89 void 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 
IMPL_LINK_NOARG(SvRTLInputBox,OkHdl,weld::Button &,void)97 IMPL_LINK_NOARG( SvRTLInputBox, OkHdl, weld::Button&, void )
98 {
99     m_aText = m_xEdit->get_text();
100     m_xDialog->response(RET_OK);
101 }
102 
IMPL_LINK_NOARG(SvRTLInputBox,CancelHdl,weld::Button &,void)103 IMPL_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 
SbRtl_InputBox(StarBASIC *,SbxArray & rPar,bool)111 void SbRtl_InputBox(StarBASIC *, SbxArray & rPar, bool)
112 {
113     sal_uInt32 nArgCount = rPar.Count();
114     if ( nArgCount < 2 )
115         StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
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             {
130                 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
131                 return;
132             }
133             nX = rPar.Get(4)->GetLong();
134             nY = rPar.Get(5)->GetLong();
135         }
136         vcl::Window* pParent = Application::GetDefDialogParent();
137         SvRTLInputBox aDlg(pParent ? pParent->GetFrameWeld() : nullptr,rPrompt,aTitle,aDefault,nX,nY);
138         aDlg.run();
139         rPar.Get(0)->PutString(aDlg.GetText());
140     }
141 }
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
144