summaryrefslogtreecommitdiff
path: root/indra/newview/llpanelnearbymedia.cpp
blob: 82aa67783ded3ec3e2d8a394e51d87a2a769a095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/** 
 * @file llpanelnearbymedia.cpp
 * @brief Management interface for muting and controlling nearby media
 *
 * $LicenseInfo:firstyear=2005&license=viewergpl$
 * 
 * Copyright (c) 2005-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llpanelnearbymedia.h"

#include "llaudioengine.h"
#include "llcheckboxctrl.h"
#include "llcombobox.h"
#include "llscrolllistctrl.h"
#include "llscrolllistitem.h"
#include "llscrolllistcell.h"
#include "llsliderctrl.h"
#include "llagent.h"
#include "llagentui.h"
#include "llbutton.h"
#include "lltextbox.h"
#include "llviewermedia.h"
#include "llviewerparcelmedia.h"
#include "llviewerregion.h"
#include "llviewermediafocus.h"
#include "llviewerparcelmgr.h"
#include "llparcel.h"
#include "llpluginclassmedia.h"
#include "llvovolume.h"
#include "llstatusbar.h"
#include "llsdutil.h"

#include <stringize.h>

extern LLControlGroup gSavedSettings;

// Ugh, isInternetStreamPlaying() returns not a bool, but an *int*, with
// 0 = stopped, 1 = playing, 2 = paused.
static const int PARCEL_AUDIO_STOPPED = 0;
static const int PARCEL_AUDIO_PLAYING = 1;
static const int PARCEL_AUDIO_PAUSED = 2;

//
// LLPanelNearByMedia
//

LLPanelNearByMedia::LLPanelNearByMedia()
:	mMediaList(NULL),
	mEnableAllCtrl(NULL),
	mEnableParcelMediaCtrl(NULL),	  
	mAllMediaDisabled(false),
	mDebugInfoVisible(false)
{
	mParcelAudioAutoStart = gSavedSettings.getBOOL(LLViewerMedia::AUTO_PLAY_MEDIA_SETTING);

	mCommitCallbackRegistrar.add("MediaListCtrl.EnableAll",		boost::bind(&LLPanelNearByMedia::onClickEnableAll, this));
	mCommitCallbackRegistrar.add("MediaListCtrl.DisableAll",		boost::bind(&LLPanelNearByMedia::onClickDisableAll, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.ParcelMediaVolume",		boost::bind(&LLPanelNearByMedia::onParcelMediaVolumeSlider, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.MuteParcelMedia",		boost::bind(&LLPanelNearByMedia::onClickMuteParcelMedia, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.EnableParcelMedia",		boost::bind(&LLPanelNearByMedia::onClickEnableParcelMedia, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.DisableParcelMedia",		boost::bind(&LLPanelNearByMedia::onClickDisableParcelMedia, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.Play",		boost::bind(&LLPanelNearByMedia::onClickParcelMediaPlay, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.Stop",		boost::bind(&LLPanelNearByMedia::onClickParcelMediaStop, this));
	mCommitCallbackRegistrar.add("ParcelMediaCtrl.Pause",		boost::bind(&LLPanelNearByMedia::onClickParcelMediaPause, this));
	mCommitCallbackRegistrar.add("ParcelAudioCtrl.Play",		boost::bind(&LLPanelNearByMedia::onClickParcelAudioPlay, this));
	mCommitCallbackRegistrar.add("ParcelAudioCtrl.Stop",		boost::bind(&LLPanelNearByMedia::onClickParcelAudioStop, this));
	mCommitCallbackRegistrar.add("ParcelAudioCtrl.Pause",		boost::bind(&LLPanelNearByMedia::onClickParcelAudioPause, this));
	LLUICtrlFactory::instance().buildPanel(this, "panel_nearby_media.xml");
}

LLPanelNearByMedia::~LLPanelNearByMedia()
{
}

BOOL LLPanelNearByMedia::postBuild()
{
	LLPanel::postBuild();

	mMediaList = getChild<LLScrollListCtrl>("media_list");
	mEnableAllCtrl = getChild<LLUICtrl>("all_nearby_media_enable_btn");
	mDisableAllCtrl = getChild<LLUICtrl>("all_nearby_media_disable_btn");
	mParcelMediaVolumeSlider = getChild<LLSliderCtrl>("parcel_media_volume");
	mParcelMediaMuteCtrl = getChild<LLButton>("parcel_media_mute");
	mEnableParcelMediaCtrl = getChild<LLUICtrl>("parcel_media_enable_btn");
	mDisableParcelMediaCtrl = getChild<LLUICtrl>("parcel_media_disable_btn");
	mParcelMediaText = getChild<LLTextBox>("parcel_media_name");
	mItemCountText = getChild<LLTextBox>("media_item_count");
	mParcelMediaPlayCtrl = getChild<LLButton>("parcel_media_play_btn");
	mParcelMediaPauseCtrl = getChild<LLButton>("parcel_media_pause_btn");
	mParcelMediaCtrl = getChild<LLUICtrl>("parcel_media_ctrls");
	mParcelAudioPlayCtrl = getChild<LLButton>("parcel_audio_play_btn");
	mParcelAudioPauseCtrl = getChild<LLButton>("parcel_audio_pause_btn");
	mParcelAudioCtrl = getChild<LLUICtrl>("parcel_audio_ctrls");
	mShowCtrl = getChild<LLComboBox>("show_combo");
	
	mEmptyNameString = getString("empty_item_text");
	mDefaultParcelMediaName = getString("default_parcel_media_name");
	mDefaultParcelMediaToolTip = getString("default_parcel_media_tool_tip");
	
	mMediaList->setDoubleClickCallback(onZoomMedia, this);
	mMediaList->sortByColumnIndex(PROXIMITY_COLUMN, TRUE);
	mMediaList->sortByColumnIndex(VISIBILITY_COLUMN, FALSE);

	std::string url = LLViewerParcelMedia::getURL();
	if (!url.empty())
	{
		std::string name = LLViewerParcelMedia::getName();
		mParcelMediaText->setValue(name.empty()?url:name);
		mParcelMediaText->setToolTip(url);	
	}
	refreshList();
	updateColumns();
	
	return TRUE;
}

/*virtual*/
void LLPanelNearByMedia::onMouseEnter(S32 x, S32 y, MASK mask)
{
	mHoverTimer.stop();
	LLPanel::onMouseEnter(x,y,mask);
}


