plugins/consoles/guicons/s60/src/GuiConsAppUi.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // GuiConsAppUi.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <avkon.hrh>
       
    14 #include <aknnotewrappers.h>
       
    15 #include <stringloader.h>
       
    16 #include <GuiCons.rsg>
       
    17 #include <f32file.h>
       
    18 #include <s32file.h>
       
    19 #include <aknnavi.h>
       
    20 #include <aknnavilabel.h>
       
    21 #include <akntabgrp.h>
       
    22 #include <akntitle.h>
       
    23 #include <hal.h>
       
    24 #include <caknmemoryselectiondialog.h>
       
    25 #include <caknfileselectiondialog.h>
       
    26 #include <fshell/ltkutils.h>
       
    27 
       
    28 #include "GuiConsAppUi.h"
       
    29 #include "GuiConsAppView.h"
       
    30 #include "GuiCons.hrh"
       
    31 
       
    32 #define EXPAND(L, X) L(X)
       
    33 #define PANIC() User::Panic(EXPAND(_L, __FILE__), __LINE__)
       
    34 #define GUICONS_ASSERT(x) __ASSERT_ALWAYS(x, PANIC())
       
    35 
       
    36 _LIT(KServerName, "guicons");
       
    37 _LIT(KNoConsole, "no console connected");
       
    38 _LIT(KConsoleClosed, "console closed");
       
    39 _LIT(KNewProcName, "fshell.exe");
       
    40 _LIT(KNewProcArgs, "--console guicons");
       
    41 
       
    42 _LIT(KIniFile, "\\system\\console\\guicons.ini");
       
    43 _LIT(KIdfFile, "\\resource\\guicons.idf");
       
    44 _LIT(KIniLongPress, "longpress");
       
    45 _LIT(KIniShowNav, "shownav");
       
    46 _LIT(KIniBufferSize, "buffersize");
       
    47 _LIT(KIniFont, "font");
       
    48 
       
    49 _LIT(KInvalidFontMsgFmt, "The file '%S' is not a valid font (error %d).\r\n\r\nA font file is a bitmap containing an 8x12 grid "
       
    50 			L"of characters with ASCII codes 32 to 127. The width of the bitmap must be a multiple of 8, and the "
       
    51 			L"height must be a multiple of 12.");
       
    52 _LIT(KFontPreview, "New font:\r\nThe quick brown fox jumps over the lazy dog.");
       
    53 
       
    54 void CGuiConsAppUi::ConstructL()
       
    55 	{
       
    56 	BaseConstructL(CAknAppUi::EAknEnableSkin);
       
    57 	iTempState = new(ELeave) CConsoleState(NULL);
       
    58 	iAppView = CGuiConsAppView::NewL( ClientRect() );
       
    59 	
       
    60 	iIniFile = LtkUtils::CIniFile::NewL(KIniFile, KIdfFile);
       
    61 	
       
    62 	iAppView->Layout();
       
    63 	iAppView->ActivateL();
       
    64 
       
    65 	CEikStatusPane* sp = StatusPane();
       
    66 	iNaviPane = (CAknNavigationControlContainer*)sp->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
       
    67 	//iNaviDecorator = iNaviPane->CreateNavigationLabelL(_L("guicons"));
       
    68 	iNaviDecorator = iNaviPane->CreateTabGroupL(this);
       
    69 	iTabGroup = static_cast<CAknTabGroup*>(iNaviDecorator->DecoratedControl());
       
    70 	iTabGroup->SetTabFixedWidthL(KTabWidthWithFourTabs);
       
    71 	iNaviPane->PushL(*iNaviDecorator);
       
    72 
       
    73 	SetStateL(ENoConsole);
       
    74 	ConsoleTitleChangedL(NULL, KNullDesC);
       
    75 
       
    76 	SetCapturingLongPressesL(iIniFile->GetBool(KIniLongPress));
       
    77 	
       
    78 	LtkUtils::CSetting* showNavSetting = iIniFile->GetSetting(KIniShowNav);
       
    79 	if (!showNavSetting->IsSet())
       
    80 		{
       
    81 		TBool hasTouchScreen = EFalse;
       
    82 		TInt err = HAL::Get(HAL::EPen, hasTouchScreen);
       
    83 		if (!err && hasTouchScreen)
       
    84 			{
       
    85 			iShouldDisplayOnscreenNav = ETrue;
       
    86 			}
       
    87 		}
       
    88 	else
       
    89 		{
       
    90 		iShouldDisplayOnscreenNav = showNavSetting->AsBool();
       
    91 		}
       
    92 	
       
    93 	iConsoleServer = StartGuiConsServerL(KServerName, *this);	
       
    94 
       
    95 	TRAP_IGNORE(LoadCommandsL());
       
    96 	}
       
    97 
       
    98 CGuiConsAppUi::CGuiConsAppUi()
       
    99 	{
       
   100 	}
       
   101 	
       
   102 void CGuiConsAppUi::SetNaviPaneTextL(const TDesC& aText)
       
   103 	{
       
   104 	if (iNaviDecorator->ControlType() == CAknNavigationDecorator::ENaviLabel)
       
   105 		{
       
   106 		CAknNaviLabel* naviLabel = (CAknNaviLabel*)iNaviDecorator->DecoratedControl();
       
   107 		naviLabel->SetTextL(aText);
       
   108 		iNaviPane->DrawNow();
       
   109 		naviLabel->DrawNow();
       
   110 		}
       
   111 	}
       
   112 	
       
   113 TKeyResponse CGuiConsAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
       
   114 	{
       
   115 	if (aType == EEventKey)
       
   116 		{
       
   117 		switch (iState)
       
   118 			{
       
   119 		case EEdwinActive:
       
   120 			GUICONS_ASSERT(iConsole);
       
   121 			// fallthrough
       
   122 		case EConsoleClosed:
       
   123 			switch (aKeyEvent.iCode)
       
   124 				{
       
   125 			case EKeyEnter:
       
   126 			case EKeyDevice3:
       
   127 				if (iState == EEdwinActive) 
       
   128 					{
       
   129 					HBufC* text = iAppView->GetEditorTextL();
       
   130 					if (text)
       
   131 						{
       
   132 							{
       
   133 							CleanupStack::PushL(text);
       
   134 							iConsole->InjectKeysL(*text);
       
   135 							CleanupStack::PopAndDestroy();
       
   136 							}
       
   137 						iConsole->InjectKeysL(_L("\r"));
       
   138 						iAppView->SetEditorTextL(NULL);
       
   139 						//iAppView->Editor()->DrawNow();
       
   140 						}
       
   141 					SetStateL(iState);
       
   142 					return EKeyWasConsumed;
       
   143 					}
       
   144 			case EKeyUpArrow:
       
   145 				iConsole->ViewScrollUp();
       
   146 				return EKeyWasConsumed;
       
   147 			case EKeyDownArrow:
       
   148 				iConsole->ViewScrollDown();
       
   149 				return EKeyWasConsumed;
       
   150 			case EKeyPageDown:
       
   151 				iConsole->ViewPageDown();
       
   152 				return EKeyWasConsumed;
       
   153 			case EKeyPageUp:
       
   154 				iConsole->ViewPageUp();
       
   155 				return EKeyWasConsumed;
       
   156 			case EKeyHome:
       
   157 				iConsole->ViewHome();
       
   158 				return EKeyWasConsumed;
       
   159 			case EKeyEnd:
       
   160 				iConsole->ViewEnd();
       
   161 				return EKeyWasConsumed;
       
   162 			default:
       
   163 				break;
       
   164 				}
       
   165 		case EConsoleActive:
       
   166 		case ENoConsole:
       
   167 		default:
       
   168 			return EKeyWasNotConsumed;
       
   169 			}
       
   170 		}
       
   171 	return EKeyWasNotConsumed;
       
   172 	}
       
   173 	
       
   174 	
       
   175 void CGuiConsAppUi::SetSoftkeysL()
       
   176 	{
       
   177 	CEikButtonGroupContainer* cba = CEikButtonGroupContainer::Current();
       
   178 	
       
   179 	TInt softkeysResourceId;
       
   180 	switch (iState)
       
   181 		{
       
   182 	case EConsoleActive:
       
   183 		softkeysResourceId = R_GUICONS_INTERACTIVE_CBA;
       
   184 		break;
       
   185 	case EEdwinActive:
       
   186 		softkeysResourceId = R_GUICONS_LINE_CBA;
       
   187 		break;
       
   188 	case EConsoleClosed:
       
   189 		softkeysResourceId = R_GUICONS_CLOSED_CBA;
       
   190 		break;
       
   191 	case ENoConsole:
       
   192 	default:
       
   193 		softkeysResourceId = R_AVKON_SOFTKEYS_OPTIONS_EXIT;
       
   194 		break;
       
   195 		}
       
   196 	cba->SetCommandSetL(softkeysResourceId);
       
   197 	cba->DrawNow();
       
   198 	}
       
   199 	
       
   200 void CGuiConsAppUi::SetStateL(TState aNewState)
       
   201 	{
       
   202 	switch (aNewState)
       
   203 		{
       
   204 	case ENoConsole:
       
   205 		GUICONS_ASSERT(!iConsole);
       
   206 		CCoeEnv::Static()->RootWin().CancelTextCursor();
       
   207 		iAppView->ShowLabelL(KNoConsole);
       
   208 		break;
       
   209 		
       
   210 	case EConsoleActive:
       
   211 		GUICONS_ASSERT(iConsole);
       
   212 		iAppView->FocusEditor(EFalse);
       
   213 		iAppView->RemoveEditorFromStack(this);
       
   214 
       
   215 		RemoveFromStack(iConsole);
       
   216 		AddToStackL(iConsole);
       
   217 		iConsole->SetFocusing(ETrue);
       
   218 		iConsole->SetFocus(ETrue);
       
   219 		iAppView->ShowEditor();
       
   220 		iEdwinWasLastRunningState = EFalse;
       
   221 		break;
       
   222 		
       
   223 	case EEdwinActive:
       
   224 		GUICONS_ASSERT(iConsole);
       
   225 		iConsole->SetFocus(EFalse);
       
   226 		iConsole->SetFocusing(EFalse);
       
   227 		RemoveFromStack(iConsole);
       
   228 		iAppView->ShowEditor();
       
   229 		iAppView->FocusEditor(ETrue);
       
   230 		
       
   231 		iAppView->RemoveEditorFromStack(this);
       
   232 		iAppView->AddEditorToStackL(this);
       
   233 
       
   234 		iAppView->ShowEditor();
       
   235 		iEdwinWasLastRunningState = ETrue;
       
   236 		break;
       
   237 		
       
   238 	case EConsoleClosed:
       
   239 		GUICONS_ASSERT(iConsole);
       
   240 		iAppView->FocusEditor(EFalse);
       
   241 		iAppView->RemoveEditorFromStack(this);
       
   242 		RemoveFromStack(iConsole);
       
   243 		iAppView->ShowLabelL(KConsoleClosed);
       
   244 		CCoeEnv::Static()->RootWin().CancelTextCursor();
       
   245 		break;
       
   246 		}
       
   247 	iState = aNewState;
       
   248 	SetSoftkeysL();
       
   249 	}
       
   250 	
       
   251 void CGuiConsAppUi::HandleNewConsoleL(CConsoleControl* aConsole)
       
   252 	{
       
   253 	CConsoleState* newState = new(ELeave) CConsoleState(aConsole);
       
   254 	iTabs.AppendL(newState);
       
   255 	if (aConsole == iTempState->iControl)
       
   256 		{
       
   257 		newState->iTitle.Copy(iTempState->iTitle);
       
   258 		iTempState->iControl = NULL;
       
   259 		}
       
   260 	TBuf<2> text;
       
   261 	text.Num(iTabs.Count());
       
   262 	iTabGroup->AddTabL(reinterpret_cast<TInt>(newState), text);
       
   263 	SwitchTabL(iTabs.Count()-1);
       
   264 	SetStateToPreferredL();
       
   265 	aConsole->ActivateL();
       
   266 	}
       
   267 	
       
   268 void CGuiConsAppUi::HandleConsoleClosed(CConsoleControl* aConsole)
       
   269 	{
       
   270 	CConsoleState* state = StateForConsole(aConsole);
       
   271 	if (state)
       
   272 		{
       
   273 		state->iConsoleClosed = ETrue;
       
   274 		}
       
   275 
       
   276 	if (aConsole && (aConsole == iConsole))
       
   277 		{
       
   278 		TRAP_IGNORE(SetStateL(EConsoleClosed));
       
   279 		}
       
   280 	}
       
   281 	
       
   282 void CGuiConsAppUi::ConsoleTitleChangedL(const CConsoleControl* aConsole, const TDesC& aTitle)
       
   283 	{
       
   284 	//SetNaviPaneTextL(aTitle);
       
   285 
       
   286 	// Put it in the title so the navi bar is free for tabs
       
   287 	if (aConsole == iConsole)
       
   288 		{
       
   289 		// If aConsole isn't current, we don't want to update anything
       
   290 		CEikStatusPane* sp = StatusPane();
       
   291 		CAknTitlePane* titlePane = NULL;
       
   292 		if (sp) titlePane = (CAknTitlePane*)sp->ControlL(TUid::Uid(EEikStatusPaneUidTitle));
       
   293 		if (titlePane)
       
   294 			{
       
   295 			if (aTitle.Length())
       
   296 				{
       
   297 				titlePane->SetTextL(aTitle);
       
   298 				}
       
   299 			else
       
   300 				{
       
   301 				titlePane->SetTextL(_L("GuiCons"));
       
   302 				}
       
   303 			}
       
   304 		}
       
   305 
       
   306 	// And update the state
       
   307 	CConsoleState* state = StateForConsole(aConsole);
       
   308 	if (state)
       
   309 		{
       
   310 		state->iTitle = aTitle.Left(state->iTitle.MaxLength());
       
   311 		}
       
   312 	else
       
   313 		{
       
   314 		iTempState->iControl = const_cast<CConsoleControl*>(aConsole);
       
   315 		iTempState->iTitle = aTitle.Left(iTempState->iTitle.MaxLength());
       
   316 		}
       
   317 	}
       
   318 	
       
   319 TInt CGuiConsAppUi::GetConsoleBufferSize()
       
   320 	{
       
   321 	return iIniFile->GetInt(KIniBufferSize);
       
   322 	}
       
   323 	
       
   324 const TDesC& CGuiConsAppUi::GetConsoleFont()
       
   325 	{
       
   326 	return iIniFile->GetString(KIniFont);
       
   327 	}
       
   328 
       
   329 CGuiConsAppUi::~CGuiConsAppUi()
       
   330 	{
       
   331 	CKeyCapturer::DeleteCapturer();
       
   332 
       
   333 	iAppView->RemoveEditorFromStack(this);
       
   334 	delete iConsoleServer;
       
   335 	iTabs.ResetAndDestroy();
       
   336 	delete iNaviDecorator;
       
   337 	delete iAppView;
       
   338 	delete iTempState;
       
   339 	iCommands.ResetAndDestroy();
       
   340 	delete iIniFile;
       
   341 	}
       
   342 
       
   343 void CGuiConsAppUi::HandleCommandL(TInt aCommand)
       
   344 	{
       
   345 	if (!CommandValidInCurrentState(aCommand))
       
   346 		{
       
   347 		_LIT(KNotAvailableNow, "Not available now");
       
   348 		iEikonEnv->InfoMsg(KNotAvailableNow);
       
   349 		return;
       
   350 		}
       
   351 	
       
   352 	switch(aCommand)
       
   353 		{
       
   354 	case EEikCmdExit:
       
   355 	case EAknSoftkeyExit:
       
   356 		Exit();
       
   357 		break;
       
   358 
       
   359 	case EGuiConsFocusConsole:
       
   360 		SetStateL(EConsoleActive);
       
   361 		break;
       
   362 	case EGuiConsFocusEdwin:
       
   363 		SetStateL(EEdwinActive);
       
   364 		break;
       
   365 	case EGuiConsPageUp:
       
   366 		iConsole->ViewPageUp();
       
   367 		break;
       
   368 	case EGuiConsPageDown:
       
   369 		iConsole->ViewPageDown();
       
   370 		break;
       
   371 	case EGuiConsHome:
       
   372 		iConsole->ViewHome();
       
   373 		break;
       
   374 	case EGuiConsEnd:
       
   375 		iConsole->ViewEnd();
       
   376 		break;
       
   377 	case EGuiConsCloseConsole:
       
   378 		DeleteCurrentConsoleL();
       
   379 		break;
       
   380 	case EGuiConsNewConsole:
       
   381 		NewFshellProcessL(KNullDesC);
       
   382 		break;
       
   383 	case EGuiConsCtrlC:
       
   384 		{
       
   385 		TKeyEvent key;
       
   386 		key.iCode = CTRL('c');
       
   387 		key.iScanCode = 0;
       
   388 		key.iRepeats = 0;
       
   389 		key.iModifiers = EModifierLeftCtrl | EModifierCtrl;
       
   390 		iConsole->OfferKeyEventL(key, EEventKey);
       
   391 		break;
       
   392 		}
       
   393 	case EGuiConsShow5Way:
       
   394 		{
       
   395 		iShouldDisplayOnscreenNav = !iShouldDisplayOnscreenNav;
       
   396 		if (iConsole) iConsole->SetDisplayFivewayNav(iShouldDisplayOnscreenNav);
       
   397 		TRAP_IGNORE(iIniFile->SetL(KIniShowNav, iShouldDisplayOnscreenNav); iIniFile->WriteL());
       
   398 		break;
       
   399 		}
       
   400 	case EGuiConsLongPressSwitchesTabs:
       
   401 		{
       
   402 		TBool capturing = CKeyCapturer::GetCapturerL()->IsCaptured(EKeyLeftArrow);
       
   403 		SetCapturingLongPressesL(!capturing);
       
   404 		TRAP_IGNORE(iIniFile->SetL(KIniLongPress, !capturing); iIniFile->WriteL());
       
   405 		break;
       
   406 		}
       
   407 	case EGuiConsChooseFont:
       
   408 		{
       
   409 		// Note, we have to use the memory and file selection dialogs seperately
       
   410 		// so that we can choose things from C: outside of C:\Data
       
   411 		
       
   412 		// choose drive
       
   413 		// TODO see if we can do something better here, so we can also choose files from Z: and
       
   414 		// other drives
       
   415 		CAknMemorySelectionDialog::TMemory memory = CAknMemorySelectionDialog::EPhoneMemory;
       
   416 		if (!CAknMemorySelectionDialog::RunDlgLD(memory))
       
   417 			{
       
   418 			break;
       
   419 			}
       
   420 		TChar drive = (memory == CAknMemorySelectionDialog::EPhoneMemory) ? 'C' : 'E';
       
   421 		
       
   422 		TFileName* fn = new(ELeave)TFileName;
       
   423 		CleanupStack::PushL(fn);
       
   424 		fn->Append(drive);
       
   425 		fn->Append(_L(":\\"));
       
   426 		CAknFileSelectionDialog* fileSel = CAknFileSelectionDialog::NewL(ECFDDialogTypeSelect);
       
   427 		CleanupStack::PushL(fileSel);
       
   428 		fileSel->SetDefaultFolderL(_L("\\"));
       
   429 		if (fileSel->ExecuteL(*fn))
       
   430 			{
       
   431 			
       
   432 			// verify that the file chosen is a valid font
       
   433 			CConsoleFont* font = NULL;
       
   434 			TRAPD(err, font = CConsoleFont::NewL(*fn));
       
   435 			if (err!=KErrNone)
       
   436 				{
       
   437 				DisplayMessageL(KInvalidFontMsgFmt, fn, err);
       
   438 				}
       
   439 			else
       
   440 				{
       
   441 				TRAP_IGNORE(iIniFile->SetL(KIniFont, *fn); iIniFile->WriteL());
       
   442 				DisplayMessageL(KFontPreview);
       
   443 				}
       
   444 			delete font;
       
   445 			}
       
   446 		CleanupStack::PopAndDestroy(2, fn); // fn, fileSel
       
   447 		break;
       
   448 		}
       
   449 	
       
   450 	case EGuiConsResetFont:
       
   451 		iIniFile->RemoveValueL(KIniFont);
       
   452 		iIniFile->WriteL();
       
   453 		break;
       
   454 		
       
   455 	default:		
       
   456 		break;
       
   457 		}
       
   458 
       
   459 	if (aCommand >= ETabStart)
       
   460 		{
       
   461 		TInt tabIdx = aCommand - ETabStart;
       
   462 		SwitchTabL(tabIdx);
       
   463 		}
       
   464 	else if (aCommand >= ECommandStart)
       
   465 		{
       
   466 		TInt cmdIdx = aCommand - ECommandStart;
       
   467 		InputTextL(*iCommands[cmdIdx]->iValue);
       
   468 		}
       
   469 	}
       
   470 	
       
   471 void CGuiConsAppUi::NewFshellProcessL(const TDesC& aArgs)
       
   472 	{
       
   473 	RBuf args;
       
   474 	args.Create(aArgs.Length()+1+KNewProcArgs().Length());
       
   475 	CleanupClosePushL(args);
       
   476 	args.Append(KNewProcArgs);
       
   477 	args.Append(' ');
       
   478 	args.Append(aArgs);
       
   479 	
       
   480 	RProcess proc;
       
   481 	User::LeaveIfError(proc.Create(KNewProcName, args));
       
   482 	proc.Resume();
       
   483 	proc.Close();
       
   484 	CleanupStack::PopAndDestroy(&args);
       
   485 	}
       
   486 
       
   487 void CGuiConsAppUi::HandleStatusPaneSizeChange()
       
   488 	{
       
   489 	iAppView->SetRect( ClientRect() );
       
   490 	}
       
   491 	
       
   492 void CGuiConsAppUi::SetMenuItemDimmed(TInt aCommandId, CEikMenuPane* aMenuPane)
       
   493 	{
       
   494 	aMenuPane->SetItemDimmed(aCommandId, !CommandValidInCurrentState(aCommandId));
       
   495 	}
       
   496 	
       
   497 void CGuiConsAppUi::DynInitMenuPaneL(TInt aMenuId, CEikMenuPane* aMenuPane)
       
   498 	{
       
   499 	if (aMenuId == R_GUICONS_MENU)
       
   500 		{
       
   501 		SetMenuItemDimmed(EAknSoftkeyExit, aMenuPane);
       
   502 		for (TInt i = EGuiConsCommandFirst; i<EGuiConsCommandLast; ++i)
       
   503 			{
       
   504 			SetMenuItemDimmed(i, aMenuPane);
       
   505 			}
       
   506 		}
       
   507 	else if (aMenuId == R_GUICONS_TABS_SUBMENU)
       
   508 		{
       
   509 		for (TInt i = 0; i < iTabs.Count(); i++)
       
   510 			{
       
   511 			CConsoleState* state = iTabs[i];
       
   512 			CEikMenuPaneItem::SData menu;
       
   513 			menu.iCommandId = ETabStart + i;
       
   514 			menu.iCascadeId = 0;
       
   515 			menu.iFlags = 0;
       
   516 			menu.iText = state->iTitle;
       
   517 			aMenuPane->AddMenuItemL(menu);
       
   518 			}
       
   519 		}
       
   520 	else if (aMenuId == R_GUICONS_SETTINGS_SUBMENU)
       
   521 		{
       
   522 		TBool capturing = CKeyCapturer::GetCapturerL()->IsCaptured(EKeyLeftArrow);
       
   523 		aMenuPane->SetItemButtonState(EGuiConsLongPressSwitchesTabs, capturing ? EEikMenuItemSymbolOn : 0);
       
   524 		aMenuPane->SetItemButtonState(EGuiConsShow5Way, iShouldDisplayOnscreenNav ? EEikMenuItemSymbolOn : 0);
       
   525 		SetMenuItemDimmed(EGuiConsResetFont, aMenuPane);
       
   526 		}
       
   527 	else if (aMenuId == R_GUICONS_COMMANDS_SUBMENU)
       
   528 		{
       
   529 		for (TInt i = 0; i < iCommands.Count(); i++)
       
   530 			{
       
   531 			CCommandText* command = iCommands[i];
       
   532 			CEikMenuPaneItem::SData menu;
       
   533 			menu.iCommandId = ECommandStart + i;
       
   534 			menu.iCascadeId = 0;
       
   535 			menu.iFlags = 0;
       
   536 			menu.iText = command->iName;
       
   537 			aMenuPane->AddMenuItemL(menu);
       
   538 			}
       
   539 		}
       
   540 	}
       
   541 	
       
   542 TBool CGuiConsAppUi::CommandValidInCurrentState(TInt aCommandId)
       
   543 	{
       
   544 	switch (aCommandId)
       
   545 		{
       
   546 	case EEikCmdExit:
       
   547 	case EAknSoftkeyExit:
       
   548 		return (iState == ENoConsole || iState == EConsoleClosed);
       
   549 	case EGuiConsFocusConsole:
       
   550 		return (iState == EEdwinActive);
       
   551 	case EGuiConsFocusEdwin:
       
   552 		return (iState == EConsoleActive);
       
   553 	case EGuiConsPageUp:
       
   554 	case EGuiConsPageDown:
       
   555 	case EGuiConsHome:
       
   556 	case EGuiConsEnd:
       
   557 		return (iState == EEdwinActive) || (iState == EConsoleActive) || (iState == EConsoleClosed);
       
   558 	case EGuiConsCloseConsole:
       
   559 		return (iState == EConsoleClosed);
       
   560 	case EGuiConsNewConsole:
       
   561 		return ETrue; //(iState == ENoConsole);
       
   562 	case EGuiConsSwitchTab:
       
   563 		return iTabs.Count() > 1;
       
   564 	case EGuiConsCtrlC:
       
   565 		return (iState == EEdwinActive) || (iState == EConsoleActive);
       
   566 	case EGuiConsNavigationSubMenu:
       
   567 		return CommandValidInCurrentState(EGuiConsHome) || CommandValidInCurrentState(EGuiConsEnd) || CommandValidInCurrentState(EGuiConsPageUp) || CommandValidInCurrentState(EGuiConsPageDown);
       
   568 	case EGuiConsCommandsSubMenu:
       
   569 		return (iState == EEdwinActive) || (iState == EConsoleActive);
       
   570 	case EGuiConsResetFont:
       
   571 		return iIniFile->GetSetting(KIniFont)->IsSet();
       
   572 	default:
       
   573 		return ETrue;
       
   574 		}
       
   575 	}
       
   576 
       
   577 
       
   578 void CBlankControl::Draw(const TRect& aRect) const
       
   579 	{
       
   580 	CWindowGc& gc = SystemGc();
       
   581 	gc.SetPenStyle(CGraphicsContext::ENullPen);
       
   582 	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   583 	gc.SetBrushColor(TRgb(0x80, 0x80, 0x80));
       
   584 	gc.DrawRect(aRect);
       
   585 	}
       
   586 
       
   587 void CGuiConsAppUi::TabChangedL(TInt aIndex)
       
   588 	{
       
   589 	//__DEBUGGER();
       
   590 	CConsoleState* newTabState = (CConsoleState*)(TAny*)iTabGroup->TabIdFromIndex(aIndex);
       
   591 	CConsoleControl* newTab = newTabState->iControl;
       
   592 	CConsoleControl* oldTab = iConsole;
       
   593 	iConsole = newTab;
       
   594 
       
   595 	// Update what's on screen, and what has keyboard focus
       
   596 	if (oldTab)
       
   597 		{
       
   598 		// Could be null, maybe
       
   599 		RemoveFromStack(oldTab);
       
   600 		oldTab->MakeVisible(EFalse);
       
   601 		}
       
   602 	if (newTab)
       
   603 		{
       
   604 		// Could be null if we haven't implemented this tab yet
       
   605 		//AppUi()->AddToStackL(*this, newTab, ECoeStackPriorityDefault-1); // One less than the tab bar to ensure we don't unintentionally consume left and right arrows
       
   606 		AddToStackL(newTab);
       
   607 		newTab->MakeVisible(ETrue);
       
   608 		newTab->SetDisplayFivewayNav(iShouldDisplayOnscreenNav); // Make sure this is sync'd
       
   609 		}
       
   610 	iAppView->ClearLargeControl();
       
   611 	iAppView->SetLargeControlL(iConsole);
       
   612 	if (newTabState->iConsoleClosed)
       
   613 		{
       
   614 		SetStateL(EConsoleClosed);
       
   615 		}
       
   616 	else
       
   617 		{
       
   618 		SetStateToPreferredL();
       
   619 		}
       
   620 	ConsoleTitleChangedL(iConsole, newTabState->iTitle);
       
   621 	}
       
   622 
       
   623 CConsoleState* CGuiConsAppUi::StateForConsole(const CConsoleControl* aControl)
       
   624 	{
       
   625 	for (TInt i = 0; i < iTabs.Count(); i++)
       
   626 		{
       
   627 		if (iTabs[i]->iControl == aControl) return iTabs[i];
       
   628 		}
       
   629 	return NULL;
       
   630 	}
       
   631 
       
   632 void CGuiConsAppUi::SetStateToPreferredL()
       
   633 	{
       
   634 	if (iEdwinWasLastRunningState)
       
   635 		{
       
   636 		SetStateL(EEdwinActive);
       
   637 		}
       
   638 	else
       
   639 		{
       
   640 		SetStateL(EConsoleActive);
       
   641 		}
       
   642 	}
       
   643 
       
   644 void CGuiConsAppUi::DeleteCurrentConsoleL()
       
   645 	{
       
   646 	TInt found = KErrNotFound;
       
   647 	for (TInt i = 0; i < iTabs.Count(); i++)
       
   648 		{
       
   649 		if (iTabs[i]->iControl == iConsole)
       
   650 			{
       
   651 			found = i;
       
   652 			break;
       
   653 			}
       
   654 		}
       
   655 	GUICONS_ASSERT(found >= 0);
       
   656 
       
   657 	CConsoleState* state = iTabs[found];
       
   658 	iTabs.Remove(found);
       
   659 	CleanupStack::PushL(state); // Keep the state around until we've finished switching, just in case
       
   660 	iTabGroup->DeleteTabL((TInt)state);
       
   661 	if (found >= iTabs.Count()) found = iTabs.Count()-1;
       
   662 	if (found >= 0)
       
   663 		{
       
   664 		SwitchTabL(found);
       
   665 		}
       
   666 	else
       
   667 		{
       
   668 		iConsole = NULL;
       
   669 		iAppView->ClearLargeControl();
       
   670 		SetStateL(ENoConsole);
       
   671 		ConsoleTitleChangedL(NULL, KNullDesC);
       
   672 		}
       
   673 	CleanupStack::PopAndDestroy(state);
       
   674 	}
       
   675 
       
   676 void CGuiConsAppUi::CapturedKeyPressedL(TInt aIdentifier)
       
   677 	{
       
   678 	TKeyEvent key;
       
   679 	key.iCode = aIdentifier;
       
   680 	key.iRepeats = 0;
       
   681 	key.iModifiers = 0;
       
   682 	switch (aIdentifier)
       
   683 		{
       
   684 		case EKeyLeftArrow:
       
   685 			key.iScanCode = EStdKeyLeftArrow;
       
   686 			iTabGroup->OfferKeyEventL(key, EEventKey);
       
   687 			break;
       
   688 		case EKeyRightArrow:
       
   689 			key.iScanCode = EStdKeyRightArrow;
       
   690 			iTabGroup->OfferKeyEventL(key, EEventKey);
       
   691 			break;
       
   692 		default:
       
   693 			break;
       
   694 		}
       
   695 	}
       
   696 
       
   697 void CGuiConsAppUi::SwitchTabL(TInt aTabIdx)
       
   698 	{
       
   699 	iTabGroup->SetActiveTabByIndex(aTabIdx);
       
   700 	TabChangedL(aTabIdx);
       
   701 	}
       
   702 
       
   703 void CGuiConsAppUi::SetCapturingLongPressesL(TBool aEnable)
       
   704 	{
       
   705 	CKeyCapturer::GetCapturerL()->RemoveCapture(this, EKeyLeftArrow);
       
   706 	CKeyCapturer::GetCapturerL()->RemoveCapture(this, EKeyRightArrow);
       
   707 	if (aEnable)
       
   708 		{
       
   709 		CKeyCapturer::GetCapturerL()->AddLongCaptureL(this, EKeyLeftArrow, EKeyLeftArrow, EStdKeyLeftArrow);
       
   710 		CKeyCapturer::GetCapturerL()->AddLongCaptureL(this, EKeyRightArrow, EKeyRightArrow, EStdKeyRightArrow);
       
   711 		}
       
   712 	}
       
   713 
       
   714 void CGuiConsAppUi::InputTextL(const TDesC& aText)
       
   715 	{
       
   716 	if (iState == EEdwinActive)
       
   717 		{
       
   718 		iAppView->SetEditorTextL(&aText);
       
   719 		}
       
   720 	else
       
   721 		{
       
   722 		iConsole->InjectKeysL(aText);
       
   723 		}
       
   724 	}
       
   725 
       
   726 void CGuiConsAppUi::LoadCommandsL()
       
   727 	{
       
   728 	_LIT(KCommandsDir, "\\system\\console\\commands\\");
       
   729 	_LIT(KCommandNameMatch, "*.txt");
       
   730 	TFindFile findfile(iEikonEnv->FsSession());
       
   731 	CDir* list = NULL;
       
   732 	TInt found = findfile.FindWildByDir(KCommandsDir, KCommandNameMatch, list);
       
   733 	TFileName filename;
       
   734 	while (found == KErrNone)
       
   735 		{
       
   736 		filename = TParsePtrC(findfile.File()).DriveAndPath();
       
   737 		TInt dirLen = filename.Length();
       
   738 		CleanupStack::PushL(list);
       
   739 		for (TInt i = 0; i < list->Count(); i++)
       
   740 			{
       
   741 			const TEntry& entry = (*list)[i];
       
   742 			const TDesC& name = entry.iName;
       
   743 			filename.SetLength(dirLen);
       
   744 			filename.Append(name);
       
   745 			TLex lex(name);
       
   746 			TInt order = 0;
       
   747 			lex.Val(order);
       
   748 			if (lex.Peek() == '-') lex.Inc();
       
   749 			CCommandText* cmd = new(ELeave) CCommandText(order);
       
   750 			CleanupStack::PushL(cmd);
       
   751 			cmd->iName = TParsePtrC(lex.Remainder()).Name().Left(cmd->iName.MaxLength());
       
   752 			LtkUtils::ReplaceText(cmd->iName, _L("_"), _L(" "));
       
   753 			cmd->iValue = HBufC::NewMaxL(entry.iSize);
       
   754 			TPtr8 narrow((TUint8*)cmd->iValue->Ptr(), 0, entry.iSize*2);
       
   755 			User::LeaveIfError(iEikonEnv->FsSession().ReadFileSection(filename, 0, narrow, entry.iSize));
       
   756 			narrow.Expand();
       
   757 			iCommands.InsertInOrderAllowRepeatsL(cmd, TLinearOrder<CCommandText>(&CCommandText::Order));
       
   758 			CleanupStack::Pop(cmd);
       
   759 			}
       
   760 		CleanupStack::PopAndDestroy(list);
       
   761 		found = findfile.FindWild(list);
       
   762 		}
       
   763 	}
       
   764 	
       
   765 _LIT(KShellEchoArgsStart, "--exec \"echo --wrap ^\"");
       
   766 _LIT(KShellEchoArgsEnd, "^\"\"");
       
   767 _LIT(KEsc, "^^");
       
   768 
       
   769 TBool NeedsEscape(TChar aChar)
       
   770 	{
       
   771 	return (aChar == '"');
       
   772 	}
       
   773 
       
   774 void CGuiConsAppUi::DisplayMessageL(TRefByValue<const TDesC> aFmt, ...)
       
   775 	{
       
   776 	VA_LIST list;
       
   777 	VA_START(list, aFmt);
       
   778 
       
   779 	IoUtils::CTextBuffer* msgBuf = IoUtils::CTextBuffer::NewLC(0x20);
       
   780 	msgBuf->AppendFormatListL(aFmt, list);	
       
   781 	
       
   782 	IoUtils::CTextBuffer* buf = IoUtils::CTextBuffer::NewLC(0x20);
       
   783 	
       
   784 	buf->AppendL(KShellEchoArgsStart);
       
   785 	const TDesC& msg(msgBuf->Descriptor());
       
   786 	
       
   787 	for (TInt i=0; i<msg.Length(); ++i)
       
   788 		{
       
   789 		if (msg[i] == '\n')
       
   790 			{
       
   791 			buf->AppendL(KEsc);
       
   792 			buf->AppendL('n');
       
   793 			}
       
   794 		else if (msg[i] == '\r')
       
   795 			{
       
   796 			buf->AppendL(KEsc);
       
   797 			buf->AppendL('r');
       
   798 			}
       
   799 		else if (NeedsEscape(msg[i]))
       
   800 			{
       
   801 			buf->AppendL(KEsc);
       
   802 			buf->AppendL(msg[i]);
       
   803 			}
       
   804 		else
       
   805 			{
       
   806 			buf->AppendL(msg[i]);
       
   807 			}
       
   808 		}
       
   809 	buf->AppendL(KShellEchoArgsEnd);
       
   810 	
       
   811 	NewFshellProcessL(buf->Descriptor());
       
   812 	CleanupStack::PopAndDestroy(2, msgBuf);
       
   813 	}
       
   814 
       
   815 // CConsoleState
       
   816 
       
   817 CConsoleState::CConsoleState(CConsoleControl* aControl)
       
   818 : iControl(aControl), iConsoleClosed(EFalse)
       
   819 	{
       
   820 	}
       
   821 
       
   822 CConsoleState::~CConsoleState()
       
   823 	{
       
   824 	delete iControl;
       
   825 	}
       
   826 
       
   827 // CCommandText
       
   828 
       
   829 CCommandText::CCommandText(TInt aOrder)
       
   830 	: iOrder(aOrder)
       
   831 	{}
       
   832 
       
   833 TInt CCommandText::Order(const CCommandText& aLeft, const CCommandText& aRight)
       
   834 	{
       
   835 	return aLeft.iOrder - aRight.iOrder;
       
   836 	}
       
   837 
       
   838 CCommandText::~CCommandText()
       
   839 	{
       
   840 	delete iValue;
       
   841 	}