If this is your first time reading this, you probably haven't signed up yet. Do so now by clicking here.

Members Login

Username:
Password:
Remember me ?      

Advertisement

Recommended Books

Partner Links

Forum - iDevApps » Mac, iPhone, iPad & iPod Development » Mac OS X Programming » Real World Cocoa Apps (help me escape from windows hell)

Mac OS X Programming Questions and tips on developing Mac OS X applications, as well as porting.

Reply
Old 2007.09.03, 09:46 PM   #1
jturpin
Null
 
Join Date: Sep 2007
Posts: 5
Default Real World Cocoa Apps (help me escape from windows hell)

I am interested in learning how to create applications in Cocoa. I am a Windows programmer by day - and a die-hard mac fanatic by night (I have a 2G DP G5 Mac Pro and an 04’ Powerbook. I have spent the last year writing apps in C# and WPF, and prior to that many many years writing desktop and embedded rich media applications in C, and C++ - I don’t do Java, or HTML, or PHP or any of that other back-end web stuff - not dissing it, but I just don’t do it - I am pretty much strictly a desktop/GUI app guy. And programming on Windows is not really hell - well, at least not with WPF (Windows Forms on the other hand - just stupid). WPF actually has some really nice features.

I am having trouble finding real-world examples/tutorials for Cocoa applications - like using Apple interface standards - like the iApps or the Pro Apps. And, yeah - I have seen the Currency Converter tutorial - thats a dialog - not an App!

I was somewhat troubled by a recent post here asking how to implement something like the Aperture interface in Cocoa. The answers were basically along the lines of... thats all custom controls - thats apple’s Pro App framework (which why don’t they release a programming API for anyway?) - and good luck - its difficult. Well, it shouldn’t be. Isn’t that the nature of declarative UI frameworks - to make designing and programming the UI easy and rapid so that you have more time to concentrate on the hard stuff. The UI and controls should be the easy part. Free if you will.

So I sat down and thought about how I would do something like this in WPF - and basically developed an Aperture like shell over the weekend - using the latest dev tools from Microsoft - VS2008 and Expression Blend (a GUI/XAML editing tool) - kind of like WPF’s version of Interface Builder.

So anyway, the purpose of this little experiment was to figure out how I would create an App framework in WPF, and come up with detailed examples and questions for App development workflows in the Cocoa world.

As an example, the Icons in my app were drawn in Adobe Illustrator and imported directly into WPF as vector graphics and used as vector images in the buttons - this is really simple with WPF (and will be included in part of my discussion).

Is this possible with Cocoa? What is the fastest/easiest way to design an icon/graphic in an external editor like Illustrator and import for use in your application - with a preference in maintaining the image as a scalable vector graphic? (Note - WPF uses a resolution independent UI model - similar to Apple's Quartz - where vector graphics scale effortlessly - and in WPF - are drawn using HW acceleration - so why not keep all of your icons in vector-land and skip the step of having the designers render out pngs).

Part 2 in next post - read on if you are interested!

Last edited by jturpin; 2007.09.03 at 10:55 PM.
jturpin is offline   Reply With Quote
Old 2007.09.03, 10:01 PM   #2
jturpin
Null
 
Join Date: Sep 2007
Posts: 5
Default

Part 2:

I am not interested in starting a thread debating the pros/cons between C#/Objective C, or Windows vs OS X, or Mac vs PC, or managed vs unmanaged code, etc... I simply want some pointers on how real-world Cocoa apps are developed and how Cocoa/XCode can be used in a rapid development framework for rich GUIs. Both frameworks (Cocoa and WPF) seem to rely on a declarative concept for the UI (view) with code doing all the heavy lifting (model and controller bits). Though it seems that Interface Builder is not a true interface builder in this manner - at least not in the way the Blend can be used to design, refine and style an app - and also Interface Builder hides the declarative bits - i.e., NIBs are not editable, while WPF uses an editable XML file format called XAML (rhymes with zamel). Believe me it pains me to admit that microsoft has developed a neat application, but Blend is pretty damn cool.

I have seen many posts applauding XCode and Cocoa for its rapid application capabilities - but I don’t really know where to start. See here and here. As noted in the latter:
Quote:
Net vs Cocoa for Developers.
Despite being an expert in .Net but brand new to the Cocoa platform, Hoffman said the time to complete his project was much faster on Cocoa. The same project took only one third the time, despite his having far more experience with Vista's .Net frameworks than Apple's Cocoa.
My idea is to demonstrate how I created my shell in WPF - hoping to illicit a similar response from the Cocoa dev community for the same type of app in Cocoa.

If you have a Windows PC and would like to see see my working shell, you can download it here: (ironically, hosted on my .mac account...) and no, its not a killer windows virus/mallware worm, whatever...

Below are some screenshots of the app running on my windows XP box just to wet your appetites and let you know that I am not b.s.ing. I really did create an Aperture like framework in basically 1 and half days. Note that you need to have the WPF bits installed on XP in order to run this. It should run on Vista without anything extra.

Again, to reiterate - the purpose of this exercise was to create enough of a shell to cover all of the bases for the types of controls, UI elements, and layout mechanisms that are needed for real apps. It is obviously not a full-working app - and obviously the tree-view list is not styled - and there are many missing things - however, it demonstrates a working Menu Bar, ToolBar, a Content area spit horizontally 3 ways using draggable grid splitters with the middle (viewer) part split horizontally using a draggable grid splitter and the right info windows split horizontally using a dragable splitter. Also, all UI elements are implemented using vector graphics - so it scales beautifully from crappy old 13" screens to the illustrious 30" Cinema Display.

The styling is a an approximation of the “aperture” style - but I didn’t spend too much time dialing in the details - as obviously its not really important for this exercise - and in this "declarative" world - that part is meant for the designers - though in the "real-world" I haven't seen a workflow capable of truly integrating graphic designers into UI development.

Is this of interest to this community? Is this the appropriate forum to do something like this? If so I can continue in this thread, first with a short overview of WPF followed by a explanation of how I setup the app.

Some screenshots in next posts:

Last edited by jturpin; 2007.09.04 at 10:29 AM.
jturpin is offline   Reply With Quote
Old 2007.09.03, 10:18 PM   #3
jturpin
Null
 
Join Date: Sep 2007
Posts: 5
Default

Post 3: Screenshots


click for fullscreen


click for fullscreen

Icon design in Illustrator:




Icons imported into Blend (WPF App):

jturpin is offline   Reply With Quote
Old 2007.09.03, 10:25 PM   #4
jturpin
Null
 
Join Date: Sep 2007
Posts: 5
Default

So again, if this is of interest to anyone please reply and I will continue with an explanation of my process in hope of a similar/contrasting explanation from the cocoa pt of view...

hmm.... seems like embedded pictures don't work in this thread...just click on the links above to see the images.
jturpin is offline   Reply With Quote
Old 2007.09.04, 02:59 AM   #5
unknown
Moderator
 
unknown's Avatar
 
Join Date: Aug 2005
Posts: 225
Default

I would really stress strongly that as a budding mac programmer you should not recreate your own interface from scratch.
Using the standard Cocoa one to learn exactly how it all works before reskinning it is important for the usability of a program.
It's also somewhat grueling work which may be best spent on non-superficial programming like making sure an app is robust and efficient.
It has been done successfully in many cases though, but again it is not usual to do this as it can be confusing and may not add much.

A couple of examples of apps source code where the interface has been reskinned:
http://lumacode.com/simon/developer.html
http://fax.twilightcoders.net/Mov2Gif/

So, I'll reiterate, I think it is be best to get very well acquainted with the working of good mac programs and the interface before considering modifying it drastically.
unknown is offline   Reply With Quote
Old 2007.09.05, 02:05 AM   #6
titaniumdecoy
Moderator
 
Join Date: Aug 2007
Location: California
Posts: 122
Default

I am interested in an explanation of the process you used. I would also like to see what the "unskinned" version of your app looks like. (Correct me if I am wrong, but I assume that this sort of thing is almost always accomplished by subclassing the control in question and customizing the "paint" method.)

Additionally, it seems to me that custom UI skins are discouraged on the mac because it is so difficult to make a skin that looks as good as or (gasp!) better than aqua. Apple (and especially iTunes) is the exception, of course, although often their "custom skins" are incorporated into the mac UI in the next major OS upgrade.
titaniumdecoy is offline   Reply With Quote
Old 2007.09.06, 12:09 AM   #7
jturpin
Null
 
Join Date: Sep 2007
Posts: 5
Default

I think I am a lot less interested in doing custom skins, than I am in learning something simple like laying out the basic structure of the application. The thing that is really confusing is that it appears you can't really do this with Interface Builder. Maybe its the name "Interface Builder" that is confusing me?

As an example (lets take something simple and very Apple standard like iTunes for instance - yes I know its Carbon - but it IS defining a UI standard). You can identify the key Views and layout as something like:

Code:
Window
   Top Toolbar 
   Content
       SplittableView
          LeftView
              Library Chooser Panel
              Now Playing Panel
          Splitter
          RightView
             Big Honking Fancy List Control
             iTunes MiniStore
   Bottom Status Bar
So how do you layout these basic elements, views, whatever you want to call them - in Interface Builder? How do you make a toolbar? How do you add a splitter to a view? How do you make an Image Button? Can you use vector graphic images for Image Buttons? These are the kind of basic principles that I am looking for in a tutorial - which I haven't found anywhere.

I am willing to start simple - however, Currency Converter is just a little too simple.

The apertures shell was written using WPF - a brand new framework from the bad-guys. WPF is a complete departure from the "typical" way windows or even Mac controls and drawing are usually done. There is no drawing "event loop" that you are part of. The drawing model is a retained drawing model, in which there is a logical object tree - which represents all things that are rendered in your window. Rendering happens automatically and is all done in HW (directX) - there are no pixels. You can't really even get to a pixel - weird, I know... Its kinda like if you integrated Quartz Composer tightly into the Cocoa UI process.

There is an abstraction layer in WPF that is not present in cocoa and or Windows forms. Controls in WPF are inherently "lookless". The part that defines how the control looks (is drawn) is defined in a template which is a "property" of the control - an actual property in the OO language sense - so you can actually data bind templates to the control if you want. So depending on the type of data for instance, the control can choose different templates - and thats really neat.

The UI is meant to be defined declaratively using an XML based language called XAML - though this more or less optional, as everything that can be done declaratively in XAML can also be done in Code. The XAML part is meant to make it easier to make it more friendly for the graphic artists types. So this brings up a good question... Are .NIBs editable by hand? Is .NIB and XML format?

WPF Controls by default use a default template defined by the OS you are running on - so XP has different built-in templates than Vista for instance.

So to make a "hello" button, it would look something like:
Code:
<Button Content="Hello">
or in Code,

Code:
Button b = new Button();
b.Content = "Hello";
And this automatically uses the "default" template of your system.

The cool thing is that "Content" can be anything. So this actually does what you would expect:
Code:
<Button>
  <Button.Content>
    <Image Source="someImage.png"/>
  </Button.Content>
</Button>
This would still use all of the "default" skins for button states (so the mouse-over, pushed state are defined by the OS - but the Content area of the button is drawn using an Image object.

To create a totally custom button with custom states and behaviors, you would define a template for the button and assign it to the Button object like so:

Code:
<ControlTemplate x:Name="MyCustomButton">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
    <Image Source="image.png" Grid.Column="0"/>
    <ContentPresenter Grid.Column="1"/>
   </Grid>
</ControTemplage>
Basically, this is a Button which displays two items side by side. One item is hardwired Image - and the other is a special display type which basically is mapped to the content property of the button.

The weird "*" width stuff specifies that the second column is always 2x the size of the first column - based on the size of the grid.

and to use this template in a button would look something like:

<Button Template="{DynamicResource MyCustomButton}" Content="I am a Combo Button"/>


WPF also has a concept of containers - very similar to Cocoa's NSView. In WPF these are called Panels. One of the really cool things in WPF are the different "Layout" panels that are included. Basically, there is a nice collection of Panel subclasses which handle any kind of layout situation you can dream of. The Canvas panel is the one most familiar to normal layout systems - basically allows its children to specify left and top properties in a cartesian coordinate system.

There are other more interesting panels however, which do a lot of layout work for you. Examples include:
  • Grid - Similar to HTML Table element
  • DockPanel - Allows you to specify children to be justified to top, bottom, right, left and has a neat option to auto size its last child. This was used a lot in my aperture layout.
  • StackPanel - automatically stacks its children either vertically or horizontally
  • WrapPanel - similar to StackPanel but Auto-Wraps children at boundary

Panels can contain other panels as well as controls, and elementary image and graphic objects. Most panels can contain multiple children, one exception is the Border Panel - however you can simply use a mult-child panel as the child of the border if you want it to contain multiple items. The Border panel is really powerful though, as it has stroke, fill, and "corner radius" properties which allow you to easily do nice rounded-rectangle interfaces - it was used a lot in my Aperture shell.

One of the truly powerful things about WPF Panels - which is what allows such flexible "skinning" - is that all Panels have Background and Opacity Mask properties and these can be any host of powerful brushes, such as solid color, gradient, tiled image, or even a visual "snapshot" of any other panel. So you can "paint" if you will, the background of a Panel with the output of any other panel. The layout tool (called Blend) also makes it easy to adjust things like background colors, gradients, and such.

Ok, two closing items...

I feel I am guilty of coming across pro-microshaft... believe me it is not what I was intending - though to be honest, WPF as an architecture is f*n elegant. I would highly suggest at least researching it to learn about a different way of approaching UIs - which is obviously my primary interest.

My honest intentions were to figure out how to go about learning to think like a cocoa developer when you have been trained for so long to think in a declarative, object oriented way.

Also, please do not confuse WPF with WPF/E (or Silverlight) - as these are two TOTALLY different Microsoft technologies. WPF is a desktop application framework - though the managed code part does get in its way... WPF/E or Silverlight is ms's "internet" plugin meant to compete with Flash. Even though the names are similar, and there is some overlap in some areas, they are not the same thing at all - Silverlight lacks all of the features that makes WPF cool, like panels, data binding, and Control Templates. And not to mention it is dependent on Javascript...Uhg! Woops, I think I said previously, I would not get into language debates...


I guess what I am really looking for, are really good Cocoa tutorials. If I had the time and the $$$$, I would go to Big Nerd Ranch - heck I grew up in the south and need me some vacation time, but for now I am stuck in Brooklyn, NY... looking to find the best Mac programming community I can find on the interweb...

cheers!
jturpin is offline   Reply With Quote
Old 2008.02.15, 07:06 AM   #8
hodari
Null
 
Join Date: Feb 2008
Posts: 1
Default Did you get anywhere?

Hi it was interesting to read your post... I come from windows development side as well and I am perplexed like you ... where are the components? How do I set properties?... Let me know.

Regards
hodari is offline   Reply With Quote
Old 2009.01.14, 10:36 AM   #9
adamj
Null
 
Join Date: Jan 2009
Location: Khi
Posts: 1
Default

Really helpful post, i was searching on google and found this post.

Quote:
I would really stress strongly that as a budding mac programmer you should not recreate your own interface from scratch.
Using the standard Cocoa one to learn exactly how it all works before reskinning it is important for the usability of a program.
It's also somewhat grueling work which may be best spent on non-superficial programming like making sure an app is robust and efficient.
It has been done successfully in many cases though, but again it is not usual to do this as it can be confusing and may not add much.

A couple of examples of apps source code where the interface has been reskinned:
http://lumacode.com/simon/developer.html
http://fax.twilightcoders.net/Mov2Gif/

So, I'll reiterate, I think it is be best to get very well acquainted with the working of good mac programs and the interface before considering modifying it drastically.
Hey unknown your tips are great.

many thanks
adamj is offline   Reply With Quote
Old 2009.01.14, 04:26 PM   #10
wadesworld
Moderator
 
Join Date: Mar 2005
Posts: 187
Default

Well, my 2 cents....

You're in for a long road if you continually compare how you did something on Windows and how it's done on Cocoa. Things on Cocoa will likely always seem more inscrutable, because you've got years of experience doing them on Windows.

An Aperture-style interface would best be accomplished through CoreAnimation. I haven't started using it yet, so I can't give specific guidance, but I do know it makes such interfaces pretty painless to create.

Mainly, I think you're starting off too big. Yes, I understand Currency Converter is brain-dead simple. However, until you truly understand the Cocoa way of doing things, the chances of suddenly being able to understand a complex interface are slim.

Why not take Currency Converter and add a preference dialog? Then add some more functionality to it and add a toolbar. What you add to it doesn't have to be currency-conversion related - just something for you to learn how things work. Then maybe add an image and animate it from one side of the window to the other.

Just building up a simple example like that, even though it's throw-away work, will be a huge time saver in the long run, rather than trying to sit and think..."well, in windows, I'd set this property to accomplish that - why can't I find that property in Cocoa?"
wadesworld is offline   Reply With Quote
Old 2009.12.22, 06:21 PM   #11
harm
Null
 
Join Date: Dec 2009
Location: amsterdam
Posts: 1
Default Components != Objects

I think a major distinction is in the use of Components. This concept was pioneered, I think, by Delphi. Essential is that you don't really subclass your widgets. You instantiate them at design time and add code right to each instance.
Also in the click-hierarchy there is a short-cut of sorts: the way you stack the components on each other automatically defines the click-chain.
This allows you to quickly mash together an almost working prototype pf your app -you just have to fill in the code blanks.

Cocoa takes a more pure OO road. It is very normal to subclass a view and than put your custom code in the 'draw' method.
Also, the code is only loosely coupled with interface objects. You cannot see the code executed when clicked on a button. only what message will be send to what receiver.

I remember a thought experiment: suppose you want to make a special class of button that always makes a beep sound when clicked before executing the code. The outcome was that in Delphi/VB this was more tedious because it involved making a custom component. In Interface Builder you just make a subclass of the standard button, add the beep, calls super and you set the class of the widget to this subclass.
The components concept has led to an aftermarket of commercially available components. A similar 3rd party market for Cocoa objects has never really established, I think because these widgets are more difficult to include in a drag-and-drop way into your project.


If you want to develop for Cocoa with the convenience of the components-model, I think you may want to look at Lazarus. en.wikipedia.org/wiki/Lazarus_(software) . But then, you are not really developing the Cocoa way.

The Cocoa way is more bare bones in a sense, has a steeper learning curve
and involves more coding in the oo style. It's a different paradigm that gives you more flexibility, but at a price.

Disclaimer: I didn't code for delphi nor osx (actually next) for quite some years;
Note: the .xib (interface) files are now xml, you can edit the source;

Harm
harm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:24 AM.