[5.20b13] CanvasGadget : Unable to refresh the tooltip

Mac OSX specific forum
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

[5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by Niffo »

Unable to dynamically change the tooltip in a CanvasGadget :

Code: Select all

OpenWindow(0, 100, 100, 300, 200, "Test")
CanvasGadget(0, 10, 10, 280, 180)

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_Gadget
         Select EventGadget()
            Case 0 ; CanvasGadget
               Select EventType()
                  Case #PB_EventType_MouseMove
                     If Tooltip.s <> Str(GetGadgetAttribute(0, #PB_Canvas_MouseX))
                        Tooltip = Str(GetGadgetAttribute(0, #PB_Canvas_MouseX))
                        GadgetToolTip(0, Tooltip)
                     EndIf
               EndSelect
         EndSelect
      Case #PB_Event_CloseWindow
         Break
   EndSelect
ForEver
Niffo
Fred
Administrator
Administrator
Posts: 18499
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by Fred »

Seems to work here. But you need to exit the gadget area to have the new tooltip displayed, it's the way tooltips works. Or did I miss something ?
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: [5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by Niffo »

Yes, if you exit the gadget it "works". But no need to do that on Windows or under Carbon. This is useful to have tooltip related to a part of a CanvasGadget (custom toolbar, etc ...)
If you think there is no solution under Cocoa, you can delete this thread.
Niffo
Fred
Administrator
Administrator
Posts: 18499
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by Fred »

We use the standard "setToolTop:" function of NSView. According to this thread, native tooltip in Cocoa won't update immediately, you should probably create your own tooltip if you want such feature

http://stackoverflow.com/questions/5397 ... ustom-view
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: [5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by Niffo »

Thank you for your answer.
Niffo
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by wilbert »

For showing coordinates you should create something yourself but with a little cocoa you can define several tips for one canvas.
Here's an example. The NSString objects for the tooltip are not allowed to be autorelease objects so I used a PureBasic map object for those.
If a new tip is assigned on a previous location, the previous object is reused. The only problem is that it won't work properly if you resize your canvas gadget.

Code: Select all

Global NewMap ToolTipString.i()

Procedure.i AddCanvasTip(CanvasGadget, x, y, Width, Height, Text.s)
  Protected ToolTipRect.NSRect, TipID.s, Tip.i
  ToolTipRect\origin\x = x
  ToolTipRect\origin\y = GadgetHeight(CanvasGadget) - y - Height
  ToolTipRect\size\width = width
  ToolTipRect\size\height = height
  TipID = Str(CanvasGadget) + ":" + Str(x) + ":" + Str(y)
  Tip = ToolTipString(TipID)
  If Tip = 0
    Tip = CocoaMessage(0, CocoaMessage(0, 0, "NSMutableString alloc"), "initWithCapacity:", 0)
    ToolTipString(TipID) = Tip
  EndIf
  CocoaMessage(0, Tip, "setString:$", @Text)
  ProcedureReturn CocoaMessage(0, GadgetID(CanvasGadget), "addToolTipRect:@", @ToolTipRect, "owner:", Tip, "userData:", 0) 
EndProcedure

Procedure RemoveCanvasTip(CanvasGadget, Tip)
  CocoaMessage(0, GadgetID(CanvasGadget), "removeToolTip:", Tip)
EndProcedure



OpenWindow(0, 100, 100, 300, 200, "Test")
CanvasGadget(0, 10, 10, 280, 180)

StartDrawing(CanvasOutput(0))
Box(10, 10, 100, 20, RGB(255, 0, 0))
Box(10, 30, 100, 20, RGB(0, 255, 0))
StopDrawing()

AddCanvasTip(0, 10, 10, 100, 20, "Tip 1")
AddCanvasTip(0, 10, 30, 100, 20, "Tip 2")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
Windows (x64)
Raspberry Pi OS (Arm64)
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: [5.20b13] CanvasGadget : Unable to refresh the tooltip

Post by Niffo »

Thank you for this very interesting code !
Niffo
Post Reply