[patch] downloads dpi progress bar
123 <[email protected]>
| Newsgroups | gmane.comp.web.dillo.devel |
|---|---|
| Message-ID | <[email protected]> |
Now progress bar is implemented as custom widget on top of Fl_Widget class. But FLTK already has Fl_Progress class for the same thing. Progress bar looks the same with two exceptions: 1. No tooltip: it is not displayed for Fl_Progress even when set. Normally progress bars, labels and other widgets for status information don't have tooltips so I think it is ok. 2. When downloading file without Content-Length, progress bar is always empty. With current implementation it changes for one percent on every update and remains somewhere in the middle when download is finished. For me keeping it at zero looks better. Patch and python HTTP server for testing downloads without Content-Length attached. _______________________________________________ Dillo-dev mailing list [email protected] http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/dillo-dev
downloads.diff
(text/plain, 5.7 KB)
diff -r 61a08f777e5a dpi/downloads.cc
--- a/dpi/downloads.cc Sun Jun 03 03:18:46 2012 +0400
+++ b/dpi/downloads.cc Sun Jun 03 05:22:32 2012 +0400
@@ -30,15 +30,16 @@
#include <sys/wait.h>
#include <FL/Fl.H>
-#include <FL/fl_draw.H>
-#include <FL/Fl_File_Chooser.H>
-#include <FL/Fl_Window.H>
-#include <FL/Fl_Widget.H>
-#include <FL/Fl_Group.H>
-#include <FL/Fl_Scroll.H>
-#include <FL/Fl_Pack.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
+#include <FL/Fl_File_Chooser.H>
+#include <FL/Fl_Group.H>
+#include <FL/Fl_Pack.H>
+#include <FL/Fl_Progress.H>
+#include <FL/Fl_Scroll.H>
+#include <FL/Fl_Widget.H>
+#include <FL/Fl_Window.H>
+#include <FL/fl_draw.H>
#include "dpiutil.h"
#include "../dpip/dpip.h"
@@ -64,39 +65,6 @@
* Class declarations
*/
-// ProgressBar widget --------------------------------------------------------
-
-class ProgressBar : public Fl_Box {
-protected:
- double mMin;
- double mMax;
- double mPresent;
- double mStep;
- bool mShowPct, mShowMsg;
- char mMsg[64];
- Fl_Color mTextColor;
- void draw();
-public:
- ProgressBar(int x, int y, int w, int h, const char *lbl = 0);
- void range(double min, double max, double step = 1) {
- mMin = min; mMax = max; mStep = step;
- };
- void step(double step) { mPresent += step; redraw(); };
- void move(double step);
- double minimum() { return mMin; }
- double maximum() { return mMax; }
- void minimum(double nm) { mMin = nm; };
- void maximum(double nm) { mMax = nm; };
- double position () { return mPresent; }
- double step() { return mStep; }
- void position(double pos) { mPresent = pos; redraw(); }
- void showtext(bool st) { mShowPct = st; }
- void message(char *msg) { mShowMsg = true; strncpy(mMsg,msg,63); redraw(); }
- bool showtext() { return mShowPct; }
- void text_color(Fl_Color col) { mTextColor = col; }
- Fl_Color text_color() { return mTextColor; }
-};
-
// Download-item class -------------------------------------------------------
class DLItem {
@@ -121,7 +89,7 @@
int gw, gh;
Fl_Group *group;
- ProgressBar *prBar;
+ Fl_Progress *prBar;
Fl_Button *prButton;
Fl_Widget *prTitle, *prGot, *prSize, *prRate, *pr_Rate, *prETA, *prETAt;
@@ -138,7 +106,7 @@
pid_t pid() { return mPid; }
void pid(pid_t p) { mPid = p; }
void child_finished(int status);
- void status_msg(const char *msg) { prBar->message((char*)msg); }
+ void status_msg(const char *msg) { prBar->label(msg); }
Fl_Widget *get_widget() { return group; }
int widget_done() { return WidgetDone; }
void widget_done(int val) { WidgetDone = val; }
@@ -227,61 +195,6 @@
// The download window object
static class DLWin *dl_win = NULL;
-
-
-// ProgressBar widget --------------------------------------------------------
-
-void ProgressBar::move(double step)
-{
- mPresent += step;
- if (mPresent > mMax)
- mPresent = mMin;
- redraw();
-}
-
-ProgressBar::ProgressBar(int x, int y, int w, int h, const char *lbl)
-: Fl_Box(x, y, w, h, lbl)
-{
- mMin = mPresent = 0;
- mMax = 100;
- mShowPct = true;
- mShowMsg = false;
- box(FL_THIN_UP_BOX);
- color(FL_WHITE);
-}
-
-void ProgressBar::draw()
-{
- struct Rectangle {
- int x, y, w, h;
- };
-
- //drawstyle(style(), flags());
- draw_box();
- Rectangle r = {x(), y(), w(), h()};
- if (mPresent > mMax)
- mPresent = mMax;
- if (mPresent < mMin)
- mPresent = mMin;
- double pct = (mPresent - mMin) / mMax;
-
- r.w = r.w * pct + .5;
- fl_rectf(r.x, r.y, r.w, r.h, FL_BLUE);
-
- if (mShowMsg) {
- fl_color(FL_RED);
- fl_font(this->labelfont(), this->labelsize());
- fl_draw(mMsg, x(), y(), w(), h(), FL_ALIGN_CENTER);
- } else if (mShowPct) {
- char buffer[30];
- sprintf(buffer, "%d%%", int (pct * 100 + .5));
- fl_color(FL_RED);
- fl_font(this->labelfont(), this->labelsize());
- fl_draw(buffer, x(), y(), w(), h(), FL_ALIGN_CENTER);
- }
-}
-
-
// Download-item class -------------------------------------------------------
static void prButton_scb(Fl_Widget *, void *cb_data)
@@ -364,9 +277,10 @@
log_text_add(fullname, strlen(fullname));
log_text_add("\n\n", 2);
- prBar = new ProgressBar(24, 40, 92, 20);
+ prBar = new Fl_Progress(24, 40, 92, 20);
+ prBar->color(FL_WHITE, FL_BLUE);
+ prBar->labelcolor(FL_RED);
prBar->box(FL_THIN_UP_BOX);
- prBar->tooltip("Progress Status");
int ix = 122, iy = 37, iw = 50, ih = 14;
Fl_Widget *o = new Fl_Box(ix,iy,iw,ih, "Got");
@@ -585,7 +499,7 @@
void DLItem::update_size(int new_sz)
{
- char buf[64];
+ static char buf[64];
if (curr_bytesize == 0 && new_sz) {
// Start the timer with the first bytes got
@@ -599,19 +513,19 @@
curr_bytesize = new_sz;
if (curr_bytesize > 1024 * 1024)
- snprintf(buf, 64, "%.1fMB", (float)curr_bytesize / (1024*1024));
+ snprintf(buf, sizeof buf, "%.1fMB", (float)curr_bytesize / (1024*1024));
else
- snprintf(buf, 64, "%.0fKB", (float)curr_bytesize / 1024);
+ snprintf(buf, sizeof buf, "%.0fKB", (float)curr_bytesize / 1024);
+ buf[63] = '\0';
prGot->copy_label(buf);
- if (total_bytesize == -1) {
- prBar->showtext(false);
- prBar->move(1);
- } else {
- prBar->showtext(true);
- double pos = 100.0;
- if (total_bytesize > 0)
- pos *= (double)curr_bytesize / total_bytesize;
- prBar->position(pos);
+ if (total_bytesize == -1)
+ prBar->label("Downloading");
+ else if (total_bytesize > 0) {
+ snprintf(buf, sizeof buf, "%d%%", curr_bytesize * 100 / total_bytesize);
+ buf[63] = '\0';
+ prBar->label(buf);
+ prBar->maximum(total_bytesize);
+ prBar->value(curr_bytesize);
}
}
httpserv.py
(text/x-python, 407 B)
from http.server import HTTPServer, BaseHTTPRequestHandler
import time
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'application/binary')
self.end_headers()
for i in range(10):
self.wfile.write(b"OK" * 1000)
time.sleep(1)
server_address = ('', 8000)
httpd = HTTPServer(server_address, MyHandler)
httpd.serve_forever()