-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_flexTable.ahk
269 lines (229 loc) · 9.57 KB
/
class_flexTable.ahk
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Class for placing text controls onto a GUI, in a special table where the whole table can wrap around to new columns.
Concept:
If whole table, unconstrained would look like this:
1 2 3
4 5
6
7 8
9 10 11
12 13 14 15
We could break it up into multiple columns, like this:
1 2 3 | 7 8
4 5 | 9 10 11
6 | 12 13 14 15
Usage:
Create a new FlexTable instance
Start adding cells (.addCell)
Add new rows (.addRow) and break into new columns (.addColumn) as needed.
Notes:
Cell width is not enforced at all - if given it will be used, and if not it will be calculated based on the label's contents (not relative to other rows).
The first column, and the first row per column, are implied and do not need to be explicitly added.
New columns will start <columnPadding> pixels away from the right-most edge of any cells added in the previous column.
Example:
Code:
flex := new FlexTable("Gui105") ; Start table at 0,0 in gui and use default properties.
; First column and row are implied, so no need to add them
flex.addCell("a") ; First cell on first row, with calculated width to match contents
flex.addCell("b", 10, 50) ; Another cell on same row, starting 10px out from the previous one with a width of 50px
flex.addCell("c") ; Another cell on same row, starting at end of previous cell
flex.addRow() ; New row (like hitting return in a text editor)
flex.addHeaderCell("HEADER") ; Special kind of cell with formatting (bold/underline)
flex.addRow() ; New row
flex.addCell("d") ; First cell in row, calculated width
flex.addCell("e") ; Next cell in row, starting immediately after
flex.addRow()
flex.addCell("f")
flex.addCell("g")
flex.addColumn()
flex.addHeaderCell("NEXT COLUMN HEADER")
flex.addRow()
flex.addCell("Right-aligned value", 0, 100, "Right")
Result (ALL CAPS are bolded/underlined):
a b c NEXT COLUMN HEADER
HEADER! Right-aligned value
de
fg
*/
class FlexTable {
; ==============================
; == Public ====================
; ==============================
;---------
; DESCRIPTION: Create a new FlexTable instance.
; PARAMETERS:
; guiId (I,REQ) - ID of the GUI that we should add text controls to
; x (I,OPT) - X coordinate (in pixels) where the table should start. Defaults to 0 (left edge).
; y (I,OPT) - Y coordinate (in pixels) where the table should start. Defaults to 0 (top edge).
; rowHeight (I,OPT) - The height (in pixels) that a row should be in the table. Defaults to 25.
; columnPadding (I,OPT) - How much space (in pixels) should be between the end of the right-most
; cell in the previous column, and a new column. Defaults to 30.
; minColumnWidth (I,OPT) - Minimum width that a column must be (regardless of where
; its right-most cell ends). Defaults to 0 (width of contents).
; RETURNS: Reference to new FlexTable object
;---------
__New(guiId, x := 0, y := 0, rowHeight := 25, columnPadding := 30, minColumnWidth := 0) {
this.guiId := guiId
this.rowHeight := rowHeight
this.columnPadding := columnPadding
this.minColumnWidth := minColumnWidth
this.xMin := x
this.yMin := y
this.setX(x)
this.setY(y)
this.xCurrColumn := x
this.xMax := minColumnWidth ; Make the minimum column width the starting point for the right edge of the column.
}
;---------
; DESCRIPTION: Add a "cell" to the current row.
; PARAMETERS:
; cellText (I,OPT) - Text to show in the cell
; leftPadding (I,OPT) - How far out from the previous cell (or start of the row/column if it's the
; first cell) this one should start.
; width (I,OPT) - How wide the cell should be. If not given, will calculate the width of the given text and use that.
; extraProperties (I,OPT) - Any extra properties you want to apply to the text control (i.e. "Right" for right-aligned text)
; RETURNS: Reference to new FlexTable object
;---------
addCell(cellText := "", leftPadding := "", width := "", extraProperties := "") {
this.makeGuiTheDefault()
if(leftPadding)
this.addToX(leftPadding)
propString := "x" this.xCurr " y" this.yCurr
if(width != "")
propString .= " w" width
if(extraProperties != "")
propString .= " " extraProperties
displayText := escapeCharUsingRepeat(cellText, "&") ; Escape any ampersands in the string, as they'll otherwise turn the next character into an underlined hotkey.
Gui, Add, Text, % propString, % displayText
if(width = "")
width := getLabelWidthForText(cellText, FlexTable.getNextUniqueControlId())
this.addToX(width)
}
;---------
; DESCRIPTION: Add a specially-formatted (bold + underline) cell to the current row.
; PARAMETERS:
; titleText (I,REQ) - Text to show in the cell
; leftPadding (I,OPT) - How far out from the previous cell (or start of the row/column if it's the
; first cell) this one should start.
; width (I,OPT) - How wide the cell should be. If not given, will calculate the width of the given text and use that.
; extraProperties (I,OPT) - Any extra properties you want to apply to the text control (i.e. "Right" for right-aligned text)
; RETURNS: Reference to new FlexTable object
;---------
addHeaderCell(titleText, leftPadding := "", width := "", extraProperties := "") {
this.makeGuiTheDefault()
applyTitleFormat()
this.addCell(titleText, leftPadding, width, extraProperties)
clearTitleFormat()
}
;---------
; DESCRIPTION: Add a "cell" to the current row.
; PARAMETERS:
; cellText (I,OPT) - Text to show in the cell
; leftPadding (I,OPT) - How far out from the previous cell (or start of the row/column if it's the
; first cell) this one should start.
; width (I,OPT) - How wide the cell should be. If not given, will calculate the width of the given text and use that.
; extraProperties (I,OPT) - Any extra properties you want to apply to the text control (i.e. "Right" for right-aligned text)
;---------
addRow() {
this.addToY(this.rowHeight)
this.setX(this.xCurrColumn)
}
;---------
; DESCRIPTION: Add a new column (that wraps the whole table, not within the table) to the table
;---------
addColumn() {
this.forceLastColumnToMinWidth()
this.xCurrColumn := this.xMax + this.columnPadding
this.setX(this.xCurrColumn)
this.setY(this.yMin)
; Start the right edge of the column at the minimum column width (if anything pushes it over the edge, that will be the new max)
this.xMax := this.xCurrColumn + this.minColumnWidth
}
;---------
; DESCRIPTION: Get the total height of the table
; RETURNS: The total height of the table, from its starting point to the bottom of the lowest row.
;---------
getTotalHeight() {
return this.yMax - this.yMin + this.rowHeight
}
;---------
; DESCRIPTION: Get the total width of the table
; RETURNS: The total width of the table, from its starting point to the right edge of the last cell.
;---------
getTotalWidth() {
this.forceLastColumnToMinWidth()
return this.xMax - this.xMin
}
; ==============================
; == Private ===================
; ==============================
guiId := ""
static uniqueControlNum := 0 ; Used to get a unique name for each control we have to figure out the width of.
; Top-left corner of table
xMin := ""
yMin := ""
; Bottom-right corner of table
xMax := ""
yMax := ""
; Current position within gui
xCurr := ""
yCurr := ""
; Starting X for current column
xCurrColumn := ""
; Gui sizing/spacing properties
rowHeight := ""
columnPadding := ""
minColumnWidth := ""
;---------
; DESCRIPTION: Add the given value to the current X/Y coordinate.
; PARAMETERS:
; value (I,REQ) - Number to add
;---------
addToX(value) {
this.setX(this.xCurr + value)
}
addToY(value) {
this.setY(this.yCurr + value)
}
;---------
; DESCRIPTION: Set the current X/Y coordinate to the given value.
; PARAMETERS:
; value (I,REQ) - New value
; SIDE EFFECTS: Updates the maximum value for X/Y accordingly
;---------
setX(value) {
this.xCurr := value
this.xMax := max(this.xMax, value)
}
setY(value) {
this.yCurr := value
this.yMax := max(this.yMax, value)
}
;---------
; DESCRIPTION: Get a unique ID to use for determining the width of text within a label.
; RETURNS: "FlexTableControl" + a globally incremented value.
;---------
getNextUniqueControlId() {
FlexTable.uniqueControlNum++
return "FlexTableControl" FlexTable.uniqueControlNum
}
;---------
; DESCRIPTION: Make the gui ID that we were given the default GUI (so
; all of the relevant Gui, * commands apply to it)
;---------
makeGuiTheDefault() {
Gui, % this.guiId ":Default"
}
; Debug info (used by the Debug class)
debugName := "FlexTable"
debugToString(debugBuilder) {
debugBuilder.addLine("Gui ID", this.guiId)
debugBuilder.addLine("Unique control number", this.uniqueControlNum)
debugBuilder.addLine("Min X", this.xMin)
debugBuilder.addLine("Min Y", this.yMin)
debugBuilder.addLine("Max X", this.xMax)
debugBuilder.addLine("Max Y", this.yMax)
debugBuilder.addLine("Row height", this.rowHeight)
debugBuilder.addLine("Column padding", this.columnPadding)
debugBuilder.addLine("Min column width", this.minColumnWidth)
}
}