void
CMSourceDirector::ClearDisplay()
{
	itsTable->ClearLineNumbers();
	itsText->SetText("");
	itsFileDisplay->SetText("");
	itsCurrentFile.Clear();
	itsCurrentFn.Clear();
	UpdateWindowTitle(NULL);

	UpdateFileType();
}
void
CMSourceDirector::ClearDisplay()
{
	CMLineAddressTable* table = dynamic_cast<CMLineAddressTable*>(itsTable);
	if (table != NULL)
		{
		table->ClearLineNumbers();
		}
	itsText->SetText("");
	itsFileDisplay->SetText("");
	itsCurrentFile.Clear();
	itsCurrentFn.Clear();
	UpdateWindowTitle(NULL);

	UpdateFileType();
}
void
CMSourceDirector::DisplayFile
	(
	const JCharacter*	fileName,
	const JIndex		lineNumber,
	const JBoolean		markLine
	)
{
	assert( !JStringEmpty(fileName) );

	if (!itsCurrentFile.IsEmpty() && JSameDirEntry(fileName, itsCurrentFile))
		{
		DisplayLine(lineNumber, markLine);
		UpdateWindowTitle(NULL);
		}
	else if (JFileReadable(fileName))
		{
		itsCurrentFile = fileName;

		JTextEditor::PlainTextFormat format;
		itsText->ReadPlainText(itsCurrentFile, &format);

		// Assembly must not end with newline, so we have to enforce this for
		// source code, too.

		const JString& text = itsText->GetText();
		JIndex i = text.GetLength();
		while (text.GetCharacter(i) == '\n')
			{
			i--;
			}
		if (i < text.GetLength())
			{
			itsText->SetSelection(i+1, text.GetLength());
			itsText->DeleteSelection();
			}
		itsText->SetCaretLocation(1);

		DisplayLine(lineNumber, markLine);
		itsFileDisplay->SetText(itsCurrentFile);

		UpdateFileType();
		UpdateWindowTitle(NULL);
		}
}
CMSourceDirector::CMSourceDirector
	(
	CMCommandDirector*	commandDir,
	const Type			type
	)
	:
	JXWindowDirector(JXGetApplication()),
	itsType(type),
	itsSrcMainCmd(NULL),
	itsGetAssemblyCmd(NULL)
{
	assert( IsMainSourceWindow() );
	CMSourceViewDirectorX(commandDir);

	if (itsType == kMainSourceType)
		{
		itsSrcMainCmd = itsLink->CreateDisplaySourceForMain(this);
		}
	else if (itsType == kMainAsmType)
		{
		UpdateFileType();	// only need to do this once
		}
}
void
CMSourceDirector::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (IsMainSourceWindow() &&
		sender == itsLink && message.Is(CMLink::kDebuggerStarted))
		{
		ClearDisplay();
		}
	else if (sender == itsLink && message.Is(CMLink::kPrepareToLoadSymbols))
		{
		if (!itsCurrentFile.IsEmpty())		// reload file, in case it changed
			{
			const JString fileName = itsCurrentFile;
			const JIndex lineIndex = itsText->HasSelection() ?
									 itsText->GetLineForChar(itsText->GetInsertionIndex()) :
									 itsTable->GetCurrentLine();
			itsCurrentFile.Clear();			// force reload
			DisplayFile(fileName, lineIndex);
			}
		}
	else if (IsMainSourceWindow() &&
			 sender == itsLink && message.Is(CMLink::kSymbolsLoaded))
		{
		const CMLink::SymbolsLoaded* info =
			dynamic_cast<const CMLink::SymbolsLoaded*>(&message);
		assert( info != NULL );
		UpdateWindowTitle(info->GetProgramName());
		}
	else if (IsMainSourceWindow() &&
			 sender == itsLink && message.Is(CMLink::kProgramStopped))
		{
		const CMLink::ProgramStopped* info =
			dynamic_cast<const CMLink::ProgramStopped*>(&message);
		assert( info != NULL);
		const CMLocation* loc;
		const JBoolean hasFile = info->GetLocation(&loc);
		if (itsType == kMainSourceType && hasFile)
			{
			DisplayFile(loc->GetFileName(), loc->GetLineNumber());
			}
		else if (itsType == kMainAsmType &&
				 !loc->GetFunctionName().IsEmpty() &&
				 !loc->GetMemoryAddress().IsEmpty())
			{
			DisplayDisassembly(*loc);
			}
		else if (itsType == kMainAsmType)
			{
			// wait for kProgramStopped2
			}
		else
			{
			#ifdef _J_OLD_OSX
			itsTable->SetCurrentLine(0);	// we get blank location the first time
			#else
			ClearDisplay();
			(CMGetCommandDirector())->GetStackDir()->Activate();
			#endif
			}
		}
	else if (itsType == kMainAsmType && sender == itsLink &&
			 message.Is(CMLink::kProgramStopped2))
		{
		const CMLink::ProgramStopped2* info =
			dynamic_cast<const CMLink::ProgramStopped2*>(&message);
		assert( info != NULL);
		const CMLocation* loc;
		info->GetLocation(&loc);
		if (!loc->GetFunctionName().IsEmpty() &&
			!loc->GetMemoryAddress().IsEmpty())
			{
			DisplayDisassembly(*loc);
			}
		else
			{
			ClearDisplay();
			}
		}

	else if (sender == itsLink &&
			 (message.Is(CMLink::kProgramFinished) ||
			  message.Is(CMLink::kCoreCleared)     ||
			  message.Is(CMLink::kDetachedFromProcess)))
		{
		if (itsSrcMainCmd != NULL)
			{
			itsSrcMainCmd->CMCommand::Send();
			}
		else if (itsType == kMainAsmType)
			{
			ClearDisplay();
			}
		}

	else if (sender == itsFileMenu && message.Is(JXMenu::kNeedsUpdate))
		{
		UpdateFileMenu();
		}
	else if (sender == itsFileMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandleFileMenu(selection->GetIndex());
		}

	else if (sender == itsDebugMenu && message.Is(JXMenu::kNeedsUpdate))
		{
		itsCommandDir->UpdateDebugMenu(itsDebugMenu, itsText, NULL);
		}
	else if (sender == itsDebugMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		itsCommandDir->HandleDebugMenu(itsDebugMenu, selection->GetIndex(), itsText, NULL);
		}

	else if (sender == itsPrefsMenu && message.Is(JXMenu::kNeedsUpdate))
		{
		UpdatePrefsMenu();
		}
	else if (sender == itsPrefsMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandlePrefsMenu(selection->GetIndex());
		}

	else if (sender == itsHelpMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast<const JXMenu::ItemSelected*>(&message);
		assert( selection != NULL );
		HandleHelpMenu(selection->GetIndex());
		}

	else if (sender == CMGetPrefsManager() && message.Is(CMPrefsManager::kFileTypesChanged))
		{
		UpdateFileType();
		}
	else if (sender == CMGetPrefsManager() && message.Is(CMPrefsManager::kCustomCommandsChanged))
		{
		itsCommandDir->AdjustDebugMenu(itsDebugMenu);
		}

	else
		{
		JXWindowDirector::Receive(sender,message);
		}
}