/* * Cocoa Module for R : A Computer Language for Statistical Data Analysis * Copyright (C) 2004 R Development Core Team * Authors: Simon Urbanek and Stefano Iacus * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #import "Controller.h" #import "TextController.h" static Controller *sharedController; @implementation Controller /** there is always only one shared controller associated with this bundle */ + (Controller *)sharedController { return sharedController; } /** initialization of the bundle controller - this is called in initializeBundle. Note that we assume the existence of the Carbon initialization! */ - (id)init { self = [super init]; sharedController = self; NSApplicationLoad(); if (![NSBundle loadNibNamed:@"MyWindow" owner:self]) { NSLog(@"failed to load bundle's nib"); } return self; } /** set the associated text controller */ - (void) setTextController: (TextController*) c { txtCtrl = c; } /** retrieve the associated text controller */ - (TextController*) textController { if (!txtCtrl) NSLog(@"Warning! [Controller textController] is nil! A crash is likely to follow!"); return txtCtrl; } /** set the callback function of userInput; we call this function when we want to pass commands to R */ - (void)setUserInputFn:(userInputType) fn { userInputFn = fn; } /** show the associated main window and make it foremost */ - (void)showWindow { [window makeKeyAndOrderFront:nil]; } /** send the text as input to the R process */ - (void) userInput: (NSString*) text { if (userInputFn) userInputFn([text cString]); } @end /*===================================================================== bundle entry functions - those functions are loaded and used by R */ /** initializes the bundle by creating the main shared constroller. returns the feature bitmask of this bundle */ int initializeBundle(userInputType uiFn) { Controller *controller; NSAutoreleasePool *localPool; localPool = [[NSAutoreleasePool alloc] init]; controller = [[Controller alloc] init]; [controller setUserInputFn: uiFn]; [localPool release]; return 3; /* combination of the following possible flags: 1=cocoa_basic; 2=cocoa_loop; 4=cocoa_menu */ } /** calls showWindow of the main controller */ int selectWindow(int wid) { NSAutoreleasePool *localPool; localPool = [[NSAutoreleasePool alloc] init]; [[Controller sharedController] showWindow]; /* currently we have only one window so we ignore wid */ [localPool release]; return 0; } /** writes string (R output) to the console */ int writeConsole(CFStringRef message) { NSAutoreleasePool *localPool; localPool = [[NSAutoreleasePool alloc] init]; [[[Controller sharedController] textController] writeConsole:(NSString *)message]; [localPool release]; return 0; } /** writes echoed user input to the console */ int writeUserInput(CFStringRef message) { NSAutoreleasePool *localPool; localPool = [[NSAutoreleasePool alloc] init]; [[[Controller sharedController] textController] writeUserInput:(NSString *)message]; [localPool release]; return 0; } /** writes prompt to the console */ int writePrompt(CFStringRef message) { NSAutoreleasePool *localPool; localPool = [[NSAutoreleasePool alloc] init]; [[[Controller sharedController] textController] writePrompt:(NSString *)message]; [localPool release]; return 0; } /** indicates whether R is busy or idle */ int RisBusy(int i) { NSAutoreleasePool *localPool; localPool = [[NSAutoreleasePool alloc] init]; [[[Controller sharedController] textController] RisBusy:i]; [localPool release]; return 0; } int processEvents(int lnr) { [[[Controller sharedController] textController] runLoop]; return 0; } /* optional, unimplemented functions: int setupMenu(int wnr); */