Update ooo320-m1
[ooovba.git] / svtools / source / misc / ehdl.cxx
blobbfb873014c3e57d8228566c425a5bb8a4e490a8d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ehdl.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <vos/mutex.hxx>
34 #include <tools/debug.hxx>
35 #include <tools/rcid.h>
36 #include <vcl/wintypes.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <vcl/svapp.hxx>
39 #if defined(OS2)
40 #include <vcl/sound.hxx>
41 #endif
43 #ifndef GCC
44 #endif
46 #include <svtools/ehdl.hxx>
47 #include <svtools/svtdata.hxx>
48 #include <svtools/svtools.hrc>
49 #include "sfxecode.hxx"
51 //=========================================================================
53 static USHORT aWndFunc(
54 Window *pWin, // Parent des Dialoges
55 USHORT nFlags,
56 const String &rErr, // Fehlertext
57 const String &rAction) // Actiontext
59 /* [Beschreibung]
61 Bringt eine Fehlerbox auf den Schirm. Je nach nFlags werden
62 Error/ Info usw. Boxen mit den gewuenschten Buttons angezeigt
64 Rueckgabewert ist der gedrueckte Button
70 NAMESPACE_VOS( OGuard ) aGuard( Application::GetSolarMutex() );
72 // aus den Flags die benoetigten WinBits ermitteln
73 WinBits eBits=0;
74 if ( (ERRCODE_BUTTON_CANCEL|ERRCODE_BUTTON_RETRY) == (nFlags & (ERRCODE_BUTTON_CANCEL|ERRCODE_BUTTON_RETRY)) )
75 eBits = WB_RETRY_CANCEL;
76 else if ( ERRCODE_BUTTON_OK_CANCEL == (nFlags & ERRCODE_BUTTON_OK_CANCEL) )
77 eBits = WB_OK_CANCEL;
78 else if ( ERRCODE_BUTTON_OK == (nFlags & ERRCODE_BUTTON_OK) )
79 eBits = WB_OK;
80 else if ( ERRCODE_BUTTON_YES_NO_CANCEL == (nFlags & ERRCODE_BUTTON_YES_NO_CANCEL) )
81 eBits = WB_YES_NO_CANCEL;
82 else if ( ERRCODE_BUTTON_YES_NO == (nFlags & ERRCODE_BUTTON_YES_NO) )
83 eBits = WB_YES_NO;
85 switch(nFlags & 0x0f00)
87 case ERRCODE_BUTTON_DEF_OK:
88 eBits |= WB_DEF_OK;
89 break;
91 case ERRCODE_BUTTON_DEF_CANCEL:
92 eBits |= WB_DEF_CANCEL;
93 break;
95 case ERRCODE_BUTTON_DEF_YES:
96 eBits |= WB_DEF_YES;
97 break;
99 case ERRCODE_BUTTON_DEF_NO:
100 eBits |= WB_DEF_NO;
101 break;
104 String aErr(SvtResId(STR_ERR_HDLMESS));
105 String aAction(rAction);
106 if ( aAction.Len() )
107 aAction += String::CreateFromAscii( ":\n" );
108 aErr.SearchAndReplace(String::CreateFromAscii( "$(ACTION)" ), aAction);
109 aErr.SearchAndReplace(String::CreateFromAscii( "$(ERROR)" ), rErr);
111 MessBox* pBox;
112 switch ( nFlags & 0xf000 )
114 case ERRCODE_MSG_ERROR:
115 pBox = new ErrorBox(pWin, eBits, aErr);
116 break;
118 case ERRCODE_MSG_WARNING:
119 pBox = new WarningBox(pWin, eBits, aErr);
120 break;
122 case ERRCODE_MSG_INFO:
123 pBox = new InfoBox(pWin, aErr);
124 break;
126 case ERRCODE_MSG_QUERY:
127 pBox = new QueryBox(pWin, eBits, aErr);
128 break;
130 default:
132 DBG_ERRORFILE( "no MessBox type");
133 pBox = NULL;
134 return ERRCODE_BUTTON_OK;
138 USHORT nRet = RET_CANCEL;
139 switch ( pBox->Execute() )
141 case RET_OK:
142 nRet = ERRCODE_BUTTON_OK;
143 break;
144 case RET_CANCEL:
145 nRet = ERRCODE_BUTTON_CANCEL;
146 break;
147 case RET_RETRY:
148 nRet = ERRCODE_BUTTON_RETRY;
149 break;
150 case RET_YES:
151 nRet = ERRCODE_BUTTON_YES;
152 break;
153 case RET_NO:
154 nRet = ERRCODE_BUTTON_NO;
155 break;
156 default:
157 DBG_ERRORFILE( "Unknown MessBox return value" );
158 break;
160 delete pBox;
161 return nRet;
164 //-------------------------------------------------------------------------
166 SfxErrorHandler::SfxErrorHandler(USHORT nIdP, ULONG lStartP, ULONG lEndP, ResMgr *pMgrP) :
168 lStart(lStartP), lEnd(lEndP), nId(nIdP), pMgr(pMgrP), pFreeMgr( NULL )
171 RegisterDisplay(&aWndFunc);
172 if( ! pMgr )
174 com::sun::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
175 pFreeMgr = pMgr = ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(ofa), aLocale );
179 //-------------------------------------------------------------------------
181 SfxErrorHandler::~SfxErrorHandler()
183 if( pFreeMgr )
184 delete pFreeMgr;
187 //-------------------------------------------------------------------------
189 BOOL SfxErrorHandler::CreateString(
190 const ErrorInfo *pErr, String &rStr, USHORT& nFlags) const
192 /* [Beschreibung]
194 Der Fehlerstring fuer die ErrorInfo pErr wird zusammengesetzt.
199 ULONG nErrCode = pErr->GetErrorCode() & ERRCODE_ERROR_MASK;
200 if( nErrCode>=lEnd || nErrCode<=lStart )
201 return FALSE;
202 MessageInfo *pMsgInfo=PTR_CAST(MessageInfo,pErr);
203 if(pMsgInfo)
205 if(GetMessageString(nErrCode, rStr, nFlags))
207 for (xub_StrLen i = 0; i < rStr.Len();)
209 i = rStr.SearchAndReplace(String::CreateFromAscii( "$(ARG1)" ),
210 pMsgInfo->GetMessageArg(), i);
211 if (i == STRING_NOTFOUND)
212 break;
213 i = i + pMsgInfo->GetMessageArg().Len();
215 return TRUE;
218 else if(GetErrorString(nErrCode, rStr, nFlags))
220 StringErrorInfo *pStringInfo=PTR_CAST(StringErrorInfo,pErr);
221 if(pStringInfo)
222 for (xub_StrLen i = 0; i < rStr.Len();)
224 i = rStr.SearchAndReplace(String::CreateFromAscii( "$(ARG1)" ),
225 pStringInfo->GetErrorString(), i);
226 if (i == STRING_NOTFOUND)
227 break;
228 i = i + pStringInfo->GetErrorString().Len();
230 else
232 TwoStringErrorInfo * pTwoStringInfo = PTR_CAST(TwoStringErrorInfo,
233 pErr);
234 if (pTwoStringInfo)
235 for (USHORT i = 0; i < rStr.Len();)
237 USHORT nArg1Pos = rStr.Search(String::CreateFromAscii( "$(ARG1)" ), i);
238 USHORT nArg2Pos = rStr.Search(String::CreateFromAscii( "$(ARG2)" ), i);
239 if (nArg1Pos < nArg2Pos)
241 rStr.Replace(nArg1Pos, 7, pTwoStringInfo->GetArg1());
242 i = nArg1Pos + pTwoStringInfo->GetArg1().Len();
244 else if (nArg2Pos < nArg1Pos)
246 rStr.Replace(nArg2Pos, 7, pTwoStringInfo->GetArg2());
247 i = nArg2Pos + pTwoStringInfo->GetArg2().Len();
249 else break;
252 return TRUE;
254 return FALSE;
257 //-------------------------------------------------------------------------
259 class ResString: public String
261 /* [Beschreibung]
263 Hilfsklasse zum Auslesen eines Strings und optionaler ExtraData aus
264 einer String Resource.
269 USHORT nFlags;
270 public:
271 USHORT GetFlags() const {return nFlags;}
272 const String & GetString() const {return *this;}
273 ResString( ResId &rId);
276 //-------------------------------------------------------------------------
278 ResString::ResString(ResId & rId):
279 String(rId.SetAutoRelease(FALSE)),
280 nFlags(0)
282 ResMgr * pResMgr = rId.GetResMgr();
283 // String ctor temporarily sets global ResManager
284 if (pResMgr->GetRemainSize())
285 nFlags = USHORT(pResMgr->ReadShort());
286 rId.SetAutoRelease(TRUE);
287 pResMgr->PopContext();
290 //-------------------------------------------------------------------------
292 struct ErrorResource_Impl : private Resource
294 /* [Beschreibung]
296 Hilfsklasse zum Zugriff auf String SubResourcen einer Resource
301 ResId aResId;
303 ErrorResource_Impl(ResId& rErrIdP, USHORT nId)
304 : Resource(rErrIdP),aResId(nId,*rErrIdP.GetResMgr()){}
306 ~ErrorResource_Impl() { FreeResource(); }
308 operator ResString(){ return ResString( aResId ); }
309 operator BOOL(){return IsAvailableRes(aResId.SetRT(RSC_STRING));}
314 BOOL SfxErrorHandler::GetClassString(ULONG lClassId, String &rStr) const
316 /* [Beschreibung]
318 Erzeugt den String fuer die Klasse des Fehlers. Wird immer aus der
319 Resource des Sfx gelesen
325 ResId aId(RID_ERRHDL, *pMgr);
326 ErrorResource_Impl aEr(aId, (USHORT)lClassId);
327 if(aEr)
329 rStr=((ResString)aEr).GetString();
330 return TRUE;
332 else
333 return FALSE;
336 //-------------------------------------------------------------------------
338 BOOL SfxErrorHandler::GetMessageString(
339 ULONG lErrId, String &rStr, USHORT &nFlags) const
341 /* [Beschreibung]
343 Erzeugt den String fuer die Ausgabe in einer MessageBox
348 BOOL bRet = FALSE;
349 ResId *pResId= new ResId(nId, *pMgr);
351 ErrorResource_Impl aEr(*pResId, (USHORT)lErrId);
352 if(aEr)
354 ResString aErrorString(aEr);
355 USHORT nResFlags = aErrorString.GetFlags();
356 if( nResFlags )
357 nFlags=aErrorString.GetFlags();
358 rStr = aErrorString.GetString();
359 bRet = TRUE;
362 delete pResId;
363 return bRet;
366 //-------------------------------------------------------------------------
368 BOOL SfxErrorHandler::GetErrorString(
369 ULONG lErrId, String &rStr, USHORT &nFlags) const
371 /* [Beschreibung]
372 Erzeugt den Fehlerstring fuer den eigentlichen Fehler ohne
373 dessen Klasse
378 NAMESPACE_VOS( OGuard ) aGuard( Application::GetSolarMutex() );
380 BOOL bRet = FALSE;
381 rStr=String(SvtResId(RID_ERRHDL_CLASS));
382 ResId *pResId = new ResId(nId, *pMgr);
385 ErrorResource_Impl aEr(*pResId, (USHORT)lErrId);
386 if(aEr)
388 ResString aErrorString(aEr);
390 USHORT nResFlags = aErrorString.GetFlags();
391 if ( nResFlags )
392 nFlags = nResFlags;
393 rStr.SearchAndReplace(
394 String::CreateFromAscii("$(ERROR)"), aErrorString.GetString());
395 bRet = TRUE;
397 else
398 bRet = FALSE;
401 if( bRet )
403 String aErrStr;
404 GetClassString(lErrId & ERRCODE_CLASS_MASK,
405 aErrStr);
406 if(aErrStr.Len())
407 aErrStr+=String::CreateFromAscii( ".\n" );
408 rStr.SearchAndReplace(String::CreateFromAscii( "$(CLASS)" ),aErrStr);
411 delete pResId;
412 return bRet;
415 //-------------------------------------------------------------------------
417 SfxErrorContext::SfxErrorContext(
418 USHORT nCtxIdP, Window *pWindow, USHORT nResIdP, ResMgr *pMgrP)
419 : ErrorContext(pWindow), nCtxId(nCtxIdP), nResId(nResIdP), pMgr(pMgrP)
421 if( nResId==USHRT_MAX )
422 nResId=RID_ERRCTX;
425 //-------------------------------------------------------------------------
427 SfxErrorContext::SfxErrorContext(
428 USHORT nCtxIdP, const String &aArg1P, Window *pWindow,
429 USHORT nResIdP, ResMgr *pMgrP)
430 : ErrorContext(pWindow), nCtxId(nCtxIdP), nResId(nResIdP), pMgr(pMgrP),
431 aArg1(aArg1P)
433 if( nResId==USHRT_MAX )
434 nResId=RID_ERRCTX;
437 //-------------------------------------------------------------------------
439 BOOL SfxErrorContext::GetString(ULONG nErrId, String &rStr)
441 /* [Beschreibung]
443 Baut die Beschreibung eines ErrorContextes auf
447 bool bRet = false;
448 ResMgr* pFreeMgr = NULL;
449 if( ! pMgr )
451 com::sun::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
452 pFreeMgr = pMgr = ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(ofa), aLocale );
454 if( pMgr )
456 NAMESPACE_VOS( OGuard ) aGuard( Application::GetSolarMutex() );
458 ResId aResId( nResId, *pMgr );
460 ErrorResource_Impl aTestEr( aResId, nCtxId );
461 if ( aTestEr )
463 rStr = ( (ResString)aTestEr ).GetString();
464 rStr.SearchAndReplace( String::CreateFromAscii( "$(ARG1)" ), aArg1 );
465 bRet = true;
467 else
469 DBG_ERRORFILE( "ErrorContext cannot find the resource" );
470 bRet = false;
473 if ( bRet )
475 USHORT nId = ( nErrId & ERRCODE_WARNING_MASK ) ? ERRCTX_WARNING : ERRCTX_ERROR;
476 ResId aSfxResId( RID_ERRCTX, *pMgr );
477 ErrorResource_Impl aEr( aSfxResId, nId );
478 rStr.SearchAndReplace( String::CreateFromAscii( "$(ERR)" ), ( (ResString)aEr ).GetString() );
482 if( pFreeMgr )
484 delete pFreeMgr;
485 pMgr = NULL;
487 return bRet;