OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 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 "chrome/browser/views/find_bar_host.h" |
| 6 |
| 7 #include "chrome/browser/find_bar_controller.h" |
| 8 #include "chrome/browser/renderer_host/render_view_host.h" |
| 9 #include "chrome/browser/tab_contents/tab_contents.h" |
| 10 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 11 #include "chrome/browser/views/frame/browser_view.h" |
| 12 #include "views/controls/scrollbar/native_scroll_bar.h" |
| 13 #include "views/widget/widget_win.h" |
| 14 |
| 15 class FindBarHostWidget : public views::WidgetWin { |
| 16 public: |
| 17 explicit FindBarHostWidget(FindBarHost* find_bar) : find_bar_(find_bar) { |
| 18 // Don't let WidgetWin manage our lifetime. We want our lifetime to |
| 19 // coincide with TabContents. |
| 20 set_delete_on_destroy(false); |
| 21 set_window_style(WS_CHILD | WS_CLIPCHILDREN); |
| 22 set_window_ex_style(WS_EX_TOPMOST); |
| 23 } |
| 24 |
| 25 void OnFinalMessage(HWND window) { |
| 26 find_bar_->OnFinalMessage(); |
| 27 } |
| 28 |
| 29 private: |
| 30 FindBarHost* find_bar_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(FindBarHostWidget); |
| 33 }; |
| 34 |
| 35 // TODO(brettw) this should not be so complicated. The view should really be in |
| 36 // charge of these regions. CustomFrameWindow will do this for us. It will also |
| 37 // let us set a path for the window region which will avoid some logic here. |
| 38 void FindBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { |
| 39 // |w| is used to make it easier to create the part of the polygon that curves |
| 40 // the right side of the Find window. It essentially keeps track of the |
| 41 // x-pixel position of the right-most background image inside the view. |
| 42 // TODO(finnur): Let the view tell us how to draw the curves or convert |
| 43 // this to a CustomFrameWindow. |
| 44 int w = new_pos.width() - 6; // -6 positions us at the left edge of the |
| 45 // rightmost background image of the view. |
| 46 |
| 47 // This polygon array represents the outline of the background image for the |
| 48 // dialog. Basically, it encompasses only the visible pixels of the |
| 49 // concatenated find_dlg_LMR_bg images (where LMR = [left | middle | right]). |
| 50 static const POINT polygon[] = { |
| 51 {0, 0}, {0, 1}, {2, 3}, {2, 29}, {4, 31}, |
| 52 {4, 32}, {w+0, 32}, |
| 53 {w+0, 31}, {w+1, 31}, {w+3, 29}, {w+3, 3}, {w+6, 0} |
| 54 }; |
| 55 |
| 56 // Find the largest x and y value in the polygon. |
| 57 int max_x = 0, max_y = 0; |
| 58 for (int i = 0; i < arraysize(polygon); i++) { |
| 59 max_x = std::max(max_x, static_cast<int>(polygon[i].x)); |
| 60 max_y = std::max(max_y, static_cast<int>(polygon[i].y)); |
| 61 } |
| 62 |
| 63 // We then create the polygon and use SetWindowRgn to force the window to draw |
| 64 // only within that area. This region may get reduced in size below. |
| 65 HRGN region = CreatePolygonRgn(polygon, arraysize(polygon), ALTERNATE); |
| 66 |
| 67 // Are we animating? |
| 68 if (find_dialog_animation_offset_ > 0) { |
| 69 // The animation happens in two steps: First, we clip the window and then in |
| 70 // GetDialogPosition we offset the window position so that it still looks |
| 71 // attached to the toolbar as it grows. We clip the window by creating a |
| 72 // rectangle region (that gradually increases as the animation progresses) |
| 73 // and find the intersection between the two regions using CombineRgn. |
| 74 |
| 75 // |y| shrinks as the animation progresses from the height of the view down |
| 76 // to 0 (and reverses when closing). |
| 77 int y = find_dialog_animation_offset_; |
| 78 // |y| shrinking means the animation (visible) region gets larger. In other |
| 79 // words: the rectangle grows upward (when the dialog is opening). |
| 80 HRGN animation_region = CreateRectRgn(0, y, max_x, max_y); |
| 81 // |region| will contain the intersected parts after calling this function: |
| 82 CombineRgn(region, animation_region, region, RGN_AND); |
| 83 DeleteObject(animation_region); |
| 84 |
| 85 // Next, we need to increase the region a little bit to account for the |
| 86 // curved edges that the view will draw to make it look like grows out of |
| 87 // the toolbar. |
| 88 POINT left_curve[] = { |
| 89 {0, y+0}, {0, y+1}, {2, y+3}, {2, y+0}, {0, y+0} |
| 90 }; |
| 91 POINT right_curve[] = { |
| 92 {w+3, y+3}, {w+6, y+0}, {w+3, y+0}, {w+3, y+3} |
| 93 }; |
| 94 |
| 95 // Combine the region for the curve on the left with our main region. |
| 96 HRGN r = CreatePolygonRgn(left_curve, arraysize(left_curve), ALTERNATE); |
| 97 CombineRgn(region, r, region, RGN_OR); |
| 98 DeleteObject(r); |
| 99 |
| 100 // Combine the region for the curve on the right with our main region. |
| 101 r = CreatePolygonRgn(right_curve, arraysize(right_curve), ALTERNATE); |
| 102 CombineRgn(region, r, region, RGN_OR); |
| 103 DeleteObject(r); |
| 104 } |
| 105 |
| 106 // Now see if we need to truncate the region because parts of it obscures |
| 107 // the main window border. |
| 108 gfx::Rect dialog_bounds; |
| 109 GetDialogBounds(&dialog_bounds); |
| 110 |
| 111 // Calculate how much our current position overlaps our boundaries. If we |
| 112 // overlap, it means we have too little space to draw the whole dialog and |
| 113 // we allow overwriting the scrollbar before we start truncating our dialog. |
| 114 // |
| 115 // TODO(brettw) this constant is evil. This is the amount of room we've added |
| 116 // to the window size, when we set the region, it can change the size. |
| 117 static const int kAddedWidth = 7; |
| 118 int difference = (new_pos.right() - kAddedWidth) - |
| 119 dialog_bounds.width() - |
| 120 views::NativeScrollBar::GetVerticalScrollBarWidth() + |
| 121 1; |
| 122 if (difference > 0) { |
| 123 POINT exclude[4] = {0}; |
| 124 exclude[0].x = max_x - difference; // Top left corner. |
| 125 exclude[0].y = 0; |
| 126 |
| 127 exclude[1].x = max_x; // Top right corner. |
| 128 exclude[1].y = 0; |
| 129 |
| 130 exclude[2].x = max_x; // Bottom right corner. |
| 131 exclude[2].y = max_y; |
| 132 |
| 133 exclude[3].x = max_x - difference; // Bottom left corner. |
| 134 exclude[3].y = max_y; |
| 135 |
| 136 // Subtract this region from the original region. |
| 137 HRGN exclude_rgn = CreatePolygonRgn(exclude, arraysize(exclude), ALTERNATE); |
| 138 int result = CombineRgn(region, region, exclude_rgn, RGN_DIFF); |
| 139 DeleteObject(exclude_rgn); |
| 140 } |
| 141 |
| 142 // The system now owns the region, so we do not delete it. |
| 143 ::SetWindowRgn(host_->GetNativeView(), region, TRUE); // TRUE = Redraw. |
| 144 } |
| 145 |
| 146 NativeWebKeyboardEvent FindBarHost::GetKeyboardEvent( |
| 147 const TabContents* contents, |
| 148 const views::Textfield::Keystroke& key_stroke) { |
| 149 HWND hwnd = contents->GetContentNativeView(); |
| 150 return NativeWebKeyboardEvent( |
| 151 hwnd, key_stroke.message(), key_stroke.key(), 0); |
| 152 } |
| 153 |
| 154 void FindBarHost::AudibleAlert() { |
| 155 MessageBeep(MB_OK); |
| 156 } |
| 157 |
| 158 views::Widget* FindBarHost::CreateHost() { |
| 159 return new FindBarHostWidget(this); |
| 160 } |
| 161 |
| 162 void FindBarHost::SetDialogPositionNative(const gfx::Rect& new_pos, |
| 163 bool no_redraw) { |
| 164 gfx::Rect window_rect; |
| 165 host_->GetBounds(&window_rect, true); |
| 166 DWORD swp_flags = SWP_NOOWNERZORDER; |
| 167 if (!window_rect.IsEmpty()) |
| 168 swp_flags |= SWP_NOSIZE; |
| 169 if (no_redraw) |
| 170 swp_flags |= SWP_NOREDRAW; |
| 171 if (!host_->IsVisible()) |
| 172 swp_flags |= SWP_SHOWWINDOW; |
| 173 |
| 174 ::SetWindowPos(host_->GetNativeView(), HWND_TOP, new_pos.x(), new_pos.y(), |
| 175 new_pos.width(), new_pos.height(), swp_flags); |
| 176 } |
| 177 |
| 178 void FindBarHost::GetDialogPositionNative(gfx::Rect* avoid_overlapping_rect) { |
| 179 RECT frame_rect = {0}, webcontents_rect = {0}; |
| 180 ::GetWindowRect( |
| 181 static_cast<views::WidgetWin*>(host_.get())->GetParent(), &frame_rect); |
| 182 ::GetWindowRect( |
| 183 find_bar_controller_->tab_contents()->view()->GetNativeView(), |
| 184 &webcontents_rect); |
| 185 avoid_overlapping_rect->Offset(0, webcontents_rect.top - frame_rect.top); |
| 186 } |
| 187 |
| 188 gfx::NativeView FindBarHost::GetNativeView(BrowserView* browser_view) { |
| 189 return browser_view->GetWidget()->GetNativeView(); |
| 190 } |
| 191 |
OLD | NEW |