Does a window have a sheet?
By Thom McGrath on
So Norman Palardy recently posted some code for detecting if a window has a sheet attached. Charles Yeomans took this code and modified it to work without subclassing window.
I take it one step further, and fix a bug...
See, when I tried to use the code, I quickly ran into an issue: MessageDialog. When showing a MessageDialog as a sheet, the current code returns false. That's because the code loops through the REALbasic windows until it finds a sheet window, then asks for the parent of that sheet, and compares the result of that to the window passed to it. But MessageDialog is not a subclass of Window.
So we need to delve deeper into the Carbon API.
When a sheet window is created, a Window Group is created as well and the sheet is then grouped with the parent window. This is how the two stay "linked" together. We'll exploit that.
First, we find the group of the window in question. Then, we loop through all the windows using declares until we find one that is in the same window group and is a sheet. This is done completely using declares, which is good, because it'll work for both window-based sheets and MessageDialog sheets.
Function HasSheet(Extends w As Window) As Boolean #If targetMacOS Const framework = "Carbon.framework" Const kSheetWindowClass = 11 Soft Declare Function GetWindowList Lib framework () As Integer Soft Declare Function GetNextWindow Lib framework (window As Integer) As Integer Soft Declare Function GetWindowClass Lib framework (window As Integer, ByRef outclass As Integer) As Integer Soft Declare Function GetWindowGroup Lib framework (window As Integer) As Integer Dim windGroup As Integer = GetWindowGroup(w.handle) Dim nextWind As Integer = GetWindowList() Dim windClass As Integer While nextWind <> 0 If GetWindowGroup(nextWind) = windGroup Then If GetWindowClass(nextwind,windclass) <> 0 Then Return False End If windclass = kSheetWindowClass Then Return True End End nextWind = GetNextWindow(nextWind) Wend Return False #Else Return False #EndIf End Function
There isn't an excellent amount of error checking, but then again, the API doesn't leave a lot of room for something to go wrong.