android: Update app-specific/MIME type icons
[LibreOffice.git] / avmedia / source / framework / MediaControlBase.cxx
blobd2c254293811bf7c0159991473035cb47db239f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #include <avmedia/MediaControlBase.hxx>
20 #include <avmedia/mediaplayer.hxx>
21 #include <avmedia/mediaitem.hxx>
22 #include <tools/time.hxx>
23 #include <unotools/localedatawrapper.hxx>
24 #include <strings.hrc>
25 #include <helpids.h>
26 #include <mediamisc.hxx>
28 constexpr sal_Int32 AVMEDIA_DB_RANGE = -40;
29 constexpr double AVMEDIA_LINEINCREMENT = 1.0;
30 constexpr double AVMEDIA_PAGEINCREMENT = 10.0;
32 namespace avmedia {
34 MediaControlBase::MediaControlBase()
35 : mbCurrentlySettingZoom(false)
39 void MediaControlBase::UpdateTimeField( MediaItem const & aMediaItem, double fTime )
41 if( aMediaItem.getURL().isEmpty())
42 return;
44 OUString aTimeString;
46 SvtSysLocale aSysLocale;
47 const LocaleDataWrapper& rLocaleData = aSysLocale.GetLocaleData();
49 aTimeString += rLocaleData.getDuration( tools::Time( 0, 0, static_cast< sal_uInt32 >( floor( fTime ) ) ) ) +
50 " / " +
51 rLocaleData.getDuration( tools::Time( 0, 0, static_cast< sal_uInt32 >( floor( aMediaItem.getDuration() ) )) );
53 if( mxTimeEdit->get_text() != aTimeString )
54 mxTimeEdit->set_text( aTimeString );
57 void MediaControlBase::UpdateVolumeSlider( MediaItem const & aMediaItem )
59 if( aMediaItem.getURL().isEmpty() )
60 mxVolumeSlider->set_sensitive(false);
61 else
63 mxVolumeSlider->set_sensitive(true);
64 const sal_Int32 nVolumeDB = aMediaItem.getVolumeDB();
65 mxVolumeSlider->set_value( std::clamp( nVolumeDB, AVMEDIA_DB_RANGE, sal_Int32(0)) );
69 void MediaControlBase::UpdateTimeSlider( MediaItem const & aMediaItem )
71 if( aMediaItem.getURL().isEmpty() )
72 mxTimeSlider->set_sensitive(false);
73 else
75 mxTimeSlider->set_sensitive(true);
77 const double fDuration = aMediaItem.getDuration();
79 if( fDuration > 0.0 )
81 const double fTime = std::min( aMediaItem.getTime(), fDuration );
83 bool bChanged(false);
84 int nStep(0), nPage(0);
85 mxTimeSlider->get_increments(nStep, nPage);
86 if (!nStep)
88 nStep = AVMEDIA_TIME_RANGE * AVMEDIA_LINEINCREMENT / fDuration;
89 bChanged = true;
91 if (!nPage)
93 nPage = AVMEDIA_TIME_RANGE * AVMEDIA_PAGEINCREMENT / fDuration;
94 bChanged = true;
96 if (bChanged)
97 mxTimeSlider->set_increments(nStep, nPage);
99 mxTimeSlider->set_value(fTime / fDuration * AVMEDIA_TIME_RANGE);
104 void MediaControlBase::InitializeWidgets()
106 mxPlayToolBox->set_item_help_id("play", HID_AVMEDIA_TOOLBOXITEM_PLAY);
107 mxPlayToolBox->set_item_help_id("pause", HID_AVMEDIA_TOOLBOXITEM_PAUSE);
108 mxPlayToolBox->set_item_help_id("stop", HID_AVMEDIA_TOOLBOXITEM_STOP);
109 mxPlayToolBox->set_item_help_id("loop", HID_AVMEDIA_TOOLBOXITEM_LOOP);
110 mxMuteToolBox->set_item_help_id("mute", HID_AVMEDIA_TOOLBOXITEM_MUTE);
112 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_50), AvmResId( AVMEDIA_STR_ZOOM_50 ));
113 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_100), AvmResId( AVMEDIA_STR_ZOOM_100 ));
114 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_200), AvmResId( AVMEDIA_STR_ZOOM_200 ));
115 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_FIT), AvmResId( AVMEDIA_STR_ZOOM_FIT ));
116 mxZoomListBox->set_help_id( HID_AVMEDIA_ZOOMLISTBOX );
117 mxZoomListBox->set_tooltip_text(AvmResId( AVMEDIA_STR_ZOOM_TOOLTIP ));
119 mxTimeEdit->set_text( " 00:00:00/00:00:00 " );
120 mxTimeEdit->set_help_id( HID_AVMEDIA_TIMEEDIT );
121 mxTimeEdit->set_sensitive(false);
123 mxVolumeSlider->set_range(AVMEDIA_DB_RANGE, 0);
124 mxVolumeSlider->set_tooltip_text( AvmResId( AVMEDIA_STR_VOLUME ));
125 mxVolumeSlider->set_help_id( HID_AVMEDIA_VOLUMESLIDER );
127 mxTimeSlider->set_range( 0, AVMEDIA_TIME_RANGE );
128 mxTimeSlider->set_tooltip_text( AvmResId( AVMEDIA_STR_POSITION ));
131 void MediaControlBase::UpdatePlayState(const MediaItem& rMediaItem)
133 if (rMediaItem.getState() == MediaState::Play)
135 mxPlayToolBox->set_item_active("play", true);
136 mxPlayToolBox->set_item_active("pause", false);
137 mxPlayToolBox->set_item_active("stop", false);
139 else if( rMediaItem.getState() == MediaState::Pause )
141 mxPlayToolBox->set_item_active("play", false);
142 mxPlayToolBox->set_item_active("pause", true);
143 mxPlayToolBox->set_item_active("stop", false);
145 else
147 mxPlayToolBox->set_item_active("play", false);
148 mxPlayToolBox->set_item_active("pause", false);
149 mxPlayToolBox->set_item_active("stop", true);
153 void MediaControlBase::UpdateToolBoxes(const MediaItem& rMediaItem)
155 const bool bValidURL = !rMediaItem.getURL().isEmpty();
156 mxPlayToolBox->set_item_sensitive("play", bValidURL);
157 mxPlayToolBox->set_item_sensitive("pause", bValidURL);
158 mxPlayToolBox->set_item_sensitive("stop", bValidURL);
159 mxPlayToolBox->set_item_sensitive("loop", bValidURL);
160 mxMuteToolBox->set_item_sensitive("mute", bValidURL);
161 if( !bValidURL )
163 mxZoomListBox->set_sensitive(false);
164 mxMuteToolBox->set_sensitive(false);
166 else
168 mxPlayToolBox->set_sensitive(true);
169 mxMuteToolBox->set_sensitive(true);
170 UpdatePlayState(rMediaItem);
171 mxPlayToolBox->set_item_active("loop", rMediaItem.isLoop());
172 mxMuteToolBox->set_item_active("mute", rMediaItem.isMute());
173 if (!mbCurrentlySettingZoom)
175 sal_uInt16 nSelectEntryPos ;
177 switch( rMediaItem.getZoom() )
179 case css::media::ZoomLevel_ZOOM_1_TO_2:
180 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_50;
181 break;
182 case css::media::ZoomLevel_ORIGINAL:
183 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_100;
184 break;
185 case css::media::ZoomLevel_ZOOM_2_TO_1:
186 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_200;
187 break;
188 case css::media::ZoomLevel_FIT_TO_WINDOW_FIXED_ASPECT:
189 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_FIT;
190 break;
191 case css::media::ZoomLevel_FIT_TO_WINDOW:
192 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_SCALED;
193 break;
195 default:
196 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_INVALID;
197 break;
200 if( nSelectEntryPos != AVMEDIA_ZOOMLEVEL_INVALID )
202 mxZoomListBox->show();
203 mxZoomListBox->set_sensitive(true);
204 mxZoomListBox->set_active(nSelectEntryPos);
206 else
207 mxZoomListBox->set_sensitive(false);
212 void MediaControlBase::SelectPlayToolBoxItem( MediaItem& aExecItem, MediaItem const & aItem, std::u16string_view rId)
214 if (rId == u"apply")
216 MediaFloater* pFloater = avmedia::getMediaFloater();
218 if( pFloater )
219 pFloater->dispatchCurrentURL();
221 else if (rId == u"play")
223 aExecItem.setState( MediaState::Play );
225 if( aItem.getTime() == aItem.getDuration() )
226 aExecItem.setTime( 0.0 );
227 else
228 aExecItem.setTime( aItem.getTime() );
230 UpdatePlayState(aExecItem);
232 else if (rId == u"pause")
234 aExecItem.setState( MediaState::Pause );
236 UpdatePlayState(aExecItem);
238 else if (rId == u"stop")
240 aExecItem.setState( MediaState::Stop );
241 aExecItem.setTime( 0.0 );
243 UpdatePlayState(aExecItem);
245 else if (rId == u"mute")
247 aExecItem.setMute( mxMuteToolBox->get_item_active("mute") );
249 else if (rId == u"loop")
251 aExecItem.setLoop( mxPlayToolBox->get_item_active("loop") );
255 void MediaControlBase::disposeWidgets()
257 mxZoomListBox.reset();
258 mxTimeEdit.reset();
259 mxVolumeSlider.reset();
260 mxMuteToolBox.reset();
261 mxTimeSlider.reset();
262 mxPlayToolBox.reset();
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */