| 20 | | def undo_handler(event): |
|---|
| 21 | | global undo_list |
|---|
| 22 | | undo_list.append(event) |
|---|
| 23 | | |
|---|
| 24 | | |
|---|
| 25 | | class MyBox(Box): |
|---|
| 26 | | """Box with an example connection protocol. |
|---|
| 27 | | """ |
|---|
| 28 | | |
|---|
| 29 | | class MyLine(Line): |
|---|
| 30 | | """Line with experimental connection protocol. |
|---|
| 31 | | """ |
|---|
| 32 | | |
|---|
| 33 | | def draw_head(self, context): |
|---|
| 34 | | cr = context.cairo |
|---|
| 35 | | cr.move_to(0, 0) |
|---|
| 36 | | cr.line_to(10, 10) |
|---|
| 37 | | cr.stroke() |
|---|
| 38 | | # Start point for the line to the next handle |
|---|
| 39 | | cr.move_to(0, 0) |
|---|
| 40 | | |
|---|
| 41 | | def draw_tail(self, context): |
|---|
| 42 | | cr = context.cairo |
|---|
| 43 | | cr.line_to(0, 0) |
|---|
| 44 | | cr.line_to(10, 10) |
|---|
| 45 | | cr.stroke() |
|---|
| 46 | | |
|---|
| 47 | | |
|---|
| 48 | | |
|---|
| 49 | | |
|---|
| 50 | | class MyText(Text): |
|---|
| 51 | | """ |
|---|
| 52 | | Text with experimental connection protocol. |
|---|
| 53 | | """ |
|---|
| 54 | | |
|---|
| 55 | | def draw(self, context): |
|---|
| 56 | | Text.draw(self, context) |
|---|
| 57 | | cr = context.cairo |
|---|
| 58 | | w, h = text_extents(cr, self.text, multiline=self.multiline) |
|---|
| 59 | | cr.rectangle(0, 0, w, h) |
|---|
| 60 | | cr.set_source_rgba(.3, .3, 1., .6) |
|---|
| 61 | | cr.stroke() |
|---|
| 62 | | |
|---|
| 63 | | |
|---|
| 64 | | def create_window(canvas, title, zoom=1.0): |
|---|
| 65 | | view = GtkView() |
|---|
| 66 | | view.tool = DefaultExampleTool() |
|---|
| 67 | | |
|---|
| 68 | | w = gtk.Window() |
|---|
| 69 | | w.set_title(title) |
|---|
| 70 | | h = gtk.HBox() |
|---|
| 71 | | w.add(h) |
|---|
| 72 | | |
|---|
| 73 | | # VBox contains buttons that can be used to manipulate the canvas: |
|---|
| 74 | | v = gtk.VBox() |
|---|
| 75 | | v.set_property('border-width', 3) |
|---|
| 76 | | v.set_property('spacing', 2) |
|---|
| 77 | | f = gtk.Frame() |
|---|
| 78 | | f.set_property('border-width', 1) |
|---|
| 79 | | f.add(v) |
|---|
| 80 | | h.pack_start(f, expand=False) |
|---|
| 81 | | |
|---|
| 82 | | b = gtk.Button('Move by (100, 50)') |
|---|
| 83 | | |
|---|
| 84 | | def on_clicked(button): |
|---|
| 85 | | global movable_item |
|---|
| 86 | | item = movable_item |
|---|
| 87 | | import time |
|---|
| 88 | | t1 = time.time() |
|---|
| 89 | | for i in range(20): |
|---|
| 90 | | item.matrix.translate(1, 1) |
|---|
| 91 | | item.request_update() |
|---|
| 92 | | # visualize each event: |
|---|
| 93 | | while gtk.events_pending(): |
|---|
| 94 | | gtk.main_iteration() |
|---|
| 95 | | t2 = time.time() |
|---|
| 96 | | print 'move time: %0.2f' % (t2 - t1,) |
|---|
| 97 | | |
|---|
| 98 | | b.connect('clicked', on_clicked) |
|---|
| 99 | | v.add(b) |
|---|
| 100 | | |
|---|
| 101 | | |
|---|
| 102 | | # b = gtk.Button('Cursor') |
|---|
| 103 | | # |
|---|
| 104 | | # def on_clicked(button, li): |
|---|
| 105 | | # c = li[0] |
|---|
| 106 | | # li[0] = (c+2) % 154 |
|---|
| 107 | | # button.set_label('Cursor %d' % c) |
|---|
| 108 | | # button.window.set_cursor(gtk.gdk.Cursor(c)) |
|---|
| 109 | | # |
|---|
| 110 | | # b.connect('clicked', on_clicked, [0]) |
|---|
| 111 | | # v.add(b) |
|---|
| 112 | | |
|---|
| 113 | | # Add the actual View: |
|---|
| 114 | | |
|---|
| 115 | | t = gtk.Table(2,2) |
|---|
| 116 | | h.add(t) |
|---|
| 117 | | |
|---|
| 118 | | w.connect('destroy', gtk.main_quit) |
|---|
| 119 | | |
|---|
| 120 | | view.canvas = canvas |
|---|
| 121 | | view.zoom(zoom) |
|---|
| 122 | | view.set_size_request(150, 120) |
|---|
| 123 | | hs = gtk.HScrollbar(view.hadjustment) |
|---|
| 124 | | vs = gtk.VScrollbar(view.vadjustment) |
|---|
| 125 | | t.attach(view, 0, 1, 0, 1) |
|---|
| 126 | | t.attach(hs, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL) |
|---|
| 127 | | t.attach(vs, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL) |
|---|
| 128 | | |
|---|
| 129 | | w.show_all() |
|---|
| 130 | | |
|---|
| 131 | | def handle_changed(view, item, what): |
|---|
| 132 | | print what, 'changed: ', item |
|---|
| 133 | | |
|---|
| 134 | | view.connect('focus-changed', handle_changed, 'focus') |
|---|
| 135 | | view.connect('hover-changed', handle_changed, 'hover') |
|---|
| 136 | | view.connect('selection-changed', handle_changed, 'selection') |
|---|
| 137 | | return view |
|---|
| 138 | | |
|---|
| 139 | | def main(): |
|---|
| 140 | | global movable_item |
|---|
| 141 | | if len(sys.argv) == 2: |
|---|
| 142 | | count = int(sys.argv[1]) |
|---|
| 143 | | else: |
|---|
| 144 | | count = 1 |
|---|
| 145 | | c=Canvas() |
|---|
| 146 | | |
|---|
| 147 | | view = create_window(c, 'View created before') |
|---|
| 148 | | |
|---|
| 149 | | # Add stuff to the canvas: |
|---|
| 150 | | |
|---|
| 151 | | movable_item=b=MyBox() |
|---|
| 152 | | b.min_width = 20 |
|---|
| 153 | | b.min_height = 30 |
|---|
| 154 | | print 'box', b |
|---|
| 155 | | b.matrix=(1.0, 0.0, 0.0, 1, 20,20) |
|---|
| 156 | | b.width=b.height = 40 |
|---|
| 157 | | c.add(b) |
|---|
| 158 | | |
|---|
| 159 | | bb=Box() |
|---|
| 160 | | print 'box', bb |
|---|
| 161 | | bb.matrix=(1.0, 0.0, 0.0, 1, 10,10) |
|---|
| 162 | | c.add(bb, parent=b) |
|---|
| 163 | | #v.selected_items = bb |
|---|
| 164 | | |
|---|
| 165 | | # AJM: extra boxes: |
|---|
| 166 | | bb=Box() |
|---|
| 167 | | print 'box', bb |
|---|
| 168 | | bb.matrix.rotate(math.pi/4.) |
|---|
| 169 | | c.add(bb, parent=b) |
|---|
| 170 | | n = math.floor(count ** 0.5) |
|---|
| 171 | | for i in xrange(count): |
|---|
| 172 | | bb=Box() |
|---|
| 173 | | print 'box', bb |
|---|
| 174 | | x = int(i % n) * 40 |
|---|
| 175 | | y = int(i / n) * 40 |
|---|
| 176 | | bb.matrix.translate(20 + x, y) |
|---|
| 177 | | bb.matrix.rotate(math.pi/4.0 * i / 10.0) |
|---|
| 178 | | bb.width = bb.height = 20 |
|---|
| 179 | | c.add(bb, parent=b) |
|---|
| 180 | | bb1 = Box() |
|---|
| 181 | | bb1.matrix.translate(5, 5) |
|---|
| 182 | | c.add(bb1, parent=bb) |
|---|
| 183 | | |
|---|
| 184 | | tool = view.tool._tools[1] |
|---|
| 185 | | for i in range(40): |
|---|
| 186 | | bb = MyBox() |
|---|
| 187 | | bb.width = bb.height = 20 |
|---|
| 188 | | x = int(i % 4) |
|---|
| 189 | | y = int(i / 4) |
|---|
| 190 | | bb.matrix.translate(20 + x * 30, 100 + y * 30) |
|---|
| 191 | | c.add(bb) |
|---|
| 192 | | |
|---|
| 193 | | if x > 0: |
|---|
| 194 | | l = Line() |
|---|
| 195 | | l.fyzzyness = 1 |
|---|
| 196 | | h1, h2 = l.handles() |
|---|
| 197 | | h2.pos = (10, 0) |
|---|
| 198 | | l.matrix.translate(10 + x * 30, 110 + y * 30) |
|---|
| 199 | | c.add(l) |
|---|
| 200 | | tool.connect(view, l, h1, 10 + x * 30, 110 + y * 30) |
|---|
| 201 | | tool.connect(view, l, h2, 20 + x * 30, 110 + y * 30) |
|---|
| 202 | | if y > 0: |
|---|
| 203 | | l = Line() |
|---|
| 204 | | l.fyzzyness = 1 |
|---|
| 205 | | h1, h2 = l.handles() |
|---|
| 206 | | h2.pos = (0, -10) |
|---|
| 207 | | l.matrix.translate(x * 30 + 30, 100 + y * 30) |
|---|
| 208 | | c.add(l) |
|---|
| 209 | | tool.connect(view, l, h1, x * 30 + 30, 100 + y * 30) |
|---|
| 210 | | tool.connect(view, l, h2, x * 30 + 30, 90 + y * 30) |
|---|
| 211 | | |
|---|
| 212 | | t=MyText('Single line') |
|---|
| 213 | | t.matrix.translate(70,70) |
|---|
| 214 | | c.add(t) |
|---|
| 215 | | |
|---|
| 216 | | l=MyLine() |
|---|
| 217 | | l.fyzzyness = 1 |
|---|
| 218 | | l.handles()[1].pos = (30, 30) |
|---|
| 219 | | l.split_segment(0, 3) |
|---|
| 220 | | l.matrix.translate(30, 60) |
|---|
| 221 | | c.add(l) |
|---|
| 222 | | l.orthogonal = True |
|---|
| 223 | | |
|---|
| 224 | | off_y = 0 |
|---|
| 225 | | for align_x in (-1, 0, 1): |
|---|
| 226 | | for align_y in (-1, 0, 1): |
|---|
| 227 | | t=MyText('Aligned text %d/%d' % (align_x, align_y), |
|---|
| 228 | | align_x=align_x, align_y=align_y) |
|---|
| 229 | | t.matrix.translate(120, 200 + off_y) |
|---|
| 230 | | off_y += 30 |
|---|
| 231 | | c.add(t) |
|---|
| 232 | | |
|---|
| 233 | | t=MyText('Multiple\nlines', multiline = True) |
|---|
| 234 | | t.matrix.translate(70,100) |
|---|
| 235 | | c.add(t) |
|---|
| 236 | | |
|---|
| 237 | | gtk.main() |
|---|