summaryrefslogtreecommitdiffstats
path: root/views/controls/listbox/native_listbox_win.cc
blob: 1c6d9859206518f08edba3ef859bb006b42728d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/controls/listbox/native_listbox_win.h"

#include <commctrl.h>
#include <windowsx.h>

#include "app/l10n_util.h"
#include "app/l10n_util_win.h"
#include "app/resource_bundle.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "base/win_util.h"
#include "gfx/font.h"
#include "views/controls/listbox/listbox.h"
#include "views/widget/widget.h"

namespace views {

////////////////////////////////////////////////////////////////////////////////
// NativeListboxWin, public:

NativeListboxWin::NativeListboxWin(Listbox* listbox,
                                   const std::vector<string16>& strings,
                                   Listbox::Listener* listener)
    : listbox_(listbox),
      strings_(strings),
      listener_(listener) {
  // Associates the actual HWND with the listbox so the listbox is the one
  // considered as having the focus (not the wrapper) when the HWND is
  // focused directly (with a click for example).
  set_focus_view(listbox);
}

NativeListboxWin::~NativeListboxWin() {
}

////////////////////////////////////////////////////////////////////////////////
// NativeListboxWin, NativeListboxWrapper implementation:

int NativeListboxWin::GetRowCount() const {
  if (!native_view())
    return 0;
  return ListBox_GetCount(native_view());
}

int NativeListboxWin::SelectedRow() const {
  if (!native_view())
    return -1;
  return ListBox_GetCurSel(native_view());
}

void NativeListboxWin::SelectRow(int row) {
  if (!native_view())
    return;
  ListBox_SetCurSel(native_view(), row);
}

View* NativeListboxWin::GetView() {
  return this;
}

////////////////////////////////////////////////////////////////////////////////
// NativeListboxWin, View overrides:

gfx::Size NativeListboxWin::GetPreferredSize() {
  SIZE sz = {0};
  SendMessage(native_view(), BCM_GETIDEALSIZE, 0,
              reinterpret_cast<LPARAM>(&sz));

  return gfx::Size(sz.cx, sz.cy);
}

////////////////////////////////////////////////////////////////////////////////
// NativeListboxWin, NativeControlWin overrides:

bool NativeListboxWin::ProcessMessage(UINT message, WPARAM w_param,
                                      LPARAM l_param, LRESULT* result) {
  if (message == WM_COMMAND) {
    switch (HIWORD(w_param)) {
      case LBN_SELCHANGE:
        if (listener_)
          listener_->ListboxSelectionChanged(listbox_);
        return true;
      default:
        break;
    }
  }

  return NativeControlWin::ProcessMessage(message, w_param, l_param, result);
}

////////////////////////////////////////////////////////////////////////////////
// NativeListboxWin, protected:

void NativeListboxWin::CreateNativeControl() {
  int style = WS_CHILD | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY;
  // If there's only one column and the title string is empty, don't show a
  // header.
  HWND hwnd = ::CreateWindowEx(WS_EX_CLIENTEDGE | GetAdditionalRTLStyle(),
                               WC_LISTBOX,
                               L"",
                               style,
                               0, 0, width(), height(),
                               listbox_->GetWidget()->GetNativeView(),
                               NULL, NULL, NULL);
  HFONT font = ResourceBundle::GetSharedInstance().
      GetFont(ResourceBundle::BaseFont).hfont();
  SendMessage(hwnd, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE);
  l10n_util::AdjustUIFontForWindow(hwnd);

  for (size_t i = 0; i < strings_.size(); ++i)
    ListBox_AddString(hwnd, UTF16ToWide(strings_[i]).c_str());

  NativeControlCreated(hwnd);

  // Bug 964884: detach the IME attached to this window.
  // We should attach IMEs only when we need to input CJK strings.
  ::ImmAssociateContextEx(hwnd, NULL, 0);
}

////////////////////////////////////////////////////////////////////////////////
// NativeListboxWrapper, public:

// static
NativeListboxWrapper* NativeListboxWrapper::CreateNativeWrapper(
    Listbox* listbox,
    const std::vector<string16>& strings,
    Listbox::Listener* listener) {
  return new NativeListboxWin(listbox, strings, listener);
}

}  // namespace views