Hand on with Appkit, Objective-J and Cappuccino

We are looking at some classes available in the Appkit.

CPWebView

CPWebView enables you to display a web page within your application. You can use methods like: goBack: goForward: print: or stopLoading: to control your embeded navigator.

-(void) addNavigationButtonsToContentView:(CPView)contentView { var backButton = [[CPButton alloc] initWithFrame:CGRectMake(100, 20, 80, 18)]; [backButton setTitle:"Back"]; [backButton setTarget:webView]; [backButton setAction:@selector(goBack:)]; [contentView addSubview:backButton]; var forwardButton = [[CPButton alloc] initWithFrame:CGRectMake(150, 20, 80, 18)]; [forwardButton setTitle:"Forward"]; [forwardButton setTarget:webView]; [forwardButton setAction:@selector(goForward:)]; [contentView addSubview:forwardButton]; var printButton = [[CPButton alloc] initWithFrame: CGRectMake(250, 20, 80, 18)]; [printButton setTitle:"Print"]; [printButton setTarget:webView]; [printButton setAction:@selector(print:)]; [contentView addSubview:printButton]; var stopButton = [[CPButton alloc] initWithFrame: CGRectMake(300, 20, 80, 18)]; [stopButton setTitle:"Stop"]; [stopButton setTarget:webView]; [stopButton setAction:@selector(stopLoading:)]; [contentView addSubview:stopButton]; } -(void) addWebViewToContentView:(CPView)contentView { webView = [[CPWebView alloc] initWithFrame: CGRectMake(10, 100, CGRectGetWidth([contentView bounds]) - 20, 200)]; [webView setAutoresizingMask: CPViewWidthSizable | CPViewMinYMargin | CPViewMaxYMargin]; // [webView setDrawsBackground:YES]; [contentView addSubview:webView]; }

CPTabView and CPTabViewItem

-(void) addTabViewToContentView:(CPView)contentView { var tabView = [[CPTabView alloc] initWithFrame: CGRectMake(10, 500, CGRectGetWidth([contentView bounds]) - 20, 200)]; [tabView setTabViewType:CPTopTabsBezelBorder]; [tabView setAutoresizingMask: CPViewWidthSizable | CPViewMinYMargin | CPViewMaxYMargin]; [contentView addSubview:tabView]; var tabViewItem1 = [[CPTabViewItem alloc] initWithIdentifier:@"tabViewItem1"]; [tabViewItem1 setLabel:@"First Tab"]; var view1 = [[CPView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)]; var button = [[CPButton alloc] initWithFrame: CGRectMake(0, 0, 40, 18)]; [button setTitle:"view1"]; [button setTarget:self]; [button setAction:@selector(showAlert:)]; [view1 addSubview:button]; [tabViewItem1 setView:view1]; var auxiliaryView1 = [[CPView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)]; var button = [[CPButton alloc] initWithFrame: CGRectMake(0, 0, 40, 18)]; [button setTitle:"auxiliaryView1"]; [button setTarget:self]; [button setAction:@selector(showAlert:)]; [auxiliaryView1 addSubview:button]; [tabViewItem1 setAuxiliaryView:auxiliaryView1]; var tabViewItem2 = [[CPTabViewItem alloc] initWithIdentifier:@"tabViewItem2"]; [tabViewItem2 setLabel:@"Second Tab"]; var view2 = [[CPView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)]; [tabViewItem2 setView:view2]; var auxiliaryView2 = [[CPView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)]; [tabViewItem2 setAuxiliaryView:auxiliaryView2]; [self addColorWellToContentView:view2]; [self addSecureTextFieldToContentView:view2]; [tabView addTabViewItem:tabViewItem1]; [tabView addTabViewItem:tabViewItem2]; [tabView selectFirstTabViewItem:self]; }

CPSegmentedControl

-(void) addSegmentedControlToContentView:(CPView)contentView { var segmentedControl = [[CPSegmentedControl alloc] initWithFrame: CGRectMake(400, 10, 200, 20)]; [contentView addSubview:segmentedControl]; [segmentedControl setSegmentCount:2]; [segmentedControl setWidth:50 forSegment:0]; [segmentedControl setLabel:@"AAA" forSegment:0]; [segmentedControl setLabel:@"BBB" forSegment:1]; var menu0 = [[CPMenu alloc] initWithTitle:@"dummy"]; [menu0 addItem:[[CPMenuItem alloc] initWithTitle:@"ZZZ" action:nil keyEquivalent:@"2"]]; var menu1 = [[CPMenu alloc] initWithTitle:@"dummy"]; [menu1 addItem:[[CPMenuItem alloc] initWithTitle:@"XXX" action:nil keyEquivalent:@"2"]]; [segmentedControl setMenu:menu0 forSegment:0]; [segmentedControl setMenu:menu1 forSegment:1]; }

CPPopUpButton

-(void) addPopUpButtonToContentView:(CPView)contentView { var button = [[CPPopUpButton alloc] initWithFrame:CGRectMake(20, 200, 200, 20) pullsDown:NO]; [button addItemWithTitle:@"AAA"]; [button addItemWithTitle:@"BBB"]; [button sizeToFit]; [contentView addSubview:button]; var button = [[CPPopUpButton alloc] initWithFrame:CGRectMake(20, 250, 200, 20) pullsDown:YES]; [button addItemWithTitle:@"Choose an action:"]; [button addItemWithTitle:@"Start Listening"]; var menuItem = [button itemAtIndex:1]; [menuItem setTarget:self]; [menuItem setAction:@selector(startListening:)]; var menuItem = [button itemAtIndex:2]; [menuItem setTarget:self]; [menuItem setAction:@selector(stopListening:)]; [button sizeToFit]; [contentView addSubview:button]; }

CPMenu and CPMenuItem

-(void) setupMenu { var menu = [[CPMenu alloc] initWithTitle:@"dummy"]; var menuItem1 = [[CPMenuItem alloc] initWithTitle:@"File" action:nil keyEquivalent:@"1"]; [menu addItem:menuItem1]; var menu11 = [[CPMenu alloc] initWithTitle:@"dummy"]; var menuItem111 = [[CPMenuItem alloc] initWithTitle:@"AAA" action:@selector(showAlert:) keyEquivalent:@"3"]; [menu11 addItem:menuItem111]; [menu11 addItem:[CPMenuItem separatorItem]]; var menuItem112 = [[CPMenuItem alloc] initWithTitle:@"BBB" action:@selector(showAlert:) keyEquivalent:@"3"]; [menu11 addItem:menuItem112]; [menuItem1 setSubmenu:menu11]; var menuItem2 = [[CPMenuItem alloc] initWithTitle:@"Edit" action:nil keyEquivalent:@"4"]; [menu addItem:menuItem2]; var menu2 = [[CPMenu alloc] initWithTitle:@"dummy"]; var menuItem3 = [[CPMenuItem alloc] initWithTitle:@"CCC" action:@selector(showAlert:) keyEquivalent:@"3"]; [menu2 addItem:menuItem3]; [menu2 addItem:[CPMenuItem separatorItem]]; var menuItem4 = [[CPMenuItem alloc] initWithTitle:@"DDD" action:nil keyEquivalent:@"2"]; [menu2 addItem:menuItem4]; [menu2 addItem:[[CPMenuItem alloc] initWithTitle:@"EEE" action:nil keyEquivalent:@"2"]]; var menu3 = [[CPMenu alloc] initWithTitle:@"Phil"]; [menu3 addItem:[[CPMenuItem alloc] initWithTitle:@"GGG" action:@selector(showAlert:) keyEquivalent:@"2"]]; [menuItem4 setSubmenu:menu3]; [menuItem2 setSubmenu:menu2]; var sharedApplication = [CPApplication sharedApplication]; [sharedApplication setMainMenu:menu]; // Show the application menu [CPMenu setMenuBarVisible:YES]; }

CPFont

label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()]; [label setStringValue:@"Hello World!"]; [label setFont:[CPFont systemFontOfSize:24.0]]; [label setFont:[CPFont boldSystemFontOfSize:24.0]]; [label setFont:[CPFont fontWithName:@"Courier" size:24.0]]; [label setFont:[CPFont boldFontWithName:@"Courier" size:24.0]];

CPFontManager

-(void)playWithFonts:(id)sender { var sharedFontManager = [CPFontManager sharedFontManager]; var availableFonts = [sharedFontManager availableFonts]; var font = nil; var enumerator = [availableFonts objectEnumerator]; while (font = [enumerator nextObject]) { CPLog.warn(font); // CPLog.warn([font cssString]); // CPLog.warn([font familyName]); } }

CPSecureTextField

-(void) addSecureTextFieldToContentView:(CPView)contentView { var secureTextField = [[CPSecureTextField alloc] initWithFrame:CGRectMake(100, 10, 40, 40)]; [secureTextField setStringValue:@"password"]; [secureTextField sizeToFit]; [contentView addSubview:secureTextField]; var password = [secureTextField stringValue]; }

CPColorWell

-(void) addColorWellToContentView:(CPView)contentView { var colorWell = [[CPColorWell alloc] initWithFrame: CGRectMake(10, 10, 40, 40)]; [colorWell setBordered:YES]; [contentView addSubview:colorWell]; }

CPUserSessionManager

-(void) startListening:(id)sender { var defaultCenter = [CPNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(handleSession:) name:CPUserSessionManagerStatusDidChangeNotification object:[CPUserSessionManager defaultManager]]; [defaultCenter addObserver:self selector:@selector(handleSession:) name:CPUserSessionManagerUserIdentifierDidChangeNotification object:[CPUserSessionManager defaultManager]]; } -(void) stopListening:(id)sender { var defaultCenter = [CPNotificationCenter defaultCenter]; [defaultCenter removeObserver:self name:CPUserSessionManagerStatusDidChangeNotification object:[CPUserSessionManager defaultManager]]; [defaultCenter removeObserver:self name:CPUserSessionManagerUserIdentifierDidChangeNotification object:[CPUserSessionManager defaultManager]]; } -(void)handleSession:(CPNotification)aNotification { var name = [aNotification name]; CPLog.trace(name); var userSessionManager = [aNotification object]; var number = [CPNumber numberWithLong:[userSessionManager status]]; CPLog.trace([number stringValue]); CPLog.trace([userSessionManager userIdentifier]); } -(void)playWithUserSessionManager:(id)sender { var defaultManager = [CPUserSessionManager defaultManager]; [defaultManager setUserIdentifier:@"Philippe"]; /* CPUserSessionUndeterminedStatus CPUserSessionLoggedInStatus CPUserSessionLoggedOutStatus */ [defaultManager setStatus:CPUserSessionLoggedInStatus]; }

CPBundle

-(void)playWithBundle:(id)sender { debugger; var mainBundle = [CPBundle mainBundle]; var path = [mainBundle bundlePath]; var resourcePath = [mainBundle resourcePath]; // cause a problem //CPLog.trace(path); CPLog.trace(resourcePath); var path = [mainBundle pathForResource:"test.jpg"]; CPLog.trace(path); var infoDictionary = [mainBundle infoDictionary]; CPLog.trace([infoDictionary objectForKey:@"CPApplicationDelegateClass"]); CPLog.trace([infoDictionary objectForKey:@"CPBundleName"]); CPLog.trace([infoDictionary objectForKey:@"CPPrincipalClass"]); }

CPColorPanel

- (void)showColorPanel:(id)sender { var sharedColorPanel = [CPColorPanel sharedColorPanel]; [CPColorPanel setPickerMode:CPSliderColorPickerMode]; [sharedColorPanel orderFront:self]; }

CPURLConnection

-(void)sendSynchronousRequest { var request = [CPURLRequest requestWithURL:"http://macbookpro.local/"]; var response; var error; debugger; // For now response and error are not used var data = [CPURLConnection sendSynchronousRequest:request returningResponse:response error:error]; if (data != nil) { var text = [data string]; console.log([data string]); CPLog.trace(text); } }

CPAlert

- (void)showAlert:(id)sender { var alert = [[CPAlert alloc] init]; [alert setMessageText:@"My new alert"]; [alert setDelegate:self]; [alert setAlertStyle:CPWarningAlertStyle]; [alert addButtonWithTitle:@"OK"] [alert addButtonWithTitle:@"Cancel"] [alert runModal]; } -(void)alertDidEnd:(CPAlert)theAlert returnCode:(int)returnCode { if (returnCode == 0) { [label setStringValue:@"OK !"]; [webView setMainFrameURL:@"http://cappuccino.org/"]; } else if (returnCode == 1) { [label setStringValue:@"Cancel !"]; // [webView setMainFrameURL:@"http://www.nice-panorama.com/"]; [self sendSynchronousRequest]; } }

Done with Appkit, next will be Appkit

Now that we have seen the objects available in Fundation, we will play with the GUI part of Cappuccino : AppKit.

If you'd like to see the complete code listing from the tutorial, you can download it all in a single file: Tutorial2.zip. The web application is available online: Tutorial2

Copyright © 2009 - Philippe Laval. Cappuccino and Objective-J are registered Trademarks of 280 North.