// $Revision: 1.2 $ // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. // TilesPresenter.C // Included for archive extraction #ifndef Taligent_LOCALIZATION #include #endif // Included for archive extraction #ifndef Taligent_ARCHIVE #include #endif // Include for Presentation Framework commands #ifndef Taligent_GUICOMPOUNDDOCUMENTCOMMAND #include #endif // Include for TMomentaryMenuItem, TMenu #ifndef Taligent_MENUCONTROLS #include #endif #ifndef TaligentSample_TILESPRESENTER #include "TilesPresenter.h" #endif #ifndef TaligentSample_TILESMODEL #include "TilesModel.h" #endif #ifndef TaligentSample_TILESCOMMANDS #include "TilesCommands.h" #endif TaligentTypeExtensionMacro (TTilesPresenter); TTilesPresenter::TTilesPresenter() : fArchiveRead(false) {} // The constructor for TTilesPresenter must take a TGUIBundle which it passes on // to the constructor for TGUIPresenterFor. TTilesPresenter::TTilesPresenter(const TGUIBundle& bundle) : TGUIPresenterFor(bundle), fArchiveRead(false) { } TTilesPresenter::~TTilesPresenter() { } void TTilesPresenter::HandleMenuActivate(TMenu& theMainMenu ) { TGUIPresenterFor::HandleMenuActivate(theMainMenu); #ifdef USE_ARCHIVES // USE_ARCHIVES version extracts the menu from the archive and relies on // Actions sent from the archived menus to activate commands. // The fArchivedRead flag is set to false in the constructors. Set to true before // attempting to read from the archive. If an exceptions occurs (mostly likely the archive // is missing), no further attempts will be made to read the archive. if (!fArchiveRead) { fArchiveRead = true; if (!fMenuHolder) fMenuHolder = CopyArchivedMenu("Menu"); } #else // Non-USE_ARCHIVES version builds the menu programmatically and relies on // command control states to link menu selections to commands. if (!fMenuHolder) { TMomentaryMenuItem* item; TMenu* tilesMenu; // This menu will become the Tiles menu tilesMenu = new TMenu; tilesMenu->SetControlLayout(MControl::kTopToBottom); tilesMenu->SetItemLayout( MControl::kLeftToRight ); // First create a menu item labeled Red item = new TMomentaryMenuItem(); item->AdoptLabel(new TTextLabel(TStandardText("Red"))); // Create a "red" change color control state with the GUIBundle, an undo/redo // label and a command prototype. Have the menu item adopt the control state. // The menu item will call the control state's Select() function when the user // clicks on the menu item. // The control state's that change colors specify true in their constructor to // connect for notification when the current selection changes. The control states // to create tiles do not need to connect for this notification (the default). item->AdoptState(new TTilesControlState(GetGUIBundle(), TStandardText("Set To Red"), new TChangeColorCommand (TRGBColor(1,0,0)), true)); // Attach to the tilesMenu tilesMenu->AdoptLast(item); // Repeat for a menu item labeled Green item = new TMomentaryMenuItem(); item->AdoptLabel(new TTextLabel(TStandardText("Green"))); item->AdoptState(new TTilesControlState(GetGUIBundle(), TStandardText("Set To Green"), new TChangeColorCommand (TRGBColor(0,1,0)), true)); tilesMenu->AdoptLast(item); // Repeat for a menu item labeled Blue item = new TMomentaryMenuItem(); item->AdoptLabel(new TTextLabel(TStandardText("Blue"))); item->AdoptState(new TTilesControlState(GetGUIBundle(), TStandardText("Set To Blue"), new TChangeColorCommand (TRGBColor(0,0,1)), true)); tilesMenu->AdoptLast(item); // Build a menu item for creating a Rock tile item = new TMomentaryMenuItem(); item->AdoptLabel(new TTextLabel(TStandardText("Create Rock"))); item->AdoptState(new TTilesControlState(GetGUIBundle(), TStandardText("Create Rock"), new TCreateTileCommand (TTile::kRock, TGPoint(10,10), TGPoint(50,50)))); tilesMenu->AdoptLast(item); // Build a menu item for creating a Paper tile item = new TMomentaryMenuItem(); item->AdoptLabel(new TTextLabel(TStandardText("Create Paper"))); item->AdoptState(new TTilesControlState(GetGUIBundle(), TStandardText("Create Paper"), new TCreateTileCommand (TTile::kPaper, TGPoint(70,10), TGPoint(50,50)))); tilesMenu->AdoptLast(item); // Build a menu item for creating a Scissors tile item = new TMomentaryMenuItem(); item->AdoptLabel(new TTextLabel(TStandardText("Create Scissors"))); item->AdoptState(new TTilesControlState(GetGUIBundle(), TStandardText("Create Scissors"), new TCreateTileCommand (TTile::kScissors, TGPoint(130,10), TGPoint(50,50)))); tilesMenu->AdoptLast(item); fMenuHolder = new TSubMenuItem (tilesMenu, new TTextLabel(TStandardText("Tiles"))); } #endif // USE_ARCHIVES fMenuHolder.ActivateAfter(theMainMenu, kGUIPresenterDomainID, kEditMenu); } void TTilesPresenter::HandleMenuDeactivate(TMenu& theMainMenu ) { if (fMenuHolder) fMenuHolder.Deactivate(); TGUIPresenterFor::HandleMenuDeactivate(theMainMenu); } #ifdef USE_ARCHIVES // The following routines are used to extract the menu from the archive and to process // the actions sent out by the archived menus. // Extract the menu by name from the default archive (i.e., the archive named the same as // the shared library). TMenu* TTilesPresenter::CopyArchivedMenu(const char* key) const { TDeleterFor defaultArchive = CopyDefaultArchive(); TArchiveEnvelope menu(defaultArchive); return menu.CopyObject(TStandardText(key)); } // Open the archive that is named the same as the shared library TArchive* TTilesPresenter::CopyDefaultArchive() const { TArchive* defaultArchive; try { defaultArchive = TArchive::CopyArchiveForSharedLibrary(); Assertion(defaultArchive != NIL); } catch (const TStandardException& e) { GetGUIBundle()->GetGUIMessageLog()->ReportException(e, TStandardText("TTilesPresenter::CreateDefaultArchive - Can't open the archive \"TilesTutorialLib\"")); throw; } return defaultArchive; } // ID's for enabling/disabling menu items. const TMenuItemID TTilesPresenter::kRedMenuItem = 1; const TMenuItemID TTilesPresenter::kGreenMenuItem = 2; const TMenuItemID TTilesPresenter::kBlueMenuItem = 3; // Process the actions sent out by the menu. Similar to an event loop. bool TTilesPresenter::HandleMenuAction(TMenuAction& action) { bool result = true; TStandardText message(action.GetMessage()); if (message == TStandardText("RedAction")) { DoCommand(new TChangeColorCommand (TRGBColor(1,0,0)), TStandardText("Set To Red")); } else if (message == TStandardText("GreenAction")) { DoCommand(new TChangeColorCommand (TRGBColor(0,1,0)), TStandardText("Set To Green")); } else if (message == TStandardText("BlueAction")) { DoCommand(new TChangeColorCommand (TRGBColor(0,0,1)), TStandardText("Set To Blue")); } else if (message == TStandardText("RockAction")) { DoCommand(new TCreateTileCommand (TTile::kRock, TGPoint(10,10), TGPoint(50,50)), TStandardText("Create Rock")); } else if (message == TStandardText("PaperAction")) { DoCommand(new TCreateTileCommand (TTile::kPaper, TGPoint(70,10), TGPoint(50,50)), TStandardText("Create Paper")); } else if (message == TStandardText("ScissorsAction")) { DoCommand(new TCreateTileCommand (TTile::kScissors, TGPoint(130,10), TGPoint(50,50)), TStandardText("Create Scissors")); } else { result = TGUIPresenterFor::HandleMenuAction(action); } return result; } // Apply the provided command to the current selection. void TTilesPresenter::DoCommand(TCommandOn* command, const TStandardText& label) { TModelSelection* selection = ::CopyPointer(GetCurrentModelSelection()); TTilesSelection* tilesSelection = NIL; if (DynamicTypeInfo(*selection) == StaticTypeInfo(TTilesSelection)) { tilesSelection = (TTilesSelection *) selection; // Create a command binding to bind command and selection TTilesCommandBinding* commandBinding = new TTilesCommandBinding (command, tilesSelection, *GetGUIBundle(), label); // Access the document and have it adopt and do the command binding AdoptAndDo (commandBinding); } }; // Enable/disable appropriate menu items whenever the current selection changes. void TTilesPresenter::HandleCurrentSelectionChange(const TNotification& notification) { TGUIPresenterFor::HandleCurrentSelectionChange(notification); bool enabled = !GetCurrentModelSelection()->IsEmpty(); fMenuHolder.SetEnabled(enabled, kRedMenuItem); fMenuHolder.SetEnabled(enabled, kGreenMenuItem); fMenuHolder.SetEnabled(enabled, kBlueMenuItem); } #endif // USE_ARCHIVES // Use static to force instantiation of a class template from the template class. // Future C++ syntax will support a template keyword for doing this. static TGUIModelPresenterStationeryFor theStationery;