Categories: Symbian C++ | UI | S60 | Code Examples
This page was last modified 12:21, 31 March 2008.
CS000868 - Custom control: Focusing
From Forum Nokia Wiki
| ID | CS000868 | Creation date | March 28, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N95 |
| Category | Symbian C++ | Subcategory | UI |
| Keywords (APIs, classes, methods, functions): CCoeControl, TKeyEvent |
Overview
This code snippet shows how to add key event handling to the custom container control and how change focus of the selected component.
This example extends the existing code snippet CS000861 - Custom control: Container control. Check the See also section of this article for other snippets of the Custom Control series.
CMyContainerControl - Header
Add three methods to the CMyContainerControl component header for receiving key events and changing focus.
private: TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType); public: void MoveFocusUp(); void MoveFocusDown();
CMultiViewsView1 - Source
CMyContainerControl must be added to the control stack for receiving key events AppUi()->AddToStackL().
// Create Compound control iContainerControl = CMyContainerControl::NewL(ClientRect()); // Create control to the compound container CMyControl* control1 = CMyControl::NewLC(ClientRect(),iContainerControl); iContainerControl->AddControlL(control1,1); CleanupStack::Pop(); //control 1 // Add the control to the control stack AppUi()->AddToStackL(iContainerControl);
CMyContainerControl - Source
OfferKeyEventL() catches key events EKeyUpArrow and EKeyDownArrow for changing focus of the selected component. MoveFocusUp() and MoveFocusDown() changes the active component focus to another one with CCoeContainer::SetFocus().
TKeyResponse CMyContainerControl::OfferKeyEventL (const TKeyEvent& aKeyEvent,TEventCode /*aType*/) { // Move focus switch ( aKeyEvent.iCode ) { case EKeyUpArrow: { MoveFocusUp(); DrawNow(); // Draw components again return EKeyWasConsumed; } case EKeyDownArrow: { MoveFocusDown(); DrawNow(); // Draw components again return EKeyWasConsumed; } default: { return EKeyWasNotConsumed; } } } void CMyContainerControl::MoveFocusUp() { CCoeControlArray::TCursor cursor = Components().Begin(); CCoeControl* ctrl = NULL; CCoeControl* prevCtrl = NULL; while ((ctrl = cursor.Control<CCoeControl>()) != NULL) { if (ctrl->IsFocused()) { if (prevCtrl) { // Set focus to previous control ctrl->SetFocus(EFalse); prevCtrl->SetFocus(ETrue); break; } else { break; // First control is already focused } } prevCtrl = ctrl; cursor.Next(); } } void CMyContainerControl::MoveFocusDown() { CCoeControlArray::TCursor cursor = Components().Begin(); CCoeControl* ctrl = NULL; CCoeControl* nextCtrl = NULL; while ((ctrl = cursor.Control<CCoeControl>()) != NULL) { if (ctrl->IsFocused()) { cursor.Next(); nextCtrl = cursor.Control<CCoeControl>(); if (nextCtrl) { // Set focus to next control ctrl->SetFocus(EFalse); nextCtrl->SetFocus(ETrue); break; } else { break; // Last control is already focused } } cursor.Next(); } }
Drawing focus
Focus rectangle is drawn in the custom control CMyControl::Draw() method which calls CMyControl::DrawFocusFrame() that checks component focus. If the component has focus, it is drawn with CWindowGc::DrawRoundRect().
Postconditions
CMyContainerControl receives key events and shows active component focus.
See also
Custom Control Series:
- CS000859 - Custom control How to define custom control
- CS000860 - Custom control: Construct from resource Creating a control from a resource
- CS000861 - Custom control: Container control Creating a container control
- CS000869 - Custom control: Scrollbars Adding scroll bar to custom control
- CS000870 - Custom control: In dialog Adding a custom control into CAknDialog
- Image:CustomControl.zip Example code patch
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Settings list in a dialog | mark_williams | Symbian User Interface | 5 | 2006-05-16 10:01 |
| Displaying Unicode characters | jp4symbian | Symbian User Interface | 35 | 2007-05-18 06:48 |
| How to use my image as my app skin? | olivier_randria | Symbian User Interface | 9 | 2007-04-10 06:55 |
| Best way to market s60 software | chancp1969 | General Discussion | 4 | 2007-11-25 20:40 |
| how to create a dialog with a grid | handle_cn | General Symbian C++ | 4 | 2006-02-21 18:39 |

