00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "CustomControls.h"
00056 #include <eikstart.h>
00057
00058
00060
00061
00062
00064 CMainWinControl::CMainWinControl()
00065 {
00066 }
00067
00068 CMainWinControl::~CMainWinControl()
00069 {
00070 delete iContainer;
00071 }
00072
00073
00074
00075 void CMainWinControl::ConstructL(const TRect& rect)
00076 {
00077
00078 CreateWindowL();
00079 SetRect(rect);
00080
00081
00082 iContainer = new(ELeave) CSmileyContainer;
00083 iContainer->SetContainerWindowL(*this);
00084 TRect containerRect=Rect();
00085 iContainer->ConstructL(containerRect);
00086
00087
00088 ActivateL();
00089 DrawNow();
00090 }
00091
00092
00093 TInt CMainWinControl::CountComponentControls() const
00094 {
00095 return 1;
00096 }
00097
00098 CCoeControl* CMainWinControl::ComponentControl(TInt ) const
00099 {
00100 return iContainer;
00101 }
00102
00103
00104 void CMainWinControl::Draw(const TRect& ) const
00105 {
00106 CWindowGc& gc=SystemGc();
00107 gc.SetBrushColor(KRgbWhite);
00108 gc.Clear(Rect());
00109 }
00110
00111
00112
00113
00114 TKeyResponse CMainWinControl::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
00115 {
00116 return (iContainer->OfferKeyEventL(aKeyEvent, aType));
00117 }
00118
00120
00121
00122
00124 CSmileyContainer::CSmileyContainer()
00125 {}
00126
00127
00128 CSmileyContainer::~CSmileyContainer()
00129 {
00130
00131 delete iSmiley1;
00132 delete iSmiley2;
00133 }
00134
00135
00136
00137
00138
00139 void CSmileyContainer::ConstructL(const TRect& aRect)
00140 {
00141 TBool isSmiling=ETrue;
00142
00143
00144
00145 iSmiley1 = new(ELeave) CSmiley(isSmiling);
00146 iSmiley1->SetContainerWindowL(*this);
00147
00148 isSmiling=EFalse;
00149
00150 iSmiley2 = new(ELeave) CSmiley(isSmiling);
00151 iSmiley2->SetContainerWindowL(*this);
00152
00153 iSmiley1->SetFocus(ETrue);
00154
00155
00156
00157
00158
00159
00160 iSmiley1->SetObserver(this);
00161 iSmiley2->SetObserver(this);
00162
00163
00164
00165
00166
00167 SetRect(aRect);
00168 }
00169
00170
00171
00172 void CSmileyContainer::ConstructFromResourceL(TResourceReader& aReader)
00173 {
00174
00175 TBool isSmiling=(TBool)aReader.ReadInt8();
00176
00177 TInt width=aReader.ReadInt16();
00178
00179 TSize containerSize (width, width/2);
00180
00181 iSmiley1 = new(ELeave) CSmiley(isSmiling);
00182 iSmiley1->SetContainerWindowL(*this);
00183
00184 iSmiley2 = new(ELeave) CSmiley(isSmiling);
00185 iSmiley2->SetContainerWindowL(*this);
00186
00187 iSmiley1->SetFocus(ETrue);
00188
00189 iSmiley1->SetObserver(this);
00190 iSmiley2->SetObserver(this);
00191
00192 SetSize(containerSize);
00193
00194 ActivateL();
00195 }
00196
00197
00198 TInt CSmileyContainer::CountComponentControls() const
00199 {
00200 return 2;
00201 }
00202
00203 CCoeControl* CSmileyContainer::ComponentControl(TInt aIndex) const
00204 {
00205 if (aIndex==0)
00206 return iSmiley1;
00207 else
00208 return iSmiley2;
00209 }
00210
00211
00212
00213
00214 void CSmileyContainer::SizeChanged()
00215 {
00216 TInt containerWidth=Size().iWidth;
00217 TInt containerHeight=Size().iHeight;
00218
00219 TInt length=containerHeight>containerWidth ? containerWidth/4 : containerHeight/4;
00220 TSize smileySize(length,length);
00221
00222
00223
00224 TInt xOffset=smileySize.iWidth/4;
00225 TInt yOffset=(containerHeight - smileySize.iHeight) / 2;
00226 iSmiley1->SetPosition(Position() +
00227 TPoint(containerWidth/2 - smileySize.iWidth - xOffset, yOffset));
00228 iSmiley2->SetPosition(Position() +
00229 TPoint(containerWidth/2 + xOffset, yOffset));
00230
00231 iSmiley1->SetSize(smileySize);
00232 iSmiley2->SetSize(smileySize);
00233 }
00234
00235 void CSmileyContainer::Draw(const TRect& aRect) const
00236 {
00237
00238 CWindowGc& gc=SystemGc();
00239 gc.Clear(aRect);
00240 gc.SetClippingRect(aRect);
00241 gc.DrawRect(Rect());
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 void CSmileyContainer::HandleControlEventL(CCoeControl* aControl,
00251 TCoeEvent aEventType)
00252 {
00253 switch (aEventType)
00254 {
00255 case EEventRequestFocus:
00256 {
00257 if (aControl->IsFocused())
00258 return;
00259 SwapFocus(aControl);
00260 }
00261 break;
00262 default:
00263 break;
00264 }
00265 }
00266
00267
00268
00269
00270
00271
00272 void CSmileyContainer::PrepareForFocusLossL()
00273 {
00274 if (!iSmiley1->IsSmiling() && !iSmiley2->IsSmiling())
00275 {
00276 CEikonEnv::Static()->LeaveWithInfoMsg(R_EXAMPLE_TEXT_VALIDATE);
00277 }
00278 }
00279
00280
00281
00282
00283 void CSmileyContainer::FocusChanged(TDrawNow aDrawNow)
00284 {
00285 if (IsFocused())
00286 {
00287 iSmiley1->SetFocus(ETrue, EDrawNow);
00288 }
00289 else
00290 {
00291 if (iSmiley1->IsFocused())
00292 iSmiley1->SetFocus(EFalse, EDrawNow);
00293 else
00294 iSmiley2->SetFocus(EFalse, EDrawNow);
00295 }
00296 if (aDrawNow)
00297 DrawNow();
00298 }
00299
00300
00301 void CSmileyContainer::SwapFocus(CCoeControl* aControl)
00302 {
00303 if (aControl==iSmiley1)
00304 {
00305 iSmiley2->SetFocus(EFalse, EDrawNow);
00306 iSmiley1->SetFocus(ETrue, EDrawNow);
00307 }
00308 else
00309 {
00310 iSmiley1->SetFocus(EFalse, EDrawNow);
00311 iSmiley2->SetFocus(ETrue, EDrawNow);
00312 }
00313 }
00314
00315 TKeyResponse CSmileyContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00316 {
00317
00318 switch (aKeyEvent.iScanCode)
00319 {
00320 case EStdKeySpace:
00321 if (iSmiley1->IsFocused())
00322 return iSmiley1->OfferKeyEventL(aKeyEvent, aType);
00323 else if (iSmiley2->IsFocused())
00324 return iSmiley2->OfferKeyEventL(aKeyEvent, aType);
00325 break;
00326 case EStdKeyRightArrow:
00327 if (iSmiley1->IsFocused())
00328 SwapFocus(iSmiley2);
00329 else
00330 return EKeyWasConsumed;
00331 break;
00332 case EStdKeyLeftArrow:
00333 if (iSmiley2->IsFocused())
00334 SwapFocus(iSmiley1);
00335 else
00336 return EKeyWasConsumed;
00337 break;
00338 default:
00339 break;
00340 }
00341
00342
00343 return EKeyWasNotConsumed;
00344 }
00345
00346
00347
00348
00350
00351
00352
00354
00355
00356
00357 CSmiley::CSmiley(TBool aSmiling) : iSmiling(aSmiling)
00358 {
00359 }
00360
00361 CSmiley::~CSmiley()
00362 {
00363 }
00364
00365 TBool CSmiley::IsSmiling()
00366 {
00367 return iSmiling;
00368 }
00369
00370 void CSmiley::Draw(const TRect& aRect) const
00371 {
00372 CWindowGc& gc=SystemGc();
00373 if (IsFocused())
00374 {
00375 gc.SetPenColor(KRgbBlack);
00376 }
00377 else
00378 {
00379 gc.SetPenColor(KRgbWhite);
00380 }
00381 gc.SetBrushColor(KRgbWhite);
00382 gc.Clear(Rect());
00383 gc.DrawRect(Rect());
00384
00385 gc.SetClippingRect(aRect);
00386
00387
00388 gc.SetPenColor(KRgbBlack);
00389
00390 gc.DrawEllipse(iSmileyRect);
00391
00392 TPoint leftEye(iSmileyWidth/3, iSmileyHeight/3);
00393 TPoint rightEye(iSmileyWidth*2/3, iSmileyHeight/3);
00394 gc.SetPenSize(TSize(5,5));
00395 gc.Plot(iSmileyRect.iTl+leftEye);
00396 gc.Plot(iSmileyRect.iTl+rightEye);
00397
00398 gc.SetPenSize(TSize(1,1));
00399 gc.SetPenColor(KRgbWhite);
00400 if (iSmiling)
00401 gc.DrawArc(iFrownRect, iFrownRect.iTl+TPoint(iSmileyWidth/2,iFrownRect.Height()/2),
00402 iFrownRect.iTl+TPoint(0,iFrownRect.Height()/2));
00403 else
00404 gc.DrawArc(iSmileRect, iSmileRect.iTl+TPoint(0,iSmileRect.Height()/2),
00405 iSmileRect.iTl+TPoint(iSmileyWidth/2,iSmileRect.Height()/2));
00406 gc.SetPenColor(KRgbBlack);
00407 if (iSmiling)
00408 gc.DrawArc(iSmileRect, iSmileRect.iTl+TPoint(0,iSmileRect.Height()/2),
00409 iSmileRect.iTl+TPoint(iSmileyWidth/2,iSmileRect.Height()/2));
00410 else
00411 gc.DrawArc(iFrownRect, iFrownRect.iTl+TPoint(iSmileyWidth/2,iFrownRect.Height()/2),
00412 iFrownRect.iTl+TPoint(0,iFrownRect.Height()/2));
00413 }
00414
00415 void CSmiley::SizeChanged()
00416 {
00417
00418 iSmileyRect=Rect();
00419
00420 iSmileyRect.Shrink(3,3);
00421 iSmileyWidth=iSmileyRect.Width();
00422 iSmileyHeight=iSmileyRect.Height();
00423 iSmileRect.SetRect(iSmileyRect.iTl+TPoint(iSmileyWidth/4, iSmileyHeight/2),
00424 TSize(iSmileyWidth/2, iSmileyHeight/3));
00425 iFrownRect.SetRect(iSmileyRect.iTl+TPoint(iSmileyWidth/4, iSmileyHeight*2/3),
00426 TSize(iSmileyWidth/2, iSmileyHeight/3));
00427 }
00428
00429 void CSmiley::FocusChanged(TDrawNow aDrawNow)
00430 {
00431 if (aDrawNow)
00432 DrawNow();
00433 }
00434
00435 void CSmiley::HandlePointerEventL(const TPointerEvent& aPointerEvent)
00436 {
00437 if (aPointerEvent.iType==TPointerEvent::EButton1Down)
00438 {
00439 iSmiling = !iSmiling;
00440 DrawNow();
00441 }
00442 }
00443
00444 TKeyResponse CSmiley::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00445 {
00446
00447 if (aType==EEventKey && aKeyEvent.iScanCode==EStdKeySpace)
00448 {
00449 iSmiling = !iSmiling;
00450 DrawNow();
00451 return EKeyWasConsumed;
00452 }
00453 else
00454 {
00455 return EKeyWasNotConsumed;
00456 }
00457 }
00458
00460
00461
00462
00464 TBool CSmileyDialog::RunDlgLD()
00465 {
00466 CEikDialog* dialog = new (ELeave) CSmileyDialog();
00467 return (dialog->ExecuteLD(R_SMILEY_DIALOG));
00468 }
00469
00470
00471
00472 SEikControlInfo CSmileyDialog::CreateCustomControlL(TInt aControlType)
00473 {
00474 SEikControlInfo controlInfo;
00475 controlInfo.iControl = NULL;
00476 controlInfo.iTrailerTextId = 0;
00477 controlInfo.iFlags = 0;
00478
00479 switch (aControlType)
00480 {
00481 case ESmileyControl:
00482 controlInfo.iControl = new(ELeave) CSmileyContainer;
00483 break;
00484 default:
00485 break;
00486 }
00487 return controlInfo;
00488 }
00489
00491
00492
00493
00495 void CExampleAppUi::ConstructL()
00496 {
00497
00498 BaseConstructL();
00499
00500
00501 iMainWinControl=new(ELeave) CMainWinControl;
00502 iMainWinControl->ConstructL(ClientRect());
00503
00504
00505 AddToStackL(iMainWinControl);
00506 }
00507
00508
00509 CExampleAppUi::~CExampleAppUi()
00510 {
00511 RemoveFromStack(iMainWinControl);
00512
00513 delete iMainWinControl;
00514 }
00515
00516 void CExampleAppUi::HandleCommandL(TInt aCommand)
00517 {
00518
00519
00520
00521 switch (aCommand)
00522 {
00523
00524 case EEikCmdExit:
00525 OnCmdExit();
00526 break;
00527 case ECreateSmileyDialog:
00528 CSmileyDialog::RunDlgLD();
00529 break;
00530 default :
00531 break;
00532 }
00533 }
00534
00535 void CExampleAppUi::OnCmdExit()
00536 {
00537 CBaActiveScheduler::Exit();
00538 }
00539
00540 void CExampleAppUi::HandleModelChangeL()
00541 {
00542 }
00543
00545
00546
00547
00549
00550 CExampleDocument::CExampleDocument(CEikApplication& aApp)
00551 : CEikDocument(aApp)
00552 {}
00553
00554 CExampleDocument::~CExampleDocument()
00555 {
00556 }
00557
00558 CExampleDocument* CExampleDocument::NewL(CEikApplication& aApp)
00559 {
00560 CExampleDocument* self=new(ELeave) CExampleDocument(aApp);
00561 CleanupStack::PushL(self);
00562 self->CreateModelL();
00563 CleanupStack::Pop();
00564 return self;
00565 }
00566
00567 void CExampleDocument::ResetModelL()
00568 {
00569 CreateModelL();
00570 }
00571
00572 void CExampleDocument::CreateModelL()
00573 {
00574 }
00575
00576 CEikAppUi* CExampleDocument::CreateAppUiL()
00577 {
00578 return(new(ELeave) CExampleAppUi);
00579 }
00580
00581 void CExampleDocument::NewDocumentL()
00582 {
00583 ResetModelL();
00584 }
00585
00586 void CExampleDocument::StoreL(CStreamStore& ,CStreamDictionary& ) const
00587 {
00588 }
00589
00590 void CExampleDocument::RestoreL(const CStreamStore& ,const CStreamDictionary& )
00591 {
00592 }
00593
00594
00596
00597
00598
00600 TUid CExampleApplication::AppDllUid() const
00601 {
00602 return(KUidExampleApp);
00603 }
00604
00605
00606 CApaDocument* CExampleApplication::CreateDocumentL()
00607 {
00608 return CExampleDocument::NewL(*this);
00609 }
00610
00611
00612
00613
00614
00615
00616
00617 LOCAL_C CApaApplication* NewApplication()
00618 {
00619 return new CExampleApplication;
00620 }
00621
00622 GLDEF_C TInt E32Main()
00623 {
00624 return EikStart::RunApplication(NewApplication);
00625 }
00626
00627
00628
00629
00630