/*virtual*/
void LLPanelNearByMedia::onMouseLeave(S32 x, S32 y, MASK mask)
{
	mHoverTimer.start();
	LLPanel::onMouseLeave(x,y,mask);
}

/*virtual*/ 
void LLPanelNearByMedia::handleVisibilityChange ( BOOL new_visibility )
{
	if (new_visibility)	
	{
		mHoverTimer.start(); // timer will be stopped when mouse hovers over panel
		gFocusMgr.setTopCtrl(this);
	}
	else
	{
		mHoverTimer.stop();
		if (gFocusMgr.getTopCtrl() == this)
		{
			gFocusMgr.setTopCtrl(NULL);
		}
	}
}

/*virtual*/ 
void LLPanelNearByMedia::onTopLost ()
{
	setVisible(FALSE);
}


const F32 AUTO_CLOSE_FADE_TIME_START= 4.0f;
const F32 AUTO_CLOSE_FADE_TIME_END = 5.0f;

void LLPanelNearByMedia::draw()
{
	mItemCountText->setValue(llformat(getString("media_item_count_format").c_str(), mMediaList->getItemCount()));
	
	refreshParcelMediaUI();
	refreshParcelAudioUI();
	refreshList();
	
	F32 alpha = mHoverTimer.getStarted() 
		? clamp_rescale(mHoverTimer.getElapsedTimeF32(), AUTO_CLOSE_FADE_TIME_START, AUTO_CLOSE_FADE_TIME_END, 1.f, 0.f)
		: 1.0f;
	LLViewDrawContext context(alpha);

	LLPanel::draw();

	if (alpha == 0.f)
	{
		setVisible(false);
	}
}

bool LLPanelNearByMedia::getParcelAudioAutoStart()
{
	return mParcelAudioAutoStart;
}

