Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/TableViewsController.mm

1//
2// TableViewsController.mm
3// ChameleonPrefPane
4//
5// Created by Rekursor on 1/29/10.
6//
7
8#import "ChameleonPrefPane.h"
9#import "ChameleonPropertyList.h"
10#import "PreferencesControllerBase.H"
11#import "TableViewsController.h"
12#import "BootSetupController.h"
13#import "ShellProcess.h"
14
15#import <SecurityFoundation/SFAuthorization.h>
16#import <SecurityInterface/SFAuthorizationView.h>
17#include <string>
18#include <vector>
19
20//--------------------------------------------------------------------------
21// MenuEntry definitions
22//--------------------------------------------------------------------------
23typedef enum {
24PaneBootFrom, PaneBootSetup, PaneBootFlags, PanePeripherals,
25PaneAdvanced, PaneEfiInject, PaneSmbios, PaneAbout
26} MenuEntryPane;
27
28struct MenuEntry
29{
30NSString* title;
31MenuEntryPane pane;
32NSImage**img;
33};
34
35
36static NSImage*mImgPaneBootFrom;
37static NSImage*mImgPaneBootSetup;
38static NSImage*mImgPaneBootFlags;
39static NSImage*mImgPanePeripherals;
40static NSImage*mImgPaneAdvanced;
41static NSImage*mImgPaneEfiInject;
42static NSImage*mImgPaneSmbios;
43static NSImage*mImgPaneAbout;
44
45
46static const MenuEntry sMenuList[] =
47{
48{@"Boot_From", PaneBootFrom, &mImgPaneBootFrom},
49{@"Boot_Setup",PaneBootSetup, &mImgPaneBootSetup},
50{@"Boot_Flags", PaneBootFlags, &mImgPaneBootFlags},
51{@"Peripherals", PanePeripherals, &mImgPanePeripherals},
52{@"Efi_Inject", PaneEfiInject, &mImgPaneEfiInject},
53{@"Bios", PaneSmbios, &mImgPaneSmbios},
54{@"Advanced", PaneAdvanced, &mImgPaneAdvanced},
55{@"About", PaneAbout, &mImgPaneAbout}
56};
57const int iMenuListSize = sizeof(sMenuList)/ sizeof(MenuEntry);
58
59static TableViewsController* _instance; // Singleton DP def.
60
61
62//--------------------------------------------------------------------------
63@implementation CenteredTextFieldCell
64//--------------------------------------------------------------------------
65- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
66{
67 NSSize contentSize = [self cellSize];
68 cellFrame.origin.y += (cellFrame.size.height - contentSize.height) / 2.0;
69 cellFrame.size.height = contentSize.height;
70
71NSAttributedString *drawString = [self attributedStringValue];
72
73NSRange range;
74NSDictionary *attributes = [drawString attributesAtIndex:0 effectiveRange:&range];
75
76float maxWidth = cellFrame.size.width;
77float stringWidth = [drawString size].width;
78
79if (maxWidth < stringWidth)
80{
81int i;
82
83for (i = 0;i <= [drawString length];i++)
84{
85if ([[drawString attributedSubstringFromRange:NSMakeRange(0,i)]size].width >= maxWidth)
86{
87drawString = [[NSMutableAttributedString alloc] initWithString:
88 [[[drawString attributedSubstringFromRange:NSMakeRange(0,i-3)]string] stringByAppendingString:@"..."]attributes:attributes];
89[drawString autorelease];
90}
91}
92}
93
94[drawString drawInRect:cellFrame];
95
96}
97
98@end
99
100//--------------------------------------------------------------------------
101@implementation TableViewsController
102//--------------------------------------------------------------------------
103- (id) init
104{
105[super init];
106_instance = self;
107
108currentRowSel = -1;
109// set the image to display the current file being played
110mMacOSXImage = [self getImageResource: @"MacOSX" ofType: @"png"];
111mWindowsImage = [self getImageResource: @"Windows" ofType: @"png"];
112mLinuxImage = [self getImageResource: @"Linux" ofType: @"png"];
113mCDROMImage = [self getImageResource: @"CDROM" ofType: @"png"];
114mUnknownImage = [self getImageResource: @"Chameleon" ofType: @"tiff"];
115
116mImgPaneBootFrom = [self getImageResource:@"disk" ofType:@"png"];
117mImgPaneBootSetup = [self getImageResource:@"wrench" ofType:@"png"];
118mImgPaneBootFlags = [self getImageResource:@"flag" ofType:@"png"];
119mImgPanePeripherals = [self getImageResource:@"plug" ofType:@"png"];
120mImgPaneAdvanced = [self getImageResource:@"advanced" ofType:@"png"];
121mImgPaneEfiInject = [self getImageResource:@"syringe" ofType:@"png"];
122mImgPaneSmbios = [self getImageResource:@"chip" ofType:@"png"];
123mImgPaneAbout = [self getImageResource:@"chamsmall" ofType:@"png"];
124
125return self;
126}
127
128//--------------------------------------------------------------------------
129// Singleton impl
130+ (TableViewsController*) instance
131{
132return _instance;
133}
134//--------------------------------------------------------------------------
135+ (bool) isUnlocked
136{
137return [[ChameleonPrefPane instance] isUnlocked];
138}
139
140//--------------------------------------------------------------------------
141+ (AuthorizationRef) authorization
142{
143return [TableViewsController isUnlocked] ?
144[ [ [ChameleonPrefPane instance]->authView authorization] authorizationRef] : NULL;
145}
146//--------------------------------------------------------------------------
147- (NSTableView*) partitionsTable
148{
149return mPartitionsTable;
150}
151//--------------------------------------------------------------------------
152- (id) getImageResource: (NSString *) str ofType: (NSString*) sType
153{
154NSImage * img=nil;
155if(!str) return nil;
156NSBundle * b = [NSBundle bundleForClass:[self class]];
157NSString* sRes = [b pathForResource: str ofType:sType ];
158img = [[NSImage alloc] initWithContentsOfFile: sRes];
159return img;
160}
161
162//--------------------------------------------------------------------------
163- (void)tableViewSelectionDidChange:(NSNotification *)notification {
164
165NSTableView* tv= [notification object];
166if (tv == mPartitionsTable)
167{
168if ([tv selectedRow] != currentRowSel)
169{
170currentRowSel = [tv selectedRow];
171if (currentRowSel>=0)
172{
173const std::vector<PartitionInfo>& partInfo = PartitionExtractor::instance().partList();
174char hd[7+1]="hd(n,m)";
175hd[3]= ('0'+partInfo[currentRowSel].disk());
176hd[5]= ('0'+partInfo[currentRowSel].partition());
177AuthorizationRef auth= [TableViewsController authorization];
178if(BootProp::instance().setStringForKey(kDefaultPartition, hd))
179BootProp::instance().save(auth);
180[ PreferencesControllerBase loadOptionsFromBootFile];
181}
182else
183{ // no line selected
184BootProp::instance().removeKeyAndValue(kDefaultPartition);
185[ PreferencesControllerBase loadOptionsFromBootFile];
186
187}
188}
189}
190else if (tv == mTabViewPanesSelect)
191{
192int selected = [tv selectedRow];
193if(selected>=0)
194{
195switch (sMenuList[selected].pane)
196{
197case PaneBootFrom:
198[mTabViewPanes selectTabViewItem: mBootFrom];
199break;
200case PaneBootSetup:
201[mTabViewPanes selectTabViewItem: mBootSetup];
202break;
203case PaneBootFlags:
204[mTabViewPanes selectTabViewItem: mBootFlags];
205break;
206case PanePeripherals:
207[mTabViewPanes selectTabViewItem: mPeripherals];
208break;
209case PaneEfiInject:
210[mTabViewPanes selectTabViewItem: mEfiInject];
211break;
212case PaneAdvanced:
213[mTabViewPanes selectTabViewItem: mAdvanced];
214break;
215case PaneSmbios:
216[mTabViewPanes selectTabViewItem: mSmbios];
217break;
218case PaneAbout:
219[mTabViewPanes selectTabViewItem: mAbout];
220break;
221default:
222break;
223}
224[mTabViewPanes setNeedsDisplay: true ];
225}
226}
227}
228
229//--------------------------------------------------------------------------
230- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView
231{
232int size=0;
233if (tableView == mPartitionsTable)
234size = PartitionExtractor::instance().partList().size();
235else
236size = iMenuListSize;
237return size;
238}
239
240//--------------------------------------------------------------------------
241- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
242{
243id ret=nil;
244
245const std::vector<PartitionInfo>& partInfo = PartitionExtractor::instance().partList();
246
247if (tableColumn == mPartitionImgColumn)
248{
249switch(partInfo[row].imageIndexFromFs())
250{
251case 0:
252ret = mMacOSXImage;
253break;
254case 1:
255ret = mWindowsImage;
256break;
257case 2:
258ret = mLinuxImage;
259break;
260default:
261ret = mUnknownImage;
262break;
263
264}
265}
266else if (tableColumn == mFileSystemColumn)
267{
268ret = [NSString stringWithFormat: @"%s", partInfo[row].cfsType() ];
269}
270else if (tableColumn == mPartitionNameColumn)
271{
272ret = [NSString stringWithFormat: @"%s", partInfo[row].clabel()];
273}
274else if (tableColumn == mPartitionIDColumn)
275{
276ret= [NSString stringWithFormat: @"hd(%d,%d)",
277 partInfo[row].disk(),
278 partInfo[row].partition()
279 ];
280}
281// menu tv handling
282else if (tableColumn == mMenuName)
283{
284NSBundle * b = [NSBundle bundleForClass:[self class]];
285ret = NSLocalizedStringFromTableInBundle(sMenuList[row].title, nil, b, @"comment");
286}
287else if (tableColumn == mMenuIcon)
288{
289ret = *sMenuList[row].img;
290}
291return ret;
292}
293//--------------------------------------------------------------------------
294
295@end
296

Archive Download this file

Revision: 106