summaryrefslogtreecommitdiffstats
path: root/views/window/native_window_gtk.cc
blob: 7d238d43a48ab2ba0a2cfd1bfae0e6e54ce878a2 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) 2011 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/window/native_window_gtk.h"

#include "base/i18n/rtl.h"
#include "base/utf_string_conversions.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/path.h"
#include "ui/gfx/rect.h"
#include "views/events/event.h"
#include "views/window/hit_test.h"
#include "views/window/native_window_delegate.h"
#include "views/window/non_client_view.h"
#include "views/window/window_delegate.h"

namespace {

// Converts a Windows-style hit test result code into a GDK window edge.
GdkWindowEdge HitTestCodeToGDKWindowEdge(int hittest_code) {
  switch (hittest_code) {
    case HTBOTTOM:
      return GDK_WINDOW_EDGE_SOUTH;
    case HTBOTTOMLEFT:
      return GDK_WINDOW_EDGE_SOUTH_WEST;
    case HTBOTTOMRIGHT:
    case HTGROWBOX:
      return GDK_WINDOW_EDGE_SOUTH_EAST;
    case HTLEFT:
      return GDK_WINDOW_EDGE_WEST;
    case HTRIGHT:
      return GDK_WINDOW_EDGE_EAST;
    case HTTOP:
      return GDK_WINDOW_EDGE_NORTH;
    case HTTOPLEFT:
      return GDK_WINDOW_EDGE_NORTH_WEST;
    case HTTOPRIGHT:
      return GDK_WINDOW_EDGE_NORTH_EAST;
    default:
      NOTREACHED();
      break;
  }
  // Default to something defaultish.
  return HitTestCodeToGDKWindowEdge(HTGROWBOX);
}

// Converts a Windows-style hit test result code into a GDK cursor type.
GdkCursorType HitTestCodeToGdkCursorType(int hittest_code) {
  switch (hittest_code) {
    case HTBOTTOM:
      return GDK_BOTTOM_SIDE;
    case HTBOTTOMLEFT:
      return GDK_BOTTOM_LEFT_CORNER;
    case HTBOTTOMRIGHT:
    case HTGROWBOX:
      return GDK_BOTTOM_RIGHT_CORNER;
    case HTLEFT:
      return GDK_LEFT_SIDE;
    case HTRIGHT:
      return GDK_RIGHT_SIDE;
    case HTTOP:
      return GDK_TOP_SIDE;
    case HTTOPLEFT:
      return GDK_TOP_LEFT_CORNER;
    case HTTOPRIGHT:
      return GDK_TOP_RIGHT_CORNER;
    default:
      break;
  }
  // Default to something defaultish.
  return GDK_LEFT_PTR;
}

}  // namespace

