forked from foone/VGAPride
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.cpp
More file actions
276 lines (249 loc) · 6.18 KB
/
Copy pathselector.cpp
File metadata and controls
276 lines (249 loc) · 6.18 KB
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
270
271
272
273
274
275
276
#include <Quickdraw.h>
#include <Fonts.h>
#include <TextUtils.h>
#include <string.h>
#include <ctype.h>
#include "selector.h"
#include "qd_render.h"
#include "mac_compat.h"
static const short kListLeft=10;
static const short kListTop=10;
static const short kPreviewTop=10;
static const short kRowHeight=16;
static const short kMargin=10;
static const short kCreditHeight=18;
static const short kNominalListWidth=190;
static const short kNarrowListWidth=160;
static const short kMinPreviewWidth=320;
static const short kMinPreviewHeight=240;
struct SelectorLayout {
Rect list;
Rect preview;
Rect credit;
};
static WindowPtr selectorWindow=NULL;
static int selectedIndex=0;
static int scrollTop=0;
static int flagCount=0;
static char typeBuffer[64];
static unsigned long lastTypeTick=0;
static int CountFlags(){
int count=0;
while(PRIDE_FLAGS[count]!=NULL){
count++;
}
return count;
}
static void GetLayout(SelectorLayout *layout){
Rect content=WindowPortRect(selectorWindow);
short contentWidth=content.right - content.left;
short listWidth=contentWidth<760 ? kNarrowListWidth : kNominalListWidth;
short previewLeft=kListLeft + listWidth + kMargin;
short availableWidth=content.right - previewLeft - kMargin;
short availableHeight=content.bottom - kPreviewTop - kCreditHeight - (kMargin * 2);
short previewWidth;
short previewHeight;
if(availableWidth<kMinPreviewWidth){
availableWidth=kMinPreviewWidth;
}
if(availableHeight<kMinPreviewHeight){
availableHeight=kMinPreviewHeight;
}
previewWidth=availableWidth;
previewHeight=(short)(((long)previewWidth * 3) / 4);
if(previewHeight>availableHeight){
previewHeight=availableHeight;
previewWidth=(short)(((long)previewHeight * 4) / 3);
}
SetRect(&layout->list, kListLeft, kListTop, kListLeft+listWidth, kListTop+previewHeight);
SetRect(&layout->preview, previewLeft, kPreviewTop, previewLeft+previewWidth, kPreviewTop+previewHeight);
SetRect(&layout->credit, previewLeft, layout->preview.bottom+6, previewLeft+previewWidth, layout->preview.bottom+6+kCreditHeight);
}
static int VisibleRows(){
SelectorLayout layout;
GetLayout(&layout);
return ((layout.list.bottom - layout.list.top) / kRowHeight);
}
static void EnsureSelectionVisible(){
int rows=VisibleRows();
if(selectedIndex<scrollTop){
scrollTop=selectedIndex;
}
if(selectedIndex>=scrollTop+rows){
scrollTop=selectedIndex-rows+1;
}
if(scrollTop<0){
scrollTop=0;
}
if(scrollTop>flagCount-rows){
scrollTop=flagCount-rows;
}
if(scrollTop<0){
scrollTop=0;
}
}
static void DrawScrollbarHint(const Rect *list){
Rect track=*list;
track.left=track.right-8;
RGBColor gray={0x9999,0x9999,0x9999};
RGBForeColor(&gray);
PaintRect(&track);
if(flagCount<=VisibleRows()){
return;
}
Rect thumb=track;
int rows=VisibleRows();
int trackHeight=track.bottom-track.top;
int thumbHeight=(trackHeight*rows)/flagCount;
if(thumbHeight<18){
thumbHeight=18;
}
thumb.top=track.top + ((trackHeight-thumbHeight)*scrollTop)/(flagCount-rows);
thumb.bottom=thumb.top + thumbHeight;
RGBColor dark={0x4444,0x4444,0x4444};
RGBForeColor(&dark);
PaintRect(&thumb);
}
static void DrawList(){
SelectorLayout layout;
Rect list;
GetLayout(&layout);
list=layout.list;
EraseRect(&list);
FrameRect(&list);
TextFont(3);
TextSize(10);
int rows=VisibleRows();
for(int row=0;row<rows;row++){
int index=scrollTop+row;
if(index>=flagCount){
break;
}
Rect rowRect;
SetRect(&rowRect, list.left+1, list.top+1+row*kRowHeight, list.right-9, list.top+1+(row+1)*kRowHeight);
if(index==selectedIndex){
InvertRect(&rowRect);
}
MoveTo(rowRect.left+4, rowRect.top+12);
DrawText((Ptr)PRIDE_FLAGS[index]->name, 0, strlen(PRIDE_FLAGS[index]->name));
}
DrawScrollbarHint(&list);
}
static void DrawPreview(){
SelectorLayout layout;
GetLayout(&layout);
DrawFlag(GetSelectedFlag(), &layout.preview, false);
FrameRect(&layout.preview);
DrawFlagCredit(GetSelectedFlag(), &layout.credit);
}
static bool StartsWithFolded(const char *text, const char *prefix){
while(*prefix){
if(tolower((unsigned char)*text)!=tolower((unsigned char)*prefix)){
return false;
}
text++;
prefix++;
}
return true;
}
static void SelectIndex(int index){
if(index<0){
index=0;
}
if(index>=flagCount){
index=flagCount-1;
}
if(index==selectedIndex){
return;
}
selectedIndex=index;
EnsureSelectionVisible();
DrawSelector(selectorWindow);
}
static void TypeSelect(char ch){
unsigned long now=TickCount();
if(now-lastTypeTick>60){
typeBuffer[0]=0;
}
lastTypeTick=now;
int len=strlen(typeBuffer);
if(len<(int)sizeof(typeBuffer)-1 && ch>=32 && ch<127){
typeBuffer[len]=ch;
typeBuffer[len+1]=0;
}
if(typeBuffer[0]==0){
return;
}
for(int offset=0;offset<flagCount;offset++){
int index=(selectedIndex+offset)%flagCount;
if(StartsWithFolded(PRIDE_FLAGS[index]->name, typeBuffer)){
SelectIndex(index);
return;
}
}
}
void InitSelector(WindowPtr window){
selectorWindow=window;
flagCount=CountFlags();
selectedIndex=0;
scrollTop=0;
typeBuffer[0]=0;
}
void DrawSelector(WindowPtr window){
if(window==NULL){
return;
}
SetWindowPortCompat(window);
Rect content=WindowPortRect(window);
EraseRect(&content);
DrawList();
DrawPreview();
}
bool HandleSelectorEvent(WindowPtr window, const EventRecord *event){
if(window==NULL || event==NULL){
return false;
}
SetWindowPortCompat(window);
if(event->what==keyDown || event->what==autoKey){
char ch=event->message & charCodeMask;
char key=(event->message & keyCodeMask) >> 8;
if(ch==0x1e || key==0x7e){
SelectIndex(selectedIndex-1);
return true;
}
if(ch==0x1f || key==0x7d){
SelectIndex(selectedIndex+1);
return true;
}
if(ch==0x0b || key==0x74){
SelectIndex(selectedIndex-VisibleRows());
return true;
}
if(ch==0x0c || key==0x79){
SelectIndex(selectedIndex+VisibleRows());
return true;
}
if(ch>=32 && ch<127){
TypeSelect(ch);
return true;
}
}
if(event->what==mouseDown){
Point p=event->where;
GlobalToLocal(&p);
SelectorLayout layout;
GetLayout(&layout);
if(PtInRect(p, &layout.list)){
int row=(p.v-layout.list.top-1)/kRowHeight;
SelectIndex(scrollTop+row);
return true;
}
}
return false;
}
Flag *GetSelectedFlag(){
if(flagCount==0){
return NULL;
}
return PRIDE_FLAGS[selectedIndex];
}