tbx  0.7.5
docmanager.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 /*
25  * DocManager.h
26  *
27  * Created on: 8 Oct 2010
28  * Author: alanb
29  */
30 
31 #ifndef TBX_DOCMANAGER_H_
32 #define TBX_DOCMANAGER_H_
33 
34 #include <list>
35 #include "../prequitlistener.h"
36 #include "../quit.h"
37 #include "../command.h"
38 #include "../loader.h"
39 
40 namespace tbx
41 {
42 namespace doc
43 {
44 
45 class Document;
46 
51 {
52  int _file_type;
53 public:
59  DocCreatorBase(int file_type) : _file_type(file_type) {}
60  virtual ~DocCreatorBase() {}
61 
65  int file_type() const {return _file_type;}
66 
72  virtual Document *create_document() = 0;
78  virtual void create_window(Document *doc) = 0;
79 
80 };
81 
86 template<class T, class W> class DocCreator : public DocCreatorBase
87 {
88 public:
94  DocCreator(int file_type) : DocCreatorBase(file_type) {}
95  virtual ~DocCreator() {}
96  virtual Document *create_document() {return new T();}
97  virtual void create_window(Document *doc) {new W((T *)doc);}
98 };
99 
100 
127 class DocManager :
128  public tbx::PreQuitListener
129 {
130 private:
131  static DocManager *_instance;
132  DocCreatorBase *_doc_creator;
133  std::list<Document *> _documents;
134 
138  class NewDocumentCommand : public tbx::Command
139  {
140  public:
141  virtual ~NewDocumentCommand() {}
142  virtual void execute();
143  } _new_doc_command;
144 
148  class QuitCommand : public tbx::Command
149  {
150  public:
151  virtual ~QuitCommand() {};
152  virtual void execute();
153  } _quit_command;
154 
158  class FileLoader : public tbx::Loader
159  {
160  public:
161  virtual ~FileLoader() {}
162  bool load_file(tbx::LoadEvent &event);
163  } _file_loader;
164 
165  void add_document(Document *doc);
166  void remove_document(Document *doc);
167  friend class Document;
168 
169 public:
170  DocManager(DocCreatorBase *doc_creator);
171  virtual ~DocManager();
172 
176  static DocManager *instance() {return _instance;}
180  DocCreatorBase *doc_creator() {return _doc_creator;}
181 
182  unsigned int modified_count() const;
183 
188  tbx::Command *new_document_command() {return &_new_doc_command;}
193  tbx::Command *quit_command() {return &_quit_command;}
194 
198  tbx::Loader *file_loader() {return &_file_loader;}
199 
203  int file_type() const {return _doc_creator->file_type();}
204 
205  bool load_files(int num_files, char *file_names[]);
206  bool load_file(const std::string &file_name, int estimated_size = -1, bool from_filer = true);
207 
208  virtual void pre_quit(tbx::PreQuitEvent &event);
209 
210 private:
214  class QuitPrompt :
215  public tbx::QuitQuitListener
216  {
217  tbx::Quit _quit;
218  tbx::QuitRestarter _restart;
219  public:
220  QuitPrompt(const tbx::QuitRestarter restart, int num_modified);
221  ~QuitPrompt();
222  virtual void quit_quit(const tbx::EventInfo &info);
223  };
224 
225 
226 };
227 
228 }
229 }
230 
231 #endif /* TBX_DOCMANAGER_H_ */
Class to manage all the documents.
Definition: docmanager.h:127
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
Listener for when a Quit is selected from the Quit dialog.
Definition: quit.h:194
Event passed to pre quit listeners.
Definition: prequitlistener.h:82
Helper classes to create a document based application.
DocCreatorBase * doc_creator()
Get the object that creates documents and their main window.
Definition: docmanager.h:180
Class to allow a quit cancelled during the prequit message to be restarted.
Definition: prequitlistener.h:45
Base class for commands in tbx.
Definition: command.h:36
Listen for the pre quit message from the desktop.
Definition: prequitlistener.h:109
int file_type() const
Get the document file type.
Definition: docmanager.h:203
int file_type() const
Return the file type handled by this document creator.
Definition: docmanager.h:65
virtual void create_window(Document *doc)
Create the window used to display the given document.
Definition: docmanager.h:97
virtual void create_window(Document *doc)=0
Create the window used to display the given document.
Base class for a document.
Definition: document.h:52
Templated class to provide information on a documents file type and how to create the document and it...
Definition: docmanager.h:86
tbx::Loader * file_loader()
Return a loader to load a document into a new window.
Definition: docmanager.h:198
static DocManager * instance()
Get single instance of the document manager.
Definition: docmanager.h:176
tbx::Command * quit_command()
Return a command that can be executed to quit the application showing a Quit dialogue if necessary...
Definition: docmanager.h:193
Class to handle file/data loading from the filer or an external application.
Definition: loader.h:179
tbx::Command * new_document_command()
Return a command that can be executed to create a new document with its window.
Definition: docmanager.h:188
DocCreator(int file_type)
Construct document creator for a file type.
Definition: docmanager.h:94
Class to provide information on a toolbox event.
Definition: eventinfo.h:48
virtual Document * create_document()=0
Override this method to create the document object.
Class with details of a file load operation.
Definition: loader.h:41
Class to show a toolbox Quit object that gives the user an option to Quit or Cancel the application...
Definition: quit.h:60
virtual Document * create_document()
Override this method to create the document object.
Definition: docmanager.h:96
Base class to help create a document and a window to show it in.
Definition: docmanager.h:50
DocCreatorBase(int file_type)
Construct a document and window creator for a file type.
Definition: docmanager.h:59