namespace views {

NativeWindowGtk::NativeWindowGtk(internal::NativeWindowDelegate* delegate)
    : NativeWidgetGtk(delegate->AsNativeWidgetDelegate()),
      delegate_(delegate),
      window_closed_(false) {
  is_window_ = true;
}

NativeWindowGtk::~NativeWindowGtk() {
}

////////////////////////////////////////////////////////////////////////////////
// NativeWindowGtk, NativeWidgetGtk overrides:

gboolean NativeWindowGtk::OnButtonPress(GtkWidget* widget,
                                        GdkEventButton* event) {
  GdkEventButton transformed_event = *event;
  MouseEvent mouse_event(TransformEvent(&transformed_event));

  int hittest_code =
      GetWindow()->non_client_view()->NonClientHitTest(mouse_event.location());
  switch (hittest_code) {
    case HTCAPTION: {
      // Start dragging if the mouse event is a single click and *not* a right
      // click. If it is a right click, then pass it through to
      // NativeWidgetGtk::OnButtonPress so that View class can show ContextMenu
      // upon a mouse release event. We only start drag on single clicks as we
      // get a crash in Gtk on double/triple clicks.
      if (event->type == GDK_BUTTON_PRESS &&
          !mouse_event.IsOnlyRightMouseButton()) {
        gfx::Point screen_point(event->x, event->y);
        View::ConvertPointToScreen(GetWindow()->GetRootView(), &screen_point);
        gtk_window_begin_move_drag(GetNativeWindow(), event->button,
                                   screen_point.x(), screen_point.y(),
                                   event->time);
        return TRUE;
      }
      break;
    }
    case HTBOTTOM:
    case HTBOTTOMLEFT:
    case HTBOTTOMRIGHT:
    case HTGROWBOX:
    case HTLEFT:
    case HTRIGHT:
    case HTTOP:
    case HTTOPLEFT:
    case HTTOPRIGHT: {
      gfx::Point screen_point(event->x, event->y);
      View::ConvertPointToScreen(GetWindow()->GetRootView(), &screen_point);
      // TODO(beng): figure out how to get a good minimum size.
      gtk_widget_set_size_request(GetNativeView(), 100, 100);
      gtk_window_begin_resize_drag(GetNativeWindow(),
                                   HitTestCodeToGDKWindowEdge(hittest_code),
                                   event->button, screen_point.x(),
                                   screen_point.y(), event->time);
      return TRUE;
    }
    default:
      // Everything else falls into standard client event handling...
      break;
  }
  return NativeWidgetGtk::OnButtonPress(widget, event);
}

gboolean NativeWindowGtk::OnConfigureEvent(GtkWidget* widget,
                                           GdkEventConfigure* event) {
  SaveWindowPosition();
  return FALSE;
}

gboolean NativeWindowGtk::OnMotionNotify(GtkWidget* widget,
                                         GdkEventMotion* event) {
  GdkEventMotion transformed_event = *event;
  TransformEvent(&transformed_event);
  gfx::Point translated_location(transformed_event.x, transformed_event.y);

  // Update the cursor for the screen edge.
  int hittest_code =
      GetWindow()->non_client_view()->NonClientHitTest(translated_location);
  if (hittest_code != HTCLIENT) {
    GdkCursorType cursor_type = HitTestCodeToGdkCursorType(hittest_code);
    gdk_window_set_cursor(widget->window, gfx::GetCursor(cursor_type));
  }

  return NativeWidgetGtk::OnMotionNotify(widget, event);
}

void NativeWindowGtk::OnSizeAllocate(GtkWidget* widget,
                                     GtkAllocation* allocation) {
  NativeWidgetGtk::OnSizeAllocate(widget, allocation);

  // The Window's NonClientView may provide a custom shape for the Window.
  gfx::Path window_mask;
  GetWindow()->non_client_view()->GetWindowMask(gfx::Size(allocation->width,
                                                          allocation->height),
                                                &window_mask);
  GdkRegion* mask_region = window_mask.CreateNativeRegion();
  gdk_window_shape_combine_region(GetNativeView()->window, mask_region, 0, 0);
  if (mask_region)
    gdk_region_destroy(mask_region);

  SaveWindowPosition();
}

gboolean NativeWindowGtk::OnLeaveNotify(GtkWidget* widget,
                                        GdkEventCrossing* event) {
  gdk_window_set_cursor(widget->window, gfx::GetCursor(GDK_LEFT_PTR));

  return NativeWidgetGtk::OnLeaveNotify(widget, event);
}

void NativeWindowGtk::IsActiveChanged() {
  NativeWidgetGtk::IsActiveChanged();
  delegate_->OnNativeWindowActivationChanged(IsActive());
}

void NativeWindowGtk::InitNativeWidget(const Widget::InitParams& params) {
  NativeWidgetGtk::InitNativeWidget(params);

  g_signal_connect(G_OBJECT(GetNativeWindow()), "configure-event",
                   G_CALLBACK(CallConfigureEvent), this);
}

////////////////////////////////////////////////////////////////////////////////
// NativeWindowGtk, NativeWindow implementation:

NativeWidget* NativeWindowGtk::AsNativeWidget() {
  return this;
}

const NativeWidget* NativeWindowGtk::AsNativeWidget() const {
  return this;
}

gfx::Rect NativeWindowGtk::GetRestoredBounds() const {
  // We currently don't support tiling, so this doesn't matter.
  return GetWindowScreenBounds();
}

void NativeWindowGtk::ShowNativeWindow(ShowState state) {
  // No concept of maximization (yet) on ChromeOS.
  if (state == NativeWindow::SHOW_INACTIVE)
    gtk_window_set_focus_on_map(GetNativeWindow(), false);
  gtk_widget_show(GetNativeView());
}

void NativeWindowGtk::BecomeModal() {
  gtk_window_set_modal(GetNativeWindow(), true);
}

void NativeWindowGtk::EnableClose(bool enable) {
  gtk_window_set_deletable(GetNativeWindow(), enable);
}

Window* NativeWindowGtk::GetWindow() {
  return delegate_->AsWindow();
}

const Window* NativeWindowGtk::GetWindow() const {
  return delegate_->AsWindow();
}

NonClientFrameView* NativeWindowGtk::CreateFrameViewForWindow() {
  return NULL;
}

void NativeWindowGtk::UpdateFrameAfterFrameChange() {
  // We currently don't support different frame types on Gtk, so we don't
  // need to implement this.
  NOTIMPLEMENTED();
}

bool NativeWindowGtk::ShouldUseNativeFrame() const {
  return false;
}

void NativeWindowGtk::FrameTypeChanged() {
  // This is called when the Theme has changed, so forward the event to the root
  // widget.
  GetWidget()->ThemeChanged();
  GetWidget()->GetRootView()->SchedulePaint();
}

////////////////////////////////////////////////////////////////////////////////
// NativeWindowGtk, NativeWidgetGtk overrides:

void NativeWindowGtk::Restore() {
  if (IsFullscreen())
    SetFullscreen(false);
  else
    NativeWidgetGtk::Restore();
}

gboolean NativeWindowGtk::OnWindowStateEvent(GtkWidget* widget,
                                             GdkEventWindowState* event) {
  if (!(event->new_window_state & GDK_WINDOW_STATE_WITHDRAWN))
    SaveWindowPosition();
  return NativeWidgetGtk::OnWindowStateEvent(widget, event);
}

////////////////////////////////////////////////////////////////////////////////
// NativeWindowGtk, private:

// static
gboolean NativeWindowGtk::CallConfigureEvent(GtkWidget* widget,
                                             GdkEventConfigure* event,
                                             NativeWindowGtk* window_gtk) {
  return window_gtk->OnConfigureEvent(widget, event);
}

void NativeWindowGtk::SaveWindowPosition() {
  // The delegate may have gone away on us.
  if (!GetWindow()->window_delegate())
    return;

  bool maximized = window_state_ & GDK_WINDOW_STATE_MAXIMIZED;
  GetWindow()->window_delegate()->SaveWindowPlacement(
      GetWidget()->GetWindowScreenBounds(),
      maximized);
}

void NativeWindowGtk::OnDestroy(GtkWidget* widget) {
  delegate_->OnNativeWindowDestroying();
  NativeWidgetGtk::OnDestroy(widget);
}

void NativeWindowGtk::OnDestroyed(GObject *where_the_object_was) {
  delegate_->OnNativeWindowDestroyed();
  NativeWidgetGtk::OnDestroyed(where_the_object_was);
}

////////////////////////////////////////////////////////////////////////////////
// NativeWindow, public:

// static
NativeWindow* NativeWindow::CreateNativeWindow(
    internal::NativeWindowDelegate* delegate) {
  return new NativeWindowGtk(delegate);
}

}  // namespace views