OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include <windows.h> |
| 6 #include <atlbase.h> |
| 7 #include <atlapp.h> |
| 8 #include <atlcrack.h> |
| 9 #include <atlmisc.h> |
| 10 |
| 11 #include "base/at_exit.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "gfx/canvas.h" |
| 14 #include "ui/views/view.h" |
| 15 #include "ui/views/widget/widget.h" |
| 16 #include "ui/views/widget/native_widget_win.h" |
| 17 |
| 18 class V2DemoDispatcher : public MessageLoopForUI::Dispatcher { |
| 19 public: |
| 20 V2DemoDispatcher() {} |
| 21 virtual ~V2DemoDispatcher() {} |
| 22 |
| 23 private: |
| 24 // Overridden from MessageLoopForUI::Dispatcher: |
| 25 virtual bool Dispatch(const MSG& msg) { |
| 26 TranslateMessage(&msg); |
| 27 DispatchMessage(&msg); |
| 28 return true; |
| 29 } |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(V2DemoDispatcher); |
| 32 }; |
| 33 |
| 34 class ColorView : public ui::View { |
| 35 public: |
| 36 explicit ColorView(SkColor color) : color_(color) { |
| 37 } |
| 38 ColorView() : color_(SK_ColorBLACK) {} |
| 39 virtual ~ColorView() {} |
| 40 |
| 41 protected: |
| 42 SkColor color() const { return color_; } |
| 43 void set_color(SkColor color) { color_ = color; } |
| 44 |
| 45 private: |
| 46 // Overridden from ui::View: |
| 47 virtual void OnPaint(gfx::Canvas* canvas) { |
| 48 canvas->FillRectInt(color_, 0, 0, width(), height()); |
| 49 } |
| 50 virtual bool OnMousePressed(const ui::MouseEvent& event) { |
| 51 color_ = color_ == SK_ColorBLACK ? SK_ColorWHITE : SK_ColorBLACK; |
| 52 Invalidate(); |
| 53 return true; |
| 54 } |
| 55 virtual void OnMouseReleased(const ui::MouseEvent& event, bool canceled) { |
| 56 color_ = color_ == SK_ColorWHITE ? SK_ColorMAGENTA : SK_ColorGREEN; |
| 57 Invalidate(); |
| 58 } |
| 59 virtual void OnMouseMoved(const ui::MouseEvent& event) { |
| 60 U8CPU r = SkColorGetR(color_); |
| 61 color_ = SkColorSetRGB(++r % 255, SkColorGetG(color_), |
| 62 SkColorGetB(color_)); |
| 63 Invalidate(); |
| 64 } |
| 65 virtual bool OnMouseDragged(const ui::MouseEvent& event) { |
| 66 U8CPU g = SkColorGetG(color_); |
| 67 color_ = SkColorSetRGB(SkColorGetR(color_), ++g % 255, |
| 68 SkColorGetB(color_)); |
| 69 Invalidate(); |
| 70 return true; |
| 71 } |
| 72 |
| 73 SkColor color_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(ColorView); |
| 76 }; |
| 77 |
| 78 class FancyPantsView : public ColorView { |
| 79 public: |
| 80 FancyPantsView() |
| 81 : ColorView(SK_ColorMAGENTA), |
| 82 c1_(new ColorView(SK_ColorGREEN)), |
| 83 c2_(new ColorView(SK_ColorRED)) { |
| 84 AddChildView(c1_); |
| 85 AddChildView(c2_); |
| 86 } |
| 87 virtual ~FancyPantsView() {} |
| 88 |
| 89 // Overridden from ui::View: |
| 90 virtual void Layout() { |
| 91 c1_->SetBounds(20, 20, width() - 40, height() - 40); |
| 92 c2_->SetBounds(50, 50, 50, 50); |
| 93 Invalidate(); |
| 94 } |
| 95 virtual bool OnMousePressed(const ui::MouseEvent& event) { |
| 96 old_color_ = color(); |
| 97 set_color(SK_ColorWHITE); |
| 98 mouse_offset_ = event.location(); |
| 99 return true; |
| 100 } |
| 101 virtual bool OnMouseDragged(const ui::MouseEvent& event) { |
| 102 gfx::Rect old_bounds = bounds(); |
| 103 SetPosition(gfx::Point(event.x() - mouse_offset_.x(), |
| 104 event.y() - mouse_offset_.y())); |
| 105 gfx::Rect new_bounds = bounds(); |
| 106 parent()->InvalidateRect(old_bounds.Union(new_bounds)); |
| 107 return true; |
| 108 } |
| 109 virtual void OnMouseReleased(const ui::MouseEvent& event) { |
| 110 set_color(old_color_); |
| 111 } |
| 112 virtual void OnMouseCaptureLost() { |
| 113 set_color(SK_ColorYELLOW); |
| 114 } |
| 115 |
| 116 private: |
| 117 View* c1_; |
| 118 View* c2_; |
| 119 |
| 120 gfx::Point mouse_offset_; |
| 121 SkColor old_color_; |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(FancyPantsView); |
| 124 }; |
| 125 |
| 126 |
| 127 |
| 128 class ContentsView : public ColorView { |
| 129 public: |
| 130 ContentsView() |
| 131 : c1_(new ColorView(SK_ColorBLUE)), |
| 132 c2_(new ColorView(SK_ColorGREEN)), |
| 133 c3_(new FancyPantsView()), |
| 134 ColorView(SK_ColorRED) { |
| 135 set_parent_owned(false); |
| 136 AddChildView(c1_); |
| 137 AddChildView(c2_); |
| 138 c3_->SetPosition(gfx::Point(200, 200)); |
| 139 AddChildView(c3_); |
| 140 } |
| 141 |
| 142 virtual ~ContentsView() {} |
| 143 |
| 144 void Init() { |
| 145 //c3_->SetHasLayer(true); |
| 146 } |
| 147 |
| 148 private: |
| 149 // Overridden from ui::View: |
| 150 virtual void Layout() { |
| 151 c1_->SetBounds(20, 20, width() - 40, height() - 40); |
| 152 c2_->SetBounds(50, 50, 50, 50); |
| 153 c3_->SetSize(gfx::Size(75, 75)); |
| 154 Invalidate(); |
| 155 } |
| 156 |
| 157 View* c1_; |
| 158 View* c2_; |
| 159 FancyPantsView* c3_; |
| 160 }; |
| 161 |
| 162 int main(int argc, char **argv) { |
| 163 OleInitialize(NULL); |
| 164 base::AtExitManager exit_manager; |
| 165 MessageLoop main_message_loop(MessageLoop::TYPE_UI); |
| 166 |
| 167 ContentsView cv; |
| 168 ui::Widget widget(&cv); |
| 169 widget.InitWithNativeViewParent(NULL, gfx::Rect(20, 20, 400, 400)); |
| 170 cv.Init(); |
| 171 widget.Show(); |
| 172 |
| 173 V2DemoDispatcher dispatcher; |
| 174 MessageLoopForUI::current()->Run(&dispatcher); |
| 175 |
| 176 OleUninitialize(); |
| 177 } |
OLD | NEW |