Changeset 8480 for trunk/source/examples


Ignore:
Timestamp:
Feb 13, 2008, 6:51:31 PM (12 years ago)
Author:
mikel
Message:

added sections on making a general nib-loading function and nib unloading

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/examples/cocoa/nib-loading/HOWTO.html

    r8479 r8480  
    149149      <pre>
    150150        ? (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)))
    155155        #&lt;LISP-APPLICATION &lt;LispApplication: 0x1b8de0&gt; (#x1B8DE0)&gt;
    156156      </pre>
     
    159159
    160160      <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 it
    162       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>
    163163
    164164      <p>We need an <code>NSString</code> object that contains the
    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>
     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>
    170170
    171171      <p>The code next uses the class-name to get the
    172       actual <code>NSApplication</code> class object, by
    173       calling <code>#_NSClassFromString</code>.</p>
     172        actual <code>NSApplication</code> class object, by
     173        calling <code>#_NSClassFromString</code>.</p>
    174174
    175175      <p>Finally, after first releasing the <code>NSString</code>
    176       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           #&lt;A Foreign Pointer #x8B000&gt;
    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           #&lt;NS-MUTABLE-DICTIONARY {
    226                                   NSNibOwner = &lt;LispApplication: 0x1b8e10&gt;;
    227                                   } (#x137F3DD0)&gt;
    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
     176        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        #&lt;A Foreign Pointer #x8B000&gt;
     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        #&lt;NS-MUTABLE-DICTIONARY {
     226        NSNibOwner = &lt;LispApplication: 0x1b8e10&gt;;
     227        } (#x137F3DD0)&gt;
     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
    236236        can load the nibfile. We just need to create an NSString with
    237237        the proper pathname first:</p>
    238238
    239         <pre>
    240           ? (setf *nib-path*
    241                   (%make-nsstring
    242                      (namestring "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib")))
    243           #&lt;NS-MUTABLE-STRING "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib" (#x13902C10)&gt;
    244         </pre>
    245 
    246         <p>Now we can actually load the nibfile, passing the method
     239      <pre>
     240        ? (setf *nib-path*
     241        (%make-nsstring
     242        (namestring "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib")))
     243        #&lt;NS-MUTABLE-STRING "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib" (#x13902C10)&gt;
     244      </pre>
     245
     246      <p>Now we can actually load the nibfile, passing the method
    247247        the objects we've created:</p>
    248248
    249         <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
     249      <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
    259259        on the
    260260        screen. The <code>loadNibFile:externalNameTable:withZone:</code>
     
    263263        returned <code>NIL</code>.</p>
    264264
    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.