xref: /trunk/main/cui/source/dialogs/pastedlg.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 #include <com/sun/star/embed/Aspects.hpp>
31 
32 #include <pastedlg.hxx>
33 #include <svtools/svmedit.hxx>
34 #include <svtools/insdlg.hxx>
35 #include <vcl/dialog.hxx>
36 #include <vcl/button.hxx>
37 #include <vcl/fixed.hxx>
38 #include <vcl/group.hxx>
39 #include <vcl/lstbox.hxx>
40 #include <vcl/msgbox.hxx>
41 #include "svuidlg.hrc"
42 #include <sot/formats.hxx>
43 #include <sot/stg.hxx>
44 #include <svtools/sores.hxx>
45 #include <vcl/svapp.hxx>
46 
47 #include <dialmgr.hxx>
48 
49 SvPasteObjectDialog::SvPasteObjectDialog( Window* pParent )
50 
51     : ModalDialog( pParent, CUI_RES( MD_PASTE_OBJECT ) ),
52     aFtSource( this, CUI_RES( FT_SOURCE ) ),
53     aFtObjectSource( this, CUI_RES( FT_OBJECT_SOURCE ) ),
54     aRbPaste( this, CUI_RES( RB_PASTE ) ),
55     aRbPasteLink( this, CUI_RES( RB_PASTE_LINK ) ),
56     aCbDisplayAsIcon( this, CUI_RES( CB_DISPLAY_AS_ICON ) ),
57     aPbChangeIcon( this, CUI_RES( PB_CHANGE_ICON ) ),
58     aFlChoice( this, CUI_RES( FL_CHOICE ) ),
59     aLbInsertList( this, CUI_RES( LB_INSERT_LIST ) ),
60     aOKButton1( this, CUI_RES( 1 ) ),
61     aCancelButton1( this, CUI_RES( 1 ) ),
62     aHelpButton1( this, CUI_RES( 1 ) ),
63     aSObject( CUI_RES( S_OBJECT ) )
64 {
65     FreeResource();
66     SetHelpId( HID_PASTE_DLG );
67     SetUniqueId( HID_PASTE_DLG );
68 
69     Font aFont = aFtObjectSource.GetFont();
70     aFont.SetWeight( WEIGHT_LIGHT );
71     aFtObjectSource.SetFont( aFont );
72     aOKButton1.Disable();
73 
74     ObjectLB().SetSelectHdl( LINK( this, SvPasteObjectDialog, SelectHdl ) );
75     ObjectLB().SetDoubleClickHdl( LINK( this, SvPasteObjectDialog, DoubleClickHdl ) );
76     SetDefault();
77 
78     aLbInsertList.SetAccessibleName(aFlChoice.GetText());
79 }
80 
81 void SvPasteObjectDialog::SelectObject()
82 {
83     if ( aLbInsertList.GetEntryCount() &&
84          !aRbPaste.IsVisible() && !aRbPasteLink.IsVisible() )
85     {
86         aLbInsertList.SelectEntryPos(0);
87         SelectHdl( &aLbInsertList );
88     }
89 }
90 
91 IMPL_LINK( SvPasteObjectDialog, SelectHdl, ListBox *, pListBox )
92 {
93     (void)pListBox;
94 
95     if ( !aOKButton1.IsEnabled() )
96         aOKButton1.Enable();
97     return 0;
98 }
99 
100 IMPL_LINK_INLINE_START( SvPasteObjectDialog, DoubleClickHdl, ListBox *, pListBox )
101 {
102     (void)pListBox;
103 
104     EndDialog( RET_OK );
105     return 0;
106 }
107 IMPL_LINK_INLINE_END( SvPasteObjectDialog, DoubleClickHdl, ListBox *, pListBox )
108 
109 void SvPasteObjectDialog::SetDefault()
110 {
111     bLink   = sal_False;
112     nAspect = (sal_uInt16)::com::sun::star::embed::Aspects::MSOLE_CONTENT;
113 }
114 
115 SvPasteObjectDialog::~SvPasteObjectDialog()
116 {
117     void * pStr = aSupplementTable.First();
118     while( pStr )
119     {
120         delete (String *)pStr;
121         pStr = aSupplementTable.Next();
122     }
123 }
124 
125 /*************************************************************************
126 |*    SvPasteObjectDialog::Insert()
127 |*
128 |*    Beschreibung
129 |*    Ersterstellung    MM 14.06.94
130 |*    Letzte Aenderung  KA 16.03.2001
131 *************************************************************************/
132 void SvPasteObjectDialog::Insert( SotFormatStringId nFormat, const String& rFormatName )
133 {
134     String * pStr = new String( rFormatName );
135     if( !aSupplementTable.Insert( nFormat, pStr ) )
136         delete pStr;
137 }
138 
139 sal_uLong SvPasteObjectDialog::GetFormat( const TransferableDataHelper& rHelper,
140                                       const DataFlavorExVector* pFormats,
141                                       const TransferableObjectDescriptor* )
142 {
143     //TODO/LATER: why is the Descriptor never used?!
144     TransferableObjectDescriptor aDesc;
145     if( rHelper.HasFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR ) )
146         ((TransferableDataHelper&)rHelper).GetTransferableObjectDescriptor(
147                                 SOT_FORMATSTR_ID_OBJECTDESCRIPTOR, aDesc );
148     if ( !pFormats )
149         pFormats = &rHelper.GetDataFlavorExVector();
150 
151     //Dialogbox erzeugen und fuellen
152     String aSourceName, aTypeName;
153     sal_uLong nSelFormat = 0;
154     SvGlobalName aEmptyNm;
155 
156     ObjectLB().SetUpdateMode( sal_False );
157 
158     DataFlavorExVector::iterator aIter( ((DataFlavorExVector&)*pFormats).begin() ),
159                                  aEnd( ((DataFlavorExVector&)*pFormats).end() );
160     while( aIter != aEnd )
161     {
162         ::com::sun::star::datatransfer::DataFlavor aFlavor( *aIter );
163         SotFormatStringId nFormat = (*aIter++).mnSotId;
164 
165         String* pName = (String*) aSupplementTable.Get( nFormat );
166         String aName;
167 
168 #ifdef WNT
169 /*
170         if( !pName &&
171             ( nFormat == SOT_FORMATSTR_ID_EMBED_SOURCE_OLE || nFormat == SOT_FORMATSTR_ID_EMBEDDED_OBJ_OLE ) )
172         {
173             sal_Bool IsClipboardObject_Impl( SotDataObject * );
174             if( IsClipboardObject_Impl( pDataObj ) )
175             {
176                 IDataObject * pDO = NULL;
177                 OleGetClipboard( &pDO );
178                 if( pDO )
179                 {
180                     FORMATETC fe;
181                     STGMEDIUM stm;
182                     (fe).cfFormat=RegisterClipboardFormat( "Object Descriptor" );
183                     (fe).dwAspect=DVASPECT_CONTENT;
184                     (fe).ptd=NULL;
185                     (fe).tymed=TYMED_HGLOBAL;
186                     (fe).lindex=-1;
187 
188                     if (SUCCEEDED(pDO->GetData(&fe, &stm)))
189                     {
190                         LPOBJECTDESCRIPTOR pOD=(LPOBJECTDESCRIPTOR)GlobalLock(stm.hGlobal);
191                         if( pOD->dwFullUserTypeName )
192                         {
193                             OLECHAR * pN = (OLECHAR *)(((sal_uInt8 *)pOD) + pOD->dwFullUserTypeName);
194                             aName.Append( pN );
195                             pName = &aName;
196                             // set format to ole object
197                             nFormat = SOT_FORMATSTR_ID_EMBED_SOURCE_OLE;
198                         }
199                         if( pOD->dwSrcOfCopy )
200                         {
201                             OLECHAR * pN = (OLECHAR *)(((sal_uInt8 *)pOD) + pOD->dwSrcOfCopy);
202                             aSourceName.Append( *pN++ );
203                         }
204                         else
205                             aSourceName = String( ResId( STR_UNKNOWN_SOURCE, SOAPP->GetResMgr() ) );
206                         GlobalUnlock(stm.hGlobal);
207                         ReleaseStgMedium(&stm);
208                     }
209                 }
210             }
211         }
212 */
213 #endif
214 
215         // if there is an "Embed Source" or and "Embedded Object" on the
216         // Clipboard we read the Description and the Source of this object
217         // from an accompanied "Object Descriptor" format on the clipboard
218         // Remember: these formats mostly appear together on the clipboard
219         if ( !pName )
220         {
221             SvPasteObjectHelper::GetEmbeddedName(rHelper,aName,aSourceName,nFormat);
222             if ( aName.Len() )
223                 pName = &aName;
224         }
225 
226 
227         if( pName )
228         {
229             aName = *pName;
230 
231             if( SOT_FORMATSTR_ID_EMBED_SOURCE == nFormat )
232             {
233                 if( aDesc.maClassName != aEmptyNm )                                {
234                     aSourceName = aDesc.maDisplayName;
235 
236                     if( aDesc.maClassName == aObjClassName )
237                         aName = aObjName;
238                     else
239                         aName = aTypeName = aDesc.maTypeName;
240                 }
241             }
242             else if( SOT_FORMATSTR_ID_LINK_SOURCE == nFormat )
243             {
244                 PasteLink().Enable();
245                 continue;
246             }
247             else if( !aName.Len() )
248                 aName = SvPasteObjectHelper::GetSotFormatUIName( nFormat );
249 
250             if( LISTBOX_ENTRY_NOTFOUND == ObjectLB().GetEntryPos( aName ) )
251                 ObjectLB().SetEntryData(
252                     ObjectLB().InsertEntry( aName ), (void*) nFormat );
253         }
254     }
255 
256     if( !aTypeName.Len() && !aSourceName.Len() )
257     {
258         if( aDesc.maClassName != aEmptyNm )
259         {
260             aSourceName = aDesc.maDisplayName;
261             aTypeName = aDesc.maTypeName;
262         }
263 
264         if( !aTypeName.Len() && !aSourceName.Len() )
265         {
266             com::sun::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
267             ResMgr* pMgr = ResMgr::CreateResMgr( "svt", aLocale );
268             // global resource from svtools (former so3 resource)
269             if( pMgr )
270                 aSourceName = String( ResId( STR_UNKNOWN_SOURCE, *pMgr ) );
271             delete pMgr;
272         }
273     }
274 
275     ObjectLB().SetUpdateMode( sal_True );
276     SelectObject();
277 
278     if( aSourceName.Len() )
279     {
280         if( aTypeName.Len() )
281             aTypeName += '\n';
282 
283         aTypeName += aSourceName;
284         aTypeName.ConvertLineEnd();
285     }
286 
287     ObjectSource().SetText( aTypeName );
288 
289     SetDefault();
290 
291     if( Dialog::Execute() == RET_OK )
292     {
293         bLink = PasteLink().IsChecked();
294 
295         if( AsIconBox().IsChecked() )
296             nAspect = (sal_uInt16)com::sun::star::embed::Aspects::MSOLE_ICON;
297 
298         nSelFormat  = (sal_uLong)ObjectLB().GetEntryData( ObjectLB().GetSelectEntryPos() );
299     }
300 
301     return nSelFormat;
302 }
303 
304 void SvPasteObjectDialog::SetObjName( const SvGlobalName & rClass, const String & rObjName )
305 {
306     aObjClassName = rClass;
307     aObjName = rObjName;
308 }
309 
310