File size: 5,153 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

class CTemplateCSSlider
{
  CTemplateCSSlider *next,*last;
  U8 *name;
  I64 num;
};

class CTemplateCS
{
  CTemplateCSSlider *next_slider,*last_slider;
  I64 num_sliders,range,spacing,border;
  U8 *glbl_name;
};

CTemplateCS *CtrlSliderGet()
{
  CTemplateCSSlider *tmps;
  U8 *st,pmt[STR_LEN];
  CTemplateCS *res=CAlloc(sizeof(CTemplateCS));

  "$PURPLE$Ctrl Slider$FG$\n\n";

  res->glbl_name=GetStr("Glbl Struct Name (ENTER for No Glbl):");
  res->range=GetI64     ("Range   (%4d):",30);
  res->spacing=GetI64("Spacing (%4d):",20);
  res->border=GetI64 ("Border  (%4d):",2);

  res->next_slider=res->last_slider=&res->next_slider;
  while (TRUE) {
    StrPrint(pmt,"Slider #%d Name:",res->num_sliders+1);
    st=GetStr(pmt);
    if (!*st) {
      Free(st);
      break;
    }
    tmps=CAlloc(sizeof(CTemplateCSSlider));
    tmps->name=st;
    QueIns(tmps,res->last_slider);
    tmps->num=res->num_sliders++;
  }
  return res;
}