void LLPanelNearByMedia::addMediaItem(const LLUUID &id)
{
	if (NULL == mMediaList) return;
	
	// Just set up the columns -- the values will be filled in by updateMediaItem().
	
	LLSD row;
	row["id"] = id;
	
	LLSD &columns = row["columns"];
	
	columns[CHECKBOX_COLUMN]["column"] = "media_checkbox_ctrl";
	columns[CHECKBOX_COLUMN]["type"] = "checkbox";	
	//if(mDebugInfoVisible)
	{
	columns[PROXIMITY_COLUMN]["column"] = "media_proximity";
	columns[PROXIMITY_COLUMN]["value"] = "";
		columns[VISIBILITY_COLUMN]["column"] = "media_visibility";
		columns[VISIBILITY_COLUMN]["value"] = "";
		columns[CLASS_COLUMN]["column"] = "media_class";
		columns[CLASS_COLUMN]["type"] = "text";
		columns[CLASS_COLUMN]["value"] = "";
	}
	columns[NAME_COLUMN]["column"] = "media_name";
	columns[NAME_COLUMN]["type"] = "text";
	columns[NAME_COLUMN]["value"] = "";
	//if(mDebugInfoVisible)
	{
		columns[DEBUG_COLUMN]["column"] = "media_debug";
		columns[DEBUG_COLUMN]["type"] = "text";
		columns[DEBUG_COLUMN]["value"] = "";
	}
	
	LLScrollListItem* new_item = mMediaList->addElement(row);
	LLScrollListCheck* scroll_list_check = dynamic_cast<LLScrollListCheck*>(new_item->getColumn(CHECKBOX_COLUMN));
	if (scroll_list_check)
	{
		LLCheckBoxCtrl *check = scroll_list_check->getCheckBox();
		check->setCommitCallback(boost::bind(&LLPanelNearByMedia::onCheckItem, this, _1, id));
	}
}

void LLPanelNearByMedia::updateMediaItem(LLScrollListItem* item, LLViewerMediaImpl* impl)
{
	LLScrollListCell* cell = item->getColumn(PROXIMITY_COLUMN);
	if(cell)
	{
		// since we are forced to sort by text, encode sort order as string
		std::string proximity_string = STRINGIZE(/*llformat("%010d", */impl->getProximity());
		std::string old_proximity_string = cell->getValue().asString();
		if(proximity_string != old_proximity_string)
		{
			cell->setValue(proximity_string);
			mMediaList->setNeedsSort(true);
		}
	}
	
	cell = item->getColumn(CHECKBOX_COLUMN);
	if(cell)
	{
		cell->setValue(!impl->isMediaDisabled());
	}
	
	cell = item->getColumn(VISIBILITY_COLUMN);
	if(cell)
	{
		S32 old_visibility = cell->getValue();
		S32 new_visibility = (impl->hasMedia()) ? 1 : ((impl->isMediaDisabled()) ? 0 : -1);
		cell->setValue(STRINGIZE(new_visibility));
		if (new_visibility != old_visibility)
		{			
			mMediaList->setNeedsSort(true);
		}
	}
		
	S32 media_class = -1;
	cell = item->getColumn(NAME_COLUMN);
	if(cell)
	{
		std::string name;
		std::string url;
		
		getNameAndUrlHelper(impl, name, url, mEmptyNameString);
		
		cell->setToolTip(url);
		std::string old_name = cell->getValue().asString();
		if(old_name != name)
		{
			cell->setValue(name);
		}
		
		LLColor4 cell_color = LLColor4::white;
		U8 font_style = LLFontGL::NORMAL;
		
		// Focused
		if (impl->hasFocus())
		{
			if (mDebugInfoVisible) cell_color = LLColor4::yellow;
			media_class = MEDIA_CLASS_FOCUSED;
		}
		// Is attached to another avatar?
		else if (impl->isAttachedToAnotherAvatar())
		{
			if (mDebugInfoVisible) cell_color = LLColor4::red;
			media_class = MEDIA_CLASS_ON_OTHERS;
		}
		// Outside agent parcel
		else if (!impl->isInAgentParcel())
		{
			if (mDebugInfoVisible) cell_color = LLColor4::orange;
			media_class = MEDIA_CLASS_OUTSIDE_PARCEL;
		}
		else {
			// inside parcel
			media_class = MEDIA_CLASS_WITHIN_PARCEL;
		}
		if (impl->isMediaDisabled())
		{
			if (mDebugInfoVisible) font_style |= LLFontGL::ITALIC;
			if (mDebugInfoVisible) cell_color = LLColor4::black;
		}
		// Dim it if it isn't "showing"
		else if (!impl->hasMedia())
		{
			cell_color.setAlpha(0.25);
		}
		// Bold it if it is time-based media and it is playing
		else if (impl->isMediaTimeBased() &&
				 impl->isMediaPlaying())
		{
			if (mDebugInfoVisible) font_style |= LLFontGL::BOLD;
		}
		cell->setColor(cell_color);
		LLScrollListText *text_cell = dynamic_cast<LLScrollListText*> (cell);
		if (text_cell)
		{
			text_cell->setFontStyle(font_style);
		}
	}
	
	cell = item->getColumn(CLASS_COLUMN);
	if(cell)
	{
		// TODO: clean this up!
		cell->setValue(STRINGIZE(media_class));
	}
	
	if(mDebugInfoVisible)
	{
		cell = item->getColumn(DEBUG_COLUMN);
		if(cell)
		{
			std::string s;
			
			s += llformat("%g/", (float)impl->getInterest());

			// proximity distance is actually distance squared -- display it as straight distance.
			s += llformat("%g/", fsqrtf(impl->getProximityDistance()));

//			s += llformat("%g/", (float)impl->getCPUUsage());
//			s += llformat("%g/", (float)impl->getApproximateTextureInterest());
			s += llformat("%g/", (float)(NULL == impl->getSomeObject()) ? 0.0 : impl->getSomeObject()->getPixelArea());
			
			s += LLPluginClassMedia::priorityToString(impl->getPriority());
			
			if(impl->hasMedia())
			{
				s += '@';
			}
			else if(impl->isPlayable())
			{
				s += '+';
			}
			else if(impl->isForcedUnloaded())
			{
				s += '!';
			}
				
			cell->setValue(s);
		}
	}
}

