license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | // Download utilities. |
| 6 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 7 | #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_ |
| 8 | #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | |
| 10 | #include <objidl.h> |
| 11 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 12 | #include <set> |
| 13 | #include <string> |
| 14 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/basictypes.h" |
| 16 | #include "base/task.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "chrome/views/event.h" |
| 18 | #include "chrome/views/menu.h" |
| 19 | #include "chrome/views/view.h" |
| 20 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 21 | class BaseDownloadItemModel; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | class DownloadItem; |
| 23 | class SkBitmap; |
| 24 | |
| 25 | namespace download_util { |
| 26 | |
| 27 | // DownloadContextMenu --------------------------------------------------------- |
| 28 | |
| 29 | // The base class of context menus that provides the various commands. |
| 30 | // Subclasses are responsible for creating and running the menu. |
| 31 | class BaseContextMenu : public Menu::Delegate { |
| 32 | public: |
| 33 | explicit BaseContextMenu(DownloadItem* download); |
| 34 | virtual ~BaseContextMenu(); |
| 35 | |
| 36 | enum ContextMenuCommands { |
| 37 | SHOW_IN_FOLDER = 1, // Open an Explorer window with the item highlighted |
| 38 | COPY_LINK, // Copy the download's URL to the clipboard |
| 39 | COPY_PATH, // Copy the download's full path to the clipboard |
| 40 | COPY_FILE, // Copy the downloaded file to the clipboard |
| 41 | OPEN_WHEN_COMPLETE, // Open the download when it's finished |
| 42 | ALWAYS_OPEN_TYPE, // Default this file extension to always open |
| 43 | REMOVE_ITEM, // Remove the download |
| 44 | CANCEL, // Cancel the download |
| 45 | MENU_LAST |
| 46 | }; |
| 47 | |
| 48 | // Menu::Delegate interface |
| 49 | virtual bool IsItemChecked(int id) const; |
| 50 | virtual bool IsItemDefault(int id) const; |
| 51 | virtual std::wstring GetLabel(int id) const; |
| 52 | virtual bool SupportsCommand(int id) const; |
| 53 | virtual bool IsCommandEnabled(int id) const; |
| 54 | virtual void ExecuteCommand(int id); |
| 55 | |
| 56 | protected: |
| 57 | // Information source. |
| 58 | DownloadItem* download_; |
| 59 | |
| 60 | private: |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(BaseContextMenu); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // Menu for the download shelf. |
| 65 | class DownloadShelfContextMenu : public BaseContextMenu { |
| 66 | public: |
| 67 | DownloadShelfContextMenu(DownloadItem* download, |
| 68 | HWND window, |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 69 | BaseDownloadItemModel* model, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | const CPoint& point); |
| 71 | virtual ~DownloadShelfContextMenu(); |
| 72 | |
| 73 | virtual bool IsItemDefault(int id) const; |
| 74 | virtual void ExecuteCommand(int id); |
| 75 | |
| 76 | private: |
| 77 | // A model to control the cancel behavior. |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 78 | BaseDownloadItemModel* model_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 80 | DISALLOW_COPY_AND_ASSIGN(DownloadShelfContextMenu); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Menu for the download destination view. |
| 84 | class DownloadDestinationContextMenu : public BaseContextMenu { |
| 85 | public: |
| 86 | DownloadDestinationContextMenu(DownloadItem* download, |
| 87 | HWND window, |
| 88 | const CPoint& point); |
| 89 | virtual ~DownloadDestinationContextMenu(); |
| 90 | |
| 91 | private: |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 92 | DISALLOW_COPY_AND_ASSIGN(DownloadDestinationContextMenu); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // DownloadProgressTask -------------------------------------------------------- |
| 96 | |
| 97 | // A class for managing the timed progress animations for a download view. The |
| 98 | // view must implement an UpdateDownloadProgress() method. |
| 99 | template<class DownloadView> |
| 100 | class DownloadProgressTask : public Task { |
| 101 | public: |
| 102 | DownloadProgressTask(DownloadView* view) : view_(view) {} |
| 103 | virtual ~DownloadProgressTask() {} |
| 104 | virtual void Run() { |
| 105 | view_->UpdateDownloadProgress(); |
| 106 | } |
| 107 | private: |
| 108 | DownloadView* view_; |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 109 | DISALLOW_COPY_AND_ASSIGN(DownloadProgressTask); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | // Download opening ------------------------------------------------------------ |
| 113 | |
| 114 | // Whether it is OK to open this download. |
| 115 | bool CanOpenDownload(DownloadItem* download); |
| 116 | |
| 117 | // Open the file associated with this download (wait for the download to |
| 118 | // complete if it is in progress). |
| 119 | void OpenDownload(DownloadItem* download); |
| 120 | |
| 121 | // Download progress animations ------------------------------------------------ |
| 122 | |
| 123 | // Arc sweep angle for use with downloads of unknown size |
| 124 | const int kUnknownAngleDegrees = 50; |
| 125 | |
| 126 | // Rate of progress for use with downloads of unknown size |
| 127 | const int kUnknownIncrementDegrees = 12; |
| 128 | |
| 129 | // Start angle for downloads with known size (midnight position) |
| 130 | const int kStartAngleDegrees = -90; |
| 131 | |
| 132 | // A circle |
| 133 | const int kMaxDegrees = 360; |
| 134 | |
| 135 | // Progress animation timer period, in milliseconds. |
| 136 | const int kProgressRateMs = 150; |
| 137 | |
[email protected] | 5db2a6e | 2008-09-12 21:20:04 | [diff] [blame] | 138 | // XP and Vista must support icons of this size. |
| 139 | const int kSmallIconSize = 16; |
| 140 | const int kBigIconSize = 32; |
| 141 | |
[email protected] | 454dcb8 | 2008-10-31 21:11:41 | [diff] [blame] | 142 | // Our progress halo around the icon. |
| 143 | int GetBigProgressIconSize(); |
| 144 | |
[email protected] | 5db2a6e | 2008-09-12 21:20:04 | [diff] [blame] | 145 | const int kSmallProgressIconSize = 39; |
| 146 | const int kBigProgressIconSize = 52; |
| 147 | |
| 148 | // The offset required to center the icon in the progress bitmaps. |
[email protected] | 454dcb8 | 2008-10-31 21:11:41 | [diff] [blame] | 149 | int GetBigProgressIconOffset(); |
| 150 | |
[email protected] | 5db2a6e | 2008-09-12 21:20:04 | [diff] [blame] | 151 | const int kSmallProgressIconOffset = |
| 152 | (kSmallProgressIconSize - kSmallIconSize) / 2; |
[email protected] | 5db2a6e | 2008-09-12 21:20:04 | [diff] [blame] | 153 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | enum PaintDownloadProgressSize { |
| 155 | SMALL = 0, |
| 156 | BIG |
| 157 | }; |
| 158 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | // Paint the common download animation progress foreground and background, |
| 160 | // clipping the foreground to 'percent' full. If percent is -1, then we don't |
| 161 | // know the total size, so we just draw a rotating segment until we're done. |
| 162 | // |
| 163 | // |containing_view| is the View subclass within which the progress animation |
| 164 | // is drawn (generally either DownloadItemTabView or DownloadItemView). We |
| 165 | // require the containing View in addition to the canvas because if we are |
| 166 | // drawing in a right-to-left locale, we need to mirror the position of the |
| 167 | // progress animation within the containing View. |
| 168 | void PaintDownloadProgress(ChromeCanvas* canvas, |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 169 | views::View* containing_view, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | int origin_x, |
| 171 | int origin_y, |
| 172 | int start_angle, |
| 173 | int percent, |
| 174 | PaintDownloadProgressSize size); |
| 175 | |
| 176 | void PaintDownloadComplete(ChromeCanvas* canvas, |
[email protected] | c2dacc9 | 2008-10-16 23:51:38 | [diff] [blame] | 177 | views::View* containing_view, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | int origin_x, |
| 179 | int origin_y, |
| 180 | double animation_progress, |
| 181 | PaintDownloadProgressSize size); |
| 182 | |
| 183 | // Drag support ---------------------------------------------------------------- |
| 184 | |
| 185 | // Helper function for download views to use when acting as a drag source for a |
| 186 | // DownloadItem. If 'icon' is NULL, no image will be accompany the drag. |
| 187 | void DragDownload(const DownloadItem* download, SkBitmap* icon); |
| 188 | |
| 189 | // Executable file support ----------------------------------------------------- |
| 190 | |
| 191 | // Copy all executable file extensions. |
[email protected] | 64da0b93 | 2009-02-24 02:30:04 | [diff] [blame^] | 192 | void InitializeExeTypes(std::set<std::string>* exe_extensions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
| 194 | } // namespace download_util |
| 195 | |
| 196 | |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 197 | #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_ |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 198 | |