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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/gtk/download_item_gtk.h"
#include "app/gfx/chrome_canvas.h"
#include "app/gfx/chrome_font.h"
#include "app/gfx/text_elider.h"
#include "app/slide_animation.h"
#include "base/basictypes.h"
#include "base/string_util.h"
#include "base/time.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/gtk/download_shelf_gtk.h"
#include "chrome/browser/gtk/menu_gtk.h"
#include "chrome/browser/gtk/nine_box.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
namespace {
// The width of the |menu_button_| widget. It has to be at least as wide as the
// bitmap that we use to draw it, i.e. 16, but can be more.
const int kMenuButtonWidth = 16;
// Amount of space we allot to showing the filename. If the filename is too wide
// it will be elided.
const int kTextWidth = 140;
// The minimum width we will ever draw the download item. Used as a lower bound
// during animation. This number comes from the width of the images used to
// make the download item.
const int kMinDownloadItemWidth = 13 + download_util::kSmallProgressIconSize;
const char* kLabelColorMarkup = "<span color='#%s'>%s</span>";
const char* kFilenameColor = "576C95"; // 87, 108, 149
const char* kStatusColor = "7B8DAE"; // 123, 141, 174
// New download item animation speed in milliseconds.
const int kNewItemAnimationDurationMs = 800;
// How long the 'download complete' animation should last for.
const int kCompleteAnimationDurationMs = 2500;
} // namespace
// DownloadShelfContextMenuGtk -------------------------------------------------
class DownloadShelfContextMenuGtk : public DownloadShelfContextMenu,
public MenuGtk::Delegate {
public:
// The constructor creates the menu and immediately pops it up.
// |model| is the download item model associated with this context menu,
// |widget| is the button that popped up this context menu, and |e| is
// the button press event that caused this menu to be created.
DownloadShelfContextMenuGtk(BaseDownloadItemModel* model)
: DownloadShelfContextMenu(model),
menu_is_for_complete_download_(false) {
}
~DownloadShelfContextMenuGtk() {
}
void Popup(GtkWidget* widget, GdkEvent* event) {
// Create the menu if we have not created it yet or we created it for
// an in-progress download that has since completed.
bool download_is_complete = download_->state() == DownloadItem::COMPLETE;
if (menu_.get() == NULL ||
(download_is_complete && !menu_is_for_complete_download_)) {
menu_.reset(new MenuGtk(this, download_is_complete ?
finished_download_menu : in_progress_download_menu, NULL));
menu_is_for_complete_download_ = download_is_complete;
}
menu_->Popup(widget, event);
}
// MenuGtk::Delegate implementation ------------------------------------------
virtual bool IsCommandEnabled(int id) const {
return IsItemCommandEnabled(id);
}
virtual bool IsItemChecked(int id) const {
return ItemIsChecked(id);
}
virtual void ExecuteCommand(int id) {
return ExecuteItemCommand(id);
}
private:
// The menu we show on Popup(). We keep a pointer to it for a couple reasons:
// * we don't want to have to recreate the menu every time it's popped up.
// * we have to keep it in scope for longer than the duration of Popup(), or
// completing the user-selected action races against the menu's
// destruction.
scoped_ptr<MenuGtk> menu_;
// If true, the MenuGtk in |menu_| refers to a finished download menu.
bool menu_is_for_complete_download_;
// We show slightly different menus if the download is in progress vs. if the
// download has finished.
static MenuCreateMaterial in_progress_download_menu[];
static MenuCreateMaterial finished_download_menu[];
};
MenuCreateMaterial DownloadShelfContextMenuGtk::finished_download_menu[] = {
{ MENU_NORMAL, OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN, 0, NULL },
{ MENU_CHECKBOX, ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE,
0, NULL},
{ MENU_SEPARATOR, 0, 0, 0, NULL },
{ MENU_NORMAL, SHOW_IN_FOLDER, IDS_DOWNLOAD_LINK_SHOW, 0, NULL},
{ MENU_SEPARATOR, 0, 0, 0, NULL },
{ MENU_NORMAL, CANCEL, IDS_DOWNLOAD_MENU_CANCEL, 0, NULL},
{ MENU_END, 0, 0, 0, NULL },
};
MenuCreateMaterial DownloadShelfContextMenuGtk::in_progress_download_menu[] = {
{ MENU_CHECKBOX, OPEN_WHEN_COMPLETE, IDS_DOWNLOAD_MENU_OPEN_WHEN_COMPLETE,
0, NULL },
{ MENU_CHECKBOX, ALWAYS_OPEN_TYPE, IDS_DOWNLOAD_MENU_ALWAYS_OPEN_TYPE,
0, NULL},
{ MENU_SEPARATOR, 0, 0, 0, NULL },
{ MENU_NORMAL, SHOW_IN_FOLDER, IDS_DOWNLOAD_LINK_SHOW, 0, NULL},
{ MENU_SEPARATOR, 0, 0, 0, NULL },
{ MENU_NORMAL, CANCEL, IDS_DOWNLOAD_MENU_CANCEL, 0, NULL},
{ MENU_END, 0, 0, 0, NULL },
};
// DownloadItemGtk -------------------------------------------------------------
NineBox* DownloadItemGtk::body_nine_box_normal_ = NULL;
NineBox* DownloadItemGtk::body_nine_box_prelight_ = NULL;
NineBox* DownloadItemGtk::body_nine_box_active_ = NULL;
NineBox* DownloadItemGtk::menu_nine_box_normal_ = NULL;
NineBox* DownloadItemGtk::menu_nine_box_prelight_ = NULL;
NineBox* DownloadItemGtk::menu_nine_box_active_ = NULL;
DownloadItemGtk::DownloadItemGtk(DownloadShelfGtk* parent_shelf,
BaseDownloadItemModel* download_model)
: parent_shelf_(parent_shelf),
progress_angle_(download_util::kStartAngleDegrees),
download_model_(download_model),
bounding_widget_(parent_shelf->GetRightBoundingWidget()) {
InitNineBoxes();
body_ = gtk_button_new();
gtk_widget_set_app_paintable(body_, TRUE);
g_signal_connect(body_, "expose-event",
G_CALLBACK(OnExpose), this);
GTK_WIDGET_UNSET_FLAGS(body_, GTK_CAN_FOCUS);
// Remove internal padding on the button.
GtkRcStyle* no_padding_style = gtk_rc_style_new();
no_padding_style->xthickness = 0;
no_padding_style->ythickness = 0;
gtk_widget_modify_style(body_, no_padding_style);
g_object_unref(no_padding_style);
name_label_ = gtk_label_new(NULL);
// TODO(estade): This is at best an educated guess, since we don't actually
// use gfx::Font() to draw the text. This is why we need to add so
// much padding when we set the size request. We need to either use gfx::Font
// or somehow extend TextElider.
std::wstring elided_filename = gfx::ElideFilename(
download_model_->download()->GetFileName(),
gfx::Font(), kTextWidth);
gchar* label_markup =
g_markup_printf_escaped(kLabelColorMarkup, kFilenameColor,
WideToUTF8(elided_filename).c_str());
gtk_label_set_markup(GTK_LABEL(name_label_), label_markup);
g_free(label_markup);
status_label_ = gtk_label_new(NULL);
// Left align and vertically center the labels.
gtk_misc_set_alignment(GTK_MISC(name_label_), 0, 0.5);
gtk_misc_set_alignment(GTK_MISC(status_label_), 0, 0.5);
// Stack the labels on top of one another.
GtkWidget* text_stack = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(text_stack), name_label_, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(text_stack), status_label_, FALSE, FALSE, 0);
// We use a GtkFixed because we don't want it to have its own window.
// This choice of widget is not critically important though.
progress_area_ = gtk_fixed_new();
gtk_widget_set_size_request(progress_area_,
download_util::kSmallProgressIconSize,
download_util::kSmallProgressIconSize);
gtk_widget_set_app_paintable(progress_area_, TRUE);
g_signal_connect(progress_area_, "expose-event",
G_CALLBACK(OnProgressAreaExpose), this);
// Put the download progress icon on the left of the labels.
GtkWidget* body_hbox = gtk_hbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(body_), body_hbox);
gtk_box_pack_start(GTK_BOX(body_hbox), progress_area_, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(body_hbox), text_stack, TRUE, TRUE, 0);
menu_button_ = gtk_button_new();
gtk_widget_set_app_paintable(menu_button_, TRUE);
GTK_WIDGET_UNSET_FLAGS(menu_button_, GTK_CAN_FOCUS);
g_signal_connect(menu_button_, "expose-event",
G_CALLBACK(OnExpose), this);
g_signal_connect(menu_button_, "button-press-event",
G_CALLBACK(OnMenuButtonPressEvent), this);
g_object_set_data(G_OBJECT(menu_button_), "left-align-popup",
reinterpret_cast<void*>(true));
gtk_widget_set_size_request(menu_button_, kMenuButtonWidth, 0);
GtkWidget* shelf_hbox = parent_shelf->GetHBox();
hbox_ = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox_), body_, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox_), menu_button_, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(shelf_hbox), hbox_, FALSE, FALSE, 0);
// Insert as the leftmost item.
gtk_box_reorder_child(GTK_BOX(shelf_hbox), hbox_, 1);
resize_handler_id_ = g_signal_connect(G_OBJECT(shelf_hbox), "size-allocate",
G_CALLBACK(OnShelfResized), this);
download_model_->download()->AddObserver(this);
new_item_animation_.reset(new SlideAnimation(this));
new_item_animation_->SetSlideDuration(kNewItemAnimationDurationMs);
gtk_widget_show_all(hbox_);
new_item_animation_->Show();
}
DownloadItemGtk::~DownloadItemGtk() {
StopDownloadProgress();
g_signal_handler_disconnect(parent_shelf_->GetHBox(), resize_handler_id_);
gtk_widget_destroy(hbox_);
download_model_->download()->RemoveObserver(this);
}
void DownloadItemGtk::OnDownloadUpdated(DownloadItem* download) {
DCHECK_EQ(download, download_model_->download());
switch (download->state()) {
case DownloadItem::REMOVING:
parent_shelf_->RemoveDownloadItem(this); // This will delete us!
return;
case DownloadItem::CANCELLED:
StopDownloadProgress();
break;
case DownloadItem::COMPLETE:
StopDownloadProgress();
complete_animation_.reset(new SlideAnimation(this));
complete_animation_->SetSlideDuration(kCompleteAnimationDurationMs);
complete_animation_->SetTweenType(SlideAnimation::NONE);
complete_animation_->Show();
break;
case DownloadItem::IN_PROGRESS:
download_model_->download()->is_paused() ?
StopDownloadProgress() : StartDownloadProgress();
break;
default:
NOTREACHED();
}
// Now update the status label. We may have already removed it; if so, we
// do nothing.
if (!status_label_) {
return;
}
std::wstring status_text = download_model_->GetStatusText();
// Remove the status text label.
if (status_text.empty()) {
gtk_widget_destroy(status_label_);
status_label_ = NULL;
return;
}
gchar* label_markup =
g_markup_printf_escaped(kLabelColorMarkup, kStatusColor,
WideToUTF8(status_text).c_str());
gtk_label_set_markup(GTK_LABEL(status_label_), label_markup);
g_free(label_markup);
}
void DownloadItemGtk::AnimationProgressed(const Animation* animation) {
if (animation == complete_animation_.get()) {
gtk_widget_queue_draw(progress_area_);
} else {
DCHECK(animation == new_item_animation_.get());
// See above TODO for explanation of the extra 50.
int showing_width = std::max(kMinDownloadItemWidth,
static_cast<int>((kTextWidth + 50 +
download_util::kSmallProgressIconSize) *
new_item_animation_->GetCurrentValue()));
showing_width = std::max(showing_width, kMinDownloadItemWidth);
gtk_widget_set_size_request(body_, showing_width, -1);
}
}
// Download progress animation functions.
void DownloadItemGtk::UpdateDownloadProgress() {
progress_angle_ = (progress_angle_ +
download_util::kUnknownIncrementDegrees) %
download_util::kMaxDegrees;
gtk_widget_queue_draw(progress_area_);
}
void DownloadItemGtk::StartDownloadProgress() {
if (progress_timer_.IsRunning())
return;
progress_timer_.Start(
base::TimeDelta::FromMilliseconds(download_util::kProgressRateMs), this,
&DownloadItemGtk::UpdateDownloadProgress);
}
void DownloadItemGtk::StopDownloadProgress() {
progress_timer_.Stop();
}
// static
void DownloadItemGtk::InitNineBoxes() {
if (body_nine_box_normal_)
return;
body_nine_box_normal_ = new NineBox(
IDR_DOWNLOAD_BUTTON_LEFT_TOP,
IDR_DOWNLOAD_BUTTON_CENTER_TOP,
IDR_DOWNLOAD_BUTTON_RIGHT_TOP,
IDR_DOWNLOAD_BUTTON_LEFT_MIDDLE,
IDR_DOWNLOAD_BUTTON_CENTER_MIDDLE,
IDR_DOWNLOAD_BUTTON_RIGHT_MIDDLE,
IDR_DOWNLOAD_BUTTON_LEFT_BOTTOM,
IDR_DOWNLOAD_BUTTON_CENTER_BOTTOM,
IDR_DOWNLOAD_BUTTON_RIGHT_BOTTOM);
body_nine_box_prelight_ = new NineBox(
IDR_DOWNLOAD_BUTTON_LEFT_TOP_H,
IDR_DOWNLOAD_BUTTON_CENTER_TOP_H,
IDR_DOWNLOAD_BUTTON_RIGHT_TOP_H,
IDR_DOWNLOAD_BUTTON_LEFT_MIDDLE_H,
IDR_DOWNLOAD_BUTTON_CENTER_MIDDLE_H,
IDR_DOWNLOAD_BUTTON_RIGHT_MIDDLE_H,
IDR_DOWNLOAD_BUTTON_LEFT_BOTTOM_H,
IDR_DOWNLOAD_BUTTON_CENTER_BOTTOM_H,
IDR_DOWNLOAD_BUTTON_RIGHT_BOTTOM_H);
body_nine_box_active_ = new NineBox(
IDR_DOWNLOAD_BUTTON_LEFT_TOP_P,
IDR_DOWNLOAD_BUTTON_CENTER_TOP_P,
IDR_DOWNLOAD_BUTTON_RIGHT_TOP_P,
IDR_DOWNLOAD_BUTTON_LEFT_MIDDLE_P,
IDR_DOWNLOAD_BUTTON_CENTER_MIDDLE_P,
IDR_DOWNLOAD_BUTTON_RIGHT_MIDDLE_P,
IDR_DOWNLOAD_BUTTON_LEFT_BOTTOM_P,
IDR_DOWNLOAD_BUTTON_CENTER_BOTTOM_P,
IDR_DOWNLOAD_BUTTON_RIGHT_BOTTOM_P);
menu_nine_box_normal_ = new NineBox(
IDR_DOWNLOAD_BUTTON_MENU_TOP, 0, 0,
IDR_DOWNLOAD_BUTTON_MENU_MIDDLE, 0, 0,
IDR_DOWNLOAD_BUTTON_MENU_BOTTOM, 0, 0);
menu_nine_box_prelight_ = new NineBox(
IDR_DOWNLOAD_BUTTON_MENU_TOP_H, 0, 0,
IDR_DOWNLOAD_BUTTON_MENU_MIDDLE_H, 0, 0,
IDR_DOWNLOAD_BUTTON_MENU_BOTTOM_H, 0, 0);
menu_nine_box_active_ = new NineBox(
IDR_DOWNLOAD_BUTTON_MENU_TOP_P, 0, 0,
IDR_DOWNLOAD_BUTTON_MENU_MIDDLE_P, 0, 0,
IDR_DOWNLOAD_BUTTON_MENU_BOTTOM_P, 0, 0);
}
// static
gboolean DownloadItemGtk::OnExpose(GtkWidget* widget, GdkEventExpose* e,
DownloadItemGtk* download_item) {
NineBox* nine_box = NULL;
// If true, this widget is |body_|, otherwise it is |menu_button_|.
bool is_body = widget == download_item->body_;
if (GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
nine_box = is_body ? body_nine_box_prelight_ : menu_nine_box_prelight_;
else if (GTK_WIDGET_STATE(widget) == GTK_STATE_ACTIVE)
nine_box = is_body ? body_nine_box_active_ : menu_nine_box_active_;
else
nine_box = is_body ? body_nine_box_normal_ : menu_nine_box_normal_;
nine_box->RenderToWidget(widget);
GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget));
if (child)
gtk_container_propagate_expose(GTK_CONTAINER(widget), child, e);
return TRUE;
}
// static
gboolean DownloadItemGtk::OnProgressAreaExpose(GtkWidget* widget,
GdkEventExpose* event, DownloadItemGtk* download_item) {
// Create a transparent canvas.
ChromeCanvasPaint canvas(event, false);
if (download_item->complete_animation_.get()) {
if (download_item->complete_animation_->IsAnimating()) {
download_util::PaintDownloadComplete(&canvas,
widget->allocation.x, widget->allocation.y,
download_item->complete_animation_->GetCurrentValue(),
download_util::SMALL);
}
} else {
download_util::PaintDownloadProgress(&canvas,
widget->allocation.x, widget->allocation.y,
download_item->progress_angle_,
download_item->download_model_->download()->PercentComplete(),
download_util::SMALL);
}
// TODO(estade): paint download icon.
return TRUE;
}
// static
gboolean DownloadItemGtk::OnMenuButtonPressEvent(GtkWidget* button,
GdkEvent* event,
DownloadItemGtk* item) {
// Stop any completion animation.
if (item->complete_animation_.get() &&
item->complete_animation_->IsAnimating()) {
item->complete_animation_->End();
}
item->complete_animation_->End();
// TODO(port): this never puts the button into the "active" state,
// so this may need to be changed. See note in BrowserToolbarGtk.
if (event->type == GDK_BUTTON_PRESS) {
GdkEventButton* event_button = reinterpret_cast<GdkEventButton*>(event);
if (event_button->button == 1) {
if (item->menu_.get() == NULL) {
item->menu_.reset(new DownloadShelfContextMenuGtk(
item->download_model_.get()));
}
item->menu_->Popup(button, event);
}
}
return FALSE;
}
// static
void DownloadItemGtk::OnShelfResized(GtkWidget *widget,
GtkAllocation *allocation,
DownloadItemGtk* item) {
if (item->hbox_->allocation.x + item->hbox_->allocation.width >
item->bounding_widget_->allocation.x)
gtk_widget_hide(item->hbox_);
else
gtk_widget_show(item->hbox_);
}
|