00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "precomp.h"
00010 #include "scrollbar_generic.h"
00011 #include "API/GUI/scrollbar.h"
00012 #include "API/GUI/component.h"
00013 #include "API/GUI/component_options.h"
00014 #include "API/GUI/stylemanager.h"
00015 #include "API/GUI/button.h"
00016 #include "API/Display/Input/input.h"
00017 #include "API/Display/Input/mouse.h"
00018
00019 #define cl_max(a,b) (a > b ? a : b)
00020 #define cl_min(a,b) (a < b ? a : b)
00021
00023
00024
00025 CL_ComponentOptions CL_ScrollBar_Generic::create_options(
00026 const CL_Rect &pos,
00027 int min,
00028 int max,
00029 int value,
00030 bool orientation,
00031 bool tracking)
00032 {
00033 CL_ComponentOptions options;
00034
00035 options.add_option_as_int("x", pos.x1);
00036 options.add_option_as_int("y", pos.y1);
00037 options.add_option_as_int("width", pos.get_width());
00038 options.add_option_as_int("height", pos.get_height());
00039 options.add_option_as_int("min", min);
00040 options.add_option_as_int("max", max);
00041 options.add_option_as_int("value", value);
00042 options.add_option_as_bool("orientation", orientation);
00043 options.add_option_as_bool("tracking", tracking);
00044
00045 return options;
00046 }
00047
00048 CL_ScrollBar_Generic::CL_ScrollBar_Generic(
00049 CL_ScrollBar *self,
00050 const CL_ComponentOptions &options,
00051 CL_StyleManager *style)
00052 :
00053 scrollbar(self),
00054 tracking(true),
00055 dragging(false),
00056 fixed_length(false),
00057 slider_length(16),
00058 initialized(false)
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 {
00080 if(options.exists("orientation"))
00081 {
00082 CL_String s(options.get_value("orientation"));
00083 s.to_lower();
00084 if (s == "hor" || s == "horizontal" || s == "1")
00085 vertical = false;
00086 else if (s == "ver" || s == "vertical" || s == "0")
00087 vertical = true;
00088 else
00089 throw CL_Error("Invalid scrollbar orientation value");
00090 }
00091 else
00092 vertical = false;
00093
00094 if (options.exists("min"))
00095 set_min_value(options.get_value_as_int("min"));
00096 else
00097 set_min_value(0);
00098
00099 if (options.exists("max"))
00100 set_max_value(options.get_value_as_int("max"));
00101 else
00102 set_max_value(min_value);
00103
00104 if (options.exists("value"))
00105 set_value(options.get_value_as_int("value"));
00106 else
00107 set_value(min_value);
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 CL_Rect rect(0, 0, scrollbar->get_width(), scrollbar->get_height());
00129 client_area = new CL_Component(rect, scrollbar, style);
00130
00131
00132
00133
00134
00135
00136
00137 slot_keydown = scrollbar->sig_key_down().connect(
00138 CL_CreateSlot(this, &CL_ScrollBar_Generic::on_key_down));
00139 slot_keyup = scrollbar->sig_key_up().connect(
00140 CL_CreateSlot(this, &CL_ScrollBar_Generic::on_key_up));
00141 slot_child_add = scrollbar->sig_child_add().connect(
00142 CL_CreateSlot(this, &CL_ScrollBar_Generic::on_child_add));
00143 slot_child_remove = scrollbar->sig_child_remove().connect(
00144 CL_CreateSlot(this, &CL_ScrollBar_Generic::on_child_remove));
00145
00146 initialized = true;
00147 }
00148
00150
00151
00152 int CL_ScrollBar_Generic::get_range() const
00153 {
00154 return max_value - min_value + 1;
00155 }
00156
00158
00159
00160 void CL_ScrollBar_Generic::set_vertical(bool enable)
00161 {
00162 throw CL_Error("CL_ScrollBar_Generic::set_vertical() is not implemented");
00163 }
00164
00165 void CL_ScrollBar_Generic::set_min_value(int value)
00166 {
00167 min_value = value;
00168 if(max_value < min_value)
00169 max_value = min_value;
00170
00171 if(cur_value < min_value)
00172 cur_value = min_value;
00173
00174 calculate_slider_size();
00175 }
00176
00177 void CL_ScrollBar_Generic::set_max_value(int value)
00178 {
00179 max_value = value;
00180 if(max_value < min_value)
00181 max_value = min_value;
00182
00183 if(cur_value > max_value)
00184 cur_value = max_value;
00185
00186 calculate_slider_size();
00187 }
00188
00189 void CL_ScrollBar_Generic::set_value(int value)
00190 {
00191 if (value < min_value) value = min_value;
00192 if (value > max_value) value = max_value;
00193
00194 if (cur_value != value)
00195 {
00196 cur_value = value;
00197 sig_value_changed(cur_value);
00198 }
00199
00200 calculate_slider_size();
00201 }
00202
00204
00205
00206 void CL_ScrollBar_Generic::on_child_add(CL_Component *child)
00207 {
00208 CL_Rect rect_child = child->get_position();
00209 CL_Rect rect_clientarea = client_area->get_position();
00210
00211
00212
00213
00214
00215
00216 if(rect_child.y2 < rect_clientarea.get_height() / 2)
00217 rect_clientarea.y1 = rect_child.y2;
00218 else
00219 rect_clientarea.y2 = rect_child.y1;
00220
00221 client_area->set_position(rect_clientarea);
00222
00223 calculate_slider_size();
00224 }
00225
00226 void CL_ScrollBar_Generic::on_child_remove(CL_Component *child)
00227 {
00228 }
00229
00230 void CL_ScrollBar_Generic::on_key_down(CL_Component *comp, CL_InputDevice *device, CL_Key key)
00231 {
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 }
00292
00293 void CL_ScrollBar_Generic::on_key_up(CL_Component *comp, CL_InputDevice *device, CL_Key key)
00294 {
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00507
00508
00509 void CL_ScrollBar_Generic::calculate_slider_size()
00510 {
00511 if(initialized == false)
00512 return;
00513
00514 int slider_offset;
00515
00516 if(fixed_length)
00517 {
00518 throw CL_Error("fixed-length sliders not implemented");
00519 }
00520 else {
00521 int scrollbar_length;
00522 if (vertical)
00523 scrollbar_length = scrollbar->get_client_area()->get_height();
00524 else
00525 scrollbar_length = scrollbar->get_client_area()->get_width();
00526
00527 int range = get_range();
00528
00529 slider_length = scrollbar_length / range;
00530 if(slider_length < 20)
00531 slider_length = 20;
00532
00533 float y = 0.0f;
00534 if(range > 1)
00535 {
00536 int available_area = scrollbar_length - slider_length;
00537 y = (float)available_area / (range - 1);
00538 }
00539
00540 slider_offset = y * (cur_value - min_value);
00541 }
00542
00543 rect_slider.y1 = slider_offset;
00544 rect_slider.y2 = slider_offset + slider_length;
00545 rect_slider.x1 = 0;
00546 rect_slider.x2 = scrollbar->get_client_area()->get_width();
00547 }
00548