WWDC2003 Session 423
Transcript
Kind: captions
Language: en
my name is Xavier lagoon and the Mac
os10 Avengers I work in the reparations
and i'd like to welcome you to session
making hamburger and kevin application
the funny thing is that we were talking
with the guys from which I to lburg and
we're wondering about the sessions this
year we had great content for HIV we had
wanted to give you a good update on
what's going on with developing using
vhi toolbox and then we go to the
conclusion that there were still one
missing piece you see people have been
using cover any different terms in the
last three or four years since Mac os10
there is this notion that carbon you
know it's ready to carbon leave and so
you know some people think that well you
know they're coming to the platform is
wondering well should i use carbon it is
carbon leave don't want to work on mac
OS 9 so wanted to emphasize the fact
that you know when we talk about crabber
and development now on mac OS 10 we r we
talking about developing using the tool
box and it's the idea is writing a
border and c and c++ application and the
goal of this session is really to like
kind of scratch what we've been doing of
like you know scratch like the fact that
you know you have covered it to support
and mac OS 9 because we think now we are
past that point and we want you guys to
let make sure we understand that as well
and the goal of this station is way to
focus on new technologies we've been
introducing new concepts that are very
very important on mac OS 10 for you to
develop a modern c and c++ application
all right so vital like to invite David
McLeod on stage who is going to be
taking you to like the different
concepts and the main key architecture
that you should think of when you
develop your application on my question
thank you very much and thanks for
coming I really am happy about that
because it means to me that our message
is getting out that the H I Toolbox
isn't renewed and you can make a modern
carbon application with a new h I to a
book first I'd like to kind of qualify
who should be here it's kind of three
groups first people that haven't made a
carbon application yet second people
that have made applications but remember
maybe Mac OS 9 based and made to wait
next event applications and third
there's probably a group of you that are
responsible for porting applications to
Mac OS 10 so this will be helpful for
you and that's sort of the target
audience today oops what do we mean by a
modern carbon application modern carbon
application is a full OS can citizen
behaves efficiently doesn't block and
use up too many resources and we get
that because we've introduced a
simplified event dispatching model
called carbon events were you not
pulling any more to get carbon to get it
to handle your event and this makes for
a very efficient application also your
user interface is now defined using nibs
get into those in more detail as a
session goes on also your modern carbon
application uses up-to-date OS services
like navigation services CF preferences
icon services quartz all the modern
services that are available to you on
the operating system so what I'm going
to go over today we're to show you how
to make a new carbon application using
Xcode go over making a user interface
using interface builder making your user
interface a noob files I'm going to show
you some of the user interface elements
that are available in a chai toolbox
it's actually very complete and you can
make a very full application just using
the pieces that are available from h-i
toolbox having those pieces of nice but
also show you how to handle events and
commands
so you can deal with the user as the
user application I'm also going to touch
on using alerts in your applications to
sort of interrupt and notify the user of
things and communicate with them the
last thing I'll touch on is navigation
services a little bit just so you can
show how to interact with users in terms
of selecting files and folders so I'm
really only going to cover two
frameworks today and they're both sub
frameworks of the carbon frame work so
I'm going a couple cover a chai toolbox
was going to cover all of the user
interface stuff I'm also going to touch
on navigation services overall what is
carbon like when we talk about carbon
what does it mean a lot of people think
you know its carbon lib likes of you is
hanging or that it's some sort of
bridging technology just to get you onto
s 10 it's actually not true carbon is a
full set of C and C++ API is that you
can use to make a full Native Client a
native application on OS 10 gives you
full access a P eyes give you full
access to the left and services that are
available to you like the user interface
accessing the file system using the
drawing primitives in courts
specifically a chai toolbox is the seat
set of scene C++ ap is that you can use
to make a user interface
so we're throwing around the term modern
a little bit what do I mean by being a
modern application something that's
important to us is that you use xcode so
that you can make a mock o application
another benefit of using xcode is that
your application will get may be made as
a bundle what kind of cover that in the
next slide also being modern means that
you're going to use interface builder to
design your interface and make it and
make them user interface with nib files
so they see the pre stated previously
being modern also means using existing
mac OS tennis services that are
appropriate for your application such as
quartz navigation services a chive use
to make your application also you want
to use the carbon event dispatcher so
that the modern efficient non polling
event dispatching mechanism that's
available in a chai toolbox I'm going to
cover that later on so for those of you
that may be familiar with writing Mac
applications the old way may be similar
to the way you wrote them on OS 9 or the
way you wrote them using carbon lives
and weight next event applications
there's a few differences first the old
way a lot of the API is dealt with
pascal strings that made it very hard to
internationalize your products because
you couldn't use extended character sets
are available and are needed for other
languages like Japanese Chinese etc all
of the new AP is ucf strings and that
means that they're Unicode savvy and you
can use all these extended character
sets to provide your your application
specifically the user interface the old
way of dealing with events with use wait
next event and pull continuously using
up cpu cycles the new ways is to use
carbon events and not really pull at all
and only be notified when important
events are coming to you that means that
you block and you're not using the cpu
you and your an efficient great
performing application on OS 10 the old
way of dealing with files is not even
available anymore standard file and is
replaced by using navigation services to
select files and folders and tell the
communicate between the user and the
application where files are going to be
saved and retrieved from another
difference is moving from a single
application file where the application
was totally enclosed in this one big
file with resource forks and all kinds
of things in there to an application
bundle application bundle it seems the
same to you when you use OS 10 but it's
still like when you go to use OS 10 it's
it appears to be one icon with
everything all squished into one binary
but it's actually represented by a
folder underneath that's only evident to
you as a developer and that folder
contains the binary of your application
and your nib files any other additional
files like maybe some sounds or some
image files as well as your plist to
describe yourself so it's actually a
whole hierarchy that's underneath you
can also provide localization
information in there and it's all
seamlessly done through the application
bundle the old way of making your user
interface was to build it up in a set of
resources and maybe low gets loaded with
the dialogue manager or something the
new way is to use a nib based user
interface by designing your user
interface with interface builder I'm
going to talk about mibs a little bit
later on in the presentation the old way
of actually creating your user interface
was to use controls and the new ways to
use a chive use they've totally replace
controls even the things that you're
used to calling controls like buttons
etc are actually underneath the covers
really just a chive use so the modern
way is a chive use because they also do
what we call the compositing system and
that means that the drawing model is
much more efficient when your user
interface draws only what needs to be
drawn gets drawn and lastly the old way
of drawing what to do it with quick draw
and ports and all that and the new way
it's course it's got all kinds of really
great features it's very efficient
and it's used system-wide so you want to
use the system-wide drying mechanism in
your carbon application so I'd like to
invite guy fullerton up on the stage
he's a chai toolbox engineer with me and
he's just going to show you how to make
an application with Xcode switch over to
the demo yeah good all right so what I'm
going to show you today is just kind of
a reinforcing of some of the things
David's going to talk about on his
slides let's say I want to build a new
application let's say I want to make
some kind of picture viewer so I'll take
you through the steps I might do for
that first thing I'm going to do is
launch Xcode to create my project and
I'll choose new project now though
there's a couple different choices for
carbon applications there's just plain
old carbon application and then there's
the need-based carbon application I want
the mid-bass one because that's the more
modern alternative so I'll choose that
let me put it on my desktop and let's
call it I'm spacing I've got to get this
right otherwise everything's going to go
bad let's call it strip okay so Xcode
creates the project for me and Xcode is
going to take care of a lot of the other
sort of modern features automatically
going to build the application into a
bundle and it'll have a plist and all
those kinds of things so Xcode already
sets up a main for me it's just a very
skeleton main does the basic things you
need to do a tap bring up time which is
very little these days for a modern
carbon app and I'll go through that in a
little bit more detail in a later demo
but for now let's just build it and run
it and see what we get so the app
launches it just gives us a basic window
we've got a menu bar that you know you
can track in and whatnot you can resize
the window unfortunately that's not the
most modern UI for resizing but we'll
take care of that little while later so
that shows you just how to get sort of a
bare bones skeleton application up and
I'll go into more details in successive
demos later so it's david so hopefully
you've installed panther on your
machines already and maybe have tried to
do this and you've seen that it's pretty
easy just to get a simple app up and
running
but a simple apps not very fun unless
you have a user interface it's not all
about being fun but you want to be able
to have some functionality and your
functionality can't really be provided
to user without a user interface when
you make your user interface you want to
make a modern user interface and I'd
like to kind of touch on the point that
really define what a modern user user
interfaces first you want to adhere as
closely as possible with exceptions to
the human interface guidelines so
there's a document that will refer will
put up a reference to you later on and
you could read through that it sort of
gives you the guidelines about how
you're supposed to use user interface
elements in conjunction with each other
one thing about laying out the user
interface you can see in the top right
here I have a picture of interface
builder there's some little blue dashed
lines so it has a guidelines feature an
interface builder helps you lay out your
user interface according to some of the
guidelines provided by the human
interface guidelines document the next
thing you can do to adhere to the user
interface guidelines the aqua excuse me
the human interface guidelines is to use
a chai toolbox controls we worked very
hard on the controls to make sure they
adhere as strongly as possible to the
guidelines given to us by the human
interface team and if you use them you
get all of our hard work for free next
you want to use modern compositing
windows I mentioned compositing earlier
it's a drawing model where only what's
needed to be drawn gets drawn in your
application and it's much more efficient
you can provide an application that's
much more modern by using this it's kind
of nice because it's just a checkbox in
interface builder you click the checkbox
for compositing you use all of our
controls they can be used in composited
windows and you get the efficiency right
there for free that's nice the next
thing is live resizing guy showed he was
resizing the window there in the
application it just shows showed an
outline of the window and that's not
really a responsive user interface so
you want to turn on live resizing again
it's just a checkbox in interface
builder you check it and that means that
as the user modifies the window changing
the size etc the user interface response
instantaneously and the user can see
what resizing that window
you also want to use the standard event
handler again guess where the check
boxes using the standard event handler
for a window means that all of the
routine event handling for a window like
what to do when some clicks on the title
bar and drag it around or clicks in the
closed box or the grow box all gets
handled automatically through the
standard event handler also when it
comes time to interact with the user may
be to interrupt them and give them
information you can use sheets or
standard alerts as appropriate when it
comes time to interact with the user to
describe locations of files and folders
you can use navigation services so all
of these things together will give you
the basics of a modern carbon
application so I'll talk about the user
interface elements that are available to
you there's quite a few I can't stick
them all on one slide so I had to break
them up into bits because there's just
so much available to you first there's a
few kinds of menus you can use there's
many as you're used to that come out of
the menu bar there are contextual menus
that you get when you command click or
right click on user interface and these
contextual menu menus changed according
to what piece of user interface is being
interacted with and there's also pop-up
button menus all of these are just lists
of commands that the user can can make a
select to perform action so they're just
simple actions that a user can pick next
we have a whole bunch of different kinds
of windows and you want to use windows
to present your user interface and your
information to the user so the biggest
window we have up there is a document
window you'll put your the contents of
any sort of document functionality of
your application there here it's just a
picture from text edit I think or simple
text simple text out the side of that
window is a drawer where you can provide
additional information about specific
content on the bottom left there's a
utility window that's a good place to
show properties of objects in a user in
fish and the bottom right I show a sheet
and a sheet is a kind of window that
actually is grouped with like a document
window and it comes out of the top and
is associated directly with that window
so you can interrupt the user and
provide information about a specific
window that way next there are a few
kinds of toolbars you have metal tool
bars I show a few examples here the
second one is large icons with text and
then I shall see me is there's different
modes that you get all automatically and
free you can select small icons without
text and you can have icon list entirely
I don't know if you can see right well
in this slide but there's a little
Chevron on the right and that that's all
provided automatically the chevron on
the right turns into a set of menu items
when there's not enough room to display
the toolbar functionality is available
now the controls there's a lot of
controls available to you and I'll break
it out into a few slides in a second but
I've highlighted some in yellow or
orange here these are the new ones that
we've introduced in Panther we have that
segment view has a little asterisk there
because we didn't finalize the api's in
time to get on to your seed CD sorry
about that my fault a little bit also
we've introduced a text view which is a
wrapper around the MLT text em ltd text
objects that's very powerful we've also
added a search field so that you can
provide the search functionality like
you see in the finder and that you've
seen introduced in many places in
Panther let's go back for a second so
I've kind of divided those up into a few
different groups that's kind of the way
I group them mentally I don't know if
everybody does well I'm just going to go
through those groups to talk about the
different kinds of controls that are
available so the first group is buttons
there's push buttons that you're all
used to radio buttons and checkboxes
that if you don't know what they are by
now you've been using them for quite a
long time the standard ones you're used
to we have bevel buttons sort of squares
buttons they can have icon and textual
content
little arrows which you see are the top
on the top right there it's a little set
of arrows used to well allow a user to
increment or decrement some values and
we have the new segment view which is
you can see on the bottom like take a
second to tell you what the segment view
is the segment view is a way to allow
the user either to select something or
to provide some sort of state
functionality when you click on the
buttons it's very powerful guy had a
session earlier where he talked about it
in detail if you can see that on a DVD
he really provides it a lot of good
information about the segment view the
next group is text controls we have a
lot of those now because we've been
adding some first there's edit text
which are used to for putting small
textual items in say a forum user
interface of some sort static text for
labeling pieces of your user interface
we have the new text view where you can
provide much more complex textual
information allow the user to edit it
and have it styled etc using the ML te
objects at the Edit Center we have the
clock control which is what we call the
feature that used to display the time
and the date and you can allow the user
to set the display times or dates to
users or allow them to set times and
dates we have the new search field which
you can use to provide the user a chance
to give you feedback about what they
want to search for it should be use
exclusively for searching rather than
any other specialized content because
it's it's been branded to be a search
field and it's very important to
maintain that look as something a user
will automatically think about as a
searching item and lastly up there we
have a combo box which is sort of a
combination between a pop-up button with
a menu and a text field where they can
sort of type select things
the next group is tracks so we have the
scroll bars in there where a user can
scroll the user interface a slider for
indicating values of us on a sliding
scale progress indicator for you to
provide feedback both the progress of
your application that's the one that i
got a blue rectangle on it there and a
relevance bar for providing feedback
about how relevant maybe something in a
sort of searches is to user the more
full the relevant spark more relevant it
is to the user we have kinds of controls
I call grouping controls like the tab
control which is the outermost group
here says 12 at the top or you can group
the user interface by tabbed pains a
group box show that there is just to
sort of put a box around these are
interface elements to relate it to each
other there's also the radio group which
is kind of a nice feature does some
automatic behaviors for you you can put
up a series of radio buttons then as the
user selects the radio button it
communicates with the other radio
buttons to turn them off and on
automatically and the last kind of
controls is kind of a miscellaneous pack
it's our specialized controls we're
getting to have a quite a few of those
now so we have an imageview got
spongebob up in an image view here is
anybody who no who is fungible obvious
if you come to my office you can meet
them we also have the chasing arrows so
that used to be a control that had
arrows chasing each other now it kind of
looks like a pinwheel or a spoke effect
shows an animation that shows your
applications doing something we have
separator bars there's a vertical and
horizontal lines you can draw those to
the user interface guidelines by using
that control they use their use to
separate your user interface into
different compartments we have the icon
control and image well where you can
show a sort of other image content in it
they're clickable we have the scrolling
view here that's that other image here
with a person then pink in it and it
allows you to put content in it and
automatically scroll it a lot of
functionality that you got used to have
to do by hand and also for brought
providing sort of hierarchical data
representation or table data
representations we have a sort of a
complex control called a data browser so
back to the big list that shows that
there's a lot of user interface that you
can use without creating your own to
create your user to create your
applications user interface you
shouldn't really have to create too many
custom controls you should be able to
use these I make a fairly full-featured
application all these user interface
elements it's nice to talk about them
but I'd like to just touch on nibs and
talk about what i mean by nims nibs are
an xml-based extensible way of
describing a user interface I'm going to
get I think it's best of all to show guy
it to get guy to show you how to make
them but before he shows you I'm going
to talk about how you can load your user
interface out of a nib loading a nib is
very straightforward there's an API
cells create nib reference so you're
going to have a file inside of your
application bundle and you'll know the
name of it you provide it to this API in
the incoming nib name parameter tell it
you want to load that nib reference and
that's all there is to loading it you'll
bloat after that you'll load all of your
nib content out of it when you're
finished with it you're just both of the
nib reference now I don't mean that you
have to keep your nib reference around
for the whole lifetime of your
application just open the nib reference
get what you need out of it and close it
again if you need to use it later on in
your application you can reopen it
without a problem that's okay but you
know all you're doing is opening the
neighbor reference the interesting part
is taking your user interface out as an
impressions so there's a few api's for
doing that you can set your menu bar
from a nib you can create a menu from a
nib or you can kind of most
interestingly because you're going to be
making a lot of your user interface in
Windows you can create window from the
all of these api's take a reference to
the nib that you created with creating
of reference the API showed a previous
slide as well as a name that you have
decided while you were creating the nib
so say you need you named your window
main window you provide that string
there and that is the nib that will be
loaded with create window from nib once
you've loaded a few things out of your
nibs so what you know you have you've
loaded them what do you do then how do
they work there's a simple API called
run application event loop and it's
simple because it's very easy to call I
have a prototype at the bottom here run
application event loop and it kind of
get everything going it starts the event
dispatcher which starts all of the
standard of and handling that happens in
your application it's important to note
that this API you call it and it goes
away it doesn't come back for the
duration of your application until your
application quit does all the standard
event handling like application d
activation and deactivation not kind of
thing it calls all the event handlers
that are necessary for your user
interface to work and it's pretty easy
to use and why I bring this up is
because just in the API is that I've
gone through I can show you a nib sample
code right up here and it's a full
application doesn't do much it's just
user interface user interface but it's a
full application so I create a nib
reference referring to the nib file in
my application bundle I set up a menu
bar from that nib by passing in them I
have a menu bar named my menu bar in
this example then i create a window from
the nib called my window i'm done with
the nib reference so i disposed nib
reference i run application event loop
this is a full application and it'll
start up put up a menu bar put up a
window that we can be dragged around and
used so i'd like to right guy to show
the next step making a user interface
with interface builder thank you David
okay so we've got all the he told us
about all the tool
we need to put together an interface and
I will show you how to build one so when
I created the project with Xcode it
automatically created a main nib for me
and in fact it loaded some of those
things automatically in the little
skeleton app that Xcode created for me
and this code looks almost identical to
the code David showed you before so no
surprise there so let's take a look at
the nib that Xcode created for us and
we'll look at that with interface
builder first thing you'll notice is
that the title of the name is main name
that is the minus e dot nib part minus
the extension that is the name you would
pass to the create nib reference API to
create your nib and similarly everything
that's in your nib also has a name for
instance the many bars just called menu
bar so you would load it with that name
and the main window is just called main
window and loaded with that name alright
so let's actually do something
interesting with this nib open up our
window in the nib and I want to make a
picture viewer so I need a place to
display some pictures first of all so
I'm going to Oh before I do that I
actually want this thing to look a
little bit better than just a plain
white canvas so when we bring up the
inspector and I want to set a few
attributes on the window so there is a
notion of a theme brush that can be
associated with the window and this
determines how the window background
paints since this is going to be a
normal window in the document layer of
an application and that's sort of the
modeless layer I want the modeless look
which will give me the aqua line now
another thing I need to set on the
window is I want some of those modern
features turned on so I'm going to cook
the library size checkbox and a
compositing check box so that i can use
a chive use and so i can get a nicer
looking resize and just that sort of
rectangle alright so now let me actually
add some stuff to the window I want a
place to put my images so I'm going to
drag out a group box here but I don't
want to title on it so i'll delete that
and let me put it in the right place so
here's an example of some of the guides
david was mentioning for interface
builder it lets you sort of snap your
your controls and views to the right
place according to the user interface
guidelines and interface builder does
that automatically so let me grow this
thing out a little bit and I want some
push buttons
you've done the right spot here on the
previous button one thing I want to do
to this button is I want to be able to
react to it when the user clicks on it
and the way I'm going to do that is to
set up a command associate a command
with this button just a little for
character code this command will
automatically be sent out via the carbon
I met mechanism whenever the user clicks
on the button now another way i want to
set up this button is by giving it a
signature this signature allows me in my
code to be able to fetch that button
from the window i can say for you know
give me give me the control with the
signature previous and id0 out of a
window so I can manipulate it later if I
need to so I'll go ahead and set that up
and in addition to the previous button I
want a next button let's get that in the
right spot give it the right title of
course a signature and a command to get
broadcast and now I want to make sure I
get that at the bottom of the group box
at the right position according to the
human interface guidelines and now I
actually need to be able to display an
image and we've got a view all set up
for that for you out already it's called
a chai image of you drop that into my
group box throw that to the right size
and I need to set a image view specific
oh I needed you two things to the image
you first of all need to give it the
right signature so I can find it later
just one does need a command so I don't
need to do that let's set up an
attribute I'm going to set the scale to
fit attribute it just modifies the way
the image will get actually displayed in
the image view okay so let's save our
noob and rebuild our project and it
comes up just like that so the interface
got loaded now unfortunately even though
we've got live resize turned on the
views are not relaying out this is
something we addressed in Panther we
have a new set of API is called HIV
layout we're going to talk about it in
session I think it's for 25 which is
immediately following this one but in
the room upstairs and we'll go into
details on exactly how to use that now
I've already written some code to do
that and
go ahead and add that to our application
and add a bunch of files we add it to a
different place here add a bunch of
files so I'm also adding some files for
some other functionality I'll show off
later the important thing I needed is in
filmstrip view see let's go back into
main and let's set up the layout so I
will need to include a prototype that's
in filmstrip view and all right so see
I've created my window right before I
show it i want to set up the layouts
that take advantage of HIV layout
automatically resize all my widgets and
that is yeah my setup window layout
function so now the theft and i should
be able to build and run and now when i
resize the window oh you know what I
must not I must have missed something in
the NIT but I'll see that later so
anyway the the buttons move around
appropriately and the group should move
around as well but I bought something up
but that's okay I'll fix that for the
next one so that shows you how you can
construct a basic interface and like I
said we'll go into more details on how
to use HIV layouts which is a really
powerful technology and we'll go into
details in another session but so in
subsequent demos I'm going to actually
add some functionality to this app but
before I do that David needs steadily
some more groundwork for you Thank You
guy so guys showed that it's pretty easy
to use interface builder to add some of
the user interface elements that are
available from h-i toolbox into your
application so that you can provide
feedback to the user about how your
application is going to look and work
next I want to go over roughly what the
architecture of a modern carbon
application is so we showed you the nibs
that define the user interface I think
that's very important once those nibs
are up carbon events or what makes it
would animate it and what give it life
so the handling of carbon events is
important like I mentioned before it's a
non polling way of handling events where
you only listen to the events that are
important to the application I'm going
to talk about event handlers in a few
slides there's also the handling of H I
command an H i command the way I think
about them is that they're sort of
simplified H I its simplified carbon
events where they just describe an
action that the user wants to take and
you do this through an H I command which
is just a 32-bit values that identifies
an action you also want to provide a
reactive user interface what I mean by
that is that as the user as the
application changes its state underneath
you want the buttons and menus etc to
react to that change of state by
becoming enabled or disabled or how the
names of menus change etc menu items
change etc other than that all you need
to focus on is the functionality that
you're providing in your application
that makes it special and makes your
customers want to buy it so here's kind
of a pictorial overview of what I just
said a minute ago so we've got these
yellow blocks up here they represent the
nibs that you made as guy showed you as
you load them in you can maybe
programmatically modify them as he did
with adding layouts to them and we'll
show you that you can also install event
handlers to provide your custom
functionality from the application once
you load them you run obligation event
loop and the standard of ant handler is
going to take care of all the rules the
routine operations that's what those
orange circles mean that means that all
the window dragging and menu tracking
etc are going to be handled by the
application for free it's all routine
stuff and you don't have to worry about
it
let's say you put in a vent handler
command handler on the ok button when
they click the ok button you can have a
custom event handler that's there that
gets called and then you can deal with
your functionality afterwards and that's
really what you want to focus on is your
functionality that you're providing that
makes your application special like
you're talking about events what are
these events there's a basic messaging
system of a chai toolbox so when a user
clicks oh there's any typing or there's
communication buff between user
interface elements or they or the any
part of the application there's these
messages going around and their carbon
events a chai toolbox internally sends
tons and tons of events and it's really
that's what makes the whole event a
dispatching system so powerful the cool
thing is that you don't really need to
handle too many in fact you don't need
to handle any at all you only have to
focus on the ones you care about you
ignore most of them all the time there's
many many events that are happening that
you never even know are happening you
don't really care i mentioned standard
event handling a few times now what is
standard event handling and what's so
special about it well like getting your
eyesight this WWDC or getting a t-shirt
or whatever the best thing about it is
there's tons of free things in there it
means that you don't have to do any work
and the less work that you have to do
the more you can focus on the
functionality of your application so H I
toolbox really deals with all those
event of the events that happen
automatically like application
activation and deactivation you don't
really want to pay attention to that
unless you care menu tracking control
tracking and all the window handling the
more you use this automatic behavior the
better off you are because you haven't
had to do much work and if we change the
way the handling works we're going to
change it in the standard event handler
so that it behaves the specified way and
you'll get that behavior for free even
in an already shifting application if we
can best of all it simplifies your UI
code so there's less work for you less
work for you to keep track of and it's
easier for you to maintain your code
so I got a standard event handle off
because you notice some of the spaces
fell out a standard event handler all
squished together that's kind of a
special event handler that we have for
windows I mention it because you need to
turn it on with that checkbox that I
showed in a previous slide and was on in
the interface builder one guy was making
the user interface it handles all of the
standard window behavior for free so you
don't have to worry about dragging the
window around or resizing it or handling
the clicks in the closed box it also
much more importantly routes all the
mouse and keyboard events to the
controls and the user interface that you
use inside of a window to make up your
your applications interface as I
mentioned earlier you just click a
checkbox an IV and you can forget about
it that windows just going to behave
properly for the duration of your
application so I'd like to kind of break
down events and event handlers a little
bit events are identified by their class
and their kind it's a pair it's a pair
of two 32-bit values so there's an event
class like k event control class like a
bit class control or k event class
window or k event class menu and then
there's different event kinds in those
classes so say with the the control
class there's k event control bounds
changed or k event control part hits or
whatever there's a lot of events as i
mentioned and we work very hard to
document all of them very well if you go
and read carbon events th inside the H I
toolbox headers in system library
frameworks they're very well documented
and the more you learn these the more
you learn about what kind of power you
can add your application when you need
to now there are a few different event
types we break them down into sort of
three categories there are methods types
of events and that's an event that gets
sent to something to tell it that it's
it's it's a chance to handle its
functionality
specific to to itself for example you
could have a key event controlled draw
event gets into a control and that's
that's the event system calling a
control and letting it know that it
should do its functionality then there's
also notification events that get sent
to different event targets Follette let
the target know that something is
happening an example of this might be a
que event control bounce changed event
so you can let a control know that
somebody has changed its bounds and a
control needs to react to that somehow
and there's also hook kind of events we
call them hook kind of hook event and
that's where the event system what's a
target know that something's happening
and then that target can optionally do
something about it if it wants to good
example of that would be maybe a que
event window bounds changing event
notice ing at the NSF changed instead of
changing instead of changed that means
that the window is in the process of
changing and if you want to override
that behavior bit and modify the way
exchanging before it actually changes
that's your hook to be able to do that
so I set of event targets a few times
there there are a few different kinds of
event targets the main ones are the
application eventtarget so any any
events that are going to the application
like the quick commander of the
application event applications activated
command event will go to the application
eventtarget there's also a window
eventtarget so any events that are
related to windows will go to the window
eventtarget same goes for controls in
there there's a few other specialized
kinds that I won't get into this is all
nice but what you want to do is install
your own custom event handler on the
pieces on the targets that are important
to you to add your functionality and you
do that with a custom event handler and
here's the prototype of how your
customer event handler should look so I
have it's called my control event
handler be great if it was bigger I
guess I didn't make it big enough and it
takes when it gets called it gets passed
an event handler call Ref
generally don't really care about that
unless you're passing the event on to
some other part of the system you get an
event ref which is important it tells
you what kind of event is coming in and
you're also given a chance to set up
some user data beforehand and that user
data will be passed into the event
handler let's go into that a little bit
more detail here's an example of an
handler it's a it's a control event
handler and it happens to be handling a
a notification that the control has
changed its bounds so first thing it
does is it takes some user data that's
passed in and it changes to cast it into
a control I'll show you why that is in a
little bit but it's a good way of
passing in which control is being
handled which control is being passed
this event next thing you want to do is
use get event parameter to extract
information out of the event as you
handle it in this case is extracting the
current bounds of the control as a
rectangle and sticking it into the new
bound hrs once you have that this is
where your new your chance to provide
your functionality you do something with
what the new bounds of the control was
maybe you relay out a control or I don't
know exactly what you're going to do and
then when you're finished you return
nowhere to mean I handle to control so
you really only want to return to
different types of things from an event
handler but the the event subsystem is
going to care about nowhere which means
I handle it and there's no errors you
don't have to worry about anything or an
event not handled there which means I
didn't handle event you can do whatever
you want on top of that installing an
event handler is very simple you use its
stall event handler and you pass in the
target and that means who is who you're
installing the event handler on and
who's going to be receiving these events
that you're going to be allowed to do
some handling on passing an event
handler upp that's a upp universal
procedure pointer I think to the
universal proc pointer to the
eventhandler custom event handler that
you've written you going to tell it how
many events you're interested in and
you're able to pass in an event type
spec a pointer to an event type spec
array which is a bunch of pairs of class
and kind event class and event kind that
describes which events your handler is
interested in receiving calls for the
last highlighted item in user data you
can pass in whatever user data you want
what I like to do is pass in a the
instance of the kind of target that I'm
passing in so if I'm installing a ton of
control almost always fast in the
control ref is my user data it's a good
easy fast way of setting your control
rest back out when your event handler
gets called so on a window I passing the
window ref as my user data etc and what
you get out of the event handler ref
which you can use in various ways
afterwards if you care to can be null
most of the time you don't really care
there are a few shortcuts to get the
target so everything has a target
doesn't mention or like an application a
window or control and so there are some
macros that are just going to wrappers
around this API where you can say
install install control event handler
and specify the control rather than
getting the target out of the control
and specifying that and just makes a
little bit easier for you to call it
there's a nice one for install window
event handler and you can call install
application event handler and it'll just
go get the current event application
target and pass that in very similar to
the one above
so here's an example of installing an
event handler like I said it's very
straightforward we got the prototype for
my van handler which this is implemented
elsewhere this is just a prototype for
it it's implemented a few slides back I
have an event types back array where
I've specified that I am interested in
caving class control class events and K
event control bounce chains event kinds
and then I just install control event
handler on my control goes and gets the
this visit the wrapper goes and gets the
event handler the event target rf4 for
that control passing a upp to my control
van handler conveniently on OS can you
pee pee's are just the function pointer
so in this case I'm just passing the
function pointer for an easy example I
count how many event types were in that
event list there's a nice macro in
carbon event about H get event type
count which we'll just do a size
calculation and passing the right number
there all the time no matter what you
change the array to pass in the event
list and also the last one there is my
control so I'm as my user data I'm
passing in the the point of the control
ref to my control so I can extract out
later on easily here's an example of a
little more complicated event handler
works handling more than one kind of
event by using get event class and get
event kind to check the class and kind
of the incoming event so uses the in
event parameter that's coming in who's
get event class and get event kind to
check if it's a again cave in class
controller key event control balance
change that's all nice but it's kind of
like to give you a little overview about
sort of the flow of the events and
commands in your application when an
event is first sent mr. kind of a
general thing out I'll talk more maybe
some specifics at the end goes to the
focus control if the focus control
doesn't handle it say in this case the
bottom of the focus control the if
button doesn't handle it it gives its
apparent the group box a chance to
handle it if the group box has any
parents say a tabcontrol in this example
if that parent a chance to handle it if
that control this and handle it gives
the parent window a chance to handle it
preparing window doesn't handle it gives
the application chance analyt so it kind
of goes up this it goes up the hierarchy
and gives everybody a chance to handle
events now there are a few exceptions
you know sometimes notifications aren't
really important to propagate or some
method calls you tell the control hey
dry yourself you know you don't want to
give the window a chance to draw the
control it just doesn't make sense so
there are a few exceptions and those are
all well documented and carbon up
instead H but I just want to touch on
handling commands a bit like I said
they're simplified carbon events where
you just have a 32-bit value identifying
an action that user wants to take and
what's what's really nice about these is
that the source of the command is not
really relevant issue if you want to
close the window you don't care if the
user is going to close the window by
clicking the closed box or selecting
clothes from the close menu all you care
about is they want to close the window
so that it's nice that you can kind of
ignore the source of a command and
that's what makes them special doesn't
really matter where they come from if
they can come from menus they can come
from controls they can be
programmatically generated you just want
to handle them say hey that's command
happens I want to handle that command
and it doesn't that the handle handling
of the command is really contextual so
whoever handled the command it kind of
depends on what the command is so you
can install a command handler on
different controls and your user
interface to respond or anywhere in any
of your user in any of your event
handlers like I said you don't really
care about where the command comes from
and what's nice about that is that a
command can come from multiple different
sources and you don't care as long as
you're getting from an in this case I'm
getting a khi command close which is a
sort of a standardized command that you
could use out of carbon event H and it
doesn't matter that it's coming from the
closed box or maybe an okay box on a
window or from a menu item all you care
about is that the user wants to close
and you can handle it and not worry
about where it came from so here's an
example command handler and it's just a
specialized event handler where we're
checking we're doing good event class
and get event kind again but we're
checking for this specialized class
commit a class and kind pair cave in
class command and k event command
process kind the command ID is a
parameter of the events we just extract
the H I command out of the event using
get event parameter and then we switch
based on the command ID and this is
where we dispatch out and handle our
action this is where you can provide
your functionality so next I want to ask
guy just to go through handling events
and commands in the application a bit
and kind of build up our application a
bit more okay yeah so let's let's add
some event handling here because Xcode
and interface builder already set up my
window to use the standard event handler
most of the functionality happens for
free you can click on the menus you can
resize the window drag the window around
click on buttons and stuff all I really
care about is when the user clicks on a
couple of buttons in my application now
if you recall back when I set up the nib
I put commands and in each of the
previous and next buttons and so those
commands are going to automatically be
sent out whenever the user clicks on
those buttons so I just need to listen
for those commands let's go ahead and
wire up that functionality okay so what
I need to do here is include another
header for the prototype I've got a
handler already written and I'm just
going to make sure to install that on my
window after I've set up the layout and
before I show the window
now let's take a look at that handler
actually before I show you the hand let
me show you something else I've already
defined some constants that represent
those same commands that I associated
with a button and I'll be using those a
little bit later on in the code so
here's my little wrapper routine that
shows you how to install an event
handler on a window I built up one of
those event types of X which is a list
of all the events I want to handle in
this case I only want to handle one
event it's an event of class cabin class
command k event command process or sorry
k event process command this is the
event that gets sent out when the
command is chosen from something and
then I install my handler on the window
and the handler looks an awful lot like
the code you just saw from David if I
get one of these events the cave and
class command K event process command
event I need to extract this H I command
structure that describes things about
the clan it tells you specifically what
that command ideas as well as some
information about where the command came
from so you can find out if it came from
a menu and if so which menu item should
you need to know that in my particular
case I don't need to know any of that so
I just switch on the command ID that
comes in and right now I've got some
subs out functionality but just for this
demo real quick here I'm going to beat
whenever the user chooses the previous
or next damage so let's go ahead and
build and run and hopefully we've got
volume on on this machine oh I don't
know if the volume is turned on okay
there we go yeah so you can hear this to
speak working that's not a whole lot of
great functionality I'll actually show
you how to put some real functionality
behind this in a few minutes after David
goes over some other features Thank You
guy so you can see that it is very easy
to install an event handler to take care
of the events that you care about in
this case it with some commands handling
commands thanks I just want to touch on
we're not really going to use alert
alert in our demo application I just
want to touch on alerts and when you
should use them so what are they there a
way of intercepting interrupting the
users tell them something important
you've seen them before so you can
inform the user of errors or warnings or
bad things that have happened maybe good
things that have happened I want you to
use them well you should use them when
it's important enough to interrupt the
users workflow with something that they
need to address there's two different
kinds of alerts you get standard alerts
which come up in a dialogue as you see
on the top there and that interrupts the
application application why that means
that the user can't use the application
anymore until they address the alert
that's come up in front of them you want
to do that with something that's
important say they quit the application
and they have multiple open documents
you want to interrupt the whole
application and say hey you don't want
to quit until you have you you've saved
all your hard work you're going to lose
it there's also sheet alerts and these
don't interrupt the application the user
application why'd they interrupt the
user just on a per window basis so they
come up out of a sheet maybe out of a
document window and interrupt that one
window and give information about that
one window so say user just going to
close a window that they had modified
you can have a sheet come out of there a
sheet alert come out of there and say
hey you've changed things in this
document you're going to lose them if
you just close it without saving using
standard works is very straightforward
there's an API called create standard
alert you pass in the alert type you can
give it a string for what kind of error
has happened or and you can give a
little explanation about that error
there's a few settings that you can set
up in a / am blocked I'm not really
going to cover that you can read about
it in dialogues H and what comes out is
a standard alert what comes out is a
dialogue ref to the standard alert which
you can then pass to the next API run
standard alert where you pass in the
alert dialog ref of the alert you've
just created and that will block the
application and won't return until the
user is dealt with that interruption by
clicking out one of the buttons and
closing the dialog and when they click
on one of the buttons which button gets
clicked gets passed back in the
out item hit parameter that you pass in
so it's a pointer to a dialogue item
index and you'll get the index of the
the part of that dialogue that was
clicked there's a set of standard ones
that are listed in dialogue H that you
can use to determine which how that
dialogue was dismissed and then you
could take the right course of action
using here's an example of them in use
so I'm creating a stop lurk because I
can't save because a file is locked in a
can override it so I just use create
standard alert to do that and I run
standard lurk an interruption comes up
the dialog that contains that message
with maybe an ok button that's all I
care about i run i run the standard
alert and when it comes back the only
way they can dismiss it is by clicking
the ok button my party it will indicate
that the ok button was clicked using
alert sheets is very similar you're
passing the alert type in there an
explanation everything is exactly the
same except they also passing a notified
target and this is important because
you're not going to stop the application
and block it application wide you're
only getting interrupted one window so
you'll want to pass in a target it's
going to get events from this this sheet
that comes up to let your application
know when when it's been dealt with so
most of the time you'll pass the window
target of the window that's being
interrupted and what you get out of that
is a dialogue ref to the standard alert
sheet that is created to show this sheet
use the show sheet window it's another
API you'll notice though that it takes
as its a parameter in sheet is a window
ref that's we've been using that
dialogue rest for a sheet of the
standard sheets up till now and it's
kind of a little bit of a legacy work
around where you have to get the window
out of the dialog ref I'll show that in
a second here so here's an example of
using a standard sheet so stop alert
that comes up there's this very similar
message to what I use before can't save
that file because the file is locked but
in this case I'm half passing a
notification target and that
who's going to receive events when
things happen to the sheet so i get
window.event target of my window and
that's the window that's being
interrupted by this standard sheet I get
that sheet back as a dialogue rest and
then I need to pass a window rest to
show sheet window so I get dialog window
from that sheet and that brings up the
sheet and then your application will
then continue and you'll only get
notifications about that she clothing
later on as events coming into your
event target in this case your window
vent target another way that you will
kind of need to interrupt to interact
with your user is file very very common
one users are doing all kinds of things
of files you want to be able to select
files and folders maybe select save
destinations and do you excuse me to do
this you want to use navigation services
it does all the work for you don't have
to worry about how the file system looks
or wherever things laid out or anything
like that you just call the api's and
it'll do all of the open save panel
stuff for you these are very used to it
they know how to use it and you're
providing user interface that they are
used to good thing about this is that
when navigation services revs like it
did for Panther you might have noticed
you get all these improvements for your
for free in your application so
everybody that was using navigation
services got the new open and save
panels application was just and wide for
Panther it's also very customizable so
if you need to do something special like
have a pop-up box box to select kind or
some checkboxes for some settings you
can add that to navigation services easy
easily if you're not doing any
customization it's very straightforward
to use navigation services in fact I'd
say it's pretty easy although the API
looks a bit daunting so say you want to
choose a file nav tues file you can
specify a whole bunch of parameters and
I encourage you to go to navigation
services framework navigation dot H and
read about them but in general unless
you're doing some customization don't
really care about those you'll notice
that they're all documented down the
right-hand side is being able to be null
so if you pass a null for all of them
you'll get the standard the standard
behaviors of navigation services
tracked all I really care about it's the
reply coming out that's the most
important thing because that tells you
what the user selected or that they have
selected now choose folder is very
similar again you can see there's a
whole bunch of different parameters but
the only real important one to you is
reply unless you want to do some
customization so here's navigation
services in action I use nav choose
folder I'm passing null for all the
parameters except for the reply which
I'm passing is a pointer to a nav reply
record after that returns I check to see
if the reply with a valid record if the
user actually selected something and
then because the reply can contain a lot
of a lot of information like multiple
selections etc eat we use Apple events
to extract the selection out of the
returned reply and it's pretty
straightforward so you use AE gets an
pointer you pass in the selection from
the reply you tell it you only want one
thing and you're interested in fsr and
you get you can get out your folder f so
let's bring guy back and he'll show you
how to use one of those api's in the
application okay so a picture viewers
not very good unless I can provide these
are a way to choose some pictures to
view so I'm going to add that sort of
functionality but I need to add another
button to mine in here so that the user
can choose something so let's add
another button cool it though I don't
know that folder make it a little bit
bigger so it looks nice and put it in
the right place now in order to react to
a click in this button I need to give it
a command so we'll do that and to show
off the power of clans a little bit
let's also modify our menu bar why don't
we skip the menu pronounced running oh
we're running short on time oh and EVR
okay so I could put that command in the
menu bar and book it that way as well
but for now we'll just put it in the
button here okay so we're saved the next
thing I need to do so I don't want to
really worry myself about the very
image formats that there are on Mac OS
that we support I'm going to let
quicktime deal with that because it does
it very well so what I need to do here
is add another framework quicktime
framework to my project do that like so
now the quick dry was in place the other
thing i need to do is I'd like to do is
take out those two speech I put in place
and put the real functionality behind
the previous and next buttons and so
you'll also see that I handle a third
command and that handler I already
showed you which handles the set folder
button now my code I've already written
for set folder just uses that NAB choose
folder API exactly the way David showed
you know if there before so we'll just
build and run this click set folder up
comes the nav dialogue and now I'm
browsing some pictures and I react to
that is the user clicks the next and
previous button so I did all that just
by installing one carbon event handler
and building my nib now that's cool but
not every application can just use these
standard views that we already provide a
lot of applications you need their own
custom views and I'd like one as well
I'd like this interface to be a little
bit better I'd like to kind of show
previews if you will of all the images
are of the images in the folder that I'm
browsing so we'll add that very quickly
reopen my nib screw the window a little
bit bigger and oh there we go I'm going
to throw a user painting place just sort
of a plate as a placeholder and my code
goes in and replaces just with my actual
view put that in the right spot let's
give him the who I'm trying to remember
my command hyah give them the right
signature so I can find him later and I
don't actually need to give him a
command so give it a command I think I
should give it a claim ok looks like a
mess Oh David knows we should give it a
command that means we we must all right
and so my codes already been modified
actually to deal with that so all I need
to do is rebuild and rerun the
application
and choose another folder this one and
all of a sudden hey I can see myself and
I get previews in myself as well and I
can scroll around and react to the next
and previous buttons these are images
from last year's demo so they just
happen to have these previews so that's
it that's pretty much straightforward
stuff will actually go into details in
the HIV you in-depth session on how you
can create a custom view not exactly
this one that I showed here but
something that's actually a lot cooler
because this one here is actually fairly
simple so anyway I'll hand it back over
to David you wrap up Thank You guy so
finally we got some functionality into
the application that the end
functionally that you're going to add to
your application which is important and
you can kind of forgets about some of
the user interface details they didn't
have to worry about the window resizing
and the window handling and all that get
the full application by focusing on the
functionality of the application so in
summary it's hard to show you everything
that's available in a chai toolbox we
just want to give you a good start so
you want to use the new h I toolbox to
make it fully featured full Mac os10
citizen using C and C++ API s I want to
use nib files to create a great user
interface but one that you don't have to
really you worried about how how it's
all implemented because you'll use
carbon events you only pay attention to
the attention to the events that you're
interested in to make your functionality
to make you a great app so I'd like to
bring as avi a up to go over some of the
documentation that's available and the
roadmap for other sessions that relate
to a chai toolbox thank
apologize we've been running a little
bit late so I'm going to try to like
quickly over the documentation to
Western for some questions we have a
bunch of documentation available now for
carbon events and H had you in general I
encourage you to go on VDC home where
you check to your list of the new
documentation and all the things that
I've available for doing C and C++
development when you look for the key no
documentation or simple could check out
H I toolbox first and then go to the
comment section depending on what you
want to find out some documentation as
well is available for like the human
interface guidelines to help you design
great user experience on Mac OS 10 and
hopefully none of you need the carbon-14
guide anymore as I said lot of sample
code looking the H I toolbox you know
for mac OS 10 folder on the sample code
in the carbon folder and you see a bunch
of things for like a shadow as prisoners
and encourage you to go which brings me
again to the documentation and which is
something actually the team wanted miri
twin size size which has been spent a
lot of time actually documenting the
headers as much as possible as the first
you know the first point of
documentation for you guys it doesn't
replace you know of course real
documentation but I think it's a good
start for you like when you're looking
for like what a carbon event does a
special carbon event and things like
that so check the header files one
session if you have kissed em controls
that you should really really really go
and see is the next one on the third
floor procedure room where actually edge
will be talking about HIV and we're
going to give you with very cool demos
when we're going to show you on how to
implement your own custom h-have you
very nice encouraged to attend that
session of course
you