forked from thefossilrecord/beebem-rs97
-
Notifications
You must be signed in to change notification settings - Fork 5
/
progressdialog.cc
176 lines (131 loc) · 4.24 KB
/
progressdialog.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "progressdialog.h"
static SDL_Rect CalcRectCentered(SDL_Surface *s, int width, int height)
{
SDL_Rect tmp;
tmp.x = (320-width)/2;
tmp.y = (240-height)/2;
tmp.w = width;
tmp.h = height;
return(tmp);
}
/* The dialog is shown in a separate thread.
*
* (EG is not thread safe, so don't try and show two dialogs!)
*/
static void* DisplayProcess(void *v_p)
{
SDL_Event event;
ProgressDialog *pd_p = (ProgressDialog*) v_p;
if (pd_p == NULL)
return 0;
pthread_mutex_lock(&pd_p->displayed_mutex);
pd_p->displayed=1;
pthread_mutex_unlock(&pd_p->displayed_mutex);
while(! EG_Window_IsHidden(pd_p->window_p)) {
EG_Graphics_Lock(EG_Window_GetSurface(pd_p->window_p));
while ( SDL_PollEvent(&event) ) {
EG_Window_ProcessEvent(pd_p->window_p, &event, 0, 0);
if (event.type == SDL_QUIT)
EG_Window_Hide(pd_p->window_p);
}
SDL_Delay(40);
EG_Window_ProcessEvent(pd_p->window_p, NULL, 0, 0);
EG_Graphics_Unlock(EG_Window_GetSurface(pd_p->window_p));
SDL_UpdateRect(EG_Window_GetSurface(pd_p->window_p), 0,0,0,0);
}
pthread_mutex_lock(&pd_p->displayed_mutex);
pd_p->displayed=0;
pthread_mutex_unlock(&pd_p->displayed_mutex);
return 0;
}
//static void DisplayDialog(ProgressDialog *pd_p)
//{
//}
ProgressDialog* ProgressDialog_Create(SDL_Surface *surface_p, const char *caption_p, float position)
{
ProgressDialog *pd_p;
SDL_Rect win, r;
SDL_Color col = DARKER_MENU_COLORS;
SDL_Color col2 = EG_COLOR_NORMAL;
char default_caption[]="Progress:";
if ( (pd_p=(ProgressDialog*)EG_Malloc( sizeof(ProgressDialog) ) ) == NULL) {
fprintf(stderr, "ProgressDialog: Unable to malloc struct.\n");
return NULL;
}
win = CalcRectCentered(surface_p, 200, 30);
pd_p->window_p = EG_Window_Create("win_progressdialog", surface_p, col, win);
r = win; r.x+=4; r.w-=8;
if (caption_p == NULL || strlen(caption_p) == 0)
caption_p = default_caption;
pd_p->caption_p = EG_Label_Create("progressdialog_title", EG_COLOR_ALT, EG_LABEL_ALIGN_CENTER, caption_p, C_1_1(r, 1, 2));
EG_Window_AddWidget(pd_p->window_p, pd_p->caption_p);
pd_p->progress_bar_p = EG_ProgressBar_Create("progressdialog_bar", col, col2, C_1_1(r, 3, 2));
EG_Window_AddWidget(pd_p->window_p, pd_p->progress_bar_p);
if (position == -1.0)
EG_ProgressBar_PositionIsUnknown(pd_p->progress_bar_p);
pd_p->displayed_mutex = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
pd_p->displayed = 0;
return pd_p;
}
void ProgressDialog_SetCaption(ProgressDialog *pd_p, const char* caption_p)
{
if (pd_p == NULL)
return;
EG_Label_SetCaption(pd_p->caption_p, caption_p);
}
void ProgressDialog_SetPosition(ProgressDialog *pd_p, float position)
{
if (pd_p == NULL)
return;
EG_ProgressBar_SetPosition(pd_p->progress_bar_p, position);
}
float ProgressDialog_GetPosition(ProgressDialog *pd_p)
{
if (pd_p == NULL)
return 0.0;
return EG_ProgressBar_GetPosition(pd_p->progress_bar_p);
}
void ProgressDialog_Show(ProgressDialog *pd_p)
{
if (pd_p == NULL)
return;
if (EG_ProgressBar_PositionIsUnknown(pd_p->progress_bar_p)) {
EG_ProgressBar_SetPosition(pd_p->progress_bar_p, 0.0);
EG_ProgressBar_PositionIsUnknown(pd_p->progress_bar_p);
}
if (EG_Window_IsHidden(pd_p->window_p))
EG_Window_Show(pd_p->window_p);
pthread_mutex_lock(&pd_p->displayed_mutex);
if (pd_p->displayed == 0) {
if (pthread_create(&pd_p->thread_p, NULL, DisplayProcess, (void*) pd_p) != 0) {
fprintf(stderr, "ProgressDialog: Unable to create thread (pthread_create).\n");
}
}
pthread_mutex_unlock(&pd_p->displayed_mutex);
}
void ProgressDialog_Hide(ProgressDialog *pd_p)
{
if (pd_p == NULL)
return;
// This will cause the other threads loop to end.
if (! EG_Window_IsHidden(pd_p->window_p))
EG_Window_Hide(pd_p->window_p);
}
void ProgressDialog_Free(ProgressDialog *pd_p)
{
int d;
if (pd_p == NULL)
return;
if (! EG_Window_IsHidden(pd_p->window_p))
ProgressDialog_Hide(pd_p);
// Block until other thread ends.
d=1; while (d) {
pthread_mutex_lock(&pd_p->displayed_mutex);
d = pd_p->displayed;
pthread_mutex_unlock(&pd_p->displayed_mutex);
}
// Now can safely dump resources
EG_Window_DestroyAllChildWidgets(pd_p->window_p);
EG_Window_Destroy(pd_p->window_p);
EG_Free(pd_p);
}