uwekel,
the most simple solution for your example in your first posting would be to simply call the procedure CustomGadgetResize() directly after ResizeGadget(0,...) without using BindGadgetEvent() at all. This solution is also automatically cross-platform because it doesn't use any API calls...
Code: Select all
Structure CustomGadgetData
Button1.i
Button2.i
EndStructure
Procedure CustomGadgetResize()
g = EventGadget()
*data.CustomGadgetData = GetGadgetData(g)
w = GadgetWidth(g)
h = GadgetHeight(g) / 2
ResizeGadget(*data\Button1, #PB_Ignore, #PB_Ignore, w, h)
ResizeGadget(*data\Button2, #PB_Ignore, h, w, h)
EndProcedure
Procedure CustomGadget(Gadget, X, Y, W, H)
ContainerGadget(Gadget, x, y, w, h, #PB_Container_BorderLess)
*data.CustomGadgetData = AllocateMemory(SizeOf(CustomGadgetData))
SetGadgetData(Gadget, *data)
h / 2
*data\Button1 = ButtonGadget(#PB_Any, 0, 0, w, h, "1")
*data\Button2 = ButtonGadget(#PB_Any, 0, h, w, h, "2")
CloseGadgetList()
EndProcedure
If OpenWindow(0, 0, 0, 300, 200, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
CustomGadget(0, 10, 10, 280, 180)
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Debug "GadgetID = " + EventGadget()
Case #PB_Event_SizeWindow
ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20, WindowHeight(0) - 20)
CustomGadgetResize()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
EndIf
The following code is a much more complicated true cross-platform example for detecting the resizing of a ContainerGadget. It works in both ASCII and Unicode mode on all platforms. I have tested it successfully with PB 5.22 on these operating systems:
- Kubuntu 12.04 x64 with KDE
- Kubuntu 14.04 x86 with KDE
- MacOS X 10.6.8 (Snow Leopard) with PB x86 and x64
- Ubuntu 14.04 x86 with Unity
- Windows XP SP3 x86
- Windows 7 SP1 x86
- Windows 7 SP1 x64 with PB x64
The MacOS part currently doesn't do a live resizing of the Button in the ContainerGadget. The resizing happens after releasing the mouse button when resizing the window. If you need live resizing on MacOS, I can easily add that feature to my example although it would become even more complicated...
By the way: in order to work in Unicode mode the following example has to be more complicated as necessary because of workarounds for bugs in the PB versions for Linux and MacOS. I have already reported a
bug in the pseudotypes P-ASCII and P-UTF8 in the MacOS bug subforum and an
incorrect predefined API function g_signal_connect_data_() in the Linux bug subforum.
Code: Select all
EnableExplicit
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux ; --------------------------------------------------
ProcedureC ContainerResizeCallback(*Widget.GtkWidget,
*Allocation.GtkAllocation, *UserData)
Static GadgetHeight.I
Static GadgetWidth.I
If *Allocation\width <> GadgetWidth Or *Allocation\height <> GadgetHeight
GadgetWidth = *Allocation\width
GadgetHeight = *Allocation\height
ResizeGadget(1, #PB_Ignore, #PB_Ignore, GadgetWidth, GadgetHeight)
EndIf
EndProcedure
CompilerCase #PB_OS_MacOS ; --------------------------------------------------
ImportC ""
sel_registerName(MethodName.S)
class_addMethod(Class.I, Selector.I, Implementation.I, Types.S)
EndImport
Define AppDelegate.I = CocoaMessage(0, CocoaMessage(0, 0,
"NSApplication sharedApplication"), "delegate")
Define NotificationCenter.I = CocoaMessage(0, 0,
"NSNotificationCenter defaultCenter")
ProcedureC ContainerResizeCallback(Object.I, Selector.I, Sender.I)
ResizeGadget(1, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20,
WindowHeight(0) - 20)
EndProcedure
Procedure.S ConvertToUTF8(String.S)
Protected UTF8String.S = Space(StringByteLength(String))
PokeS(@UTF8String, String, -1, #PB_UTF8)
ProcedureReturn UTF8String
EndProcedure
CompilerCase #PB_OS_Windows ; ------------------------------------------------
Procedure ContainerResizeCallback()
ResizeGadget(1, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20,
WindowHeight(0) - 20)
EndProcedure
CompilerEndSelect ; ------------------------------------------------------------
OpenWindow(0, 270, 100, 120, 50, "Container resizing",
#PB_Window_SystemMenu | #PB_Window_SizeGadget)
WindowBounds(0, 110, 50, #PB_Ignore, #PB_Ignore)
ContainerGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
ButtonGadget(1, 0, 0, GadgetWidth(0), GadgetHeight(0), "Test Button")
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux ; --------------------------------------------------
Define SignalName.S = Space(StringByteLength("size-allocate", #PB_UTF8))
PokeS(@SignalName, "size-allocate", -1, #PB_UTF8)
g_signal_connect_(GadgetID(0), SignalName, @ContainerResizeCallback(), 0)
CompilerCase #PB_OS_MacOS ; --------------------------------------------------
class_addMethod(CocoaMessage(0, AppDelegate, ConvertToUTF8("class")),
sel_registerName(ConvertToUTF8("ContainerResizeCallback")),
@ContainerResizeCallback(), ConvertToUTF8("v@:@"))
CocoaMessage(0, NotificationCenter,
"addObserver:", AppDelegate,
"selector:", sel_registerName(ConvertToUTF8("ContainerResizeCallback")),
"name:$", @"NSViewFrameDidChangeNotification",
"object:", GadgetID(0))
CompilerCase #PB_OS_Windows ; ------------------------------------------------
BindGadgetEvent(0, @ContainerResizeCallback(), #PB_EventType_LeftClick)
CompilerEndSelect ; ------------------------------------------------------------
Repeat
Select WaitWindowEvent()
Case #PB_Event_SizeWindow
ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 20,
WindowHeight(0) - 20)
Case #PB_Event_CloseWindow
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CocoaMessage(0, NotificationCenter, "removeObserver:", AppDelegate)
CompilerEndIf
Break
EndSelect
ForEver