# wxDocManager Class Reference
[Document/View Framework]

`#include <wx/docview.h>`

Inheritance diagram for wxDocManager:
[legend]

## Detailed Description

The wxDocManager class is part of the document/view framework supported by wxWidgets, and cooperates with the wxView, wxDocument and wxDocTemplate classes.

##

Library: wxCore

Category: Document/View Framework

See also:
  wxDocManager overview, wxDocument, wxView, wxDocTemplate, wxFileHistory

## Public Member Functions
| wxDocManager (long flags=0, bool initialize=true)
| Constructor.
virtual | ~wxDocManager ()
| Destructor.
virtual void | ActivateView (wxView *doc, bool activate=true)
| Sets the current view.
void | AddDocument (wxDocument *doc)
| Adds the document to the list of documents.
virtual void | AddFileToHistory (const wxString &filename)
| Adds a file to the file history list, if we have a pointer to an appropriate file menu.
void | AssociateTemplate (wxDocTemplate *temp)
| Adds the template to the document manager's template list.
wxDocTemplate * | FindTemplate (const wxClassInfo *classinfo)
| Search for a particular document template.
bool | CloseDocument (wxDocument *doc, bool force=false)
| Closes the specified document.
bool | CloseDocuments (bool force=true)
| Closes all currently opened documents.
virtual wxDocument * | CreateDocument (const wxString &path, long flags=0)
| Creates a new document.
wxDocument * | CreateNewDocument ()
| Creates an empty new document.
virtual wxView * | CreateView (wxDocument *doc, long flags=0)
| Creates a new view for the given document.
void | DisassociateTemplate (wxDocTemplate *temp)
| Removes the template from the list of templates.
virtual void | FileHistoryAddFilesToMenu ()
| Appends the files in the history list to all menus managed by the file history object.
virtual void | FileHistoryAddFilesToMenu (wxMenu *menu)
| Appends the files in the history list to the given menu only.
virtual void | FileHistoryLoad (const wxConfigBase &config)
| Loads the file history from a config object.
virtual void | FileHistoryRemoveMenu (wxMenu *menu)
| Removes the given menu from the list of menus managed by the file history object.
virtual void | FileHistorySave (wxConfigBase &resourceFile)
| Saves the file history into a config object.
virtual void | FileHistoryUseMenu (wxMenu *menu)
| Use this menu for appending recently-visited document filenames, for convenient access.
virtual wxDocTemplate * | FindTemplateForPath (const wxString &path)
| Given a path, try to find template that matches the extension.
wxDocument * | GetCurrentDocument () const
| Returns the document associated with the currently active view (if any).
virtual wxView * | GetCurrentView () const
| Returns the currently active view.
wxList & | GetDocuments ()
| Returns a reference to the list of documents.
virtual wxFileHistory * | GetFileHistory () const
| Returns a pointer to file history.
virtual size_t | GetHistoryFilesCount () const
| Returns the number of files currently stored in the file history.
wxString | GetLastDirectory () const
| Returns the directory last selected by the user when opening a file.
int | GetMaxDocsOpen () const
| Returns the number of documents that can be open simultaneously.
wxList & | GetTemplates ()
| Returns a reference to the list of associated templates.
virtual wxPreviewFrame * | CreatePreviewFrame (wxPrintPreviewBase *preview, wxWindow *parent, const wxString &title)
| Create the frame used for print preview.
virtual bool | Initialize ()
| Initializes data; currently just calls OnCreateFileHistory().
virtual wxString | MakeNewDocumentName ()
| Return a string containing a suitable default name for a new document.
virtual wxFileHistory * | OnCreateFileHistory ()
| A hook to allow a derived class to create a different type of file history.
void | OnFileClose (wxCommandEvent &event)
| Closes and deletes the currently active document.
void | OnFileCloseAll (wxCommandEvent &event)
| Closes and deletes all the currently opened documents.
void | OnFileNew (wxCommandEvent &event)
| Creates a document from a list of templates (if more than one template).
void | OnFileOpen (wxCommandEvent &event)
| Creates a new document and reads in the selected file.
void | OnFileRevert (wxCommandEvent &event)
| Reverts the current document by calling wxDocument::Revert() for the current document.
void | OnFileSave (wxCommandEvent &event)
| Saves the current document by calling wxDocument::Save() for the current document.
void | OnFileSaveAs (wxCommandEvent &event)
| Calls wxDocument::SaveAs() for the current document.
void | RemoveDocument (wxDocument *doc)
| Removes the document from the list of documents.
virtual wxDocTemplate * | SelectDocumentPath (wxDocTemplate **templates, int noTemplates, wxString &path, long flags, bool save=false)
| Under Windows, pops up a file selector with a list of filters corresponding to document templates.
virtual wxDocTemplate * | SelectDocumentType (wxDocTemplate **templates, int noTemplates, bool sort=false)
| Returns a document template by asking the user (if there is more than one template).
virtual wxDocTemplate * | SelectViewType (wxDocTemplate **templates, int noTemplates, bool sort=false)
| Returns a document template by asking the user (if there is more than one template), displaying a list of valid views.
void | SetLastDirectory (const wxString &dir)
| Sets the directory to be displayed to the user when opening a file.
void | SetMaxDocsOpen (int n)
| Sets the maximum number of documents that can be open at a time.
## Public Attributes
wxView * | m_currentView
| The currently active view.
int | m_defaultDocumentNameCounter
| Stores the integer to be used for the next default document name.
wxList | m_docs
| A list of all documents.
wxFileHistory * | m_fileHistory
| A pointer to an instance of wxFileHistory, which manages the history of recently-visited files on the File menu.
long | m_flags
| Stores the flags passed to the constructor.
int | m_maxDocsOpen
| Stores the maximum number of documents that can be opened before existing documents are closed.

