tablecontrol.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 2,673 行 · 第 1/5 页
CPP
2,673 行
//TableControl.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*//* Generated by Together */#include "vcf/ApplicationKit/ApplicationKit.h"#include "vcf/ApplicationKit/TableControl.h"#include "vcf/ApplicationKit/DefaultTableModel.h"#include "vcf/ApplicationKit/TableItemEditor.h"#include "vcf/ApplicationKit/Containers.h"using namespace VCF;#define TABLECONTROL_KBRD_HANDLER "TCKeyboardHandler"class FinishEditingEvent : public Event {public: FinishEditingEvent( Control* source, TableItemEditor* editor ): Event(source,0), editor_(editor){ } TableItemEditor* editor_;};TableControl::TableControl( TableModel* model ): CustomControl( true ), totalRowHeight_(0.0), drawGridLinesStyle_(TableControl::dglDrawBoth), enableSelection_(true), allowSingleColumnSelection_(true), allowSingleRowSelection_(true), allowFixedColumnSelection_(true), allowFixedRowSelection_(true), allowLiveResizing_(true), autoSizeStyle_(TableControl::asoBoth), defaultCellColor_(NULL), defaultCellFont_(NULL){ setContainerDelegate( this ); setViewModel( dynamic_cast<Model*>(model) ); init();}TableControl::~TableControl(){ defaultCellFont_->free(); defaultCellFont_ = NULL; defaultCellColor_->free(); defaultCellColor_ = NULL;}void TableControl::setDrawGridLinesStyle( DrawGridLines val ){ drawGridLinesStyle_ = val; repaint();}void TableControl::paint( GraphicsContext * context ){ Point oldOrigin = context->getOrigin(); Rect oldViewBounds = context->getViewableBounds(); CustomControl::paint( context ); Point origin = context->getOrigin(); Rect viewBounds = context->getViewableBounds(); bool hasScrollable = false; Scrollable* scrollable = getScrollable(); if ( NULL != scrollable ) { hasScrollable = true; } if ( hasScrollable ) { //reset origin context->setOrigin( oldOrigin ); context->setViewableBounds( oldViewBounds ); } TableModel* tm = getTableModel(); ulong32 rowCount = tm->getRowCount(); ulong32 columnCount = tm->getColumnCount(); if ( (rowCount > 0) && (columnCount > 0) ) { int gcs = context->saveState(); ulong32 fixedRowCount = tm->getFixedRowsCount(); ulong32 fixedColumnCount = tm->getFixedColumnsCount(); int fixedRowHeight = getFixedRowHeight(); int fixedColWidth = getFixedColumnWidth(); CellID topLeft = getTopLeftNonFixedCell(); int minVisibleRow = topLeft.row; int minVisibleCol = topLeft.column; Rect visibleRect; CellRange visibleCellRange = getVisibleNonFixedCellRange(&visibleRect); int maxVisibleRow = visibleCellRange.getMaxRow(); int maxVisibleCol = visibleCellRange.getMaxCol(); Rect rect; rect.bottom_ = -1; ulong32 rowHeight = 0; ulong32 colWidth = 0; TableCellItem* cellItem = NULL; ulong32 row, col; Rect clipRect = context->getViewableBounds(); //getClippingRect(); for (row = 0; row < fixedRowCount; row++) { rowHeight = rowHeights_[row]; if ( rowHeight <= 0 ) { continue; } rect.top_ = rect.bottom_+1; rect.bottom_ = rect.top_ + rowHeight-1; rect.right_ = -1; for (col = 0; col < fixedColumnCount; col++) { colWidth = columnWidths_[col]; if ( colWidth <= 0) { continue; } rect.left_ = rect.right_+1; rect.right_ = rect.left_ + colWidth-1; cellItem = tm->getItem( row, col ); cellItem->paint( context, &rect ); } } context->restoreState( gcs ); gcs = context->saveState(); // draw fixed column cells: m_nFixedRows..n, 0..m_nFixedCols-1 rect.bottom_ = fixedRowHeight-1; for (row = minVisibleRow; row <= maxVisibleRow; row++) { rowHeight = rowHeights_[row]; if ( rowHeight <= 0 ) { continue; } rect.top_ = rect.bottom_+1; rect.bottom_ = rect.top_ + rowHeight-1; // rect.bottom = bottom pixel of previous row if ( rect.top_ > clipRect.bottom_ ) { break; // Gone past cliprect } if ( rect.bottom_ < clipRect.top_ ) { continue; // Reached cliprect yet? } rect.right_ = -1; for (col = 0; col < fixedColumnCount; col++) { colWidth = columnWidths_[col]; if ( colWidth <= 0 ) { continue; } rect.left_ = rect.right_+1; rect.right_ = rect.left_ + colWidth-1; if ( rect.left_ > clipRect.right_ ) { break; // gone past cliprect } if ( rect.right_ < clipRect.left_ ) { continue; // Reached cliprect yet? } cellItem = tm->getItem( row, col ); cellItem->paint( context, &rect ); } } context->restoreState( gcs ); gcs = context->saveState(); // draw fixed row cells 0..m_nFixedRows, m_nFixedCols..n rect.bottom_ = -1; for (row = 0; row < fixedRowCount; row++) { rowHeight = rowHeights_[row]; if ( rowHeight <= 0) { continue; } rect.top_ = rect.bottom_+1; rect.bottom_ = rect.top_ + rowHeight-1; // rect.bottom = bottom pixel of previous row if ( rect.top_ > clipRect.bottom_ ) { break; // Gone past cliprect } if ( rect.bottom_ < clipRect.top_ ) { continue; // Reached cliprect yet? } rect.right_ = fixedColWidth-1; TableRowItemEnumerator* rowItemEnum = tm->getRowItemEnumerator( row ); col = 0; while ( rowItemEnum->hasMoreElements() && (col <= maxVisibleCol) ){ cellItem = rowItemEnum->nextElement(); if ( col >= minVisibleCol ) { colWidth = columnWidths_[col]; if ( colWidth > 0 ) { rect.left_ = rect.right_+1; rect.right_ = rect.left_ + colWidth-1; if (rect.left_ > clipRect.right_ ) { break; // gone past cliprect } if (rect.right_ < clipRect.left_) { continue; // Reached cliprect yet? } cellItem->paint( context, &rect ); } } col ++; } } context->restoreState( gcs ); gcs = context->saveState(); // draw rest of non-fixed cells if ( hasScrollable ) { Rect tmp( fixedColWidth, fixedRowHeight, viewBounds.right_, viewBounds.bottom_ ); //set clipping region clipRect = tmp; //context->setClippingRect( &tmp ); } rect.bottom_ = fixedRowHeight-1; for (row = minVisibleRow; row <= maxVisibleRow; row++) { rowHeight = rowHeights_[row]; if ( rowHeight <= 0) { continue; } rect.top_ = rect.bottom_+1; rect.bottom_ = rect.top_ + rowHeight-1; // rect.bottom = bottom pixel of previous row if (rect.top_ > clipRect.bottom_) { break; // Gone past cliprect } if (rect.bottom_ < clipRect.top_) { continue; // Reached cliprect yet? } rect.right_ = fixedColWidth-1; TableRowItemEnumerator* rowItemEnum = tm->getRowItemEnumerator( row ); col = 0; while ( rowItemEnum->hasMoreElements() && (col <= maxVisibleCol) ) { cellItem = rowItemEnum->nextElement(); if ( col >= minVisibleCol ) { colWidth = columnWidths_[col]; if ( colWidth > 0 ) { rect.left_ = rect.right_+1; rect.right_ = rect.left_ + colWidth-1; //StringUtils::traceWithArgs( "Cliprect: %s\n", clipRect.toString().c_str() ); if (rect.left_ > clipRect.right_ ) { break; // gone past cliprect } if (rect.right_ < clipRect.left_ ) { continue; // Reached cliprect yet? } //context->setColor( &Color(0xCCCCCC) ); //context->rectangle( &rect ); //context->fillPath(); cellItem->paint( context, &rect ); } } col ++; } } context->restoreState( gcs ); gcs = context->saveState(); context->setStrokeWidth( 1 ); context->setColor( &Color(0.0,0.0,0.0) ); // draw vertical lines (drawn at ends of cells) if ( drawGridLinesStyle_ == TableControl::dglDrawBoth || drawGridLinesStyle_ == TableControl::dglDrawVerticalLines ) { int x = fixedColWidth; for (col = minVisibleCol; col <= maxVisibleCol; col++) { colWidth = columnWidths_[col]; if ( colWidth <= 0 ) { continue; } x += colWidth; context->moveTo( x-1, fixedRowHeight ); context->lineTo( x-1, visibleRect.bottom_ ); } context->strokePath(); } // draw horizontal lines (drawn at bottom of each cell) if ( drawGridLinesStyle_ == TableControl::dglDrawBoth || drawGridLinesStyle_ == TableControl::dglDrawHorizontalLines ) { int y = fixedRowHeight; for (row = minVisibleRow; row <= maxVisibleRow; row++) { rowHeight = rowHeights_[row]; if ( rowHeight <= 0 ) { continue; } y += rowHeight; context->moveTo( fixedColWidth, y-1 ); context->lineTo( visibleRect.right_, y-1 ); } context->strokePath(); } context->restoreState( gcs ); } paintChildren( context );}void TableControl::init(){ autoResizeColumns_ = true; allowColumnResizing_ = true; allowRowResizing_ = true; hiddenColumnUnhide_ = false; hiddenRowUnhide_ = false; listMode_ = false; allowColumnHide_ = false; allowRowHide_ = false; defaultCellColor_ = new Color(); *defaultCellColor_ = *GraphicsToolkit::getSystemColor( SYSCOLOR_WINDOW ); defaultCellFont_ = new Font(); *defaultCellFont_ = *getFont(); //selectedCellItem_ = NULL; currentEditingControl_ = NULL; currentItemEditor_ = NULL; setContainer( new StandardContainer() ); defaultColumnWidth_ = DEFAULT_COLUMN_WIDTH; defaultRowHeight_ = UIToolkit::getUIMetricsManager()->getDefaultHeightFor( UIMetricsManager::htLabelHeight ); mouseState_ = msNone; resizeCaptureRange_ = 5; if ( NULL == getViewModel() ){ setTableModel( new DefaultTableModel() ); addComponent( getViewModel() ); } EventHandler* tmh = new TableModelEventHandler<TableControl>( this, &TableControl::onTableModelChanged, "TableModelHandler" ); TableModel* model = getTableModel(); model->TableCellAdded += tmh; model->TableCellDeleted += tmh; model->TableColumnsAdded += tmh; model->TableColumnsDeleted += tmh; model->TableRowsAdded += tmh; model->TableRowsDeleted += tmh; model->TableCellsSelected += tmh; ModelEventHandler<TableControl>* modelHandler = new ModelEventHandler<TableControl>( this, &TableControl::onTableModelEmptied, "ModelHandler" ); getViewModel()->addModelHandler( modelHandler ); KeyboardEventHandler<TableControl>* kh = new KeyboardEventHandler<TableControl>( this, &TableControl::onEditingControlKeyPressed, TABLECONTROL_KBRD_HANDLER ); EventHandler* focusLost = new FocusEventHandler<TableControl>( this, &TableControl::onFocusLost, "TableControl::onFocusLost" ); ScrollbarManager* scroller = new ScrollbarManager(); addComponent( scroller ) ; scroller->setHasHorizontalScrollbar( true ); scroller->setHasVerticalScrollbar( true ); scroller->setTarget( this ); EventHandler* ev = new GenericEventHandler<TableControl>(this, &TableControl::onVerticalScrolling, "TableControl::onVerticalScrolling" ); scroller->VerticalScrolling += ev; ev = new GenericEventHandler<TableControl>(this, &TableControl::onHorizontalScrolling, "TableControl::onHorizontalScrolling" ); scroller->HorizontalScrolling += ev;}void TableControl::onTableModelEmptied( ModelEvent* e ){ //this needs to happen here cause we receive this event //AFTER the table model's items have been cleared so the //model is now empty at this point currentItemEditor_ = NULL; finishEditing(); columnWidths_.clear(); rowHeights_.clear(); //selectedCellItem_ = NULL; resizeDragPt_.setNull(); lastMousePoint_.setNull(); leftClickPoint_.setNull(); clickCell_.row = -1; clickCell_.column = -1; selectionStartCell_.row = -1; selectionStartCell_.column = -1; currentCell_.row = -1; currentCell_.column = -1;}void TableControl::onTableModelChanged( TableModelEvent* event ){ finishEditing(); //selectedCellItem_ = NULL; TableModel* tm = getTableModel(); EventHandler* itemHandler = getEventHandler(TABLECELL_HANDLER); if ( NULL == itemHandler ) { //itemHandler = new } switch ( event->getType() ){ case COLUMNS_DELETED:{ } break; case COLUMNS_ADDED:{ int start = event->getStartColumnThatChanged(); for (int col=start;col<event->getNumberOfColumnsAffected()+start;col++ ) { columnWidths_.insert( columnWidths_.begin() + col, defaultColumnWidth_ ); } uint32 rowCount = tm->getRowCount(); for ( ulong32 row=0;row<rowCount;row++){ for (int col=start;col<event->getNumberOfColumnsAffected()+start;col++ ) { TableCellItem* item = tm->getItem( row, col ); if ( NULL != item ){ if ( NULL != itemHandler ) { item->addItemSelectedHandler( itemHandler ); } item->setControl( this ); item->setColor( getDefaultTableCellColor() ); item->setFont( getDefaultTableCellFont() ); } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?