LLMediaCtrl::LLMediaCtrl( const std::string& name, const LLRect& rect ) :
	LLUICtrl( name, rect, FALSE, NULL, NULL),
	LLInstanceTracker<LLMediaCtrl, LLUUID>(LLUUID::generateNewID()),
	mTextureDepthBytes( 4 ),
	mWebBrowserImage( 0 ),
	mBorder(NULL),
	mFrequentUpdates( true ),
	mForceUpdate( false ),
	mOpenLinksInExternalBrowser( false ),
	mOpenLinksInInternalBrowser( false ),
	mTrusted( false ),
	mHomePageUrl( "" ),
	mIgnoreUIScale( true ),
	mAlwaysRefresh( false ),
	mExternalUrl( "" ),
	mMediaSource( 0 ),
	mTakeFocusOnClick( true ),
	mCurrentNavUrl( "about:blank" ),
	mLastSetCursor( UI_CURSOR_ARROW ),
	mStretchToFill( true ),
	mMaintainAspectRatio ( true ),
	mDecoupleTextureSize ( false ),
	mTextureWidth ( 1024 ),
	mTextureHeight ( 1024 ),
	mHideLoading (false)
{
	if(!getDecoupleTextureSize())
	{
		S32 screen_width = mIgnoreUIScale ? 
			llround((F32)getRect().getWidth() * LLUI::getScaleFactor().mV[VX]) : getRect().getWidth();
		S32 screen_height = mIgnoreUIScale ? 
			llround((F32)getRect().getHeight() * LLUI::getScaleFactor().mV[VY]) : getRect().getHeight();
	
		setTextureSize(screen_width, screen_height);
	}
	// We don't need to create the media source up front anymore unless we have a non-empty home URL to navigate to.
	if(!mHomePageUrl.empty())
	{
		navigateHome();
	}
		

	LLRect border_rect( 0, getRect().getHeight() + 2, getRect().getWidth() + 2, 0 );
	mBorder = new LLViewBorder( std::string("web control border"), border_rect, LLViewBorder::BEVEL_IN );
	addChild( mBorder );
}
HelpWidget::HelpWidget(const SimRobot::Object& object) : QSplitter(Qt::Horizontal), object(object)
{
  QString qhc = QFileInfo(HelpModule::application->getAppPath()).dir().path() + 
#ifdef MACOSX
  "/../Resources" +
#endif
  "/helpcollection.qhc";
  helpEngine = new QHelpEngine(qhc, this);
  helpBrowser = new HelpBrowser(helpEngine, this);
  helpBrowser->setFrameStyle(QFrame::NoFrame);

  if(!helpEngine->setupData())
    HelpModule::application->showWarning(tr("SimRobotHelp"), helpEngine->error());

  QHelpContentWidget* contentWidget = helpEngine->contentWidget();
  contentWidget->setFrameStyle(QFrame::NoFrame);
  
  QHelpIndexWidget* indexWidget = helpEngine->indexWidget();
  indexWidget->setFrameStyle(QFrame::NoFrame);
  
  QTabWidget* leftWidget = new QTabWidget(this);
  leftWidget->addTab(contentWidget, "Contents");
  leftWidget->addTab(indexWidget, "Index");
  
  QWidget* searchTabWidget = new QWidget(this);
  QVBoxLayout* searchTabLayout = new QVBoxLayout(this);
  searchTabLayout->addWidget(helpEngine->searchEngine()->queryWidget());
  searchTabLayout->addWidget(new QLabel("Search results:", this));
  searchResultsWidget = new QTextBrowser(this);
  searchTabLayout->addWidget(searchResultsWidget);
  searchTabWidget->setLayout(searchTabLayout);  
  
  leftWidget->addTab(searchTabWidget, "Search");
  
  QToolBar* toolBar = new QToolBar(this);
  
  QAction* navigateHomeAction = new QAction(QIcon(":/Icons/home.png"), tr("Home"), this); 
  navigateHomeAction->setStatusTip(tr("Navigate Home"));
  connect(navigateHomeAction, SIGNAL(triggered()), this, SLOT(navigateHome()));  
  toolBar->addAction(navigateHomeAction);
  
  QAction* locatePageAction = new QAction(QIcon(":/Icons/locate.png"), tr("Locate"), this); 
  locatePageAction->setStatusTip(tr("Locate current page in contents window"));
  connect(locatePageAction, SIGNAL(triggered()), this, SLOT(locatePage()));  
  toolBar->addAction(locatePageAction);
  
  toolBar->addSeparator();  
  
  QAction* navigateBackwardAction = new QAction(QIcon(":/Icons/back.png"), tr("Back"), this); 
  navigateBackwardAction->setStatusTip(tr("Navigate Back"));
  connect(navigateBackwardAction, SIGNAL(triggered()), this, SLOT(navigateBackward()));
  toolBar->addAction(navigateBackwardAction);  
  
  QAction* navigateForwardAction = new QAction(QIcon(":/Icons/forward.png"), tr("Forward"), this); 
  navigateForwardAction->setStatusTip(tr("Navigate Forward"));
  connect(navigateForwardAction, SIGNAL(triggered()), this, SLOT(navigateForward()));
  toolBar->addAction(navigateForwardAction);
  
  QWidget* rightWidget = new QWidget(this);
  
  QVBoxLayout* rightWidgetLayout = new QVBoxLayout(this);
  rightWidgetLayout->addWidget(toolBar);
  rightWidgetLayout->addWidget(helpBrowser);
  rightWidget->setLayout(rightWidgetLayout);

  addWidget(leftWidget);
  addWidget(rightWidget);
  
  connect(helpBrowser, SIGNAL(forwardAvailable(bool)), navigateForwardAction, SLOT(setEnabled(bool)));  
  connect(helpBrowser, SIGNAL(backwardAvailable(bool)), navigateBackwardAction, SLOT(setEnabled(bool)));  

  connect(contentWidget, SIGNAL(linkActivated(const QUrl&)), helpBrowser, SLOT(setSource(const QUrl &)));
  connect(indexWidget, SIGNAL(linkActivated(const QUrl&, const QString&)), helpBrowser, SLOT(setSource(const QUrl &)));
  
  connect(this, SIGNAL(tabChangeRequested(int)), leftWidget, SLOT(setCurrentIndex(int)));
  connect(helpEngine->searchEngine()->queryWidget(), SIGNAL(search()), this, SLOT(searchInvoked()));
  connect(helpEngine->searchEngine(), SIGNAL(searchingFinished(int)), this, SLOT(searchFinished(int)));

  helpEngine->searchEngine()->reindexDocumentation();
  
  searchResultsWidget->setOpenLinks(false);
  searchResultsWidget->setOpenExternalLinks(false);
  
  connect(searchResultsWidget, SIGNAL(anchorClicked(const QUrl&)), helpBrowser, SLOT(setSource(const QUrl&)));
  
  navigateHome();

  setStretchFactor(0,1);
  setStretchFactor(1,2);
  
  setFrameStyle(QFrame::NoFrame);
  
  QSettings& settings = HelpModule::application->getLayoutSettings();
  settings.beginGroup(object.getFullName());
  restoreState(settings.value("State").toByteArray());
  settings.endGroup();
}
LLMediaCtrl::LLMediaCtrl( const Params& p) :
	LLPanel( p ),
	LLInstanceTracker<LLMediaCtrl, LLUUID>(LLUUID::generateNewID()),
	mTextureDepthBytes( 4 ),
	mBorder(NULL),
	mFrequentUpdates( true ),
	mForceUpdate( false ),
	mHomePageUrl( "" ),
	mAlwaysRefresh( false ),
	mMediaSource( 0 ),
	mTakeFocusOnClick( p.focus_on_click ),
	mCurrentNavUrl( "" ),
	mStretchToFill( true ),
	mMaintainAspectRatio ( true ),
	mDecoupleTextureSize ( false ),
	mTextureWidth ( 1024 ),
	mTextureHeight ( 1024 ),
	mClearCache(false),
	mHomePageMimeType(p.initial_mime_type),
	mErrorPageURL(p.error_page_url),
	mTrusted(p.trusted_content),
	mWindowShade(NULL),
	mHoverTextChanged(false),
	mContextMenu(NULL)
{
	{
		LLColor4 color = p.caret_color().get();
		setCaretColor( (unsigned int)color.mV[0], (unsigned int)color.mV[1], (unsigned int)color.mV[2] );
	}

	setHomePageUrl(p.start_url, p.initial_mime_type);
	
	setBorderVisible(p.border_visible);
	
	setDecoupleTextureSize(p.decouple_texture_size);
	
	setTextureSize(p.texture_width, p.texture_height);

	if(!getDecoupleTextureSize())
	{
		S32 screen_width = llround((F32)getRect().getWidth() * LLUI::getScaleFactor().mV[VX]);
		S32 screen_height = llround((F32)getRect().getHeight() * LLUI::getScaleFactor().mV[VY]);
			
		setTextureSize(screen_width, screen_height);
	}
	
	mMediaTextureID = getKey();
	
	// We don't need to create the media source up front anymore unless we have a non-empty home URL to navigate to.
	if(!mHomePageUrl.empty())
	{
		navigateHome();
	}
		
	LLWindowShade::Params params;
	params.name = "notification_shade";
	params.rect = getLocalRect();
	params.follows.flags = FOLLOWS_ALL;
	params.modal = true;

	mWindowShade = LLUICtrlFactory::create<LLWindowShade>(params);

	addChild(mWindowShade);
}