summaryrefslogtreecommitdiff
path: root/indra/llui/lltooltip.cpp
blob: f30e56b907a5552e50f67b7a54b4fb990217f1a9 (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
/** 
 * @file lltooltip.cpp
 * @brief LLToolTipMgr class implementation and related classes
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-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 "linden_common.h"

// self include
#include "lltooltip.h"

// Library includes
#include "lltextbox.h"
#include "lliconctrl.h"
#include "llmenugl.h"       // hideMenus()
#include "llui.h"			// positionViewNearMouse()
#include "llwindow.h"

//
// Constants
//

//
// Local globals
//

LLToolTipView *gToolTipView = NULL;

//
// Member functions
//

LLToolTipView::Params::Params()
{
	mouse_opaque = false;
}

LLToolTipView::LLToolTipView(const LLToolTipView::Params& p)
:	LLView(p)
{
}

void LLToolTipView::draw()
{
	LLToolTipMgr::instance().updateToolTipVisibility();

	// do the usual thing
	LLView::draw();
}

BOOL LLToolTipView::handleHover(S32 x, S32 y, MASK mask)
{
	static S32 last_x = x;
	static S32 last_y = y;

	LLToolTipMgr& tooltip_mgr = LLToolTipMgr::instance();

	if (x != last_x && y != last_y)
	{
		// allow new tooltips because mouse moved
		tooltip_mgr.unblockToolTips();
	}

	last_x = x;
	last_y = y;
	return LLView::handleHover(x, y, mask);
}

BOOL LLToolTipView::handleMouseDown(S32 x, S32 y, MASK mask)
{
	LLToolTipMgr::instance().blockToolTips();

	if (LLView::handleMouseDown(x, y, mask))
	{
		// If we are handling the mouse event menu holder 
		// won't get a chance to close menus so do this here 
		LLMenuGL::sMenuContainer->hideMenus();
		return TRUE;
	}

	return FALSE;
}

BOOL LLToolTipView::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
{
	LLToolTipMgr::instance().blockToolTips();
	return LLView::handleMiddleMouseDown(x, y, mask);
}

BOOL LLToolTipView::handleRightMouseDown(S32 x, S32 y, MASK mask)
{
	LLToolTipMgr::instance().blockToolTips();
	return LLView::handleRightMouseDown(x, y, mask);
}


BOOL LLToolTipView::handleScrollWheel( S32 x, S32 y, S32 clicks )
{
	LLToolTipMgr::instance().blockToolTips();
	return FALSE;
}

void LLToolTipView::onMouseLeave(S32 x, S32 y, MASK mask)
{
	LLToolTipMgr::instance().blockToolTips();
}


void LLToolTipView::drawStickyRect()
{
	gl_rect_2d(LLToolTipMgr::instance().getMouseNearRect(), LLColor4::white, false);
}
//
// LLToolTip
//


static LLDefaultChildRegistry::Register<LLToolTip> r("tool_tip");


LLToolTip::Params::Params()
:	max_width("max_width", 200),
	padding("padding", 4),
	wrap("wrap", true),
	pos("pos"),
	message("message"),
	delay_time("delay_time", LLUI::sSettingGroups["config"]->getF32( "ToolTipDelay" )),
	visible_time_over("visible_time_over", LLUI::sSettingGroups["config"]->getF32( "ToolTipVisibleTimeOver" )),
	visible_time_near("visible_time_near", LLUI::sSettingGroups["config"]->getF32( "ToolTipVisibleTimeNear" )),
	visible_time_far("visible_time_far", LLUI::sSettingGroups["config"]->getF32( "ToolTipVisibleTimeFar" )),
	sticky_rect("sticky_rect"),
	image("image")
{
	name = "tooltip";
	font = LLFontGL::getFontSansSerif();
	bg_opaque_color = LLUIColorTable::instance().getColor( "ToolTipBgColor" );
	background_visible = true;
}

LLToolTip::LLToolTip(const LLToolTip::Params& p)
:	LLPanel(p),
	mMaxWidth(p.max_width),
	mHasClickCallback(p.click_callback.isProvided()),
	mPadding(p.padding)
{
	LLTextBox::Params params;
	params.initial_value = "tip_text";
	params.name = params.initial_value().asString();
	// bake textbox padding into initial rect
	params.rect = LLRect (mPadding, mPadding + 1, mPadding + 1, mPadding);
	params.follows.flags = FOLLOWS_ALL;
	params.h_pad = 0;
	params.v_pad = 0;
	params.mouse_opaque = false;
	params.text_color = LLUIColorTable::instance().getColor( "ToolTipTextColor" );
	params.bg_visible = false;
	params.font = p.font;
	params.use_ellipses = true;
	params.wrap = p.wrap;
	mTextBox = LLUICtrlFactory::create<LLTextBox> (params);
	addChild(mTextBox);

	if (p.image.isProvided())
	{
		LLIconCtrl::Params icon_params;
		icon_params.name = "tooltip_icon";
		LLRect icon_rect;
		LLUIImage* imagep = p.image;
		const S32 TOOLTIP_ICON_SIZE = (imagep ? imagep->getWidth() : 16);
		icon_rect.setOriginAndSize(mPadding, mPadding, TOOLTIP_ICON_SIZE, TOOLTIP_ICON_SIZE);
		icon_params.rect = icon_rect;
		icon_params.follows.flags = FOLLOWS_LEFT | FOLLOWS_BOTTOM;
		icon_params.image = p.image;
		icon_params.mouse_opaque = false;
		addChild(LLUICtrlFactory::create<LLIconCtrl>(icon_params));

		// move text over to fit image in
		mTextBox->translate(TOOLTIP_ICON_SIZE + mPadding, 0);
	}

	if (p.click_callback.isProvided())
	{
		setMouseUpCallback(boost::bind(p.click_callback()));
	}
}

void LLToolTip::setValue(const LLSD& value)
{
	const S32 REALLY_LARGE_HEIGHT = 10000;
	reshape(mMaxWidth, REALLY_LARGE_HEIGHT);

	mTextBox->setValue(value);

	LLRect text_contents_rect = mTextBox->getContentsRect();
	S32 text_width = llmin(mMaxWidth, text_contents_rect.getWidth());
	S32 text_height = text_contents_rect.getHeight();
	mTextBox->reshape(text_width, text_height);

	// reshape tooltip panel to fit text box
	LLRect tooltip_rect = calcBoundingRect();
	tooltip_rect.mTop += mPadding;
	tooltip_rect.mRight += mPadding;
	tooltip_rect.mBottom = 0;
	tooltip_rect.mLeft = 0;

	setRect(tooltip_rect);
}

void LLToolTip::setVisible(BOOL visible)
{
	// fade out tooltip over time
	if (visible)
	{
		mVisibleTimer.start();
		mFadeTimer.stop();
		LLPanel::setVisible(TRUE);
	}
	else
	{
		mVisibleTimer.stop();
		// don't actually change mVisible state, start fade out transition instead
		if (!mFadeTimer.getStarted())
		{
			mFadeTimer.start();
		}
	}
}

BOOL LLToolTip::handleHover(S32 x, S32 y, MASK mask)
{
	LLPanel::handleHover(x, y, mask);
	if (mHasClickCallback)
	{
		getWindow()->setCursor(UI_CURSOR_HAND);
	}
	return TRUE;
}

void LLToolTip::draw()
{
	F32 alpha = 1.f;

	if (mFadeTimer.getStarted())
	{
		F32 tool_tip_fade_time = LLUI::sSettingGroups["config"]->getF32("ToolTipFadeTime");
		alpha = clamp_rescale(mFadeTimer.getElapsedTimeF32(), 0.f, tool_tip_fade_time, 1.f, 0.f);
		if (alpha == 0.f)
		{
			// finished fading out, so hide ourselves
			mFadeTimer.stop();
			LLPanel::setVisible(false);
		}
	}

	// draw tooltip contents with appropriate alpha
	{
		LLViewDrawContext context(alpha);
		LLPanel::draw();
	}
}

bool LLToolTip::isFading() 
{ 
	return mFadeTimer.getStarted(); 
}

F32 LLToolTip::getVisibleTime() 
{ 
	return mVisibleTimer.getStarted() ? mVisibleTimer.getElapsedTimeF32() : 0.f; 
}

bool LLToolTip::hasClickCallback() 
{
	return mHasClickCallback; 
}


//
// LLToolTipMgr
//

LLToolTipMgr::LLToolTipMgr()
:	mToolTip(NULL),
	mNeedsToolTip(false)
{}

void LLToolTipMgr::createToolTip(const LLToolTip::Params& params)
{
	// block all other tooltips until tooltips re-enabled (e.g. mouse moved)
	blockToolTips(); 

	delete mToolTip;

	LLToolTip::Params tooltip_params(params);
	// block mouse events if there is a click handler registered (specifically, hover)
	tooltip_params.mouse_opaque = params.click_callback.isProvided();
	tooltip_params.rect = LLRect (0, 1, 1, 0);

	mToolTip = LLUICtrlFactory::create<LLToolTip> (tooltip_params);
	mToolTip->setValue(params.message());
	gToolTipView->addChild(mToolTip);

	if (params.pos.isProvided())
	{
		LLCoordGL pos = params.pos;
		// try to spawn at requested position
		LLUI::positionViewNearMouse(mToolTip, pos.mX, pos.mY);
	}
	else
	{
		// just spawn at mouse location
		LLUI::positionViewNearMouse(mToolTip);
	}

	//...update "sticky" rect and tooltip position
	if (params.sticky_rect.isProvided())
	{
		mMouseNearRect = params.sticky_rect;
	}
	else
	{
		S32 mouse_x;
		S32 mouse_y;
		LLUI::getMousePositionLocal(gToolTipView->getParent(), &mouse_x, &mouse_y);

		// allow mouse a little bit of slop before changing tooltips
		mMouseNearRect.setCenterAndSize(mouse_x, mouse_y, 3, 3);
	}

	// allow mouse to move all the way to the tooltip without changing tooltips
	// (tooltip can still time out)
	if (mToolTip->hasClickCallback())
	{
		// keep tooltip up when we mouse over it
		mMouseNearRect.unionWith(mToolTip->getRect());
	}
}


void LLToolTipMgr::show(const std::string& msg)
{
	show(LLToolTip::Params().message(msg));
}

void LLToolTipMgr::show(const LLToolTip::Params& params)
{
	if (!params.validateBlock()) 
	{
		llwarns << "Could not display tooltip!" << llendl;
		return;
	}
	
	S32 mouse_x;
	S32 mouse_y;
	LLUI::getMousePositionLocal(gToolTipView, &mouse_x, &mouse_y);

	// are we ready to show the tooltip?
	if (!mToolTipsBlocked									// we haven't hit a key, moved the mouse, etc.
		&& LLUI::getMouseIdleTime() > params.delay_time)	// the mouse has been still long enough
	{
		bool tooltip_changed = mLastToolTipParams.message() != params.message()
								|| mLastToolTipParams.pos() != params.pos();

		bool tooltip_shown = mToolTip 
							 && mToolTip->getVisible() 
							 && !mToolTip->isFading();

		mNeedsToolTip = tooltip_changed || !tooltip_shown;
		// store description of tooltip for later creation
		mNextToolTipParams = params;
	}
}

// allow new tooltips to be created, e.g. after mouse has moved
void LLToolTipMgr::unblockToolTips()
{
	mToolTipsBlocked = false;
}

// disallow new tooltips until unblockTooltips called
void LLToolTipMgr::blockToolTips()
{
	hideToolTips();
	mToolTipsBlocked = true;
}

void LLToolTipMgr::hideToolTips() 
{ 
	if (mToolTip)
	{
		mToolTip->setVisible(FALSE);
	}
}

bool LLToolTipMgr::toolTipVisible()
{
	return mToolTip ? mToolTip->isInVisibleChain() : false;
}

LLRect LLToolTipMgr::getToolTipRect()
{
	if (mToolTip && mToolTip->getVisible())
	{
		return mToolTip->getRect();
	}
	return LLRect();
}


LLRect LLToolTipMgr::getMouseNearRect() 
{ 
	return toolTipVisible() ? mMouseNearRect : LLRect(); 
}

// every frame, determine if current tooltip should be hidden
void LLToolTipMgr::updateToolTipVisibility()
{
	// create new tooltip if we have one ready to go
	if (mNeedsToolTip)
	{
		mNeedsToolTip = false;
		createToolTip(mNextToolTipParams);
		mLastToolTipParams = mNextToolTipParams;
		
		return;
	}

	// hide tooltips when mouse cursor is hidden
	if (LLUI::getWindow()->isCursorHidden())
	{
		blockToolTips();
		return;
	}

	// hide existing tooltips if they have timed out
	S32 mouse_x, mouse_y;
	LLUI::getMousePositionLocal(gToolTipView, &mouse_x, &mouse_y);

	F32 tooltip_timeout = 0.f;
	if (toolTipVisible())
	{
		// mouse far away from tooltip
		tooltip_timeout = mLastToolTipParams.visible_time_far;
		// mouse near rect will only include the tooltip if the 
		// tooltip is clickable
		if (mMouseNearRect.pointInRect(mouse_x, mouse_y))
		{
			// mouse "close" to tooltip
			tooltip_timeout = mLastToolTipParams.visible_time_near;

			// if tooltip is clickable (has large mMouseNearRect)
			// than having cursor over tooltip keeps it up indefinitely
			if (mToolTip->parentPointInView(mouse_x, mouse_y))
			{
				// mouse over tooltip itself, don't time out
				tooltip_timeout = mLastToolTipParams.visible_time_over;
			}
		}

		if (mToolTip->getVisibleTime() > tooltip_timeout)
		{
			hideToolTips();
		}
	}
}



// EOF