wxgraphgui.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_WXGUI_H
00023 #define MECHSYS_WXGUI_H
00024 
00025 #include <cstdlib> // for srand and rand
00026 
00027 #ifdef HAVE_CONFIG_H
00028   #include "config.h"
00029 #else
00030   #ifndef REAL
00031     #define REAL double
00032   #endif
00033 #endif
00034 
00035 #include <wx/wx.h>
00036 #include <wx/thread.h>
00037 
00038 #include "gui/wxgraph.h"
00039 
00040 
00041 // ------------------------------------- RunSim -----------------------------------
00042 
00043 class RunSim : public wxThread
00044 {
00045 public:
00046     // Constructor
00047     RunSim (WxGraph * pPlot, bool * pRunning) : _plot(pPlot), _running(pRunning) {}
00048     // Methods
00049     virtual void * Entry();
00050     // Events
00051     virtual void OnExit(); // called when the thread exits; whether it terminates normally or is stopped with Delete(), but not when it is Kill()ed!
00052 private:
00053     // Data
00054     WxGraph * _plot;
00055     bool    * _running;
00056 }; // class RunSim
00057 
00058 
00059 // ------------------------------------- wxApp ------------------------------------
00060 
00061 class App : public wxApp
00062 {
00063 public:
00064     virtual bool OnInit();
00065 }; // class App
00066 
00067 
00068 // ------------------------------------ wxFrame -----------------------------------
00069 
00070 class Frame : public wxFrame
00071 {
00072 public:
00073     // Constructor & Destructor
00074     Frame(wxString const & Title);
00075     ~Frame();
00076     // Events
00077     void OnQuit  (wxCommandEvent & Event);
00078     void OnAbout (wxCommandEvent & Event);
00079     void OnRun   (wxCommandEvent & Event);
00080     void OnStop  (wxCommandEvent & Event);
00081 private:
00082     // Event table
00083     DECLARE_EVENT_TABLE()
00084     // Controls
00085     wxTextCtrl * _term;
00086     // Data
00087     WxGraph * _plot;
00088     bool      _running;
00089     RunSim  * _run_thread;
00090 }; // class Frame
00091 
00092 
00094 
00095 
00096 // ------------------------------------- RunSim -----------------------------------
00097 
00098 inline void * RunSim::Entry() // {{{
00099 {
00100     for (int i=0; i<1000; ++i)
00101     {
00102         if (TestDestroy()) break;
00103 
00104         const int n_points=10000;
00105         REAL X1[n_points];
00106         REAL Y1[n_points];
00107         REAL X2[n_points];
00108         REAL Y2[n_points];
00109         REAL phase=i/10;
00110         REAL x0=500;
00111         REAL y0=500;
00112         REAL r=400/10000.0;
00113         for (int j=0; j<n_points; ++j)
00114         {
00115             X1[j] = x0+r*j*sin(phase+r*j*.02); 
00116             X2[j] = x0-r*j*sin(phase+r*j*.02); 
00117             Y1[j] = y0+r*j*cos(phase+r*j*.07);
00118             Y2[j] = y0-r*j*cos(phase+r*j*.07);
00119         }
00120 
00121         wxMutexGuiEnter();
00122         _plot->ResetCurve(0, X1,Y1,n_points);
00123         _plot->ResetCurve(1, X2,Y2,n_points);
00124         wxMutexGuiLeave();
00125 
00126         //std::cout << "hi from Entry with i = " << i << std::endl;
00127         wxThread::Sleep(10);
00128     }
00129 
00130     std::cout << "Finished\n";
00131     return NULL;
00132 } // }}}
00133 
00134 inline void RunSim::OnExit() // {{{
00135 {
00136     //wxMutexGuiEnter();
00137     //(*_running) = false;
00138     //wxMutexGuiLeave();
00139 } // }}}
00140 
00141 // ------------------------------------- wxApp ------------------------------------
00142 
00143 inline bool App::OnInit() // {{{
00144 {
00145     Frame * frame = new Frame(_("Minimal wxWidgets GUI App"));
00146     frame->Show(true);
00147     return true;
00148 } // }}}
00149 
00150 DECLARE_APP (App) // Implements App & GetApp()
00151 
00152 
00153 // ------------------------------------ wxFrame -----------------------------------
00154 
00155 enum // {{{
00156 {
00157     ID_RUN  = 100,
00158     ID_STOP = 101
00159 }; // }}}
00160 
00161 inline Frame::Frame(wxString const & Title) // {{{
00162     : wxFrame(/* parent */ NULL, wxID_ANY, Title),
00163       _running(false)
00164 {
00165     // Menubar
00166     wxMenuBar * mnu_bar  = new wxMenuBar;
00167     wxMenu    * mnu_file = new wxMenu;
00168     wxMenu    * mnu_run  = new wxMenu;
00169     wxMenu    * mnu_help = new wxMenu;
00170     mnu_file->Append(wxID_EXIT , _("&Quit\tCtrl-Q") , _("Quit this program"));
00171     mnu_run ->Append(ID_RUN    , _("&Run\tCtrl-R")  , _("Run simulation"));
00172     mnu_run ->Append(ID_STOP   , _("&Stop\tCtrl-S") , _("Stop running simulation, if any"));
00173     mnu_help->Append(wxID_ABOUT, _("&About\tF1")    , _("Show about dialog"));
00174     mnu_bar ->Append(mnu_file  , _("&File"));
00175     mnu_bar ->Append(mnu_run   , _("&Run"));
00176     mnu_bar ->Append(mnu_help  , _("&Help"));
00177     SetMenuBar(mnu_bar);
00178 
00179     // Statusbar
00180     CreateStatusBar(2);
00181     SetStatusText(_("Welcome !"));
00182 
00183     // Text control (Terminal)
00184     _term = new wxTextCtrl(this, wxID_ANY, _("\n"), wxDefaultPosition, wxSize(200,100), wxTE_MULTILINE);
00185     _term->SetBackgroundColour(wxT("wheat"));
00186 
00187     // Graph
00188     _plot = new WxGraph(this, "yellow", "wheat");
00189     //_plot->EqualScales(true);
00190     //_plot->Grid().Active(false);
00191     _plot->XRuler().Active(false);
00192 
00193     // Ruler
00194     _plot->XRuler ().nMajorTicks(30).Thickness(43).MajorTickLen(40);
00195     _plot->YRuler ().nMajorTicks(10);
00196     _plot->X2Ruler().nMajorTicks(15);
00197     _plot->Y2Ruler().nMajorTicks(40);
00198 
00199     // Curves
00200     REAL Xa[] = {0, 1, 2, 3, 4, 25};
00201     REAL Ya[] = {0, 5, 1, 5, 1, 25};
00202     //REAL Ya[] = {0, 1, 4, 9, 16, 25};
00203     REAL Xb[] = {-1, 5, 8, 10, 3, 1, 0};
00204     REAL Yb[] = { 1, 2, 3,  4, 5, 6, 7};
00205     REAL Xc[] = {0, 1, 2, 3, 4, 5};
00206     REAL Yc[] = {1, 2, 3, 4, -2, 0};
00207     REAL Xd[] = { 0, 1, 2, 3, 4, 6};    
00208     REAL Yd[] = { 0, 5, 1, 5, 1, 10};   
00209     _plot->AddCurve(Xa, Ya, 6).LineColour(_T("red"   )).LineStyle(wxSOLID     ).PointFGColour(_T("black")).PointBGColour(_T("yellow")).PointType(WxCurve::WxCurve_PS_NONE);
00210     _plot->AddCurve(Xb, Yb, 7).LineColour(_T("blue"  )).LineStyle(wxDOT       ).PointFGColour(_T("black")).PointBGColour(_T("green" )).PointType(WxCurve::WxCurve_PS_SQUARE_MED);
00211     _plot->AddCurve(Xc, Yc, 6).LineColour(_T("yellow")).LineStyle(wxLONG_DASH ).PointFGColour(_T("black")).PointBGColour(_T("blue"  )).PointType(WxCurve::WxCurve_PS_DOT);
00212     _plot->AddCurve(Xd, Yd, 6).LineColour(_T("blue"  )).LineStyle(wxSHORT_DASH).PointFGColour(_T("black")).PointBGColour(_T("black" )).PointType(WxCurve::WxCurve_PS_CROSS_BIG);
00213 
00214     // Sizer
00215     wxBoxSizer * top_sizer = new wxBoxSizer(wxVERTICAL);
00216     //top_sizer->Add(_term, /*stretch factor*/2, wxEXPAND, /*border*/wxALL, /*border width*/0);
00217     top_sizer->Add(_plot,                   1, wxEXPAND,           wxALL,                 0);
00218 
00219     // SetSizer
00220     SetSizer(top_sizer);
00221     top_sizer->Fit(this);
00222     top_sizer->SetSizeHints(this);
00223 
00224 } // }}}
00225 
00226 inline Frame::~Frame() // {{{
00227 {
00228     //if (_running) _run_thread->Delete();
00229     //_run_thread->Wait();
00230 } // }}}
00231 
00232 inline void Frame::OnQuit(wxCommandEvent & Event) // {{{
00233 {
00234     Close();
00235 } // }}}
00236 
00237 inline void Frame::OnAbout(wxCommandEvent & Event) // {{{
00238 {
00239     wxString msg;
00240     msg.Printf(_("Hello and welcome to %s"), wxVERSION_STRING);
00241     wxMessageBox(msg, _("About Minimal"), wxOK | wxICON_INFORMATION, this);
00242 } // }}}
00243 
00244 inline void Frame::OnRun(wxCommandEvent & Event) // {{{
00245 {
00246     if (!_running)
00247     {
00248         _plot->EnableCurve(1, false);
00249         _plot->EnableCurve(2, false);
00250         _plot->EnableCurve(3, false);
00251         _run_thread = new RunSim(_plot, &_running);
00252         if (_run_thread->Create()!=wxTHREAD_NO_ERROR) wxLogError(_("Can not create thread!"));
00253         else
00254         {
00255             _running = true;
00256             _run_thread->Run();
00257         }
00258     }
00259     else wxLogError(_("Simulation is already running"));
00260 } // }}}
00261 
00262 inline void Frame::OnStop(wxCommandEvent & Event) // {{{
00263 {
00264     if (_running)
00265     {
00266         _run_thread->Delete();
00267         _running = false;
00268     }
00269     else wxLogError(_("Simulation is not running"));
00270 } // }}}
00271 
00272 BEGIN_EVENT_TABLE(Frame, wxFrame) // {{{
00273     EVT_MENU (wxID_EXIT , Frame::OnQuit )
00274     EVT_MENU (wxID_ABOUT, Frame::OnAbout)
00275     EVT_MENU (ID_RUN    , Frame::OnRun  )
00276     EVT_MENU (ID_STOP   , Frame::OnStop )
00277 END_EVENT_TABLE() // }}}
00278 
00279 #endif // MECHSYS_WXGUI_H
00280 
00281 // vim:fdm=marker

Generated on Wed Jan 24 15:56:26 2007 for MechSys by  doxygen 1.4.7