| 1 | import gtk |
|---|
| 2 | import gobject |
|---|
| 3 | import random |
|---|
| 4 | |
|---|
| 5 | class test4A(gtk.Widget): |
|---|
| 6 | def __init__(self): |
|---|
| 7 | gtk.Widget.__init__(self) |
|---|
| 8 | |
|---|
| 9 | def do_realize(self): |
|---|
| 10 | self.set_flags(self.flags() | gtk.REALIZED) |
|---|
| 11 | |
|---|
| 12 | self.window = gtk.gdk.Window(self.get_parent_window(), |
|---|
| 13 | width=self.allocation.width, |
|---|
| 14 | height=self.allocation.height, |
|---|
| 15 | window_type=gtk.gdk.WINDOW_CHILD, |
|---|
| 16 | wclass=gtk.gdk.INPUT_OUTPUT, |
|---|
| 17 | event_mask=self.get_events() | |
|---|
| 18 | gtk.gdk.EXPOSURE_MASK) |
|---|
| 19 | |
|---|
| 20 | self.window.set_user_data(self) |
|---|
| 21 | self.style.attach(self.window) |
|---|
| 22 | self.style.set_background(self.window, gtk.STATE_NORMAL) |
|---|
| 23 | self.window.move_resize(*self.allocation) |
|---|
| 24 | |
|---|
| 25 | def do_unrealize(self): |
|---|
| 26 | self.window.set_user_data(None) |
|---|
| 27 | |
|---|
| 28 | def do_size_request(self, requisition): |
|---|
| 29 | requisition.width = 50 |
|---|
| 30 | requisition.height = 50 |
|---|
| 31 | |
|---|
| 32 | def do_size_allocate(self, allocation): |
|---|
| 33 | self.allocation = allocation |
|---|
| 34 | |
|---|
| 35 | if self.flags() & gtk.REALIZED: |
|---|
| 36 | self.window.move_resize(*allocation) |
|---|
| 37 | |
|---|
| 38 | def do_expose_event(self, event): |
|---|
| 39 | cr = self.window.cairo_create() |
|---|
| 40 | cr.rectangle(*event.area) |
|---|
| 41 | cr.clip() |
|---|
| 42 | |
|---|
| 43 | cr.rectangle(*event.area) |
|---|
| 44 | cr.set_source_rgb(random.random(), random.random(), random.random()) |
|---|
| 45 | cr.fill() |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | gobject.type_register(test4A) |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | class test4B(gtk.Widget): |
|---|
| 52 | def __init__(self): |
|---|
| 53 | gtk.Widget.__init__(self) |
|---|
| 54 | self.x = 0 |
|---|
| 55 | self.y = 0 |
|---|
| 56 | |
|---|
| 57 | def do_realize(self): |
|---|
| 58 | self.set_flags(self.flags() | gtk.REALIZED) |
|---|
| 59 | |
|---|
| 60 | self.window = gtk.gdk.Window(self.get_parent_window(), |
|---|
| 61 | width=self.allocation.width, |
|---|
| 62 | height=self.allocation.height, |
|---|
| 63 | window_type=gtk.gdk.WINDOW_CHILD, |
|---|
| 64 | wclass=gtk.gdk.INPUT_OUTPUT, |
|---|
| 65 | event_mask=self.get_events() | |
|---|
| 66 | gtk.gdk.EXPOSURE_MASK) |
|---|
| 67 | |
|---|
| 68 | self.window.set_user_data(self) |
|---|
| 69 | self.style.attach(self.window) |
|---|
| 70 | self.style.set_background(self.window, gtk.STATE_NORMAL) |
|---|
| 71 | self.window.move_resize(*self.allocation) |
|---|
| 72 | |
|---|
| 73 | def do_unrealize(self): |
|---|
| 74 | self.window.set_user_data(None) |
|---|
| 75 | |
|---|
| 76 | def do_size_request(self, requisition): |
|---|
| 77 | requisition.width = 250 |
|---|
| 78 | requisition.height = 250 |
|---|
| 79 | |
|---|
| 80 | def do_size_allocate(self, allocation): |
|---|
| 81 | self.allocation = allocation |
|---|
| 82 | |
|---|
| 83 | if self.flags() & gtk.REALIZED: |
|---|
| 84 | self.window.move_resize(*allocation) |
|---|
| 85 | |
|---|
| 86 | def do_expose_event(self, event): |
|---|
| 87 | cr = self.window.cairo_create() |
|---|
| 88 | cr.rectangle(*event.area) |
|---|
| 89 | cr.clip() |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | cr.rectangle(self.x, self.y, 50, 50) |
|---|
| 93 | cr.fill() |
|---|
| 94 | |
|---|
| 95 | gobject.timeout_add(25, self.timeout) |
|---|
| 96 | |
|---|
| 97 | def timeout(self): |
|---|
| 98 | if self.y + 50 > self.allocation[3]: |
|---|
| 99 | self.y = self.allocation[3] - 50 |
|---|
| 100 | self.x += 5 |
|---|
| 101 | if self.x + 50 > self.allocation[2]: |
|---|
| 102 | self.y -= 5 |
|---|
| 103 | self.x = self.allocation[2] - 49 |
|---|
| 104 | else: |
|---|
| 105 | self.y += 5 |
|---|
| 106 | |
|---|
| 107 | self.queue_draw() |
|---|
| 108 | return False |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | gobject.type_register(test4B) |
|---|