Thursday, November 12, 2009

Dropdown box popup menu

A button can be customized to show popup menu. The dropdown button code I have taken it from Apples example code for ButtonMadness.

///DropDownButton.h


#import "Cocoa/Cocoa.h"


@interface DropDownButton : NSButton
{
NSPopUpButtonCell* popUpCell;
}

- (void)setUsesMenu:(BOOL)flag;
- (BOOL)usesMenu;

@end

///DropDownButton.m
#import "DropDownButton.h"

@implementation DropDownButton

// -------------------------------------------------------------------------------
// awakeFromNib:
// -------------------------------------------------------------------------------
- (void)awakeFromNib
{
//NSLog(@"DropDownButton awakeFromNib");
if ([self menu] != nil)
{
[self setUsesMenu:YES];
}
}

// -------------------------------------------------------------------------------
// dealloc:
// -------------------------------------------------------------------------------
- (void)dealloc
{
[popUpCell release];
[super dealloc];
}

// -------------------------------------------------------------------------------
// setUsesMenu:flag
// -------------------------------------------------------------------------------
- (void)setUsesMenu:(BOOL)flag
{
if (popUpCell == nil && flag)
{
popUpCell = [[NSPopUpButtonCell alloc] initTextCell:@""];
[popUpCell setPullsDown:YES];
[popUpCell setPreferredEdge:NSMaxYEdge];
}
else if (popUpCell != nil && !flag)
{
[popUpCell release];
popUpCell = nil;
}
}

// -------------------------------------------------------------------------------
// usesMenu:
// -------------------------------------------------------------------------------
- (BOOL)usesMenu
{
return (popUpCell != nil);
}

// -------------------------------------------------------------------------------
// runPopUp:theEvent
// -------------------------------------------------------------------------------
- (void)runPopUp:(NSEvent*)theEvent
{
//NSLog(@"Button runPopUp");

// create the menu the popup will use
NSMenu* popUpMenu = [[self menu] copy];
[popUpMenu insertItemWithTitle:@"" action:NULL keyEquivalent:@"" atIndex:0]; // blank item at top
[popUpCell setMenu:popUpMenu];

// and show it
[popUpCell performClickWithFrame:[self bounds] inView:self];

[popUpMenu release];

[self setNeedsDisplay: YES];
}

// -------------------------------------------------------------------------------
// mouseDown:theEvent
// -------------------------------------------------------------------------------
- (void)mouseDown:(NSEvent*)theEvent
{
//NSLog(@"Button mouseDown");
if ([self usesMenu])
{
[self runPopUp:theEvent];
}
else
{
[super mouseDown:theEvent];
}
}

////DropDownButtonController.h
@class DropDownButton;
@interface DropDownButtonController : NSObject
{
IBOutlet DropDownButton *mDropDownButton;

}

- (IBAction)menuItem1Handler:(id)sender;
- (IBAction)menuItem2Handler:(id)sender;
@end

////DropDownButtonController.m
#import "DropDownButton.h"

// -------------------------------------------------------------------------------
// awakeFromNib:
// -------------------------------------------------------------------------------

- (void)awakeFromNib
{
NSMenu *dropDownMenu = [[NSMenu alloc] init];
NSInteger index =0;
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"MenuItem1", nil)
action:@selector(menuItem1Handler:)
keyEquivalent:@""];
[dropDownMenu insertItem:menuItem atIndex:index++];
[menuItem release];

menuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"MenuItem2", nil)
action:@selector(menuItem2Handler:)
keyEquivalent:@""];
[dropDownMenu insertItem:menuItem atIndex:index++];
[menuItem release];

[mDropDownButton setMenu:[[dropDownMenu copy] autorelease]];
[dropDownMenu release];

[mDropDownButton setUsesMenu:YES];
}

// -------------------------------------------------------------------------------
//
menuItem1Handler
// -------------------------------------------------------------------------------

- (IBAction)menuItem1Handler:(id)sender
{
//Here you can get the menuitem properties since it is nothing but a button
NSLog(@"MenuText:[%@]", [sender title]);
}

// -------------------------------------------------------------------------------
//
menuItem2Handler
// -------------------------------------------------------------------------------

- (IBAction)menuItem2Handler:(id)sender
{
}

Thursday, March 6, 2008

Charger connected state

How to find whether charger connected or not?

Here is the sample code:

//System include
#include < etelthirdparty.h >

//*** CBatteryInfo *************/

class CBatteryInfo : public CActive
{
private:
CTelephony* iTelephony;
CTelephony::TIndicatorV1 iIndicatorV1;
CTelephony::TIndicatorV1Pckg iIndicatorV1Pckg;
public:
CBatteryInfo();
void GetIndicator();

private:
/*
These are the pure virtual methods from CActive that
MUST be implemented by all active objects
*/
void RunL();
void DoCancel();
};

//************************************************
CBatteryInfo* CBatteryInfo::NewL()
{
CBatteryInfo* self = new (ELeave) CBatteryInfo;
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}

void CBatteryInfo::ConstructL()
{
iTelephony = CTelephony::NewL();
}

CBatteryInfo::CBatteryInfo()
: CActive(EPriorityStandard),
iIndicatorV1Pckg(iIndicatorV1)
{
//default constructor
CActiveScheduler::Add(this);
}

void CBatteryInfo::GetIndicator()
{
iTelephony->GetIndicator(iStatus, iIndicatorV1Pckg);
SetActive();
}

void CBatteryInfo::RunL()
{
if(iStatus==KErrNone)
{
if(iIndicatorV1.iCapabilities & CTelephony::KIndChargerConnected)
{
// We can detect when a charger is connected
if(iIndicatorV1.iIndicator & CTelephony::KIndChargerConnected)
{
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
// Show the information Note
note->ExecuteLD(_L("Charger Connected...!"));
} // Charger is connected
else
{
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
// Show the information Note
note->ExecuteLD(_L("XXX: Charger Not Connected...!"));
} // Charger is not connected
}
else
{} // We do not know whether a charger is connected
}
TRequestStatus* status = &iStatus;
User::RequestComplete(status, KErrNone);
}

void CBatteryInfo::DoCancel()
{
iTelephony->CancelAsync(CTelephony::EGetIndicatorCancel);
}

//*************caller code

iBatteryInfo = CBatteryInfo::NewL();
iBatteryInfo->GetIndicator();
CActiveScheduler::Start();
delete iBatteryInfo;
iBatteryInfo = NULL;