Changeset 8480
- Timestamp:
- Feb 13, 2008, 10:51:31 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/source/examples/cocoa/nib-loading/HOWTO.html
r8479 r8480 149 149 <pre> 150 150 ? (setf *my-app* 151 (let* ((class-name (%make-nsstring "NSApplication"))152 (appclass (#_NSClassFromString class-name)))153 (#/release class-name)154 (#/sharedApplication appclass)))151 (let* ((class-name (%make-nsstring "NSApplication")) 152 (appclass (#_NSClassFromString class-name))) 153 (#/release class-name) 154 (#/sharedApplication appclass))) 155 155 #<LISP-APPLICATION <LispApplication: 0x1b8de0> (#x1B8DE0)> 156 156 </pre> … … 159 159 160 160 <p>First of all, it's going to store the returned application 161 object in the variable <code>*my-app*</code>, so that we have it162 convenient for later use.</p>161 object in the variable <code>*my-app*</code>, so that we have it 162 convenient for later use.</p> 163 163 164 164 <p>We need an <code>NSString</code> object that contains the 165 name of the application class, so the code allocates one by166 calling <code>%make-nsstring</code>. The <code>NSString</code>167 object is a dynamically-allocated foreign object, not managed by168 Lisp's garbage-collector, so we'll have to be sure to release it169 later.</p>165 name of the application class, so the code allocates one by 166 calling <code>%make-nsstring</code>. The <code>NSString</code> 167 object is a dynamically-allocated foreign object, not managed by 168 Lisp's garbage-collector, so we'll have to be sure to release it 169 later.</p> 170 170 171 171 <p>The code next uses the class-name to get the 172 actual <code>NSApplication</code> class object, by173 calling <code>#_NSClassFromString</code>.</p>172 actual <code>NSApplication</code> class object, by 173 calling <code>#_NSClassFromString</code>.</p> 174 174 175 175 <p>Finally, after first releasing the <code>NSString</code> 176 object, it calls <code>#/sharedApplication</code> to get the177 running application object, which turns out to be an instance178 of <code>LispApplication</code>.</p>179 180 <p>Voilà! We have a reference to the running Clozure CL181 application object! Now we can ask it for its zone, where it182 allocates objects in memory:</p>183 184 <pre>185 ? (setf *my-zone* (#/zone *my-app*))186 #<A Foreign Pointer #x8B000>187 </pre>188 189 <p>Now we have a reference to the application's zone, which is190 one of the parameters we need to pass191 to <code>loadNibFile:externalNameTable:withZone:</code>.</p>192 193 <div class="section-head">194 <h3>2. Make a Dictionary</h3>195 </div>196 197 <p>The dictionary argument198 to <code>loadNibFile:externalNameTable:withZone:</code> is199 used for two purposes: to identify the nibfile's owner, and200 to collect toplevel objects.</p>201 202 <p>The nibfile's owner becomes the owner of all the toplevel203 objects created when the nibfile is loaded, objects such as204 windows, buttons, and so on. A nibfile's owner manages the205 objects created when the nibfile is loaded, and provides a206 way for your code to get references to those objects. You207 supply an owner object in the dictionary, under the208 key <code>"NSNibOwner"</code>.</p>209 210 <p>The toplevel objects are objects, such as windows, that are211 created when the nibfile is loaded. To collect these, you212 can pass an <code>NSMutableArray</code> object under the213 key <code>"NSNibTopLevelObjects"</code>.</p>214 215 <p>For this first example, we'll pass an owner object (the216 application object), but we don't need to collect toplevel217 objects, so we'll omit218 the <code>"NSNibTopLevelObjects"</code> key.</p>219 220 <pre>221 ? (setf *my-dict*222 (#/dictionaryWithObject:forKey: (@class ns-mutable-dictionary)223 *my-app*224 #@"NSNibOwner"))225 #<NS-MUTABLE-DICTIONARY {226 NSNibOwner = <LispApplication: 0x1b8e10>;227 } (#x137F3DD0)>228 229 </pre>230 231 <div class="section-head">232 <h3>3. Load the Nibfile</h3>233 </div>234 235 <p>Now that we have the zone and the dictionary we need, we176 object, it calls <code>#/sharedApplication</code> to get the 177 running application object, which turns out to be an instance 178 of <code>LispApplication</code>.</p> 179 180 <p>Voilà! We have a reference to the running Clozure CL 181 application object! Now we can ask it for its zone, where it 182 allocates objects in memory:</p> 183 184 <pre> 185 ? (setf *my-zone* (#/zone *my-app*)) 186 #<A Foreign Pointer #x8B000> 187 </pre> 188 189 <p>Now we have a reference to the application's zone, which is 190 one of the parameters we need to pass 191 to <code>loadNibFile:externalNameTable:withZone:</code>.</p> 192 193 <div class="section-head"> 194 <h3>2. Make a Dictionary</h3> 195 </div> 196 197 <p>The dictionary argument 198 to <code>loadNibFile:externalNameTable:withZone:</code> is 199 used for two purposes: to identify the nibfile's owner, and 200 to collect toplevel objects.</p> 201 202 <p>The nibfile's owner becomes the owner of all the toplevel 203 objects created when the nibfile is loaded, objects such as 204 windows, buttons, and so on. A nibfile's owner manages the 205 objects created when the nibfile is loaded, and provides a 206 way for your code to get references to those objects. You 207 supply an owner object in the dictionary, under the 208 key <code>"NSNibOwner"</code>.</p> 209 210 <p>The toplevel objects are objects, such as windows, that are 211 created when the nibfile is loaded. To collect these, you 212 can pass an <code>NSMutableArray</code> object under the 213 key <code>"NSNibTopLevelObjects"</code>.</p> 214 215 <p>For this first example, we'll pass an owner object (the 216 application object), but we don't need to collect toplevel 217 objects, so we'll omit 218 the <code>"NSNibTopLevelObjects"</code> key.</p> 219 220 <pre> 221 ? (setf *my-dict* 222 (#/dictionaryWithObject:forKey: (@class ns-mutable-dictionary) 223 *my-app* 224 #@"NSNibOwner")) 225 #<NS-MUTABLE-DICTIONARY { 226 NSNibOwner = <LispApplication: 0x1b8e10>; 227 } (#x137F3DD0)> 228 229 </pre> 230 231 <div class="section-head"> 232 <h3>3. Load the Nibfile</h3> 233 </div> 234 235 <p>Now that we have the zone and the dictionary we need, we 236 236 can load the nibfile. We just need to create an NSString with 237 237 the proper pathname first:</p> 238 238 239 <pre>240 ? (setf *nib-path*241 (%make-nsstring242 (namestring "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib")))243 #<NS-MUTABLE-STRING "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib" (#x13902C10)>244 </pre>245 246 <p>Now we can actually load the nibfile, passing the method239 <pre> 240 ? (setf *nib-path* 241 (%make-nsstring 242 (namestring "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib"))) 243 #<NS-MUTABLE-STRING "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib" (#x13902C10)> 244 </pre> 245 246 <p>Now we can actually load the nibfile, passing the method 247 247 the objects we've created:</p> 248 248 249 <pre>250 ? (#/loadNibFile:externalNameTable:withZone:251 (@class ns-bundle)252 *nib-path*253 *my-dict*254 *my-zone*)255 T256 </pre>257 258 <p>The window defined in the "hello.nib" file should appear249 <pre> 250 ? (#/loadNibFile:externalNameTable:withZone: 251 (@class ns-bundle) 252 *nib-path* 253 *my-dict* 254 *my-zone*) 255 T 256 </pre> 257 258 <p>The window defined in the "hello.nib" file should appear 259 259 on the 260 260 screen. The <code>loadNibFile:externalNameTable:withZone:</code> … … 263 263 returned <code>NIL</code>.</p> 264 264 265 </div> 266 267 </body> 268 </html> 269 265 <p>At this point we no longer need the pathname and 266 dictionary objects, and we can release them:</p> 267 268 <pre> 269 ? (setf *nib-path* (#/release *nib-path*)) 270 NIL 271 ? (setf *my-dict* (#/release *my-dict*)) 272 NIL 273 </pre> 274 275 <div class="section-head"> 276 <h2>Making a Nib-loading Function</h2> 277 </div> 278 279 <p>Loading a nibfile seems like something we might want to do 280 repeatedly, and so it makes sense to make it as easy as possible 281 to do. Let's make a single function we can call to load a nib as 282 needed.</p> 283 284 285 <div class="section-head"> 286 <h2>What About Unloading Nibfiles?</h2> 287 </div> 288 289 </div> 290 291 </body> 292 </html> 293
Note:
See TracChangeset
for help on using the changeset viewer.