void LLPanelNearByMedia::removeMediaItem(const LLUUID &id)
{
	if (NULL == mMediaList) return;
	
	mMediaList->deleteSingleItem(mMediaList->getItemIndex(id));
}

void LLPanelNearByMedia::refreshParcelMediaUI()
{	
	std::string url = LLViewerParcelMedia::getURL();
	LLStyle::Params style_params;
	if (url.empty())
	{	
		style_params.font.style = "ITALIC";
		mParcelMediaText->setText(mDefaultParcelMediaName, style_params);
		mParcelMediaText->setToolTip(mDefaultParcelMediaToolTip);	
		mEnableParcelMediaCtrl->setEnabled(false);
		mDisableParcelMediaCtrl->setEnabled(false);
	}
	else {
		std::string name = LLViewerParcelMedia::getName();
		if (name.empty()) name = url;
		mParcelMediaText->setText(name, style_params);
		mParcelMediaText->setToolTip(url);
		mEnableParcelMediaCtrl->setEnabled(true);
		mDisableParcelMediaCtrl->setEnabled(true);
	}
	
	// Set up the default play controls state
	mParcelMediaPauseCtrl->setEnabled(false);
	mParcelMediaPauseCtrl->setVisible(false);
	mParcelMediaPlayCtrl->setEnabled(true);
	mParcelMediaPlayCtrl->setVisible(true);
	mParcelMediaCtrl->setEnabled(false);
	
	if (LLViewerParcelMedia::getParcelMedia())
	{
		if (LLViewerParcelMedia::getParcelMedia()->getMediaPlugin() &&
			LLViewerParcelMedia::getParcelMedia()->getMediaPlugin()->pluginSupportsMediaTime())
		{
			mParcelMediaCtrl->setEnabled(true);
			
			switch(LLViewerParcelMedia::getParcelMedia()->getMediaPlugin()->getStatus())
			{
				case LLPluginClassMediaOwner::MEDIA_PLAYING:
					mParcelMediaPlayCtrl->setEnabled(false);
					mParcelMediaPlayCtrl->setVisible(false);
					mParcelMediaPauseCtrl->setEnabled(true);
					mParcelMediaPauseCtrl->setVisible(true);
					break;
				case LLPluginClassMediaOwner::MEDIA_PAUSED:
				default:
					// default play status is kosher
					break;
			}
		}
	}
}

