Advertisement
AntonioVillanueva

Text Control en wxWidgets

Jul 9th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. //Test Text Control 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/slider.h> //wxSlider
  8. #include <wx/textctrl.h> //Text Control
  9. //using namespace std;
  10.  
  11. //Declaraciones
  12. //----------------------------------------------------------------------
  13. //Cada aplicacion wxWidget define una clase derivada de wxApp
  14. class MiApp:public wxApp
  15. {
  16.     public:
  17.     //Llamado al inicio startup, es como el main en c
  18.     virtual bool OnInit();//main wxWidgets , mas abajo se implementa
  19. };
  20.  
  21. //Declaracion de la clase frame principal
  22.  
  23. //----------------------------------------------------------------------
  24.  
  25. class MiFrame:public wxFrame
  26. {
  27.     public:
  28.     //Constructor de la clase
  29.     MiFrame(const wxString& titulo);
  30.     void OnButtonOk(wxCommandEvent& event);//Manipula boton OK     
  31.    
  32.     // "event handlers" , gestion de eventos
  33.     void ProcesaEnter(wxCommandEvent& event);//wxCtrlText ENTER
  34.            
  35.     private:
  36.  
  37.     wxPanel* ventana;
  38.    
  39.     wxStaticText* label;
  40.     wxStaticText* datos;
  41.     wxTextCtrl* textCtrl;
  42.  
  43.     /*Macro para informar a wxWidgets de la gestion de eventos
  44.     *Declara la tabla de eventos en esta clase ,mas abajo
  45.     * la implemento entre BEGIN_ y END_EVENT_TABLE
  46.     */
  47.    
  48.     //Declaracion dinamica eventos no utilizo el macro
  49.     //Trabajaremos desde OnInit para eventos dinamicos
  50.     DECLARE_EVENT_TABLE()
  51. };
  52. //----------------------------------------------------------------------
  53. //----------------------------------------------------------------------
  54.  
  55. /*Implementacion , MiApp
  56. *Cuando se crea el objeto MiApp se asigna a la variable global wxTheApp
  57. * pero mejor que hacer un CAST emplear el MACRO  DECLARE_APP despues de
  58. * la declaracion de la clase , wxGetApp retorna una ref. al objeto MiApp
  59. */
  60. DECLARE_APP(MiApp)
  61.  
  62. //Macro que Proporciona a wxWidgets los medios de crear el objeto MiApp
  63. IMPLEMENT_APP(MiApp)
  64.  
  65. //----------------------------------------------------------------------
  66. //----------------------------------------------------------------------
  67. //----------------------------------------------------------------------
  68.  
  69. //Implementacion OnInit,Inicializa la aplicacion
  70.  
  71. bool MiApp::OnInit()
  72. {
  73.     //Crea la ventana principal , una instancia de nuestra clase MiFrame
  74.     //El titulo lo pasamos al constructor envuelto en el macro wxT
  75.     //macro de conversion de strings y char al tipo apropiado
  76.     MiFrame *frame=new MiFrame(wxT("Titulo Test wxWidgets"));
  77.    
  78.     //Mostrar la ventana
  79.     frame->Show(true);
  80.    
  81.     //Arranca el bucle de eventos
  82.     return true ;//Si false limpia sus estructuras y sale
  83. }
  84.  
  85. //----------------------------------------------------------------------
  86.  
  87. const wxWindowIDRef ID_TEXTCTRL = wxWindow::NewControlId();
  88. //Tabla de eventos para MiFrame DECLARE_EVENT_TABLE() en MiFrame
  89.  
  90. BEGIN_EVENT_TABLE ( MiFrame,wxFrame)
  91.     EVT_TEXT_ENTER(ID_TEXTCTRL,MiFrame::ProcesaEnter)//Down Up arrow event 
  92.     EVT_BUTTON(wxID_OK,     MiFrame::OnButtonOk)     
  93. END_EVENT_TABLE()
  94.  
  95. //wxID_ANY le dice a Widgets de generar un identificador por su cuenta
  96. MiFrame::MiFrame(const wxString& titulo):wxFrame(NULL,wxID_ANY,wxT("Barra Sup"))
  97. {
  98.     //Crea  wxPanel
  99.     //Create (wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name=wxPanelNameStr)
  100.     ventana=new wxPanel(this,wxID_ANY);
  101.                  
  102.     //Crea Sizers wxBoxSizer VERTICAL HORIZONTAL    
  103.     wxBoxSizer* vbox  = new wxBoxSizer(wxVERTICAL); //sizer Vertical
  104.     wxBoxSizer* hbox1 = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal
  105.     wxBoxSizer* hbox2 = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal
  106.  
  107.     //Anade la wxBoxSizer los dos wxSizer horizontales  hbox1 & hbox2  
  108.     vbox->Add(hbox1);//Anade wxBoxSizer HORIZONTAL1 al VERTICAL
  109.     vbox->Add(hbox2);//Anade wxBoxSizer HORIZONTAL2 al VERTICAL    
  110.  
  111.     ventana->SetSizer(vbox);//wxPanel utiliza el wxSizer vertical
  112.  
  113.     //inicializa wxStaticText como labels
  114.     label = new wxStaticText( ventana, wxID_ANY, " BARRA -> ");
  115.     datos = new wxStaticText( ventana, wxID_ANY, " VALOR = "); 
  116.        
  117.     wxButton* boton1=new wxButton(ventana,wxID_OK,wxT("OK 1"));
  118.     hbox1->Add(boton1);
  119.    
  120.     //wxTextCtrl    
  121.     textCtrl =new wxTextCtrl(ventana,ID_TEXTCTRL,wxEmptyString,wxDefaultPosition,
  122.     wxSize(240,100),
  123.     wxTE_MULTILINE|wxTE_PROCESS_ENTER);
  124.     //Anade componentes a los wxSizer
  125.     hbox1->Add(datos);//Barra superior datos   
  126.     hbox2->Add(label);//Barra inferior una etiqueta
  127.     hbox2->Add(textCtrl);//y finalmente anadimos  el wxTextCtrl
  128. }      
  129. //Procesa el evento ENTER en el wxTextCtrl
  130. void MiFrame::ProcesaEnter(wxCommandEvent& event){
  131.     wxString texto;
  132.    
  133.     for (int linea=0;linea<=textCtrl->GetNumberOfLines ();linea++){
  134.         texto+=textCtrl->GetLineText (linea);
  135.     }      
  136.     //Abre una ventana con el texto escrito ..
  137.         wxMessageBox( wxT("Has hecho ENTER en el texto = "+texto) );   
  138. }
  139. void MiFrame::OnButtonOk(wxCommandEvent& event)//Manipula boton OK
  140. {
  141.     //Abre ventana informando ....
  142.     wxMessageBox( wxT("Has hecho click en el boton OK !") );
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement