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.
5 #ifndef UI_VIEWS_CONTROLS_NATIVE_CONTROL_H_
6 #define UI_VIEWS_CONTROLS_NATIVE_CONTROL_H_
10 #include "ui/events/keycodes/keyboard_codes.h"
11 #include "ui/views/view.h"
16 class NativeControlContainer;
18 ////////////////////////////////////////////////////////////////////////////////
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
24 ////////////////////////////////////////////////////////////////////////////////
25 class VIEWS_EXPORT NativeControl : public View {
33 virtual ~NativeControl();
35 virtual void ViewHierarchyChanged(
36 const ViewHierarchyChangedDetails& details) OVERRIDE;
37 virtual void Layout();
39 // Overridden to properly set the native control state.
40 virtual void SetVisible(bool f);
41 virtual void OnEnabledChanged();
43 // Overridden to do nothing.
44 virtual void OnPaint(gfx::Canvas* canvas);
47 friend class NativeControlContainer;
49 // Overridden by sub-classes to create the windows control which is wrapped
50 virtual HWND CreateNativeControl(HWND parent_container) = 0;
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;
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; }
58 // Invoked when the appropriate gesture for a context menu is issued.
59 virtual void OnContextMenu(const POINT& location);
61 // Overridden so to set the native focus to the native control.
62 virtual void OnFocus();
64 // Invoked when the native control sends a WM_DESTORY message to its parent.
65 virtual void OnDestroy() { }
67 // Return the native control
68 virtual HWND GetNativeControlHWND();
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();
75 // Overridden so that the control properly reflects parent's visibility.
76 virtual void VisibilityChanged(View* starting_from, bool is_visible);
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);
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; }
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.
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;
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;
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_;
106 // Fixed size information. -1 for a size means no fixed size.
108 Alignment horizontal_alignment_;
110 Alignment vertical_alignment_;
114 void ValidateNativeControl();
116 static LRESULT CALLBACK NativeControlWndProc(HWND window, UINT message,
117 WPARAM w_param, LPARAM l_param);
119 NativeControlContainer* container_;
121 DISALLOW_COPY_AND_ASSIGN(NativeControl);
126 #endif // UI_VIEWS_CONTROLS_NATIVE_CONTROL_H_