Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[cocoa] Programmatically create a GUI

80 views
Skip to first unread message

Dragonmaster Lou

unread,
Jul 10, 2002, 2:37:24 PM7/10/02
to
Assuming it's even possible to built a GUI using nothing but code (no
InterfaceBuilder at all) in Cocoa, can anyone here point me to any
documentation/pointers/etc., on how to accomplish this?

Basically, the program I'm trying to write involves dynamically adding
tabs to a tab view. It's a chat program, and each tab would represent a
different person, chat root, etc., you're currently chatting with. The
actual controls in the tab would differ slightly depending on who/what
you're talking to.

If there is a way to accomplish this with InterfaceBuilder, I'd
certainly consider it if suggested. :) However, I'm also curious if
there is a way I can build up a GUI at run-time because I can see some
situations where a static, pre-defined IB GUI may not be appropriate.
Also, if Cocoa has anything along the lines of the Java layout managers
(GridBagLayout, etc.), that would be very, very convenient.

Thanks!

--

-------------------- http://www.techhouse.org/lou ----------------------
"Dragonmaster Lou" | "Searching for a distant star, heading off to
lou at techhouse org | Iscandar, leaving all we love behind, who knows
Tech House Alum | what dangers we'll find..."
-----------------------------------------------------------------------

Doc O'Leary

unread,
Jul 10, 2002, 6:01:02 PM7/10/02
to
In article <slrnaiovo...@techhouse.brown.edu>,
Dragonmaster Lou <l...@SPAM.ME.AND.DIE.techhouse.org> wrote:

> Assuming it's even possible to built a GUI using nothing but code (no
> InterfaceBuilder at all) in Cocoa, can anyone here point me to any
> documentation/pointers/etc., on how to accomplish this?

Start with the AppKit documentation; the same stuff you should be
reading to be getting the most out of what Interface Builder offers.
That is to say, *all* Cocoa interface elements are done in code.
Interface Builder just gives you a GUI to do it with before it archives
the objects to the disk.

> Basically, the program I'm trying to write involves dynamically adding
> tabs to a tab view. It's a chat program, and each tab would represent a
> different person, chat root, etc., you're currently chatting with. The
> actual controls in the tab would differ slightly depending on who/what
> you're talking to.

So you're just talking about an NSTabView with some NSTabViewItem
objects that have different, custom content views. Whether those can be
built in IB or should be done on the fly depends entirely on details you
haven't given. Overall, it seems simple enough to accomplish.

Michael Ash

unread,
Jul 10, 2002, 10:01:35 PM7/10/02
to

> Basically, the program I'm trying to write involves dynamically adding
> tabs to a tab view. It's a chat program, and each tab would represent a
> different person, chat root, etc., you're currently chatting with. The
> actual controls in the tab would differ slightly depending on who/what
> you're talking to.

The tab view class has everything you'd need to know. It inherits from
NSView which will have all the stuff for making your tab view a subview
of your window (or whatever), and then there are APIs for adding and
removing tabs, I'm sure.

On a side note, this sounds like a horrible interface.

Chris Hanson

unread,
Jul 10, 2002, 10:23:40 PM7/10/02
to

> Assuming it's even possible to built a GUI using nothing but code (no
> InterfaceBuilder at all) in Cocoa, can anyone here point me to any
> documentation/pointers/etc., on how to accomplish this?

It is, in fact, possible. "Building Cocoa Applications" by Garfinkel &
Mahoney has an example. Apple's Cocoa reference documentation is
fairly complete now too, and should have everything you need for your
particular application.

In your application specifically, you shouldn't need to construct
everything by hand. You should be able to create a nib file for your
window and one for each different type of view you put in your tab,
etc. and then just load them as needed and use a little glue to hook
them up.

-- Chris

--
Chris Hanson | Email: c...@bDistributed.com
bDistributed.com, Inc. | Phone: +1-847-372-3955
Making Business Distributed | Fax: +1-847-589-3738
http://bdistributed.com/ | Personal Email: c...@mac.com

Jonathan Deutsch

unread,
Jul 10, 2002, 10:39:11 PM7/10/02
to
Dragonmaster Lou <l...@SPAM.ME.AND.DIE.techhouse.org> wrote in message news:<slrnaiovo...@techhouse.brown.edu>...
[snip]

> Basically, the program I'm trying to write involves dynamically adding
> tabs to a tab view. It's a chat program, and each tab would represent a
> different person, chat root, etc., you're currently chatting with. The
> actual controls in the tab would differ slightly depending on who/what
> you're talking to.

I assume you're talking about something in the style of Adium. I made
a Java source code editor that did a similar thing. I created the
original window and tab view in Interface Builder, but the dynamic
adding of tabs was done programmatically. I hope this code helps:

first, you'll need to make a NSTabView in IB with an outlet:

IBOutlet id tabView;

next, you need a method that is the action which creates the new tab.
In my program, a user double clicks a table item to open a file.
Therefore, I'm calling my method "open." I've stripped out the
unnecessary code.

