| 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 <memory> |
| 21 |
#include <mutex> |
| 22 |
|
| 23 |
#include <unotools/resmgr.hxx> |
| 24 |
#include <vcl/svapp.hxx> |
| 25 |
#include <tools/debug.hxx> |
| 26 |
#include <vcl/weld.hxx> |
| 27 |
|
| 28 |
#include <basic/sbstar.hxx> |
| 29 |
#include <basic/basrdll.hxx> |
| 30 |
#include <strings.hrc> |
| 31 |
#include <sbxbase.hxx> |
| 32 |
#include <config_features.h> |
| 33 |
|
| 34 |
namespace |
| 35 |
{ |
| 36 |
struct BasicDLLImpl : public SvRefBase |
| 37 |
{ |
| 38 |
bool bDebugMode; |
| 39 |
bool bBreakEnabled; |
| 40 |
|
| 41 |
SbxAppData aSbxAppData; |
| 42 |
|
| 43 |
BasicDLLImpl() |
| 44 |
: bDebugMode(false) |
| 45 |
, bBreakEnabled(true) |
| 46 |
{ } |
| 47 |
|
| 48 |
static BasicDLLImpl* BASIC_DLL; |
| 49 |
static std::mutex& getMutex() |
| 50 |
{ |
| 51 |
static std::mutex aMutex; |
| 52 |
return aMutex; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
BasicDLLImpl* BasicDLLImpl::BASIC_DLL = nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
BasicDLL::BasicDLL() |
| 60 |
{ |
| 61 |
std::scoped_lock aGuard(BasicDLLImpl::getMutex()); |
| 62 |
if (!BasicDLLImpl::BASIC_DLL) |
| 63 |
BasicDLLImpl::BASIC_DLL = new BasicDLLImpl; |
| 64 |
m_xImpl = BasicDLLImpl::BASIC_DLL; |
| 65 |
} |
| 66 |
|
| 67 |
BasicDLL::~BasicDLL() |
| 68 |
{ |
| 69 |
std::scoped_lock aGuard(BasicDLLImpl::getMutex()); |
| 70 |
const bool bLastRef = m_xImpl->GetRefCount() == 1; |
| 71 |
if (bLastRef) { |
| 72 |
BasicDLLImpl::BASIC_DLL->aSbxAppData.m_aGlobErr.clear(); |
| 73 |
} |
| 74 |
m_xImpl.clear(); |
| 75 |
// only reset BASIC_DLL after the object had been destroyed |
| 76 |
if (bLastRef) |
| 77 |
BasicDLLImpl::BASIC_DLL = nullptr; |
| 78 |
} |
| 79 |
|
| 80 |
void BasicDLL::EnableBreak( bool bEnable ) |
| 81 |
{ |
| 82 |
DBG_ASSERT( BasicDLLImpl::BASIC_DLL, "BasicDLL::EnableBreak: No instance yet!" ); |
| 83 |
if (BasicDLLImpl::BASIC_DLL) |
| 84 |
{ |
| 85 |
BasicDLLImpl::BASIC_DLL->bBreakEnabled = bEnable; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void BasicDLL::SetDebugMode( bool bDebugMode ) |
| 90 |
{ |
| 91 |
DBG_ASSERT( BasicDLLImpl::BASIC_DLL, "BasicDLL::EnableBreak: No instance yet!" ); |
| 92 |
if (BasicDLLImpl::BASIC_DLL) |
| 93 |
{ |
| 94 |
BasicDLLImpl::BASIC_DLL->bDebugMode = bDebugMode; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
void BasicDLL::BasicBreak() |
| 100 |
{ |
| 101 |
DBG_ASSERT( BasicDLLImpl::BASIC_DLL, "BasicDLL::EnableBreak: No instance yet!" ); |
| 102 |
#if HAVE_FEATURE_SCRIPTING |
| 103 |
if (!BasicDLLImpl::BASIC_DLL) |
| 104 |
return; |
| 105 |
|
| 106 |
// bJustStopping: if there's someone pressing STOP like crazy umpteen times, |
| 107 |
// but the Basic doesn't stop early enough, the box might appear more often... |
| 108 |
static bool bJustStopping = false; |
| 109 |
if (StarBASIC::IsRunning() && !bJustStopping |
| 110 |
&& (BasicDLLImpl::BASIC_DLL->bBreakEnabled || BasicDLLImpl::BASIC_DLL->bDebugMode)) |
| 111 |
{ |
| 112 |
bJustStopping = true; |
| 113 |
StarBASIC::Stop(); |
| 114 |
std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(nullptr, |
| 115 |
VclMessageType::Info, VclButtonsType::Ok, |
| 116 |
BasResId(IDS_SBERR_TERMINATED))); |
| 117 |
xInfoBox->run(); |
| 118 |
bJustStopping = false; |
| 119 |
} |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
SbxAppData& GetSbxData_Impl() |
| 124 |
{ |
| 125 |
return BasicDLLImpl::BASIC_DLL->aSbxAppData; |
| 126 |
} |
| 127 |
|
| 128 |
bool IsSbxData_Impl() |
| 129 |
{ |
| 130 |
return BasicDLLImpl::BASIC_DLL; |
| 131 |
} |
| 132 |
|
| 133 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |