Chromium Code Reviews| Index: chrome/browser/ui/views/color_chooser_dialog.cc |
| diff --git a/chrome/browser/ui/views/color_chooser_dialog.cc b/chrome/browser/ui/views/color_chooser_dialog.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..186a781b456a544529c882c06d01d648fbcf3029 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/color_chooser_dialog.cc |
| @@ -0,0 +1,91 @@ |
| +// Copyright (c) 2012 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/ui/views/color_chooser_dialog.h" |
| + |
| +#include <commdlg.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop.h" |
| +#include "base/threading/thread.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "skia/ext/skia_utils_win.h" |
| + |
| +using content::BrowserThread; |
| + |
| +static const COLORREF kColorBlack = 0x000000; |
|
Peter Kasting
2012/03/01 20:44:18
You can drop this and all the initializers. The a
keishi
2012/03/02 06:12:13
Done.
|
| + |
| +COLORREF ColorChooserDialog::g_custom_colors[kCustomColorCount] = {kColorBlack, |
| + kColorBlack, kColorBlack, kColorBlack, kColorBlack, kColorBlack, |
| + kColorBlack, kColorBlack, kColorBlack, kColorBlack, kColorBlack, |
| + kColorBlack, kColorBlack, kColorBlack, kColorBlack, kColorBlack}; |
| + |
| +ColorChooserDialog::ExecuteOpenParams::ExecuteOpenParams(SkColor color, |
| + RunState run_state, |
| + HWND owner) |
| + : color(color), |
| + run_state(run_state), |
| + owner(owner) { |
| +} |
| + |
| +ColorChooserDialog::ColorChooserDialog(Listener* listener, |
| + SkColor initial_color, |
| + gfx::NativeWindow owning_window) |
| + : listener_(listener) { |
| + DCHECK(listener_); |
| + ExecuteOpenParams execute_params(initial_color, BeginRun(owning_window), |
| + owning_window); |
| + execute_params.run_state.dialog_thread->message_loop()->PostTask(FROM_HERE, |
| + base::Bind(&ColorChooserDialog::ExecuteOpen, this, execute_params)); |
| +} |
| + |
| +ColorChooserDialog::~ColorChooserDialog() { |
| +} |
| + |
| +bool ColorChooserDialog::IsRunning(HWND owning_hwnd) const { |
| + return listener_ && IsRunningDialogForOwner(owning_hwnd); |
| +} |
| + |
| +void ColorChooserDialog::ListenerDestroyed() { |
| + // Our associated listener has gone away, so we shouldn't call back to it if |
| + // our worker thread returns after the listener is dead. |
| + listener_ = NULL; |
| +} |
| + |
| +void ColorChooserDialog::ExecuteOpen(const ExecuteOpenParams& params) { |
| + CHOOSECOLOR cc; |
| + cc.lStructSize = sizeof(CHOOSECOLOR); |
| + cc.hwndOwner = params.owner; |
| + cc.rgbResult = skia::SkColorToCOLORREF(params.color); |
| + CopyCustomColors(g_custom_colors, custom_colors_); |
|
Peter Kasting
2012/03/01 20:44:18
You need to do this in the constructor like I aske
keishi
2012/03/02 06:12:13
Done.
|
| + cc.lpCustColors = custom_colors_; |
| + cc.Flags = CC_ANYCOLOR | CC_FULLOPEN | CC_RGBINIT; |
| + bool success = !!ChooseColor(&cc); |
| + DisableOwner(cc.hwndOwner); |
| + if (success) { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&ColorChooserDialog::DidChooseColor, this, true, |
| + skia::COLORREFToSkColor(cc.rgbResult), |
| + params.run_state)); |
| + } else { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&ColorChooserDialog::DidChooseColor, this, false, 0, |
| + params.run_state)); |
| + } |
| +} |
| + |
| +void ColorChooserDialog::DidChooseColor(bool success, SkColor color, |
| + RunState run_state) { |
| + if (!listener_) |
| + return; |
| + EndRun(run_state); |
| + CopyCustomColors(custom_colors_, g_custom_colors); |
| + if (success) |
| + listener_->DidChooseColor(color); |
| + listener_->DidEnd(); |
| +} |
| + |
| +void ColorChooserDialog::CopyCustomColors(COLORREF* src, COLORREF* dst) { |
| + memcpy(dst, src, sizeof(COLORREF) * 16); |
|
Peter Kasting
2012/03/01 20:44:18
Nit: Use your constant or else arraysize() rather
keishi
2012/03/02 06:12:13
Done.
|
| +} |