Toggle window attributes

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Toggle window attributes

Post by Shardik »

The Carbon framework of MacOS offers the API function

Code: Select all

ChangeWindowAttributes_(WindowID(), AttributeToSet, AttributeToClear)
which enables the programmer to change attributes of a displayed window during
runtime. You can combine several constants with the Or operand and set or clear
attributes with one call of this function.

My example code demonstrates how to toggle attributes of a displayed window:

Code: Select all

EnableExplicit

ImportC ""
  GetWindowAttributes(WindowRef.L, *WindowAttributes)
EndImport

#kWindowCloseBoxAttribute = 1 << 0
#kWindowHorizontalZoomAttribute = 1 << 1
#kWindowVerticalZoomAttribute = 1 << 2
#kWindowFullZoomAttribute = #kWindowVerticalZoomAttribute | #kWindowHorizontalZoomAttribute
#kWindowCollapseBoxAttribute = 1 << 3
#kWindowResizableAttribute = 1 << 4
#kWindowToolbarButtonAttribute = 1 << 6
#kWindowMetalAttribute = 1 << 8
#kWindowNoShadowAttribute = 1 << 21

Define ButtonID.L
Define ButtonText.S
Define i.I
Define NumButtons.I
Define WindowAttributeName.S
Define WindowAttributes.I

Dim WindowAttributeID.L(0)
Dim WindowAttributeName.S(0)

OpenWindow(0, 100, 100, 300, 260, "Toggle Window attributes", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_Invisible)

; ----- Read the constant descriptions and its values into 2 arrays and create buttons

Repeat
  Read.S WindowAttributeName

  If WindowAttributeName = ""
    NumButtons = i
    Break
  Else
    ReDim WindowAttributeID(i)
    ReDim WindowAttributeName(i)
    WindowAttributeName(i) = WindowAttributeName
    Read.L WindowAttributeID(i)

    GetWindowAttributes(WindowID(0), @WindowAttributes)

    If WindowAttributes & WindowAttributeID(i)
      ButtonText = "Disable "
    Else
      ButtonText = "Enable "
    EndIf

    ButtonGadget(i, 10, i * 30 + 10, WindowWidth(0) - 20, 20, ButtonText + WindowAttributeName(i))
    i + 1
  EndIf
ForEver

; ----- Adapt vertical size of window to display all buttons

ResizeWindow(0, #PB_Ignore, #PB_Ignore, #PB_Ignore, NumButtons * 30 + 10)
HideWindow(0, #False)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      ButtonID = EventGadget()

      ; ----- Read current attributes of window

      GetWindowAttributes(WindowID(0), @WindowAttributes)

      ; ----- Toggle the window attribute connected to the pressed button
      ;       and change the text of the button

      If WindowAttributes & WindowAttributeID(ButtonID)
        ChangeWindowAttributes_(WindowID(0), 0, WindowAttributeID(ButtonID))
        ButtonText = "Enable "
      Else
        ChangeWindowAttributes_(WindowID(0), WindowAttributeID(ButtonID), 0)
        ButtonText = "Disable "
      EndIf

      SetGadgetText(ButtonID, ButtonText + WindowAttributeName(ButtonID))
  EndSelect
ForEver

End

DataSection
  Data.S "closing of window"
  Data.L #kWindowCloseBoxAttribute
  Data.S "minimizing to dock"
  Data.L #kWindowCollapseBoxAttribute
  Data.S "maximizing"
  Data.L #kWindowFullZoomAttribute
  Data.S "non-shadow look"
  Data.L #kWindowNoShadowAttribute
  Data.S "toolbar button"
  Data.L #kWindowToolbarButtonAttribute
  Data.S "resizing of window"
  Data.L #kWindowResizableAttribute
  Data.S ""
EndDataSection
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Toggle window attributes

Post by WilliamL »

Hey Shardik,

Way cool dude! :D

I especially like being able to get rid of the 'toolbar' button. It was something I asked for some time ago. Your code is a nice presentation of showing how the options look.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Toggle window attributes

Post by Shardik »

William,

I am glad that my code example has been useful for you. :)

You may even extend the list of attributes by adding

Code: Select all

Data.S "metal skin"
Data.L #kWindowMetalAttribute
before the trailing

Code: Select all

Data.S ""
which marks the end of the list. The new button enables you to toggle between
the metal skin and the normal appearance. Unfortunately the background of
the buttons won't be drawn correctly. If you define the buttons with the flag
#PB_Button_Toggle
the button background will be drawn correctly.

Another problem arises if you try to resize the window in the metal skin mode:
the buttons will disappear. One solution is to add

Code: Select all

SetControlVisibility(ControlRef.L, IsVisible.L, DoDraw.L)
in the ImportC block and to add

Code: Select all

SetControlVisibility(GadgetID(0), #True, #True)
in the Window event loop...
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Toggle window attributes

Post by WilliamL »

Shardik,

Oh, I see. I played with those attributes and discovered those problems (most of the code was already in your offering). Thanks for the work-around. The shadowing of several gadgets seem to be a general problem over-laying window colors.

Thanks for the contribution.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply