首页 > 代码库 > Splitters
Splitters
1 //Splitters.cpp 2 3 /* 4 Copyright 2000-2004 The VCF Project. 5 Please see License.txt in the top level directory 6 where you installed the VCF. 7 */ 8 9 10 #define USING_SPLITTER 11 #define NUM_OF_PANELS 5 // 1-6 12 13 14 #include "vcf/ApplicationKit/ApplicationKit.h" 15 #include "vcf/ApplicationKit/ControlsKit.h" 16 17 using namespace VCF; 18 19 class SplittersWindow; 20 21 class MyPanel : public Panel 22 { 23 public: 24 MyPanel( SplittersWindow* mainWnd ) { 25 mainWnd_ = mainWnd; 26 MouseClicked += new ClassProcedure1<ControlEvent*,MyPanel>(this, &MyPanel::onMouseClicked, "MyPanel::onMouseClicked" ); 27 } 28 29 SplittersWindow* getMainWindow() { 30 return mainWnd_; 31 } 32 33 void onm ouseClicked( ControlEvent* e ); 34 35 public: 36 SplittersWindow* mainWnd_; 37 }; 38 39 class SplittersWindow : public Window { 40 public: 41 SplittersWindow() { 42 clickedPanel_ = NULL; 43 panelVisible_ = true; 44 45 setCaption( "Splitters" ); 46 47 ControlSized += new ClassProcedure1<ControlEvent*,SplittersWindow>(this, &SplittersWindow::onResized, "SplittersWindow::onResized" ); 48 ControlPositioned += new ClassProcedure1<ControlEvent*,SplittersWindow>(this, &SplittersWindow::onPosChanged, "SplittersWindow::onPosChanged" ); 49 50 Panel* buttonsPanel = new Panel(); 51 buttonsPanel->setUseColorForBackground( true ); 52 buttonsPanel->setHeight( 30 ); 53 buttonsPanel->setColor( Color::getColor("lightyellow") ); 54 add( buttonsPanel, AlignTop ); 55 56 57 btnShow_ = new CommandButton(); 58 btnShow_->ButtonClicked += 59 new ClassProcedure1<ButtonEvent*,SplittersWindow>(this,&SplittersWindow::onBtnShow, "SplittersWindow::onBtnShow"); 60 btnShow_->setBounds( 120, 2, 100, btnShow_->getPreferredHeight() ); 61 btnShow_->setCaption( "Hide" ); 62 btnShow_->setToolTipText( "Hide/Show any selected panel" ); 63 buttonsPanel->add( btnShow_, AlignLeft ); 64 65 Label* label = NULL; 66 label = new Label(); 67 label->setWidth( 50 ); 68 label->setWordWrap( true ); 69 label->setCaption( "Click into a panel first and then press the Hide/Show button." ); 70 buttonsPanel->add( label, AlignClient ); 71 72 label = new Label(); 73 label->setHeight( 20 ); 74 label->setWordWrap( true ); 75 label->setCaption( "Drag a splitter to resize a panel - Try to DoubleClick on a splitter - Try then with the <Shift> key" ); 76 label->setColor( Color::getColor("blue") ); 77 add( label, AlignBottom ); 78 79 80 81 main_ = new Panel; 82 main_->setHeight( 25 ); 83 main_->setUseColorForBackground( true ); 84 main_->setColor( Color::getColor("brown") ); 85 main_->setToolTipText( "main: AlignClient, brown" ); 86 add( main_, AlignClient ); 87 88 panelLeft_ = new Panel; 89 panelLeft_->setUseColorForBackground( true ); 90 panelLeft_->setWidth( main_->getWidth()/2 ); 91 panelLeft_->setColor( Color::getColor("cyan") ); 92 panelLeft_->setToolTipText( "left: AlignLeft, cyan" ); 93 main_->add( panelLeft_, AlignLeft ); 94 95 panelRight_ = new Panel; 96 panelRight_->setUseColorForBackground( true ); 97 panelRight_->setWidth( main_->getWidth()/2 ); 98 panelRight_->setColor( Color::getColor("navy") ); 99 panelRight_->setToolTipText( "right: AlignRight, navy" );100 main_->add( panelRight_, AlignRight );101 102 addPanels( panelLeft_ , NUM_OF_PANELS, AlignLeft );103 addPanels( panelRight_, NUM_OF_PANELS, AlignTop );104 }105 106 void addPanels( Panel* panel, int num, AlignmentType alignment )107 {108 panel->setContainer( new StandardContainer() );109 110 int n = maxVal<int>( minVal<int>( num, NUM_OF_PANELS ), 1 );111 int width = panel->getWidth() / n;112 int height = panel->getHeight() / n;113 114 std::vector< String > colors;115 colors.push_back( "red" );116 colors.push_back( "green" );117 colors.push_back( "blue" );118 colors.push_back( "violet" );119 colors.push_back( "yellow" );120 colors.push_back( "gold" );121 122 String alignments[6];123 alignments[AlignNone] = "AlignNone";124 alignments[AlignTop] = "AlignTop";125 alignments[AlignLeft] = "AlignLeft";126 alignments[AlignRight] = "AlignRight";127 alignments[AlignBottom] = "AlignBottom";128 alignments[AlignClient] = "AlignClient";129 130 String s, sTip;131 Splitter* splitter = NULL;132 133 for ( int i = 1; i <= n; i ++ ) {134 MyPanel* p = new MyPanel( this );135 p->setUseColorForBackground( true );136 p->setWidth( width );137 p->setHeight( height );138 p->setColor( Color::getColor( colors[i-1] ) );139 p->setBorder( NULL );140 s = Format( "panel%d" ) % i;141 p->setName( s );142 if ( i < n ) {143 sTip = Format( "p%d: %s, %s" ) % i % alignments[alignment] % colors[i-1];144 p->setToolTipText( sTip );145 panel->add( p, alignment );146 147 #ifdef USING_SPLITTER148 splitter = new Splitter( alignment );149 s = Format( "splitter %d" ) % i;150 splitter->setToolTipText( s );151 splitter->setToolTipText( s );152 splitter->setWidth( 5 );153 splitter->setMinimumWidth( 35 );154 splitter->setDblClickEnabled( true );155 panel->add( splitter, alignment );156 #endif157 } else {158 sTip = Format( "p%d: %s, %s" ) % i % alignments[AlignClient] % colors[i-1];159 p->setToolTipText( sTip );160 panel->add( p, AlignClient );161 }162 }163 }164 165 virtual ~SplittersWindow(){};166 167 void onBtnHide( ButtonEvent* e ) {168 int i = 1;169 String s = Format( "panel%d" ) % i;170 //Panel* panel = (Panel*)panelLeft_->findComponent( s );171 if ( NULL != clickedPanel_ ) {172 clickedPanel_->setVisible( false );173 int x = 0;174 }175 }176 177 void onBtnShow( ButtonEvent* e ) {178 //String s = Format( "panel%d" ) % i ;179 //Panel* panel = (Panel*)panelLeft_->findComponent( s );180 if ( NULL != clickedPanel_ ) {181 panelVisible_ = !panelVisible_;182 clickedPanel_->setVisible( panelVisible_ );183 if ( panelVisible_ ) {184 clickedPanel_ = NULL;185 btnShow_->setCaption( "Hide" );186 } else {187 btnShow_->setCaption( "Show " + clickedPanel_->getName() );188 }189 } else {190 Dialog::showMessage( "Please, click on a window first." );191 }192 }193 194 void setClickedPanel( Panel* panel ) {195 if ( panelVisible_ ) {196 clickedPanel_ = panel;197 } else {198 Dialog::showMessage( "Please, press the show/hide button first" );199 }200 }201 /*202 virtual void resizeChildren( Control* control ) {203 Window::resizeChildren( control );204 }205 */206 void onResized( ControlEvent*e ) {207 208 double w = main_->getWidth()/2;209 StringUtils::trace( Format("w: %0.2f\n") % w );210 211 panelLeft_->setWidth( w );212 213 panelRight_->setWidth( w );214 }215 216 void onPosChanged( ControlEvent*e ) {217 218 }219 220 public:221 Panel* main_;222 Panel* panelLeft_;223 Panel* panelRight_;224 Panel* clickedPanel_;225 bool panelVisible_;226 CommandButton* btnShow_;227 };228 229 void MyPanel::onMouseClicked( ControlEvent* e ) {230 //Panel* panel = (Panel*)e->getSource();231 getMainWindow()->setClickedPanel( this );232 getMainWindow()->btnShow_->setCaption( "Hide " + getName() );233 //Rect r = getBounds();234 //trace( Format( "New Left/Top: %0.2f, %0.2f\n", r.left_, r.top_ );235 }236 237 238 239 class SplittersApplication : public Application {240 public:241 242 SplittersApplication( int argc, char** argv ) : Application(argc, argv) {243 244 }245 246 virtual bool initRunningApplication(){247 bool result = Application::initRunningApplication();248 249 Window* mainWindow = new SplittersWindow();250 setMainWindow(mainWindow);251 mainWindow->setBounds( &Rect( 100.0, 100.0, 700.0, 700.0 ) );252 mainWindow->show();253 254 return result;255 }256 257 };258 259 260 int main(int argc, char *argv[])261 {262 Application* app = new SplittersApplication( argc, argv );263 264 Application::main();265 266 return 0;267 }268 269 270 /**271 $Id: Splitters.cpp 3184 2007-09-17 02:26:00Z ddiego $272 */
Splitters
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。