PageRenderTime 67ms CodeModel.GetById 27ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llui/lltooltip.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 590 lines | 410 code | 92 blank | 88 comment | 51 complexity | 98c6f6f41d3b319f8af43a9938be2d77 MD5 | raw file
  1/** 
  2 * @file lltooltip.cpp
  3 * @brief LLToolTipMgr class implementation and related classes
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29// self include
 30#include "lltooltip.h"
 31
 32// Library includes
 33#include "lltextbox.h"
 34#include "lliconctrl.h"
 35#include "llbutton.h"
 36#include "llmenugl.h"       // hideMenus()
 37#include "llui.h"			// positionViewNearMouse()
 38#include "llwindow.h"
 39#include "lltrans.h"
 40//
 41// Constants
 42//
 43
 44//
 45// Local globals
 46//
 47
 48LLToolTipView *gToolTipView = NULL;
 49
 50//
 51// Member functions
 52//
 53
 54static LLDefaultChildRegistry::Register<LLToolTipView> register_tooltip_view("tooltip_view");
 55
 56LLToolTipView::Params::Params()
 57{
 58	changeDefault(mouse_opaque, false);
 59}
 60
 61LLToolTipView::LLToolTipView(const LLToolTipView::Params& p)
 62:	LLView(p)
 63{
 64}
 65
 66void LLToolTipView::draw()
 67{
 68	LLToolTipMgr::instance().updateToolTipVisibility();
 69
 70	// do the usual thing
 71	LLView::draw();
 72}
 73
 74BOOL LLToolTipView::handleHover(S32 x, S32 y, MASK mask)
 75{
 76	static S32 last_x = x;
 77	static S32 last_y = y;
 78
 79	LLToolTipMgr& tooltip_mgr = LLToolTipMgr::instance();
 80
 81	if (x != last_x && y != last_y && !tooltip_mgr.getMouseNearRect().pointInRect(x, y))
 82	{
 83		// allow new tooltips because mouse moved outside of mouse near rect
 84		tooltip_mgr.unblockToolTips();
 85	}
 86
 87	last_x = x;
 88	last_y = y;
 89	return LLView::handleHover(x, y, mask);
 90}
 91
 92BOOL LLToolTipView::handleMouseDown(S32 x, S32 y, MASK mask)
 93{
 94	LLToolTipMgr::instance().blockToolTips();
 95
 96	if (LLView::handleMouseDown(x, y, mask))
 97	{
 98		// If we are handling the mouse event menu holder 
 99		// won't get a chance to close menus so do this here 
100		LLMenuGL::sMenuContainer->hideMenus();
101		return TRUE;
102	}
103
104	return FALSE;
105}
106
107BOOL LLToolTipView::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
108{
109	LLToolTipMgr::instance().blockToolTips();
110	return LLView::handleMiddleMouseDown(x, y, mask);
111}
112
113BOOL LLToolTipView::handleRightMouseDown(S32 x, S32 y, MASK mask)
114{
115	LLToolTipMgr::instance().blockToolTips();
116	return LLView::handleRightMouseDown(x, y, mask);
117}
118
119
120BOOL LLToolTipView::handleScrollWheel( S32 x, S32 y, S32 clicks )
121{
122	LLToolTipMgr::instance().blockToolTips();
123	return FALSE;
124}
125
126void LLToolTipView::drawStickyRect()
127{
128	gl_rect_2d(LLToolTipMgr::instance().getMouseNearRect(), LLColor4::white, false);
129}
130
131// defaults for floater param block pulled from widgets/floater.xml
132static LLWidgetNameRegistry::StaticRegistrar sRegisterInspectorParams(&typeid(LLInspector::Params), "inspector");
133
134//
135// LLToolTip
136//
137
138
139static LLDefaultChildRegistry::Register<LLToolTip> register_tooltip("tool_tip");
140
141
142LLToolTip::Params::Params()
143:	max_width("max_width", 200),
144	padding("padding", 4),
145	wrap("wrap", true),
146	pos("pos"),
147	message("message"),
148	delay_time("delay_time", LLUI::sSettingGroups["config"]->getF32( "ToolTipDelay" )),
149	visible_time_over("visible_time_over", LLUI::sSettingGroups["config"]->getF32( "ToolTipVisibleTimeOver" )),
150	visible_time_near("visible_time_near", LLUI::sSettingGroups["config"]->getF32( "ToolTipVisibleTimeNear" )),
151	visible_time_far("visible_time_far", LLUI::sSettingGroups["config"]->getF32( "ToolTipVisibleTimeFar" )),
152	sticky_rect("sticky_rect"),
153	image("image"),
154	text_color("text_color"),
155	time_based_media("time_based_media", false),
156	web_based_media("web_based_media", false),
157	media_playing("media_playing", false)
158{
159	changeDefault(chrome, true);
160}
161
162LLToolTip::LLToolTip(const LLToolTip::Params& p)
163:	LLPanel(p),
164	mHasClickCallback(p.click_callback.isProvided()),
165	mPadding(p.padding),
166	mTextBox(NULL),
167	mInfoButton(NULL),
168	mPlayMediaButton(NULL),
169	mHomePageButton(NULL)
170{
171	LLTextBox::Params params;
172	params.name = params.initial_value().asString();
173	// bake textbox padding into initial rect
174	params.rect = LLRect (mPadding, mPadding + 1, mPadding + 1, mPadding);
175	params.h_pad = 0;
176	params.v_pad = 0;
177	params.mouse_opaque = false;
178	params.text_color = p.text_color;
179	params.bg_visible = false;
180	params.font = p.font;
181	params.use_ellipses = true;
182	params.wrap = p.wrap;
183	params.parse_urls = false; // disallow hyperlinks in tooltips, as they want to spawn their own explanatory tooltips
184	mTextBox = LLUICtrlFactory::create<LLTextBox> (params);
185	addChild(mTextBox);
186	
187	S32 TOOLTIP_ICON_SIZE = 0;
188	S32 TOOLTIP_PLAYBUTTON_SIZE = 0;
189	if (p.image.isProvided())
190	{
191		LLButton::Params icon_params;
192		icon_params.name = "tooltip_info";
193		icon_params.label(""); // provid label but set to empty so name does not overwrite it -angela
194		LLRect icon_rect;
195		LLUIImage* imagep = p.image;
196		TOOLTIP_ICON_SIZE = (imagep ? imagep->getWidth() : 16);
197		icon_rect.setOriginAndSize(mPadding, mPadding, TOOLTIP_ICON_SIZE, TOOLTIP_ICON_SIZE);
198		icon_params.rect = icon_rect;
199		icon_params.image_unselected(imagep);
200		icon_params.image_selected(imagep);
201
202		icon_params.scale_image(true);
203		icon_params.flash_color.control = "ButtonUnselectedFgColor";
204		mInfoButton  = LLUICtrlFactory::create<LLButton>(icon_params);
205		if (p.click_callback.isProvided())
206		{
207			mInfoButton->setCommitCallback(boost::bind(p.click_callback()));
208		}
209		addChild(mInfoButton);
210		
211		// move text over to fit image in
212		mTextBox->translate(TOOLTIP_ICON_SIZE + mPadding, 0);
213	}
214	
215	if (p.time_based_media)
216	{
217		LLButton::Params p_button;
218		p_button.name(std::string("play_media"));
219		p_button.label(""); // provide label but set to empty so name does not overwrite it -angela
220		TOOLTIP_PLAYBUTTON_SIZE = 16;
221		LLRect button_rect;
222		button_rect.setOriginAndSize((mPadding +TOOLTIP_ICON_SIZE+ mPadding ), mPadding, TOOLTIP_ICON_SIZE, TOOLTIP_ICON_SIZE);
223		p_button.rect = button_rect;
224		p_button.image_selected.name("button_anim_pause.tga");
225		p_button.image_unselected.name("button_anim_play.tga");
226		p_button.scale_image(true);
227		
228		mPlayMediaButton = LLUICtrlFactory::create<LLButton>(p_button); 
229		if(p.click_playmedia_callback.isProvided())
230		{
231			mPlayMediaButton->setCommitCallback(boost::bind(p.click_playmedia_callback()));
232		}
233		mPlayMediaButton->setToggleState(p.media_playing);
234		addChild(mPlayMediaButton);
235		
236		// move text over to fit image in
237		mTextBox->translate(TOOLTIP_PLAYBUTTON_SIZE + mPadding, 0);
238	}
239	
240	if (p.web_based_media)
241	{
242		LLButton::Params p_w_button;
243		p_w_button.name(std::string("home_page"));
244		p_w_button.label(""); // provid label but set to empty so name does not overwrite it -angela
245		TOOLTIP_PLAYBUTTON_SIZE = 16;
246		LLRect button_rect;
247		button_rect.setOriginAndSize((mPadding +TOOLTIP_ICON_SIZE+ mPadding ), mPadding, TOOLTIP_ICON_SIZE, TOOLTIP_ICON_SIZE);
248		p_w_button.rect = button_rect;
249		p_w_button.image_unselected.name("map_home.tga");
250		p_w_button.scale_image(true);
251		
252		mHomePageButton = LLUICtrlFactory::create<LLButton>(p_w_button); 
253		if(p.click_homepage_callback.isProvided())
254		{
255			mHomePageButton->setCommitCallback(boost::bind(p.click_homepage_callback()));
256		}
257		addChild(mHomePageButton);
258		
259		// move text over to fit image in
260		mTextBox->translate(TOOLTIP_PLAYBUTTON_SIZE + mPadding, 0);
261	}
262	
263	if (p.click_callback.isProvided())
264	{
265		setMouseUpCallback(boost::bind(p.click_callback()));
266	}
267}
268
269void LLToolTip::initFromParams(const LLToolTip::Params& p)
270{
271	LLPanel::initFromParams(p);
272
273	// do this *after* we've had our size set in LLPanel::initFromParams();
274	const S32 REALLY_LARGE_HEIGHT = 10000;
275	mTextBox->reshape(p.max_width, REALLY_LARGE_HEIGHT);
276
277	if (p.styled_message.isProvided())
278	{
279		for (LLInitParam::ParamIterator<LLToolTip::StyledText>::const_iterator text_it = p.styled_message.begin();
280			text_it != p.styled_message.end();
281			++text_it)
282		{
283			mTextBox->appendText(text_it->text(), false, text_it->style);
284		}
285	}
286	else
287	{
288		mTextBox->setText(p.message());
289	}
290
291	S32 text_width = llmin(p.max_width(), mTextBox->getTextPixelWidth());
292	S32 text_height = mTextBox->getTextPixelHeight();
293	mTextBox->reshape(text_width, text_height);
294
295	// reshape tooltip panel to fit text box
296	LLRect tooltip_rect = calcBoundingRect();
297	tooltip_rect.mTop += mPadding;
298	tooltip_rect.mRight += mPadding;
299	tooltip_rect.mBottom = 0;
300	tooltip_rect.mLeft = 0;
301
302	setShape(tooltip_rect);
303}
304
305void LLToolTip::setVisible(BOOL visible)
306{
307	// fade out tooltip over time
308	if (visible)
309	{
310		mVisibleTimer.start();
311		mFadeTimer.stop();
312		LLPanel::setVisible(TRUE);
313	}
314	else
315	{
316		mVisibleTimer.stop();
317		// don't actually change mVisible state, start fade out transition instead
318		if (!mFadeTimer.getStarted())
319		{
320			mFadeTimer.start();
321		}
322	}
323}
324
325BOOL LLToolTip::handleHover(S32 x, S32 y, MASK mask)
326{
327	//mInfoButton->setFlashing(true);
328	if(mInfoButton)
329		mInfoButton->setHighlight(true);
330	
331	LLPanel::handleHover(x, y, mask);
332	if (mHasClickCallback)
333	{
334		getWindow()->setCursor(UI_CURSOR_HAND);
335	}
336	return TRUE;
337}
338
339void LLToolTip::onMouseLeave(S32 x, S32 y, MASK mask)
340{
341	//mInfoButton->setFlashing(true);
342	if(mInfoButton)
343		mInfoButton->setHighlight(false);
344	LLUICtrl::onMouseLeave(x, y, mask);
345}
346
347void LLToolTip::draw()
348{
349	F32 alpha = 1.f;
350
351	if (mFadeTimer.getStarted())
352	{
353		F32 tool_tip_fade_time = LLUI::sSettingGroups["config"]->getF32("ToolTipFadeTime");
354		alpha = clamp_rescale(mFadeTimer.getElapsedTimeF32(), 0.f, tool_tip_fade_time, 1.f, 0.f);
355		if (alpha == 0.f)
356		{
357			// finished fading out, so hide ourselves
358			mFadeTimer.stop();
359			LLPanel::setVisible(false);
360		}
361	}
362
363	// draw tooltip contents with appropriate alpha
364	{
365		LLViewDrawContext context(alpha);
366		LLPanel::draw();
367	}
368}
369
370bool LLToolTip::isFading() 
371{ 
372	return mFadeTimer.getStarted(); 
373}
374
375F32 LLToolTip::getVisibleTime() 
376{ 
377	return mVisibleTimer.getStarted() ? mVisibleTimer.getElapsedTimeF32() : 0.f; 
378}
379
380bool LLToolTip::hasClickCallback() 
381{
382	return mHasClickCallback; 
383}
384
385
386//
387// LLToolTipMgr
388//
389
390LLToolTipMgr::LLToolTipMgr()
391:       mToolTipsBlocked(false),
392	mToolTip(NULL),
393	mNeedsToolTip(false)
394{}
395
396void LLToolTipMgr::createToolTip(const LLToolTip::Params& params)
397{
398	// block all other tooltips until tooltips re-enabled (e.g. mouse moved)
399	blockToolTips(); 
400
401	delete mToolTip;
402
403	LLToolTip::Params tooltip_params(params);
404	// block mouse events if there is a click handler registered (specifically, hover)
405	if (params.click_callback.isProvided() && !params.mouse_opaque.isProvided())
406	{
407		// set mouse_opaque to true if it wasn't already set to something else
408		// this prevents mouse down from going "through" the tooltip and ultimately
409		// causing the tooltip to disappear
410		tooltip_params.mouse_opaque = true;
411	}
412	tooltip_params.rect = LLRect (0, 1, 1, 0);
413
414	mToolTip = LLUICtrlFactory::create<LLToolTip> (tooltip_params);
415
416	gToolTipView->addChild(mToolTip);
417
418	if (params.pos.isProvided())
419	{
420		LLCoordGL pos = params.pos;
421		// try to spawn at requested position
422		LLUI::positionViewNearMouse(mToolTip, pos.mX, pos.mY);
423	}
424	else
425	{
426		// just spawn at mouse location
427		LLUI::positionViewNearMouse(mToolTip);
428	}
429
430	//...update "sticky" rect and tooltip position
431	if (params.sticky_rect.isProvided())
432	{
433		mMouseNearRect = params.sticky_rect;
434	}
435	else
436	{
437		S32 mouse_x;
438		S32 mouse_y;
439		LLUI::getMousePositionLocal(gToolTipView->getParent(), &mouse_x, &mouse_y);
440
441		// allow mouse a little bit of slop before changing tooltips
442		mMouseNearRect.setCenterAndSize(mouse_x, mouse_y, 3, 3);
443	}
444
445	// allow mouse to move all the way to the tooltip without changing tooltips
446	// (tooltip can still time out)
447	if (mToolTip->hasClickCallback())
448	{
449		// keep tooltip up when we mouse over it
450		mMouseNearRect.unionWith(mToolTip->getRect());
451	}
452}
453
454
455void LLToolTipMgr::show(const std::string& msg)
456{
457	show(LLToolTip::Params().message(msg));
458}
459
460void LLToolTipMgr::show(const LLToolTip::Params& params)
461{
462	// fill in default tooltip params from tool_tip.xml
463	LLToolTip::Params params_with_defaults(params);
464	params_with_defaults.fillFrom(LLUICtrlFactory::instance().getDefaultParams<LLToolTip>());
465	if (!params_with_defaults.validateBlock()) 
466	{
467		llwarns << "Could not display tooltip!" << llendl;
468		return;
469	}
470	
471	S32 mouse_x;
472	S32 mouse_y;
473	LLUI::getMousePositionLocal(gToolTipView, &mouse_x, &mouse_y);
474
475	// are we ready to show the tooltip?
476	if (!mToolTipsBlocked									// we haven't hit a key, moved the mouse, etc.
477		&& LLUI::getMouseIdleTime() > params_with_defaults.delay_time)	// the mouse has been still long enough
478	{
479		bool tooltip_changed = mLastToolTipParams.message() != params_with_defaults.message()
480								|| mLastToolTipParams.pos() != params_with_defaults.pos()
481								|| mLastToolTipParams.time_based_media() != params_with_defaults.time_based_media()
482								|| mLastToolTipParams.web_based_media() != params_with_defaults.web_based_media();
483
484		bool tooltip_shown = mToolTip 
485							 && mToolTip->getVisible() 
486							 && !mToolTip->isFading();
487
488		mNeedsToolTip = tooltip_changed || !tooltip_shown;
489		// store description of tooltip for later creation
490		mNextToolTipParams = params_with_defaults;
491	}
492}
493
494// allow new tooltips to be created, e.g. after mouse has moved
495void LLToolTipMgr::unblockToolTips()
496{
497	mToolTipsBlocked = false;
498}
499
500// disallow new tooltips until unblockTooltips called
501void LLToolTipMgr::blockToolTips()
502{
503	hideToolTips();
504	mToolTipsBlocked = true;
505}
506
507void LLToolTipMgr::hideToolTips() 
508{ 
509	if (mToolTip)
510	{
511		mToolTip->setVisible(FALSE);
512	}
513}
514
515bool LLToolTipMgr::toolTipVisible()
516{
517	return mToolTip ? mToolTip->isInVisibleChain() : false;
518}
519
520LLRect LLToolTipMgr::getToolTipRect()
521{
522	if (mToolTip && mToolTip->getVisible())
523	{
524		return mToolTip->getRect();
525	}
526	return LLRect();
527}
528
529
530LLRect LLToolTipMgr::getMouseNearRect() 
531{ 
532	return toolTipVisible() ? mMouseNearRect : LLRect(); 
533}
534
535// every frame, determine if current tooltip should be hidden
536void LLToolTipMgr::updateToolTipVisibility()
537{
538	// create new tooltip if we have one ready to go
539	if (mNeedsToolTip)
540	{
541		mNeedsToolTip = false;
542		createToolTip(mNextToolTipParams);
543		mLastToolTipParams = mNextToolTipParams;
544		
545		return;
546	}
547
548	// hide tooltips when mouse cursor is hidden
549	if (LLUI::getWindow()->isCursorHidden())
550	{
551		blockToolTips();
552		return;
553	}
554
555	// hide existing tooltips if they have timed out
556	S32 mouse_x, mouse_y;
557	LLUI::getMousePositionLocal(gToolTipView, &mouse_x, &mouse_y);
558
559	F32 tooltip_timeout = 0.f;
560	if (toolTipVisible())
561	{
562		// mouse far away from tooltip
563		tooltip_timeout = mLastToolTipParams.visible_time_far;
564		// mouse near rect will only include the tooltip if the 
565		// tooltip is clickable
566		if (mMouseNearRect.pointInRect(mouse_x, mouse_y))
567		{
568			// mouse "close" to tooltip
569			tooltip_timeout = mLastToolTipParams.visible_time_near;
570
571			// if tooltip is clickable (has large mMouseNearRect)
572			// than having cursor over tooltip keeps it up indefinitely
573			if (mToolTip->parentPointInView(mouse_x, mouse_y))
574			{
575				// mouse over tooltip itself, don't time out
576				tooltip_timeout = mLastToolTipParams.visible_time_over;
577			}
578		}
579
580		if (mToolTip->getVisibleTime() > tooltip_timeout)
581		{
582			hideToolTips();
583			unblockToolTips();
584		}
585	}
586}
587
588
589
590// EOF