List of all members.

## Constructor & Destructor Documentation

wxDocManager::wxDocManager | ( | long | flags = `0`,
|  | bool | initialize = `true` |
| ) |  |  |

Constructor.

Create a document manager instance dynamically near the start of your application before doing any document or view operations.

If initialize is true, the Initialize() function will be called to create a default history list object. If you derive from wxDocManager, you may wish to call the base constructor with false, and then call Initialize() in your own constructor, to allow your own Initialize() or OnCreateFileHistory functions to be called.

Parameters:

  | flags | Currently unused.
  | initialize | Indicates whether Initialize() should be called by this ctor.

virtual wxDocManager::~wxDocManager | ( |  | ) | `[virtual]`

Destructor.

## Member Function Documentation

virtual void wxDocManager::ActivateView | ( | wxView * | doc,
|  | bool | activate = `true` |
| ) |  |  | `[virtual]`

Sets the current view.

void wxDocManager::AddDocument | ( | wxDocument * | doc | ) |

Adds the document to the list of documents.

virtual void wxDocManager::AddFileToHistory | ( | const wxString & | filename | ) | `[virtual]`

Adds a file to the file history list, if we have a pointer to an appropriate file menu.

void wxDocManager::AssociateTemplate | ( | wxDocTemplate * | temp | ) |

Adds the template to the document manager's template list.

bool wxDocManager::CloseDocument | ( | wxDocument * | doc,
|  | bool | force = `false` |
| ) |  |  |

Closes the specified document.

If force is true, the document is closed even if it has unsaved changes.

Parameters:

  | doc | The document to close, must be non-NULL.
  | force | If true, close the document even if wxDocument::Close() returns false.

Returns:
  true if the document was closed or false if closing it was cancelled by user (only in force = false case).

bool wxDocManager::CloseDocuments | ( | bool | force = `true` | ) |

Closes all currently opened documents.

See also:
  CloseDocument()

virtual wxDocument* wxDocManager::CreateDocument | ( | const wxString & | path,
|  | long | flags = `0` |
| ) |  |  | `[virtual]`

Creates a new document.

This function can either create a document corresponding to a new file or to an already existing one depending on whether `wxDOC_NEW` is specified in the flags.

By default, this function asks the user for the type of document to open and the path to its file if it's not specified, i.e. if path is empty. Specifying `wxDOC_SILENT` flag suppresses any prompts and means that the path must be non-empty and there must be a registered document template handling the extension of this file, otherwise a warning message is logged and the function returns NULL. Notice that `wxDOC_SILENT` can be combined with `wxDOC_NEW`, however in this case the path must still be specified, even if the file with this path typically won't exist.

Finally notice that if this document manager was configured to allow only a limited number of simultaneously opened documents using SetMaxDocsOpen(), this function will try to close the oldest existing document if this number was reached before creating a new document. And if closing the old document fails (e.g. because it was vetoed by user), this function fails as well.

Parameters:

  | path | Path to a file or an empty string. If the path is empty, the user will be asked to select it (thus, this is incompatible with the use of `wxDOC_SILENT`). The file should exist unless flags includes `wxDOC_NEW`.
  | flags | By default, none. May include `wxDOC_NEW` to indicate that the new document corresponds to a new file and not an existing one and `wxDOC_SILENT` to suppress any dialogs asking the user about the file path and type.

Returns:
  a new document object or NULL on failure.

wxDocument* wxDocManager::CreateNewDocument | ( |  | ) |

Creates an empty new document.

This is equivalent to calling CreateDocument() with `wxDOC_NEW` flags and without the file name.

virtual wxPreviewFrame* wxDocManager::CreatePreviewFrame | ( | wxPrintPreviewBase * | preview,
|  | wxWindow * | parent,
|  | const wxString & | title |
| ) |  |  | `[virtual]`

Create the frame used for print preview.

This method can be overridden if you need to change the behaviour or appearance of the preview window. By default, a standard wxPreviewFrame is created.

Since:
  2.9.1

Parameters:

  | preview | The associated preview object.
  | parent | The parent window for the frame.
  | title | The suggested title for the print preview frame.

Returns:
  A new print preview frame, must not return NULL.

virtual wxView* wxDocManager::CreateView | ( | wxDocument * | doc,
|  | long | flags = `0` |
| ) |  |  | `[virtual]`

Creates a new view for the given document.

If more than one view is allowed for the document (by virtue of multiple templates mentioning the same document type), a choice of view is presented to the user.

void wxDocManager::DisassociateTemplate | ( | wxDocTemplate * | temp | ) |

Removes the template from the list of templates.

virtual void wxDocManager::FileHistoryAddFilesToMenu | ( | wxMenu * | menu | ) | `[virtual]`

Appends the files in the history list to the given menu only.

virtual void wxDocManager::FileHistoryAddFilesToMenu | ( |  | ) | `[virtual]`

Appends the files in the history list to all menus managed by the file history object.

virtual void wxDocManager::FileHistoryLoad | ( | const wxConfigBase & | config | ) | `[virtual]`

Loads the file history from a config object.

See also:
  wxConfigBase

virtual void wxDocManager::FileHistoryRemoveMenu | ( | wxMenu * | menu | ) | `[virtual]`

Removes the given menu from the list of menus managed by the file history object.

virtual void wxDocManager::FileHistorySave | ( | wxConfigBase & | resourceFile | ) | `[virtual]`

Saves the file history into a config object.

This must be called explicitly by the application.

See also:
  wxConfigBase

virtual void wxDocManager::FileHistoryUseMenu | ( | wxMenu * | menu | ) | `[virtual]`

Use this menu for appending recently-visited document filenames, for convenient access.

Calling this function with a valid menu pointer enables the history list functionality.

Note:
  You can add multiple menus using this function, to be managed by the file history object.

wxDocTemplate* wxDocManager::FindTemplate | ( | const wxClassInfo * | classinfo | ) |

Search for a particular document template.

Example:

               // creating a document instance of the specified document type:
               m_doc = (MyDoc*)docManager->FindTemplate(CLASSINFO(MyDoc))->
                            CreateDocument(wxEmptyString, wxDOC_SILENT);

Parameters:

  | classinfo | Class info of a document class for which a wxDocTemplate had been previously created.

Returns:
  Pointer to a wxDocTemplate, or NULL if none found.

Since:
  2.9.2

virtual wxDocTemplate* wxDocManager::FindTemplateForPath | ( | const wxString & | path | ) | `[virtual]`

Given a path, try to find template that matches the extension.

This is only an approximate method of finding a template for creating a document.

wxDocument* wxDocManager::GetCurrentDocument | ( |  | ) | const

Returns the document associated with the currently active view (if any).

virtual wxView* wxDocManager::GetCurrentView | ( |  | ) | const`[virtual]`

Returns the currently active view.

wxList& wxDocManager::GetDocuments | ( |  | ) |

Returns a reference to the list of documents.

virtual wxFileHistory* wxDocManager::GetFileHistory | ( |  | ) | const`[virtual]`

Returns a pointer to file history.

virtual size_t wxDocManager::GetHistoryFilesCount | ( |  | ) | const`[virtual]`

Returns the number of files currently stored in the file history.

wxString wxDocManager::GetLastDirectory | ( |  | ) | const

Returns the directory last selected by the user when opening a file.

Initially empty.

int wxDocManager::GetMaxDocsOpen | ( |  | ) | const

Returns the number of documents that can be open simultaneously.

wxList& wxDocManager::GetTemplates | ( |  | ) |

Returns a reference to the list of associated templates.

virtual bool wxDocManager::Initialize | ( |  | ) | `[virtual]`

Initializes data; currently just calls OnCreateFileHistory().

Some data cannot always be initialized in the constructor because the programmer must be given the opportunity to override functionality. If OnCreateFileHistory() was called from the constructor, an overridden virtual OnCreateFileHistory() would not be called due to C++'s 'interesting' constructor semantics. In fact Initialize() is called from the wxDocManager constructor, but this can be vetoed by passing false to the second argument, allowing the derived class's constructor to call Initialize(), possibly calling a different OnCreateFileHistory() from the default.

The bottom line: if you're not deriving from Initialize(), forget it and construct wxDocManager with no arguments.

virtual wxString wxDocManager::MakeNewDocumentName | ( |  | ) | `[virtual]`

Return a string containing a suitable default name for a new document.

By default this is implemented by appending an integer counter to the string unnamed but can be overridden in the derived classes to do something more appropriate.

virtual wxFileHistory* wxDocManager::OnCreateFileHistory | ( |  | ) | `[virtual]`

A hook to allow a derived class to create a different type of file history.

Called from Initialize().

void wxDocManager::OnFileClose | ( | wxCommandEvent & | event | ) |

Closes and deletes the currently active document.

void wxDocManager::OnFileCloseAll | ( | wxCommandEvent & | event | ) |

Closes and deletes all the currently opened documents.

void wxDocManager::OnFileNew | ( | wxCommandEvent & | event | ) |

Creates a document from a list of templates (if more than one template).

void wxDocManager::OnFileOpen | ( | wxCommandEvent & | event | ) |

Creates a new document and reads in the selected file.

void wxDocManager::OnFileRevert | ( | wxCommandEvent & | event | ) |

Reverts the current document by calling wxDocument::Revert() for the current document.

void wxDocManager::OnFileSave | ( | wxCommandEvent & | event | ) |

Saves the current document by calling wxDocument::Save() for the current document.

void wxDocManager::OnFileSaveAs | ( | wxCommandEvent & | event | ) |

Calls wxDocument::SaveAs() for the current document.

void wxDocManager::RemoveDocument | ( | wxDocument * | doc | ) |

Removes the document from the list of documents.

virtual wxDocTemplate* wxDocManager::SelectDocumentPath | ( | wxDocTemplate ** | templates,
|  | int | noTemplates,
|  | wxString & | path,
|  | long | flags,
|  | bool | save = `false` |
| ) |  |  | `[virtual]`

Under Windows, pops up a file selector with a list of filters corresponding to document templates.

The wxDocTemplate corresponding to the selected file's extension is returned.

On other platforms, if there is more than one document template a choice list is popped up, followed by a file selector.

This function is used in CreateDocument().

wxPerl Note: In wxPerl templates is a reference to a list of templates. If you override this method in your document manager it must return two values, eg:

            (doctemplate, path) = My::DocManager->SelectDocumentPath(...);

virtual wxDocTemplate* wxDocManager::SelectDocumentType | ( | wxDocTemplate ** | templates,
|  | int | noTemplates,
|  | bool | sort = `false` |
| ) |  |  | `[virtual]`

Returns a document template by asking the user (if there is more than one template).

This function is used in CreateDocument().

Parameters:

  | templates | Pointer to an array of templates from which to choose a desired template.
  | noTemplates | Number of templates being pointed to by the templates pointer.
  | sort | If more than one template is passed into templates, then this parameter indicates whether the list of templates that the user will have to choose from is sorted or not when shown the choice box dialog. Default is false.

wxPerl Note: In wxPerl templates is a reference to a list of templates.

virtual wxDocTemplate* wxDocManager::SelectViewType | ( | wxDocTemplate ** | templates,
|  | int | noTemplates,
|  | bool | sort = `false` |
| ) |  |  | `[virtual]`

Returns a document template by asking the user (if there is more than one template), displaying a list of valid views.

This function is used in CreateView(). The dialog normally will not appear because the array of templates only contains those relevant to the document in question, and often there will only be one such.

Parameters:

  | templates | Pointer to an array of templates from which to choose a desired template.
  | noTemplates | Number of templates being pointed to by the templates pointer.
  | sort | If more than one template is passed into templates, then this parameter indicates whether the list of templates that the user will have to choose from is sorted or not when shown the choice box dialog. Default is false.

wxPerl Note: In wxPerl templates is a reference to a list of templates.

void wxDocManager::SetLastDirectory | ( | const wxString & | dir | ) |

Sets the directory to be displayed to the user when opening a file.

Initially this is empty.

void wxDocManager::SetMaxDocsOpen | ( | int | n | ) |

Sets the maximum number of documents that can be open at a time.

By default, this is `INT_MAX`, i.e. the number of documents is unlimited. If you set it to 1, existing documents will be saved and deleted when the user tries to open or create a new one (similar to the behaviour of Windows Write, for example). Allowing multiple documents gives behaviour more akin to MS Word and other Multiple Document Interface applications.

## Member Data Documentation

wxView* wxDocManager::m_currentView

The currently active view.

int wxDocManager::m_defaultDocumentNameCounter

Stores the integer to be used for the next default document name.

wxList wxDocManager::m_docs

A list of all documents.

wxFileHistory * wxDocManager::m_fileHistory

A pointer to an instance of wxFileHistory, which manages the history of recently-visited files on the File menu.

The directory last selected by the user when opening a file.

long wxDocManager::m_flags

Stores the flags passed to the constructor.

int wxDocManager::m_maxDocsOpen

Stores the maximum number of documents that can be opened before existing documents are closed.

By default, this is 10,000.

|  | [ top ]