2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/controls/tooltip/tooltip-properties.h>
21 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
22 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
25 using namespace Dali::Toolkit;
29 const Vector4 STAGE_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the stage
30 const char * const THEME_PATH( DEMO_STYLE_DIR "tooltip-example-theme.json" ); ///< The theme used for this example
31 const float POSITION_INCREMENTER( 0.2f ); ///< The position difference between the controls along the Y-Axis.
32 } // unnamed namespace
35 * @brief Creates a controller which demonstrates the tooltip functionality in control.
37 * The Control base class supports tooltip functionality. However, the Toolkit Tooltip style is only set on Buttons by default.
38 * This example portrays the different ways in which a tooltip can be displayed and customised.
40 class TooltipController : public ConnectionTracker
44 TooltipController( Application& application )
45 : mApplication( application ),
46 previousPosition( 0.0f )
48 // Connect to the Application's Init signal
49 mApplication.InitSignal().Connect( this, &TooltipController::Create );
54 // The Init signal is received once (only) during the Application lifetime
55 void Create( Application& application )
57 // Hide the indicator bar
58 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
60 // Set the stage background color and connect to the stage's key signal to allow Back and Escape to exit.
61 Stage stage = Stage::GetCurrent();
62 stage.SetBackgroundColor( STAGE_COLOR );
63 stage.KeyEventSignal().Connect( this, &TooltipController::OnKeyEvent );
64 const Vector2 stageSize = stage.GetSize();
66 // Add a text label at the top for information purposes
67 Control label = TextLabel::New( "Hover over buttons to see tooltip" );
68 label.SetParentOrigin( ParentOrigin::TOP_CENTER );
69 label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
70 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "Center" );
73 // Simple tooltip from stylesheet
74 Control simple = PushButton::New();
75 simple.SetStyleName( "TooltipTextOnly" );
76 SetLabel( simple, "Simple" );
77 Layout( simple, stageSize );
80 // Tooltip with icon and text, from stylesheet
81 Control iconWithText = PushButton::New();
82 iconWithText.SetStyleName( "TooltipArray" );
83 SetLabel( iconWithText, "Icon with Text" );
84 Layout( iconWithText, stageSize );
85 stage.Add( iconWithText );
87 // Tooltip with custom style, from stylesheet
88 Control customFromStylesheet = PushButton::New();
89 customFromStylesheet.SetStyleName( "TooltipCustom" );
90 SetLabel( customFromStylesheet, "Custom From Stylesheet" );
91 Layout( customFromStylesheet, stageSize );
92 stage.Add( customFromStylesheet );
94 // Tooltip with custom style, from code
95 Control customFromCode = PushButton::New();
96 SetLabel( customFromCode, "Custom From Code" );
97 Layout( customFromCode, stageSize );
98 customFromCode.SetProperty( DevelControl::Property::TOOLTIP,
99 Property::Map().Add( Tooltip::Property::CONTENT,
100 Property::Array().Add( Property::Map().Add( Visual::Property::TYPE, Visual::IMAGE )
101 .Add( ImageVisual::Property::URL, DEMO_IMAGE_DIR "Logo-for-demo.png" ) )
102 .Add( Property::Map().Add( Visual::Property::TYPE, DevelVisual::TEXT )
103 .Add( TextVisual::Property::TEXT_COLOR, Color::WHITE )
104 .Add( TextVisual::Property::TEXT, "Custom coded style\nat hover point" )
105 .Add( TextVisual::Property::MULTI_LINE, true )
106 .Add( TextVisual::Property::HORIZONTAL_ALIGNMENT, "CENTER" )
107 .Add( TextVisual::Property::POINT_SIZE, 16 ) ) )
108 .Add( Tooltip::Property::LAYOUT, Vector2( 2, 1 ) )
109 .Add( Tooltip::Property::POSITION, Tooltip::Position::HOVER_POINT )
110 .Add( Tooltip::Property::BACKGROUND,
111 Property::Map().Add( Tooltip::Background::Property::VISUAL, DEMO_IMAGE_DIR "tooltip.9.png" )
112 .Add( Tooltip::Background::Property::BORDER, Rect< int >( 1, 5, 5, 1 ) ) )
114 stage.Add( customFromCode );
118 * @brief Called when any key event is received
120 * Will use this to quit the application if Back or the Escape key is received
121 * @param[in] event The key event information
123 void OnKeyEvent( const KeyEvent& event )
125 if( event.state == KeyEvent::Down )
127 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
135 * @brief Sets the label on the control.
136 * @param[in] label The label to set.
138 void SetLabel( Control control, std::string label )
142 control.SetProperty( Button::Property::LABEL,
143 Property::Map().Add( Visual::Property::TYPE, DevelVisual::TEXT )
144 .Add( TextVisual::Property::TEXT, label ) );
149 * @brief Lays out the control in the appropriate location.
150 * @param[in] control The control to layout.
151 * @param[in] stageSize The size of the stage, passing it in so we don't have to retrieve it every time.
153 void Layout( Control control, const Vector2& stageSize )
157 previousPosition += POSITION_INCREMENTER;
158 control.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
159 control.SetSizeModeFactor( Vector3( 0.75, 0.1, 1.0 ) );
160 control.SetAnchorPoint( AnchorPoint::CENTER );
161 control.SetParentOrigin( ParentOrigin::TOP_CENTER );
162 control.SetY( stageSize.height * previousPosition );
167 Application& mApplication;
168 float previousPosition;
171 int DALI_EXPORT_API main( int argc, char **argv )
173 Application application = Application::New( &argc, &argv, THEME_PATH );
175 TooltipController test( application );
177 application.MainLoop();