/* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain this list of conditions and the following disclaimer. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #import "WBIPv4Control.h" #define WBIPV4CONTROL_LEFT_OFFSET 3.5 #define WBIPV4CONTROL_INTERCELL_SPACE 2 #define WBIPV4CONTROL_RIGHT_OFFSET 3 #define WBIPV4CONTROL_TOP_OFFSET 3 #define WBIPV4CONTROL_BOTTOM_OFFSET 3 int _WBTimeControlMax[WBIPV4CONTROL_CELL_COUNT]={255,255,255,255}; @implementation WBIPv4Control - (BOOL) acceptsFirstResponder { return [self isEnabled]; } - (BOOL) needsPanelToBecomeKey { return YES; } - (BOOL) becomeFirstResponder { BOOL tResult=[super becomeFirstResponder]; if (tResult==YES) { NSSelectionDirection tSelectionDirection= [_window keyViewSelectionDirection]; if (tSelectionDirection==NSSelectingNext) { [self editCell:0]; } else if (tSelectionDirection==NSSelectingPrevious) { [self editCell:WBIPV4CONTROL_CELL_COUNT-1]; } } return tResult; } + (Class)cellClass { return [NSTextFieldCell class]; } - (id)cell { return [self selectedCell]; } - (void)setEnabled:(BOOL)flag { int i; [super setEnabled:flag]; for(i=0;i>24]]; [cells[WBIPV4CONTROL_B_ID] setStringValue:[NSString stringWithFormat:@"%d",(currentAddress_ & 0xFF0000)>>16]]; [cells[WBIPV4CONTROL_C_ID] setStringValue:[NSString stringWithFormat:@"%d",(currentAddress_ & 0xFF00)>>8]]; [cells[WBIPV4CONTROL_D_ID] setStringValue:[NSString stringWithFormat:@"%d",currentAddress_ & 0xFF]]; } } - (NSString *) stringValue { return [NSString stringWithFormat:@"%d.%d.%d.%d",(currentAddress_ & 0xFF000000)>>24, (currentAddress_ & 0xFF0000)>>16, (currentAddress_ & 0xFF00)>>8, currentAddress_ & 0xFF]; } - (void) setStringValue:(NSString *) aValue { if (aValue!=nil) { NSArray * tArray; int i,tCount; tArray=[aValue componentsSeparatedByString:@"."]; tCount=[tArray count]; if (tCount==4) { currentAddress_=0; for(i=0;i=0 && inA<=_WBTimeControlMax[WBIPV4CONTROL_A_ID]) { currentAddress_=(currentAddress_ & 0x00FFFFFF) + (inA<<24); if (isUsingFieldEditor==YES && selected==WBIPV4CONTROL_A_ID) { [[self currentEditor] setString:[NSString stringWithFormat:@"%d",inA]]; [self editOff]; } else { [cells[WBIPV4CONTROL_A_ID] setStringValue:[NSString stringWithFormat:@"%d",inA]]; } } } - (unsigned char) A { return ((currentAddress_ & 0xFF000000)>>24); } - (void)setB:(int)inB { if (inB>=0 && inB<=_WBTimeControlMax[WBIPV4CONTROL_B_ID]) { currentAddress_=(currentAddress_ & 0xFF00FFFF) + (inB<<16); if (isUsingFieldEditor==YES && selected==WBIPV4CONTROL_B_ID) { [[self currentEditor] setString:[NSString stringWithFormat:@"%d",inB]]; [self editOff]; } else { [cells[WBIPV4CONTROL_B_ID] setStringValue:[NSString stringWithFormat:@"%d",inB]]; } } } - (unsigned char) B { return ((currentAddress_ & 0xFF0000)>>16); } - (void)setC:(int)inC { if (inC>=0 && inC<=_WBTimeControlMax[WBIPV4CONTROL_C_ID]) { currentAddress_=(currentAddress_ & 0xFFFF00FF) + (inC<<8); if (isUsingFieldEditor==YES && selected==WBIPV4CONTROL_C_ID) { [[self currentEditor] setString:[NSString stringWithFormat:@"%d",inC]]; [self editOff]; } else { [cells[WBIPV4CONTROL_C_ID] setStringValue:[NSString stringWithFormat:@"%d",inC]]; } } } - (unsigned char) C { return ((currentAddress_ & 0xFF00)>>8); } - (void)setD:(int)inD { if (inD>=0 && inD<=_WBTimeControlMax[WBIPV4CONTROL_D_ID]) { currentAddress_=(currentAddress_ & 0xFFFFFF00) + inD; if (isUsingFieldEditor==YES && selected==WBIPV4CONTROL_D_ID) { [[self currentEditor] setString:[NSString stringWithFormat:@"%d",inD]]; [self editOff]; } else { [cells[WBIPV4CONTROL_D_ID] setStringValue:[NSString stringWithFormat:@"%d",inD]]; } } } - (unsigned char) D { return (currentAddress_ & 0xFF); } - (int)selected { return selected; } - (void)setSelected:(int)aSelected { selected=aSelected; [self setNeedsDisplay]; } - (BOOL) isOpaque { return NO; } - (void) drawRect:(NSRect) aFrame { int i; NSRect tBounds=[self bounds]; NSRect tRect; [[NSColor whiteColor] set]; // Draw the background tRect=tBounds; tRect.origin.y=WBIPV4CONTROL_BOTTOM_OFFSET-3; tRect.size.height-=tRect.origin.y; NSRectFill(tRect); // Draw the Frame [(NSTextFieldCell *) cells[0] _drawThemeBezelWithFrame:tRect inView:self]; if ([_window isKeyWindow]) { if (isUsingFieldEditor==YES) { NSRect tRect = [self bounds]; NSBezierPath * tBezierPath; tBezierPath=[NSBezierPath bezierPathWithRect:tRect]; [NSGraphicsContext saveGraphicsState]; NSSetFocusRingStyle(NSFocusRingOnly); [tBezierPath fill]; [NSGraphicsContext restoreGraphicsState]; } } // Draw the Time Separator for(i=0;i<(WBIPV4CONTROL_CELL_COUNT-1);i++) { [pointCells[i] drawWithFrame:pointRects[i] inView:self]; } // Draw the cells for(i=0;iWBIPV4CONTROL_A_ID) { [self editCell:selected-1]; break; } [_window selectKeyViewPrecedingView: self]; if ([_window firstResponder] == _window) { [self editCell:WBIPV4CONTROL_D_ID]; } break; } } } - (void)setKeyboardFocusRingNeedsDisplayInRect:(NSRect) aFrame { [super setKeyboardFocusRingNeedsDisplayInRect:[self bounds]]; } - (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString { int i,tLength; unichar tUniChar; tLength=[replacementString length]; if (affectedCharRange.location>=3 || affectedCharRange.length>3) { return NO; } for(i=0;i'9') { return NO; } } return YES; } @end