Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Study Questions: study the source code (C++) and answer the following questions:

ID: 672514 • Letter: S

Question

 Study Questions: study the source code (C++) and answer the following questions:  Question 1. Which of the 3 different ways is the timer used in this program  Question 2. What is the period for the timer used?  Question 3. Which object and function is called with timer repeated periodic notification?  Question 4. What event causes the text to be moved?  // Render timer - use of a timer //  http://wiki.wxwidgets.org/Making_a_render_loop #include <wx/sizer.h> #include <wx/wx.h> #include <wx/timer.h>  // prototypes class BasicDrawPane; class MyFrame;  // class definitions class RenderTimer : public wxTimer {     BasicDrawPane* pane; public:     RenderTimer(BasicDrawPane* pane);     void Notify();     void start(); };   class BasicDrawPane : public wxPanel {  public:     BasicDrawPane(wxFrame* parent);      void paintEvent(wxPaintEvent& evt);     void render( wxDC& dc );      DECLARE_EVENT_TABLE() };    class MyApp: public wxApp {     bool OnInit();      MyFrame* frame; public:  };   RenderTimer::RenderTimer(BasicDrawPane* pane) : wxTimer() {     RenderTimer::pane = pane; }  void RenderTimer::Notify() {     pane->Refresh(); }  void RenderTimer::start() {     wxTimer::Start(10); }  IMPLEMENT_APP(MyApp)  class MyFrame : public wxFrame {     RenderTimer* timer;     BasicDrawPane* drawPane;  public:     MyFrame() : wxFrame((wxFrame *)NULL, -1,  wxT("Hello wxDC"), wxPoint(50,50), wxSize(500,300))     {          drawPane = new BasicDrawPane( this );          timer = new RenderTimer(drawPane);         Show();         timer->start();     }     ~MyFrame()     {         delete timer;     }     void onClose(wxCloseEvent& evt)     {         timer->Stop();         evt.Skip();     }     DECLARE_EVENT_TABLE() };  // event table for MyFrame BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_CLOSE(MyFrame::onClose) END_EVENT_TABLE()  bool MyApp::OnInit() {     frame = new MyFrame();     frame->Show();      return true; }  // event table for BasicDrawPane BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel) EVT_PAINT(BasicDrawPane::paintEvent) END_EVENT_TABLE()    BasicDrawPane::BasicDrawPane(wxFrame* parent) : wxPanel(parent) { }   void BasicDrawPane::paintEvent(wxPaintEvent& evt) {     wxPaintDC dc(this);     render(dc); }   void BasicDrawPane::render( wxDC& dc ) {     static int y = 0;     static int y_speed = 2;      y += y_speed;     if(y<0) y_speed = 2;     if(y>200) y_speed = -2;      dc.SetBackground( *wxWHITE_BRUSH );     dc.Clear();     dc.DrawText(wxT("Testing"), 40, y); } 

Explanation / Answer

Question 1:

wxTimer:

Using wxTimerThe wxTimer class lets your application receive periodic notification, either as a "single shot" or repeatedly.

You can choose how your code will be notified. If you prefer to use a virtual function, derive a classfrom wxTimer and override the Notify

function. If you prefer to receive a wxTimerEventevent, pass a

wxEvtHandler pointer to the timer object (in the constructor or

using SetOwner), and use EVT_TIMER(id, func)

to connect the timer to an event handler function.

Question 2:

Optionally, you can pass an identifier that you passed to the constructor or

SetOwner to uniquely identify the timer object and then pass that identifier to EVT_TIMER. This technique is useful if you have several timer objects to handle.Start the timer by calling Start,passing a time interval in milliseconds and wxTIMER_ONE_SHOT if only a single notification is required. Calling Stop stops the timer, and IsRunning

can be used to determine whether the timer is running.

Question 3:

The inherited wxWindow::Refresh() is called. Refresh()creates wxPaintEvent which calls our applicationBasicDrawPane::paintEvent.

The function BasicDrawPane::paintEvent():

creates a'Device Context' for 'this'window

calls BasicDrawPane::render()

The function BasicDrawPane::render():

creates new x and y coordinates

clears the wxPanel Device Context

draws the word “Testing” on the wxPanel Device Contex

Question 4:

An event table is placed in an implementation file to tell wxWidgets how to map events to member functions. These member functions are not virtual functions, but they are all similar in form: they take a single wxEvent-derived argument, and have a void return type.

Let us now look at the details of this definition: the first line means that we are defining the event table for MyFrame class and that its base class is wxframe, so events not processed by MyFrame will, by default, be handled by wxframe. The next four lines define bindings of individual events to their handlers: the first two of them map menu commands from the items with the identifiers specified as the first macro parameter to two different member functions. In the next one, EVT_SIZE means that any changes in the size of the frame will result in calling OnSize() method. Note that this macro doesn't need a window identifier, since normally you are only interested in the current window's size events.