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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/gtk/constrained_window_gtk.h" 6 7 #include <gdk/gdkkeysyms.h> 8 9 #include "chrome/browser/ui/gtk/gtk_util.h" 10 #include "ui/base/gtk/gtk_hig_constants.h" 11 12 namespace { 13 gboolean OnKeyPress(GtkWidget* sender, GdkEventKey* key, gpointer user_data) { 14 if (key->keyval == GDK_Escape) { 15 gtk_widget_destroy(sender); 16 return TRUE; 17 } 18 19 return FALSE; 20 } 21 } // namespace 22 23 GtkWidget* CreateWebContentsModalDialogGtk( 24 GtkWidget* contents, 25 GtkWidget* focus_widget) { 26 // Unlike other users of CreateBorderBin, we need a dedicated frame around 27 // our "window". 28 GtkWidget* border = gtk_event_box_new(); 29 GtkWidget* frame = gtk_frame_new(NULL); 30 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); 31 32 GtkWidget* alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0); 33 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 34 ui::kContentAreaBorder, ui::kContentAreaBorder, 35 ui::kContentAreaBorder, ui::kContentAreaBorder); 36 37 if (gtk_widget_get_parent(contents)) 38 gtk_widget_reparent(contents, alignment); 39 else 40 gtk_container_add(GTK_CONTAINER(alignment), contents); 41 42 gtk_container_add(GTK_CONTAINER(frame), alignment); 43 gtk_container_add(GTK_CONTAINER(border), frame); 44 45 gtk_widget_add_events(border, GDK_KEY_PRESS_MASK); 46 g_signal_connect(border, 47 "key-press-event", 48 reinterpret_cast<GCallback>(&OnKeyPress), 49 NULL); 50 51 // This is a little hacky, but it's better than subclassing GtkWidget just to 52 // add one new property. 53 g_object_set_data(G_OBJECT(border), "focus_widget", focus_widget); 54 55 return border; 56 } 57