Nokia N97 SDK
Example Applications Guide

openchelloglibeventexui.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : OpenCHelloGlibEventExUi.cpp
00004 *  Part of     : Open C Hello World with Glib Events Example
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2007 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 
00017 // INCLUDE FILES
00018 #include <avkon.hrh>
00019 #include <aknnotewrappers.h>
00020 #include <stringloader.h>
00021 #include <OpenCHelloGlibEventEx.rsg>
00022 #include <f32file.h>
00023 #include <s32file.h>
00024 #include <stdlib.h>
00025 
00026 #include "OpenCHelloGlibEventEx.pan"
00027 #include "OpenCHelloGlibEventExUi.h"
00028 #include "OpenCHelloGlibEventExView.h"
00029 #include "OpenCHelloGlibEventEx.hrh"
00030 
00031 #include <glib.h>
00032 
00033 
00034 // Callback function for the timeout source
00035 gboolean timeout_cb(gpointer data)
00036         {
00037         COpenCHelloGlibEventExView* view = (COpenCHelloGlibEventExView*)data;
00038         
00039         static int i = 0;
00040         
00041         // set the new text to the displayed by the View
00042         TBuf<30> buf;
00043         buf.Format(_L("Callback No. %d"), i);
00044         view->SetText(buf);
00045         
00046         // notifying the UI framework that the view is "dirty" and should be drawn again
00047         view->DrawNow();
00048 
00049         // increment the callback count
00050         i++;
00051 
00052         // return TRUE to continue receiving timeout events, FALSE to stop
00053         return TRUE;
00054         }
00055 
00056 
00057 // ============================ MEMBER FUNCTIONS ===============================
00058 
00059 
00060 // -----------------------------------------------------------------------------
00061 // COpenCHelloGlibEventExUi::ConstructL()
00062 // Symbian 2nd phase constructor can leave.
00063 // -----------------------------------------------------------------------------
00064 //
00065 void COpenCHelloGlibEventExUi::ConstructL()
00066     {
00067     // Initialise app UI with standard value.
00068     BaseConstructL(CAknAppUi::EAknEnableSkin);
00069 
00070     // Create view object
00071     iAppView = COpenCHelloGlibEventExView::NewL( ClientRect() );
00072         
00073         // required for preventing Alloc Panics in Debug builds
00074         setenv("G_SLICE", "always-malloc", 1);
00075         
00076         // In the Glib event loop paradigm, events can come from any number of 
00077         // different types of sources.
00078         // To allow multiple independent sets of sources to be handled in different 
00079         // threads, each source is associated with a GMainContext. A GMainContext 
00080         // can only be running in a single thread.
00081         // In this example, we create a new GMainContext in which the timeout 
00082         // source will be added.
00083         // NOTE: This is NOT required if the event sources are to be added to the 
00084         // "default" context.
00085     iMainContext = g_main_context_new();
00086         
00087         // An event source is represented by the GSource structure, and new
00088         // source types are created by "deriving" from the GSource structure.
00089         // In this example, we create an instance of timeout source, 
00090         // with the timout value of 1000 milliseconds.
00091         // The newly created GSource is intially not attached to any GMainContext
00092         iTimeoutSource = g_timeout_source_new(1000);
00093         
00094         // Associate a callback function with the timeout source
00095         // The iAppView pointer is being passed to glib here, so that the 
00096         // callback function gets called with the iAppView pointer as the argument
00097         g_source_set_callback(iTimeoutSource, timeout_cb, iAppView, NULL);
00098         
00099         // add the timeout source to the GMainContext
00100         // NOTE: Pass NULL instead of iMainContext to attach the source to the 
00101         // "default" context
00102         g_source_attach(iTimeoutSource, iMainContext);
00103         
00104         // Create an instance of the event handler active object, for handling
00105         // event sources of the given GMainContext
00106         // The event handler active object adds itself to the current 
00107         // installed active scheduler (created by the Symbian UI framework)
00108         // NOTE 1: Create one instance of event handler each for each of the 
00109         // GMainContexts in the application.
00110         // NOTE 2: Pass NULL as the argument to create an event handler for the 
00111         // "default" context
00112         iGlibEventHandler = COpenCHelloGlibEventExHandler::NewL(iMainContext);
00113         
00114         // Start the event handler active object
00115         // NOTE: This needs to be done for all event handlers created
00116         // Once started, the event handler active object schedules itself based
00117         // on the Symbian RTimer service. It runs one non-blocking iteration
00118         // of the event loop for the given GMainContext, each time it gets
00119         // scheduled.
00120         iGlibEventHandler->Start();
00121     }
00122     
00123 // -----------------------------------------------------------------------------
00124 // COpenCHelloGlibEventExUi::COpenCHelloGlibEventExUi()
00125 // C++ default constructor can NOT contain any code, that might leave.
00126 // -----------------------------------------------------------------------------
00127 //
00128 COpenCHelloGlibEventExUi::COpenCHelloGlibEventExUi()
00129     {
00130     // No implementation required
00131     }
00132 
00133 // -----------------------------------------------------------------------------
00134 // COpenCHelloGlibEventExUi::COpenCHelloGlibEventExUi()
00135 // Destructor.
00136 // -----------------------------------------------------------------------------
00137 //
00138 COpenCHelloGlibEventExUi::~COpenCHelloGlibEventExUi()
00139     {
00140     // Stop and delete the event handler active object
00141     // NOTE: This needs to be done for all event handlers created
00142     iGlibEventHandler->Stop();
00143     delete iGlibEventHandler;
00144    
00145         // unref the Timeout source
00146         g_source_unref(iTimeoutSource);
00147         
00148         // unref the GMainContext
00149         // NOTE: This is not required for the "default" context
00150     g_main_context_unref(iMainContext);
00151     
00152     if ( iAppView )
00153         {
00154         delete iAppView;
00155         iAppView = NULL;
00156         }
00157 
00158     }
00159 
00160 // -----------------------------------------------------------------------------
00161 // COpenCHelloGlibEventExUi::HandleCommandL()
00162 // Takes care of command handling.
00163 // -----------------------------------------------------------------------------
00164 //
00165 void COpenCHelloGlibEventExUi::HandleCommandL( TInt aCommand )
00166     {
00167     switch( aCommand )
00168         {
00169         case EEikCmdExit:
00170         case EAknSoftkeyExit:
00171             Exit();
00172             break;
00173 
00174         case EOpenCHelloGlibEventExCommand1:
00175             {
00176             
00177             // Load a string from the resource file and display it
00178             HBufC* textResource = StringLoader::LoadLC( R_OCHG_COMMAND1_TEXT );
00179             CAknInformationNote* informationNote;
00180 
00181             informationNote = new ( ELeave ) CAknInformationNote;
00182 
00183             // Show the information Note with
00184             // textResource loaded with StringLoader.
00185             informationNote->ExecuteLD( *textResource);
00186 
00187             // Pop HBuf from CleanUpStack and Destroy it.
00188             CleanupStack::PopAndDestroy( textResource );
00189             }
00190             break;
00191         default:
00192             break;
00193         }
00194     }
00195     
00196 // -----------------------------------------------------------------------------
00197 //  Called by the framework when the application status pane
00198 //  size is changed.  Passes the new client rectangle to the
00199 //  AppView
00200 // -----------------------------------------------------------------------------
00201 //
00202 void COpenCHelloGlibEventExUi::HandleStatusPaneSizeChange()
00203         {
00204         iAppView->SetRect( ClientRect() );
00205         
00206         } 
00207 
00208 // End of File
00209 

© Nokia 2009

Back to top