Advertisement
AntonioVillanueva

wxGauge en wxWidgets con dos botones de ctrl.

Jul 9th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. //Test wxGauge en wxWidgets Antonio Villanueva
  2. //g++ -Wall -static-libstdc++ -std=c++11 -Wunused-but-set-variable `wx-config --cxxflags` -o hola *.cpp `wx-config --libs`
  3. #include <iostream>
  4. #include "wx/wx.h"
  5. #include <wx/stattext.h>
  6. #include <wx/sizer.h> //Layouts
  7. #include <wx/tglbtn.h> //wxToggleButton
  8.  
  9. //Declaraciones
  10. //----------------------------------------------------------------------
  11. //Cada aplicacion wxWidget define una clase derivada de wxApp
  12. class MiApp:public wxApp
  13. {
  14.     public:
  15.     //Llamado al inicio startup, es como el main en c
  16.     virtual bool OnInit();//main wxWidgets , mas abajo se implementa
  17. };
  18.  
  19. //Declaracion de la clase frame principal
  20.  
  21. //----------------------------------------------------------------------
  22.  
  23. class MiFrame:public wxFrame
  24. {
  25.     public:
  26.     //Constructor de la clase
  27.     MiFrame(const wxString& titulo);
  28.     void OnButtonPlus(wxCommandEvent& event);//Manipula boton +
  29.     void OnButtonMinus(wxCommandEvent& event);//Manipula boton -   
  30.    
  31.     // "event handlers" , gestion de eventos
  32.     void Calibre(wxCommandEvent& event);//Eventos wxToggleButton   
  33.            
  34.     private:
  35.  
  36.     wxPanel* ventana;
  37.    
  38.     wxStaticText* label;
  39.     wxStaticText* datos;
  40.     wxGauge* calibre;
  41.  
  42.     /*Macro para informar a wxWidgets de la gestion de eventos
  43.     *Declara la tabla de eventos en esta clase ,mas abajo
  44.     * la implemento entre BEGIN_ y END_EVENT_TABLE
  45.     */
  46.    
  47.     //Declaracion dinamica eventos no utilizo el macro
  48.     //Trabajaremos desde OnInit para eventos dinamicos
  49.     DECLARE_EVENT_TABLE()
  50. };
  51. //----------------------------------------------------------------------
  52. //----------------------------------------------------------------------
  53.  
  54. /*Implementacion , MiApp
  55. *Cuando se crea el objeto MiApp se asigna a la variable global wxTheApp
  56. * pero mejor que hacer un CAST emplear el MACRO  DECLARE_APP despues de
  57. * la declaracion de la clase , wxGetApp retorna una ref. al objeto MiApp
  58. */
  59. DECLARE_APP(MiApp)
  60.  
  61. //Macro que Proporciona a wxWidgets los medios de crear el objeto MiApp
  62. IMPLEMENT_APP(MiApp)
  63.  
  64. //----------------------------------------------------------------------
  65. //----------------------------------------------------------------------
  66. //----------------------------------------------------------------------
  67.  
  68. //Implementacion OnInit,Inicializa la aplicacion
  69.  
  70. bool MiApp::OnInit()
  71. {
  72.     //Crea la ventana principal , una instancia de nuestra clase MiFrame
  73.     //El titulo lo pasamos al constructor envuelto en el macro wxT
  74.     //macro de conversion de strings y char al tipo apropiado
  75.     MiFrame *frame=new MiFrame(wxT("Titulo Test wxWidgets"));
  76.    
  77.     //Mostrar la ventana
  78.     frame->Show(true);
  79.    
  80.     //Arranca el bucle de eventos
  81.     return true ;//Si false limpia sus estructuras y sale
  82. }
  83.  
  84. //----------------------------------------------------------------------
  85.  
  86. const wxWindowIDRef ID_GAUGE = wxWindow::NewControlId();
  87. const wxWindowIDRef ID_BPLUS = wxWindow::NewControlId();
  88. const wxWindowIDRef ID_MINUS = wxWindow::NewControlId();
  89. //Tabla de eventos para MiFrame DECLARE_EVENT_TABLE() en MiFrame
  90.  
  91. BEGIN_EVENT_TABLE ( MiFrame,wxFrame)
  92.     EVT_BUTTON(ID_BPLUS, MiFrame::OnButtonPlus)//Button +
  93.     EVT_BUTTON(ID_MINUS, MiFrame::OnButtonMinus)//Button + 
  94. END_EVENT_TABLE()
  95.  
  96. //wxID_ANY le dice a Widgets de generar un identificador por su cuenta
  97. MiFrame::MiFrame(const wxString& titulo):wxFrame(NULL,wxID_ANY,wxT("Barra Sup"))
  98. {
  99.     //Crea  wxPanel
  100.     //Create (wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name=wxPanelNameStr)
  101.     ventana=new wxPanel(this,wxID_ANY);
  102.                  
  103.     //Crea Sizers wxBoxSizer VERTICAL HORIZONTAL    
  104.     wxBoxSizer* vbox  = new wxBoxSizer(wxVERTICAL); //sizer Vertical
  105.     wxBoxSizer* hbox1 = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal
  106.     wxBoxSizer* hbox2 = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal
  107.  
  108.     //Anade la wxBoxSizer los dos wxSizer horizontales  hbox1 & hbox2  
  109.     vbox->Add(hbox1);//Anade wxBoxSizer HORIZONTAL1 al VERTICAL
  110.     vbox->Add(hbox2);//Anade wxBoxSizer HORIZONTAL2 al VERTICAL    
  111.  
  112.     ventana->SetSizer(vbox);//wxPanel utiliza el wxSizer vertical
  113.  
  114.     //inicializa wxStaticText como labels
  115.     label = new wxStaticText( ventana, wxID_ANY, " BARRA -> ");
  116.     datos = new wxStaticText( ventana, wxID_ANY, " ESTADO = ");
  117.        
  118.     wxButton* boton1=new wxButton(ventana,ID_BPLUS,wxT(" +"));
  119.     wxButton* boton2=new wxButton(ventana,ID_MINUS,wxT(" -")); 
  120.     hbox1->Add(boton2);//menos     
  121.     hbox1->Add(boton1);//mas
  122.      
  123.     //Calibre wxGauge
  124.     #define MAX 100
  125.     calibre=new wxGauge(ventana,ID_GAUGE,MAX,wxDefaultPosition,wxDefaultSize
  126.                 ,wxGA_HORIZONTAL);
  127.     calibre->SetValue(MAX/2);//Valor del calibre
  128.     //Copiamos el valor en la etiqueta
  129.     datos->SetLabel( wxString::Format (wxT("%i"),calibre->GetValue()));
  130.    
  131.     //Anade componentes a los wxSizer
  132.    
  133.     hbox2->Add(label);//Barra inferior una etiqueta
  134.     hbox2->Add(calibre);//y finalmente anadimos  el wxTextCtrl
  135.     hbox2->Add(datos);
  136. }      
  137.  
  138. void MiFrame::OnButtonPlus(wxCommandEvent& event)//Manipula boton +
  139. {
  140.     //Abre ventana informando ....
  141.   //  wxMessageBox( wxT("Has hecho click en el boton + !") );
  142.   if (!(calibre->GetValue()  <calibre->GetRange ())) {return ;}
  143.    calibre->SetValue (calibre->GetValue() +1);
  144.    datos->SetLabel( wxString::Format (wxT("%i"),calibre->GetValue()));
  145. }
  146.  
  147.  
  148. void MiFrame::OnButtonMinus(wxCommandEvent& event)//Manipula boton -
  149. {
  150.     //Abre ventana informando ....
  151.     //wxMessageBox( wxT("Has hecho click en el boton - !") );
  152.   if (!(calibre->GetValue() >0)) {return ;}    
  153.    calibre->SetValue (calibre->GetValue() -1);
  154.    datos->SetLabel( wxString::Format (wxT("%i"),calibre->GetValue()));  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement