/**** * * This file is a simple main program to construct an IdrawInterface object and * perform some basic tests on it. The tests illustrate how to use the * services of IdrawInterface. * */ #include "quit-exception.h" #include "iv-support.h" #include #include #include #include #include #include /** * Class TestButton is used to activate the simple IdrawInterface testing code. * The test button comes up in a separate window labled "Press to Test". */ class TestButton : public PushButton { public: TestButton(IdrawInterface* ii); protected: virtual void Press(); IdrawInterface* ii; }; TestButton::TestButton(IdrawInterface* ii) : PushButton("Press to Test", new ButtonState(), true) { this->ii = ii; } void TestButton::Press() { Selection* s; // Selection is the type of object that is drawn // by all of the IdrawInterface operations. int l, b, r, t; // The standard coordinate measure for an object is a // four coordinates for the left (l), bottom (b), right // (r), and top (t) of the object. BoxObj bo; // The position of any selection can be determined by // calling Selection::GetBox, which takes a BoxObj as // a reference parameter. A BoxObj has four public // data members named left, bottom, top, and right. Event e; // An Event is an InterViews class that holds // information about an input event. See the // InterViews reference manual for a complete // description of the Event class. /* * Draw a small rectangle. Note that all drawing functions return the * Selection object that they draw on the canvas. */ s = ii->DrawRect(10,10,20,20); /* * Move the Selection that was just drawn 30 pixels up and 30 pixels to the * right. */ ii->Move(s,30,30); /* * Select the rectange selection. This places small balck "handles" on the * corner of the object, as when the user clicks on it in the normal idraw. * Note that selecting an object makes it "current", so that the * non-parameter versions of the object manipulation functions will apply * to it. For example, ii->Select(s) followed by ii->Rotate(), has the * same effect as ii->Rotate(s). */ ii->Select(s); /* * Rotate the current selection 45 degrees. */ ii->Rotate(45); /* * Change the foreground and background colors of the current selection. */ ii->FgColorSelect(3); ii->BgColorSelect(3); /* * Unselect the rectangle. */ ii->Unselect(s); /* * Test input tracking by calling TrackMove and TrackRectangle. In order * for TrackMove to do any work, the mouse must be clicked on the rectangle * drawn above, since it is the only shape in the drawing area. If the * mouse is clicked elsewhere, nothing will happen for TrackMove. Note * also that TrackMove does not actually move the tracked object, it only * allows the user to drag a rectangle that is the same size as the object * being tracked. To accomplish the actual movement, the * IdrawInterface::Move function must be called. * * As described in the indraw-interface.h file, all track functions wait * for a left mouse down, then proceed to track the user mouse movement * until the mouse button is releases, whereupon the tracking function * returns the l,b,r,t coordinates where the tracking finshed. The * coordinates are returned in the four function reference parameters. */ ii->TrackMove(l,b,r,t); printf("l,b,r,t from TrackRect = %d,%d,%d,%d\n", l, b, r, t); ii->TrackRectangle(l,b,r,t); printf("l,b,r,t from TrackRect = %d,%d,%d,%d\n", l, b, r, t); /* * A useful function for manipulating selections is provided by the * "drawing" member if IdrawInterface. The drawing member is inherited * from Idraw and is the lowest level abstraction that manages the drawing * of shapes. The member itself is protected, but is accessed by the * public GetDrawing function. The drawing provides a function named * PickSelectionIntersecting(Coord x, Coord y), which returns the frontmost * Selection, if any, that intersectst the given x,y coordinates. * PickSelectionIntersecting is useful when an event is received in the * drawing area to see if the event happened on some Selection. For * example, the following test code calls * IdrawInterface::WaitForLeftMouseDown, and then determines if the mouse * event occured anywhere on the rectangle selection that drawn earlier. */ ii->WaitForLeftMouseDown(e); if (ii->GetDrawing()->PickSelectionIntersecting(e.x, e.y) == s) { printf("The left mouse down click DID happen on the rectangle.\n"); } else { printf("The left mouse down click did NOT happen on the rectangle.\n"); } } /** * Class QuitButton is used to exit from the test program, by throwing a * QuitException. */ class QuitButton : public PushButton { public: QuitButton(); protected: virtual void Press(); }; QuitButton::QuitButton() : PushButton("Press to Quit", new ButtonState(), true) { } void QuitButton::Press() { throw (new QuitException); } /** * Class ButtonBox is a simple view that holds the test and quit buttons. */ class ButtonBox : public View { public: ButtonBox(Screen* s, IdrawInterface* ii); void Compose(); protected: TestButton* tb; QuitButton* qb; }; ButtonBox::ButtonBox(Screen* s, IdrawInterface* ii) : View(s) { tb = new TestButton(ii); qb = new QuitButton(); } void ButtonBox::Compose() { w = new VBox(tb,qb); } /** * Function main allocates, composes, and runs. Note that a Screen is an alias * for an InterViews World. Hence a Screen has the same constructors as a * World, including a constructor that takes properties and options to * initialize the GUI environment. In this case, we're initializing with the * idraw-interface properties defined in default-idraw-properties.h. */ int main (int argc, char** argv) { Screen* s = new Screen("Idraw Interface", properties, options, argc, argv); IdrawInterface* ii = new IdrawInterface(s, argc, argv); ButtonBox* bb = new ButtonBox(s, ii); bb->Compose(); bb->Show(); ii->Show(); try { ii->Run(); } catch (QuitException*) { delete bb; delete ii; delete s; } }