scide: refactoring - move ReferencesDialog to lookup_dialog.xpp files
[supercollider.git] / examples / CocoaBridge / SCNSObjectsTest.scd
blobb64caf174dcd77f67c2a17b5a5dced6676c6107f
1 /*------
2         Simple Tests
3         Charles - TheLych at gmail dot com =)
4 */
7 a = SCNSObject("NSURL", "initWithString:", ["http://www.audiosynth.com"]);
8 SCNSObject.dumpPool; // dump the current NS objects retained in SC
12 SCNSObject.freePool; // free all those objects -- carefull if their already in use
13 SCNSObject.dumpPool;
17 a = SCNSObject("NSWindow", "initWithContentRect:styleMask:backing:defer:", [Rect(250, 250, 800, 600), 15, 2, 1]);
18 a.registerNotification("NSWindowWillCloseNotification", {|notificationName, nsNotification|
19         [notificationName, nsNotification].postln;
20         a.release;
21 });
22 a.invoke("makeKeyAndOrderFront:", [a], true);
23 SCNSObject.dumpPool;
27 /*----------------------
28 Notification Examples
29 using Webview
30 ________________________*/
33 var win, root, cocoaUI, cell, webview, levelIndicator;
34 win = SCNSObject("NSWindow", "initWithContentRect:styleMask:backing:defer:", [Rect(250, 250, 800, 600), 15, 2, 1]);
36 root = SCNSObject("NSView", "initWithFrame:", [Rect(0, 0, 800, 600)]);
37 root.invoke("setAutoresizingMask:", [1 + 2 + 8 + 16]);
39 webview = SCNSObject("WebView", "initWithFrame:frameName:groupName:", [Rect(10, 30, 800-20, 600-40), "mywebview", "mywebviewgroup"]);
40 webview.invoke("setAutoresizingMask:", [1 + 2 + 8 + 16]);
42 ~webview = webview; // just to retrieve the source after
44 cell = SCNSObject("NSLevelIndicatorCell", "initWithLevelIndicatorStyle:", [1]);
45 levelIndicator = SCNSObject("NSLevelIndicator", "initWithFrame:", [Rect(10, 5, 800-20, 10)]);
46 levelIndicator.invoke("setCell:", [cell]);
47 levelIndicator.invoke("setMinValue:", [0]);
48 levelIndicator.invoke("setMaxValue:", [100]);
49 levelIndicator.invoke("setFloatValue:", [0]);
50 levelIndicator.invoke("setContinuous:", [true]);
51 cell.release;
53 cocoaUI.add(root);
54 cocoaUI.add(webview);
55 cocoaUI.add(levelIndicator);
57 win.invoke("setContentView:", [root]);
58 root.invoke("addSubview:", [webview]);
59 root.invoke("addSubview:", [levelIndicator]);
61 ///// Notifications
62 // Window 
63 win.registerNotification("NSWindowWillCloseNotification", {
64         |notificationName, nsNotificationObjectAsRawPointer|
65         "closing window".postln;
66         cocoaUI.do {|ui| ui.invoke("removeFromSuperviewWithoutNeedingDisplay")};
67         win.release;
68         root.release;
69         webview.release;
70         levelIndicator.release;
71         ~webview = nil;
72 });
74 win.registerNotification("NSWindowDidMoveNotification", {
75         |notificationName, nsNotificationObjectAsRawPointer|
76         notificationName.postln;
77 });
79 win.registerNotification("NSWindowDidMiniaturizeNotification", {
80         |notificationName, nsNotificationObjectAsRawPointer|
81         notificationName.postln;
82 });
83 // Webview Notifications
84 webview.registerNotification("WebProgressEstimateChangedNotification", {
85         |notificationName, nsNotificationObjectAsRawPointer|
86         var value;
87         value = webview.invoke("estimatedProgress");
88         levelIndicator.invoke("setFloatValue:", [value*100]);
89         ("loading progress: "+ (value*100) + "%").postln;
90 });
92 webview.registerNotification("WebProgressFinishedNotification", {
93         |notificationName, nsNotificationObjectAsRawPointer|
94         var t0, t1;
95         levelIndicator.invoke("setFloatValue:", [0]);
96         t0 = webview.invoke("mainFrame");
97         t1 = t0.invoke("dataSource"); t0.release;
98         t0 = t1.invoke("initialRequest"); t1.release;
99         t1 = t0.invoke("URL"); t0.release;
100         t0 = t1.invoke("absoluteString"); t1.release;
101         (t0 ++ " finished Loading").postln;
102         win.invoke("setTitle:", [t0]);
104 ///// Show Window
105 win.invoke("makeKeyAndOrderFront:", [win], true);
107 ///// URL Loading
109         var url;
110         url = "http://swiki.hfbk-hamburg.de:8888/MusicTechnology/6";
111         webview.invoke("setMainFrameURL:", [url]);
112         SCNSObject.dumpPool;
113 }.defer(0.2);
118 /*----------------------
119 NSData conversion
120 using Webview html source
121 ________________________*/
123 /// interpret it AFTER previous example for getting source html file
124 var mainframe, datasource, nsdata;
125 mainframe = ~webview.invoke("mainFrame");
126 datasource = mainframe.invoke("dataSource"); mainframe.release;
127 nsdata = datasource.invoke("data"); datasource.release;
128 nsdata.isSubclassOf("NSData").postln; // 
129 "---- HTML Source ----".postln;
130 nsdata.asArray(\string).postln;
131 "---- End of HTML Source ----".postln;
132 nsdata.release;
135 // dump to test after closing
136 SCNSObject.dumpPool;
141 /*----------------------
142 special Delegates actions with return values
143 using NSURLConnection
144 ________________________*/
146 var url;
148 // first URL Request
149 url = SCNSObject("NSURL", "initWithString:", ["http://www.audiosynth.com"]);
150 ~urlRequest = SCNSObject("NSURLRequest", "requestWithURL:cachePolicy:timeoutInterval:", [url, 0, 60]); url.release;
152 // redirection to set after delegate call
153 url = SCNSObject("NSURL", "initWithString:", ["http://www.apple.com"]);
154 ~redirection = SCNSObject("NSURLRequest", "requestWithURL:cachePolicy:timeoutInterval:", [url, 0, 60]); url.release;
156 // we need here to set a void object to set its delegate before it is allocated really
157 // because urlConnection does not have a setDelegate: method
158 ~urlConnection = SCNSObject.newClear;
159 ~urlConnection.setDelegate; // create and attach a special delegate
160 ~urlConnection.nsDelegate.addMethod("connectionDidFinishLoading:", nil, "@", {
161 |method, args| [method, args].postln;
164 //// Custom Delegate Method with return values allowed (automatic conversion for most)
165 //// Here we have to provide the (name, return type of the delegate method, and the type encoding for the arguments)
166 //// see http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_13_section_9.html#//apple_ref/doc/uid/TP30001163-CH9-TPXREF165 for explanations
167 ~urlConnection.nsDelegate.addMethod("connection:didReceiveResponse:", nil, "@@", {
168         |method, args| [method, args].postln;
171 ~urlConnection.nsDelegate.addMethod("connection:willSendRequest:redirectResponse:", "@", "@@@", {
172         |method, arguments|
173         [method, arguments].postln;
174         url = ~redirection.invoke("URL");
175         "redirecting to "++url.invoke("absoluteString"); url.release;
176         ^~redirection; // redirect !
179 // we can init the object now
180 ~urlConnection.init("NSURLConnection", "initWithRequest:delegate:", [~urlRequest, ~urlConnection.nsDelegate]); // now we can alloc the object and attach its delegate
184 ~urlConnection.release;
185 ~urlRequest.release;
186 ~redirection.release;
192 * NSToolbar
196 var win, toolbar, delegate, toolbarItems, items, toolbarItem=nil, itemAction /*, myCustomToolbarItemIcon = Document.current.path.dirname ++ "/../icons/TrBtn_pause_on.tiff"*/;
198 win = SCNSObject("NSWindow", "initWithContentRect:styleMask:backing:defer:", [Rect(250, 250, 800, 600), 1 + 2 + 4 + 8, 2, 1]);
200 "-> Creating Toolbar".postln;
201 toolbar = SCNSObject.newClear;
202 itemAction = {|a, b|
203         [a, b].postln;
206 toolbarItems = SCNSObject("NSMutableArray", "initWithCapacity:", [16]);
207 toolbarItems.invoke("addObject:", ["NSToolbarSeparatorItem"]);
208 toolbarItems.invoke("addObject:", ["NSToolbarShowFontsItem"]);
209 toolbarItems.invoke("addObject:", ["NSToolbarShowColorsItem"]);
210 toolbarItems.invoke("addObject:", ["NSToolbarCustomizeToolbarItem"]);
211 /*toolbarItems.invoke("addObject:", ["MyCustomToolbarItem"]);*/ 
213 delegate = toolbar.setDelegate;
214 toolbar.nsDelegate.addMethod("toolbarAllowedItemIdentifiers:", "@", "@", {
215         |method, arguments|
216         "toolbarAllowedItemIdentifiers called".postln;
217         ^toolbarItems;
220 toolbar.nsDelegate.addMethod("toolbarSelectableItemIdentifiers:", "@", "@", {
221         |method, arguments|
222         "toolbarSelectableItemIdentifiers called".postln;
223         ^toolbarItems;
226 toolbar.nsDelegate.addMethod("toolbarDefaultItemIdentifiers:", "@", "@", {
227         |method, arguments|
228         "toolbarDefaultItemIdentifiers called".postln;
229         ^toolbarItems;
232 toolbar.nsDelegate.addMethod("toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:", "@", "@@i", {
233         |method, arguments|
234         var image;
235         arguments[1].postln;
236         /*
237         if(toolbarItem.isNil, {
238                 toolbarItem = SCNSObject("NSToolbarItem", "initWithItemIdentifier:", [arguments[1]]);
239                 image = SCNSObject("NSImage", "initWithContentsOfFile:", [myCustomToolbarItemIcon]);
240                 toolbarItem.invoke("setImage:", [image]);
241                 toolbarItem.invoke("setLabel:", ["Custom"]);
242                 toolbarItem.initAction("doAction:");
243                 toolbarItem.nsAction.action_({"----> action".postln});
244                 "toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:".postln;
245                 image.release;
246         })
247         ^toolbarItem;
248         */
249         ^nil;
252 toolbar.init("NSToolbar", "initWithIdentifier:", ["myToolbar"]);
253 toolbar.invoke("setAllowsUserCustomization:", [true]);
254 toolbar.invoke("setDisplayMode:", [0]); //default
255 toolbar.invoke("setSizeMode:", [0]); //default
257 win.invoke("setToolbar:", [toolbar], true);
258 toolbar.invoke("setDelegate:", [toolbar.nsDelegate]);
260 win.registerNotification("NSWindowWillCloseNotification", {
261         "closing window".postln;
262         win.release;
263         toolbarItems.release;
264         if(toolbarItem.notNil, {toolbarItem.release});
265         toolbar.release;
268 win.invoke("makeKeyAndOrderFront:", [win], true);
273         TabView - very simple example
276 var win, nswin, root, tabview, tabviewitems, numTabs = 5, temp, temp1;
278 win = SCWindow("Hello", Rect(100, 200, 700, 400), textured:true);
279 win.view.decorator = FlowLayout.new(win.view.bounds);
281 10.do{|i| EZSlider.new(win, 600 @ 20, "Hi"+i); win.view.decorator.nextLine};
283 root = SCNSObject.newFromRawPointer(win.dataptr); // SCGraphView
285 tabview = SCNSObject("NSTabView", "initWithFrame:", [Rect(0, 0, 700, 400)]);
286 tabview.invoke("setAutoresizingMask:", [1 + 2 + 8 + 16]);
287 tabviewitems = Array.newClear(numTabs);
288 numTabs.do {|i|
289         var obj = SCNSObject("NSTabViewItem", "initWithIdentifier:", ["Tab"++i.asString]);
290         obj.invoke("setLabel:", ["Tab"+(i+1)]);
291         tabview.invoke("addTabViewItem:", [obj]);
292         tabviewitems[i] = obj;
295 root.invoke("retain");
296 nswin = root.invoke("window");
299 "swapping views".postln;
300 nswin.invoke("setContentView:", [tabview]);
301 nswin.invoke("displayIfNeeded");
302 tabviewitems[0].invoke("setView:", [root]);
303 //tabviewitems[0].invoke("setNeedsDisplay:", [true]);
304 }.defer(0.01);
306 win.onClose_({
307         "closing window".postln;
308         tabview.release;
309         nswin.release;
310         root.release;
311         tabviewitems.do {|obj| obj.release;}
314 tabviewitems[1].invoke("view").className.postln;
316 win.front;
320         Class methods examples
321         SCUserView drawing with NSBezierPath very very simple just for test
324 var win, uview, obj, bp;
325 win = SCWindow("Hello", Rect(400, 200, 400, 300));
326 uview = SCUserView(win, win.view.bounds);
328 uview.drawFunc_({|v|
329         Color.red.setFill;
330         bp = SCNSObject("NSBezierPath", "fillRect:", [v.bounds]);
331         bp.free;
334 win.front;
337 SCNSObject.dumpPool;
338 SCNSObject.freePool;