Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(691)

Side by Side Diff: chrome/browser/ui/gtk/autofill/autofill_popup_view_gtk.cc

Issue 9187009: Basic Drawn text for new GTK Autofill popup. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Use Cairo Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "autofill_popup_view_gtk.h" 5 #include "autofill_popup_view_gtk.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/native_widget_types.h"
11 #include "ui/base/gtk/gtk_hig_constants.h"
8 #include "ui/base/gtk/gtk_windowing.h" 12 #include "ui/base/gtk/gtk_windowing.h"
13 #include "ui/gfx/font.h"
14
15 namespace {
16 const GdkColor kBorderColor = GDK_COLOR_RGB(0xc7, 0xca, 0xce);
17 const GdkColor kTextColor = GDK_COLOR_RGB(0x00, 0x00, 0x00);
18
19 // The amount of minimum padding between the autofill value and label.
Ilya Sherman 2012/01/11 23:36:29 nit: You should mention that this is measured in p
csharp 2012/01/12 19:39:11 Done.
20 const int kMiddlePadding = 10;
21
22 // We have a 1 pixel border around the entire results popup.
23 const int kBorderThickness = 1;
24
25 gfx::Rect GetWindowRect(GdkWindow* window) {
Ilya Sherman 2012/01/11 23:36:29 nit: Please include a comment for every method, ev
csharp 2012/01/12 19:39:11 Done.
26 gint width, height;
27 gdk_drawable_get_size(GDK_DRAWABLE(window), &width, &height);
28 return gfx::Rect(width, height);
29 }
30
31 gfx::Rect GetRectForLine(size_t line, int width, int height) {
Ilya Sherman 2012/01/11 23:36:29 nit: In the context of "Rect", "Line" is ambiguous
csharp 2012/01/12 19:39:11 Done.
32 return gfx::Rect(0, (line * height), width, height);
33 }
34
35 void SetTextToDraw(PangoLayout* layout,
36 const string16& text) {
Ilya Sherman 2012/01/11 23:36:29 nit: This looks like it could fit on the previous
csharp 2012/01/12 19:39:11 Done.
37 // Pango is really easy to overflow and send into a computational death
38 // spiral that can corrupt the screen. Assume that we'll never have more than
39 // 2000 characters, which should be a safe assumption until we all get robot
40 // eyes. http://crbug.com/66576
41 std::string text_utf8 = UTF16ToUTF8(text);
42 if (text_utf8.length() > 2000)
43 text_utf8 = text_utf8.substr(0, 2000);
44
45 pango_layout_set_text(layout, text_utf8.data(), text_utf8.length());
46 }
47
48 } // namespace
9 49
10 AutofillPopupViewGtk::AutofillPopupViewGtk(content::WebContents* web_contents, 50 AutofillPopupViewGtk::AutofillPopupViewGtk(content::WebContents* web_contents,
11 GtkWidget* parent) 51 GtkWidget* parent)
12 : AutofillPopupView(web_contents), 52 : AutofillPopupView(web_contents),
13 parent_(parent), 53 parent_(parent),
14 window_(gtk_window_new(GTK_WINDOW_POPUP)) { 54 window_(gtk_window_new(GTK_WINDOW_POPUP)) {
15 CHECK(parent != NULL); 55 CHECK(parent != NULL);
16 gtk_window_set_resizable(GTK_WINDOW(window_), FALSE); 56 gtk_window_set_resizable(GTK_WINDOW(window_), FALSE);
17 gtk_widget_set_app_paintable(window_, TRUE); 57 gtk_widget_set_app_paintable(window_, TRUE);
18 gtk_widget_set_double_buffered(window_, TRUE); 58 gtk_widget_set_double_buffered(window_, TRUE);
19 59
20 // Setup the window to ensure it recieves the expose event. 60 // Setup the window to ensure it recieves the expose event.
21 gtk_widget_add_events(window_, GDK_EXPOSURE_MASK); 61 gtk_widget_add_events(window_, GDK_EXPOSURE_MASK);
22 g_signal_connect(window_, "expose-event", 62 g_signal_connect(window_, "expose-event",
23 G_CALLBACK(HandleExposeThunk), this); 63 G_CALLBACK(HandleExposeThunk), this);
64
65 // Cache the layout so we don't have to create it for every expose.
66 layout_ = gtk_widget_create_pango_layout(window_, NULL);
67
68 row_height_ = font_.GetHeight();
Ilya Sherman 2012/01/11 23:36:29 Hmm, where is the font_ initialized?
csharp 2012/01/12 19:39:11 I just use the default constructor. Should I still
Ilya Sherman 2012/01/13 02:45:36 No, this is fine as is. It just seemed a little w
24 } 69 }
25 70
26 AutofillPopupViewGtk::~AutofillPopupViewGtk() { 71 AutofillPopupViewGtk::~AutofillPopupViewGtk() {
72 g_object_unref(layout_);
27 gtk_widget_destroy(window_); 73 gtk_widget_destroy(window_);
28 } 74 }
29 75
30 void AutofillPopupViewGtk::Hide() { 76 void AutofillPopupViewGtk::Hide() {
31 gtk_widget_hide(window_); 77 gtk_widget_hide(window_);
32 } 78 }
33 79
34 // TODO(csharp): Actually show the values. 80 void AutofillPopupViewGtk::ShowInternal(
35 void AutofillPopupViewGtk::Show(const std::vector<string16>& autofill_values, 81 const std::vector<string16>& autofill_values,
36 const std::vector<string16>& autofill_labels, 82 const std::vector<string16>& autofill_labels,
37 const std::vector<string16>& autofill_icons, 83 const std::vector<string16>& autofill_icons) {
38 const std::vector<int>& autofill_unique_ids,
39 int separator_index) {
40 gint origin_x, origin_y; 84 gint origin_x, origin_y;
41 gdk_window_get_origin(gtk_widget_get_window(parent_), &origin_x, &origin_y); 85 gdk_window_get_origin(gtk_widget_get_window(parent_), &origin_x, &origin_y);
42 86
87 // Move the popup to appear right below the text field it is using.
43 gtk_window_move(GTK_WINDOW(window_), 88 gtk_window_move(GTK_WINDOW(window_),
44 origin_x + element_bounds().x(), 89 origin_x + element_bounds().x(),
45 origin_y + element_bounds().y() + element_bounds().height()); 90 origin_y + element_bounds().y() + element_bounds().height());
46 91
92 // Find out the maximum bounds required by the popup.
93 // TODO(csharp): Once the icon is also displayed it will affect the required
94 // size so it will need to be include in the calculation.
95 int popup_required_width = element_bounds().width();
Ilya Sherman 2012/01/11 23:36:29 nit: How about just "popup_width"?
csharp 2012/01/12 19:39:11 Done.
96 CHECK_EQ(autofill_values.size(), autofill_labels.size());
Ilya Sherman 2012/01/11 23:36:29 nit: Can this be a DCHECK, so that we don't compil
csharp 2012/01/12 19:39:11 Done.
97 for (size_t i = 0; i < autofill_values.size(); ++i) {
98 popup_required_width = std::max(popup_required_width,
99 font_.GetStringWidth(autofill_values[i]) +
100 kMiddlePadding +
101 font_.GetStringWidth(autofill_labels[i]));
102 }
103
47 gtk_widget_set_size_request( 104 gtk_widget_set_size_request(
48 window_, 105 window_,
49 element_bounds().width(), 106 popup_required_width,
50 element_bounds().height() * autofill_values.size()); 107 row_height_ * autofill_values.size());
51 108
52 gtk_widget_show(window_); 109 gtk_widget_show(window_);
53 110
54 GtkWidget* toplevel = gtk_widget_get_toplevel(parent_); 111 GtkWidget* toplevel = gtk_widget_get_toplevel(parent_);
55 CHECK(gtk_widget_is_toplevel(toplevel)); 112 CHECK(gtk_widget_is_toplevel(toplevel));
56 ui::StackPopupWindow(window_, toplevel); 113 ui::StackPopupWindow(window_, toplevel);
57 } 114 }
58 115
59 gboolean AutofillPopupViewGtk::HandleExpose(GtkWidget* widget, 116 gboolean AutofillPopupViewGtk::HandleExpose(GtkWidget* widget,
60 GdkEventExpose* event) { 117 GdkEventExpose* event) {
118 gfx::Rect window_rect = GetWindowRect(event->window);
119 gfx::Rect damage_rect = gfx::Rect(event->area);
120
121 cairo_t* cr = gdk_cairo_create(GDK_DRAWABLE(gtk_widget_get_window(widget)));
122 gdk_cairo_rectangle(cr, &event->area);
123 cairo_clip(cr);
124
125 // This assert is kinda ugly, but it would be more currently unneeded work
126 // to support painting a border that isn't 1 pixel thick. There is no point
127 // in writing that code now, and explode if that day ever comes.
128 COMPILE_ASSERT(kBorderThickness == 1, border_1px_implied);
Ilya Sherman 2012/01/11 23:36:29 Huh, I've never seen this macro used before. Nift
Elliot Glaysher 2012/01/12 20:34:11 OT: I'm using it in profile_impl to make sure that
129 // Draw the 1px border around the entire window.
130 gdk_cairo_set_source_color(cr, &kBorderColor);
131 cairo_rectangle(cr, 0, 0, window_rect.width(), window_rect.height());
132 cairo_stroke(cr);
133
134 SetupLayout(window_rect, &kTextColor);
135
136 int actual_content_width, actual_content_height;
137 pango_layout_get_size(layout_, &actual_content_width, &actual_content_height);
138 actual_content_width /= PANGO_SCALE;
139 actual_content_height /= PANGO_SCALE;
140
141 for (size_t i = 0; i < autofill_values().size(); ++i) {
142 gfx::Rect line_rect = GetRectForLine(i, window_rect.width(), row_height_);
143 // Only repaint and layout damaged lines.
144 if (!line_rect.Intersects(damage_rect))
145 continue;
146
147 if (separator_index() == static_cast<int>(i)) {
148 int line_y = i * row_height_;
149
150 cairo_save(cr);
151 cairo_move_to(cr, 0, line_y);
152 cairo_line_to(cr, window_rect.width(), line_y);
153 cairo_stroke(cr);
154 cairo_restore(cr);
155 }
156
157 // Center the text within the line.
158 int content_y = std::max(
159 line_rect.y(),
160 line_rect.y() + ((row_height_ - actual_content_height) / 2));
Ilya Sherman 2012/01/11 23:36:29 I don't understand what this computation does -- w
csharp 2012/01/12 19:39:11 It will be positive because row_height_ >= actual_
Ilya Sherman 2012/01/13 02:45:36 Hmm, where is actual_content_height updated to be
csharp 2012/01/13 18:09:03 When I checked with the debugger the value for hei
Elliot Glaysher 2012/01/13 20:39:30 I unfortunately don't. And the original author of
161
162 // Draw the autofill value.
163 SetTextToDraw(layout_, autofill_values()[i]);
164
165 cairo_save(cr);
166 cairo_move_to(cr, 0, content_y);
167 pango_cairo_show_layout(cr, layout_);
168 cairo_restore(cr);
169
170 // Draw the autofill label.
171 int x_align_left = window_rect.width()
172 - font_.GetStringWidth(autofill_labels()[i]);
Ilya Sherman 2012/01/11 23:36:29 nit: The minus sign should be on the preceding lin
csharp 2012/01/12 19:39:11 Done.
173 SetTextToDraw(layout_, autofill_labels()[i]);
174
175 cairo_save(cr);
176 cairo_move_to(cr, x_align_left, line_rect.y());
177 pango_cairo_show_layout(cr, layout_);
178 cairo_restore(cr);
179 }
180
181 cairo_destroy(cr);
182
61 return TRUE; 183 return TRUE;
62 } 184 }
185
186 void AutofillPopupViewGtk::SetupLayout(const gfx::Rect& window_rect,
187 const GdkColor* text_color) {
Ilya Sherman 2012/01/11 23:36:29 nit: Can this be passed by const-reference instead
csharp 2012/01/12 19:39:11 Done.
188 int allocated_content_width = window_rect.width();
189 pango_layout_set_width(layout_, allocated_content_width * PANGO_SCALE);
190 pango_layout_set_height(layout_, row_height_ * PANGO_SCALE);
191
192 PangoAttrList* attrs = pango_attr_list_new();
193
194 PangoAttribute* fg_attr = pango_attr_foreground_new(text_color->red,
195 text_color->green,
196 text_color->blue);
197 pango_attr_list_insert(attrs, fg_attr); // Ownership taken.
198
199
200 pango_layout_set_attributes(layout_, attrs); // Ref taken.
201 pango_attr_list_unref(attrs);
202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698