/* A simple Cappuccino application to demonstrate the a couple of problems I've run into Issue (1): CPMenu and CPUndoManager If my custom menu bar is present the undo/redo operatio fails with error: "rsh is null" Remove the menu bar and the undo/redo works as expected. Issue (2): windowShouldClose method Upon closing the window it seems the windowShouldClose is called twice Author: Zoltan Klinger */ import @implementation AppController : CPObject { CPWindow theWindow; } - (void)applicationDidFinishLaunching:(CPNotification)aNotification { theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask] [theWindow setBackgroundColor: [CPColor blackColor]]; var contentView = [theWindow contentView]; mainMenu = [[CPMenu alloc] initWithTitle:"AAA"]; [mainMenu setTitle: "BBB"]; var newItem = [mainMenu addItemWithTitle:"New" action:@selector(newDocument) keyEquivalent:nil]; [[CPApplication sharedApplication] setMainMenu:mainMenu]; [theWindow orderFront:self]; [CPMenu setMenuBarVisible:YES]; // Prompt user for a new document [self newDocument]; CPLogRegister(CPLogConsole); } - (void) newDocument() { var windows = [[CPApplication sharedApplication] windows]; var windowCount = [ windows count] -2; var name = prompt("Please enter a title for a new Document", "Document " + windowCount); if (null != name) { // Create a new window var drawing = [[DrawingWindow alloc] initWithContentRect: CGRectMake(0, 0, 400, 300)]; [drawing setFrameOrigin:(CPPointMake(20*windowCount, 30+20*windowCount))]; [drawing setTitle: name]; [drawing orderFront:self]; } } @end /* ========================== DrawingWindow ================== */ @implementation DrawingWindow : CPPanel { CPWindow bridgeWindow; PlanView plan; ScaleView scaleView; } - (id)initWithContentRect:(CGRect)aRect { self = [super initWithContentRect:aRect styleMask:CPHUDBackgroundWindowMask|CPClosableWindowMask| CPResizableWindowMask]; if (self) { [self setAcceptsMouseMovedEvents:YES]; [self setFloatingPanel:YES]; [self setDelegate:self]; var contentView = [self contentView]; var bounds = [contentView bounds]; plan = [[PlanView alloc] initWithFrame: CGRectMake(10, 26+10, bounds.size.width - 2*10, bounds.size.height - 26 - 2*10)]; [contentView addSubview: plan]; } return self; } -(BOOL) windowShouldClose:(id)window { return confirm("Close window '" + [window title] + "'?"); } @end /* ========================== PlanView ======================== */ @implementation PlanView : CPView { CPButton btnAdd; CPButton btnSubstract; CPTextField lblResult; CPNumber currentValue; } - (id)initWithFrame:(CGRect)aFrame { self = [super initWithFrame:aFrame]; if (self) { self._DOMElement.style.cursor = 'crosshair'; [self setBackgroundColor: [CPColor blueColor]]; [self setBoundsOrigin: CGPointMake(0,0)]; currentValue = 0; btnAdd = [[CPButton alloc] initWithFrame:CGRectMake(100,100,50, 18)]; [btnAdd setTitle:"Add"]; [btnAdd setTarget:self]; [btnAdd setAction:@selector(doAdd)]; [self addSubview:btnAdd]; btnSubstract = [[CPButton alloc] initWithFrame:CGRectMake(200,100,50, 18)]; [btnSubstract setTitle:"Substract"]; [btnSubstract setTarget:self]; [btnSubstract setAction:@selector(doSubstract)]; [self addSubview:btnSubstract]; lblResult = [[CPTextField alloc] initWithFrame:CGRectMake(150, 150, 100, 30)]; [lblResult setFont: [CPFont systemFontOfSize: 14.0]]; [lblResult setTextColor: [CPColor whiteColor]]; [lblResult setStringValue: "0"]; [self addSubview: lblResult]; } return self; } - (void) doAdd() { [self calculate: 1]; } - (void) doSubstract() { [self calculate: -1]; } - (void)calculate:(CPNumber)aNumber { // Register an undo operation var undoValue = -1 * aNumber; [[[self window] undoManager] registerUndoWithTarget:self selector:@selector(calculate:) object:undoValue]; currentValue += aNumber; [lblResult setStringValue: currentValue]; } @end