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 .
21 #include <vcl/svapp.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/dispatch.hxx>
24 #include <avmedia/mediaplayer.hxx>
26 #include "galbrws2.hxx"
27 #include "svx/galtheme.hxx"
28 #include "svx/galmisc.hxx"
29 #include "svx/galctrl.hxx"
30 #include "editeng/AccessibleStringWrap.hxx"
31 #include <editeng/svxfont.hxx>
33 #include <avmedia/mediawindow.hxx>
34 #include "gallery.hrc"
35 #include <vcl/graphicfilter.hxx>
37 #define GALLERY_BRWBOX_TITLE 1
38 #define GALLERY_BRWBOX_PATH 2
40 DBG_NAME(GalleryPreview
)
42 GalleryPreview::GalleryPreview( GalleryBrowser2
* pParent
, GalleryTheme
* pTheme
) :
43 Window( pParent
, WB_TABSTOP
| WB_BORDER
),
44 DropTargetHelper( this ),
45 DragSourceHelper( this ),
48 DBG_CTOR(GalleryPreview
,NULL
);
50 SetHelpId( HID_GALLERY_WINDOW
);
54 GalleryPreview::GalleryPreview( Window
* pParent
, const ResId
& rResId
) :
55 Window( pParent
, rResId
),
56 DropTargetHelper( this ),
57 DragSourceHelper( this ),
60 DBG_CTOR(GalleryPreview
,NULL
);
62 SetHelpId( HID_GALLERY_PREVIEW
);
66 GalleryPreview::~GalleryPreview()
68 DBG_DTOR(GalleryPreview
,NULL
);
72 bool GalleryPreview::SetGraphic( const INetURLObject
& _aURL
)
76 if( ::avmedia::MediaWindow::isMediaURL( _aURL
.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS
) ) )
78 aGraphic
= BitmapEx( GAL_RES( RID_SVXBMP_GALLERY_MEDIA
) );
82 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
83 GalleryProgress
aProgress( &rFilter
);
84 if( rFilter
.ImportGraphic( aGraphic
, _aURL
, GRFILTER_FORMAT_DONTKNOW
) )
88 SetGraphic( aGraphic
);
93 void GalleryPreview::InitSettings()
95 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
96 SetControlBackground( GALLERY_BG_COLOR
);
97 SetControlForeground( GALLERY_FG_COLOR
);
100 void GalleryPreview::DataChanged( const DataChangedEvent
& rDCEvt
)
102 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
105 Window::DataChanged( rDCEvt
);
108 sal_Bool
GalleryPreview::ImplGetGraphicCenterRect( const Graphic
& rGraphic
, Rectangle
& rResultRect
) const
110 const Size
aWinSize( GetOutputSizePixel() );
111 Size
aNewSize( LogicToPixel( rGraphic
.GetPrefSize(), rGraphic
.GetPrefMapMode() ) );
112 sal_Bool bRet
= sal_False
;
114 if( aNewSize
.Width() && aNewSize
.Height() )
116 // scale to fit window
117 const double fGrfWH
= (double) aNewSize
.Width() / aNewSize
.Height();
118 const double fWinWH
= (double) aWinSize
.Width() / aWinSize
.Height();
120 if ( fGrfWH
< fWinWH
)
122 aNewSize
.Width() = (long) ( aWinSize
.Height() * fGrfWH
);
123 aNewSize
.Height()= aWinSize
.Height();
127 aNewSize
.Width() = aWinSize
.Width();
128 aNewSize
.Height()= (long) ( aWinSize
.Width() / fGrfWH
);
131 const Point
aNewPos( ( aWinSize
.Width() - aNewSize
.Width() ) >> 1,
132 ( aWinSize
.Height() - aNewSize
.Height() ) >> 1 );
134 rResultRect
= Rectangle( aNewPos
, aNewSize
);
141 void GalleryPreview::Paint( const Rectangle
& rRect
)
143 Window::Paint( rRect
);
145 if( ImplGetGraphicCenterRect( aGraphicObj
.GetGraphic(), aPreviewRect
) )
147 const Point
aPos( aPreviewRect
.TopLeft() );
148 const Size
aSize( aPreviewRect
.GetSize() );
150 if( aGraphicObj
.IsAnimated() )
151 aGraphicObj
.StartAnimation( this, aPos
, aSize
);
153 aGraphicObj
.Draw( this, aPos
, aSize
);
157 void GalleryPreview::MouseButtonDown( const MouseEvent
& rMEvt
)
159 if( mpTheme
&& ( rMEvt
.GetClicks() == 2 ) )
160 ( (GalleryBrowser2
*) GetParent() )->TogglePreview( this );
163 void GalleryPreview::Command(const CommandEvent
& rCEvt
)
165 Window::Command( rCEvt
);
167 if( mpTheme
&& ( rCEvt
.GetCommand() == COMMAND_CONTEXTMENU
) )
168 ( (GalleryBrowser2
*) GetParent() )->ShowContextMenu( this,
169 ( rCEvt
.IsMouseEvent() ? &rCEvt
.GetMousePosPixel() : NULL
) );
172 void GalleryPreview::KeyInput( const KeyEvent
& rKEvt
)
176 GalleryBrowser2
* pBrowser
= static_cast< GalleryBrowser2
* >( GetParent() );
178 switch( rKEvt
.GetKeyCode().GetCode() )
180 case( KEY_BACKSPACE
):
181 pBrowser
->TogglePreview( this );
185 pBrowser
->Travel( GALLERYBROWSERTRAVEL_FIRST
);
189 pBrowser
->Travel( GALLERYBROWSERTRAVEL_LAST
);
194 pBrowser
->Travel( GALLERYBROWSERTRAVEL_PREVIOUS
);
199 pBrowser
->Travel( GALLERYBROWSERTRAVEL_NEXT
);
204 if( !pBrowser
->KeyInput( rKEvt
, this ) )
205 Window::KeyInput( rKEvt
);
211 Window::KeyInput( rKEvt
);
214 sal_Int8
GalleryPreview::AcceptDrop( const AcceptDropEvent
& rEvt
)
219 nRet
= ( (GalleryBrowser2
*) GetParent() )->AcceptDrop( *this, rEvt
);
221 nRet
= DND_ACTION_NONE
;
226 sal_Int8
GalleryPreview::ExecuteDrop( const ExecuteDropEvent
& rEvt
)
231 nRet
= ( (GalleryBrowser2
*) GetParent() )->ExecuteDrop( *this, rEvt
);
233 nRet
= DND_ACTION_NONE
;
238 void GalleryPreview::StartDrag( sal_Int8
, const Point
& )
241 ( (GalleryBrowser2
*) GetParent() )->StartDrag( this );
244 void GalleryPreview::PreviewMedia( const INetURLObject
& rURL
)
246 if( rURL
.GetProtocol() != INET_PROT_NOT_VALID
)
248 ::avmedia::MediaFloater
* pFloater
= AVMEDIA_MEDIAWINDOW();
252 SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER
, SFX_CALLMODE_SYNCHRON
);
253 pFloater
= AVMEDIA_MEDIAWINDOW();
257 pFloater
->setURL( rURL
.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS
), true );
261 void drawCheckered(OutputDevice
& rOut
, const Point
& rPos
, const Size
& rSize
)
263 // draw checkered background
264 static const sal_uInt32
nLen(8);
265 static const Color
aW(COL_WHITE
);
266 static const Color
aG(0xef, 0xef, 0xef);
268 rOut
.DrawCheckered(rPos
, rSize
, nLen
, aW
, aG
);
271 DBG_NAME(GalleryIconView
)
273 GalleryIconView::GalleryIconView( GalleryBrowser2
* pParent
, GalleryTheme
* pTheme
) :
274 ValueSet( pParent
, WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
| WB_ITEMBORDER
| WB_DOUBLEBORDER
| WB_VSCROLL
| WB_FLATVALUESET
),
275 DropTargetHelper( this ),
276 DragSourceHelper( this ),
279 DBG_CTOR(GalleryIconView
,NULL
);
281 EnableFullItemMode( sal_False
);
283 SetHelpId( HID_GALLERY_WINDOW
);
285 SetExtraSpacing( 2 );
286 SetItemWidth( S_THUMB
+ 6 );
287 SetItemHeight( S_THUMB
+ 6 );
290 GalleryIconView::~GalleryIconView()
292 DBG_DTOR(GalleryIconView
,NULL
);
295 void GalleryIconView::InitSettings()
297 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
298 SetControlBackground( GALLERY_BG_COLOR
);
299 SetControlForeground( GALLERY_FG_COLOR
);
300 SetColor( GALLERY_BG_COLOR
);
303 void GalleryIconView::DataChanged( const DataChangedEvent
& rDCEvt
)
305 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
308 ValueSet::DataChanged( rDCEvt
);
311 void GalleryIconView::UserDraw( const UserDrawEvent
& rUDEvt
)
313 const sal_uInt16 nId
= rUDEvt
.GetItemId();
317 SgaObject
* pObj
= mpTheme
->AcquireObject( nId
- 1 );
321 const Rectangle
& rRect
= rUDEvt
.GetRect();
322 OutputDevice
* pDev
= rUDEvt
.GetDevice();
324 bool bTransparent(false);
326 if( pObj
->IsThumbBitmap() )
330 if( pObj
->GetObjKind() == SGA_OBJ_SOUND
)
332 Bitmap aTemp
= pObj
->GetThumbBmp().GetBitmap();
334 aTemp
.Replace( COL_LIGHTMAGENTA
, COL_WHITE
);
335 aBitmapEx
= BitmapEx(aTemp
);
339 aBitmapEx
= pObj
->GetThumbBmp();
340 bTransparent
= aBitmapEx
.IsTransparent();
343 if( ( pDev
->GetBitCount() <= 8 ) && ( aBitmapEx
.GetBitCount() >= 8 ) )
345 aBitmapEx
.Dither( BMP_DITHER_FLOYD
);
348 aGraphic
= aBitmapEx
;
352 aGraphic
= pObj
->GetThumbMtf();
356 Size
aSize( aGraphic
.GetSizePixel( pDev
) );
358 if ( aSize
.Width() && aSize
.Height() )
360 if( ( aSize
.Width() > rRect
.GetWidth() ) || ( aSize
.Height() > rRect
.GetHeight() ) )
362 const double fBmpWH
= (double) aSize
.Width() / aSize
.Height();
363 const double fThmpWH
= (double) rRect
.GetWidth() / rRect
.GetHeight();
365 // Bitmap an Thumbgroesse anpassen
366 if ( fBmpWH
< fThmpWH
)
368 aSize
.Width() = (long) ( rRect
.GetHeight() * fBmpWH
);
369 aSize
.Height()= rRect
.GetHeight();
373 aSize
.Width() = rRect
.GetWidth();
374 aSize
.Height()= (long) ( rRect
.GetWidth() / fBmpWH
);
378 const Point
aPos( ( ( rRect
.GetWidth() - aSize
.Width() ) >> 1 ) + rRect
.Left(),
379 ( ( rRect
.GetHeight() - aSize
.Height() ) >> 1 ) + rRect
.Top() );
383 // draw checkered background for full rectangle.
384 drawCheckered(*pDev
, rRect
.TopLeft(), rRect
.GetSize());
387 aGraphic
.Draw( pDev
, aPos
, aSize
);
390 SetItemText( nId
, GalleryBrowser2::GetItemText( *mpTheme
, *pObj
, GALLERY_ITEM_TITLE
) );
391 mpTheme
->ReleaseObject( pObj
);
396 void GalleryIconView::MouseButtonDown( const MouseEvent
& rMEvt
)
398 ValueSet::MouseButtonDown( rMEvt
);
400 if( rMEvt
.GetClicks() == 2 )
401 ( (GalleryBrowser2
*) GetParent() )->TogglePreview( this, &rMEvt
.GetPosPixel() );
404 void GalleryIconView::Command( const CommandEvent
& rCEvt
)
406 ValueSet::Command( rCEvt
);
408 if( rCEvt
.GetCommand() == COMMAND_CONTEXTMENU
)
410 ( (GalleryBrowser2
*) GetParent() )->ShowContextMenu( this,
411 ( rCEvt
.IsMouseEvent() ? &rCEvt
.GetMousePosPixel() : NULL
) );
415 void GalleryIconView::KeyInput( const KeyEvent
& rKEvt
)
417 if( !mpTheme
|| !static_cast< GalleryBrowser2
* >( GetParent() )->KeyInput( rKEvt
, this ) )
418 ValueSet::KeyInput( rKEvt
);
421 sal_Int8
GalleryIconView::AcceptDrop( const AcceptDropEvent
& rEvt
)
423 return( static_cast< GalleryBrowser2
* >( GetParent() )->AcceptDrop( *this, rEvt
) );
426 sal_Int8
GalleryIconView::ExecuteDrop( const ExecuteDropEvent
& rEvt
)
428 return( static_cast< GalleryBrowser2
* >( GetParent() )->ExecuteDrop( *this, rEvt
) );
431 void GalleryIconView::StartDrag( sal_Int8
, const Point
& )
433 const CommandEvent
aEvt( GetPointerPosPixel(), COMMAND_STARTDRAG
, sal_True
);
436 // call this to initiate dragging for ValueSet
437 ValueSet::StartDrag( aEvt
, aRegion
);
438 static_cast< GalleryBrowser2
* >( GetParent() )->StartDrag( this );
441 DBG_NAME(GalleryListView
)
443 GalleryListView::GalleryListView( GalleryBrowser2
* pParent
, GalleryTheme
* pTheme
) :
444 BrowseBox( pParent
, WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
),
448 DBG_CTOR(GalleryListView
,NULL
);
450 SetHelpId( HID_GALLERY_WINDOW
);
454 SetMode( BROWSER_AUTO_VSCROLL
| BROWSER_AUTOSIZE_LASTCOL
);
455 SetDataRowHeight( 28 );
456 InsertDataColumn( GALLERY_BRWBOX_TITLE
, GAL_RESSTR(RID_SVXSTR_GALLERY_TITLE
), 256 );
457 InsertDataColumn( GALLERY_BRWBOX_PATH
, GAL_RESSTR(RID_SVXSTR_GALLERY_PATH
), 256 );
460 GalleryListView::~GalleryListView()
462 DBG_DTOR(GalleryListView
,NULL
);
465 void GalleryListView::InitSettings()
467 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
468 SetControlBackground( GALLERY_BG_COLOR
);
469 SetControlForeground( GALLERY_FG_COLOR
);
472 void GalleryListView::DataChanged( const DataChangedEvent
& rDCEvt
)
474 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
477 BrowseBox::DataChanged( rDCEvt
);
480 sal_Bool
GalleryListView::SeekRow( long nRow
)
486 String
GalleryListView::GetCellText(long _nRow
, sal_uInt16 nColumnId
) const
489 if( mpTheme
&& ( _nRow
< static_cast< long >( mpTheme
->GetObjectCount() ) ) )
491 SgaObject
* pObj
= mpTheme
->AcquireObject( _nRow
);
495 sRet
= GalleryBrowser2::GetItemText( *mpTheme
, *pObj
,
496 ( GALLERY_BRWBOX_TITLE
== nColumnId
) ? GALLERY_ITEM_TITLE
: GALLERY_ITEM_PATH
);
498 mpTheme
->ReleaseObject( pObj
);
505 Rectangle
GalleryListView::GetFieldCharacterBounds(sal_Int32 _nRow
,sal_Int32 _nColumnPos
,sal_Int32 nIndex
)
507 DBG_ASSERT(_nColumnPos
>= 0 && _nColumnPos
<= USHRT_MAX
, "GalleryListView::GetFieldCharacterBounds: _nColumnId overflow");
509 if ( SeekRow(_nRow
) )
511 SvxFont
aFont( GetFont() );
512 AccessibleStringWrap
aStringWrap( *this, aFont
, GetCellText(_nRow
, sal::static_int_cast
<sal_uInt16
>( GetColumnId( sal::static_int_cast
<sal_uInt16
>(_nColumnPos
) ) ) ) );
514 // get the bounds inside the string
515 aStringWrap
.GetCharacterBounds(nIndex
, aRect
);
522 sal_Int32
GalleryListView::GetFieldIndexAtPoint(sal_Int32 _nRow
,sal_Int32 _nColumnPos
,const Point
& _rPoint
)
524 DBG_ASSERT(_nColumnPos
>= 0 && _nColumnPos
<= USHRT_MAX
, "GalleryListView::GetFieldIndexAtPoint: _nColumnId overflow");
526 if ( SeekRow(_nRow
) )
528 SvxFont
aFont( GetFont() );
529 AccessibleStringWrap
aStringWrap( *this, aFont
, GetCellText(_nRow
, sal::static_int_cast
<sal_uInt16
>(GetColumnId(sal::static_int_cast
<sal_uInt16
>(_nColumnPos
)))) );
530 nRet
= aStringWrap
.GetIndexAtPoint(_rPoint
);
535 void GalleryListView::PaintField( OutputDevice
& rDev
, const Rectangle
& rRect
, sal_uInt16 nColumnId
) const
537 rDev
.Push( PUSH_CLIPREGION
);
538 rDev
.IntersectClipRegion( rRect
);
540 if( mpTheme
&& ( mnCurRow
< mpTheme
->GetObjectCount() ) )
542 SgaObject
* pObj
= mpTheme
->AcquireObject( mnCurRow
);
546 const long nTextPosY
= rRect
.Top() + ( ( rRect
.GetHeight() - rDev
.GetTextHeight() ) >> 1 );
548 if( GALLERY_BRWBOX_TITLE
== nColumnId
)
550 Rectangle
aOutputRect( rRect
.TopLeft(), Size( rRect
.GetHeight(), rRect
.GetHeight() ) );
551 GraphicObject aGrfObj
;
552 bool bTransparent(false);
554 if( pObj
->GetObjKind() == SGA_OBJ_SOUND
)
556 aGrfObj
= Graphic( BitmapEx( GAL_RES( RID_SVXBMP_GALLERY_MEDIA
) ) );
558 else if( pObj
->IsThumbBitmap() )
560 const BitmapEx
aBitmapEx(pObj
->GetThumbBmp());
562 bTransparent
= aBitmapEx
.IsTransparent();
563 aGrfObj
= Graphic(aBitmapEx
);
567 aGrfObj
= Graphic( pObj
->GetThumbMtf() );
571 Size
aSize( rDev
.LogicToPixel( aGrfObj
.GetPrefSize(), aGrfObj
.GetPrefMapMode() ) );
573 if( aSize
.Width() && aSize
.Height() )
575 if( ( aSize
.Width() > aOutputRect
.GetWidth() ) || ( aSize
.Height() > aOutputRect
.GetHeight() ) )
577 const double fBmpWH
= (double) aSize
.Width() / aSize
.Height();
578 const double fThmpWH
= (double) aOutputRect
.GetWidth() / aOutputRect
.GetHeight();
580 // Bitmap an Thumbgroesse anpassen
581 if ( fBmpWH
< fThmpWH
)
583 aSize
.Width() = (long) ( aOutputRect
.GetHeight() * fBmpWH
);
584 aSize
.Height()= aOutputRect
.GetHeight();
588 aSize
.Width() = aOutputRect
.GetWidth();
589 aSize
.Height()= (long) ( aOutputRect
.GetWidth() / fBmpWH
);
593 aSize
.Width() = std::max( aSize
.Width(), 4L );
594 aSize
.Height() = std::max( aSize
.Height(), 4L );
596 const Point
aPos( ( ( aOutputRect
.GetWidth() - aSize
.Width() ) >> 1 ) + aOutputRect
.Left(),
597 ( ( aOutputRect
.GetHeight() - aSize
.Height() ) >> 1 ) + aOutputRect
.Top() );
601 // draw checkered background
602 drawCheckered(rDev
, aPos
, aSize
);
605 aGrfObj
.Draw( &rDev
, aPos
, aSize
);
608 rDev
.DrawText( Point( aOutputRect
.Right() + 6, nTextPosY
), GalleryBrowser2::GetItemText( *mpTheme
, *pObj
, GALLERY_ITEM_TITLE
) );
610 else if( GALLERY_BRWBOX_PATH
== nColumnId
)
611 rDev
.DrawText( Point( rRect
.Left(), nTextPosY
), GalleryBrowser2::GetItemText( *mpTheme
, *pObj
, GALLERY_ITEM_PATH
) );
613 mpTheme
->ReleaseObject( pObj
);
620 void GalleryListView::Command( const CommandEvent
& rCEvt
)
622 BrowseBox::Command( rCEvt
);
624 if( rCEvt
.GetCommand() == COMMAND_CONTEXTMENU
)
626 const Point
* pPos
= NULL
;
628 if( rCEvt
.IsMouseEvent() && ( GetRowAtYPosPixel( rCEvt
.GetMousePosPixel().Y() ) != BROWSER_ENDOFSELECTION
) )
629 pPos
= &rCEvt
.GetMousePosPixel();
631 ( (GalleryBrowser2
*) GetParent() )->ShowContextMenu( this, pPos
);
635 void GalleryListView::KeyInput( const KeyEvent
& rKEvt
)
637 if( !mpTheme
|| !static_cast< GalleryBrowser2
* >( GetParent() )->KeyInput( rKEvt
, this ) )
638 BrowseBox::KeyInput( rKEvt
);
641 void GalleryListView::DoubleClick( const BrowserMouseEvent
& rEvt
)
643 BrowseBox::DoubleClick( rEvt
);
645 if( rEvt
.GetRow() != BROWSER_ENDOFSELECTION
)
646 ( (GalleryBrowser2
*) GetParent() )->TogglePreview( this, &rEvt
.GetPosPixel() );
649 void GalleryListView::Select()
651 if( maSelectHdl
.IsSet() )
652 maSelectHdl
.Call( this );
655 sal_Int8
GalleryListView::AcceptDrop( const BrowserAcceptDropEvent
& )
657 sal_Int8 nRet
= DND_ACTION_NONE
;
659 if( mpTheme
&& !mpTheme
->IsReadOnly() )
660 nRet
= DND_ACTION_COPY
;
665 sal_Int8
GalleryListView::ExecuteDrop( const BrowserExecuteDropEvent
& rEvt
)
667 ExecuteDropEvent
aEvt( rEvt
);
669 aEvt
.maPosPixel
.Y() += GetTitleHeight();
671 return( ( (GalleryBrowser2
*) GetParent() )->ExecuteDrop( *this, aEvt
) );
674 void GalleryListView::StartDrag( sal_Int8
, const Point
& rPosPixel
)
676 ( (GalleryBrowser2
*) GetParent() )->StartDrag( this, &rPosPixel
);
679 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */