B4J Question BCTooltip using BBCodeView instead of BBLabel

Chris2

Active Member
Licensed User
Longtime User
I'm trying to adjust @Erel's BCToolTip code to have an option to use a BBCodeView instead of a BBLabel. This is with the intention of adding clickable links into the tooltip.

I've attached an example of what I've done. One button dispays the tooltip using a BBLable, the other a BBCodeView.
The problem I'm having is with getting the BBCodeView version to adjust the size of the tooltip nicely.
I think the issue is with line 102 in the BCTooTip_BBCodeView class which doesn't seem to work in the same way that it does with the BBLabel.

Can anyone tell me how to adjust the line:
B4X:
pnlBackground.SetLayoutAnimated(0, BBC1.mBase.Left + iv.Left - GapFromText, BBC1.mBase.Top + iv.Top - GapFromText, iv.Width + 2 * GapFromText, iv.Height + 2 * GapFromText)
so that it adjusts the size of the tootip to fit the contents nicely?

BCToolTip_BBCodeView:
'Based on orginal BCToolTip class - https://www.b4x.com/android/forum/threads/b4x-bctooltip-cross-platform-tool-tips.119621/
'Trying to use BBCodeView instead of BBLabel so that we can have lnks in the tooltip
'
'Added option to use BBCodeView instead if BBLabel to get clickable links
'Where changes made, original code is commented but left in.
'Added UseBBCodeView boolean to initialize sub, BBCodeView1 and mUseBBCodeView globals, and BCTootlTip2 layout
'Change in #if B4A.. section of Innitialize - IIf(mUseBBCodeView, BBCodeView1.mBase, BB1.mBase)
'Other changes using
'If mUseBBCodeView Then
'        ...
'    Else
'        ...
'End If
'in Intialize and Show subs
'Added BBC1_LinkClicked event handler.
'
'
'
Sub Class_Globals
    Public pnl As B4XView
    Private xui As XUI
    Public BB1 As BBLabel
    Private te As BCTextEngine
    Public DefaultTextColor As Int = 0xFF3E3E3E
    Public BackgroundColor As Int = 0xFFE2E2E2
    Public pnlBackground As B4XView
    Private mParent As B4XView
    Private GapFromText As Int = 20dip
    Private cvs As B4XCanvas
    Private pnlArrow As B4XView
    Private pnlTouch As B4XView
    Private ShowIndex As Int
    Public AutoHideMs As Int
  
    'Added by Chris2
    Public BBC1 As BBCodeView
    Private mUseBBCodeView As Boolean
  
End Sub

'if UseBBCodeView is True then a BBCode view wil be iused instead of a BBLabel
Public Sub Initialize (Parent As B4XView, MaxWidth As Int, UseBBCodeView As Boolean)
    mParent = Parent
    pnl = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, MaxWidth, 450dip)
    mUseBBCodeView=UseBBCodeView
'    pnl.LoadLayout("BCToolTip")
    If mUseBBCodeView Then pnl.LoadLayout("BCToolTip2") Else pnl.LoadLayout("BCToolTip")
    cvs.Initialize(pnlArrow)
    pnl.Color = xui.Color_Transparent
    te.Initialize(pnl)
  
'    BB1.DisableResizeEvent = True
'    BB1.Padding.Initialize(0, 0, 0, 0)
    If mUseBBCodeView Then
        BBC1.TextEngine = te
        BBC1.Padding.Initialize(0, 0, 0, 0)
    Else
        BB1.DisableResizeEvent = True
        BB1.Padding.Initialize(0, 0, 0, 0)
    End If
  
  
#if B4A
'    For Each p As Panel In Array(pnl, pnlBackground, pnlTouch, pnlArrow, BB1.mBase)
    For Each p As Pane In Array(pnl, pnlBackground, pnlTouch, pnlArrow, IIf(mUseBBCodeView, BBC1.mBase, BB1.mBase))
        p.Elevation = 5dip
    Next
#Else If B4i
    Dim p As Panel = pnl
    p.SetShadow(xui.Color_Black, 2dip, 2dip, 0.5, False)
#End If
End Sub

'Shows the tooltip.
'X / Y - Arrow point.
'Side - One of the following values: LEFT, TOP, RIGHT, BOTTOM
'Message - BBCode message.
Public Sub Show (X As Int, Y As Int, Side As String, Message As String)
    ShowIndex = ShowIndex + 1
    Dim MyIndex As Int = ShowIndex
  
'    BB1.ParseData.DefaultColor = DefaultTextColor
'    BB1.Text = Message
    If mUseBBCodeView Then
        BBC1.ParseData.DefaultColor = DefaultTextColor
        BBC1.Text = Message
    Else
        BB1.ParseData.DefaultColor = DefaultTextColor
        BB1.Text = Message
    End If
  
    If pnl.Parent.IsInitialized Then pnl.RemoveViewFromParent
    mParent.AddView(pnl, 100dip, 100dip, pnl.Width, pnl.Height)
  
    pnlBackground.Color = BackgroundColor
  
'    Dim iv As B4XView = BB1.ForegroundImageView
'    pnlBackground.SetLayoutAnimated(0, BB1.mBase.Left + iv.Left - GapFromText, BB1.mBase.Top + iv.Top - GapFromText, iv.Width + 2 * GapFromText, iv.Height + 2 * GapFromText)
    If mUseBBCodeView Then
        Dim iv As B4XView = BBC1.ForegroundImageView
