    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Wx - A Live Port</title>
        <link>https://members.accu.org/index.php/articles/715</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 16, #6 - Dec 2004</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c99/">166</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-99/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+99/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Wx - A Live Port</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 December 2004 13:16:10 +00:00 or Fri, 03 December 2004 13:16:10 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a></h2>
</div>
<p>The process of connecting the user interface to code is very
similar to MFC. The event table is as follows:</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e26" id="d0e26"></a>MFC</h3>
</div>
<pre class="programlisting">
ON_COMMAND(ID_VIEW_SOUNDINGS, OnViewSoundings)
ON_UPDATE_COMMAND_UI(ID_VIEW_SOUNDINGS, OnUpdateViewSoundings)
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e31" id="d0e31"></a>wxWidgets</h3>
</div>
<pre class="programlisting">
EVT_MENU(ID_SOUNDINGS, WXWindPlotView::OnViewSoundings)
EVT_UPDATE_UI(ID_SOUNDINGS, WXWindPlotView::OnUpdateViewSoundings)
</pre>
<p>For clarity I put the message/event handlers in the class that
will actually be handling it - this is the way the class wizard
built the application in the first place but this is not the a
requirement. Class wizard builds the event table with a few mouse
clicks. Under wxWidgets you have to code it manually, however there
is very little work to do.</p>
<p>As with MFC the wxWidgets framework handles the UI changing in
idle time.</p>
<p>Here is the implementation of the code:</p>
<pre class="programlisting">
void WXWindPlotView::OnViewSoundings() {
  WXWindPlotDoc* doc
              = (WXWindPlotDoc*)GetDocument();

  if(!Soundings-&gt;DepthFilePresent) {
    wxMessageBox(&quot;Ocean depth features require
                 Registration and the Xaxero
                 CD ROM.\nVisit www.xaxero.com
                 for details.&quot;);
    return;
  }

  if(bSound)
    bSound=FALSE;
  else
    bSound=TRUE;
  doc-&gt;UpdateAllViews(NULL,NULL );
}

void WXWindPlotView::OnUpdateViewSoundings(
                     wxUpdateUIEvent&amp; event) {
  event.Check(bSound);  
}
</pre>
<p>Almost identical to MFC - however one little pitfall to be
careful of. If you want to set a check in an item make sure you
have set it to checkable in the wxDesigner properties panel or you
will get assertion errors in debug.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e46" id="d0e46"></a><tt class=
"function">ARRAY</tt> Macros</h2>
</div>
<p>Like MFC, wx allows an array of classes with its own dynamic
array allocation. Here I am trying to define an array of email
addresses.</p>
<p>Prior to the class that defines an email message we define an
array container for the addresses:</p>
<pre class="programlisting">
WX_DECLARE_OBJARRAY(wxSMTPAddress,
                    arr_Recipients);

class Message {
  ...
  arr_Recipients
              Recipients;
};

#include &lt;wx/arrimpl.cpp&gt;
// this is a magic
// incantation which must
// be done!

WX_DEFINE_OBJARRAY(
         arr_ToRecipients);
