Home | History | Annotate | Download | only in views
      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 "chrome/browser/ui/views/keyboard_overlay_dialog_view.h"
      6 
      7 #include "chrome/browser/chromeos/frame/bubble_window.h"
      8 #include "chrome/browser/ui/browser_dialogs.h"
      9 #include "chrome/browser/ui/views/accelerator_table_gtk.h"
     10 #include "chrome/browser/ui/views/frame/browser_view.h"
     11 #include "chrome/browser/ui/views/keyboard_overlay_delegate.h"
     12 #include "chrome/browser/ui/views/window.h"
     13 #include "content/browser/tab_contents/tab_contents.h"
     14 #include "content/common/native_web_keyboard_event.h"
     15 #include "grit/generated_resources.h"
     16 #include "ui/base/keycodes/keyboard_codes.h"
     17 #include "ui/base/l10n/l10n_util.h"
     18 #include "views/events/event.h"
     19 #include "views/widget/root_view.h"
     20 #include "views/widget/widget.h"
     21 #include "views/window/window.h"
     22 
     23 #if defined(OS_LINUX)
     24 #include "views/window/window_gtk.h"
     25 #endif
     26 
     27 namespace {
     28 struct Accelerator {
     29   ui::KeyboardCode keycode;
     30   bool shift_pressed;
     31   bool ctrl_pressed;
     32   bool alt_pressed;
     33 } kCloseAccelerators[] = {
     34   {ui::VKEY_OEM_2, false, true, true},
     35   {ui::VKEY_OEM_2, true, true, true},
     36   {ui::VKEY_ESCAPE, true, false, false},
     37 };
     38 }  // namespace
     39 
     40 KeyboardOverlayDialogView::KeyboardOverlayDialogView(
     41     Profile* profile,
     42     HtmlDialogUIDelegate* delegate,
     43     BrowserView* parent_view)
     44     : HtmlDialogView(profile, delegate),
     45       parent_view_(parent_view) {
     46 }
     47 
     48 KeyboardOverlayDialogView::~KeyboardOverlayDialogView() {
     49 }
     50 
     51 void KeyboardOverlayDialogView::InitDialog() {
     52   DOMView::Init(profile(), NULL);
     53 
     54   tab_contents_->set_delegate(this);
     55 
     56   // Set the delegate. This must be done before loading the page. See
     57   // the comment above HtmlDialogUI in its header file for why.
     58   HtmlDialogUI::GetPropertyAccessor().SetProperty(tab_contents_->property_bag(),
     59                                                   this);
     60 
     61   for (size_t i = 0; i < arraysize(kCloseAccelerators); ++i) {
     62     views::Accelerator accelerator(kCloseAccelerators[i].keycode,
     63                                    kCloseAccelerators[i].shift_pressed,
     64                                    kCloseAccelerators[i].ctrl_pressed,
     65                                    kCloseAccelerators[i].alt_pressed);
     66     close_accelerators_.insert(accelerator);
     67     AddAccelerator(accelerator);
     68   }
     69 
     70   for (size_t i = 0; i < browser::kAcceleratorMapLength; ++i) {
     71     views::Accelerator accelerator(browser::kAcceleratorMap[i].keycode,
     72                                    browser::kAcceleratorMap[i].shift_pressed,
     73                                    browser::kAcceleratorMap[i].ctrl_pressed,
     74                                    browser::kAcceleratorMap[i].alt_pressed);
     75     // Skip a sole ALT key since it's handled on the keyboard overlay.
     76     if (views::Accelerator(ui::VKEY_MENU, false, false, false) == accelerator) {
     77       continue;
     78     }
     79     // Skip accelerators for closing the dialog since they are already added.
     80     if (IsCloseAccelerator(accelerator)) {
     81       continue;
     82     }
     83     AddAccelerator(accelerator);
     84   }
     85 
     86   DOMView::LoadURL(GetDialogContentURL());
     87 }
     88 
     89 bool KeyboardOverlayDialogView::AcceleratorPressed(
     90     const views::Accelerator& accelerator) {
     91   if (!IsCloseAccelerator(accelerator)) {
     92     parent_view_->AcceleratorPressed(accelerator);
     93   }
     94   OnDialogClosed(std::string());
     95   return true;
     96 }
     97 
     98 void KeyboardOverlayDialogView::ShowDialog(
     99     gfx::NativeWindow owning_window, BrowserView* parent_view) {
    100   KeyboardOverlayDelegate* delegate = new KeyboardOverlayDelegate(
    101       UTF16ToWide(l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_TITLE)));
    102   KeyboardOverlayDialogView* html_view =
    103       new KeyboardOverlayDialogView(parent_view->browser()->profile(),
    104                                     delegate,
    105                                     parent_view);
    106   delegate->set_view(html_view);
    107   html_view->InitDialog();
    108   chromeos::BubbleWindow::Create(owning_window,
    109                                  gfx::Rect(),
    110                                  chromeos::BubbleWindow::STYLE_XSHAPE,
    111                                  html_view);
    112   html_view->window()->Show();
    113 }
    114 
    115 bool KeyboardOverlayDialogView::IsCloseAccelerator(
    116     const views::Accelerator& accelerator) {
    117   return close_accelerators_.find(accelerator) != close_accelerators_.end();
    118 }
    119