'      The line below does not seem to work well for BBCodeView...
        pnlBackground.SetLayoutAnimated(0, BBC1.mBase.Left + iv.Left - GapFromText, BBC1.mBase.Top + iv.Top - GapFromText, iv.Width + 2 * GapFromText, iv.Height + 2 * GapFromText)
'        pnlBackground.SetLayoutAnimated(0, 0, 0, 500, 500)
'        pnlBackground.SetLayoutAnimated(0, BBC1.mBase.Left + iv.Left - GapFromText, BBC1.mBase.Top + iv.Top - GapFromText, BBC1.mBase.Width, BBC1.mBase.Height)
    Else
        Dim iv As B4XView = BB1.ForegroundImageView
        pnlBackground.SetLayoutAnimated(0, BB1.mBase.Left + iv.Left - GapFromText, BB1.mBase.Top + iv.Top - GapFromText, iv.Width + 2 * GapFromText, iv.Height + 2 * GapFromText)
    End If
  
    If xui.IsB4i Then
        pnlTouch.SetLayoutAnimated(0, 0, 0, pnl.Width, pnl.Height)
    Else
        pnlTouch.SetLayoutAnimated(0, pnlBackground.Left, pnlBackground.Top, pnlBackground.Width, pnlBackground.Height)
    End If
    cvs.ClearRect(cvs.TargetRect)
    Dim p As B4XPath
    Select Side
        Case "TOP"
            pnlArrow.SetLayoutAnimated(0, pnl.Width / 2 - pnlArrow.Width / 2, pnlBackground.Top + pnlBackground.Height, pnlArrow.Width, pnlArrow.Height)
            p.Initialize(0, 0).LineTo(pnlArrow.Width, 0).LineTo(pnlArrow.Width / 2, pnlArrow.Height).LineTo(0, 0)
            pnl.SetLayoutAnimated(0, x - pnl.Width / 2, y - pnlArrow.Height - pnlArrow.Top, pnl.Width, pnl.Height)
        Case "BOTTOM"
            pnlArrow.SetLayoutAnimated(0, pnl.Width / 2 - pnlArrow.Width / 2, pnlBackground.Top - pnlArrow.Height, pnlArrow.Width, pnlArrow.Height)
            p.Initialize(pnlArrow.Width / 2, 0).LineTo(pnlArrow.Width, pnlArrow.Height).LineTo(0, pnlArrow.Height).LineTo(pnlArrow.Width / 2, 0)
            pnl.SetLayoutAnimated(0, x - pnl.Width / 2, y - pnlArrow.Top, pnl.Width, pnl.Height)
        Case "RIGHT"
            pnlArrow.SetLayoutAnimated(0, pnlBackground.Left - pnlArrow.Width, pnl.Height / 2 - pnlArrow.Height / 2, pnlArrow.Width, pnlArrow.Height)
            p.Initialize(pnlArrow.Width, 0).LineTo(pnlArrow.Width, pnlArrow.Height).LineTo(0, pnlArrow.Height / 2).LineTo(pnlArrow.Width, 0)
            pnl.SetLayoutAnimated(0, x - pnlArrow.Left, y - pnl.Height / 2, pnl.Width, pnl.Height)
        Case "LEFT"
            pnlArrow.SetLayoutAnimated(0, pnlBackground.Left + pnlBackground.Width, pnl.Height / 2 - pnlArrow.Height / 2, pnlArrow.Width, pnlArrow.Height)
            p.Initialize(0, 0).LineTo(pnlArrow.Width, pnlArrow.Height / 2).LineTo(0, pnlArrow.Height).LineTo(0, 0)
            pnl.SetLayoutAnimated(0, x - pnlArrow.Width - pnlArrow.Left, y - pnl.Height / 2, pnl.Width, pnl.Height)
    End Select
  
    cvs.DrawPath(p, pnlBackground.Color, True, 0)
    cvs.Invalidate
    pnl.Visible = False
    pnl.SetVisibleAnimated(300, True)
    If AutoHideMs > 0 Then
        Sleep(AutoHideMs)
        If MyIndex <> ShowIndex Then Return
        Hide
    End If
End Sub

Public Sub Hide
    ShowIndex = ShowIndex + 1
    Dim MyIndex As Int = ShowIndex
    pnl.SetVisibleAnimated(300, False)
    Sleep(300)
    If MyIndex <> ShowIndex Then Return
    If pnl.Parent.IsInitialized Then pnl.RemoveViewFromParent
End Sub

Sub pnlTouch_Touch (Action As Int, X As Float, Y As Float)
    If Action = pnlTouch.TOUCH_ACTION_DOWN Then
        Hide
    End If
End Sub




' Added by Chris2
Private Sub BBC1_LinkClicked (URL As String)
    Log(URL) 
End Sub

Many thanks.
 

Attachments

  • BBCCodeTootip.zip
    7.7 KB · Views: 113
Last edited:

Chris2

Active Member
Licensed User
Longtime User
Partially solved by changing :
B4X:
Dim iv As B4XView = BBC1.ForegroundImageView
'to'
Dim iv As B4XView = BBC1.sv
 
Upvote 0
Top