Debian Codegraph @

fltk/test/table.py

raw symbols SCIP
1 #
2 # "$Id: table.py 536 2020-10-30 15:20:32Z andreasheld $"
3 #
4 # Fl_Table test program for pyFLTK the Python bindings
5 # for the Fast Light Tool Kit (FLTK).
6 #
7 # FLTK copyright 1998-1999 by Bill Spitzak and others.
8 # Fl_Table copyright 2003 by G. Ercolano
9 # pyFLTK copyright 2003 by Andreas Held and others.
10 #
11 # This library is free software you can redistribute it and/or
12 # modify it under the terms of the GNU Library General Public
13 # License, version 2.0 as published by the Free Software Foundation.
14 #
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Library General Public License for more details.
19 #
20 # You should have received a copy of the GNU Library General Public
21 # License along with this library if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 # USA.
24 #
25 # Please report all bugs and problems to "[email protected]".
26 #
27
28 from fltk import *
29 import sys
30
31
32 def button_cb(w, data):
33 print(f"BUTTON: {w.label()}")
34
35 class WidgetTable(Fl_Table_Row):
36 def __init__(self, x, y, w, h, l=""):
37 Fl_Table_Row.__init__(self, x, y, w, h, l)
38 self.col_header(1)
39 self.col_resize(1)
40 self.col_header_height(25)
41 self.row_header(1)
42 self.row_resize(1)
43 self.row_header_width(80)
44 self.end()
45
46 def draw_cell(self, context, R, C, X, Y, W, H):
47 s=f"{R}/{C}" # text for each cell
48
49 if context==self.CONTEXT_STARTPAGE:
50 fl_font(FL_HELVETICA, 12) # font used by all headers
51 return None
52 elif context==self.CONTEXT_RC_RESIZE:
53 index = 0
54 for r in range(self.rows()):
55 for c in range(self.cols()):
56 if index >= self.children():
57 break
58 #(status, X, Y, W, H) = self.find_cell(self.CONTEXT_TABLE, r, c, X, Y, W, H)
59 (status, X, Y, W, H) = self.find_cell(self.CONTEXT_TABLE, r, c)
60 print("find_cell")
61 self.child(index).resize(X,Y,W,H)
62 index += 1
63 self.init_sizes()
64 return None
65 elif context==self.CONTEXT_ROW_HEADER:
66 s1=f"Row {R}"
67 fl_push_clip(X,Y,W,H)
68 fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, self.row_header_color())
69 fl_color(FL_BLACK)
70 fl_draw(s1, X, Y, W, H, FL_ALIGN_CENTER)
71 fl_pop_clip()
72 return None
73 elif context==self.CONTEXT_COL_HEADER:
74 s1=f"Column {C}"
75 fl_push_clip(X, Y, W, H)
76 fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, self.col_header_color())
77 fl_color(FL_BLACK)
78 fl_draw(s1, X, Y, W, H, FL_ALIGN_CENTER)
79 fl_pop_clip()
80 return None
81 elif context==self.CONTEXT_CELL:
82 return None
83 else:
84 return None
85
86 def SetSize(self, newrows, newcols):
87 self.rows(newrows)
88 self.cols(newcols)
89 self.begin()
90 self.s_list = []
91 for r in range(newrows):
92 for c in range(newcols):
93 X=0
94 Y=0
95 W=self.col_width(c)
96 H=self.row_height(r)
97 #c=0
98 #(status, X, Y, W, H)=self.find_cell(self.CONTEXT_TABLE, r, c, X, Y, W, H)
99
100 if c&1:
101 s=f"{r}.{c}"
102 inp = Fl_Input(X,Y,W,H)
103 inp.value(s)
104 else:
105 s1=f"{r}/{c}"
106 butt = Fl_Light_Button(X,Y,W,H,s1)
107 self.s_list.append(s1)
108 butt.align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE)
109 butt.callback(button_cb, 0)
110 if (r+2*c)&4:
111 butt.value(1)
112 else:
113 butt.value(0)
114 self.end()
115
116
117
118 class DemoTable(Fl_Table_Row):
119 def __init__(self, x, y, w, h, l=""):
120 Fl_Table_Row.__init__(self, x, y, w, h, l)
121
122 def draw_cell(self, context, R, C, X, Y, W, H):
123 s=f"{R}/{C}" # text for each cell
124
125 if context==self.CONTEXT_STARTPAGE:
126 fl_font(FL_HELVETICA, 16)
127 return None
128 elif context==self.CONTEXT_ROW_HEADER or context==self.CONTEXT_COL_HEADER:
129 fl_push_clip(X,Y,W,H)
130 fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, self.color())
131 fl_color(FL_BLACK)
132 fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER)
133 fl_pop_clip()
134 return None
135 elif context==self.CONTEXT_CELL:
136 fl_push_clip(X,Y,W,H)
137 # BG color
138 if self.row_selected(R):
139 fl_color(self.selection_color())
140 else:
141 fl_color(FL_WHITE)
142 fl_rectf(X, Y, W, H)
143
144 # TEXT
145 fl_color(FL_BLACK)
146 fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER)
147
148 # BORDER
149 fl_color(FL_LIGHT2);
150 fl_rect(X, Y, W, H);
151 fl_pop_clip()
152 return None
153 else:
154 return None
155
156
157
158 def table_cb(o, data):
159 table = data
160 print(f"{table.label()} callback: row={table.callback_row()} col={table.callback_col()}, context={table.callback_context()}, event={Fl.event()}, clicks={Fl.event_clicks()}")
161
162 if __name__=='__main__':
163 win1 = Fl_Window(940, 500, "widgettable")
164 table = WidgetTable(20, 20, win1.w()-40, win1.h()-40, "widgettable")
165 table.SetSize(20, 20)
166 win1.end()
167 win1.resizable(table)
168 win1.show()
169
170 w = 900
171 h = 700
172 t1x = 20
173 t1y = 20
174 t1w = w - 40
175 t1h = int( (h-60) / 2 )
176 t2x = 20
177 t2y = t1y+t1h+20
178 t2w = w - 40
179 t2h = t1h
180
181 win2 = Fl_Window(w, h, "testtablerow")
182 table1 = DemoTable(t1x, t1y, t1w, t1h, "Table 1")
183 table1.selection_color(FL_YELLOW)
184 table1.when(FL_WHEN_RELEASE) # handle table events on release
185 #table1.rows(1001)
186 table1.rows(51)
187 table1.cols(31)
188 table1.col_header(1) # enable col header
189 table1.col_resize(4) # enable col resizing
190 table1.row_header(1) # enable row header
191 table1.row_resize(4) # enable row resizing
192 table1.callback(table_cb, table1)
193 table1.when(FL_WHEN_CHANGED|FL_WHEN_RELEASE)
194 table1.end()
195
196 table2 = DemoTable(t2x, t2y, t2w, t2h, "Table 2")
197 table2.selection_color(FL_YELLOW)
198 table2.when(FL_WHEN_RELEASE) # handle table events on release
199 #table2.rows(1001)
200 table2.rows(51)
201 table2.cols(31)
202 table2.col_header(1) # enable col header
203 table2.col_resize(4) # enable col resizing
204 table2.row_header(1) # enable row header
205 table2.row_resize(4) # enable row resizing
206 table2.end()
207 win2.end()
208 win2.show()
209
210 Fl.run()