首页 > 代码库 > Dialog

Dialog

  1 //Dialogs.cpp  2   3   4 #include "vcf/ApplicationKit/ApplicationKit.h"  5 #include "vcf/ApplicationKit/ControlsKit.h"  6 #include "vcf/ApplicationKit/HorizontalLayoutContainer.h"  7   8   9 using namespace VCF; 10  11 /** 12 This example demonstrates how to use dialogs 13 */ 14  15  16 /** 17 This demonstrates how to create a very simple 18 dialog complete with OK/Cancel buttons, and 19 horizontal layout that takes into consideration 20 the proper (as specified in the UI metrics for the 21 platform) horizontal and vertial spacing between 22 controls. 23 */ 24 class MyDialog : public Dialog { 25 public: 26     MyDialog() { 27  28         setWidth( 350 ); 29         setHeight( 150 ); 30  31         Panel* pane1 = new Panel(); 32         pane1->setBorder( NULL ); 33  34         HorizontalLayoutContainer* container = new HorizontalLayoutContainer(); 35         pane1->setContainer( container ); 36  37  38  39         Label* lable1 = new Label(); 40         lable1->setCaption( "Name:" ); 41         pane1->add( lable1 ); 42  43         TextControl* edit1 = new TextControl(); 44  45         pane1->add( edit1 ); 46  47  48         Label* lable2 = new Label(); 49         lable2->setCaption( "Are you Mergatroid?:" ); 50         pane1->add( lable2 ); 51  52         CheckBoxControl* checkBox = new CheckBoxControl(); 53         checkBox->setCaption( "Yes I am!" ); 54         pane1->add( checkBox ); 55  56         add( pane1, AlignClient ); 57  58         CommandButton* okBtn = new CommandButton(); 59  60         Panel* bottom = new Panel(); 61         bottom->setBorder( NULL ); 62  63         bottom->setHeight( okBtn->getPreferredHeight() + UIToolkit::getUIMetricValue( UIMetricsManager::mtContainerBorderDelta) * 2 ); 64  65  66         HorizontalLayoutContainer* container2 = new HorizontalLayoutContainer(); 67         container2->setLeftBorderWidth( getWidth() / 2.0 ); 68  69         double width = getWidth() / 2.0; 70         width -= UIToolkit::getUIMetricValue(UIMetricsManager::mtContainerBorderDelta); 71         width -= container2->getColumnTweenWidth( 0 ); 72  73         container2->setColumnWidth( 0, width / 2.0 ); 74  75         bottom->setContainer( container2 ); 76  77  78  79         bottom->add( okBtn ); 80  81         CommandButton* cancelBtn = new CommandButton(); 82         bottom->add( cancelBtn ); 83  84         okBtn->setCaption( "OK" ); 85         okBtn->setCommandType ( BC_OK ); 86         okBtn->setDefault(true); 87  88         cancelBtn->setCaption( "Cancel" ); 89         cancelBtn->setCommandType ( BC_CANCEL ); 90  91         add( bottom, AlignBottom ); 92  93         edit1->setFocused(); 94  95         setCaption( "Mergatroid Questionaire" ); 96     } 97 }; 98  99 100 101 class DialogsWindow : public Window {102 public:103     DialogsWindow() {104         setCaption( "Dialogs" );105 106 107         CommandButton* btn1 = new CommandButton();108         btn1->setBounds( 10, 20, 175, btn1->getPreferredHeight() );109         btn1->setCaption( "Dialog::showMessage 1" );110         btn1->ButtonClicked +=111             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example1, "DialogsWindow::example1" );112         add( btn1 );113 114 115         CommandButton* btn2 = new CommandButton();116         btn2->setBounds( 10, btn1->getBottom() + 20, 175, btn2->getPreferredHeight() );117         btn2->setCaption( "Dialog::showMessage 2" );118         btn2->ButtonClicked +=119             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example2, "DialogsWindow::example2" );120         add( btn2 );121 122 123         CommandButton* btn3 = new CommandButton();124         btn3->setBounds( 10, btn2->getBottom() + 20, 175, btn3->getPreferredHeight() );125         btn3->setCaption( "Show custom dialog" );126         btn3->ButtonClicked +=127             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example3, "DialogsWindow::example3" );128         add( btn3 );129 130         CommandButton* btn4 = new CommandButton();131         btn4->setBounds( 10, btn3->getBottom() + 20, 175, btn4->getPreferredHeight() );132         btn4->setCaption( "Show Common Font Dialog" );133         btn4->ButtonClicked +=134             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example4, "DialogsWindow::example4" );135         add( btn4 );136 137         CommandButton* btn5 = new CommandButton();138         btn5->setBounds( 10, btn4->getBottom() + 20, 175, btn4->getPreferredHeight() );139         btn5->setCaption( "Show Common Color Dialog" );140         btn5->ButtonClicked +=141             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example5, "DialogsWindow::example5" );142         add( btn5 );143 144         CommandButton* btn6 = new CommandButton();145         btn6->setBounds( 10, btn5->getBottom() + 20, 175, btn6->getPreferredHeight() );146         btn6->setCaption( "Show Common File Browse Dialog" );147         btn6->ButtonClicked +=148             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example6, "DialogsWindow::example6" );149         add( btn6 );150 151         CommandButton* btn7 = new CommandButton();152         btn7->setBounds( 10, btn6->getBottom() + 20, 175, btn6->getPreferredHeight() );153         btn7->setCaption( "Show Common File Open Dialog" );154         btn7->ButtonClicked +=155             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example7, "DialogsWindow::example7" );156         add( btn7 );157 158         CommandButton* btn8 = new CommandButton();159         btn8->setBounds( 10, btn7->getBottom() + 20, 175, btn6->getPreferredHeight() );160         btn8->setCaption( "Show Common File Save Dialog" );161         btn8->ButtonClicked +=162             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example8, "DialogsWindow::example8" );163         add( btn8 );164 165         CommandButton* btn9 = new CommandButton();166         btn9->setBounds( 10, btn8->getBottom() + 20, 175, btn6->getPreferredHeight() );167         btn9->setCaption( "Show Common Print Dialog" );168         btn9->ButtonClicked +=169             new ClassProcedure1<Event*, DialogsWindow>( this, &DialogsWindow::example9, "DialogsWindow::example9" );170         add( btn9 );171     }172 173     virtual ~DialogsWindow(){};174 175     /**176     This example will deomonstrate how to display a modal, information dialog,177     to present some information to a user.178     All you have to pass in is the caption - the title of the dialog will be assigned179     the name of the application180     */181     void example1( Event* e ) {182         Dialog::showMessage( "Hello - this is example 1." );183     }184 185 186     String modalReturnToString( UIToolkit::ModalReturnType val ) {187         String result;188         switch ( val ) {189             case UIToolkit::mrOK : {190                 result = "UIToolkit::mrOK";191             }192             break;193 194             case UIToolkit::mrCancel : {195                 result = "UIToolkit::mrCancel";196             }197             break;198 199             case UIToolkit::mrYes : {200                 result = "UIToolkit::mrYes";201             }202             break;203 204             case UIToolkit::mrNo : {205                 result = "UIToolkit::mrNo";206             }207             break;208 209             case UIToolkit::mrAbort : {210                 result = "UIToolkit::mrAbort";211             }212             break;213 214             case UIToolkit::mrIgnore : {215                 result = "UIToolkit::mrIgnore";216             }217             break;218 219             case UIToolkit::mrRetry : {220                 result = "UIToolkit::mrRetry";221             }222             break;223         }224         return result;225     }226 227     /**228     This example also displays a modal dialog. You can specify more options229     such as the type of icon to display, the title of the dialog, and the230     kind of buttons the user can respond with.231     */232     void example2( Event* e ) {233         UIToolkit::ModalReturnType result;234 235         result = Dialog::showMessage( "Hello - this is an example using \nOK and Cancel buttons.",236                                         "Fluffaluffagus",237                                         Dialog::mbOKCancel,238                                         Dialog::msError );239 240         Dialog::showMessage( "You selected: " + modalReturnToString( result ) );241 242         result = Dialog::showMessage( "Hello - this is an example using \nYes and No buttons.",243                                         "Fluffaluffagus",244                                         Dialog::mbYesNo,245                                         Dialog::msWarning );246 247         Dialog::showMessage( "You selected: " + modalReturnToString( result ) );248 249         result = Dialog::showMessage( "Hello - this is an example using \nAbort, Retry and Ignore buttons.",250                                         "Format your Hard Disk?",251                                         Dialog::mbAbortRetryIgnore,252                                         Dialog::msInfo );253 254         Dialog::showMessage( "You selected: " + modalReturnToString( result ) );255     }256 257     /**258     This example shows how to launch a custom259     modal dialog260     */261     void example3( Event* e ) {262         MyDialog dialog;// = new MyDialog();263         UIToolkit::ModalReturnType result;264 265         result = dialog.showModal();266 267         Dialog::showMessage( "You selected: " + modalReturnToString( result ) );268 269         //dialog->free();270     }271 272 273     /**274     This example shows how to call up the common font dialog275     */276     void example4( Event* e ) {277         CommonFontDialog dlg(this);278         dlg.setSelectedFont( getFont() );279         if ( dlg.execute() ) {280             Font font = *dlg.getSelectedFont();281 282             Dialog::showMessage( "You selected the font named: " + font.getName() );283         }284     }285 286     /**287     This example shows how to call up the common Color dialog288     */289     void example5( Event* e ) {290         CommonColorDialog dlg(this);291         dlg.setSelectedColor( getColor() );292         if ( dlg.execute() ) {293             Color color = *dlg.getSelectedColor();294 295             String hexName = color.toString();296 297             Dialog::showMessage( "You chose color (in hex) : " + hexName );298         }299     }300 301     /**302     This example shows how to call up the common file browse dialog303     */304     void example6( Event* e ) {305         CommonFileBrowseDialog dlg(this);306 307         dlg.setDirectory( System::getCurrentWorkingDirectory() );308 309         if ( dlg.execute() ) {310             Dialog::showMessage( "You picked directory: " + dlg.getDirectory() );311         }312     }313 314     /**315     This example shows how to call up the common file open dialog316     */317     void example7( Event* e ) {318         CommonFileOpenDialog dlg(this);319 320         dlg.addFilter( "Pumpernikel Bread", "*.pmk" );321         if ( dlg.execute() ) {322 323         }324     }325 326     /**327     This example shows how to call up the common file save dialog328     */329     void example8( Event* e ) {330         CommonFileSaveDialog dlg(this);331 332         if ( dlg.execute() ) {333 334         }335     }336 337     /**338     This example shows how to call up the common print dialog339     */340     void example9( Event* e ) {341         CommonPrintDialog dlg(this);342 343         if ( dlg.execute() ) {344 345         }346     }347 };348 349 350 351 352 class DialogsApplication : public Application {353 public:354 355     DialogsApplication( int argc, char** argv ) : Application(argc, argv) {356 357     }358 359     virtual bool initRunningApplication(){360         bool result = Application::initRunningApplication();361 362         Window* mainWindow = new DialogsWindow();363         setMainWindow(mainWindow);364         mainWindow->setBounds( &Rect( 100.0, 100.0, 300.0, 540.0 ) );365         mainWindow->show();366 367         return result;368     }369 370 };371 372 373 int main(int argc, char *argv[])374 {375     Application* app = new DialogsApplication( argc, argv );376 377     Application::main();378 379     return 0;380 }

 

Dialog