OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "views/controls/listbox/native_listbox_win.h" |
| 6 |
| 7 #include <commctrl.h> |
| 8 #include <windowsx.h> |
| 9 |
| 10 #include "app/l10n_util.h" |
| 11 #include "app/l10n_util_win.h" |
| 12 #include "app/resource_bundle.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/win_util.h" |
| 16 #include "gfx/font.h" |
| 17 #include "views/controls/listbox/listbox.h" |
| 18 #include "views/widget/widget.h" |
| 19 |
| 20 namespace views { |
| 21 |
| 22 //////////////////////////////////////////////////////////////////////////////// |
| 23 // NativeListboxWin, public: |
| 24 |
| 25 NativeListboxWin::NativeListboxWin(Listbox* listbox, |
| 26 const std::vector<string16>& strings, |
| 27 Listbox::Listener* listener) |
| 28 : listbox_(listbox), |
| 29 strings_(strings), |
| 30 listener_(listener) { |
| 31 // Associates the actual HWND with the listbox so the listbox is the one |
| 32 // considered as having the focus (not the wrapper) when the HWND is |
| 33 // focused directly (with a click for example). |
| 34 set_focus_view(listbox); |
| 35 } |
| 36 |
| 37 NativeListboxWin::~NativeListboxWin() { |
| 38 } |
| 39 |
| 40 //////////////////////////////////////////////////////////////////////////////// |
| 41 // NativeListboxWin, NativeListboxWrapper implementation: |
| 42 |
| 43 int NativeListboxWin::GetRowCount() const { |
| 44 if (!native_view()) |
| 45 return 0; |
| 46 return ListBox_GetCount(native_view()); |
| 47 } |
| 48 |
| 49 int NativeListboxWin::SelectedRow() const { |
| 50 if (!native_view()) |
| 51 return -1; |
| 52 return ListBox_GetCurSel(native_view()); |
| 53 } |
| 54 |
| 55 void NativeListboxWin::SelectRow(int row) { |
| 56 if (!native_view()) |
| 57 return; |
| 58 ListBox_SetCurSel(native_view(), row); |
| 59 } |
| 60 |
| 61 View* NativeListboxWin::GetView() { |
| 62 return this; |
| 63 } |
| 64 |
| 65 //////////////////////////////////////////////////////////////////////////////// |
| 66 // NativeListboxWin, View overrides: |
| 67 |
| 68 gfx::Size NativeListboxWin::GetPreferredSize() { |
| 69 SIZE sz = {0}; |
| 70 SendMessage(native_view(), BCM_GETIDEALSIZE, 0, |
| 71 reinterpret_cast<LPARAM>(&sz)); |
| 72 |
| 73 return gfx::Size(sz.cx, sz.cy); |
| 74 } |
| 75 |
| 76 //////////////////////////////////////////////////////////////////////////////// |
| 77 // NativeListboxWin, NativeControlWin overrides: |
| 78 |
| 79 bool NativeListboxWin::ProcessMessage(UINT message, WPARAM w_param, |
| 80 LPARAM l_param, LRESULT* result) { |
| 81 if (message == WM_COMMAND) { |
| 82 switch (HIWORD(w_param)) { |
| 83 case LBN_SELCHANGE: |
| 84 if (listener_) |
| 85 listener_->ListboxSelectionChanged(listbox_); |
| 86 return true; |
| 87 default: |
| 88 break; |
| 89 } |
| 90 } |
| 91 |
| 92 return NativeControlWin::ProcessMessage(message, w_param, l_param, result); |
| 93 } |
| 94 |
| 95 //////////////////////////////////////////////////////////////////////////////// |
| 96 // NativeListboxWin, protected: |
| 97 |
| 98 void NativeListboxWin::CreateNativeControl() { |
| 99 int style = WS_CHILD | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY; |
| 100 // If there's only one column and the title string is empty, don't show a |
| 101 // header. |
| 102 HWND hwnd = ::CreateWindowEx(WS_EX_CLIENTEDGE | GetAdditionalRTLStyle(), |
| 103 WC_LISTBOX, |
| 104 L"", |
| 105 style, |
| 106 0, 0, width(), height(), |
| 107 listbox_->GetWidget()->GetNativeView(), |
| 108 NULL, NULL, NULL); |
| 109 HFONT font = ResourceBundle::GetSharedInstance(). |
| 110 GetFont(ResourceBundle::BaseFont).hfont(); |
| 111 SendMessage(hwnd, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE); |
| 112 l10n_util::AdjustUIFontForWindow(hwnd); |
| 113 |
| 114 for (size_t i = 0; i < strings_.size(); ++i) |
| 115 ListBox_AddString(hwnd, UTF16ToWide(strings_[i]).c_str()); |
| 116 |
| 117 NativeControlCreated(hwnd); |
| 118 |
| 119 // Bug 964884: detach the IME attached to this window. |
| 120 // We should attach IMEs only when we need to input CJK strings. |
| 121 ::ImmAssociateContextEx(hwnd, NULL, 0); |
| 122 } |
| 123 |
| 124 //////////////////////////////////////////////////////////////////////////////// |
| 125 // NativeListboxWrapper, public: |
| 126 |
| 127 // static |
| 128 NativeListboxWrapper* NativeListboxWrapper::CreateNativeWrapper( |
| 129 Listbox* listbox, |
| 130 const std::vector<string16>& strings, |
| 131 Listbox::Listener* listener) { |
| 132 return new NativeListboxWin(listbox, strings, listener); |
| 133 } |
| 134 |
| 135 } // namespace views |
OLD | NEW |