void LLPanelNearByMedia::refreshParcelAudioUI()
{	
	bool parcel_audio_enabled = !getParcelAudioURL().empty();

	mParcelAudioCtrl->setToolTip(getParcelAudioURL());
	
	if (gAudiop && parcel_audio_enabled)
	{
		mParcelAudioCtrl->setEnabled(true);

		if (PARCEL_AUDIO_PLAYING == gAudiop->isInternetStreamPlaying())
		{
			mParcelAudioPlayCtrl->setEnabled(false);
			mParcelAudioPlayCtrl->setVisible(false);
			mParcelAudioPauseCtrl->setEnabled(true);
			mParcelAudioPauseCtrl->setVisible(true);
		}
		else {
			mParcelAudioPlayCtrl->setEnabled(true);
			mParcelAudioPlayCtrl->setVisible(true);
			mParcelAudioPauseCtrl->setEnabled(false);
			mParcelAudioPauseCtrl->setVisible(false);
		}
	}
	else {
		mParcelAudioCtrl->setEnabled(false);
		mParcelAudioPlayCtrl->setEnabled(true);
		mParcelAudioPlayCtrl->setVisible(true);
		mParcelAudioPauseCtrl->setEnabled(false);
		mParcelAudioPauseCtrl->setVisible(false);
	}
}

void LLPanelNearByMedia::refreshList()
{
	bool all_items_deleted = false;
		
	if(!mMediaList)
	{
		// None of this makes any sense if the media list isn't there.
		return;
	}
	
	// Check whether the debug column has been shown/hidden.
	bool debug_info_visible = gSavedSettings.getBOOL("MediaPerformanceManagerDebug");
	if(debug_info_visible != mDebugInfoVisible)
	{
		mDebugInfoVisible = debug_info_visible;

		// Clear all items so the list gets regenerated.
		mMediaList->deleteAllItems();
		all_items_deleted = true;
		
		updateColumns();
	}
	
	// Get the canonical list from LLViewerMedia
	LLViewerMedia::impl_list impls = LLViewerMedia::getPriorityList();
	LLViewerMedia::impl_list::iterator priority_iter;
	
	// iterate over the impl list, creating rows as necessary.
	for(priority_iter = impls.begin(); priority_iter != impls.end(); priority_iter++)
	{
		// If we just emptied out the list, every flag needs to be reset.
		if(all_items_deleted)
		{
			(*priority_iter)->setInNearbyMediaList(false);
		}
		
		if(! (*priority_iter)->isParcelMedia())
		{
			LLUUID media_id = (*priority_iter)->getMediaTextureID();
			S32 proximity = (*priority_iter)->getProximity();
			// This is expensive (i.e. a linear search) -- don't use it here.  We now use mInNearbyMediaList instead.
//			S32 index = mMediaList->getItemIndex(media_id);

			if(proximity < 0 || !shouldShow(*priority_iter))
			{
				// This isn't inworld media -- don't show it in the list.
				if ((*priority_iter)->getInNearbyMediaList())
				{
					// There's a row for this impl -- remove it.
					removeMediaItem(media_id);
					(*priority_iter)->setInNearbyMediaList(false);
				}
			}
			else
			{
				if (!(*priority_iter)->getInNearbyMediaList())
				{
					// We don't have a row for this impl -- add one.
					addMediaItem(media_id);
					(*priority_iter)->setInNearbyMediaList(true);
				}
			}
		}
	}
	
	int disabled_count = 0;
	int enabled_count = 0;

	// Iterate over the rows in the control, updating ones whose impl exists, and deleting ones whose impl has gone away.
	std::vector<LLScrollListItem*> items = mMediaList->getAllData();

	for (std::vector<LLScrollListItem*>::iterator item_it = items.begin();
		item_it != items.end();
		++item_it)
	{
		LLScrollListItem* item = (*item_it);
		LLUUID row_id = item->getUUID();
		
		LLViewerMediaImpl* impl = LLViewerMedia::getMediaImplFromTextureID(row_id);
		if(impl)
		{
			updateMediaItem(item, impl);
			if(impl->isMediaDisabled())
				disabled_count++;
			else
				enabled_count++;
		}
		else
		{
			// This item's impl has been deleted -- remove the row.
			// Removing the row won't throw off our iteration, since we have a local copy of the array.
			// We just need to make sure we don't access this item after the delete.
			removeMediaItem(row_id);
		}
	}
	mEnableAllCtrl->setEnabled(! LLViewerMedia::getInWorldMediaDisabled() && disabled_count > 0);
	mDisableAllCtrl->setEnabled(! LLViewerMedia::getInWorldMediaDisabled() && enabled_count > 0);
	
	// Set the selection to whatever media impl the media focus/hover is on. 
	// This is an experiment, and can be removed by ifdefing out these 4 lines.
	LLUUID media_target = LLViewerMediaFocus::getInstance()->getControlsMediaID();
	if(media_target.notNull())
	{
		mMediaList->selectByID(media_target);
	}
}

