Browse Source

cmDspBuiltIn.c : Added cmDspCheckbox() as an improved check box control

which can be used with specific on/off values and symbols.
master
kevin 11 years ago
parent
commit
f41a6d6709
1 changed files with 167 additions and 0 deletions
  1. 167
    0
      dsp/cmDspBuiltIn.c

+ 167
- 0
dsp/cmDspBuiltIn.c View File

@@ -2156,6 +2156,172 @@ struct cmDspClass_str* cmButtonClassCons( cmDspCtx_t* ctx )
2156 2156
 }
2157 2157
 
2158 2158
 //==========================================================================================================================================
2159
+enum
2160
+{
2161
+  kLblCbId,
2162
+  kSym1CbId,
2163
+  kSym0CbId,
2164
+  kVal1CbId,
2165
+  kVal0CbId,
2166
+  kOutCbId,
2167
+  kSymCbId,
2168
+  kInCbId
2169
+};
2170
+
2171
+cmDspClass_t _cmCheckboxDC;
2172
+
2173
+typedef struct
2174
+{
2175
+  cmDspInst_t inst;
2176
+  unsigned    resetSymId;
2177
+  unsigned    onSymId;
2178
+  unsigned    offSymId;
2179
+} cmDspCheckbox_t;
2180
+
2181
+cmDspInst_t*  _cmDspCheckboxAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
2182
+{
2183
+
2184
+
2185
+  // Check buttons should transmit their default values. Set kSendDfltDsvFl on outputs to send default values.
2186
+  
2187
+  cmDspVarArg_t args[] =
2188
+  {
2189
+    { "label",kLblCbId,  0, 0, kInDsvFl  | kStrzDsvFl   | kOptArgDsvFl,   "Label"},
2190
+    { "sym1", kSym1CbId, 0, 0, kInDsvFl  | kStrzDsvFl   | kOptArgDsvFl,   "'on' symbol value"},
2191
+    { "sym0", kSym0CbId, 0, 0, kInDsvFl  | kStrzDsvFl   | kOptArgDsvFl,   "'off' symbol value"},
2192
+    { "val1", kVal1CbId, 0, 0, kInDsvFl  | kDoubleDsvFl | kOptArgDsvFl,   "'on' value"},
2193
+    { "val0", kVal0CbId, 0, 0, kInDsvFl  | kDoubleDsvFl | kOptArgDsvFl,   "'off' value"},
2194
+    { "out",  kOutCbId,  0, 0, kOutDsvFl | kDoubleDsvFl | kOptArgDsvFl | kSendDfltDsvFl, "Value"},
2195
+    { "sym",  kSymCbId,  0, 0, kOutDsvFl | kSymDsvFl                   | kSendDfltDsvFl, "Symbol Value"},
2196
+    { "in",   kInCbId,   0, 0, kInDsvFl  | kTypeDsvMask,                  "Simulate UI"},
2197
+    { NULL, 0, 0, 0, 0 }
2198
+  };
2199
+
2200
+  cmDspCheckbox_t* p = cmDspInstAlloc(cmDspCheckbox_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
2201
+
2202
+  
2203
+  cmDspSetDefaultDouble(ctx, &p->inst, kVal1CbId, 0.0, 1.0);
2204
+  cmDspSetDefaultDouble(ctx, &p->inst, kVal0CbId, 0.0, 0.0);
2205
+  cmDspSetDefaultStrcz( ctx, &p->inst, kSym1CbId, NULL, "on");
2206
+  cmDspSetDefaultStrcz( ctx, &p->inst, kSym0CbId, NULL, "off");
2207
+  cmDspSetDefaultDouble(ctx, &p->inst, kOutCbId,  0.0, 0.0);
2208
+  cmDspSetDefaultSymbol(ctx, &p->inst, kSymCbId,        instSymId );
2209
+  cmDspSetDefaultStrcz( ctx, &p->inst, kLblCbId, NULL, cmSymTblLabel(ctx->stH,instSymId));
2210
+
2211
+  p->resetSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"_reset");
2212
+  p->onSymId    = cmSymTblRegisterSymbol(ctx->stH, cmDspStrcz(&p->inst,kSym1CbId));
2213
+  p->offSymId   = cmSymTblRegisterSymbol(ctx->stH, cmDspStrcz(&p->inst,kSym0CbId));
2214
+
2215
+  // create the UI control
2216
+  cmDspUiButtonCreate(ctx,&p->inst,kCheckDuiId,kOutCbId,kLblCbId);
2217
+
2218
+  return &p->inst;
2219
+}
2220
+
2221
+cmDspRC_t _cmDspCheckboxReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
2222
+{
2223
+  cmDspApplyAllDefaults(ctx,inst);
2224
+  return kOkDspRC;
2225
+}
2226
+
2227
+cmDspRC_t _cmDspCheckboxRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
2228
+{
2229
+  cmDspRC_t rc = kOkDspRC;
2230
+  cmDspCheckbox_t* p = (cmDspCheckbox_t*)inst;
2231
+
2232
+  switch( evt->dstVarId )
2233
+  {
2234
+    case kLblCbId:
2235
+      // TODO: we have no function for changing a UI control label.
2236
+      return rc;
2237
+
2238
+    case kVal1CbId:
2239
+    case kVal0CbId:
2240
+    case kSym1CbId:
2241
+    case kSym0CbId:
2242
+      cmDspSetEvent(ctx,inst,evt);
2243
+
2244
+      if( evt->dstVarId == kSym1CbId )
2245
+        p->onSymId    = cmSymTblRegisterSymbol(ctx->stH, cmDspStrcz(&p->inst,kSym1CbId));
2246
+
2247
+      if( evt->dstVarId == kSym0CbId )
2248
+        p->offSymId   = cmSymTblRegisterSymbol(ctx->stH, cmDspStrcz(&p->inst,kSym0CbId));
2249
+
2250
+      return rc;
2251
+
2252
+  }
2253
+
2254
+  // the 'in' port is the only input port 
2255
+  // but the UI button pushes use kOutCbId.
2256
+  assert( evt->dstVarId == kInCbId || evt->dstVarId == kOutCbId );
2257
+
2258
+  // We accept all types at the 'in' port but are only interested
2259
+  // in transmitting doubles from the 'out' port and symbols from 
2260
+  // the 'sym' port.
2261
+
2262
+  if( cmDsvCanConvertFlags( kDoubleDsvFl, evt->valuePtr->flags ) )
2263
+  {
2264
+    bool checkFl = cmDsvGetDouble(evt->valuePtr)!=0;
2265
+
2266
+    unsigned valId = checkFl ? kVal1CbId  : kVal0CbId;
2267
+    unsigned symId = checkFl ? p->onSymId : p->offSymId;
2268
+
2269
+
2270
+    // Redirect events which can be converted to type kDoubleDsvFl 
2271
+    // to the output port.
2272
+    // 
2273
+    // Convert the event dest var id from the 'kInCbId' to 'kOutCbId' 
2274
+    // and update the UI  with the incoming value 
2275
+    cmDspEvt_t e;
2276
+    cmDspValue_t v;
2277
+    
2278
+    cmDspEvtCopy(&e,evt);
2279
+    e.valuePtr = &v;
2280
+    cmDsvSetDouble(&v,cmDspDouble(inst,valId));
2281
+    cmDspSetEventUiId(ctx,inst,evt,kOutCbId);
2282
+
2283
+    cmDspSetSymbol( ctx, inst, kSymCbId, symId);
2284
+
2285
+  }
2286
+
2287
+  
2288
+  return kOkDspRC;
2289
+}
2290
+
2291
+cmDspRC_t  _cmDspCheckboxPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t*  inst,  bool storeFl )
2292
+{ 
2293
+  return cmDspVarPresetRdWr(ctx,inst,kOutCbId,storeFl); 
2294
+}
2295
+
2296
+cmDspRC_t  _cmDspCheckboxSysRecvFunc(   cmDspCtx_t* ctx, struct cmDspInst_str* inst,  unsigned attrSymId, const cmDspValue_t* value )
2297
+{
2298
+  cmDspCheckbox_t* p = (cmDspCheckbox_t*)inst;
2299
+  if( attrSymId == p->resetSymId )
2300
+  {
2301
+    cmDspSetSymbol( ctx, inst, kSymCbId, p->resetSymId );
2302
+    cmDspSetDouble(ctx,inst,kOutCbId, cmDspDouble(inst,kOutCbId));
2303
+  }
2304
+
2305
+  return kOkDspRC;
2306
+}
2307
+
2308
+struct cmDspClass_str* cmCheckboxClassCons( cmDspCtx_t* ctx )
2309
+{
2310
+  cmDspClassSetup(&_cmCheckboxDC,ctx,"Checkbox",
2311
+    NULL,
2312
+    _cmDspCheckboxAlloc,
2313
+    NULL,
2314
+    _cmDspCheckboxReset,
2315
+    NULL,
2316
+    _cmDspCheckboxRecv,
2317
+    _cmDspCheckboxPresetRdWr,
2318
+    _cmDspCheckboxSysRecvFunc,
2319
+    "Checkbox control.");
2320
+
2321
+  return &_cmCheckboxDC;
2322
+}
2323
+
2324
+//==========================================================================================================================================
2159 2325
 cmDspClass_t _cmReorderDC;
2160 2326
 
2161 2327
 typedef struct
@@ -5347,6 +5513,7 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
5347 5513
   cmMeterClassCons,
5348 5514
   cmLabelClassCons,
5349 5515
   cmButtonClassCons,
5516
+  cmCheckboxClassCons,
5350 5517
 
5351 5518
   cmReorderClassCons,
5352 5519
   cmFnameClassCons,

Loading…
Cancel
Save