- (IBAction)open:(id)sender
{
NSTabViewItem *editorTab;
NSTextView *editorView;
NSScrollView *scrollView;
NSRect theRect = [tabView contentRect];

editorView = [[NSTextView alloc] initWithFrame:theRect];

scrollView = [[NSScrollView alloc] initWithFrame:theRect];
[scrollView setDocumentView:editorView];

editorTab = [[NSTabViewItem alloc] init];
[editorTab setLabel:@"Sample Tab"];
[editorTab setView:scrollView];

[tabView addTabViewItem:editorTab];
[tabView selectLastTabViewItem:sender];
}

Hopefully that makes sense and helps you out.

I think I read somewhere that this "tabbed" interface is generally
against the Apple Human Interface Guidelines. You might want to look
into that.

(btw, this is my first post!)

- Jonathan Deutsch

P.S. Dragonmaster Lou - did you used to run a large Lunar site?

Dragonmaster Lou

unread,
Jul 10, 2002, 10:33:50 PM7/10/02
to

I know there are APIs to add and subtract tabs... So, in a nutshell,
assuming I understand what you're saying, I should be able to

1) Draw the chatroom tab in IB.
2) Draw the user chat tab in IB.
3) Use some NIB loading magic to load the NIBs into views as I need to
and stick these loaded NIB views in my tabs.

That sound reasonable to you?

> On a side note, this sounds like a horrible interface.

Well, I've used similar interfaces and liked them. The basic idea is
that the operations you can use on a chat room and on an individual user
are different, so each tab essentially has its own buttons just for
those operations, otherwise they look pretty much identical.

For the sake of an argument, do you have any suggestions on the
interface you'd use? I'm willing to take any input, although I admit
I'm modelling this interface on an existing chat program I've grown to
like.

Dragonmaster Lou

unread,
Jul 10, 2002, 10:37:46 PM7/10/02
to
In article <droleary.usenet-24...@corp.supernews.com>,

Doc O'Leary wrote:
> In article <slrnaiovo...@techhouse.brown.edu>,
> Dragonmaster Lou <l...@SPAM.ME.AND.DIE.techhouse.org> wrote:
>
>> Assuming it's even possible to built a GUI using nothing but code (no
>> InterfaceBuilder at all) in Cocoa, can anyone here point me to any
>> documentation/pointers/etc., on how to accomplish this?
>
> Start with the AppKit documentation; the same stuff you should be
> reading to be getting the most out of what Interface Builder offers.
> That is to say, *all* Cocoa interface elements are done in code.
> Interface Builder just gives you a GUI to do it with before it archives
> the objects to the disk.

I'll take another peak in there. It was kind late when I last looked in
there, and the docs looked a little dense at that time of night. :)

>> Basically, the program I'm trying to write involves dynamically adding
>> tabs to a tab view. It's a chat program, and each tab would represent a
>> different person, chat root, etc., you're currently chatting with. The
>> actual controls in the tab would differ slightly depending on who/what
>> you're talking to.
>
> So you're just talking about an NSTabView with some NSTabViewItem
> objects that have different, custom content views. Whether those can be
> built in IB or should be done on the fly depends entirely on details you
> haven't given. Overall, it seems simple enough to accomplish.

Hmm, okay... I think they can be mostly done in IB. There are a couple
of dynamic things I had wanted to do, but after reading some things in
this thread, I think I can get away with some static things and come up
with a cleaner UI that looks more like a real Mac UI. :)

Dragonmaster Lou

unread,
Jul 10, 2002, 10:40:22 PM7/10/02
to
In article <100720022123382765%c...@bdistributed.com>, Chris Hanson wrote:
> In article <slrnaiovo...@techhouse.brown.edu>, Dragonmaster Lou
><l...@SPAM.ME.AND.DIE.techhouse.org> wrote:
>
>> Assuming it's even possible to built a GUI using nothing but code (no
>> InterfaceBuilder at all) in Cocoa, can anyone here point me to any
>> documentation/pointers/etc., on how to accomplish this?
>
> It is, in fact, possible. "Building Cocoa Applications" by Garfinkel &
> Mahoney has an example. Apple's Cocoa reference documentation is
> fairly complete now too, and should have everything you need for your
> particular application.

I'll check the online docs in case my local copy isn't fully up to date.
I didn't see anything on my local copy, but that might be either due to
being incomplete or due to being half asleep at the time I looked at
them. :)

> In your application specifically, you shouldn't need to construct
> everything by hand. You should be able to create a nib file for your
> window and one for each different type of view you put in your tab,
> etc. and then just load them as needed and use a little glue to hook
> them up.

That would be the ideal. :)

Michael Ash

unread,
Jul 10, 2002, 11:47:11 PM7/10/02
to
In article <slrnaiprm...@techhouse.brown.edu>,
Dragonmaster Lou <l...@SPAM.ME.AND.DIE.techhouse.org> wrote:

