In article <C9qK...@discus.technion.ac.il>, c036...@csc.technion.ac.il (Sergio Nacht) writes:
|> Hi,
|> Do you know what the type "Page" is and how does it work??
Page is a class that allows to position a given Glyph on any position
of the canvas.
I've written a small example program that shows how Page works.
You can resize the window.
Please note that the Glyphs located on the Page are really located
on the canvas of the Page. For that reason there is a show(..)
function in the Page class.
I hope the following code will help you.
Andre'
/* dynPage.c
*
* This small InterViews program shows, how InputHandlers
* can be appended dynamically into a Page. In this example
* a QUIT-Button is the InputHandler to be appended.
*
* buttonPos_x_, buttonPos_y_ - the coordinates of the
* position where the current
* button is placed to.
* buttonWidth_, buttonHeight_ - width and height of the
* current button
*
*
* Criticisms and suggestions are most welcome.
*
* Andre' Mueller
* a...@nestler.de
*
*/
#include <IV-look/kit.h>
#include <InterViews/background.h>
#include <InterViews/color.h>
#include <InterViews/display.h>
#include <InterViews/layout.h>
#include <InterViews/session.h>
#include <InterViews/page.h>
#include <InterViews/patch.h>
#include <InterViews/window.h>
#define MY_EPS 0.00001;
class DynamicPage: public Patch {
public:
DynamicPage(Glyph* bg);
virtual ~DynamicPage();
virtual void allocate(Canvas*, const Allocation&, Extension&);
virtual void appendQUIT();
private:
Page* page_;
Coord buttonPos_x_;
Coord buttonPos_y_;
Coord buttonWidth_;
Coord buttonHeight_;
};
DynamicPage::DynamicPage (Glyph* bg): Patch(nil)
{
page_ = new Page(bg);
Resource::ref(page_);
body(page_);
buttonPos_x_ = 0;
buttonPos_y_ = 0;
buttonWidth_ = 0;
buttonHeight_ = 0;
}
DynamicPage::~DynamicPage() {
Resource::unref(page_);
}
void DynamicPage::allocate(Canvas* c, const Allocation& a, Extension& e) {
Patch::allocate(c,a,e);
Coord x, y;
boolean show_i = false; // show only if button on the page area
for(GlyphIndex i = page_->count() - 1; i >= 0; i--) {
page_->location(i, x, y);
show_i = x + buttonWidth_ <= a.right() - a.left() && y > 0 &&
y + buttonHeight_ <= a.top() - a.bottom() + MY_EPS;
page_->show(i, show_i);
}
}
void DynamicPage::appendQUIT()
{
WidgetKit* kit = WidgetKit::instance();
Button* g = kit->push_button( "QUIT" , kit->quit());
page_->append(g);
reallocate(); // to allocate button area
const Allocation& b = g->allocation();
buttonHeight_ = b.top() - b.bottom();
buttonWidth_ = b.right() - b.left();
Coord distance = 5; // for a better look
const Allocation& a = allocation();
Coord pageHeight = a.top() - a.bottom();
if(pageHeight < buttonHeight_) { // button does not fit into page
canvas()->window()->display()->ring_bell(10);
page_->remove(page_->count() - 1);
return;
}
if(!buttonPos_x_) { // these are the initial settings
buttonPos_x_ = distance;
buttonPos_y_ = pageHeight - buttonHeight_;
}
page_->move(page_->count() - 1, buttonPos_x_, buttonPos_y_ );
buttonPos_y_ -= buttonHeight_ + distance;
if(buttonPos_y_ < 0) { // open next column
buttonPos_x_ += buttonWidth_ + distance;
buttonPos_y_ = pageHeight - buttonHeight_;
}
if(buttonPos_x_ + buttonWidth_ > a.right() - a.left()) // right border
canvas()->window()->display()->ring_bell(10);
reallocate();
}
declareActionCallback (DynamicPage)
implementActionCallback (DynamicPage)
int main(int argc, char** argv)
{
Session* session = new Session("DynPage", argc, argv);
WidgetKit& kit = *WidgetKit::instance();
const LayoutKit& layout = *LayoutKit::instance();
Glyph* vglue = layout.vglue(10);
Glyph* hglue = layout.hglue(10);
Glyph* vspace = layout.vspace(15);
Requisition app_req;
Requirement a_rx (350,fil,fil,0);
Requirement a_ry (150,fil,140,0);
app_req.require(Dimension_X,a_rx);
app_req.require(Dimension_Y,a_ry);
DynamicPage* changingPage = new DynamicPage(
new Background(
LayoutKit::instance()->glue(app_req),
new Color(1,0,0,1)
)
);
session->run_window(
new ApplicationWindow(
new Background(
layout.vbox(
vspace,
layout.hbox(
hglue,
kit.push_button(
"Append Button",
new ActionCallback (DynamicPage) (
changingPage,
&DynamicPage::appendQUIT
)
),
hglue,
kit.push_button(
"Quit", kit.quit ()
),
hglue
),
vglue,
layout.hbox(
hglue,
kit.inset_frame(changingPage),
hglue
),
vglue
),
kit.background()
)
)
);
}