| 1 | //============================================================================ |
| 2 | // |
| 3 | // SSSS tt lll lll |
| 4 | // SS SS tt ll ll |
| 5 | // SS tttttt eeee ll ll aaaa |
| 6 | // SSSS tt ee ee ll ll aa |
| 7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
| 8 | // SS SS tt ee ll ll aa aa |
| 9 | // SSSS ttt eeeee llll llll aaaaa |
| 10 | // |
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
| 12 | // and the Stella Team |
| 13 | // |
| 14 | // See the file "License.txt" for information on usage and redistribution of |
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 16 | //============================================================================ |
| 17 | |
| 18 | #include "bspf.hxx" |
| 19 | #include "OSystem.hxx" |
| 20 | #include "FrameBuffer.hxx" |
| 21 | #include "Widget.hxx" |
| 22 | #include "Dialog.hxx" |
| 23 | #include "Font.hxx" |
| 24 | #include "DialogContainer.hxx" |
| 25 | #include "ProgressDialog.hxx" |
| 26 | |
| 27 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 28 | ProgressDialog::ProgressDialog(GuiObject* boss, const GUI::Font& font, |
| 29 | const string& message) |
| 30 | : Dialog(boss->instance(), boss->parent()), |
| 31 | myMessage(nullptr), |
| 32 | mySlider(nullptr), |
| 33 | myStart(0), |
| 34 | myFinish(0), |
| 35 | myStep(0) |
| 36 | { |
| 37 | const int fontWidth = font.getMaxCharWidth(), |
| 38 | fontHeight = font.getFontHeight(), |
| 39 | lineHeight = font.getLineHeight(); |
| 40 | int xpos, ypos, lwidth; |
| 41 | |
| 42 | // Calculate real dimensions |
| 43 | lwidth = font.getStringWidth(message); |
| 44 | _w = lwidth + 2 * fontWidth; |
| 45 | _h = lineHeight * 5; |
| 46 | |
| 47 | xpos = fontWidth; ypos = lineHeight; |
| 48 | myMessage = new StaticTextWidget(this, font, xpos, ypos, lwidth, fontHeight, |
| 49 | message, TextAlign::Center); |
| 50 | myMessage->setTextColor(kTextColorEm); |
| 51 | |
| 52 | xpos = fontWidth; ypos += 2 * lineHeight; |
| 53 | mySlider = new SliderWidget(this, font, xpos, ypos, lwidth, lineHeight, "" , 0, 0); |
| 54 | mySlider->setMinValue(1); |
| 55 | mySlider->setMaxValue(100); |
| 56 | |
| 57 | open(); |
| 58 | } |
| 59 | |
| 60 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 61 | void ProgressDialog::setMessage(const string& message) |
| 62 | { |
| 63 | myMessage->setLabel(message); |
| 64 | } |
| 65 | |
| 66 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 67 | void ProgressDialog::setRange(int start, int finish, int step) |
| 68 | { |
| 69 | myStart = start; |
| 70 | myFinish = finish; |
| 71 | myStep = int((step / 100.0) * (myFinish - myStart + 1)); |
| 72 | |
| 73 | mySlider->setMinValue(myStart); |
| 74 | mySlider->setMaxValue(myFinish); |
| 75 | } |
| 76 | |
| 77 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 78 | void ProgressDialog::setProgress(int progress) |
| 79 | { |
| 80 | // Only increase the progress bar if we have arrived at a new step |
| 81 | if(progress - mySlider->getValue() > myStep) |
| 82 | { |
| 83 | mySlider->setValue(progress); |
| 84 | |
| 85 | // Since this dialog is usually called in a tight loop that doesn't |
| 86 | // yield, we need to manually tell the framebuffer that a redraw is |
| 87 | // necessary |
| 88 | // This isn't really an ideal solution, since all redrawing and |
| 89 | // event handling is suspended until the dialog is closed |
| 90 | instance().frameBuffer().update(); |
| 91 | } |
| 92 | } |
| 93 | |