/* -*- Objc -*- */ /* * $Id: PlayerName.m,v 1.1 2003/08/07 08:28:03 zaral Exp $ * * Copyright (C) 2003 Free Software Foundation, Inc. * * This file is part of GNU Hégémonie. * * 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. */ #include #include #include #include #include #include "PlayerName.h" @interface PlayerNameController : NSObject { @private UiTextField *_playerName; } - (id) initWithPlayerName: (UiTextField *)playerName; /* FIXME - method names */ - (BOOL) leftMouse: (NSEvent *)event; - (BOOL) key: (NSEvent *)event; @end @implementation PlayerNameController : NSObject - (id) initWithPlayerName: (UiTextField *)playerName { NSParameterAssert (playerName); self = [super init]; if (self != nil) { _playerName = RETAIN(playerName); } return self; } - (void) dealloc { RELEASE(_playerName); [super dealloc]; } - (BOOL) leftMouse: (NSEvent *)event { /* FIXME - give the focus to the textField and put the cursor at a position */ return YES; } - (BOOL) key: (NSEvent *)event { NSParameterAssert (event); unsigned short keyCode = [event keyCode]; unsigned position; NSMutableString *str = [NSMutableString string]; position = [_playerName cursor]; str = [_playerName text]; switch (keyCode) { case SDLK_RETURN: break; case SDLK_FIRST: [_playerName setCursor: 0]; break; case SDLK_BACKSPACE: if (position > [str length]) { position--; [str deleteCharactersInRange: NSMakeRange(position, position)]; [_playerName setCursor: position]; } break; case SDLK_RIGHT: if (position < [str length]) { position++; [_playerName setCursor: position]; } break; case SDLK_LEFT: if (position > 0) { position--; [_playerName setCursor: position]; } break; case SDLK_INSERT: // [[_playerName text] insertString: [event characters] // atIndex: [_playerName cursor]]; break; case SDLK_DELETE: if (position < [str length]) { [str deleteCharactersInRange: NSMakeRange(position, position)]; [_playerName setText: str]; [_playerName sizeToFit]; } break; case SDLK_END: [_playerName setCursor: [str length]]; break; default: if ( (keyCode >= SDLK_0 && keyCode <= SDLK_9) || (keyCode >= SDLK_a && keyCode <= SDLK_z) || (keyCode == SDLK_SPACE) ) { [str insertString: [event characters] atIndex: [_playerName cursor]]; [_playerName setText: str]; [_playerName setCursor: [_playerName cursor] + 1]; [_playerName sizeToFit]; } } return YES; } @end @implementation PlayerName - (id) initWithText: (NSMutableString *)text position: (NSPoint)position height: (unsigned)height { NSParameterAssert (text); NSParameterAssert (position.x >= 0); NSParameterAssert (position.x < 1024); NSParameterAssert (position.y >= 0); NSParameterAssert (position.y < 768); NSParameterAssert (height > 0); self = [super initWithText: text position: position height: height]; if (self != nil) { PlayerNameController *_controller = [[PlayerNameController alloc] initWithPlayerName: self]; [self setDelegate: _controller]; } return self; } @end