include/gui/widgets/wx/splitter.hpp

Go to the documentation of this file.
00001 #ifndef GUI_WX_DEMO___SPLITTER__HPP
00002 #define GUI_WX_DEMO___SPLITTER__HPP
00003 
00004 /*  $Id: splitter.hpp 18397 2008-11-28 19:45:17Z dicuccio $
00005  * ===========================================================================
00006  *
00007  *                            PUBLIC DOMAIN NOTICE
00008  *               National Center for Biotechnology Information
00009  *
00010  *  This software/database is a "United States Government Work" under the
00011  *  terms of the United States Copyright Act.  It was written as part of
00012  *  the author's official duties as a United States Government employee and
00013  *  thus cannot be copyrighted.  This software/database is freely available
00014  *  to the public for use. The National Library of Medicine and the U.S.
00015  *  Government have not placed any restriction on its use or reproduction.
00016  *
00017  *  Although all reasonable efforts have been taken to ensure the accuracy
00018  *  and reliability of the software and data, the NLM and the U.S.
00019  *  Government do not and cannot warrant the performance or results that
00020  *  may be obtained by using this software or data. The NLM and the U.S.
00021  *  Government disclaim all warranties, express or implied, including
00022  *  warranties of performance, merchantability or fitness for any particular
00023  *  purpose.
00024  *
00025  *  Please cite the author in any work or product based on this material.
00026  *
00027  * ===========================================================================
00028  *
00029  * Authors:  Andrey Yazhuk
00030  *
00031  * File Description:
00032  *      CSplitter - and extended version of splitter for wxWidgets.
00033  */
00034 
00035 #include <corelib/ncbistd.hpp>
00036 
00037 #include <gui/gui_export.h>
00038 
00039 #include <wx/panel.h>
00040 
00041 
00042 BEGIN_NCBI_SCOPE
00043 
00044 ///////////////////////////////////////////////////////////////////////////////
00045 /// CSplitter - container with resizable separators.
00046 ///
00047 /// CSplitter arranges child windows in to one- or two-dimensional array.
00048 /// Cells in CSplitter are divided by separators that can be moved by the
00049 /// mouse.  Every cell in CSplitter can be occupied by a single child window
00050 /// and every window can occupy only one cell.
00051 
00052 class  CSplitter
00053     : public wxPanel
00054 {   
00055     DECLARE_EVENT_TABLE()
00056 public:
00057     typedef wxPanel TParent;
00058     typedef vector<int> TPosVector;
00059     typedef vector<int> TSizeVector;
00060 
00061     /// ESplitMode - splitter orientation
00062     enum ESplitType  {
00063         eVertical,   /// vertical space is splitted
00064         eHorizontal, /// horizontal space is splitted
00065         eGrid        /// cells are separated with in both directions
00066     };
00067 
00068     CSplitter();
00069 
00070     CSplitter(wxWindow* parent,
00071                 wxWindowID id = wxID_ANY,
00072                 const wxPoint& pos = wxDefaultPosition,
00073                 const wxSize& size = wxDefaultSize,
00074                 long style = wxTAB_TRAVERSAL,
00075                 const wxString& name = _T("panel"));
00076 
00077     CSplitter(wxWindow* parent,
00078                 wxWindowID id = wxID_ANY,
00079                 ESplitType type = eVertical);
00080 
00081     virtual ~CSplitter();
00082 
00083     ESplitType   GetSplitType() const;
00084     int GetSeparatorSize() const;
00085 
00086     /// Creates a splitter with a single cell
00087     virtual void    Split(ESplitType type);
00088 
00089     /// Creates a splitter with cells defined by "sizes". If mode == eGrid then
00090     /// size will be used as both width and heights
00091     virtual void    Split(ESplitType type, const TPosVector& sizes);
00092 
00093     /// Creates a splitter with cells defined by "sizes". If mode == eGrid then
00094     /// ar_sizes will be used as both width and heights. "ar_sizes" is -1
00095     /// terminated array.
00096     virtual void    Split(ESplitType type, const int ar_size[]);
00097 
00098     /// Creates splitter, "widths" and "height" define number and sizes of
00099     /// cells.
00100     virtual void    Split(ESplitType type, const TPosVector& widths, const TPosVector& heights);
00101 
00102     /// Creates splitter, "ar_w" and "ar_h" are arrays terminated by -1.
00103     virtual void    Split(ESplitType type, const int ar_w[], const int ar_h[]);
00104 
00105     virtual void    Split(int col_n, int row_n);
00106 
00107     int     GetColumnsCount() const;
00108     int     GetRowsCount() const;
00109 
00110     void    GetWidths(TPosVector& widths)   const;
00111     void    SetWidths(const TPosVector& widths);
00112 
00113     void    GetHeights(TPosVector& heights) const;
00114     void    SetHeights(const TPosVector& heights);
00115 
00116     // is set, this cell will resize first trying to "consume" any changes in availble space
00117     // caused by the splitter resizing or separator dragging
00118     void    SetResizableCell(int col, int row);
00119 
00120     virtual bool    Layout();
00121     virtual bool    Show(bool show = true);
00122 
00123     virtual void    BlockLayout(bool block = true);
00124 
00125     /// returns "true" if cell specified by (col, row) exists in the splitter
00126     bool    IsValidCell(int col, int row) const;
00127 
00128     int GetColumnWidth(int col) const   {   return x_GetWidth(col);     }
00129     int GetRowHeight(int row) const     {   return x_GetHeight(row);    }
00130 
00131     /// If cell [col, row] exists and vacant - adds widget to the container.
00132     /// Returns "true" if widget has been added.
00133     bool    InsertToCell(wxWindow* child, int col, int row);
00134 
00135     void    AddColumn();
00136     void    AddRow();
00137 
00138     /// overriding wxWindowBase function
00139     virtual void RemoveChild(wxWindowBase* child);
00140 
00141     virtual bool    RemoveFromCell(int col, int row);
00142     virtual void    RemoveAll();
00143 
00144     wxWindow*         GetCell(int col, int row);
00145     const wxWindow*   GetCell(int col, int row) const;
00146 
00147     bool    FindChild(const wxWindow* child, int& col, int& row)  const;
00148 
00149     virtual void x_PreserveNormalSize(bool preserve);
00150 
00151 protected:
00152      virtual void   x_DrawSeparator(wxDC& dc, int x, int y, int w, int h);
00153      void x_SplitPositionsToSizes(const TPosVector& split_positions,
00154                                  int total_size, TPosVector& sizes);
00155      void x_SizesToSplitPositions(const TPosVector& sizes,
00156                                  TPosVector& split_positions);
00157 
00158     void    x_Init();   
00159 
00160     void    x_Split(ESplitType type, const TPosVector& widths, const TPosVector& heights);
00161 
00162     int     x_GetCellIndex(int i_x, int i_y) const;
00163     int     x_GetColumn(int i_cell) const;
00164     int     x_GetRow(int i_cell) const;
00165     int     x_GetCellIndex(wxWindow* widget) const;
00166 
00167     int     x_GetLeft(int i_x)  const;
00168     int     x_GetRight(int i_x)  const;
00169     int     x_GetWidth(int i_x)  const;
00170 
00171     int     x_GetTop(int i_y)     const;
00172     int     x_GetBottom(int i_y)  const;
00173     int     x_GetHeight(int i_y)  const;
00174 
00175     bool    x_RemoveChild(int index);
00176 
00177     void    x_Clear();
00178     void    x_ResizeToCell(int i_cell);
00179     void    x_ResizeToCell(int i_x, int i_y);
00180     void    x_ResizeAllCells();
00181 
00182     void    OnPaint(wxPaintEvent& event);   
00183     void    OnMouseMove(wxMouseEvent& event);
00184     void    OnLeftDown(wxMouseEvent& event);
00185     void    OnLeftUp(wxMouseEvent& event);
00186     void    OnMouseEnter(wxMouseEvent& event);
00187     void    OnMouseLeave(wxMouseEvent& event);
00188     void    OnContextMenu(wxContextMenuEvent& event);
00189     void    OnKeyDown(wxKeyEvent& event);
00190 
00191     void    x_RequestUpdate();
00192 
00193     /// returns Separator index if hit or -1
00194     int     x_HitTestSeparator(int z, TPosVector& vpos);
00195     void    x_HitTest(int z, TPosVector& vpos, int& i_cell, int& i_sep);
00196 
00197     virtual void    x_StartDrag(int i_sep_x, int i_sep_y);
00198     virtual void    x_DoDragSeparator(wxMouseEvent& event, bool b_final);   
00199     virtual void    x_EndDrag();
00200     virtual bool    x_IsDragging() const;
00201     
00202     void    x_DistributeEvenly(bool b_x, bool b_y);
00203     void    x_DoDistributeEvenly(TPosVector& vSplitPos, int size);
00204     
00205     virtual void    x_Resize(const wxSize& old_size, const wxSize& new_size, bool b_size_changes);
00206 
00207     static void x_ShrinkCell(TPosVector& sizes, TPosVector& min_sizes, TPosVector& norm_sizes,
00208                              int index, int& delta);
00209     static void x_GrowCell(TPosVector& sizes, TPosVector& norm_sizes,
00210                            int index, int& delta, bool unlimited = false);
00211 
00212     void    x_DoResize(TPosVector& sizes, TSizeVector& norm_sizes,
00213                        TPosVector& min_sizes, int size, int new_size,
00214                        int resizable_index);
00215 
00216     void    x_MoveSeparator(TPosVector& sizes, TSizeVector& norm_sizes, TPosVector& min_sizes,
00217                             int i_sep, int delta, int resizable_index);
00218 
00219     void    x_GetMinimized(vector<bool>& min_cols, vector<bool>& min_rows) const;
00220     void    x_CalculateMinSizes(TPosVector& min_widths,
00221                                 TPosVector& min_heights) const;
00222     
00223     // updates cells belonging to [start_x, stop_x] or to [start_y, stop_y]
00224     void    x_UpdateRegion(int start_x, int stop_x, int start_y, int stop_y);
00225     
00226     void    x_NewSplit(TPosVector& split_positions, TSizeVector& v_norm_size, int size);
00227     void    x_Deprecated()  const   {   _ASSERT(false); }
00228 
00229 protected:
00230     typedef vector<wxWindow*>  TCells;
00231 
00232     ESplitType m_Type;
00233 
00234     bool    m_BlockLayout;
00235     wxSize  m_LayoutedSize; // previous size for which layoting has been done
00236     wxSize  m_SplitSize; // the size corresponding to the current separaror positions
00237 
00238     TPosVector  m_vSplitPosX; // coordinates of first pixels of separators
00239     TPosVector  m_vSplitPosY; // coordinates of first pixels of separators
00240     int m_SepSize;
00241     TSizeVector m_vNormSizeX; // normal (not "shrinked") sizes of cells
00242     TSizeVector m_vNormSizeY; // normal (not "shrinked") sizes of cells
00243 
00244     TCells  m_Cells;
00245 
00246     int m_ResizableColumn;
00247     int m_ResizableRow;
00248 
00249     /// members used for event handling
00250     wxWindow* m_PrevFocus;
00251 
00252     int m_MouseDownX, m_MouseDownY; // window coordinate of last Mouse-Down
00253     int m_StartPosX, m_StartPosY; // start position of separators being dragged (local)
00254     int m_iDragSepX, m_iDragSepY; // index of separator being dragged or -1
00255     wxPoint m_PrevSepPos;
00256 
00257     TPosVector  m_MinWidths, m_MinHeights;
00258     int m_MinSepPosX, m_MaxSepPosX; // define the range within which the seprator can be move
00259     int m_MinSepPosY, m_MaxSepPosY;
00260 
00261     int    m_HighlightCell; // index of the highlighted cell
00262 };
00263 
00264 END_NCBI_SCOPE;
00265 
00266 
00267 #endif  // GUI_WX_DEMO___SPLITTER__HPP
00268 
00269 

Generated on Wed Dec 9 03:18:05 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Wed Dec 09 08:17:31 2009 by modify_doxy.py rev. 173732