3af4e11cd50a70c76d1150429e02a65325b354b4
[platform/core/uifw/dali-demo.git] / examples / tooltip / tooltip-example.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
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>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
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
33
34 /**
35  * @brief Creates a controller which demonstrates the tooltip functionality in control.
36  *
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.
39  */
40 class TooltipController : public ConnectionTracker
41 {
42 public:
43
44   TooltipController( Application& application )
45   : mApplication( application ),
46     previousPosition( 0.0f )
47   {
48     // Connect to the Application's Init signal
49     mApplication.InitSignal().Connect( this, &TooltipController::Create );
50   }
51
52 private:
53
54   // The Init signal is received once (only) during the Application lifetime
55   void Create( Application& application )
56   {
57     // Hide the indicator bar
58     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
59
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();
65
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" );
71     stage.Add( label );
72
73     // Simple tooltip from stylesheet
74     Control simple = PushButton::New();
75     simple.SetStyleName( "TooltipTextOnly" );
76     SetLabel( simple, "Simple" );
77     Layout( simple, stageSize );
78     stage.Add( simple );
79
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 );
86
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 );
93
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 ) ) )
113                               );
114     stage.Add( customFromCode );
115   }
116
117   /**
118    * @brief Called when any key event is received
119    *
120    * Will use this to quit the application if Back or the Escape key is received
121    * @param[in]  event  The key event information
122    */
123   void OnKeyEvent( const KeyEvent& event )
124   {
125     if( event.state == KeyEvent::Down )
126     {
127       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
128       {
129         mApplication.Quit();
130       }
131     }
132   }
133
134   /**
135    * @brief Sets the label on the control.
136    * @param[in]  label  The label to set.
137    */
138   void SetLabel( Control control, std::string label )
139   {
140     if( control )
141     {
142       control.SetProperty( Button::Property::LABEL,
143                            Property::Map().Add( Visual::Property::TYPE, DevelVisual::TEXT )
144                                           .Add( TextVisual::Property::TEXT, label ) );
145     }
146   }
147
148   /**
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.
152    */
153   void Layout( Control control, const Vector2& stageSize )
154   {
155     if( control )
156     {
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 );
163     }
164   }
165
166 private:
167   Application&  mApplication;
168   float previousPosition;
169 };
170
171 int DALI_EXPORT_API main( int argc, char **argv )
172 {
173   Application application = Application::New( &argc, &argv, THEME_PATH );
174
175   TooltipController test( application );
176
177   application.MainLoop();
178
179   return 0;
180 }