Gadget_BringToFront()

Mac OSX specific forum
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Gadget_BringToFront()

Post by codeit »

Is there a way to bring a gadget to the front of all gadgets?
been looking but the only thing i have found is CocoaMessage Gadget_BringToFront
but when i try and use it i just get errors.

Thanks

"Talk is cheap. Show me the code."
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: Gadget_BringToFront()

Post by Wolfram »

Do you mean that it is layered above other gadgets or do you want to set the focus on that gadget?
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Gadget_BringToFront()

Post by wilbert »

Click a button and it will move to the front.

Code: Select all

Procedure Gadget_BringToFront(Gadget)
  Protected.i v, sv, subs
  v = GadgetID(Gadget)
  sv = CocoaMessage(0, v, "superview")
  subs = CocoaMessage(0, 0, "NSMutableArray arrayWithArray:", CocoaMessage(0, sv, "subviews"))
  If subs
    CocoaMessage(0, subs, "removeObject:", v)
    CocoaMessage(0, subs, "addObject:", v)
    CocoaMessage(0, sv, "setSubviews:", subs)
  EndIf
EndProcedure

  

If OpenWindow(0, 0, 0, 600, 340, "Gadget Reordering", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  
  RandomSeed(0)
  For i = 0 To 30
    ButtonGadget(i, Random(500), Random(310), 100, 30, "Button " + Str(i))
  Next
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Gadget_BringToFront(EventGadget())
    EndIf
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Gadget_BringToFront()

Post by codeit »

Thanks wilbert you must be top dog around here cheers
:lol:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Gadget_BringToFront()

Post by wilbert »

codeit wrote:Thanks wilbert you must be top dog around here cheers
:lol:
Don't think so :wink:

I just tried a different approach which is a bit shorter and seems to eat up less cpu time.
You might want to try it instead.

Code: Select all

Procedure Gadget_BringToFront(Gadget)
  Protected.i v = GadgetID(Gadget)
  CocoaMessage(0, CocoaMessage(0, v, "superview"), "addSubview:", v)
EndProcedure
  

If OpenWindow(0, 0, 0, 600, 340, "Gadget Reordering", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  
  RandomSeed(0)
  For i = 1 To 30
    ButtonGadget(i, Random(500), Random(310), 100, 30, "Button " + Str(i))
  Next
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Gadget_BringToFront(EventGadget())
    EndIf
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Sun Jan 06, 2019 10:47 am, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Gadget_BringToFront()

Post by codeit »

Thank's wilbert but i have gone wrong :mrgreen: i had to re create the form using form designer
and now i can't get it working at all i have played with but it looks like the bind event is not working
Please could you pass your eyes over it for me and see what's gone wrong.

https://www.dropbox.com/s/8byxcc8tdcfpt ... e.zip?dl=0
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Gadget_BringToFront()

Post by wilbert »

codeit wrote:Please could you pass your eyes over it for me and see what's gone wrong.
I hope someone else can help you out with this one.
I don't know a lot about the form designer. Usually I add gadgets by code.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Gadget_BringToFront()

Post by codeit »

Ok thank's
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Gadget_BringToFront()

Post by IdeasVacuum »

You could also consider temporarily hiding the other gadgets that are in the same location as the gadget you want to bring-to-front, no API required.

Code: Select all

HideGadget(#MyGadget01, #True)
HideGadget(#MyGadget02, #True)
HideGadget(#MyGadget03, #False) ;bring-to-front
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply