gridcombo.cpp

Go to the documentation of this file.
00001 
00002 // Name:        gridcombo.cpp
00003 // Purpose:     A grid grid combo
00004 // Author:      Mikhail Tatarnikov
00005 // Modified by:
00006 // Created:     2006-08-16
00007 // RCS-ID:      $Id: $
00008 // Copyright:   (c) 2006 Xara Ltd
00009 // Licence:     wxWindows licence
00011 
00012 #include "gridcombo.h"
00013 #include <wx/utils.h>
00014 
00015 
00016 #define HIGHLITED_TIMERID       777
00017 #define HIGHLITED_TIMERINTERVAL 250
00018 
00019 
00020 
00021 BEGIN_EVENT_TABLE(wxGridComboPopup, wxScrolledWindow)
00022     EVT_TIMER(HIGHLITED_TIMERID, wxGridComboPopup::OnTimer)
00023     EVT_SIZE(wxGridComboPopup::OnSize)
00024     EVT_MOTION(wxGridComboPopup::OnMouseMove)
00025     EVT_KEY_DOWN(wxGridComboPopup::OnKey)
00026     EVT_LEFT_UP(wxGridComboPopup::OnLeftClick)
00027 END_EVENT_TABLE()
00028 
00029 
00030 
00031 wxGridComboPopup::wxGridComboPopup(wxGridCombo* pCombo)
00032     : m_tmrHighlited(this, HIGHLITED_TIMERID)
00033 {
00034     m_pCombo      = pCombo;
00035     m_iHighlighed = -1;
00036 
00037 }
00038 
00039 
00040 /******************************************************************************
00041 Function  : wxGridComboPopup::GetColumns
00042 Author    : Mikhail Tatarnikov
00043 Purpose   : A helper function to obtain the number of columns specified by the user
00044 Returns   : int - the number of columns to display in the popup.
00045 Exceptions: 
00046 Parameters: None
00047 Notes     : All the information is stored in one place - the combobox control itself.
00048 ******************************************************************************/
00049 int wxGridComboPopup::GetColumns() const
00050 {
00051     return m_pCombo->GetColumns();
00052 }
00053 
00054 /******************************************************************************
00055 Function  : intwxGridComboPopup::GetRows
00056 Author    : Mikhail Tatarnikov
00057 Purpose   : A helper function to obtain the number of rows that should be
00058             displayd. Not all rows will be visible at once.
00059 Returns   : int - the number of all rows (visible and invisible).
00060 Exceptions: 
00061 Parameters: None
00062 Notes     : All the required to calculate the rows coun is stored in one
00063             place - the combobox control itself.
00064 ******************************************************************************/
00065 int wxGridComboPopup::GetRows() const
00066 {
00067     return m_pCombo->GetRows();
00068 }
00069 
00070 /******************************************************************************
00071 Function  : intwxGridComboPopup::GetItemsNum
00072 Author    : Mikhail Tatarnikov
00073 Purpose   : A helper function to obtain the number of items in the combobx.
00074 Returns   : int - the number of items to display.
00075 Exceptions: 
00076 Parameters: None
00077 Notes     : All the information is stored in one place - the combobox control itself.
00078 ******************************************************************************/
00079 int wxGridComboPopup::GetItemsNum() const
00080 {
00081     return m_pCombo->GetItemsNum();
00082 }
00083 
00084 /******************************************************************************
00085 Function  : wxGridComboPopup::GetItemSize
00086 Author    : Mikhail Tatarnikov
00087 Purpose   : A helper function to obtain the size of items - e.g. the popup cell
00088             size.
00089 Returns   : wxSize - the items size.
00090 Exceptions: 
00091 Parameters: None
00092 Notes     : All the information is stored in one place - the combobox control itself.
00093             All items have the same size.
00094 ******************************************************************************/
00095 wxSize wxGridComboPopup::GetItemSize() const
00096 {
00097     return m_pCombo->GetItemSize();
00098 }
00099 
00100 /******************************************************************************
00101 Function  : wxGridComboPopup::GetSelected
00102 Author    : Mikhail Tatarnikov
00103 Purpose   : Get the currentrly selected element.
00104 Returns   : int - the index of currently selected element, -1 if none.
00105 Exceptions: 
00106 Parameters: None
00107 Notes     : 
00108 ******************************************************************************/
00109 int wxGridComboPopup::GetSelected() const
00110 {
00111     return m_pCombo->GetSelected();
00112 }
00113 
00114 
00115 /******************************************************************************
00116 Function  : wxGridComboPopup::SetSelected
00117 Author    : Mikhail Tatarnikov
00118 Purpose   : Selects an element
00119 Returns   : void
00120 Exceptions: 
00121 Parameters: [in] int iItem - an element index to select.
00122 Notes     : 
00123 ******************************************************************************/
00124 void wxGridComboPopup::SetSelected(int iItem)
00125 {
00126     m_pCombo->SetSelected(iItem);
00127 }
00128 
00129 
00130 /******************************************************************************
00131 Function  : wxGridComboPopup::Init
00132 Author    : Mikhail Tatarnikov
00133 Purpose   : Performs any initialization required.
00134 Returns   : void
00135 Exceptions: 
00136 Parameters: None
00137 Notes     : 
00138 ******************************************************************************/
00139 void wxGridComboPopup::Init()
00140 {
00141 }
00142 
00143 
00144 /******************************************************************************
00145 Function  : wxGridComboPopup::Create
00146 Author    : Mikhail Tatarnikov
00147 Purpose   : Creates the popup window
00148 Returns   : bool - true if successfull, false otherwise.
00149 Exceptions: 
00150 Parameters: [in] wxWindow* pwndParent - the parent window (combobox control).
00151 Notes     : 
00152 ******************************************************************************/
00153 bool wxGridComboPopup::Create(wxWindow* pwndParent)
00154 {
00155     if (!wxScrolledWindow::Create(pwndParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
00156         wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS | wxVSCROLL | wxHSCROLL))
00157         return false;
00158 
00159     SetAutoLayout(true);
00160 
00161     return true;
00162 }
00163 
00164 /******************************************************************************
00165 Function  : wxGridComboPopup::ChangeSelection
00166 Author    : Mikhail Tatarnikov
00167 Purpose   : Notification that the selection has been changed.
00168 Returns   : void
00169 Exceptions: 
00170 Parameters: [in] int iOldSelected - the prevoiusly selected item;
00171             [in] int iNewSelected - the newly selected item.
00172 Notes     : Control has to repqint both items.
00173 ******************************************************************************/
00174 void wxGridComboPopup::ChangeSelection(int iOldSelected, int iNewSelected)
00175 {
00176     InvalidateItem(iOldSelected);
00177     InvalidateItem(iNewSelected);
00178 }
00179 
00180 /******************************************************************************
00181 Function  : wxGridComboPopup::UpdateScrollers
00182 Author    : Mikhail Tatarnikov
00183 Purpose   : Update the scroll bars with the current information
00184             (items number, columns, sizes, etc).
00185 Returns   : void
00186 Exceptions: 
00187 Parameters: None
00188 Notes     : 
00189 ******************************************************************************/
00190 void wxGridComboPopup::UpdateScrollers()
00191 {
00192     wxSize szItem = GetItemSize();
00193 
00194     wxPoint ptGrid(0, 0);
00195     int iSelected = GetSelected();
00196     if (iSelected != -1)
00197         ptGrid = ItemToGrid(iSelected);
00198 
00199     SetScrollbars(szItem.x, szItem.y, GetColumns(), GetRows(),
00200         ptGrid.x, ptGrid.y);
00201 }
00202 
00203 
00204 /******************************************************************************
00205 Function  : wxGridComboPopup::ItemToGrid
00206 Author    : Mikhail Tatarnikov
00207 Purpose   : Calculates position of an item based on its index
00208 Returns   : wxPoint - the item position (row/column).
00209 Exceptions: 
00210 Parameters: [in] int iItem - item index to calculate positio for.
00211 Notes     : 
00212 ******************************************************************************/
00213 wxPoint wxGridComboPopup::ItemToGrid(int iItem)
00214 {
00215     wxPoint ptGrid;
00216     int iCols = GetColumns();
00217     ptGrid.x = iItem % iCols;
00218     ptGrid.y = iItem / iCols;
00219 
00220     return ptGrid;
00221 }
00222 
00223 /******************************************************************************
00224 Function  : wxGridComboPopup::ItemFromPoint
00225 Author    : Mikhail Tatarnikov
00226 Purpose   : Finds item based on point (in client virtual coordinates)
00227 Returns   : int - the item index which the point belong.
00228 Exceptions: 
00229 Parameters: [in] wxPoint ptClient - the point to find item from.
00230 Notes     : 
00231 ******************************************************************************/
00232 int wxGridComboPopup::ItemFromPoint(wxPoint ptClient)
00233 {
00234     // TODO: Optimise - calculate instead of brute force iterate.
00235 
00236     int iItemsNum = GetItemsNum();
00237     for (int i = 0; i < iItemsNum; ++i)
00238     {
00239         // Calculate the area of the current item.
00240         wxRect rcItem = GetItemRect(i);
00241 
00242         // Check if the item should be updated.
00243         if (rcItem.Inside(ptClient))
00244             return i;
00245     }
00246 
00247     return -1;
00248 }
00249 
00250 /******************************************************************************
00251 Function  : wxGridComboPopup::GetItemRect
00252 Author    : Mikhail Tatarnikov
00253 Purpose   : Calculates the item area (in client virtual coordinates)
00254 Returns   : wxRect - the area occupied by the item.
00255 Exceptions: 
00256 Parameters: [in] int iItem - item to calculate the area for.
00257 Notes     : 
00258 ******************************************************************************/
00259 wxRect wxGridComboPopup::GetItemRect(int iItem)
00260 {
00261     wxPoint ptGrid = ItemToGrid(iItem);
00262     wxSize  szItem = GetItemSize();
00263 
00264     ptGrid.x *= szItem.x;
00265     ptGrid.y *= szItem.y;
00266 
00267     wxRect rcItem(ptGrid, szItem);
00268 
00269     return rcItem;
00270 }
00271 
00272 
00273 /******************************************************************************
00274 Function  : wxGridComboPopup::ClientToScrolledVirtual
00275 Author    : Mikhail Tatarnikov
00276 Purpose   : Transfers a point from client to virtual (scrolled) coordinates
00277 Returns   : void
00278 Exceptions: 
00279 Parameters: [in/out] int* pX - the absciss value to convert;
00280             [in/out] int* pY - the ordinate value to convert.
00281 Notes     : 
00282 ******************************************************************************/
00283 void wxGridComboPopup::ClientToScrolledVirtual(int* pX, int* pY)
00284 {
00285     wxSize szItem    = GetItemSize();
00286     int iXOrigin = 0;
00287     int iYOrigin = 0;
00288     GetViewStart(&iXOrigin, &iYOrigin);
00289     
00290     if (pX)
00291         *pX += iXOrigin * szItem.x;
00292         
00293     if (pY)
00294         *pY += iYOrigin * szItem.y;
00295 
00296 }
00297 
00298 
00299 /******************************************************************************
00300 Function  : wxGridComboPopup::OnShow
00301 Author    : Mikhail Tatarnikov
00302 Purpose   : Notification that the popup is about to be shown
00303 Returns   : void
00304 Exceptions: 
00305 Parameters: None
00306 Notes     : 
00307 ******************************************************************************/
00308 void wxGridComboPopup::OnShow()
00309 {
00310 }
00311 
00312 /******************************************************************************
00313 Function  : wxGridComboPopup::OnSize
00314 Author    : Mikhail Tatarnikov
00315 Purpose   : Resize notification handler.
00316 Returns   : void
00317 Exceptions: 
00318 Parameters: [in] wxSizeEvent& event - size event information.
00319 Notes     : We need to update our scroll bars.
00320 ******************************************************************************/
00321 void wxGridComboPopup::OnSize(wxSizeEvent& event)
00322 {
00323     wxScrolledWindow::OnSize(event);
00324     
00325     UpdateScrollers();
00326 }
00327 
00328 
00329 /******************************************************************************
00330 Function  : wxGridComboPopup::OnMouseMove
00331 Author    : Mikhail Tatarnikov
00332 Purpose   : mouse movement notification handler
00333 Returns   : void
00334 Exceptions: 
00335 Parameters: [in] wxMouseEvent& event - mouse event information.
00336 Notes     : 
00337 ******************************************************************************/
00338 void wxGridComboPopup::OnMouseMove(wxMouseEvent& event)
00339 {
00340     // Convert the poin to the logical coordinates.
00341     wxPoint ptMouse(event.GetX(), event.GetY());
00342     ClientToScrolledVirtual(&ptMouse.x, &ptMouse.y);    
00343 
00344     // Find out what item the mouse hover over.
00345     int iItem = ItemFromPoint(ptMouse);
00346 
00347     // Check if mouse is hovered withing the same item.
00348     if (iItem == m_iHighlighed)
00349         return;
00350 
00351     // If an item is hightlighted we need to start "dishighlight" timer
00352     // since this message can be the last one before leaving the control.
00353     if (iItem != -1)
00354         m_tmrHighlited.Start(HIGHLITED_TIMERINTERVAL);
00355 
00356     // Repaint the old and new items.
00357     InvalidateItem(m_iHighlighed);
00358     InvalidateItem(iItem);
00359 
00360     m_iHighlighed = iItem;
00361 }
00362 
00363 
00364 /******************************************************************************
00365 Function  : wxGridComboPopup::OnTimer
00366 Author    : Mikhail Tatarnikov
00367 Purpose   : Timer notification handler
00368 Returns   : void
00369 Exceptions: 
00370 Parameters: [in] wxTimerEvent& event - time event information.
00371 Notes     : We need it to trace situations when the mouse highlight an item and
00372             then leave the control leaving the item highlighted.
00373 ******************************************************************************/
00374 void wxGridComboPopup::OnTimer(wxTimerEvent& event)
00375 {
00376     if (m_iHighlighed == -1)
00377     {
00378         m_tmrHighlited.Stop();
00379         return;
00380     }
00381 
00382     wxMouseState oMouseState = wxGetMouseState();
00383     wxPoint ptMouse(oMouseState.GetX(), oMouseState.GetY());
00384 
00385     ScreenToClient(&(ptMouse.x), &(ptMouse.y));
00386 
00387     int iItem = ItemFromPoint(ptMouse);
00388 
00389     if (iItem == -1)
00390     {
00391         m_tmrHighlited.Stop();
00392         
00393         int iCurHighlighted = m_iHighlighed;
00394         m_iHighlighed        = -1;
00395         InvalidateItem(iCurHighlighted);
00396     }
00397 
00398 }
00399 
00400 
00401 /******************************************************************************
00402 Function  : wxGridComboPopup::OnKey
00403 Author    : Mikhail Tatarnikov
00404 Purpose   : Keyboard notification handler.
00405 Returns   : void
00406 Exceptions: 
00407 Parameters: [in] wxKeyEvent& event - keyboard event information.
00408 Notes     : 
00409 ******************************************************************************/
00410 void wxGridComboPopup::OnKey(wxKeyEvent& event)
00411 {
00412 }
00413 
00414 
00415 /******************************************************************************
00416 Function  : wxGridComboPopup::OnLeftClick
00417 Author    : Mikhail Tatarnikov
00418 Purpose   : Left mouse button click handler
00419 Returns   : void
00420 Exceptions: 
00421 Parameters: [in] wxMouseEvent& event - mouse event information.
00422 Notes     : 
00423 ******************************************************************************/
00424 void wxGridComboPopup::OnLeftClick(wxMouseEvent& event)
00425 {
00426     // Convert the poin to the logical coordinates.
00427     wxPoint ptMouse(event.GetX(), event.GetY());
00428     ClientToScrolledVirtual(&ptMouse.x, &ptMouse.y);    
00429     
00430     // Select a new item, if any was clicked.
00431     int iItem = ItemFromPoint(ptMouse);
00432     if (iItem != -1)
00433         SetSelected(iItem);
00434 
00435     DismissWithEvent();
00436 }
00437 
00438 /******************************************************************************
00439 Function  : wxGridComboPopup::DismissWithEvent
00440 Author    : Mikhail Tatarnikov
00441 Purpose   : Helper  function to setup a new selection, close the popup and send
00442             a notification message.
00443 Returns   : void
00444 Exceptions: 
00445 Parameters: None
00446 Notes     : 
00447 ******************************************************************************/
00448 void wxGridComboPopup::DismissWithEvent()
00449 {
00450     Dismiss();
00451 
00452     int iSelected = GetSelected();
00453     SendComboBoxEvent(iSelected);
00454 }
00455 
00456 /******************************************************************************
00457 Function  : wxGridComboPopup::SendComboBoxEvent
00458 Author    : Mikhail Tatarnikov
00459 Purpose   : Sends a combobox selection event
00460 Returns   : void
00461 Exceptions: 
00462 Parameters: [in] int iSelection - a new selection to notify clients about.
00463 Notes     : 
00464 ******************************************************************************/
00465 void wxGridComboPopup::SendComboBoxEvent(int iSelection)
00466 {
00467     wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED, m_combo->GetId());
00468 
00469     evt.SetEventObject(m_pCombo);
00470 
00471     evt.SetInt(iSelection);
00472 
00473     // Set client data, if any
00474     if (iSelection >= 0 && GetItemsNum() > iSelection)
00475     {
00476         CGridComboUserData* pClientData = m_pCombo->GetUserData(iSelection);
00477         evt.SetClientData(pClientData);
00478     }
00479 
00480     m_combo->GetEventHandler()->AddPendingEvent(evt);
00481 }
00482 
00483 
00484 /******************************************************************************
00485 Function  : wxGridComboPopup::GetAdjustedSize
00486 Author    : Mikhail Tatarnikov
00487 Purpose   : Calculates preferable size.
00488 Returns   : wxSize - the final size requested.
00489 Exceptions: 
00490 Parameters: [in] int iMinWidth   - minimum width for the control. In some circumstances
00491                                    can be ignored;
00492             [in] int iPrefHeight - preferable height;
00493             [in] int iMaxHeight  - maximum of height allowed.
00494 Notes     : 
00495 ******************************************************************************/
00496 wxSize wxGridComboPopup::GetAdjustedSize(int iMinWidth, int iPrefHeight, int iMaxHeight)
00497 {
00498     wxSize szAdjusted(0, 0);
00499     wxSize szItem   = GetItemSize();
00500     int    iColumns = GetColumns();
00501     int    iRows    = GetRows();
00502     
00503     szAdjusted.x = szItem.x * iColumns;
00504 
00505     int iRowsFit = iPrefHeight / szItem.y + 1;
00506 
00507     // Check if we fit the maximum height.
00508     int iHeight = iRowsFit * szItem.y;
00509     if (iHeight > iMaxHeight)
00510         iHeight = --iRowsFit * szItem.y;
00511 
00512     // We must show at least one row.
00513     iRowsFit = std::max(iRowsFit, 1);
00514     
00515     // No point in showing more rows than we have.
00516     iRowsFit = std::min(iRowsFit, iRows);
00517 
00518     szAdjusted.y = iRowsFit * szItem.y;
00519 
00520     // Check if a slider is required.
00521     if (iRowsFit < iRows)
00522         szAdjusted.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
00523 
00524     // Take into account the border.
00525     szAdjusted.x += 2;
00526     szAdjusted.y += 2;
00527     
00528     
00529     return szAdjusted;
00530 }
00531 
00532 /******************************************************************************
00533 Function  : wxGridComboPopup::InvalidateItem
00534 Author    : Mikhail Tatarnikov
00535 Purpose   : Requests repaint of an item.
00536 Returns   : void
00537 Exceptions: 
00538 Parameters: [in] int iItem - an item to repaint.
00539 Notes     : 
00540 ******************************************************************************/
00541 void wxGridComboPopup::InvalidateItem(int iItem)
00542 {
00543     if (iItem == -1)
00544         return;
00545 
00546     int iX = 0;
00547     int iY = 0;
00548     
00549     wxRect rcItem = GetItemRect(iItem);
00550     
00551     ClientToScrolledVirtual(&iX, &iY);
00552     rcItem.x -= iX;
00553     rcItem.y -= iY;
00554     
00555     Refresh(false, &rcItem);
00556 }
00557 
00558 
00559 /******************************************************************************
00560 Function  : wxGridComboPopup::OnDraw
00561 Author    : Mikhail Tatarnikov
00562 Purpose   : The default implementation of drawing.
00563 Returns   : void
00564 Exceptions: 
00565 Parameters: [in] wxDC& dc - device context of the control to paint to.
00566 Notes     : Breaks the control to separate items and sends a request for painting
00567             each item via DrawItem pure virtual function.
00568 ******************************************************************************/
00569 void wxGridComboPopup::OnDraw(wxDC& dc)
00570 {
00571     wxRect rcInvalid = GetUpdateClientRect();
00572     ClientToScrolledVirtual(&(rcInvalid.x), &(rcInvalid.y));
00573 
00574     // Iterate over all items, simply figing out whether we
00575     // need to update it or not.
00576     // TODO: optimize - iterate over visible items only.
00577     int iItemsNum = GetColumns() * GetRows();
00578     for (int i = 0; i < iItemsNum; ++i)
00579     {
00580         // Calculate the area of the current item.
00581         wxRect rcItem = GetItemRect(i);
00582 
00583         // Check if the item should be updated.
00584         if (!rcItem.Intersects(rcInvalid))
00585             continue;
00586 
00587         // Check if the item is selected or highlighted.
00588         int iFlags = 0;
00589         if (i == GetSelected())
00590             iFlags |= keSelected;
00591 
00592         if (i == m_iHighlighed)
00593             iFlags |= keHighlight;
00594 
00595         DrawItem(dc, rcItem, i, iFlags);
00596 
00597     }
00598 }
00599 
00600 /******************************************************************************
00601 Function  : wxGridComboPopup::PaintComboControl
00602 Author    : Mikhail Tatarnikov
00603 Purpose   : Paints an item in the combo control itself.
00604 Returns   : void
00605 Exceptions: 
00606 Parameters: [in] wxDC&         dc   - dc to paint to;
00607             [in] const wxRect& rect - area for painting.
00608 Notes     : Uses DrawItem pure virtual function.
00609 ******************************************************************************/
00610 void wxGridComboPopup::PaintComboControl(wxDC& dc, const wxRect& rect)
00611 {
00612     int iSelected = GetSelected();
00613 /*  if (iSelected == -1)
00614     {
00615         wxComboPopup::PaintComboControl(dc, rect);
00616         return;
00617     }
00618 */
00619     int iFlags = keComboControl;
00620     if (!m_pCombo->IsEnabled())
00621         iFlags |= keDisabled;
00622 
00623     DrawItem(dc, rect, iSelected, iFlags);
00624 }
00625 
00626 
00627 /******************************************************************************
00628 Function  : wxGridComboPopup::DrawItem
00629 Author    : Mikhail Tatarnikov
00630 Purpose   : Draws item based on row and column
00631 Returns   : void
00632 Exceptions: 
00633 Parameters: [in] wxDC&   dc        - device context to paint to;
00634             [in] wxRect& rcRect    - the item position;
00635             [in] int     iRow      - element's row;
00636             [in] int     iCol      - element's column;
00637             [in] bool    bSelected - indicate whether the item is selected or not.
00638 Notes     : Calculates the item index and dispatch the call to descendants via
00639             DrawItem pure virtual function.
00640 ******************************************************************************/
00641 void wxGridComboPopup::DrawItem(wxDC& dc, const wxRect& rcRect, int iRow, int iCol, bool bSelected)
00642 {
00643     int iItem = iRow * GetColumns() + iCol;
00644 
00645     int iFlags = 0;
00646     if (bSelected)
00647         iFlags |= keSelected;
00648 
00649     DrawItem(dc, rcRect, iItem, iFlags);
00650 }
00651 
00652 
00653 /******************************************************************************
00654 Function  : wxGridComboPopup::UpdateColumnsNum
00655 Author    : Mikhail Tatarnikov
00656 Purpose   : Updates the coumn number.
00657 Returns   : void
00658 Exceptions: 
00659 Parameters: None
00660 Notes     : 
00661 ******************************************************************************/
00662 void wxGridComboPopup::UpdateColumnsNum()
00663 {
00664     UpdateScrollers();
00665 }
00666 
00667 
00668 
00669 
00670 
00671 
00672 
00673 
00674 
00675 
00676 IMPLEMENT_DYNAMIC_CLASS(wxGridCombo, wxComboCtrl)
00677 
00678 /******************************************************************************
00679 Function  : wxGridCombo::Init
00680 Author    : Mikhail Tatarnikov
00681 Purpose   : Initialization routine
00682 Returns   : void
00683 Exceptions: 
00684 Parameters: None
00685 Notes     : 
00686 ******************************************************************************/
00687 void wxGridCombo::Init()
00688 {
00689     m_iColumns  = 1;    // A standard list box with one column by default.
00690     m_iSelected = -1;
00691 }
00692 
00693 wxGridCombo::wxGridCombo(wxWindow* pwndParent,
00694                          wxWindowID wiID,
00695                          const wxPoint& crptPos,
00696                          const wxSize& crszSize,
00697                          long lStyle,
00698                          const wxString& crstrName)
00699 {
00700     Init();
00701     Create(pwndParent, wiID, crptPos, crszSize, lStyle, crstrName);
00702 }
00703 
00704 /******************************************************************************
00705 Function  : wxGridCombo::Create
00706 Author    : Mikhail Tatarnikov
00707 Purpose   : Creates the control
00708 Returns   : bool - see wxWindow::Create
00709 Exceptions: 
00710 Parameters: see wxWindow::Create.
00711 Notes     : 
00712 ******************************************************************************/
00713 bool wxGridCombo::Create(wxWindow* pwndParent,
00714                            wxWindowID wiID,
00715                            const wxPoint& crptPos,
00716                            const wxSize& crszSize,
00717                            long lStyle,
00718                            const wxString& crstrName)
00719 {
00720     if (!wxComboCtrl::Create(pwndParent, wiID, wxEmptyString, crptPos, crszSize, lStyle, wxDefaultValidator, crstrName))
00721         return false;
00722 
00723     return true;
00724 }
00725 
00726 /******************************************************************************
00727 Function  : wxGridCombo::~wxGridCombo
00728 Author    : Mikhail Tatarnikov
00729 Purpose   : Destroys the object
00730 Returns   : void
00731 Exceptions: 
00732 Parameters: None
00733 Notes     :
00734 ******************************************************************************/
00735 wxGridCombo::~wxGridCombo()
00736 {
00737     DeleteUserData();
00738 }
00739 
00740 /******************************************************************************
00741 Function  : wxGridCombo::DeleteUserData
00742 Author    : Mikhail Tatarnikov
00743 Purpose   : Deletes all user data for itmes.
00744 Returns   : void
00745 Exceptions: 
00746 Parameters: None
00747 Notes     : 
00748 ******************************************************************************/
00749 void wxGridCombo::DeleteUserData()
00750 {
00751     std::vector<CGridComboUserData*>::const_iterator citCur = m_vecUserData.begin();
00752     for (; citCur != m_vecUserData.end(); ++citCur)
00753         delete *citCur;
00754 
00755     m_vecUserData.clear();
00756 }
00757 
00758 /******************************************************************************
00759 Function  : wxGridCombo::AddItem
00760 Author    : Mikhail Tatarnikov
00761 Purpose   : Adds a new item (appended to the end)
00762 Returns   : void
00763 Exceptions: 
00764 Parameters: [in] CGridComboUserData* pUserData - a new item to add.
00765 Notes     : 
00766 ******************************************************************************/
00767 void wxGridCombo::AddItem(CGridComboUserData* pUserData)
00768 {
00769     m_vecUserData.push_back(pUserData);
00770     
00771     wxGridComboPopup* pPopup = (wxGridComboPopup*)GetPopupControl();
00772     if (pPopup)
00773         pPopup->UpdateScrollers();
00774 }
00775 
00776 /******************************************************************************
00777 Function  : wxGridCombo::Clear
00778 Author    : Mikhail Tatarnikov
00779 Purpose   : Removes all elements from the combobox
00780 Returns   : void
00781 Exceptions: 
00782 Parameters: None
00783 Notes     : 
00784 ******************************************************************************/
00785 void wxGridCombo::Clear()
00786 {
00787     DeleteUserData();
00788     
00789     wxGridComboPopup* pPopup = (wxGridComboPopup*)GetPopupControl();
00790 
00791     if (pPopup)
00792         pPopup->UpdateScrollers();
00793 }
00794 
00795 
00796 /******************************************************************************
00797 Function  : wxGridCombo::GetUserData
00798 Author    : Mikhail Tatarnikov
00799 Purpose   : Retrieves user data associated with an item.
00800 Returns   : CGridComboUserData* - the user data for the item, if any.
00801 Exceptions: 
00802 Parameters: [in] int iIndex - index of the item to retrieve associated user data for.
00803 Notes     : 
00804 ******************************************************************************/
00805 CGridComboUserData* wxGridCombo::GetUserData(int iIndex)
00806 {
00807     if (iIndex < GetItemsNum())
00808         return m_vecUserData[iIndex];
00809 
00810     return NULL;
00811 }
00812 
00813 /******************************************************************************
00814 Function  : wxGridCombo::GetRows
00815 Author    : Mikhail Tatarnikov
00816 Purpose   : Calculates the number of rows.
00817 Returns   : int - the number of rows.
00818 Exceptions: 
00819 Parameters: None
00820 Notes     : 
00821 ******************************************************************************/
00822 int wxGridCombo::GetRows()
00823 {
00824     // Calculate the full rows based on the item number and the columns requested.
00825     // Then add a partial row if required.
00826     int iRows = GetItemsNum() / GetColumns();
00827     if (GetItemsNum() % GetColumns() != 0)
00828         iRows++;
00829 
00830     return iRows;
00831 }
00832 
00833 
00834 /******************************************************************************
00835 Function  : wxGridCombo::SetItemSize
00836 Author    : Mikhail Tatarnikov
00837 Purpose   : Sets the item size
00838 Returns   : void
00839 Exceptions: 
00840 Parameters: [in] wxSize szItem - a new size for items.
00841 Notes     : All items have the same size.
00842 ******************************************************************************/
00843 void wxGridCombo::SetItemSize(wxSize szItem)
00844 {
00845     m_szItem = szItem;
00846 
00847     wxGridComboPopup* pPopup = (wxGridComboPopup*)GetPopupControl();
00848 
00849     if (!pPopup)
00850         return;
00851 
00852     pPopup->UpdateScrollers();
00853 }
00854 
00855 
00856 /******************************************************************************
00857 Function  : wxGridCombo::SetColumns
00858 Author    : Mikhail Tatarnikov
00859 Purpose   : Sets the number of columns
00860 Returns   : void
00861 Exceptions: 
00862 Parameters: [in] int iCols - new column number.
00863 Notes     : 
00864 ******************************************************************************/
00865 void wxGridCombo::SetColumns(int iCols)
00866 {
00867     m_iColumns = iCols;
00868     
00869     wxGridComboPopup* pPopup = (wxGridComboPopup*)GetPopupControl();
00870 
00871     if (!pPopup)
00872         return;
00873 
00874     pPopup->UpdateColumnsNum();
00875 }
00876 
00877 
00878 /******************************************************************************
00879 Function  : wxGridCombo::SetSelected
00880 Author    : Mikhail Tatarnikov
00881 Purpose   : Selects an item.
00882 Returns   : void
00883 Exceptions: 
00884 Parameters: [in] int iItem - an item to select, -1 for unselecting.
00885 Notes     : 
00886 ******************************************************************************/
00887 void wxGridCombo::SetSelected(int iItem)
00888 {
00889     // Check if we actually changed anything.
00890     if (GetSelected() == iItem)
00891         return;
00892 
00893     wxGridComboPopup* pPopup = (wxGridComboPopup*)GetPopupControl();
00894 
00895     if (!pPopup)
00896         return;
00897 
00898     pPopup->ChangeSelection(GetSelected(), iItem);
00899 
00900     m_iSelected = iItem;
00901 
00902     // Invalidate the combo control.
00903     Refresh(false);
00904 }
00905 
00906 /******************************************************************************
00907 Function  : wxGridCombo::GetSelected
00908 Author    : Mikhail Tatarnikov
00909 Purpose   : Gets the selected item
00910 Returns   : int - the index of the selected item, -1 otherwise.
00911 Exceptions: 
00912 Parameters: None
00913 Notes     : 
00914 ******************************************************************************/
00915 int wxGridCombo::GetSelected() const
00916 {
00917     return m_iSelected;
00918 }

Generated on Sat Nov 10 03:49:01 2007 for Camelot by  doxygen 1.4.4