19 |
# Copyright 2001-2003 Free Software Foundation |
# Copyright 2001-2003 Free Software Foundation |
20 |
# |
# |
21 |
# FILE: |
# FILE: |
22 |
# curses/Renderer.py |
# curses/Driver.py |
23 |
# |
# |
24 |
# DESCRIPTION: |
# DESCRIPTION: |
25 |
# |
# |
26 |
# NOTES: |
# NOTES: |
27 |
# |
# |
28 |
|
|
29 |
|
from wxPython.wx import * |
30 |
from gnue.designer.forms.LayoutEditor.renderers.Base import * |
from gnue.designer.forms.LayoutEditor.renderers.Base import * |
31 |
|
import string |
32 |
|
|
33 |
class Renderer(BaseRenderer): |
class Renderer(BaseRenderer): |
34 |
|
|
35 |
|
########## |
36 |
|
# |
37 |
|
# Public methods |
38 |
|
# |
39 |
|
|
40 |
def init(self): |
def init(self): |
41 |
pass |
self.map = { |
42 |
|
'GFScrollbar': self.scrollbar, |
43 |
|
'GFEntry': self.entry, |
44 |
|
'GFButton': self.button, |
45 |
|
'GFImage': self.image, |
46 |
|
'GFLabel': self.label, |
47 |
|
'GFBox': self.box, |
48 |
|
|
49 |
|
self.resetFont() |
50 |
|
|
51 |
|
def removeWidget(self, object): |
52 |
|
# We don't need to do anything special |
53 |
|
# if a widget is removed |
54 |
|
return |
55 |
|
|
56 |
|
def redrawWidget(self, object): |
57 |
|
# We don't need to do anything special, |
58 |
|
# other than recreate the bitmap, if a |
59 |
|
# widget is redrawn/updated |
60 |
|
return self.addWidget(object) |
61 |
|
|
62 |
|
def addWidget(self, object): |
63 |
|
try: |
64 |
|
func = self.map[object._type] |
65 |
|
except: |
66 |
|
func = self.unknown |
67 |
|
|
68 |
|
return func(object) |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
########## |
73 |
|
# |
74 |
|
# Internal methods |
75 |
|
# |
76 |
|
|
77 |
|
|
78 |
|
# |
79 |
|
# Known objects |
80 |
|
# Return a wxBitmap |
81 |
|
# |
82 |
|
def entry(self, object): |
83 |
|
return self.simpleTextObject(object, 10, 1, COLOR_TEXTBOX, "", 0) |
84 |
|
|
85 |
|
def button(self, object): |
86 |
|
# Default the width to the length of |
87 |
|
# the label if width hasn't been set. |
88 |
|
try: width = object.Char__width |
89 |
|
except: width = len(object.label) |
90 |
|
|
91 |
|
# Create a label formatted as <Button Name> |
92 |
|
# and pad it to take up the width |
93 |
|
label = string.center('<' + object.label[:width-2] + '>', width) |
94 |
|
|
95 |
|
# Use our handy-dandy bitmap creator to do the rest |
96 |
|
return self.simpleTextObject(object, width, 1, COLOR_BUTTON, text, 0) |
97 |
|
|
98 |
|
def label(self, object): |
99 |
|
|
100 |
|
# Default the width to the length of |
101 |
|
# the label if width hasn't been set. |
102 |
|
try: width = object.Char__width |
103 |
|
except: width = len(object.label) |
104 |
|
|
105 |
|
# Get the label alignment, if set |
106 |
|
try: alignment = object.alignment |
107 |
|
except: alignment = 'left' |
108 |
|
|
109 |
|
# Align the text |
110 |
|
text = aligners[alignment](object.label[:width],width) |
111 |
|
|
112 |
|
# And use our handy-dandy bitmap creator to do the rest |
113 |
|
return self.simpleTextObject(object, width, 1, COLOR_LABEL, object.label, 1) |
114 |
|
|
115 |
|
def scrollbar(self, object): |
116 |
|
return self.unknown(object) |
117 |
|
|
118 |
|
def box(self, object): |
119 |
|
return self.unknown(object) |
120 |
|
|
121 |
|
def image(self, object): |
122 |
|
# We don't support this, |
123 |
|
# so create an "unknown" object |
124 |
|
return self.unknown(object) |
125 |
|
|
126 |
|
|
127 |
|
# |
128 |
|
# Unknown/unsupported object |
129 |
|
# Return a wxBitmap with |
130 |
|
# cross-hatching |
131 |
|
# |
132 |
|
def unknown(self, object): |
133 |
|
# TODO: something better |
134 |
|
return self.simpleTextObject(object, 10, 1, COLOR_BACKGROUND, "", 0) |
135 |
|
|
136 |
|
|
137 |
|
# |
138 |
|
# Encapsulate the creation of widgets that are nothing more than |
139 |
|
# a one-line display of text (w/appropriate colors) |
140 |
|
# |
141 |
|
def simpleTextObject(object, text, defw, defh, colors, transparent=0): |
142 |
|
dc, bitmap = self.createBitmap(object, defw, defh) |
143 |
|
self.writeText(dc, 0, 0, colors, text, transparent) |
144 |
|
|
145 |
|
|
146 |
|
# |
147 |
|
# Create a bitmap object |
148 |
|
# using the char-based width/height |
149 |
|
# |
150 |
|
def createBitmap(self, object, defw=10, defh=1): |
151 |
|
# Get the bitmap width and height |
152 |
|
try: |
153 |
|
h = object.Char__height * self.heightAdjustment |
154 |
|
except: |
155 |
|
h = defw |
156 |
|
|
157 |
|
try: |
158 |
|
w = object.Char__width * self.heightAdjustment |
159 |
|
except: |
160 |
|
w = defw |
161 |
|
|
162 |
|
dc = wxMemoryDC() |
163 |
|
bitmap = wxEmptyBitmap(w, h) |
164 |
|
dc.SelectObject(bitmap) |
165 |
|
dc.SetFont(self.font) |
166 |
|
return (dc, bitmap) |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
# |
171 |
|
# Draw text onto the bitmap |
172 |
|
# Supports background coloring |
173 |
|
# and multiline-text |
174 |
|
# |
175 |
|
def writeText(self, dc, x, y, colors, text, transparent=0): |
176 |
|
|
177 |
|
fgcolor, bgcolor = colors |
178 |
|
if not transparent: |
179 |
|
dc.SetBackground(wxBrush(bgcolor)) |
180 |
|
dc.Clear() |
181 |
|
else: |
182 |
|
dc.SetBackground(wxTRANSPARENT_BRUSH) |
183 |
|
|
184 |
|
dc.SetTextForeground(fgcolor) |
185 |
|
|
186 |
|
ox = x * self.cell_width + self.cell_horiz_margin |
187 |
|
oy = x * self.cell_height + self.cell_vert_margin |
188 |
|
|
189 |
|
|
190 |
|
for line in text.split('\n'): |
191 |
|
dc.DrawText(line, ox, oy) |
192 |
|
oy += self.cell_height |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
WHITE = wxWHITE |
197 |
|
BLUE = wxBLUE |
198 |
|
CYAN = wxCYAN |
199 |
|
BLACK = wxBLACK |
200 |
|
RED = wxRED |
201 |
|
|
202 |
|
|
203 |
|
# These names were copied over |
204 |
|
# from gnue.common.cursing; |
205 |
|
# not all are used here. |
206 |
|
COLOR_BACKGROUND=(WHITE,BLUE) |
207 |
|
COLOR_TITLEBAR=(WHITE,BLUE) |
208 |
|
COLOR_TEXTBOX_FOCUS=(BLACK,CYAN) |
209 |
|
COLOR_TEXTBOX=(BLUE,WHITE) |
210 |
|
COLOR_MENUBAR=(BLACK,WHITE) |
211 |
|
COLOR_MENUBAR_FOCUS=(BLUE,WHITE) |
212 |
|
COLOR_LABEL=(BLACK,WHITE) |
213 |
|
COLOR_STATUSBAR=(BLACK,WHITE) |
214 |
|
COLOR_STATUSBAR_FIELD=(BLACK,WHITE) |
215 |
|
COLOR_DIALOG=(BLACK,WHITE) |
216 |
|
COLOR_DIALOG_TITLE=(BLACK,WHITE) |
217 |
|
COLOR_BUTTON=(BLUE,WHITE) |
218 |
|
COLOR_BUTTON_FOCUS=(WHITE,CYAN) |
219 |
|
COLOR_SCROLL_BUTTON=(BLACK,WHITE) |
220 |
|
COLOR_SCROLL_BUTTON_FOCUS=(CYAN,WHITE) |
221 |
|
COLOR_SCROLL_BACKGROUND=(BLUE,WHITE) |
222 |
|
COLOR_SCROLL_SLIDER=(WHITE,BLUE) |
223 |
|
COLOR_SCROLL_SLIDER_FOCUS=(BLACK,CYAN) |
224 |
|
COLOR_DROPSHADOW=(WHITE,BLACK) |
225 |
|
|
226 |
|
# Convenience mapper for <label>.alignment => python method |
227 |
|
aligners = {'left':string.ljust, |
228 |
|
'right':string.rjust, |
229 |
|
'center':string.center} |
230 |
|
|