void LLPanelNearByMedia::updateColumns()
{
	if (!mDebugInfoVisible)
	{
		mMediaList->getColumn(VISIBILITY_COLUMN)->setWidth(-1);
		mMediaList->getColumn(PROXIMITY_COLUMN)->setWidth(-1);
		mMediaList->getColumn(CLASS_COLUMN)->setWidth(-1);
		mMediaList->getColumn(DEBUG_COLUMN)->setWidth(-1);
	}
	else {
		mMediaList->getColumn(VISIBILITY_COLUMN)->setWidth(20);
		mMediaList->getColumn(PROXIMITY_COLUMN)->setWidth(30);
		mMediaList->getColumn(CLASS_COLUMN)->setWidth(20);
		mMediaList->getColumn(DEBUG_COLUMN)->setWidth(200);
	}
}

void LLPanelNearByMedia::onClickEnableAll()
	{
	enableAllHelper(true);
	}

void LLPanelNearByMedia::onClickDisableAll()
{	
	enableAllHelper(false);
}

void LLPanelNearByMedia::enableAllHelper(bool enabled)
{	
	bool disabled = !enabled;
	
	// Iterate over the rows in the control, setting the disable flag on the impl for each.
	std::vector<LLScrollListItem*> items = mMediaList->getAllData();
	
	for (std::vector<LLScrollListItem*>::iterator item_it = items.begin();
		 item_it != items.end();
		 ++item_it)
	{
		setDisabled((*item_it)->getUUID(), disabled);
	}
	
	// Now do the parcel media	
	if (LLViewerParcelMedia::getParcelMedia())
	{
		LLViewerParcelMedia::getParcelMedia()->setDisabled(disabled);
	}
	
	// And finally, parcel audio
	if (disabled)
	{
		onClickParcelAudioStop();
	}
	else {
		onClickParcelAudioPlay();
	}
	
	// The hilight state of the control will be adjusted the next time through refreshList().
}

void LLPanelNearByMedia::onClickEnableParcelMedia()
{	
		LLViewerParcelMedia::play(LLViewerParcelMgr::getInstance()->getAgentParcel());
	}

void LLPanelNearByMedia::onClickDisableParcelMedia()
{	
		// This actually unloads the impl, as opposed to "stop"ping the media
		LLViewerParcelMedia::stop();
	}

void LLPanelNearByMedia::onCheckItem(LLUICtrl* ctrl, const LLUUID &row_id)
{	
	LLCheckBoxCtrl* check = static_cast<LLCheckBoxCtrl*>(ctrl);

	setDisabled(row_id, ! check->getValue());
}

bool LLPanelNearByMedia::setDisabled(const LLUUID &row_id, bool disabled)
{
	LLViewerMediaImpl* impl = LLViewerMedia::getMediaImplFromTextureID(row_id);
	if(impl)
	{
		impl->setDisabled(disabled);
		return true;
	}
	return false;
}
								
