/* * Simple illustration of the use of a Viewport. */ #include #include #include #include "funcbutton.h" #include #include #include #include #include #include #include void DoButton(void* v, Event *e); main () { World* w = new World; Tray* t = new Tray(); VBox* vb = new VBox; VBox* vb1 = new VBox; HBox* hb = new HBox; HBox* hb1; Viewport* vp; Shape* sh1, *sh2; char *bname1, *bname2;; int i; /* * Make a vbox of 35 rows of hboxes, with each hbox containing a pair of * function buttons. */ for (i = 1; i <= 70; i+=2) { bname1 = new char[15]; bname2 = new char[15]; sprintf(bname1, "Button No. %d", i); sprintf(bname2, "Button No. %d", i+1); hb1 = new HBox( new FuncButton(DoButton, bname1, 0), new FuncButton(DoButton, bname2, 0)); vb1->Insert(hb1); } /* * Stick the vbox in a viewport, slap a scroller on the viewport, an stick * the pair in a new outer hbox. Throw in a couple up and down movers * above and below the scroller. Note that the up and down movers will let * us move in the viewport beyond the normal range that the scroller will * let us move. This may or may not what is desired in all cases. */ hb->Insert(vp = new Viewport(vb1)); hb->Insert(new VBorder); hb->Insert( new VBox( new UpMover(vp,1), new HBorder, new VScroller(vp), new HBorder, new DownMover(vp,1) ) ); /* * Stick some other stuff around the outer hbox, to confirm that things get * reconfig'd nicely. */ vb->Insert(new Message("Message 1")); vb->Insert(new Message("Message 2")); vb->Insert(new Message("Message 3")); vb->Insert(new HBorder); vb->Insert(hb); vb->Insert(new HBorder); vb->Insert(new Message("Message 4")); vb->Insert(new Message("Message 5")); vb->Insert(new Message("Message 6")); vb->Align(Center); /* * Allocate the outermost tray with the vbox inside. Then call Config, in * preparation for the hbox reshaping to follow. */ t = new Tray(vb); t->Config(w); /* * Get the shape of the hbox and halve the height, so that only half of it * will appear initially in the viewport. Note here that this happens * beofre InsertApplication, in contrast to ./main.C, q.v. Here the trick * is to call Tray::Config, which will force the tray to compute its * layout, including the computation of the height of hb. This happens * since Config descends through all children, and hb is a grandchild of t. * * Note that the call to t->Config(w) cannot happen after hb-GetShape(), * since prior to t->Config, hb->shape has 0 for the height and width, and * hence sh1->height /= 2 won't do what we want. This took a bit of head * scratching to figure out at first, but it makes OK sense. * * Note finally that Interactor::Config only works correctly when called * with the world as its arg. The comment in interactor.C explains this, * even though the explanation seems a little lame. Whatever. */ sh1 = hb->GetShape(); sh1->height /= 2; hb->Reshape(*sh1); /* * Insert the outermost tray into the world. */ w->InsertApplication(t); w->Run(); delete w; } void DoButton(void* v, Event *e) { printf("In button.\n"); }