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 "ui/views/controls/textfield/gtk_views_textview.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "ui/gfx/canvas_skia_paint.h" | |
9 #include "ui/gfx/insets.h" | |
10 #include "ui/gfx/skia_utils_gtk.h" | |
11 #include "ui/views/controls/textfield/native_textfield_gtk.h" | |
12 #include "ui/views/controls/textfield/textfield.h" | |
13 | |
14 G_BEGIN_DECLS | |
15 | |
16 G_DEFINE_TYPE(GtkViewsTextView, gtk_views_textview, GTK_TYPE_TEXT_VIEW) | |
17 | |
18 static gint gtk_views_textview_expose_event(GtkWidget *widget, | |
19 GdkEventExpose *event) { | |
20 GtkTextView* text_view = GTK_TEXT_VIEW(widget); | |
21 GdkWindow* text_window = gtk_text_view_get_window(text_view, | |
22 GTK_TEXT_WINDOW_TEXT); | |
23 if (event->window == text_window) { | |
24 gint width, height; | |
25 gdk_drawable_get_size (text_window, &width, &height); | |
26 gtk_paint_flat_box(widget->style, text_window, | |
27 static_cast<GtkStateType>(GTK_WIDGET_STATE(widget)), GTK_SHADOW_NONE, | |
28 &event->area, widget, "textview", | |
29 0, 0, width, height); | |
30 } | |
31 | |
32 gint result = GTK_WIDGET_CLASS(gtk_views_textview_parent_class)->expose_event( | |
33 widget, event); | |
34 | |
35 GtkTextBuffer* text_buffer = gtk_text_view_get_buffer(text_view); | |
36 views::NativeTextfieldGtk* host = GTK_VIEWS_TEXTVIEW(widget)->host; | |
37 | |
38 GtkTextIter start; | |
39 GtkTextIter end; | |
40 gtk_text_buffer_get_bounds(text_buffer, &start, &end); | |
41 | |
42 // Paint the info text to text window if GtkTextView contains no text. | |
43 if (host && event->window == text_window && | |
44 !host->textfield()->text_to_display_when_empty().empty() && | |
45 gtk_text_iter_equal(&start, &end)) { | |
46 gfx::CanvasSkiaPaint canvas(event); | |
47 if (!canvas.is_empty()) { | |
48 gfx::Insets insets = | |
49 views::NativeTextfieldGtk::GetTextViewInnerBorder(text_view); | |
50 gfx::Font font = host->textfield()->font(); | |
51 const string16 text = host->textfield()->text_to_display_when_empty(); | |
52 canvas.DrawStringInt( | |
53 text, font, | |
54 gfx::GdkColorToSkColor(widget->style->text[GTK_STATE_INSENSITIVE]), | |
55 insets.left(), insets.top(), | |
56 widget->allocation.width - insets.width(), font.GetHeight()); | |
57 } | |
58 } | |
59 | |
60 // Draw border and focus. | |
61 if (event->window == widget->window) { | |
62 gint width; | |
63 gint height; | |
64 gdk_drawable_get_size (widget->window, &width, &height); | |
65 | |
66 bool has_focus = GTK_WIDGET_HAS_FOCUS(widget); | |
67 | |
68 gtk_paint_shadow(widget->style, widget->window, | |
69 has_focus ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL, | |
70 GTK_SHADOW_OUT, | |
71 &event->area, widget, "textview", | |
72 0, 0, width, height); | |
73 if (has_focus) { | |
74 gtk_paint_focus(widget->style, widget->window, | |
75 static_cast<GtkStateType>(GTK_WIDGET_STATE(widget)), | |
76 &event->area, widget, "textview", | |
77 0, 0, width, height); | |
78 } | |
79 } | |
80 | |
81 return result; | |
82 } | |
83 | |
84 static void gtk_views_textview_class_init( | |
85 GtkViewsTextViewClass* views_textview_class) { | |
86 GtkWidgetClass* widget_class = | |
87 reinterpret_cast<GtkWidgetClass*>(views_textview_class); | |
88 widget_class->expose_event = gtk_views_textview_expose_event; | |
89 } | |
90 | |
91 static void gtk_views_textview_init(GtkViewsTextView* text_view) { | |
92 text_view->host = NULL; | |
93 } | |
94 | |
95 GtkWidget* gtk_views_textview_new(views::NativeTextfieldGtk* host) { | |
96 gpointer text_view = g_object_new(GTK_TYPE_VIEWS_TEXTVIEW, NULL); | |
97 GTK_VIEWS_TEXTVIEW(text_view)->host = host; | |
98 return GTK_WIDGET(text_view); | |
99 } | |
100 | |
101 G_END_DECLS | |
OLD | NEW |