//static
void LLPanelNearByMedia::onZoomMedia(void* user_data)
{
	LLPanelNearByMedia* panelp = (LLPanelNearByMedia*)user_data;
	LLUUID media_id = panelp->mMediaList->getValue().asUUID();
	
	LLViewerMediaFocus::getInstance()->focusZoomOnMedia(media_id);
}

void LLPanelNearByMedia::onClickMuteParcelMedia()
{
	if (LLViewerParcelMedia::getParcelMedia())
	{
		bool muted = mParcelMediaMuteCtrl->getValue();
		LLViewerParcelMedia::getParcelMedia()->setVolume(muted ? (F32)0 : mParcelMediaVolumeSlider->getValueF32() );
	}
}

void LLPanelNearByMedia::onParcelMediaVolumeSlider()
{
	if (LLViewerParcelMedia::getParcelMedia())
	{
		LLViewerParcelMedia::getParcelMedia()->setVolume(mParcelMediaVolumeSlider->getValueF32());
	}
}

void LLPanelNearByMedia::onClickParcelMediaPlay()
{
	LLViewerParcelMedia::play(LLViewerParcelMgr::getInstance()->getAgentParcel());
}

void LLPanelNearByMedia::onClickParcelMediaStop()
{	
	if (LLViewerParcelMedia::getParcelMedia())
	{
		// This stops the media playing, as opposed to unloading it like
		// LLViewerParcelMedia::stop() does
		LLViewerParcelMedia::getParcelMedia()->stop();
	}
}

void LLPanelNearByMedia::onClickParcelMediaPause()
{
	LLViewerParcelMedia::pause();
}

void LLPanelNearByMedia::onClickParcelAudioPlay()
{
	// User *explicitly* started the internet stream, so keep the stream
	// playing and updated as they cross to other parcels etc.
	mParcelAudioAutoStart = true;

	if (!gAudiop)
		return;

	if (PARCEL_AUDIO_PAUSED == gAudiop->isInternetStreamPlaying())
	{
		// 'false' means unpause
		gAudiop->pauseInternetStream(false);
	}
	else {
		gAudiop->startInternetStream(getParcelAudioURL());
	}
}

void LLPanelNearByMedia::onClickParcelAudioStop()
{
	// User *explicitly* stopped the internet stream, so don't
	// re-start audio when i.e. they move to another parcel, until
	// they explicitly start it again.
	mParcelAudioAutoStart = false;

	if (!gAudiop)
		return;

	gAudiop->stopInternetStream();
}

void LLPanelNearByMedia::onClickParcelAudioPause()
{
	if (!gAudiop)
		return;

	// 'true' means pause
	gAudiop->pauseInternetStream(true);
}

bool LLPanelNearByMedia::shouldShow(LLViewerMediaImpl* impl)
{	
	const LLSD &choice_llsd = mShowCtrl->getSelectedValue();
	MediaClass choice = (MediaClass)choice_llsd.asInteger();

	switch (choice)
	{	
		case MEDIA_CLASS_ALL:
			return true;
			break;
		case MEDIA_CLASS_WITHIN_PARCEL:
			return impl->isInAgentParcel();
			break;
		case MEDIA_CLASS_OUTSIDE_PARCEL:
			return ! impl->isInAgentParcel();
			break;
		case MEDIA_CLASS_ON_OTHERS:
			return impl->isAttachedToAnotherAvatar();
			break;
		default:
			break;
	}
	return true;
}

// static
void LLPanelNearByMedia::getNameAndUrlHelper(LLViewerMediaImpl* impl, std::string& name, std::string & url, const std::string &defaultName)
{
	if (NULL == impl) return;
	
	name = impl->getName();
	url = impl->getCurrentMediaURL();	// This is the URL the media impl actually has loaded
	if (url.empty())
	{
		url = impl->getMediaEntryURL();	// This is the current URL from the media data
	}
	if (url.empty())
	{
		url = impl->getHomeURL();		// This is the home URL from the media data
	}
	if (name.empty())
	{
		name = url;
	}
	if (name.empty())
	{
		name = defaultName;
	}
}

// static
std::string LLPanelNearByMedia::getParcelAudioURL()
{
	return LLViewerParcelMgr::getInstance()->getAgentParcel()->getMusicURL();
}