NIREP

FastComboEditor.cpp

Go to the documentation of this file.
00001 
00002 #include "FastComboEditor.h"
00003 // For compilers that support precompilation, includes "wx/wx.h".
00004 #include "wx/wxprec.h"
00005 
00006 #ifdef __BORLANDC__
00007 #pragma hdrstop
00008 #endif
00009 
00010 #ifndef WX_PRECOMP
00011 #include "wx/wx.h"
00012 #endif
00013 #include <wx/tokenzr.h>
00014 #include "EzGrid.h"
00015 #ifndef __WINDOWS__
00016 #include <gtk/gtk.h>
00017 #include <gdk/gdk.h>
00018 #endif
00019 
00020 class wxSComboBox : public wxComboBox
00021 {
00022 public:
00023    wxSComboBox();
00024         wxSComboBox(wxWindow *parent, wxWindowID id,
00025                         const wxString& value = wxEmptyString,
00026                         const wxPoint& pos = wxDefaultPosition,
00027                         const wxSize& size = wxDefaultSize,
00028                         int n = 0, const wxString choices[] = NULL,
00029                         long style = 0,
00030                         const wxValidator& validator = wxDefaultValidator,
00031                         const wxString& name = wxComboBoxNameStr);
00032         void SetCellData(int nRow, int nCol, wxGrid* pGrid)
00033         {
00034                 m_nRow = nRow;
00035                 m_nCol = nCol;
00036                 m_pGrid = pGrid;
00037         }
00038 private:
00039         DECLARE_EVENT_TABLE()
00040         void OnChange(wxCommandEvent& event);
00041         int m_nRow;
00042         int m_nCol; 
00043         wxGrid* m_pGrid;
00044 };
00045 
00046 
00047 wxSComboBox::wxSComboBox(wxWindow *parent, wxWindowID id,
00048                 const wxString& value,
00049                 const wxPoint& pos,
00050                 const wxSize& size,
00051                 int n, const wxString choices[],
00052                 long style,
00053                 const wxValidator& validator,
00054                 const wxString& name)
00055 {
00056         Create(parent, id, value, pos, size, n, choices, style, validator, name);
00057 }
00058 
00059 wxSComboBox::wxSComboBox() : m_pGrid(NULL)
00060 { 
00061 }
00062 
00063 BEGIN_EVENT_TABLE(wxSComboBox, wxComboBox)
00064    EVT_COMBOBOX(-1, wxSComboBox::OnChange)
00065 END_EVENT_TABLE()
00066 
00067 void wxSComboBox::OnChange(wxCommandEvent& event)
00068 {
00069         if (m_pGrid) 
00070         {
00071                 m_pGrid->GetTable()->SetValue(m_nRow, m_nCol, event.GetString());
00072 //              m_pGrid->SetCellValue(m_nRow, m_nCol, event.GetString());
00073                 wxGridEvent gridEvt(m_pGrid->GetId(),
00074                         wxEVT_GRID_CELL_CHANGE, m_pGrid, m_nRow, m_nCol);
00075                 gridEvt.SetString(event.GetString());
00076                 GetEventHandler()->ProcessEvent(gridEvt);
00077         }
00078         event.Skip();
00079 }
00080 
00081 wxFastComboEditor::wxFastComboEditor(size_t count,
00082                                                                                            const wxString choices[],
00083                                                                                            bool allowOthers)
00084                                           : m_allowOthers(allowOthers)
00085 {
00086         SetClientData((void*)&m_pointActivate);
00087         if ( count )
00088         {
00089                 m_choices.Alloc(count);
00090                 for (size_t n = 0; n < count; n++ )
00091                 {
00092                         m_choices.Add(choices[n]);
00093                 }
00094         }
00095 }
00096 
00097 wxGridCellEditor *wxFastComboEditor::Clone() const
00098 {
00099         wxFastComboEditor *editor = new wxFastComboEditor;
00100         editor->m_allowOthers = m_allowOthers;
00101         editor->m_choices = m_choices;
00102         return editor;
00103 }
00104 
00105 void wxFastComboEditor::Create(wxWindow* parent,
00106                                                                         wxWindowID id,
00107                                                                         wxEvtHandler* evtHandler)
00108 {
00109         size_t count = m_choices.GetCount();
00110         wxString *choices = new wxString[count];
00111         for ( size_t n = 0; n < count; n++ )
00112         {
00113           choices[n] = m_choices[n];
00114         }
00115         m_control = new wxSComboBox(parent, id, wxEmptyString,
00116                                                                          wxDefaultPosition, wxDefaultSize,
00117                                                                          count, choices,
00118                                                                          m_allowOthers ? 0 : wxCB_READONLY);
00119         delete [] choices;
00120     int i = dynamic_cast<wxSComboBox*>(m_control)->FindString(m_selection);
00121     if( i != wxNOT_FOUND) {
00122         dynamic_cast<wxSComboBox*>(m_control)->SetSelection(i);
00123     }
00124 
00125         wxGridCellEditor::Create(parent, id, evtHandler);
00126 }
00127 
00128 void wxFastComboEditor::PaintBackground(const wxRect& rectCell,
00129                                                                                          wxGridCellAttr * attr)
00130 {
00131         wxGridCellEditor::PaintBackground(rectCell, attr);
00132 }
00133 
00134 void wxFastComboEditor::BeginEdit(int row, int col, wxGrid* grid)
00135 {
00136         wxASSERT_MSG(m_control,
00137                                  wxT("The wxGridCellEditor must be Created first!"));
00138 
00139         EzGrid* pEzGrid = (EzGrid*)grid;
00140         pEzGrid->RevertSel();
00141 
00142         m_startValue = grid->GetTable()->GetValue(row, col);
00143 
00145     // CZ: I think the following three line should be put before 'if'.
00146     // SetCellData() initializes the cell data, otherwise SetSelection()
00147     // will use an uninitialized row and colunm numbers, which were very
00148     // big to cause buffer overflow.
00150         Combo()->SetInsertionPointEnd();
00151         Combo()->SetFocus();
00152         Combo()->SetCellData(row, col, grid);
00153 
00154         if (m_allowOthers)
00155                 Combo()->SetValue(m_startValue);
00156         else
00157         {
00158                 // find the right position, or default to the first if not found
00159                 int pos = Combo()->FindString(m_startValue);
00160                 if (pos == -1)
00161                         pos = 0;
00162                 Combo()->SetSelection(pos);
00163         }
00164 
00165 
00166         if (m_pointActivate.x > -1 && m_pointActivate.y > -1)
00167         {
00168                 m_pointActivate = Combo()->ScreenToClient(m_pointActivate);
00169 #ifdef __WINDOWS__
00170                 SendMessage((HWND)Combo()->GetHandle(), WM_LBUTTONDOWN, 0,
00171                         MAKELPARAM(m_pointActivate.x, m_pointActivate.y));
00172 #else
00173 
00174         // CZ: I commented out the following gtk hacks because they are not needed any more.
00175         // The code contains APIs which were deprecated as of gtk2.4
00177 
00178 //        GtkCombo *combo = GTK_COMBO(Combo()->m_widget);
00179 //        combo->current_button = 0;
00180 //        GtkWidget *button = GTK_COMBO(Combo()->m_widget)->button;
00181 //        GdkEventButton event;
00182 //        memset(&event, 0, sizeof(event));
00183 //        gdk_window_ref (button->window);
00184 //        // to do: only call when the click point is on the button
00187 //        event.x = 0;
00188 //        event.y = 0;
00189 //        //event.deviceid = GDK_CORE_POINTER;
00190 //        event.type = GDK_BUTTON_PRESS;
00191 //        //event.source = GDK_SOURCE_MOUSE;
00192 //        event.time = 0; 
00193 //        event.window = button->window;
00194 //        event.send_event = TRUE;
00195 //        event.button = 1;
00196 //        gtk_widget_event (button, (GdkEvent *)&event);
00197 //        gdk_window_unref (button->window);
00198 //        while (gtk_events_pending())
00199 //            gtk_main_iteration();
00200 #endif
00201         }
00202 }
00203 
00204 bool wxFastComboEditor::EndEdit(int row, int col, const wxGrid* grid, const wxString& oldval, wxString* newval)
00205 {
00206     wxString value = Combo()->GetValue();
00207     bool changed = value != grid->GetTable()->GetValue(row, col);
00208 
00209     if ( changed )
00210         grid->GetTable()->SetValue(row, col, value);
00211 
00212     m_startValue = wxEmptyString;
00213     if (m_allowOthers)
00214         Combo()->SetValue(m_startValue);
00215     else {
00216         int i = Combo()->FindString(m_selection);
00217         if( i != wxNOT_FOUND) {
00218             Combo()->SetSelection(i);
00219         }
00220     }
00221                 //Combo()->SetSelection(0);
00222 
00223         return changed;
00224 }
00225 
00226 void wxFastComboEditor::Reset()
00227 {
00228         Combo()->SetValue(m_startValue);
00229         Combo()->SetInsertionPointEnd();
00230 }
00231 
00232 void wxFastComboEditor::SetParameters(const wxString& params)
00233 {
00234         if ( !params )
00235         {
00236                 return;
00237         }
00238         m_choices.Empty();
00239         wxStringTokenizer tk(params, _T(','));
00240         while ( tk.HasMoreTokens() )
00241         {
00242                 m_choices.Add(tk.GetNextToken());
00243         }
00244 }
00245 
00246 void wxFastComboEditor::SetParameters(size_t count, const wxString choices[])
00247 {
00248         m_choices.Empty();
00249         if ( count )
00250         {
00251                 m_choices.Alloc(count);
00252                 for ( size_t n = 0; n < count; n++ )
00253                 {
00254                         m_choices.Add(choices[n]);
00255                 }
00256         }
00257 }
00258 
00259 void wxFastComboEditor::SetParameters(const wxArrayString& choices)
00260 {
00261     if ( choices == (const wxArrayString)NULL )
00262     {
00263         return;
00264     }
00265     m_choices.Empty();
00266     m_choices = choices;
00267 }
00268 
00269 //JAH had to add in
00270 // return the value in the text control
00271 wxString wxFastComboEditor::GetValue() const
00272 {
00273   return Combo()->GetValue();
00274 }
00275 
00276 //JAH added
00277 void wxFastComboEditor::SetChoice(wxString& selection)
00278 {
00279   m_selection = selection;
00280   if(Combo() != NULL) {
00281     int i = Combo()->FindString(selection);
00282     Combo()->SetSelection(i);
00283   }
00284 }
00285 
00286 void wxFastComboEditor::AppendToCombo(wxString& append)
00287 {
00288   m_choices.push_back(append);
00289   if(Combo() != NULL ) {
00290     Combo()->Append(append);
00291   }
00292 }
00293 
00294 void wxFastComboEditor::ApplyEdit(int row, int col, wxGrid* grid)
00295 {
00296 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines