Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 67749e1d53ab4919a475df6bdefe75d1 > files > 36

lib64bakery2.6-devel-2.6.3-2mdv2010.0.x86_64.rpm

<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta name="description" content="Document/With with Bakery">
<meta name="keywords" content="Document, View, Bakery, C++, Model, View, Controller, MVC"><title>Document/View with Bakery</title><style type="text/css"></style></head>





<body bgcolor="#ffffff">

<h1 align="center">Document/View with Bakery</h1>



<h2>Contents</h2>



<ul>

  <li><a href="#Introduction">Introduction</a></li>

  <li><a href="#App">Create the App class</a></li>

  <li><a href="#Document">Create the Document class</a></li> 

  <li><a href="#View">Create the View class</a></li>  

  <li><a href="#AppUseView">Make your App use your View</a></li>  

  <li><a href="#AppUseDocument">Make your App use your Document</a></li>  

  <li><a href="#Summary">Summary</a></li> 

</ul> 

 

<h2><a name="Introduction"></a>Introduction</h2> 

 

<blockquote> 

 

<p>Bakery::App_WithDoc can help you to separate your application's data and user 
interface. When use this Document/View interface, Bakery will take care of 
tedious but important generic functionality such as asking the user to select a 
document to load and asking the user to save modified documents. This means that 
you only need to write code that's specific to your application.</p>



<p>You will need App, Document, and View classes. And you will need to link them 
together. The next few sections show you how to do this. The code snippets are 
simplified versions of the WithDocView example in the Bakery distribution.</p>



</blockquote> 

 

<h2><a name="App"></a>Create the App class</h2>                 

              

<blockquote>              

  <p>Derive an App class from Bakery::App_WithDoc_Gtk or App_WithDoc_GnomeUI. Override the new_instance() 
  method:</p>             

             

  <p>e.g.</p>             

             

  <pre>class AppExample : public Bakery::App_WithDoc_Gtk
{
public:
  AppExample();
  virtual ~AppExample();</pre>
  <pre>protected:
  virtual App* new_instance(); //override
};</pre>
  <pre>new_instance() should be implemented like so:</pre>
  <pre>Bakery::App* AppExample::new_instance()
{
  return new AppExample();
}</pre>

             

</blockquote>             

<h2><a name="Document"></a>Create the Document class</h2>                 

              

<blockquote>

  <p>Derive a Document class from Bakery::Document.</p>             

             

  <p>e.g.</p>             

             

  <blockquote>
    <pre>class DocExample : public Bakery::Document
{
public:
  DocExample();
  virtual ~DocExample();

  //overrides:
  virtual bool load_after();
  virtual bool save_before();

  void set_something(const std::string&amp; strSomething);
  std::string get_something() const;

protected:
  std::string m_strSomething;
};</pre>
  </blockquote>
  <p>Implement load_afer() and save_before() to read and write your document data in 
  whatever format you choose. Here's a very simple example:</p>
  <blockquote>
    <pre>bool DocExample::load_after()
{
  bool bTest = Bakery::Document::load_after();
  if(bTest)
  {
    //See comment in save().
  m_strSomething = get_contents();
  }
  
  return bTest;
}</pre>
    <pre>bool DocExample::save_before()
{
  set_contents(m_strSomething);

  return Bakery::Document::save_before();
}</pre>
  </blockquote>
  <p>When you implement methods like set_something(), which changes data in your 
  document, you should call Document::set_modified(true).</p>
  <p>I suggest that you use an XML file format for your document. The <a href="bakery_document_xml.html">Bakery::Document_XML</a> class makes this easy.</p>
</blockquote>
<h2><a name="View"></a>Create the View class</h2>  
<blockquote>
  <p>The View is the user-interface that shows your Document's data and allows 
  the user to change that data.</p>
  <p>Derive a View class from both a Gtk-- or Gnome-- widget and Bakery::View. 
  Notice that Bakery::View is a template which you specialize for your Document 
  class.</p>
  <p>For instance, this View has data in just one Entry:</p>
  <blockquote>
    <pre>class ViewExample :
  public Gtk::VBox,
  public Bakery::View&lt;DocExample&gt;
{
public:
  ViewExample();
  virtual ~ViewExample();

  //overrrides:
  virtual void load_from_document();
  virtual void save_to_document();</pre>
    <pre>protected:
  //Signal handlers:
  virtual void on_Entry_changed();

  //Child widgets:
  Gtk::Label m_Label;
  Gtk::Entry m_Entry;
};</pre>
  </blockquote>
  <p>Your View needs to know how to show and store the Document data. So 
  implement the load_from_document() and save_to_document() methods like so:</p>
  <blockquote>
    <pre>void ViewExample::load_from_document()
{
  m_Entry.set_text( get_document()-&gt;get_something() );
}</pre>
    <pre>void ViewExample::save_to_document()
{
  const std::string&amp; strText = m_Entry.get_text();
  
  //Don't trigger 'modified' unless it really is different:
  if(strText != get_document()-&gt;get_something()) 
    get_document()-&gt;set_something(strText);
}</pre>
  </blockquote>
</blockquote>
<h2><a name="AppUseView"></a>Make your App use your View</h2>  
<blockquote>
  <p>Your View is a user-interface widget so you can use it in your App like any 
  other widget. For instance, it could be a class member that is added in the 
  App::init() method..</p>
  <blockquote>
    <pre>e.g.</pre>
    <pre>class AppExample : public Bakery::App_WithDoc_Gtk
{
  ...

  ViewExample m_View;
};</pre>
    <pre>and </pre>
    <pre>void AppExample::init()
{
  //Call base method:
  Bakery::App_WithDoc_Gtk::init();

  set_contents(m_View);
}</pre>
  </blockquote>
</blockquote>
<h2><a name="AppUseDocument"></a>Make your App use your Document</h2>  
<blockquote>
  <p>Override App::init_create_document(), like so:</p> 
  <blockquote>
    <pre>void AppExample::init_create_document()
{
  if(m_pDocument == NULL)
  {
    m_pDocument = new DocExample();

    //Tell document about view:
    m_pDocument-&gt;set_view(&amp;m_View);

    //Tell view about document:

    m_View.set_document(static_cast&lt;DocExample*&gt;(m_pDocument));
  }</pre>
    <pre>  Bakery::App_WithDoc_Gtk::init_create_document(); //Sets window title. Doesn't recreate doc.
}</pre>
  </blockquote>
  <p>Notice that you are also telling the View to use the Document instance.</p>
</blockquote>
<h2><a name="Summary"></a>Summary</h2>
<blockquote>
  <p>Even if you do nothing else, your App will have a standard File menu with 
  Open and Save items that read and write your Documents and show them in your 
  View. However, you probably want customized menus and a toolbar so you should 
  also read <a href="bakery_menus.html">Bakery menus and toolbars</a>.</p>
  <p>Make sure that you call Bakery::init() and your App instance's init() method from your main() 
  function.</p>
</blockquote>

                 

<p align="center"><font size="1">Copyright © 2001-2003, Murray Cumming. Verbatim        

copying and distribution of this entire article is permitted in any medium,        

provided this notice is preserved.</font></p>       

                

</body></html>