00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "TGSlider.h"
00050 #include "TGPicture.h"
00051 #include "TImage.h"
00052 #include "TEnv.h"
00053 #include "Riostream.h"
00054
00055 ClassImp(TGSlider)
00056 ClassImp(TGVSlider)
00057 ClassImp(TGHSlider)
00058
00059
00060
00061 TGSlider::TGSlider(const TGWindow *p, UInt_t w, UInt_t h, UInt_t type, Int_t id,
00062 UInt_t options, ULong_t back)
00063 : TGFrame(p, w, h, options, back)
00064 {
00065
00066
00067 fDisabledPic = 0;
00068 fWidgetId = id;
00069 fWidgetFlags = kWidgetWantFocus | kWidgetIsEnabled;
00070 fMsgWindow = p;
00071
00072 fType = type;
00073 fScale = 10;
00074 fDragging = kFALSE;
00075 fPos = fRelPos = 0;
00076 fVmax = fVmin = 0;
00077 }
00078
00079
00080 void TGSlider::CreateDisabledPicture()
00081 {
00082
00083
00084 if (!fSliderPic) return;
00085
00086 TImage *img = TImage::Create();
00087 TImage *img2 = TImage::Create();
00088
00089 TString back = gEnv->GetValue("Gui.BackgroundColor", "#c0c0c0");
00090 img2->FillRectangle(back.Data(), 0, 0, fSliderPic->GetWidth(),
00091 fSliderPic->GetHeight());
00092 img->SetImage(fSliderPic->GetPicture(), fSliderPic->GetMask());
00093 Pixmap_t mask = img->GetMask();
00094 img2->Merge(img, "overlay");
00095
00096 TString name = "disbl_";
00097 name += fSliderPic->GetName();
00098 fDisabledPic = fClient->GetPicturePool()->GetPicture(name.Data(),
00099 img2->GetPixmap(), mask);
00100 delete img;
00101 delete img2;
00102 }
00103
00104
00105 void TGSlider::SetState(Bool_t state)
00106 {
00107
00108
00109 if (state) {
00110 SetFlags(kWidgetIsEnabled);
00111 } else {
00112 ClearFlags(kWidgetIsEnabled);
00113 }
00114 fClient->NeedRedraw(this);
00115 }
00116
00117
00118 TGVSlider::TGVSlider(const TGWindow *p, UInt_t h, UInt_t type, Int_t id,
00119 UInt_t options, ULong_t back) :
00120 TGSlider(p, kSliderWidth, h, type, id, options, back)
00121 {
00122
00123
00124 if ((fType & kSlider1))
00125 fSliderPic = fClient->GetPicture("slider1h.xpm");
00126 else
00127 fSliderPic = fClient->GetPicture("slider2h.xpm");
00128
00129 if (!fSliderPic)
00130 Error("TGVSlider", "slider?h.xpm not found");
00131
00132 CreateDisabledPicture();
00133
00134 gVirtualX->GrabButton(fId, kAnyButton, kAnyModifier,
00135 kButtonPressMask | kButtonReleaseMask |
00136 kPointerMotionMask, kNone, kNone);
00137
00138 AddInput(kStructureNotifyMask);
00139
00140 fPos = h/2; fVmin = 0; fVmax = h; fYp = 0;
00141 fEditDisabled = kEditDisableWidth;
00142
00143 if (!p && fClient->IsEditable()) {
00144 Resize(GetDefaultWidth(), 100);
00145 }
00146 }
00147
00148
00149 TGVSlider::~TGVSlider()
00150 {
00151
00152
00153 if (fSliderPic) fClient->FreePicture(fSliderPic);
00154 if (fDisabledPic) fClient->FreePicture(fDisabledPic);
00155 }
00156
00157
00158 void TGVSlider::DoRedraw()
00159 {
00160
00161
00162
00163 gVirtualX->ClearWindow(fId);
00164
00165 GContext_t drawGC = IsEnabled() ? GetBlackGC()() : GetShadowGC()();
00166
00167 gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth/2, 8, fWidth/2-1, 8);
00168 gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth/2-1, 8, fWidth/2-1, fHeight-9);
00169 gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth/2+1, 8, fWidth/2+1, fHeight-8);
00170 gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth/2+1, fHeight-8, fWidth/2, fHeight-8);
00171 gVirtualX->DrawLine(fId, drawGC, fWidth/2, 9, fWidth/2, fHeight-9);
00172
00173
00174 if (fScale == 1) fScale++;
00175 if (fScale * 2 > (int)fHeight) fScale = 0;
00176 if (fScale > 0 && !(fType & kScaleNo)) {
00177 int lines = ((int)fHeight-16) / fScale;
00178 int remain = ((int)fHeight-16) % fScale;
00179 if (lines < 1) lines = 1;
00180 for (int i = 0; i <= lines; i++) {
00181 int y = i * fScale + (i * remain) / lines;
00182 gVirtualX->DrawLine(fId, drawGC, fWidth/2+8, y+7, fWidth/2+10, y+7);
00183 if ((fType & kSlider2) && (fType & kScaleBoth))
00184 gVirtualX->DrawLine(fId, drawGC, fWidth/2-9, y+7, fWidth/2-11, y+7);
00185 }
00186 }
00187 if (fPos < fVmin) fPos = fVmin;
00188 if (fPos > fVmax) fPos = fVmax;
00189
00190
00191 fRelPos = (((int)fHeight-16) * (fPos - fVmin)) / (fVmax - fVmin) + 8;
00192 const TGPicture *pic = fSliderPic;
00193 if (!IsEnabled()) {
00194 if (!fDisabledPic) CreateDisabledPicture();
00195 pic = fDisabledPic ? fDisabledPic : fSliderPic;
00196 }
00197 if (pic) pic->Draw(fId, GetBckgndGC()(), fWidth/2-7, fRelPos-6);
00198 }
00199
00200
00201 Bool_t TGVSlider::HandleButton(Event_t *event)
00202 {
00203
00204
00205 if (!IsEnabled()) return kTRUE;
00206 if (event->fType == kButtonPress) {
00207
00208 if (event->fX < (Int_t)fWidth/2-7 || event->fX > (Int_t)fWidth/2+7) {
00209 return kTRUE;
00210 }
00211
00212 gVirtualX->GrabPointer(fId, kButtonPressMask | kButtonReleaseMask |
00213 kPointerMotionMask, kNone, kNone,
00214 kTRUE, kFALSE);
00215
00216 if (event->fY >= fRelPos - 7 && event->fY <= fRelPos + 7) {
00217
00218 fDragging = kTRUE;
00219 fYp = event->fY - (fRelPos-7);
00220 SendMessage(fMsgWindow, MK_MSG(kC_VSLIDER, kSL_PRESS), fWidgetId, 0);
00221 fClient->ProcessLine(fCommand, MK_MSG(kC_VSLIDER, kSL_PRESS), fWidgetId, 0);
00222 Pressed();
00223 } else {
00224 if (event->fCode == kButton1) {
00225
00226 int m = (fVmax - fVmin) / (fHeight-16);
00227 if (event->fY < fRelPos) {
00228 fPos -= ((m) ? m : 1);
00229 }
00230 if (event->fY > fRelPos) {
00231 fPos += ((m) ? m : 1);
00232 }
00233 } else if (event->fCode == kButton2) {
00234
00235 fPos = ((fVmax - fVmin) * event->fY) / (fHeight-16) + fVmin;
00236 }
00237 if (fPos > fVmax) fPos = fVmax;
00238 if (fPos < fVmin) fPos = fVmin;
00239 SendMessage(fMsgWindow, MK_MSG(kC_VSLIDER, kSL_POS),
00240 fWidgetId, fPos);
00241 fClient->ProcessLine(fCommand, MK_MSG(kC_VSLIDER, kSL_POS),
00242 fWidgetId, fPos);
00243 PositionChanged(fPos);
00244 }
00245 fClient->NeedRedraw(this);
00246
00247 } else {
00248
00249 fDragging = kFALSE;
00250 gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE);
00251
00252 SendMessage(fMsgWindow, MK_MSG(kC_VSLIDER, kSL_RELEASE), fWidgetId, 0);
00253 fClient->ProcessLine(fCommand, MK_MSG(kC_VSLIDER, kSL_RELEASE), fWidgetId, 0);
00254 Released();
00255 }
00256 return kTRUE;
00257 }
00258
00259
00260 Bool_t TGVSlider::HandleMotion(Event_t *event)
00261 {
00262
00263
00264 if (fDragging) {
00265 int old = fPos;
00266 fPos = ((fVmax - fVmin) * (event->fY - fYp)) / ((int)fHeight-16) + fVmin;
00267 if (fPos > fVmax) fPos = fVmax;
00268 if (fPos < fVmin) fPos = fVmin;
00269
00270
00271 if (old != fPos) {
00272 fClient->NeedRedraw(this);
00273 SendMessage(fMsgWindow, MK_MSG(kC_VSLIDER, kSL_POS),
00274 fWidgetId, fPos);
00275 fClient->ProcessLine(fCommand, MK_MSG(kC_VSLIDER, kSL_POS),
00276 fWidgetId, fPos);
00277 PositionChanged(fPos);
00278 }
00279 }
00280 return kTRUE;
00281 }
00282
00283
00284 Bool_t TGVSlider::HandleConfigureNotify(Event_t* event)
00285 {
00286
00287
00288 TGFrame::HandleConfigureNotify(event);
00289 fClient->NeedRedraw(this);
00290 return kTRUE;
00291 }
00292
00293
00294 TGHSlider::TGHSlider(const TGWindow *p, UInt_t w, UInt_t type, Int_t id,
00295 UInt_t options, ULong_t back) :
00296 TGSlider(p, w, kSliderHeight, type, id, options, back)
00297 {
00298
00299
00300 if ((fType & kSlider1))
00301 fSliderPic = fClient->GetPicture("slider1v.xpm");
00302 else
00303 fSliderPic = fClient->GetPicture("slider2v.xpm");
00304
00305 if (!fSliderPic)
00306 Error("TGHSlider", "slider?v.xpm not found");
00307
00308 CreateDisabledPicture();
00309
00310 gVirtualX->GrabButton(fId, kAnyButton, kAnyModifier,
00311 kButtonPressMask | kButtonReleaseMask |
00312 kPointerMotionMask, kNone, kNone);
00313
00314 AddInput(kStructureNotifyMask);
00315
00316 fPos = w/2; fVmin = 0; fVmax = w; fXp = 0;
00317 fEditDisabled = kEditDisableHeight;
00318
00319 if (!p && fClient->IsEditable()) {
00320 Resize(100, GetDefaultHeight());
00321 }
00322 }
00323
00324
00325 TGHSlider::~TGHSlider()
00326 {
00327
00328
00329 if (fSliderPic) fClient->FreePicture(fSliderPic);
00330 if (fDisabledPic) fClient->FreePicture(fDisabledPic);
00331 }
00332
00333
00334 void TGHSlider::DoRedraw()
00335 {
00336
00337
00338
00339 gVirtualX->ClearWindow(fId);
00340
00341 GContext_t drawGC = IsEnabled() ? GetBlackGC()() : GetShadowGC()();
00342
00343 gVirtualX->DrawLine(fId, GetShadowGC()(), 8, fHeight/2, 8, fHeight/2-1);
00344 gVirtualX->DrawLine(fId, GetShadowGC()(), 8, fHeight/2-1, fWidth-9, fHeight/2-1);
00345 gVirtualX->DrawLine(fId, GetHilightGC()(), 8, fHeight/2+1, fWidth-8, fHeight/2+1);
00346 gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-8, fHeight/2+1, fWidth-8, fHeight/2);
00347 gVirtualX->DrawLine(fId, drawGC, 9, fHeight/2, fWidth-9, fHeight/2);
00348
00349 if (fScale == 1) fScale++;
00350 if (fScale * 2 > (int)fWidth) fScale = 0;
00351 if (fScale > 0 && !(fType & kScaleNo)) {
00352 int lines = ((int)fWidth-16) / fScale;
00353 int remain = ((int)fWidth-16) % fScale;
00354 if (lines < 1) lines = 1;
00355 for (int i = 0; i <= lines; i++) {
00356 int x = i * fScale + (i * remain) / lines;
00357 gVirtualX->DrawLine(fId, drawGC, x+7, fHeight/2+8, x+7, fHeight/2+10);
00358 if ((fType & kSlider2) && (fType & kScaleBoth))
00359 gVirtualX->DrawLine(fId, drawGC, x+7, fHeight/2-9, x+7, fHeight/2-11);
00360 }
00361 }
00362 if (fPos < fVmin) fPos = fVmin;
00363 if (fPos > fVmax) fPos = fVmax;
00364
00365
00366 fRelPos = (((int)fWidth-16) * (fPos - fVmin)) / (fVmax - fVmin) + 8;
00367 const TGPicture *pic = fSliderPic;
00368 if (!IsEnabled()) {
00369 if (!fDisabledPic) CreateDisabledPicture();
00370 pic = fDisabledPic ? fDisabledPic : fSliderPic;
00371 }
00372 if (pic) pic->Draw(fId, GetBckgndGC()(), fRelPos-6, fHeight/2-7);
00373 }
00374
00375
00376 Bool_t TGHSlider::HandleButton(Event_t *event)
00377 {
00378
00379
00380 if (!IsEnabled()) return kTRUE;
00381 if (event->fType == kButtonPress) {
00382
00383 if (event->fY < (Int_t)fHeight/2-7 || event->fY > (Int_t)fHeight/2+7) {
00384 return kTRUE;
00385 }
00386 if (event->fX >= fRelPos - 7 && event->fX <= fRelPos + 7) {
00387
00388 fDragging = kTRUE;
00389 fXp = event->fX - (fRelPos-7);
00390 SendMessage(fMsgWindow, MK_MSG(kC_HSLIDER, kSL_PRESS), fWidgetId, 0);
00391 fClient->ProcessLine(fCommand, MK_MSG(kC_HSLIDER, kSL_PRESS), fWidgetId, 0);
00392 Pressed();
00393 } else {
00394 if (event->fCode == kButton1) {
00395 int m = (fVmax - fVmin) / (fWidth-16);
00396 if (event->fX < fRelPos) {
00397 fPos -= ((m) ? m : 1);
00398 }
00399 if (event->fX > fRelPos) {
00400 fPos += ((m) ? m : 1);
00401 }
00402 } else if (event->fCode == kButton2) {
00403 fPos = ((fVmax - fVmin) * event->fX) / (fWidth-16) + fVmin;
00404 }
00405 if (fPos > fVmax) fPos = fVmax;
00406 if (fPos < fVmin) fPos = fVmin;
00407 SendMessage(fMsgWindow, MK_MSG(kC_HSLIDER, kSL_POS),
00408 fWidgetId, fPos);
00409 fClient->ProcessLine(fCommand, MK_MSG(kC_HSLIDER, kSL_POS),
00410 fWidgetId, fPos);
00411 PositionChanged(fPos);
00412 }
00413 fClient->NeedRedraw(this);
00414
00415
00416 gVirtualX->GrabPointer(fId, kButtonPressMask | kButtonReleaseMask | kPointerMotionMask,
00417 kNone, kNone, kTRUE, kFALSE);
00418 } else {
00419
00420 fDragging = kFALSE;
00421 gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE);
00422
00423 SendMessage(fMsgWindow, MK_MSG(kC_HSLIDER, kSL_RELEASE), fWidgetId, 0);
00424 fClient->ProcessLine(fCommand, MK_MSG(kC_HSLIDER, kSL_RELEASE), fWidgetId, 0);
00425 Released();
00426 }
00427 return kTRUE;
00428 }
00429
00430
00431 Bool_t TGHSlider::HandleMotion(Event_t *event)
00432 {
00433
00434
00435 if (fDragging) {
00436 int old = fPos;
00437 fPos = ((fVmax - fVmin) * (event->fX - fXp)) / ((int)fWidth-16) + fVmin;
00438 if (fPos > fVmax) fPos = fVmax;
00439 if (fPos < fVmin) fPos = fVmin;
00440
00441
00442 if (old != fPos) {
00443 fClient->NeedRedraw(this);
00444 SendMessage(fMsgWindow, MK_MSG(kC_HSLIDER, kSL_POS),
00445 fWidgetId, fPos);
00446 fClient->ProcessLine(fCommand, MK_MSG(kC_HSLIDER, kSL_POS),
00447 fWidgetId, fPos);
00448 PositionChanged(fPos);
00449 }
00450 }
00451 return kTRUE;
00452 }
00453
00454
00455 Bool_t TGHSlider::HandleConfigureNotify(Event_t* event)
00456 {
00457
00458
00459 TGFrame::HandleConfigureNotify(event);
00460 fClient->NeedRedraw(this);
00461 return kTRUE;
00462 }
00463
00464
00465 TString TGSlider::GetTypeString() const
00466 {
00467
00468
00469 TString stype;
00470
00471 if (fType) {
00472 if (fType & kSlider1) {
00473 if (stype.Length() == 0) stype = "kSlider1";
00474 else stype += " | kSlider1";
00475 }
00476 if (fType & kSlider2) {
00477 if (stype.Length() == 0) stype = "kSlider2";
00478 else stype += " | kSlider2";
00479 }
00480 if (fType & kScaleNo) {
00481 if (stype.Length() == 0) stype = "kScaleNo";
00482 else stype += " | kScaleNo";
00483 }
00484 if (fType & kScaleDownRight) {
00485 if (stype.Length() == 0) stype = "kScaleDownRight";
00486 else stype += " | kScaleDownRight";
00487 }
00488 if (fType & kScaleBoth) {
00489 if (stype.Length() == 0) stype = "kScaleBoth";
00490 else stype += " | kScaleBoth";
00491 }
00492 }
00493 return stype;
00494 }
00495
00496
00497 void TGHSlider::SavePrimitive(ostream &out, Option_t *option )
00498 {
00499
00500
00501 if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
00502
00503 out <<" TGHSlider *";
00504 out << GetName() << " = new TGHSlider(" << fParent->GetName()
00505 << "," << GetWidth() << ",";
00506 out << GetTypeString() << "," << WidgetId();
00507
00508 if (fBackground == GetDefaultFrameBackground()) {
00509 if (!GetOptions()) {
00510 out <<");" << endl;
00511 } else {
00512 out << "," << GetOptionString() <<");" << endl;
00513 }
00514 } else {
00515 out << "," << GetOptionString() << ",ucolor);" << endl;
00516 }
00517 if (option && strstr(option, "keep_names"))
00518 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00519
00520 if (fVmin != 0 || fVmax != (Int_t)fWidth)
00521 out << " " << GetName() <<"->SetRange(" << fVmin << "," << fVmax << ");" << endl;
00522
00523 if (fPos != (Int_t)fWidth/2)
00524 out << " " << GetName() <<"->SetPosition(" << GetPosition() << ");" << endl;
00525
00526 if (fScale != 10)
00527 out << " " << GetName() <<"->SetScale(" << fScale << ");" << endl;
00528
00529 if (!IsEnabled())
00530 out << " " << GetName() <<"->SetState(kFALSE);" << endl;
00531 }
00532
00533
00534 void TGVSlider::SavePrimitive(ostream &out, Option_t *option )
00535 {
00536
00537
00538 if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
00539
00540 out<<" TGVSlider *";
00541 out << GetName() <<" = new TGVSlider("<< fParent->GetName()
00542 << "," << GetHeight() << ",";
00543 out << GetTypeString() << "," << WidgetId();
00544
00545 if (fBackground == GetDefaultFrameBackground()) {
00546
00547 if (!GetOptions()) {
00548 out <<");" << endl;
00549 } else {
00550 out << "," << GetOptionString() <<");" << endl;
00551 }
00552 } else {
00553 out << "," << GetOptionString() << ",ucolor);" << endl;
00554 }
00555 if (option && strstr(option, "keep_names"))
00556 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00557
00558 if (fVmin != 0 || fVmax != (Int_t)fHeight)
00559 out << " " << GetName() <<"->SetRange(" << fVmin << "," << fVmax << ");" << endl;
00560
00561 if (fPos != (Int_t)fHeight/2)
00562 out << " " << GetName() <<"->SetPosition(" << GetPosition() << ");" << endl;
00563
00564 if (fScale != 10)
00565 out << " " << GetName() <<"->SetScale(" << fScale << ");" << endl;
00566
00567 if (!IsEnabled())
00568 out << " " << GetName() <<"->SetState(kFALSE);" << endl;
00569 }