</pre>
<p>I left the comment in the include statement. I had link errors
when the <tt class="literal">WX_DEFINE_OBJARRAY</tt> was left off
and so after reading the <tt class="literal">wxArray</tt> section
of the documentation fully it all started to make sense. More
important - I went to a clean compile and link.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e65" id="d0e65"></a>Creating a
dialog</h2>
</div>
<p>Using wxDesigner (see Figure 1) you lay out your dialog.</p>
<div class="figure"><a name="d0e70" id="d0e70"></a>
<div class="mediaobject c3"><img src="/var/uploads/journals/resources/selby-fig1.png"
align="middle" alt="wxDesigner"></div>
<p class="title c4">Figure 1. wxDesigner</p>
</div>
<p>This simple example shows how 3 layers of vertical sizers
encapsulate a box. The top two layers are horizontal sizers with
adjacent controls. The hierarchy is a little tricky at first but
when you have the hang of it, design goes really fast.</p>
<p>As you create the controls you will be giving them resource
names similar to MFC. Now comes the tricky bit - to generate the
code.</p>
<p>For the whole project I am using one wdr file containing all my
dialogs, tool bars, etc. When you press the C++ button the code is
written to <tt class="filename">wxwindplotrc.cpp</tt> that has the
low level hard to read stuff that does the actual painting of the
resources.</p>
<p>We need to generate a dialog implementation class now. We press
the <tt class="filename">.cpp</tt> button to add a C source. Give
it a name (remember to add the suffix <tt class=
"filename">.cpp</tt>)</p>
<p>This will generate an empty C++ container.</p>
<p>We now need to implement a class for our dialog. Easy enough.
Press the Class button (see Figure 2).</p>
<div class="figure"><a name="d0e97" id="d0e97"></a>
<div class="mediaobject c3"><img src="/var/uploads/journals/resources/selby-fig2.png"
align="middle" alt="The Add A Class Dialog"></div>
<p class="title c4">Figure 2. The Add A Class Dialog</p>
</div>
<p>We have added the handlers on the right - everything we wanted
our dialog to do. We respond to either the OK or the cancel
messages and we have handlers to move data to the dialog and out of
it.</p>
<p>That is all there is to it - a shell is created that will
compile and run. It will not do anything yet though.</p>
<p>We have two ids that we wish to manipulate from the code.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>A check box: <tt class="literal">ID_TZCHECK</tt></p>
</li>
<li>
<p>An edit box: <tt class="literal">ID_TZDISP</tt></p>
</li>
</ul>
</div>
<p>We need to add inline functions to read and write from these
ids.</p>
<p>Press the Get button on the source code editor to add the get
functions:</p>
<div class="figure"><a name="d0e124" id="d0e124"></a>
<div class="mediaobject c3"><img src="/var/uploads/journals/resources/selby-fig3.png"
align="middle" alt="The Add C++ Getter Dialog"></div>
<p class="title c4">Figure 3. The Add C++ Getter Dialog</p>
</div>
<p>Select the dialog you wish to use and press on the field you
want to allow reading and writing from.</p>
<p>The program will select a method name. You can alter this if you
like. The Add getter button places it in your code. Now we
have:</p>
<pre class="programlisting">
// WDR: method declarations for MyDialog
-&gt; wxCheckBox* GetTzcheck() { return
      (wxCheckBoxl*) FindWindow(ID_TZCHECK); }
-&gt; wxTextCtrl* GetTzdisp()  { return
        (wxTextCtrl*) FindWindow(ID_TZDISP); } 
</pre>
<p>So far wxDesigner has been doing all the work for us. Now we
have to roll up our sleeves and start writing code.</p>
<p>Before we leave the header file we need to add variables to hold
our values. A <tt class="varname">wxString - DispUTC</tt> and
<tt class="varname">BOOL Check</tt>.</p>
<p>In the code we need to connect this to actions.</p>
<p>Look at the constructor - the first line should connect the
dialog resource name to code. The first line of the constructor is
generated as:</p>
<pre class="programlisting">
MyDialogFunc(this, TRUE);
</pre>
<p>Double-check this is what you want. Normally a meaningful name
is generated as specified. This will be in the constructor of the
generated source file.</p>
<pre class="programlisting">
Next we go to the following functions and flesh them out.

bool MyDialog::TransferDataToWindow() {
  // wxDesigner has added two getters, used to
  // set the values on startup and retrieve them 
  // when closing the dialog (next method).
  GetTzdisp()-&gt;SetValue(DispUTC);
  GetTzcheck()-&gt;SetValue(Check);
  return TRUE;
}
bool MyDialog::TransferDataFromWindow() {
  DispUTC = GetTzdisp()-&gt;GetValue();
  Check = GetTzcheck()-&gt;GetValue();
  return TRUE;
}
</pre>
<p>There we are - a fully working dialog.</p>
<p>You can include a bunch of dialogs in one chunk of source code -
useful for keeping wizard and notebook pages together.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e160" id="d0e160"></a>Invoking the
Dialog</h2>
</div>
<p>In your code include the <tt class="filename">tzdlg.h</tt> file
and a calling subroutine in the header of the calling program and
create a call in the body.</p>
<p>Invoke the dialog as follows:</p>
<pre class="programlisting">
TZDlg dialog(GetMainFrame(), -1,
             wxT(&quot;Time Zone Display&quot;));
... Initialization
dialog.ShowModal();
</pre>
<p>As easy as that !</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e174" id="d0e174"></a>Resources</h2>
</div>
<div class="bibliomixed">
<p class="bibliomixed">wxWidgets: <span class="bibliomisc"><a href=
"http://www.wxwidgets.org" target=
"_top">www.wxwidgets.org</a></span></p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">wxDesigner: <span class=
"bibliomisc"><a href="http://www.roebling.de/" target=
"_top">www.roebling.de/</a></span></p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">Another introduction to wxWidgets:
<span class="bibliomisc"><a href=
"http://www.all-the-johnsons.co.uk/accu/index.html" target=
"_top">www.all-the-johnsons.co.uk/accu/index.html</a></span></p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">Porting MFC to wxWidgets: <span class=
"bibliomisc"><a href=
"http://www-106.ibm.com/developerworks/linux/library/l-mfc/"
target="_top">www-106.ibm.com/developerworks/linux/library/l-mfc/</a></span></p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
