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#ifndef UI_VIEWS_CONTROLS_NATIVE_CONTROL_H_
6#define UI_VIEWS_CONTROLS_NATIVE_CONTROL_H_
7
8#include <windows.h>
9
10#include "ui/base/keycodes/keyboard_codes.h"
11#include "ui/views/view.h"
12
13namespace views {
14
15class NativeViewHost;
16class NativeControlContainer;
17
18////////////////////////////////////////////////////////////////////////////////
19//
20// NativeControl is an abstract view that is used to implement views wrapping
21// native controls. Subclasses can simply implement CreateNativeControl() to
22// wrap a new kind of control
23//
24////////////////////////////////////////////////////////////////////////////////
25class VIEWS_EXPORT NativeControl : public View {
26 public:
27   enum Alignment {
28     LEADING = 0,
29     CENTER,
30     TRAILING };
31
32  NativeControl();
33  virtual ~NativeControl();
34
35  virtual void ViewHierarchyChanged(
36      const ViewHierarchyChangedDetails& details) OVERRIDE;
37  virtual void Layout();
38
39  // Overridden to properly set the native control state.
40  virtual void SetVisible(bool f);
41  virtual void OnEnabledChanged();
42
43  // Overridden to do nothing.
44  virtual void OnPaint(gfx::Canvas* canvas);
45
46 protected:
47  friend class NativeControlContainer;
48
49  // Overridden by sub-classes to create the windows control which is wrapped
50  virtual HWND CreateNativeControl(HWND parent_container) = 0;
51
52  // Invoked when the native control sends a WM_NOTIFY message to its parent
53  virtual LRESULT OnNotify(int w_param, LPNMHDR l_param) = 0;
54
55  // Invoked when the native control sends a WM_COMMAND message to its parent
56  virtual LRESULT OnCommand(UINT code, int id, HWND source) { return 0; }
57
58  // Invoked when the appropriate gesture for a context menu is issued.
59  virtual void OnContextMenu(const POINT& location);
60
61  // Overridden so to set the native focus to the native control.
62  virtual void OnFocus();
63
64  // Invoked when the native control sends a WM_DESTORY message to its parent.
65  virtual void OnDestroy() { }
66
67  // Return the native control
68  virtual HWND GetNativeControlHWND();
69
70  // Invoked by the native windows control when it has been destroyed. This is
71  // invoked AFTER WM_DESTORY has been sent. Any window commands send to the
72  // HWND will most likely fail.
73  void NativeControlDestroyed();
74
75  // Overridden so that the control properly reflects parent's visibility.
76  virtual void VisibilityChanged(View* starting_from, bool is_visible);
77
78  // Controls that have fixed sizes should call these methods to specify the
79  // actual size and how they should be aligned within their parent.
80  void SetFixedWidth(int width, Alignment alignment);
81  void SetFixedHeight(int height, Alignment alignment);
82
83  // Invoked when a key is pressed on the control.
84  // Should return true if the key message was processed, false otherwise.
85  virtual bool OnKeyDown(ui::KeyboardCode virtual_key_code) { return false; }
86
87  // Returns additional extended style flags. When subclasses call
88  // CreateWindowEx in order to create the underlying control, they must OR the
89  // ExStyle parameter with the value returned by this function.
90  //
91  // We currently use this method in order to add flags such as WS_EX_LAYOUTRTL
92  // to the HWND for views with right-to-left UI layout.
93  DWORD GetAdditionalExStyle() const;
94
95  // TODO(xji): we use the following temporary function as we transition the
96  // various native controls to use the right set of RTL flags. This function
97  // will go away (and be replaced by GetAdditionalExStyle()) once all the
98  // controls are properly transitioned.
99  DWORD GetAdditionalRTLStyle() const;
100
101  // This variable is protected to provide subclassers direct access. However
102  // subclassers should always check for NULL since this variable is only
103  // initialized in ValidateNativeControl().
104  NativeViewHost* hwnd_view_;
105
106  // Fixed size information.  -1 for a size means no fixed size.
107  int fixed_width_;
108  Alignment horizontal_alignment_;
109  int fixed_height_;
110  Alignment vertical_alignment_;
111
112 private:
113
114  void ValidateNativeControl();
115
116  static LRESULT CALLBACK NativeControlWndProc(HWND window, UINT message,
117                                               WPARAM w_param, LPARAM l_param);
118
119  NativeControlContainer* container_;
120
121  DISALLOW_COPY_AND_ASSIGN(NativeControl);
122};
123
124}  // namespace views
125
126#endif  // UI_VIEWS_CONTROLS_NATIVE_CONTROL_H_
127