Page 11 of 16
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Sun May 18, 2014 12:12 am
by Danilo
CanvasGadget GetWheelDeltaX() + GetWheelDeltaY():
Code: Select all
Procedure GetCurrentEvent()
Protected app = CocoaMessage(0,0,"NSApplication sharedApplication")
If app
ProcedureReturn CocoaMessage(0,app,"currentEvent")
EndIf
EndProcedure
Procedure.CGFloat GetWheelDeltaX()
Protected wheelDeltaX.CGFloat = 0.0
Protected currentEvent = GetCurrentEvent()
If currentEvent
CocoaMessage(@wheelDeltaX,currentEvent,"scrollingDeltaX")
EndIf
ProcedureReturn wheelDeltaX
EndProcedure
Procedure.CGFloat GetWheelDeltaY()
Protected wheelDeltaY.CGFloat = 0.0
Protected currentEvent = GetCurrentEvent()
If currentEvent
CocoaMessage(@wheelDeltaY,currentEvent,"scrollingDeltaY")
EndIf
ProcedureReturn wheelDeltaY
EndProcedure
If OpenWindow(0, 0, 0, 700, 400, "ScrollAreaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ScrollAreaGadget(0, 10, 10, 680, 380, 1000, 1000, 30)
ButtonGadget (1, 10, 10, 230, 30,"Button 1")
ButtonGadget (2, 50, 50, 230, 30,"Button 2")
ButtonGadget (3, 90, 90, 230, 30,"Button 3")
CanvasGadget (5, 200, 200, 500, 500)
CloseGadgetList()
StartDrawing(CanvasOutput(5))
Box(0,0,500,500,$FF0000)
StopDrawing()
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
Select EventGadget()
Case 1
MessageRequester("Info","Button 1 was pressed!",#PB_MessageRequester_Ok)
Case 2
MessageRequester("Info","Button 2 was pressed!",#PB_MessageRequester_Ok)
Case 3
MessageRequester("Info","Button 3 was pressed!",#PB_MessageRequester_Ok)
Case 5
If EventType() = #PB_EventType_MouseWheel
wheelX.CGFloat = GetWheelDeltaX()
wheelY.CGFloat = GetWheelDeltaY()
If wheelX
SetGadgetAttribute(0, #PB_ScrollArea_X, GetGadgetAttribute(0, #PB_ScrollArea_X) - wheelX)
EndIf
If wheelY
SetGadgetAttribute(0, #PB_ScrollArea_Y, GetGadgetAttribute(0, #PB_ScrollArea_Y) - wheelY)
EndIf
EndIf
EndSelect
EndSelect
ForEver
EndIf
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Wed Jul 23, 2014 8:13 am
by grabiller
Wilbert,
If you have time, could you please post an example of how to retrieve all the currently open windows (from all running applications that is, not just PB) names, handles (or something), positions and sizes, and then how to move/resize any one of them, all this through Cocoa of course.
I'm trying to create a tool for an application that uses a lot of popup windows, to automatically arrange/snap/resize them in a clean way. The application is working on Windows & Mac but I only know how to do it on Windows. Would be great to have this tool working also on Mac OSX.
Cheers,
Guy.
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Wed Jul 23, 2014 8:35 am
by wilbert
@Grabiller, I think you don't need Cocoa for this.
Take a look at the "Quartz Window Services Reference"
https://developer.apple.com/library/mac ... tions.html
Does that make any sense ?
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Wed Jul 23, 2014 9:03 am
by grabiller
Hmm those functions seems to be limited to retrieve a list but I'm not sure what to do with it, how to then move and resize those windows. Plus I would really like to do it from Cocoa for later compatibility (how long the Carbon API will be available/supported by Apple ?).
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Wed Jul 23, 2014 9:16 am
by grabiller
From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Wed Jul 23, 2014 7:46 pm
by wilbert
grabiller wrote:From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
Apparently it's not that easy (or I'm missing something).
I've read some suggestions on using AppleScript to resize the window of another application.
Unfortunately I'm not very familiar with AppleScript

Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Thu Jul 24, 2014 5:38 am
by grabiller
wilbert wrote:grabiller wrote:From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
Apparently it's not that easy (or I'm missing something).
I've read some suggestions on using AppleScript to resize the window of another application.
Unfortunately I'm not very familiar with AppleScript

After extensive search on Google, It seems no one knows how to do that on Mac OSX, which I found totally incredible.
Well, some pretend it's easy, but never give any solution/details. The only comment/answer I've seen close to an answer is:
Use CGWindowListCopyWindowInfo to grab the kCGWindowOwnerPID of each window. Then you can use "distributed objects" if you want access to ObjectiveC stuff, or Mach RPC for other stuff. All of this is documented at http://developer.apple.com
Unfortunately he does not give the exact/direct link on the Apple website.
Perhaps that gives you a hint where to look at ?
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Thu Jul 24, 2014 6:30 am
by Danilo
Why would you want to resize external applications?
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Thu Jul 24, 2014 6:39 am
by grabiller
I found this:
https://github.com/nint22/WindowSnaps/b ... Delegate.m
Seems to do what I'm looking for. I understand part of the code but I have no idea how to transpose that in PB.
Perhaps you (or someone else) can help ? I know it's a lot to ask but I think it would be really neat to have a little cross-platform set of procedures to list, move and resize windows.
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Thu Jul 24, 2014 6:44 am
by grabiller
Danilo wrote:Why would you want to resize external applications?
I've explained that in my first message, please re-read:
grabiller wrote:../.. I'm trying to create a tool for an application that uses a lot of popup windows, to automatically arrange/snap/resize them in a clean way. The application is working on Windows & Mac but I only know how to do it on Windows. Would be great to have this tool working also on Mac OSX ../..
The application being Lightwave 3D.
Someone did a tool for that on Windows with AutoIt. Here is the result:
http://www.youtube.com/watch?v=XW7Bmmv6IXI
I would like to create something similar, but cross-platform and more advanced.
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Thu Jul 24, 2014 10:39 pm
by fsw
I'm used to organize gui-apps (in a tile'd way) on linux with OpenBox and some additional keyboard shortcuts.
Because I wanted to have the same on MacOS I searched and found
spectacle.
It's opensource and the code is on
github.
Maybe the spectacle code can give you an idea how it's done.
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Fri Jul 25, 2014 5:49 am
by Danilo
Here is the start to enum the windows and get the PID:
Code: Select all
ImportC ""
CGWindowListCreate(_1, _2)
CGWindowListCreateDescriptionFromArray(arr)
CFArrayGetCount(arr)
CFArrayGetValueAtIndex(arr, index)
CFRelease(arr)
CFDictionaryGetValue(_1,_2)
CFStringCreateWithCharacters(alloc,text.p-Unicode,len)
CFNumberGetValue(_1,_2,_3)
EndImport
;CFArrayRef
WindowList = CGWindowListCreate(1, 0)
Debug WindowList
DescriptionList = CGWindowListCreateDescriptionFromArray(WindowList)
Debug DescriptionList
Debug "--------------"
If DescriptionList
count = CFArrayGetCount(DescriptionList)
If count
For i = 0 To count-1
id = CFArrayGetValueAtIndex(DescriptionList,i)
Debug "ID: "+id
pid_num = CFDictionaryGetValue(id, CFStringCreateWithCharacters(0,"kCGWindowOwnerPID",17))
If pid_num
CFNumberGetValue(pid_num,9,@pid.l)
Debug "PID: "+pid
EndIf
Next
EndIf
EndIf
CFRelease(DescriptionList)
CFRelease(WindowList)
It is pretty straightforward to translate the code, so I hope you can do the rest yourself. I'm having a beer or two now (after returning from nightshift work),
not in the mood for doing all the work for you. It is boring because PB does not import any of this API functions, but it should be doable.
Press ALT+CMD+ESC to terminate the running processes, if it does not exit correctly (possibly some clean-up missing).
Wouldn't it be better to discuss your problem within a separate topic instead in this topic of general interest?
Maybe a moderator can split it and move grabiller's problems to a separate topic, probably starting at
&start=159
Try
Cinema4D as a cross-platform alternative.

-
Destiny - Animation Short
-
MAXON CINEMA 4D Architecture Reel 2014
-
CINEMA 4D Demo Reel - Spring 2014
-
CINEMA 4D Demo Reels
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Fri Jul 25, 2014 7:58 am
by grabiller
@fsw
Thanks a lot for pointing me to this application, it does exactly what I need, it will surely be helpful.
@Danilo
Thanks a lot. I'm not asking you to "do all the work for me" but at least this example gives me a point to start. I'll see how far I can go from there.
Regarding a separate topic you are right, sorry, if I need more help I'll create a new thread in the Mac section.
ps: What makes you think I need an alternative ?
Cheers,
Guy.
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Fri Aug 15, 2014 10:15 am
by deseven
Getting the portion of the screen in which it is currently safe to draw your application content (i.e. excluding the dock and the top menu):
Code: Select all
OpenWindow(0,0,0,0,0,"test",#PB_Window_SystemMenu|#PB_Window_Invisible)
mainScreen = CocoaMessage(0,0,"NSScreen mainScreen")
CocoaMessage(@visibleFrame.NSRect,mainScreen,"visibleFrame")
Debug visibleFrame\origin\x
Debug visibleFrame\origin\y
Debug visibleFrame\size\height
Debug visibleFrame\size\width
; origin\y starts from the bottom left corner of the screen, so to get the safe coordinates for PB we can calculate it like that:
ExamineDesktops()
winX = visibleFrame\origin\x
winY = DesktopHeight(0)-visibleFrame\size\height-visibleFrame\origin\y
titleBarH = WindowHeight(0,#PB_Window_FrameCoordinate)-WindowHeight(0,#PB_Window_InnerCoordinate)
winMaxW = visibleFrame\size\width
winMaxH = visibleFrame\size\height-titleBarH
Debug winX
Debug winY
Debug winMaxW
Debug winMaxH
ResizeWindow(0,winX,winY,winMaxW,winMaxH)
HideWindow(0,#False)
Repeat : ev = WaitWindowEvent() : Until ev = #PB_Event_CloseWindow
Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Wed Oct 01, 2014 5:22 pm
by empty
Subclassing a Gadget during runtime. This allows overriding methods for a particular gadget without affecting the others, as shown in the following example.
Code: Select all
ImportC ""
sel_registerName(STR.p-ascii)
class_addMethod(class, selector, imp, types.p-ascii)
objc_allocateClassPair(class, newClassName.p-ascii, extraBytes)
objc_registerClassPair(class)
object_setClass(object, class)
EndImport
Procedure SubClassGadget(Gadget, newClassName.s)
Protected oldclass = CocoaMessage(0, GadgetID(Gadget), "class")
Protected result = 0
result = objc_allocateClassPair(oldclass, newClassName, 0)
objc_registerClassPair(result)
object_setClass(GadgetID(Gadget), result)
ProcedureReturn result
EndProcedure
Procedure SubClassWindow(Window, newClassName.s)
Protected oldclass = CocoaMessage(0, WindowID(Window), "class")
Protected result = 0
result = objc_allocateClassPair(oldclass, newClassName, 0)
objc_registerClassPair(result)
object_setClass(Window(Window), result)
ProcedureReturn result
EndProcedure
Procedure CocoaAddMethod(Class, Method.s, CallBack)
Protected sel = sel_registerName(Method)
class_addMethod(Class, sel, CallBack, "v@:@")
EndProcedure
ProcedureC EventHandler(sender)
Debug sender
Debug "Escape Key Pressed"
EndProcedure
OpenWindow(0, 0, 0, 322, 205, "SubClassing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8, 10, 306, 23, "StringGadget (Calls callback on ESC key)")
StringGadget(1, 8, 45, 306, 20, "Second String Gadget")
class = SubClassGadget(0, "MyStringGadget")
CocoaAddMethod(Class, "cancelOperation:", @EventHandler())
Debug PeekS(CocoaMessage(0, CocoaMessage(0, GadgetID(0), "className"), "UTF8String"), -1, #PB_UTF8)
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
Edit: Added a function called SubClassWindow() for convenience.