//------------------------------------------------------ // MainWindow.cpp -- implementation of MainWindow class //------------------------------------------------------ #include using namespace std; #include #include #include #include #include #include #include "mainWindow.h" MainWindow* MainWindow::theApp = NULL; // ------------ Callbacks ------------ // void quit_cb( Fl_Widget*, void* ) { exit( 0 ); } void changeMode_cb( Fl_Widget*, int newmode ) { // Since changeMode_cb is a stand-alone function, // we have to use MainWindow::getTheApp() to get // a pointer to the running instance of MainWindow (MainWindow::getTheApp())->changeMode( newmode ); } // ------------ MainWindow definitions ------------ // MainWindow::MainWindow() { // Most FL_ constructors are ( x position, y position, width, height, ...) // Every Fl object created between here and win->end() is // placed in the window win = new Fl_Window( 200, 200, 720, 590, "JDraw v1.0" ); // Create the OpenGL drawing area glwidget = new GLWidget( 10, 40, 700, 500 ); // Create the menu bar Fl_Menu_Bar* menu = new Fl_Menu_Bar( 0, 0, 720, 30 ); menu->box( FL_PLASTIC_UP_BOX ); // add( "menu/item", shortcut, callback, data for the callback ) menu->add( "&File/&Quit", FL_CTRL + 'q', (Fl_Callback *)quit_cb ); menu->add( "&Draw/Draw &Line", FL_CTRL + 'l', (Fl_Callback *)changeMode_cb, (void*)GLWidget::DRAW_LINE ); menu->add( "&Draw/Draw &Freehand", FL_CTRL + 'f', (Fl_Callback *)changeMode_cb, (void*)GLWidget::DRAW_FREE ); menu->add( "&Draw/Draw &Texture", FL_CTRL + 't', (Fl_Callback *)changeMode_cb, (void*)GLWidget::DRAW_IMAGE ); menu->add( "&Draw/&Clear Screen", FL_CTRL + 'c', (Fl_Callback *)changeMode_cb, (void*)GLWidget::CLEAR_SCREEN ); // Create the label under the drawing area label = new Fl_Output( 10, 550, 700, 30 ); label->box( FL_PLASTIC_UP_BOX ); label->color( FL_BACKGROUND_COLOR ); label->cursor_color( FL_BACKGROUND_COLOR ); label->value( "Waiting for name..." ); // We're done adding items to the window win->end(); win->size_range(100,100); // Point our static MainWindow pointer to // this running instance theApp = this; } MainWindow::~MainWindow() { delete glwidget, label, win; } void MainWindow::show( int argc, char **argv ) { // Pass the show() command on to our window win->show( argc, argv ); } void MainWindow::getName() { char newlabel[128]; // fl_input brings up a dialog box with a text input and // "Ok" and "Cancel" buttons. Clicking "Ok" will return // the string in the edit box and clicking "Cancel" will // return null. const char* name = fl_input( "Please enter your name", "" ); if( !name || !strcmp( name, "" ) ) sprintf( newlabel, "Hi anonymous person! " ); else sprintf( newlabel, "Hi %s! ", name ); strcat( newlabel, "Please select an option from the Draw menu." ); label->value( newlabel ); } void MainWindow::changeMode( int newmode ) { cout << "in MainWindow::changeMode()" << endl << flush; glwidget->changeMode( (GLWidget::DrawMode)newmode ); char newlabel[128]; switch( newmode ) { case GLWidget::DRAW_LINE: sprintf( newlabel, "Pick any two points on the canvas to draw a line." ); break; case GLWidget::DRAW_FREE: sprintf( newlabel, "Click and drag the mouse to draw freehand." ); break; case GLWidget::DRAW_IMAGE: sprintf( newlabel, "Illlustrates use of GL Textures." ); break; case GLWidget::CLEAR_SCREEN: sprintf( newlabel, "Please select an option from the Draw menu." ); break; } label->value( newlabel ); }