3 Charles - TheLych at gmail dot com =)
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
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;
22 a.invoke("makeKeyAndOrderFront:", [a], true);
27 /*----------------------
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]);
55 cocoaUI.add(levelIndicator);
57 win.invoke("setContentView:", [root]);
58 root.invoke("addSubview:", [webview]);
59 root.invoke("addSubview:", [levelIndicator]);
63 win.registerNotification("NSWindowWillCloseNotification", {
64 |notificationName, nsNotificationObjectAsRawPointer|
65 "closing window".postln;
66 cocoaUI.do {|ui| ui.invoke("removeFromSuperviewWithoutNeedingDisplay")};
70 levelIndicator.release;
74 win.registerNotification("NSWindowDidMoveNotification", {
75 |notificationName, nsNotificationObjectAsRawPointer|
76 notificationName.postln;
79 win.registerNotification("NSWindowDidMiniaturizeNotification", {
80 |notificationName, nsNotificationObjectAsRawPointer|
81 notificationName.postln;
83 // Webview Notifications
84 webview.registerNotification("WebProgressEstimateChangedNotification", {
85 |notificationName, nsNotificationObjectAsRawPointer|
87 value = webview.invoke("estimatedProgress");
88 levelIndicator.invoke("setFloatValue:", [value*100]);
89 ("loading progress: "+ (value*100) + "%").postln;
92 webview.registerNotification("WebProgressFinishedNotification", {
93 |notificationName, nsNotificationObjectAsRawPointer|
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]);
105 win.invoke("makeKeyAndOrderFront:", [win], true);
110 url = "http://swiki.hfbk-hamburg.de:8888/MusicTechnology/6";
111 webview.invoke("setMainFrameURL:", [url]);
118 /*----------------------
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;
135 // dump to test after closing
141 /*----------------------
142 special Delegates actions with return values
143 using NSURLConnection
144 ________________________*/
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:", "@", "@@@", {
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;
186 ~redirection.release;
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;
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:", "@", "@", {
216 "toolbarAllowedItemIdentifiers called".postln;
220 toolbar.nsDelegate.addMethod("toolbarSelectableItemIdentifiers:", "@", "@", {
222 "toolbarSelectableItemIdentifiers called".postln;
226 toolbar.nsDelegate.addMethod("toolbarDefaultItemIdentifiers:", "@", "@", {
228 "toolbarDefaultItemIdentifiers called".postln;
232 toolbar.nsDelegate.addMethod("toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:", "@", "@@i", {
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;
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;
263 toolbarItems.release;
264 if(toolbarItem.notNil, {toolbarItem.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);
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]);
307 "closing window".postln;
311 tabviewitems.do {|obj| obj.release;}
314 tabviewitems[1].invoke("view").className.postln;
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);
330 bp = SCNSObject("NSBezierPath", "fillRect:", [v.bounds]);