> I know there are APIs to add and subtract tabs... So, in a nutshell,
> assuming I understand what you're saying, I should be able to
>
> 1) Draw the chatroom tab in IB.
> 2) Draw the user chat tab in IB.
> 3) Use some NIB loading magic to load the NIBs into views as I need to
> and stick these loaded NIB views in my tabs.

Loading NIBs isn't really magic, but yeah, that's the right idea.
Basically, you should do as much work in IB as possible, since it makes
changing things in your interface a lot easier. Also it's generally less
work to whip things up in IB than it is to write code for it.

> > On a side note, this sounds like a horrible interface.
>
> Well, I've used similar interfaces and liked them. The basic idea is
> that the operations you can use on a chat room and on an individual user
> are different, so each tab essentially has its own buttons just for
> those operations, otherwise they look pretty much identical.
>
> For the sake of an argument, do you have any suggestions on the
> interface you'd use? I'm willing to take any input, although I admit
> I'm modelling this interface on an existing chat program I've grown to
> like.

My main problem with this interface is that you limit the number of
conversations to the available space in your window. Something like one
window per conversation, or even a scrolling list, would remove that
limitation. Also, tabs generally switch between panels that are very
different from one another, although since each tab would have a
different conversation in it, I suppose you could make the case for that.

Anyhow, if you like it and it works for you, go for it. I just thought
I'd add in my two cents in case you hadn't thought this through at all.

Dragonmaster Lou

unread,
Jul 11, 2002, 9:53:21 AM7/11/02
to
In article <mail-EB2C2B.2...@corp.supernews.com>, Michael Ash wrote:
> In article <slrnaiprm...@techhouse.brown.edu>,
> Dragonmaster Lou <l...@SPAM.ME.AND.DIE.techhouse.org> wrote:
>
>> I know there are APIs to add and subtract tabs... So, in a nutshell,
>> assuming I understand what you're saying, I should be able to
>>
>> 1) Draw the chatroom tab in IB.
>> 2) Draw the user chat tab in IB.
>> 3) Use some NIB loading magic to load the NIBs into views as I need to
>> and stick these loaded NIB views in my tabs.
>
> Loading NIBs isn't really magic, but yeah, that's the right idea.
> Basically, you should do as much work in IB as possible, since it makes
> changing things in your interface a lot easier. Also it's generally less
> work to whip things up in IB than it is to write code for it.

True... IB is quite nice in that respect. :)

>> > On a side note, this sounds like a horrible interface.
>>
>> Well, I've used similar interfaces and liked them. The basic idea is
>> that the operations you can use on a chat room and on an individual user
>> are different, so each tab essentially has its own buttons just for
>> those operations, otherwise they look pretty much identical.
>>
>> For the sake of an argument, do you have any suggestions on the
>> interface you'd use? I'm willing to take any input, although I admit
>> I'm modelling this interface on an existing chat program I've grown to
>> like.
>
> My main problem with this interface is that you limit the number of
> conversations to the available space in your window. Something like one
> window per conversation, or even a scrolling list, would remove that
> limitation. Also, tabs generally switch between panels that are very
> different from one another, although since each tab would have a
> different conversation in it, I suppose you could make the case for that.

Well, I plan to include the ability to "detatch" tabs into their own
windows, which I think should alleviate the limited space problem.

> Anyhow, if you like it and it works for you, go for it. I just thought
> I'd add in my two cents in case you hadn't thought this through at all.

Thanks, I appreciate it!

Dragonmaster Lou

unread,
Jul 11, 2002, 10:13:15 AM7/11/02
to
In article <4844f9aa.02071...@posting.google.com>, Jonathan

Deutsch wrote:
> Dragonmaster Lou <l...@SPAM.ME.AND.DIE.techhouse.org> wrote in message news:<slrnaiovo...@techhouse.brown.edu>...
> [snip]
>> Basically, the program I'm trying to write involves dynamically adding
>> tabs to a tab view. It's a chat program, and each tab would represent a
>> different person, chat root, etc., you're currently chatting with. The
>> actual controls in the tab would differ slightly depending on who/what
>> you're talking to.
>
> I assume you're talking about something in the style of Adium. I made
> a Java source code editor that did a similar thing. I created the
> original window and tab view in Interface Builder, but the dynamic
> adding of tabs was done programmatically. I hope this code helps:

I haven't used Adium. Actually, what I'm planning to do is in the style
of X-Chat (www.xchat.org). More specifically, I'm planning to port
X-Chat to Aqua, while trying to bring the UI more in-line with Apple's
guidelines. Whether or not I'm insane to do that is another story. ;)

[code snipped]

> Hopefully that makes sense and helps you out.

It looks good to me. I'll give it a try a little later.

> I think I read somewhere that this "tabbed" interface is generally
> against the Apple Human Interface Guidelines. You might want to look
> into that.

I'll take another peek at them too.

> P.S. Dragonmaster Lou - did you used to run a large Lunar site?

Yes, I did.... It's still up (after changing servers and losing quite a
few of the pictures *sigh*), although it's massively in need of an
update. I never thought anyone would remember that site in this group
of all places. :)

0 new messages