1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/mutex.hxx>
21 #include <tools/rcid.h>
22 #include <tools/wintypes.hxx>
23 #include <vcl/msgbox.hxx>
24 #include <vcl/svapp.hxx>
26 #include <svtools/ehdl.hxx>
27 #include <svtools/svtresid.hxx>
28 #include <svtools/svtools.hrc>
29 #include <svtools/sfxecode.hxx>
31 //=========================================================================
33 static sal_uInt16
aWndFunc(
34 Window
*pWin
, // Parent des Dialoges
36 const OUString
&rErr
, // Fehlertext
37 const OUString
&rAction
) // Actiontext
41 Bringt eine Fehlerbox auf den Schirm. Je nach nFlags werden
42 Error/ Info usw. Boxen mit den gewuenschten Buttons angezeigt
44 Rueckgabewert ist der gedrueckte Button
50 SolarMutexGuard aGuard
;
52 // aus den Flags die benoetigten WinBits ermitteln
54 if ( (ERRCODE_BUTTON_CANCEL
|ERRCODE_BUTTON_RETRY
) == (nFlags
& (ERRCODE_BUTTON_CANCEL
|ERRCODE_BUTTON_RETRY
)) )
55 eBits
= WB_RETRY_CANCEL
;
56 else if ( ERRCODE_BUTTON_OK_CANCEL
== (nFlags
& ERRCODE_BUTTON_OK_CANCEL
) )
58 else if ( ERRCODE_BUTTON_OK
== (nFlags
& ERRCODE_BUTTON_OK
) )
60 else if ( ERRCODE_BUTTON_YES_NO_CANCEL
== (nFlags
& ERRCODE_BUTTON_YES_NO_CANCEL
) )
61 eBits
= WB_YES_NO_CANCEL
;
62 else if ( ERRCODE_BUTTON_YES_NO
== (nFlags
& ERRCODE_BUTTON_YES_NO
) )
65 switch(nFlags
& 0x0f00)
67 case ERRCODE_BUTTON_DEF_OK
:
71 case ERRCODE_BUTTON_DEF_CANCEL
:
72 eBits
|= WB_DEF_CANCEL
;
75 case ERRCODE_BUTTON_DEF_YES
:
79 case ERRCODE_BUTTON_DEF_NO
:
84 String
aErr(SvtResId(STR_ERR_HDLMESS
).toString());
85 String
aAction(rAction
);
87 aAction
+= OUString(":\n");
88 aErr
.SearchAndReplace(OUString("$(ACTION)"), aAction
);
89 aErr
.SearchAndReplace(OUString("$(ERROR)"), rErr
);
92 switch ( nFlags
& 0xf000 )
94 case ERRCODE_MSG_ERROR
:
95 pBox
= new ErrorBox(pWin
, eBits
, aErr
);
98 case ERRCODE_MSG_WARNING
:
99 pBox
= new WarningBox(pWin
, eBits
, aErr
);
102 case ERRCODE_MSG_INFO
:
103 pBox
= new InfoBox(pWin
, aErr
);
106 case ERRCODE_MSG_QUERY
:
107 pBox
= new QueryBox(pWin
, eBits
, aErr
);
112 SAL_WARN( "svtools.misc", "no MessBox type");
114 return ERRCODE_BUTTON_OK
;
118 sal_uInt16 nRet
= RET_CANCEL
;
119 switch ( pBox
->Execute() )
122 nRet
= ERRCODE_BUTTON_OK
;
125 nRet
= ERRCODE_BUTTON_CANCEL
;
128 nRet
= ERRCODE_BUTTON_RETRY
;
131 nRet
= ERRCODE_BUTTON_YES
;
134 nRet
= ERRCODE_BUTTON_NO
;
137 SAL_WARN( "svtools.misc", "Unknown MessBox return value" );
144 //-------------------------------------------------------------------------
146 SfxErrorHandler::SfxErrorHandler(sal_uInt16 nIdP
, sal_uLong lStartP
, sal_uLong lEndP
, ResMgr
*pMgrP
) :
148 lStart(lStartP
), lEnd(lEndP
), nId(nIdP
), pMgr(pMgrP
), pFreeMgr( NULL
)
151 RegisterDisplay(&aWndFunc
);
154 pFreeMgr
= pMgr
= ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
158 //-------------------------------------------------------------------------
160 SfxErrorHandler::~SfxErrorHandler()
165 //-------------------------------------------------------------------------
167 sal_Bool
SfxErrorHandler::CreateString(
168 const ErrorInfo
*pErr
, OUString
&rStr
, sal_uInt16
& nFlags
) const
172 Der Fehlerstring fuer die ErrorInfo pErr wird zusammengesetzt.
177 sal_uLong nErrCode
= pErr
->GetErrorCode() & ERRCODE_ERROR_MASK
;
178 if( nErrCode
>=lEnd
|| nErrCode
<=lStart
)
180 MessageInfo
*pMsgInfo
=PTR_CAST(MessageInfo
,pErr
);
183 if(GetMessageString(nErrCode
, rStr
, nFlags
))
185 rStr
= rStr
.replaceAll("$(ARG1)", pMsgInfo
->GetMessageArg());
189 else if(GetErrorString(nErrCode
, rStr
, nFlags
))
191 StringErrorInfo
*pStringInfo
=PTR_CAST(StringErrorInfo
,pErr
);
194 rStr
= rStr
.replaceAll(OUString("$(ARG1)"),
195 pStringInfo
->GetErrorString());
199 TwoStringErrorInfo
* pTwoStringInfo
= PTR_CAST(TwoStringErrorInfo
,
203 rStr
= rStr
.replaceAll("$(ARG1)", pTwoStringInfo
->GetArg1());
204 rStr
= rStr
.replaceAll("$(ARG2)", pTwoStringInfo
->GetArg2());
212 //-------------------------------------------------------------------------
214 class ResString
: public String
218 Hilfsklasse zum Auslesen eines Strings und optionaler ExtraData aus
219 einer String Resource.
226 sal_uInt16
GetFlags() const {return nFlags
;}
227 const String
& GetString() const {return *this;}
228 ResString( ResId
&rId
);
231 //-------------------------------------------------------------------------
233 ResString::ResString(ResId
& rId
):
234 String(rId
.SetAutoRelease(sal_False
).toString()),
237 ResMgr
* pResMgr
= rId
.GetResMgr();
238 // String ctor temporarily sets global ResManager
239 if (pResMgr
->GetRemainSize())
240 nFlags
= sal_uInt16(pResMgr
->ReadShort());
241 rId
.SetAutoRelease(sal_True
);
242 pResMgr
->PopContext();
245 //-------------------------------------------------------------------------
247 struct ErrorResource_Impl
: private Resource
251 Hilfsklasse zum Zugriff auf String SubResourcen einer Resource
258 ErrorResource_Impl(ResId
& rErrIdP
, sal_uInt16 nId
)
259 : Resource(rErrIdP
),aResId(nId
,*rErrIdP
.GetResMgr()){}
261 ~ErrorResource_Impl() { FreeResource(); }
263 operator ResString(){ return ResString( aResId
); }
264 operator sal_Bool(){return IsAvailableRes(aResId
.SetRT(RSC_STRING
));}
269 sal_Bool
SfxErrorHandler::GetClassString(sal_uLong lClassId
, String
&rStr
) const
273 Erzeugt den String fuer die Klasse des Fehlers. Wird immer aus der
274 Resource des Sfx gelesen
279 sal_Bool bRet
= sal_False
;
280 ResMgr
* pResMgr
= ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
283 ResId
aId(RID_ERRHDL
, *pResMgr
);
284 ErrorResource_Impl
aEr(aId
, (sal_uInt16
)lClassId
);
287 rStr
=((ResString
)aEr
).GetString();
295 //-------------------------------------------------------------------------
297 sal_Bool
SfxErrorHandler::GetMessageString(
298 sal_uLong lErrId
, OUString
&rStr
, sal_uInt16
&nFlags
) const
302 Erzeugt den String fuer die Ausgabe in einer MessageBox
307 sal_Bool bRet
= sal_False
;
308 ResId
*pResId
= new ResId(nId
, *pMgr
);
310 ErrorResource_Impl
aEr(*pResId
, (sal_uInt16
)lErrId
);
313 ResString
aErrorString(aEr
);
314 sal_uInt16 nResFlags
= aErrorString
.GetFlags();
316 nFlags
=aErrorString
.GetFlags();
317 rStr
= aErrorString
.GetString();
325 //-------------------------------------------------------------------------
327 sal_Bool
SfxErrorHandler::GetErrorString(
328 sal_uLong lErrId
, OUString
&rStr
, sal_uInt16
&nFlags
) const
331 Erzeugt den Fehlerstring fuer den eigentlichen Fehler ohne
337 SolarMutexGuard aGuard
;
339 sal_Bool bRet
= sal_False
;
340 rStr
=SvtResId(RID_ERRHDL_CLASS
).toString();
341 ResId
aResId(nId
, *pMgr
);
344 ErrorResource_Impl
aEr(aResId
, (sal_uInt16
)lErrId
);
347 ResString
aErrorString(aEr
);
349 sal_uInt16 nResFlags
= aErrorString
.GetFlags();
352 rStr
= rStr
.replaceAll(OUString("$(ERROR)"), aErrorString
.GetString());
362 GetClassString(lErrId
& ERRCODE_CLASS_MASK
,
365 aErrStr
+= OUString(".\n");
366 rStr
= rStr
.replaceAll(OUString("$(CLASS)"),aErrStr
);
372 //-------------------------------------------------------------------------
374 SfxErrorContext::SfxErrorContext(
375 sal_uInt16 nCtxIdP
, Window
*pWindow
, sal_uInt16 nResIdP
, ResMgr
*pMgrP
)
376 : ErrorContext(pWindow
), nCtxId(nCtxIdP
), nResId(nResIdP
), pMgr(pMgrP
)
378 if( nResId
==USHRT_MAX
)
382 //-------------------------------------------------------------------------
384 SfxErrorContext::SfxErrorContext(
385 sal_uInt16 nCtxIdP
, const String
&aArg1P
, Window
*pWindow
,
386 sal_uInt16 nResIdP
, ResMgr
*pMgrP
)
387 : ErrorContext(pWindow
), nCtxId(nCtxIdP
), nResId(nResIdP
), pMgr(pMgrP
),
390 if( nResId
==USHRT_MAX
)
394 //-------------------------------------------------------------------------
396 sal_Bool
SfxErrorContext::GetString(sal_uLong nErrId
, OUString
&rStr
)
400 Baut die Beschreibung eines ErrorContextes auf
405 ResMgr
* pFreeMgr
= NULL
;
408 pFreeMgr
= pMgr
= ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
412 SolarMutexGuard aGuard
;
414 ResId
aResId( nResId
, *pMgr
);
416 ErrorResource_Impl
aTestEr( aResId
, nCtxId
);
419 rStr
= ( (ResString
)aTestEr
).GetString();
420 rStr
= rStr
.replaceAll(OUString("$(ARG1)"), aArg1
);
425 SAL_WARN( "svtools.misc", "ErrorContext cannot find the resource" );
431 sal_uInt16 nId
= ( nErrId
& ERRCODE_WARNING_MASK
) ? ERRCTX_WARNING
: ERRCTX_ERROR
;
432 ResId
aSfxResId( RID_ERRCTX
, *pMgr
);
433 ErrorResource_Impl
aEr( aSfxResId
, nId
);
434 rStr
= rStr
.replaceAll( OUString("$(ERR)"), ( (ResString
)aEr
).GetString() );
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */