examples/ForumNokia/HTTPClientExample/src/ClientAppView.cpp

00001 /*
00002  * Copyright (c) 2009 Nokia Corporation.
00003  */
00004 
00005 #include <aknnotewrappers.h>
00006 #include <coemain.h>
00007 #include <eikedwin.h>
00008 #include <gdi.h>
00009 #include <txtrich.h>
00010 #include "Client.pan"
00011 #include "ClientAppView.h"
00012 #include "ClientApplication.h"
00013 #include "ClientEngine.h"
00014 #include "HTTPClientExample_sc.hlp.hrh"
00015 
00016 // Constants for CEikRichTextEditors
00017 const TInt KNumberOfControls = 2;
00018 const TInt KNumberOfLines = 0;
00019 const TInt KTextLimit = 128;
00020 
00021 const TInt KBorderThickness = 4;
00022 const TInt KSizeOffset = KBorderThickness * 2;
00023 
00024 // Background color
00025 #define KBackgroundColor    TRgb(128, 128, 128)
00026 #define KBorderColor        TRgb(180, 0, 0)
00027 
00028 // ----------------------------------------------------------------------------
00029 // CClientAppView::NewL()
00030 //
00031 // Creates instance of CClientAppView.
00032 // ----------------------------------------------------------------------------
00033 CClientAppView* CClientAppView::NewL(const TRect& aRect)
00034   {
00035     CClientAppView* self = CClientAppView::NewLC(aRect);
00036     CleanupStack::Pop(self);
00037     return self;
00038   }
00039 
00040 // ----------------------------------------------------------------------------
00041 // CClientAppView::NewLC()
00042 //
00043 // Creates instance of CClientAppView.
00044 // ----------------------------------------------------------------------------
00045 CClientAppView* CClientAppView::NewLC(const TRect& aRect)
00046   {
00047     CClientAppView* self = new (ELeave) CClientAppView;
00048     CleanupStack::PushL(self);
00049     self->ConstructL(aRect);
00050     return self;
00051   }
00052 
00053 // ----------------------------------------------------------------------------
00054 // CClientAppView::CClientAppView()
00055 //
00056 // First phase construction.
00057 // ----------------------------------------------------------------------------
00058 CClientAppView::CClientAppView()
00059   {
00060   }
00061 
00062 // ----------------------------------------------------------------------------
00063 // CClientAppView::~CClientAppView()
00064 //
00065 // Destructor.
00066 // ----------------------------------------------------------------------------
00067 CClientAppView::~CClientAppView()
00068   {
00069     delete iOutputWindow;
00070   delete iStatusWindow;
00071   }
00072 
00073 // ----------------------------------------------------------------------------
00074 // CClientAppView::ConstructL()
00075 //
00076 // Second phase construction.
00077 // ----------------------------------------------------------------------------
00078 void CClientAppView::ConstructL(const TRect& aRect)
00079   {
00080   // Create a window for this application view
00081   CreateWindowL();
00082 
00083   // Create output window
00084   iOutputWindow = new (ELeave) CEikRichTextEditor();
00085   iOutputWindow->SetContainerWindowL(*this);
00086   iOutputWindow->ConstructL(this, KNumberOfLines, KTextLimit,
00087     CEikEdwin::EReadOnly | CEikEdwin::EAvkonDisableCursor,
00088     EGulFontControlAll, EGulNoSymbolFonts);
00089   iOutputWindow->CreateScrollBarFrameL()->SetScrollBarVisibilityL(
00090       CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
00091 
00092   // Create status window
00093   iStatusWindow = new (ELeave) CEikRichTextEditor();
00094   iStatusWindow->SetContainerWindowL(*this);
00095   iStatusWindow->ConstructL(this, KNumberOfLines, KTextLimit,
00096     CEikEdwin::EReadOnly | CEikEdwin::EAvkonDisableCursor,
00097     EGulFontControlAll, EGulNoSymbolFonts);
00098   iStatusWindow->CreateScrollBarFrameL()->SetScrollBarVisibilityL(
00099       CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
00100 
00101   iStatusWindow->SetFocus(ETrue);
00102 
00103   // Activate the window, which makes it ready to be drawn
00104   ActivateL();
00105 
00106   // Set the windows size
00107   SetRect(aRect);
00108   }
00109 
00110 // ---------------------------------------------------------
00111 // CClientAppView::SizeChanged()
00112 // Called whenever SetExtent(), SetSize(), SetRect(),
00113 // SetCornerAndSize(), or SetExtentToWholeScreen() are
00114 // called on the control. NB: The window server does not
00115 // generate size-changed events.
00116 // ---------------------------------------------------------
00117 
00118 void CClientAppView::SizeChanged()
00119     {
00120     TRect r = Rect();
00121     TInt scrollbarWidth = GetScrollbarWidth(iOutputWindow);
00122     TInt editorWidth = r.Width() - KSizeOffset - scrollbarWidth;
00123     
00124     TPoint upperEditPosition = TPoint(KBorderThickness, KBorderThickness);
00125     TInt upperEditHeight = r.Height()*3/4 - KSizeOffset;
00126     iOutputWindow->SetExtent(upperEditPosition, TSize(editorWidth,
00127         upperEditHeight));
00128     
00129     upperEditPosition.iY += upperEditHeight + KBorderThickness;
00130     upperEditHeight = r.Height()/4 - KBorderThickness;
00131     iStatusWindow->SetExtent(upperEditPosition, TSize(editorWidth,
00132         upperEditHeight));
00133     }
00134 
00135 // -----------------------------------------------------------------------------
00136 // CClientAppView::GetHelpContext()
00137 // Gets the control's help context. Returns a NULL context by default.
00138 // -----------------------------------------------------------------------------
00139 
00140 void CClientAppView::GetHelpContext(TCoeHelpContext& aContext) const
00141     {
00142     // Get the help context for the application
00143     aContext.iMajor = KUidHelpFile;
00144 
00145     aContext.iContext = KContextApplication;
00146     }
00147 
00148 // ---------------------------------------------------------
00149 // CClientAppView::HandleResourceChange()
00150 // Called by the framework when resource is changed.
00151 // ---------------------------------------------------------
00152 
00153 void CClientAppView::HandleResourceChange(TInt aType)
00154     {
00155     CCoeControl::HandleResourceChange(aType);
00156 
00157     // ADDED FOR SCALABLE UI SUPPORT
00158     // *****************************
00159     if ( aType==KEikDynamicLayoutVariantSwitch )
00160         {
00161         TRect rect;
00162         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00163         SetRect(rect);
00164         }
00165     }
00166 
00167 // ----------------------------------------------------------------------------
00168 // CClientAppView::Draw()
00169 //
00170 // Draw this application's view to the screen
00171 // ----------------------------------------------------------------------------
00172 void CClientAppView::Draw(const TRect& aRect) const
00173   {
00174     CWindowGc& gc = SystemGc();
00175 
00176     gc.SetPenStyle( CGraphicsContext::ENullPen );
00177     gc.SetBrushColor( KBackgroundColor );
00178     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00179     gc.DrawRect( aRect );
00180     
00181     // Draw a custom border around the focused rich text editor
00182     gc.SetBrushColor(KBorderColor);
00183     if (iOutputWindow->IsFocused())
00184         {
00185         TInt scrollbarWidth = GetScrollbarWidth(iOutputWindow);
00186         TRect editorRect = iOutputWindow->Rect();
00187         editorRect.Grow(KBorderThickness + scrollbarWidth, KBorderThickness);
00188         editorRect.Move(scrollbarWidth, 0);
00189         gc.DrawRect(editorRect);
00190         }
00191         
00192     if (iStatusWindow->IsFocused())
00193         {
00194         TInt scrollbarWidth = GetScrollbarWidth(iStatusWindow);
00195         TRect editorRect = iStatusWindow->Rect();
00196         editorRect.Grow(KBorderThickness + scrollbarWidth, KBorderThickness);
00197         editorRect.Move(scrollbarWidth, 0);
00198         gc.DrawRect(editorRect);
00199         }
00200   }
00201 
00202 // ----------------------------------------------------------------------------
00203 // Returns the width (breadth) of the scrollbar for a particular editor
00204 // ----------------------------------------------------------------------------
00205 TInt CClientAppView::GetScrollbarWidth(CEikRichTextEditor* anEditor) const
00206     {
00207     TInt scrollbarWidth = anEditor->ScrollBarFrame()->
00208         ScrollBarBreadth(CEikScrollBar::EVertical);
00209     // TODO: If scrollbars are not drawn yet, the width remains zero. In this
00210     // case, an intentionally magical number is returned.
00211     if (scrollbarWidth == 0)
00212         {
00213         scrollbarWidth = 8;
00214         }
00215     return scrollbarWidth;
00216     }
00217     
00218 // ----------------------------------------------------------------------------
00219 // CClientAppView::CountComponentControls()
00220 //
00221 // Returns number of controls in this compound control.
00222 // ----------------------------------------------------------------------------
00223 TInt CClientAppView::CountComponentControls() const
00224   {
00225     return KNumberOfControls;
00226   }
00227 
00228 // ----------------------------------------------------------------------------
00229 // CClientAppView::ComponentControl()
00230 //
00231 // Returns pointer to control with index aIndex.
00232 // ----------------------------------------------------------------------------
00233 CCoeControl* CClientAppView::ComponentControl(TInt aIndex) const
00234   {
00235     switch(aIndex)
00236     {
00237     case 0:
00238       return iOutputWindow;
00239     case 1:
00240       return iStatusWindow;
00241     default:
00242       Panic(EClientView);
00243       return 0;
00244     }
00245   }
00246 
00247 // ----------------------------------------------------------------------------
00248 // CClientAppView::HandleControlEventL()
00249 //
00250 // Handles control events.
00251 // ----------------------------------------------------------------------------
00252 void CClientAppView::HandleControlEventL(CCoeControl* /* aControl */,
00253                        TCoeEvent /* aEventType */ )
00254   {
00255   }
00256 
00257 // ----------------------------------------------------------------------------
00258 // CClientAppView::ResetL()
00259 //
00260 // Resets contents of output and status window.
00261 // ----------------------------------------------------------------------------
00262 void CClientAppView::ResetL()
00263   {
00264     iOutputWindow->Text()->Reset();
00265     iOutputWindow->HandleTextChangedL();
00266     iOutputWindow->SetCursorPosL(0, EFalse);
00267 
00268     iStatusWindow->Text()->Reset();
00269     iStatusWindow->HandleTextChangedL();
00270     iStatusWindow->SetCursorPosL(0, EFalse);
00271   }
00272 
00273 // ----------------------------------------------------------------------------
00274 // CClientAppView::OfferKeyEventL()
00275 //
00276 // Handles key events.
00277 // ----------------------------------------------------------------------------
00278 TKeyResponse CClientAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00279                         TEventCode aType)
00280   {
00281   // Catch EStdKeyNkp5 and EStdKeyDevice3; they are used here to switch
00282   // the active CEikRichTextEditor.
00283   if(aType == EEventKey)
00284     {
00285     switch(aKeyEvent.iScanCode)
00286       {
00287       case EStdKeyNkp5:
00288       case EStdKeyDevice3:
00289         SwitchFocus();
00290         return EKeyWasConsumed;
00291       }
00292     }
00293 
00294   // Redirect keyevents to controls
00295     if (iOutputWindow)
00296     {
00297       if (iOutputWindow->IsFocused())
00298       return iOutputWindow->OfferKeyEventL(aKeyEvent, aType);
00299     }
00300 
00301     if (iStatusWindow)
00302     {
00303       if (iStatusWindow->IsFocused())
00304       return iStatusWindow->OfferKeyEventL(aKeyEvent, aType);
00305     }
00306 
00307     return EKeyWasNotConsumed;
00308   }
00309 
00310 // ----------------------------------------------------------------------------
00311 // CClientAppView::SwitchFocus()
00312 //
00313 // Switches focus between the two rich text editors.
00314 // ----------------------------------------------------------------------------
00315 void CClientAppView::SwitchFocus()
00316   {
00317   if (iOutputWindow->IsFocused())
00318     {
00319     iOutputWindow->SetFocus(EFalse);
00320     iStatusWindow->SetFocus(ETrue);
00321     }
00322   else
00323     {
00324     iStatusWindow->SetFocus(EFalse);
00325     iOutputWindow->SetFocus(ETrue);
00326     }
00327   // Update the borders
00328   DrawNow();
00329   }
00330   
00331 // ----------------------------------------------------------------------------
00332 // CClientAppView::AddToStatusWindowL()
00333 //
00334 // Add a single line of text to the status window.
00335 // ----------------------------------------------------------------------------
00336 void CClientAppView::AddToStatusWindowL(const TDesC& aText)
00337   {
00338   // Append aText to status window with line break
00339   CRichText* text = iStatusWindow->RichText();
00340   text->InsertL(text->DocumentLength(), aText);
00341   text->InsertL(text->DocumentLength(), CEditableText::ELineBreak);
00342   iStatusWindow->HandleTextChangedL();
00343   iStatusWindow->MoveCursorL(TCursorPosition::EFLineDown, EFalse);
00344   }
00345 
00346 // ----------------------------------------------------------------------------
00347 // CClientAppView::AddToOutputWindowL()
00348 //
00349 // Add text to outputwindow. No formatting is provided.
00350 // ----------------------------------------------------------------------------
00351 void CClientAppView::AddToOutputWindowL(const TDesC8& aText)
00352   {
00353   // Convert 8-bit aText to 16-bit and append it to end of output window
00354   HBufC* tempBuf = HBufC::NewL(aText.Length());
00355   CleanupStack::PushL(tempBuf);
00356   tempBuf->Des().Copy(aText);
00357   CRichText* text = iOutputWindow->RichText();
00358   text->InsertL(text->DocumentLength(), *tempBuf);
00359   iOutputWindow->HandleTextChangedL();
00360 
00361   CleanupStack::PopAndDestroy(tempBuf);
00362   }
00363 
00364 // ----------------------------------------------------------------------------
00365 // CClientAppView::ClientEvent()
00366 //
00367 // Called by CClientEngine to notify events to user
00368 // ----------------------------------------------------------------------------
00369 void CClientAppView::ClientEvent(const TDesC& aEventDescription)
00370   {
00371   TRAPD(err, AddToStatusWindowL(aEventDescription));
00372   if(err)
00373     Panic(EClientView);
00374   }
00375 
00376 // ----------------------------------------------------------------------------
00377 // CClientAppView::ClientBodyReceived()
00378 //
00379 // Called by CClientEngine when part of response body received
00380 // ----------------------------------------------------------------------------
00381 void CClientAppView::ClientBodyReceived(const TDesC8& aBodyData)
00382   {
00383   TRAPD(err, AddToOutputWindowL(aBodyData));
00384   if(err)
00385     Panic(EClientView);
00386   }
00387 
00388 // end of file

Generated by  doxygen 1.6.2