1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: galctrl.cxx,v $
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_svx.hxx"
34 #include <vcl/svapp.hxx>
35 #include <sfx2/viewfrm.hxx>
36 #include <sfx2/dispatch.hxx>
37 #include <avmedia/mediaplayer.hxx>
39 #include "galbrws2.hxx"
40 #include "galtheme.hxx"
41 #include "galmisc.hxx"
42 #include "galctrl.hxx"
43 #include "AccessibleStringWrap.hxx"
44 #include <svx/svxfont.hxx>
50 #define GALLERY_BRWBOX_TITLE 1
51 #define GALLERY_BRWBOX_PATH 2
57 GalleryPreview::GalleryPreview( GalleryBrowser2
* pParent
, GalleryTheme
* pTheme
) :
58 Window( pParent
, WB_TABSTOP
| WB_BORDER
),
59 DropTargetHelper( this ),
60 DragSourceHelper( this ),
63 SetHelpId( HID_GALLERY_WINDOW
);
67 // ------------------------------------------------------------------------
69 GalleryPreview::GalleryPreview( Window
* pParent
, const ResId
& rResId
) :
70 Window( pParent
, rResId
),
71 DropTargetHelper( this ),
72 DragSourceHelper( this ),
75 SetHelpId( HID_GALLERY_PREVIEW
);
79 // ------------------------------------------------------------------------
81 GalleryPreview::~GalleryPreview()
85 // ------------------------------------------------------------------------
87 void GalleryPreview::InitSettings()
89 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
90 SetControlBackground( GALLERY_BG_COLOR
);
91 SetControlForeground( GALLERY_FG_COLOR
);
94 // -----------------------------------------------------------------------
96 void GalleryPreview::DataChanged( const DataChangedEvent
& rDCEvt
)
98 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
101 Window::DataChanged( rDCEvt
);
104 // ------------------------------------------------------------------------
106 BOOL
GalleryPreview::ImplGetGraphicCenterRect( const Graphic
& rGraphic
, Rectangle
& rResultRect
) const
108 const Size
aWinSize( GetOutputSizePixel() );
109 Size
aNewSize( LogicToPixel( rGraphic
.GetPrefSize(), rGraphic
.GetPrefMapMode() ) );
112 if( aNewSize
.Width() && aNewSize
.Height() )
114 // scale to fit window
115 const double fGrfWH
= (double) aNewSize
.Width() / aNewSize
.Height();
116 const double fWinWH
= (double) aWinSize
.Width() / aWinSize
.Height();
118 if ( fGrfWH
< fWinWH
)
120 aNewSize
.Width() = (long) ( aWinSize
.Height() * fGrfWH
);
121 aNewSize
.Height()= aWinSize
.Height();
125 aNewSize
.Width() = aWinSize
.Width();
126 aNewSize
.Height()= (long) ( aWinSize
.Width() / fGrfWH
);
129 const Point
aNewPos( ( aWinSize
.Width() - aNewSize
.Width() ) >> 1,
130 ( aWinSize
.Height() - aNewSize
.Height() ) >> 1 );
132 rResultRect
= Rectangle( aNewPos
, aNewSize
);
139 // ------------------------------------------------------------------------
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 // ------------------------------------------------------------------------
159 void GalleryPreview::MouseButtonDown( const MouseEvent
& rMEvt
)
161 if( mpTheme
&& ( rMEvt
.GetClicks() == 2 ) )
162 ( (GalleryBrowser2
*) GetParent() )->TogglePreview( this );
165 // ------------------------------------------------------------------------
167 void GalleryPreview::Command(const CommandEvent
& rCEvt
)
169 Window::Command( rCEvt
);
171 if( mpTheme
&& ( rCEvt
.GetCommand() == COMMAND_CONTEXTMENU
) )
172 ( (GalleryBrowser2
*) GetParent() )->ShowContextMenu( this,
173 ( rCEvt
.IsMouseEvent() ? &rCEvt
.GetMousePosPixel() : NULL
) );
176 // ------------------------------------------------------------------------
178 void GalleryPreview::KeyInput( const KeyEvent
& rKEvt
)
182 GalleryBrowser2
* pBrowser
= static_cast< GalleryBrowser2
* >( GetParent() );
184 switch( rKEvt
.GetKeyCode().GetCode() )
186 case( KEY_BACKSPACE
):
187 pBrowser
->TogglePreview( this );
191 pBrowser
->Travel( GALLERYBROWSERTRAVEL_FIRST
);
195 pBrowser
->Travel( GALLERYBROWSERTRAVEL_LAST
);
200 pBrowser
->Travel( GALLERYBROWSERTRAVEL_PREVIOUS
);
205 pBrowser
->Travel( GALLERYBROWSERTRAVEL_NEXT
);
210 if( !pBrowser
->KeyInput( rKEvt
, this ) )
211 Window::KeyInput( rKEvt
);
217 Window::KeyInput( rKEvt
);
220 // ------------------------------------------------------------------------
222 sal_Int8
GalleryPreview::AcceptDrop( const AcceptDropEvent
& rEvt
)
227 nRet
= ( (GalleryBrowser2
*) GetParent() )->AcceptDrop( *this, rEvt
);
229 nRet
= DND_ACTION_NONE
;
234 // ------------------------------------------------------------------------
236 sal_Int8
GalleryPreview::ExecuteDrop( const ExecuteDropEvent
& rEvt
)
241 nRet
= ( (GalleryBrowser2
*) GetParent() )->ExecuteDrop( *this, rEvt
);
243 nRet
= DND_ACTION_NONE
;
248 // ------------------------------------------------------------------------
250 void GalleryPreview::StartDrag( sal_Int8
, const Point
& )
253 ( (GalleryBrowser2
*) GetParent() )->StartDrag( this );
256 // ------------------------------------------------------------------------
258 void GalleryPreview::PreviewMedia( const INetURLObject
& rURL
)
260 if( rURL
.GetProtocol() != INET_PROT_NOT_VALID
)
262 ::avmedia::MediaFloater
* pFloater
= AVMEDIA_MEDIAWINDOW();
266 SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute( SID_AVMEDIA_PLAYER
, SFX_CALLMODE_SYNCHRON
);
267 pFloater
= AVMEDIA_MEDIAWINDOW();
271 pFloater
->setURL( rURL
.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS
), true );
275 // ------------------------------------------------------------------------
277 // -------------------
278 // - GalleryIconView -
279 // -------------------
281 GalleryIconView::GalleryIconView( GalleryBrowser2
* pParent
, GalleryTheme
* pTheme
) :
282 ValueSet( pParent
, WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
| WB_ITEMBORDER
| WB_DOUBLEBORDER
| WB_VSCROLL
| WB_FLATVALUESET
),
283 DropTargetHelper( this ),
284 DragSourceHelper( this ),
287 EnableFullItemMode( FALSE
);
289 SetHelpId( HID_GALLERY_WINDOW
);
291 SetExtraSpacing( 2 );
292 SetItemWidth( S_THUMB
+ 6 );
293 SetItemHeight( S_THUMB
+ 6 );
296 // ------------------------------------------------------------------------
298 GalleryIconView::~GalleryIconView()
302 // ------------------------------------------------------------------------
304 void GalleryIconView::InitSettings()
306 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
307 SetControlBackground( GALLERY_BG_COLOR
);
308 SetControlForeground( GALLERY_FG_COLOR
);
309 SetColor( GALLERY_BG_COLOR
);
312 // -----------------------------------------------------------------------
314 void GalleryIconView::DataChanged( const DataChangedEvent
& rDCEvt
)
316 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
319 ValueSet::DataChanged( rDCEvt
);
322 // ------------------------------------------------------------------------
324 void GalleryIconView::UserDraw( const UserDrawEvent
& rUDEvt
)
326 const USHORT nId
= rUDEvt
.GetItemId();
330 SgaObject
* pObj
= mpTheme
->AcquireObject( nId
- 1 );
334 const Rectangle
& rRect
= rUDEvt
.GetRect();
335 OutputDevice
* pDev
= rUDEvt
.GetDevice();
338 if( pObj
->IsThumbBitmap() )
340 Bitmap
aBmp( pObj
->GetThumbBmp() );
342 if( pObj
->GetObjKind() == SGA_OBJ_SOUND
)
343 aBmp
.Replace( COL_LIGHTMAGENTA
, COL_WHITE
);
345 if( ( pDev
->GetBitCount() <= 8 ) && ( aBmp
.GetBitCount() >= 8 ) )
346 aBmp
.Dither( BMP_DITHER_FLOYD
);
351 aGraphic
= pObj
->GetThumbMtf();
353 Size
aSize( aGraphic
.GetSizePixel( pDev
) );
355 if ( aSize
.Width() && aSize
.Height() )
357 if( ( aSize
.Width() > rRect
.GetWidth() ) || ( aSize
.Height() > rRect
.GetHeight() ) )
360 const double fBmpWH
= (double) aSize
.Width() / aSize
.Height();
361 const double fThmpWH
= (double) rRect
.GetWidth() / rRect
.GetHeight();
363 // Bitmap an Thumbgroesse anpassen
364 if ( fBmpWH
< fThmpWH
)
366 aSize
.Width() = (long) ( rRect
.GetHeight() * fBmpWH
);
367 aSize
.Height()= rRect
.GetHeight();
371 aSize
.Width() = rRect
.GetWidth();
372 aSize
.Height()= (long) ( rRect
.GetWidth() / fBmpWH
);
376 const Point
aPos( ( ( rRect
.GetWidth() - aSize
.Width() ) >> 1 ) + rRect
.Left(),
377 ( ( rRect
.GetHeight() - aSize
.Height() ) >> 1 ) + rRect
.Top() );
379 aGraphic
.Draw( pDev
, aPos
, aSize
);
382 SetItemText( nId
, GalleryBrowser2::GetItemText( *mpTheme
, *pObj
, GALLERY_ITEM_THEMENAME
| GALLERY_ITEM_TITLE
| GALLERY_ITEM_PATH
) );
383 mpTheme
->ReleaseObject( pObj
);
388 // ------------------------------------------------------------------------
390 void GalleryIconView::MouseButtonDown( const MouseEvent
& rMEvt
)
392 ValueSet::MouseButtonDown( rMEvt
);
394 if( rMEvt
.GetClicks() == 2 )
395 ( (GalleryBrowser2
*) GetParent() )->TogglePreview( this, &rMEvt
.GetPosPixel() );
398 // ------------------------------------------------------------------------
400 void GalleryIconView::Command( const CommandEvent
& rCEvt
)
402 ValueSet::Command( rCEvt
);
404 if( rCEvt
.GetCommand() == COMMAND_CONTEXTMENU
)
406 ( (GalleryBrowser2
*) GetParent() )->ShowContextMenu( this,
407 ( rCEvt
.IsMouseEvent() ? &rCEvt
.GetMousePosPixel() : NULL
) );
411 // ------------------------------------------------------------------------
413 void GalleryIconView::KeyInput( const KeyEvent
& rKEvt
)
415 if( !mpTheme
|| !static_cast< GalleryBrowser2
* >( GetParent() )->KeyInput( rKEvt
, this ) )
416 ValueSet::KeyInput( rKEvt
);
419 // ------------------------------------------------------------------------
421 sal_Int8
GalleryIconView::AcceptDrop( const AcceptDropEvent
& rEvt
)
423 return( static_cast< GalleryBrowser2
* >( GetParent() )->AcceptDrop( *this, rEvt
) );
426 // ------------------------------------------------------------------------
428 sal_Int8
GalleryIconView::ExecuteDrop( const ExecuteDropEvent
& rEvt
)
430 return( static_cast< GalleryBrowser2
* >( GetParent() )->ExecuteDrop( *this, rEvt
) );
433 // ------------------------------------------------------------------------
435 void GalleryIconView::StartDrag( sal_Int8
, const Point
& )
437 const CommandEvent
aEvt( GetPointerPosPixel(), COMMAND_STARTDRAG
, TRUE
);
440 // call this to initiate dragging for ValueSet
441 ValueSet::StartDrag( aEvt
, aRegion
);
442 static_cast< GalleryBrowser2
* >( GetParent() )->StartDrag( this );
445 // -------------------
446 // - GalleryListView -
447 // -------------------
449 GalleryListView::GalleryListView( GalleryBrowser2
* pParent
, GalleryTheme
* pTheme
) :
450 BrowseBox( pParent
, WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
),
455 SetHelpId( HID_GALLERY_WINDOW
);
459 SetMode( BROWSER_AUTO_VSCROLL
| BROWSER_AUTOSIZE_LASTCOL
);
460 SetDataRowHeight( 28 );
461 InsertDataColumn( GALLERY_BRWBOX_TITLE
, String( GAL_RESID( RID_SVXSTR_GALLERY_TITLE
) ), 256 );
462 InsertDataColumn( GALLERY_BRWBOX_PATH
, String( GAL_RESID( RID_SVXSTR_GALLERY_PATH
) ), 256 );
465 // ------------------------------------------------------------------------
467 GalleryListView::~GalleryListView()
471 // ------------------------------------------------------------------------
473 void GalleryListView::InitSettings()
475 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
476 SetControlBackground( GALLERY_BG_COLOR
);
477 SetControlForeground( GALLERY_FG_COLOR
);
480 // -----------------------------------------------------------------------
482 void GalleryListView::DataChanged( const DataChangedEvent
& rDCEvt
)
484 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
487 BrowseBox::DataChanged( rDCEvt
);
490 // ------------------------------------------------------------------------
492 BOOL
GalleryListView::SeekRow( long nRow
)
498 // -----------------------------------------------------------------------------
500 String
GalleryListView::GetCellText(long _nRow
, USHORT nColumnId
) const
503 if( mpTheme
&& ( _nRow
< static_cast< long >( mpTheme
->GetObjectCount() ) ) )
505 SgaObject
* pObj
= mpTheme
->AcquireObject( _nRow
);
509 sRet
= GalleryBrowser2::GetItemText( *mpTheme
, *pObj
,
510 ( GALLERY_BRWBOX_TITLE
== nColumnId
) ? GALLERY_ITEM_TITLE
: GALLERY_ITEM_PATH
);
512 mpTheme
->ReleaseObject( pObj
);
519 // -----------------------------------------------------------------------------
521 Rectangle
GalleryListView::GetFieldCharacterBounds(sal_Int32 _nRow
,sal_Int32 _nColumnPos
,sal_Int32 nIndex
)
523 DBG_ASSERT(_nColumnPos
>= 0 && _nColumnPos
<= USHRT_MAX
, "GalleryListView::GetFieldCharacterBounds: _nColumnId overflow");
525 if ( SeekRow(_nRow
) )
527 SvxFont
aFont( GetFont() );
528 AccessibleStringWrap
aStringWrap( *this, aFont
, GetCellText(_nRow
, sal::static_int_cast
<USHORT
>( GetColumnId( sal::static_int_cast
<USHORT
>(_nColumnPos
) ) ) ) );
530 // get the bounds inside the string
531 aStringWrap
.GetCharacterBounds(nIndex
, aRect
);
538 // -----------------------------------------------------------------------------
540 sal_Int32
GalleryListView::GetFieldIndexAtPoint(sal_Int32 _nRow
,sal_Int32 _nColumnPos
,const Point
& _rPoint
)
542 DBG_ASSERT(_nColumnPos
>= 0 && _nColumnPos
<= USHRT_MAX
, "GalleryListView::GetFieldIndexAtPoint: _nColumnId overflow");
544 if ( SeekRow(_nRow
) )
546 SvxFont
aFont( GetFont() );
547 AccessibleStringWrap
aStringWrap( *this, aFont
, GetCellText(_nRow
, sal::static_int_cast
<USHORT
>(GetColumnId(sal::static_int_cast
<USHORT
>(_nColumnPos
)))) );
548 nRet
= aStringWrap
.GetIndexAtPoint(_rPoint
);
553 // ------------------------------------------------------------------------
555 void GalleryListView::PaintField( OutputDevice
& rDev
, const Rectangle
& rRect
, USHORT nColumnId
) const
557 rDev
.Push( PUSH_CLIPREGION
);
558 rDev
.IntersectClipRegion( rRect
);
560 if( mpTheme
&& ( mnCurRow
< mpTheme
->GetObjectCount() ) )
562 SgaObject
* pObj
= mpTheme
->AcquireObject( mnCurRow
);
566 const long nTextPosY
= rRect
.Top() + ( ( rRect
.GetHeight() - rDev
.GetTextHeight() ) >> 1 );
568 if( GALLERY_BRWBOX_TITLE
== nColumnId
)
570 Rectangle
aOutputRect( rRect
.TopLeft(), Size( rRect
.GetHeight(), rRect
.GetHeight() ) );
571 GraphicObject aGrfObj
;
573 if( pObj
->GetObjKind() == SGA_OBJ_SOUND
)
574 aGrfObj
= Graphic( BitmapEx( GAL_RESID( RID_SVXBMP_GALLERY_MEDIA
) ) );
575 else if( pObj
->IsThumbBitmap() )
576 aGrfObj
= Graphic( pObj
->GetThumbBmp() );
578 aGrfObj
= Graphic( pObj
->GetThumbMtf() );
580 Size
aSize( rDev
.LogicToPixel( aGrfObj
.GetPrefSize(), aGrfObj
.GetPrefMapMode() ) );
582 if( aSize
.Width() && aSize
.Height() )
584 if( ( aSize
.Width() > aOutputRect
.GetWidth() ) || ( aSize
.Height() > aOutputRect
.GetHeight() ) )
587 const double fBmpWH
= (double) aSize
.Width() / aSize
.Height();
588 const double fThmpWH
= (double) aOutputRect
.GetWidth() / aOutputRect
.GetHeight();
590 // Bitmap an Thumbgroesse anpassen
591 if ( fBmpWH
< fThmpWH
)
593 aSize
.Width() = (long) ( aOutputRect
.GetHeight() * fBmpWH
);
594 aSize
.Height()= aOutputRect
.GetHeight();
598 aSize
.Width() = aOutputRect
.GetWidth();
599 aSize
.Height()= (long) ( aOutputRect
.GetWidth() / fBmpWH
);
603 aSize
.Width() = Max( aSize
.Width(), 4L );
604 aSize
.Height() = Max( aSize
.Height(), 4L );
606 const Point
aPos( ( ( aOutputRect
.GetWidth() - aSize
.Width() ) >> 1 ) + aOutputRect
.Left(),
607 ( ( aOutputRect
.GetHeight() - aSize
.Height() ) >> 1 ) + aOutputRect
.Top() );
609 aGrfObj
.Draw( &rDev
, aPos
, aSize
);
612 rDev
.DrawText( Point( aOutputRect
.Right() + 6, nTextPosY
), GalleryBrowser2::GetItemText( *mpTheme
, *pObj
, GALLERY_ITEM_TITLE
) );
614 else if( GALLERY_BRWBOX_PATH
== nColumnId
)
615 rDev
.DrawText( Point( rRect
.Left(), nTextPosY
), GalleryBrowser2::GetItemText( *mpTheme
, *pObj
, GALLERY_ITEM_PATH
) );
617 mpTheme
->ReleaseObject( pObj
);
624 // ------------------------------------------------------------------------
626 void GalleryListView::Command( const CommandEvent
& rCEvt
)
628 BrowseBox::Command( rCEvt
);
630 if( rCEvt
.GetCommand() == COMMAND_CONTEXTMENU
)
632 const Point
* pPos
= NULL
;
634 if( rCEvt
.IsMouseEvent() && ( GetRowAtYPosPixel( rCEvt
.GetMousePosPixel().Y() ) != BROWSER_ENDOFSELECTION
) )
635 pPos
= &rCEvt
.GetMousePosPixel();
637 ( (GalleryBrowser2
*) GetParent() )->ShowContextMenu( this, pPos
);
641 // ------------------------------------------------------------------------
643 void GalleryListView::KeyInput( const KeyEvent
& rKEvt
)
645 if( !mpTheme
|| !static_cast< GalleryBrowser2
* >( GetParent() )->KeyInput( rKEvt
, this ) )
646 BrowseBox::KeyInput( rKEvt
);
649 // ------------------------------------------------------------------------
651 void GalleryListView::DoubleClick( const BrowserMouseEvent
& rEvt
)
653 BrowseBox::DoubleClick( rEvt
);
655 if( rEvt
.GetRow() != BROWSER_ENDOFSELECTION
)
656 ( (GalleryBrowser2
*) GetParent() )->TogglePreview( this, &rEvt
.GetPosPixel() );
659 // ------------------------------------------------------------------------
661 void GalleryListView::Select()
663 if( maSelectHdl
.IsSet() )
664 maSelectHdl
.Call( this );
667 // ------------------------------------------------------------------------
669 sal_Int8
GalleryListView::AcceptDrop( const BrowserAcceptDropEvent
& )
671 sal_Int8 nRet
= DND_ACTION_NONE
;
673 if( mpTheme
&& !mpTheme
->IsReadOnly() && !mpTheme
->IsImported() )
675 if( !mpTheme
->IsDragging() )
676 nRet
= DND_ACTION_COPY
;
678 nRet
= DND_ACTION_COPY
;
684 // ------------------------------------------------------------------------
686 sal_Int8
GalleryListView::ExecuteDrop( const BrowserExecuteDropEvent
& rEvt
)
688 ExecuteDropEvent
aEvt( rEvt
);
690 aEvt
.maPosPixel
.Y() += GetTitleHeight();
692 return( ( (GalleryBrowser2
*) GetParent() )->ExecuteDrop( *this, aEvt
) );
695 // ------------------------------------------------------------------------
697 void GalleryListView::StartDrag( sal_Int8
, const Point
& rPosPixel
)
699 ( (GalleryBrowser2
*) GetParent() )->StartDrag( this, &rPosPixel
);