Page 2 of 2

Re: Live resize ?

Posted: Sat Aug 27, 2011 12:13 am
by DoubleDutch
Just wondering why does the Windows version have the 16 event handler restriction?

Re: Live resize ?

Posted: Sat Aug 27, 2011 6:20 am
by wilbert
Internally OS X works with real event handlers. You can simply add as many as you wish.
Windows doesn't have them, you need to hook into things. So I had to mimic things like searching if a handler already exists so the same one can't be assigned twice.
The windows version therefore has a table for all handlers. I simply chose room for 16 since that seemed enough to me. It could also have been 32 but that would make things a little slower.
I just couldn't imagine someone would want to add more than 16 resize handlers at the same time.

Re: Live resize ?

Posted: Sat Aug 27, 2011 9:03 am
by DoubleDutch
Is it possible to release a version where you can set the number of handlers? (just in case)

Re: Live resize ?

Posted: Thu Sep 22, 2011 6:45 am
by J. Baker
wilbert wrote:Thanks for mentioning it also works on PPC.

For myself what I wanted (if possible) is a cross platform ( Win32 / OSX_x86 ) solution so I created a cross-platform userlib.
Since the lib is written in ASM, it isn't working on a PPC. For those who are using OSX_x86 and Win32, here it is
http://www.waterlijn.info/pb/wxl_Lib.zip
The zip file contains a userlib for Win32 and one for OSX_x86.

Example code that works on both platforms

Code: Select all

Procedure LiveResizeHandler (newWidth.l, newHeight.l)
  ResizeGadget(0, 10, 10, newWidth - 20, newHeight - 50)
EndProcedure

If OpenWindow(0, 0, 0, 640, 480, "PureBasic - Live Resize", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)

  wxl_AddLiveResizeEventHandler(WindowID(0), @LiveResizeHandler())

  WindowBounds(0, 120, 150, 1024, 768)
  WebGadget(0, 10, 10, 620, 430, "http://www.purebasic.com")

  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow

EndIf

End
The handler must be a procedure with two parameters.
The windows lib has the restriction that it can only handle 16 event handlers at the same time and all windows that want to use this function need to be created from the main thread.
Nice! But it gives a linker error. It assumes that your PureBasic folder is in "/User/YourName/PureBasic/...".

Re: Live resize ?

Posted: Thu Sep 22, 2011 8:45 am
by wilbert
I don't know about the linker error.
As far as I know that location isn't stored in the lib.
It probably is possible to do the same thing without a lib and use inline ASM.
For those interested, this was the source for the OS X resize procedure inside the lib.
I don't remember exactly what every line of code does since I haven't looked at it in a long time :oops:

Code: Select all

section .text

extern _ChangeWindowAttributes
extern _GetEventParameter
extern _GetWindowEventTarget
extern _InstallEventHandler
extern _RemoveEventHandler

global _PB_wxl_AddLiveResizeEventHandler
global _PB_wxl_RemoveEventHandler

callback:

  add esp,8
  pop dword [event]
  pop dword [userdata]
  sub esp,16

  sub esp,12

  sub esp,4
  push dword current
  push dword 0
  push dword 16
  push dword 0
  push dword 0x68697263
  push dword 0x63726374
  push dword [event]
  call dword [PStub_GetEventParameter]
  add esp,32

  sub esp,4
  push dword previous
  push dword 0
  push dword 16
  push dword 0
  push dword 0x68697263
  push dword 0x70726374
  push dword [event]
  call dword [PStub_GetEventParameter]
  add esp,32

  mov eax,dword [currentWidth]
  cmp eax,dword [previousWidth]
  jne _ok
  mov eax,dword [currentHeight]
  cmp eax,dword [previousHeight]
  je _exit

_ok:
  sub esp,12
  fld dword [currentHeight]
  fistp dword [esp]
  sub esp,4
  fld dword [currentWidth]
  fistp dword [esp]
  call dword [userdata]
  add esp,8

_exit:
  add esp,12

  mov eax,-9874
  ret

_PB_wxl_AddLiveResizeEventHandler:

  add esp,4
  pop dword [window]
  pop dword [procedure]
  sub esp,12

  sub esp,12

  sub esp,4
  push dword 0
  push dword 0x10000000
  push dword [window]
  call dword [PStub_ChangeWindowAttributes]
  add esp,16

  sub esp,12
  push dword [window]
  call dword [PStub_GetWindowEventTarget]
  add esp,16
  mov dword [target],eax

  sub esp,8
  mov dword [handler],0
  push dword handler
  push dword [procedure]
  push dword eventList
  push dword 1
  push dword callback
  push dword [target]  
  call dword [PStub_InstallEventHandler]
  add esp,32
  mov eax,dword [handler]

  add esp,12

  ret

_PB_wxl_RemoveEventHandler:
  mov eax,esp
  add eax,4

  sub esp,12

  sub esp,12
  push dword [eax]
  call dword [PStub_RemoveEventHandler]
  add esp,16

  add esp,12
  
  ret

section .data

PStub_ChangeWindowAttributes   dd _ChangeWindowAttributes
PStub_GetEventParameter        dd _GetEventParameter
PStub_GetWindowEventTarget     dd _GetWindowEventTarget
PStub_InstallEventHandler      dd _InstallEventHandler
PStub_RemoveEventHandler       dd _RemoveEventHandler

eventList dd $77696e64, 26

section .bss

handler         resd 1
event           resd 1
userdata        resd 1
window          resd 1
target          resd 1
procedure       resd 1
current         resq 1
currentWidth    resd 1
currentHeight   resd 1
previous        resq 1
previousWidth   resd 1
previousHeight  resd 1

Re: Live resize ?

Posted: Thu Sep 22, 2011 11:20 pm
by michel51
J. Baker wrote:
wilbert wrote:Thanks for mentioning it also works on PPC.

For myself what I wanted (if possible) is a cross platform ( Win32 / OSX_x86 ) solution so I created a cross-platform userlib.
Since the lib is written in ASM, it isn't working on a PPC. For those who are using OSX_x86 and Win32, here it is
http://www.waterlijn.info/pb/wxl_Lib.zip
The zip file contains a userlib for Win32 and one for OSX_x86.

Example code that works on both platforms

Code: Select all

Procedure LiveResizeHandler (newWidth.l, newHeight.l)
  ResizeGadget(0, 10, 10, newWidth - 20, newHeight - 50)
EndProcedure

If OpenWindow(0, 0, 0, 640, 480, "PureBasic - Live Resize", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)

  wxl_AddLiveResizeEventHandler(WindowID(0), @LiveResizeHandler())

  WindowBounds(0, 120, 150, 1024, 768)
  WebGadget(0, 10, 10, 620, 430, "http://www.purebasic.com")

  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow

EndIf

End
The handler must be a procedure with two parameters.
The windows lib has the restriction that it can only handle 16 event handlers at the same time and all windows that want to use this function need to be created from the main thread.
Nice! But it gives a linker error. It assumes that your PureBasic folder is in "/User/YourName/PureBasic/...".
Same here...Linker error.

But the lib-files are not Library-files, but unix-compiled-files. May be this is the reason for the linker error.