00001 /* 00002 * FileFacet.h 00003 * BasicApp 00004 * 00005 * Created by Steen Parl on Tue Oct 29 2002. 00006 * Copyright (c) 2002 __MyCompanyName__. All rights reserved. 00007 * 00008 */ 00009 // Class for file handling 00010 00011 // This class handles file commands associated with a window, except open. 00012 // Open is implemented by the separate FileOpener class which creates the window. 00013 // Actual opening, reading, and writing is done by the user from the class deriving from 00014 // FileBase and BasicWindow. The original file opened by the user must be read-only. 00015 // When changes are made the file is copied (to a temp folder) and the user program is told 00016 // to open the copied ('dirty') file instead with both read and write priviledges. 00017 // 00018 // The template argument can be a text pane or window, or perhaps a basic window 00019 // with some file aspects. 00020 // 00021 // A Save command invokes the Save class which calls the save() member for the frontwindow. 00022 // save() returns if the file is not dirty. Otherwise, the original file is deleted and the 00023 // dirty file is copied to the original folder and renamed to the original name. 00024 // The file is then marked as not dirty. 00025 // 00026 // A SaveAs command invokes the SaveAs class which calls the startSaveAs() member 00027 // for the front window. 00028 // startSaveAs() selects the file to save (original or dirty) and installs the navigation 00029 // services callback function and starts the standard save as dialog. 00030 // When the user has selected the destination, the callback function calls completeSaveAs(). 00031 // completeSaveAs() gets the directory and file name from the callback function. 00032 // It then copies the file as directed. it deletes the dirty file, if any, and mark the data 00033 // as not dirty. 00034 // 00035 // A close event may come from the menu close command, the close button, or other command. 00036 // The close event calls the virtual close() window function. the user program 00037 // must derive from both the window class and from FileBase. It must also have a 00038 // close() function that calls the parent close() functions. 00039 // If dirty, we bring up a Save Changes sheet. 00040 // If the response is do not save we delete the dirty file 00041 // If the response is save we call the save() for the window 00042 // In all these case we mark it not dirty and dispose of the window through the 00043 // BasicWindow::close() member. 00044 // If the response is cancel we do nothing - cancelling the close 00045 // 00046 // We create a dirty file when a change is about to be made by calling prepChange(). 00047 // Only prepChange() can set dirty_ to true. 00048 // prepChange() returns if a dirty file has already been created. 00049 // The directory for the dirty file is selected from the system temporary folder 00050 // using the folder manager. This may be an invisible folder. This is better than 00051 // having a file in same directory, which would fail if the file is on a CD. 00052 // We copy the file to the directory and sets the file path. 00053 00054 #include <Carbon/Carbon.h> 00055 #pragma once 00056 #include <string> 00057 //#include "BasicWindow.h" 00058 00059 // 00060 // This is an abstract class, application specific operations are handled by 00061 // the derived class. 00062 // 00064 class FileBase 00065 { 00066 public: 00067 FileBase(const std::string& path); //< Constructor. 00068 virtual ~FileBase(){}; //< Destructor. 00069 std::string getFileName(); //< extract file name from path_. 00070 // - could use static function. 00071 virtual void open( const std::string& path)=0; //< make dirty path the current file used 00072 virtual bool isOpen()=0; //< Check if file is open. 00073 // virtual WindowRef getWindowRef()=0; // get access to the Mac Window 00074 virtual bool hasChanged() {return dirty_;}; //< Check if file has changed 00075 // virtual bool getOldFile( string& path ){path=path_; return true;}; 00076 virtual void setWindowTitle(const CFStringRef aTitle)=0; 00077 // virtual bool handleCommand( UInt32 command )=0;// for saveChanges dialog, must be called from window 00078 00079 virtual void prepChange(); //< Copy file to new dirty file 00080 00082 void closeFile(); 00083 00085 OSErr save(); 00086 00088 OSErr deleteDirtyFile(); 00089 00091 void startSaveChanges(); 00092 void startSaveAs(); //< create Nav dialog and install callback 00093 void completeSaveAs( NavReplyRecord *reply); //< respond to callback 00094 static OSErr stringPathToFSRef( const std::string& aPath, FSRef& aFileRef); 00095 static OSErr FSRefToStringPath( const FSRef& aFileRef, std::string& aPath); 00096 std::string path_; // The original file //dbg 00097 protected: 00099 void activated_(); 00101 void deActivated_(); 00102 bool dirty_; //< true if file changed 00103 std::string dirtyPath_; //< The file with changes 00104 private: 00105 virtual void closeWindow()=0; 00106 virtual WindowRef getWindowRef_()=0; 00107 OSErr handleNavUserAction( NavCBRecPtr callbackParms); 00108 static pascal void navEventCallback( NavEventCallbackMessage callbackSelector 00109 , NavCBRecPtr callbackParms 00110 , void* callbackUD); 00111 00112 NavEventUPP eventProcUPP_; 00113 FSRef fileToSave_; 00114 FileBase* property_; 00115 00116 }; 00117 00118 // File implementent application-independent operations for file-associated windows. 00119 // 00121 template <class W> 00122 class File : public W, public FileBase 00123 { 00124 public: 00125 File( const CFStringRef inNib 00126 , const CFStringRef inName, const std::string& path ); // window 00127 // File( const CFStringRef inNib 00128 // , const CFStringRef inName, 00129 // , const ControlID paneID, const std::string& path ); // window 00130 // File( const WindowRef window, const ControlID paneID, const std::string& path ); // pane 00131 virtual ~File(); 00133 virtual void closeWindow(); 00134 // virtual bool handleCommand( UInt32 command );// for saveChanges dialog, must be called from window 00135 00137 virtual void close(); 00138 00139 protected: 00141 void activated(); 00143 void deActivated(); 00144 private: 00145 virtual WindowRef getWindowRef_(); //< get the WindowRef parameter 00146 virtual void setWindowTitle(const CFStringRef aTitle); 00147 FileBase* property_; 00148 00149 };