The ZAZ Blog: When all you have is a chicken and a rocket launcher, make some really badass scrambled eggs.

Does a window have a sheet?
« Previous Entry | Home | Next Entry »

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.

There isn't an excellent amount of error checking, but then again, the API does leave a lot of room for something to go wrong.

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

License: Creative Commons (details) | Digg | E-Mail Thom McGrath
View More Entries From The Same: Day | Month | Year

Comments
Comments are being accepted below.

Pings
This entry is accepting pings.

Leave a comment
We utilize unique methods to ensure your email address stays private while remaining useful.
Depending on the authentication scheme used, some or all of the personal data entered below will be ignored in favor of the data stored with your account.


Disclaimer: I am currently an employee of REAL Software. My comments and opinions are mine alone and do not represent those of my employer. My posts are not official REAL Software communications, and may contain information that is incorrect or misleading.