File: Progress.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (137 lines) | stat: -rw-r--r-- 2,986 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "stdafx.h"
#include "Progress.h"
#include "Container.h"
#include "Win32Dpi.h"

namespace gui {

	Progress::Progress() : myProgress(0.0f), waitMode(false), timerId(0) {}

#ifdef GUI_WIN32

	static const DWORD marqueeTime = 16;
	static const WORD progressMax = 0xFFFF;

	bool Progress::create(ContainerBase *parent, nat id) {
		DWORD flags = childFlags;

		if (waitMode)
			flags |= PBS_MARQUEE;

		if (!createEx(PROGRESS_CLASS, flags, 0, parent->handle().hwnd(), id))
			return false;

		if (waitMode) {
			SendMessage(handle().hwnd(), PBM_SETMARQUEE, 1, marqueeTime);
		} else {
			SendMessage(handle().hwnd(), PBM_SETRANGE32, 0, progressMax);
			SendMessage(handle().hwnd(), PBM_SETPOS, Nat(progressMax * myProgress), 0);
		}

		return true;
	}

	void Progress::windowDestroyed() {}

	Size Progress::minSize() {
		return dpiFromPx(currentDpi(), Size(200, 20));
	}

	void Progress::progress(Float v) {
		myProgress = v;

		if (created()) {
			if (waitMode) {
				SendMessage(handle().hwnd(), PBM_SETMARQUEE, 0, 0);
				SetWindowLong(handle().hwnd(), GWL_STYLE, childFlags);
				SendMessage(handle().hwnd(), PBM_SETRANGE32, 0, progressMax);
			}
			SendMessage(handle().hwnd(), PBM_SETPOS, Nat(progressMax * myProgress), 0);
		}

		waitMode = false;
	}

	void Progress::wait() {

		if (created()) {
			if (!waitMode) {
				SetWindowLong(handle().hwnd(), GWL_STYLE, childFlags | PBS_MARQUEE);
				SendMessage(handle().hwnd(), PBM_SETMARQUEE, 1, marqueeTime);
			}
		}

		waitMode = true;
	}

#endif
#ifdef GUI_GTK

	bool Progress::create(ContainerBase *parent, nat id) {
		GtkWidget *widget = gtk_progress_bar_new();
		initWidget(parent, widget);

		if (waitMode) {
			wait();
		} else {
			gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widget), myProgress);
		}

		return true;
	}

	void Progress::windowDestroyed() {
		if (timerId) {
			if (GSource *s = g_main_context_find_source_by_id(NULL, timerId))
				g_source_destroy(s);
			timerId = 0;
		}
	}

	Size Progress::minSize() {
		gint w = 0, h = 0;

		if (created()) {
			gtk_widget_get_preferred_width(handle().widget(), &w, NULL);
			gtk_widget_get_preferred_height(handle().widget(), &h, NULL);
		}

		return Size(Float(w), Float(h));
	}

	void Progress::progress(Float v) {
		myProgress = v;
		waitMode = false;

		if (created()) {
			if (timerId) {
				if (GSource *s = g_main_context_find_source_by_id(NULL, timerId))
					g_source_destroy(s);
				timerId = 0;
			}

			gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(handle().widget()), myProgress);
		}
	}

	static gboolean progressTimeout(gpointer widget) {
		gtk_progress_bar_pulse(GTK_PROGRESS_BAR(widget));
		return G_SOURCE_CONTINUE;
	}

	void Progress::wait() {
		waitMode = true;

		if (created()) {
			gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(handle().widget()), 0.02);
			gtk_progress_bar_pulse(GTK_PROGRESS_BAR(handle().widget()));

			if (timerId == 0) {
				timerId = g_timeout_add(16, &progressTimeout, handle().widget());
			}
		}
	}

#endif

}