U0 TemplateCtrlSlider(CDoc *doc)
{
  CTask *task;
  CTemplateCS *m=NULL;
  CTemplateCSSlider *tmps;
  I64 i;

  DocUnlock(doc);
  DocRecalc(doc);
  DocCaptureUndo(doc,TRUE);

  task=User("CTemplateCS **_m=0x%X;*_m=CtrlSliderGet;\n",&m);
  while (!m)
    Yield;

  DocPrint(doc,
"#define SLIDER_RANGE\t%d
#define SLIDER_SPACING\t%d
#define SLIDER_BORDER\t%d

class CSliderState
{
",m->range,m->spacing,m->border);

  tmps=m->next_slider;
  while (tmps!=&m->next_slider) {
    DocPrint(doc,"  I64 %s;\n",tmps->name);
    tmps=tmps->next;
  }
  if (*m->glbl_name)
    DocPrint(doc,"} %s;\n",m->glbl_name);
  else
    DocPrint(doc,"};\n");

  DocPrint(doc,
"
U0 DrawCtrlSlider(CDC *dc,CCtrl *c)
{
  CSliderState *s=c->state;

  dc->color=LTRED;
  GrRect(dc, c->left,c->top,%d*SLIDER_SPACING+%d,SLIDER_SPACING*2+SLIDER_RANGE);
  dc->color=BLUE;
  GrRect(dc, c->left+SLIDER_BORDER,c->top+SLIDER_BORDER,
    %d*SLIDER_SPACING+%d-2*SLIDER_BORDER,SLIDER_SPACING*2
    +SLIDER_RANGE-2*SLIDER_BORDER);
  dc->color=BLACK;
",m->num_sliders+1,m->num_sliders,m->num_sliders+1,m->num_sliders);

  for (i=0;i<m->num_sliders;i++)
    DocPrint(doc,
"  GrLine(dc,c->left+%d*SLIDER_SPACING+%d,c->top+SLIDER_SPACING,
             c->left+%d*SLIDER_SPACING+%d,c->top+SLIDER_SPACING+SLIDER_RANGE-1);
",i+1,i,i+1,i);

  DocPrint(doc,"  dc->color=LTRED;\n");

  tmps=m->next_slider;
  while (tmps!=&m->next_slider) {
    DocPrint(doc,
"  GrPrint(dc,c->left+%d*SLIDER_SPACING+%d-FONT_WIDTH/2,
    c->top+SLIDER_SPACING+SLIDER_RANGE+3,
    \"%%d\",s->%s*10/SLIDER_RANGE);\n",
      tmps->num+1,tmps->num,tmps->name);
    tmps=tmps->next;
  }

  tmps=m->next_slider;
  while (tmps!=&m->next_slider) {
    DocPrint(doc,
        "  GrRect(dc,c->left+%d*SLIDER_SPACING+%d-3,"
        "c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->%s-2,7,5);\n",
      tmps->num+1,tmps->num,tmps->name);
    tmps=tmps->next;
  }
  DocPrint(doc,"  dc->color=YELLOW;\n");

  tmps=m->next_slider;
  while (tmps!=&m->next_slider) {
    DocPrint(doc,"  GrRect(dc,c->left+%d*SLIDER_SPACING+%d-2,"
        "c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->%s-1,5,3);\n",
      tmps->num+1,tmps->num,tmps->name);
    tmps=tmps->next;
  }
  DocPrint(doc,
"}

U0 UpdateDerivedCtrlSlider(CCtrl *c)
{
  CSliderState *s=c->state;
  c->left=c->win_task->pix_width/2-(SLIDER_SPACING*3+2)/2;
  c->right=c->left+%d*SLIDER_SPACING+%d;
  c->top=c->win_task->pix_height/2-(SLIDER_SPACING*2+SLIDER_RANGE)/2;
  c->bottom=c->top+SLIDER_SPACING*2+SLIDER_RANGE;
",m->num_sliders+1,m->num_sliders);

  tmps=m->next_slider;
  while (tmps!=&m->next_slider) {
    DocPrint(doc,"  s->%s=ClampI64(s->%s,0,SLIDER_RANGE-1);\n",
        tmps->name,tmps->name);
    tmps=tmps->next;
  }
  DocPrint(doc,
"}

U0 LeftClickSlider(CCtrl *c,I64 x,I64 y,Bool down)
{
  no_warn down;
  CSliderState *s=c->state;
");

  tmps=m->next_slider;
  while (tmps!=&m->next_slider) {
    DocPrint(doc,"  ");
    if (tmps!=m->next_slider)
      DocPrint(doc,"else ");
    if (tmps->next==&m->next_slider)
      DocPrint(doc,"\n");
    else
      DocPrint(doc,"if (x<c->left+%d*SLIDER_SPACING+%d+SLIDER_SPACING/2)\n",
        tmps->num+1,tmps->num);
    DocPrint(doc,"    s->%s=SLIDER_RANGE-1-(y-(c->top+SLIDER_SPACING));\n",
       tmps->name);
    tmps=tmps->next;
  }

  DocPrint(doc,
"  if (c->update_derived_vals)
    (*c->update_derived_vals)(c);
}

CCtrl *SliderNew()
{
  CCtrl *c=CAlloc(sizeof(CCtrl));
  c->win_task=Fs;
  c->flags=CTRLF_SHOW|CTRLF_CAPTURE_LEFT_MS;
  c->type=CTRLT_GENERIC;
");
  if (*m->glbl_name)
    DocPrint(doc,"  c->state=&%s;\n"
        "  MemSet(&%s,0,sizeof(CSliderState));\n",m->glbl_name,m->glbl_name);
  else
    DocPrint(doc,       "  c->state=CAlloc(sizeof(CSliderState));\n");
  DocPrint(doc,
"  c->draw_it=&DrawCtrlSlider;
  c->left_click=&LeftClickSlider;
  c->update_derived_vals=&UpdateDerivedCtrlSlider;
  QueIns(c,Fs->last_ctrl);
  TaskDerivedValsUpdate;
  return c;
}

U0 SliderDel(CCtrl *c)
{
  QueRem(c);
");
  if (!*m->glbl_name)
    DocPrint(doc,"  Free(c->state);\n");
  DocPrint(doc,
"  Free(c);
}
");

  Kill(task);  //This frees memory.

  RegOneTimePopUp(ARf_MANAGE_SLIDER,
    "Modify the code to your heart's content.\n"
    "The code is no longer managed.\n");

  DocLock(doc);
}