Page 1 of 1

[5.20b13] CanvasGadget : Unable to refresh the tooltip

Posted: Mon Aug 26, 2013 1:01 pm
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

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

Posted: Mon Aug 26, 2013 1:35 pm
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 ?

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

Posted: Mon Aug 26, 2013 1:52 pm
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.

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

Posted: Mon Aug 26, 2013 1:59 pm
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

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

Posted: Mon Aug 26, 2013 2:35 pm
by Niffo
Thank you for your answer.

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

Posted: Mon Aug 26, 2013 3:02 pm
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

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

Posted: Sun Mar 09, 2014 2:38 pm
by Niffo
Thank you for this very interesting code !