diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-08-22 13:26:08 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-08-22 13:26:08 +0000 |
commit | 54ed811d6071cc843a9d2699d3ca972af9ca4c5d (patch) | |
tree | 345fa444b320f56af35ab4a0f734ec1dd8e53eef /Source/SPEncodingPopupAccessory.m | |
parent | a35bbeac66279cd42230e301332dee1c26ae4be2 (diff) | |
download | sequelpro-54ed811d6071cc843a9d2699d3ca972af9ca4c5d.tar.gz sequelpro-54ed811d6071cc843a9d2699d3ca972af9ca4c5d.tar.bz2 sequelpro-54ed811d6071cc843a9d2699d3ca972af9ca4c5d.zip |
• outsourced SPEncodingPopupAccessory
- added SPEncodingPopupAccessory class and nib
- it returns an accessory view containing a popup menu with predefined encodings
- removed that view from DBView
Diffstat (limited to 'Source/SPEncodingPopupAccessory.m')
-rw-r--r-- | Source/SPEncodingPopupAccessory.m | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/Source/SPEncodingPopupAccessory.m b/Source/SPEncodingPopupAccessory.m new file mode 100644 index 00000000..c5b30630 --- /dev/null +++ b/Source/SPEncodingPopupAccessory.m @@ -0,0 +1,118 @@ +// +// $Id$ +// +// SPEncodingPopupAccessory.m +// sequel-pro +// +// Created by Hans-Jörg Bibiko on August 22, 2009 +// +// 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 +// +// More info at <http://code.google.com/p/sequel-pro/> + +#import "SPEncodingPopupAccessory.h" + + +@implementation SPEncodingPopupAccessory + ++ (NSView *)encodingAccessory:(NSUInteger)encoding includeDefaultEntry:(BOOL)includeDefaultItem encodingPopUp:(NSPopUpButton **)popup { + SPEncodingPopupAccessory *owner = [[[SPEncodingPopupAccessory alloc] init] autorelease]; + // Rather than caching, load the accessory view everytime, as it might appear in multiple panels simultaneously. + if (![NSBundle loadNibNamed:@"EncodingPopupView" owner:owner]) { + NSLog(@"Failed to load EncodingPopupView.nib"); + return nil; + } + if (popup) *popup = owner->encodingPopUp; + [[self class] setupPopUp:owner->encodingPopUp selectedEncoding:encoding withDefaultEntry:includeDefaultItem]; + return [owner->encodingAccessoryView autorelease]; +} + +/** + * Returns the actual enabled list of encodings for open/save SQL files. + */ ++ (NSArray *)enabledEncodings +{ + static const NSInteger plainTextFileStringEncodingsSupported[] = { + kCFStringEncodingUTF8, + kCFStringEncodingUTF16, + kCFStringEncodingUTF16BE, + kCFStringEncodingUTF16LE, + kCFStringEncodingUTF32, + kCFStringEncodingWindowsLatin1, + kCFStringEncodingISOLatin1, + kCFStringEncodingWindowsLatin2, + kCFStringEncodingISOLatin2, + kCFStringEncodingISOLatin3, + kCFStringEncodingISOLatin4, + kCFStringEncodingWindowsLatin5, + kCFStringEncodingKOI8_R, + kCFStringEncodingKOI8_U, + kCFStringEncodingMacRoman, + kCFStringEncodingMacJapanese, + kCFStringEncodingShiftJIS, + kCFStringEncodingEUC_JP, + kCFStringEncodingISO_2022_JP, + kCFStringEncodingMacChineseTrad, + kCFStringEncodingMacChineseSimp, + kCFStringEncodingBig5, + kCFStringEncodingGB_18030_2000, + kCFStringEncodingEUC_CN, + kCFStringEncodingEUC_TW, + kCFStringEncodingMacKorean, + kCFStringEncodingEUC_KR, + -1 + }; + NSStringEncoding encoding; + NSInteger cnt = 0; + NSMutableArray *encs = [NSMutableArray array]; + while (plainTextFileStringEncodingsSupported[cnt] != -1) + if ((encoding = CFStringConvertEncodingToNSStringEncoding(plainTextFileStringEncodingsSupported[cnt++])) != kCFStringEncodingInvalidId) + [encs addObject:[NSNumber numberWithUnsignedInteger:encoding]]; + + return encs; +} + +/** + * This method initializes the provided popup with list of encodings; + * it also sets up the selected encoding as indicated and if includeDefaultItem is YES. + * Otherwise the tags are set to the NSStringEncoding value for the encoding. + */ ++ (void)setupPopUp:(NSPopUpButton *)popup selectedEncoding:(NSUInteger)selectedEncoding withDefaultEntry:(BOOL)includeDefaultItem +{ + NSArray *encs = [self enabledEncodings]; + NSUInteger cnt, numEncodings, itemToSelect = 0; + + // Put the encodings in the popup + [popup removeAllItems]; + + // Make sure the initial selected encoding appears in the list + if (!includeDefaultItem && (selectedEncoding != NoStringEncoding) && ![encs containsObject:[NSNumber numberWithUnsignedInteger:selectedEncoding]]) encs = [encs arrayByAddingObject:[NSNumber numberWithUnsignedInteger:selectedEncoding]]; + + numEncodings = [encs count]; + + // Fill with encodings + for (cnt = 0; cnt < numEncodings; cnt++) { + NSStringEncoding enc = [[encs objectAtIndex:cnt] unsignedIntegerValue]; + [popup addItemWithTitle:[NSString localizedNameOfStringEncoding:enc]]; + [[popup lastItem] setTag:enc]; + [[popup lastItem] setEnabled:YES]; + if (enc == selectedEncoding) itemToSelect = [popup numberOfItems] - 1; + } + + [popup selectItemAtIndex:itemToSelect]; +} + + +@end |