summaryrefslogtreecommitdiff
path: root/xc/unsupported/examples/Fresco/figviewer.cxx
blob: c437de349e3ca9197da342836bb9cdda5bffcbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <X11/Fresco/figures.h>
#include <X11/Fresco/fresco.h>
#include <X11/Fresco/layouts.h>
#include <X11/Fresco/widgets.h>
#include <X11/Fresco/Impls/viewers.h>
#include <X11/Fresco/Impls/transform.h>
#include <X11/Fresco/Impls/region.h>
#include <stdio.h>
#include <stdlib.h>

class PageFigure : public GlyphImpl { 
public:
    PageFigure(Coord width, Coord height);
    virtual ~PageFigure();

    void request(Glyph::Requisition& r);
    void extension(const Glyph::AllocationInfo& a, RegionRef r);
    void draw(GlyphTraversalRef t);
private:
    Coord w_, h_;
};

PageFigure::PageFigure(Coord w, Coord h) {
    w_ = w;
    h_ = h;
}

PageFigure::~PageFigure () {}

void PageFigure::draw(GlyphTraversalRef) {}

void PageFigure::request(Glyph::Requisition& r) {
    r.x.natural = r.x.maximum = r.x.minimum = w_;
    r.y.natural = r.y.maximum = r.y.minimum = h_;
    r.x.align = r.y.align = 0.5;
    r.z.natural = 0;
}

void PageFigure::extension(const Glyph::AllocationInfo& a, RegionRef r) {
    RegionImpl region;
    region.lower_.x = 0;
    region.lower_.y = 0;
    region.lower_.z = 0;
    region.upper_.x = w_;
    region.upper_.y = h_;
    region.upper_.z = 0;
    region.xalign_ = region.yalign_ = 0.5;
    region.zalign_ = 0;
    if (is_not_nil(a.transform)) {
	region.transform(a.transform);
    }
    r->copy(&region);
}

class FigureViewer : public ViewerImpl {
public:
    FigureViewer(Fresco* f);
    ~FigureViewer();

    void need_resize();
    Boolean press(GlyphTraversalRef, EventRef);
    Boolean drag(GlyphTraversalRef, EventRef);
    Boolean release(GlyphTraversalRef, EventRef);
    void add(Coord x, Coord y);
protected:
    FigureKit figures_;
    FigureStyle def_style_;
    Glyph root_, figroot_;
    Coord width_, height_;
    enum { move_tool, create_tool } curtool_;
    GlyphRef target_;
    Coord start_x_, start_y_;
};

FigureViewer::FigureViewer(Fresco* f) : ViewerImpl(f) {
    width_ = 300; height_ = 300;
    figures_ = f->figure_kit();
    def_style_ = figures_->default_style();
    LayoutKit layouts = f->layout_kit();

    figroot_ = figures_->group();
    Glyph g = new TransformAllocator(0.5, 0.5, 0, 0.5, 0.5, 0);
    g->body(figroot_);
    root_ = layouts->fixed(g, width_, height_);
    body(root_);
    figroot_->append(new PageFigure(width_ * 10, height_ * 10));
}

FigureViewer::~FigureViewer() { }

void FigureViewer::need_resize() { }

void FigureViewer::add(Coord x, Coord y) {
    static int toggle = 0;
    Glyph fig;
    if (++toggle%2) {
	const float r = 36;
	fig = figures_->circle(FigureKit::fill, def_style_, x, y, r);
    } else {
	const Coord hh=25, hw=25;  // half-height,width
	fig = figures_->rectangle(
	    FigureKit::stroke, def_style_, x-hw,y-hh, x+hw, y+hh
	);
    }
    Glyph::AllocationInfoList al;
    figroot_->allocations(al);
    if (al._length < 1) {
	fprintf(stderr, "%d allocations!\n", al._length);
	exit(1);
    }
    Glyph::AllocationInfo& ai = al._buffer[0];
    TransformObj figtr = fig->transform();
    figtr->postmultiply(ai.transform);
    figtr->invert();
    figroot_->append(fig);
    fig->need_redraw();
    fig->need_resize();
    Fresco::unref(ai.allocation);
}
    
Boolean FigureViewer::press(GlyphTraversalRef t, EventRef e) {
    const Coord slop = 4;
    start_x_ = e->pointer_x();
    start_y_ = e->pointer_y();
    GlyphTraversal::Operation orig_op;
    orig_op = t->swap_op(GlyphTraversal::pick_top);
    t->begin_trail(this);
    PainterObj p = t->painter();
    p->push_clipping();
    p->clip_rect(start_x_-slop, start_y_-slop, start_x_+slop, start_y_+slop);
    root_->traverse(t);  
    
    GlyphTraversal picked = t->picked();
    if (is_not_nil(picked)) {
	target_ = picked->_c_current_glyph();
	curtool_ = move_tool;
    } else {
	curtool_ = create_tool;
    }
    p->pop_clipping();
    t->end_trail();
    return true;
}

Boolean FigureViewer::drag(GlyphTraversalRef, EventRef e) {
    if (curtool_ == move_tool) {
	TransformObj tx = target_->transform();
	if (is_not_nil(tx)) {
	    Vertex v;
	    v.x = e->pointer_x() - start_x_;
	    v.y = e->pointer_y() - start_y_;
	    start_x_ = e->pointer_x();
	    start_y_ = e->pointer_y();
	    target_->need_redraw();
	    tx->translate(v);
	    target_->need_redraw();
	    target_->need_resize();
	}
    }
    return true;
}

Boolean FigureViewer::release(GlyphTraversalRef, EventRef) {
    if (curtool_ == create_tool) {
	add(start_x_, start_y_);
    } else {
	Fresco::unref(target_);
    }
    return true;
}

int main(int argc, char** argv) {
     Fresco* f = Fresco_open("Test", argc, argv);
     LayoutKit layouts = f->layout_kit();
     Viewer fv = new FigureViewer(f);
     f->main(fv, layouts->margin(fv, 10.0));
     Fresco::unref(f);
     return 0;
}