2020-12-21 17:00:29 +03:00
/*
* Copyright ( c ) 2020 , Alex McGrath < amk @ amk . ie >
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
*
* 1. Redistributions of source code must retain the above copyright notice , this
* list of conditions and the following disclaimer .
*
* 2. Redistributions in binary form must reproduce the above copyright notice ,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
* DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2021-02-22 01:06:38 +03:00
# include <LibGUI/Action.h>
# include <LibGUI/Clipboard.h>
2020-12-21 17:00:29 +03:00
# include <LibGUI/Event.h>
2020-12-26 15:00:24 +03:00
# include <LibGUI/LinkLabel.h>
2021-02-22 01:06:38 +03:00
# include <LibGUI/Menu.h>
2020-12-21 17:00:29 +03:00
# include <LibGUI/Painter.h>
# include <LibGUI/Window.h>
# include <LibGfx/Font.h>
# include <LibGfx/Palette.h>
2021-01-03 02:30:13 +03:00
REGISTER_WIDGET ( GUI , LinkLabel )
2020-12-21 17:00:29 +03:00
namespace GUI {
2020-12-26 15:07:02 +03:00
LinkLabel : : LinkLabel ( String text )
: Label ( move ( text ) )
2020-12-21 17:00:29 +03:00
{
2020-12-28 02:59:09 +03:00
set_override_cursor ( Gfx : : StandardCursor : : Hand ) ;
2020-12-21 17:00:29 +03:00
set_foreground_role ( Gfx : : ColorRole : : Link ) ;
2020-12-26 17:34:49 +03:00
set_focus_policy ( FocusPolicy : : TabFocus ) ;
2021-02-22 01:06:38 +03:00
setup_actions ( ) ;
}
void LinkLabel : : setup_actions ( )
{
2021-02-26 15:21:32 +03:00
m_open_action = GUI : : Action : : create ( " Show in File Manager " , { } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-file-manager.png " ) , [ & ] ( const GUI : : Action & ) {
2021-02-22 01:06:38 +03:00
if ( on_click )
on_click ( ) ;
2021-02-26 15:21:32 +03:00
} ) ;
2021-02-22 01:06:38 +03:00
m_copy_action = CommonActions : : make_copy_action ( [ this ] ( auto & ) { Clipboard : : the ( ) . set_plain_text ( text ( ) ) ; } , this ) ;
2020-12-21 17:00:29 +03:00
}
2020-12-26 15:17:57 +03:00
void LinkLabel : : mousedown_event ( MouseEvent & event )
2020-12-21 17:00:29 +03:00
{
2021-02-22 01:06:38 +03:00
if ( event . button ( ) ! = MouseButton : : Left )
return ;
2020-12-26 15:17:57 +03:00
Label : : mousedown_event ( event ) ;
2020-12-21 17:00:29 +03:00
if ( on_click ) {
on_click ( ) ;
}
}
2020-12-26 17:34:49 +03:00
void LinkLabel : : keydown_event ( KeyEvent & event )
{
Label : : keydown_event ( event ) ;
if ( event . key ( ) = = KeyCode : : Key_Return | | event . key ( ) = = KeyCode : : Key_Space ) {
if ( on_click )
on_click ( ) ;
}
}
2020-12-26 15:00:24 +03:00
void LinkLabel : : paint_event ( PaintEvent & event )
2020-12-21 17:00:29 +03:00
{
Label : : paint_event ( event ) ;
GUI : : Painter painter ( * this ) ;
if ( m_hovered )
2020-12-26 17:34:49 +03:00
painter . draw_line ( { 0 , rect ( ) . bottom ( ) } , { font ( ) . width ( text ( ) ) , rect ( ) . bottom ( ) } , palette ( ) . link ( ) ) ;
if ( is_focused ( ) )
painter . draw_focus_rect ( text_rect ( ) , palette ( ) . focus_outline ( ) ) ;
2020-12-21 17:00:29 +03:00
}
2020-12-26 15:17:57 +03:00
void LinkLabel : : enter_event ( Core : : Event & event )
2020-12-21 17:00:29 +03:00
{
2020-12-26 15:17:57 +03:00
Label : : enter_event ( event ) ;
2020-12-21 17:00:29 +03:00
m_hovered = true ;
update ( ) ;
}
2020-12-26 15:17:57 +03:00
void LinkLabel : : leave_event ( Core : : Event & event )
2020-12-21 17:00:29 +03:00
{
2020-12-26 15:17:57 +03:00
Label : : leave_event ( event ) ;
2020-12-21 17:00:29 +03:00
m_hovered = false ;
update ( ) ;
}
2020-12-26 15:17:57 +03:00
void LinkLabel : : did_change_text ( )
2020-12-21 17:00:29 +03:00
{
2020-12-26 15:17:57 +03:00
Label : : did_change_text ( ) ;
update_tooltip_if_needed ( ) ;
2020-12-21 17:00:29 +03:00
}
2020-12-26 15:17:57 +03:00
void LinkLabel : : update_tooltip_if_needed ( )
2020-12-21 17:00:29 +03:00
{
2020-12-26 15:17:57 +03:00
if ( width ( ) < font ( ) . width ( text ( ) ) ) {
2020-12-21 17:00:29 +03:00
set_tooltip ( text ( ) ) ;
} else {
set_tooltip ( { } ) ;
}
}
2020-12-26 15:17:57 +03:00
void LinkLabel : : resize_event ( ResizeEvent & event )
{
Label : : resize_event ( event ) ;
update_tooltip_if_needed ( ) ;
}
2021-02-22 01:06:38 +03:00
void LinkLabel : : context_menu_event ( ContextMenuEvent & event )
{
if ( ! m_context_menu ) {
m_context_menu = Menu : : construct ( ) ;
m_context_menu - > add_action ( * m_open_action ) ;
m_context_menu - > add_separator ( ) ;
m_context_menu - > add_action ( * m_copy_action ) ;
}
m_context_menu - > popup ( event . screen_position ( ) , m_open_action ) ;
}
2020-12-21 17:00:29 +03:00
}