summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/tab_contents_container_gtk.cc
blob: 71c2b5f96133e4fdfda341f8a38225bd7fe93379 (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
// Copyright (c) 2009 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 "chrome/browser/gtk/tab_contents_container_gtk.h"

#include "base/gfx/native_widget_types.h"
#include "chrome/browser/tab_contents/web_contents.h"
#include "chrome/browser/renderer_host/render_widget_host_view_gtk.h"
#include "chrome/common/notification_service.h"


TabContentsContainerGtk::TabContentsContainerGtk()
    : tab_contents_(NULL),
      vbox_(gtk_vbox_new(FALSE, 0)),
      fixed_(gtk_fixed_new()),
      findbar_(NULL) {
  gtk_widget_set_size_request(fixed_, -1, 0);
  gtk_box_pack_start(GTK_BOX(vbox_), fixed_, FALSE, FALSE, 0);
  gtk_widget_show_all(vbox_);
  g_signal_connect(fixed_, "size-allocate",
                   G_CALLBACK(OnSizeAllocate), this);
}

TabContentsContainerGtk::~TabContentsContainerGtk() {
  if (tab_contents_)
    RemoveObservers();
}

void TabContentsContainerGtk::AddContainerToBox(GtkWidget* box) {
  gtk_box_pack_start(GTK_BOX(box), vbox_, TRUE, TRUE, 0);
}

void TabContentsContainerGtk::AddFindBar(GtkWidget* findbar) {
  findbar_ = findbar;
  // We will reposition it later (when we get a size-allocate event).
  gtk_fixed_put(GTK_FIXED(fixed_), findbar, 0, 0);
}

void TabContentsContainerGtk::SetTabContents(TabContents* tab_contents) {
  if (tab_contents_) {
    gfx::NativeView widget = tab_contents_->GetNativeView();
    if (widget)
      gtk_container_remove(GTK_CONTAINER(vbox_), widget);

    tab_contents_->WasHidden();

    RemoveObservers();
  }

  tab_contents_ = tab_contents;

  // When detaching the last tab of the browser SetTabContents is invoked
  // with NULL. Don't attempt to do anything in that case.
  if (tab_contents_) {
    AddObservers();

    gfx::NativeView widget = tab_contents_->GetNativeView();
    if (widget) {
      gtk_box_pack_end(GTK_BOX(vbox_), widget, TRUE, TRUE, 0);
      gtk_widget_show_all(widget);
    }
  }
}

void TabContentsContainerGtk::Observe(NotificationType type,
                                      const NotificationSource& source,
                                      const NotificationDetails& details) {
  if (type == NotificationType::RENDER_VIEW_HOST_CHANGED) {
    RenderViewHostSwitchedDetails* switched_details =
        Details<RenderViewHostSwitchedDetails>(details).ptr();
    RenderViewHostChanged(switched_details->old_host,
                          switched_details->new_host);
  } else if (type == NotificationType::TAB_CONTENTS_DESTROYED) {
    TabContentsDestroyed(Source<TabContents>(source).ptr());
  } else {
    NOTREACHED();
  }
}

void TabContentsContainerGtk::AddObservers() {
  DCHECK(tab_contents_);
  if (tab_contents_->AsWebContents()) {
    // WebContents can change their RenderViewHost and hence the GtkWidget that
    // is shown. I'm not entirely sure that we need to observe this event under
    // GTK, but am putting a stub implementation and a comment saying that if
    // we crash after that NOTIMPLEMENTED(), we'll need it.
    NotificationService::current()->AddObserver(
        this, NotificationType::RENDER_VIEW_HOST_CHANGED,
        Source<NavigationController>(&tab_contents_->controller()));
  }
  NotificationService::current()->AddObserver(
      this,
      NotificationType::TAB_CONTENTS_DESTROYED,
      Source<TabContents>(tab_contents_));
}

void TabContentsContainerGtk::RemoveObservers() {
  DCHECK(tab_contents_);
  if (tab_contents_->AsWebContents()) {
    NotificationService::current()->RemoveObserver(
        this,
        NotificationType::RENDER_VIEW_HOST_CHANGED,
        Source<NavigationController>(&tab_contents_->controller()));
  }
  NotificationService::current()->RemoveObserver(
      this,
      NotificationType::TAB_CONTENTS_DESTROYED,
      Source<TabContents>(tab_contents_));
}

void TabContentsContainerGtk::RenderViewHostChanged(RenderViewHost* old_host,
                                                    RenderViewHost* new_host) {
  // TODO(port): Remove this method and the logic where we subscribe to the
  // RENDER_VIEW_HOST_CHANGED notification. This was used on Windows for focus
  // issues, and I'm not entirely convinced that this isn't necessary.
}

void TabContentsContainerGtk::TabContentsDestroyed(TabContents* contents) {
  // Sometimes, a TabContents is destroyed before we know about it. This allows
  // us to clean up our state in case this happens.
  DCHECK(contents == tab_contents_);
  SetTabContents(NULL);
}

void TabContentsContainerGtk::OnSizeAllocate(GtkWidget* fixed,
    GtkAllocation* allocation, TabContentsContainerGtk* contents_container) {
  GtkWidget* findbar = contents_container->findbar_;
  DCHECK(findbar);
  if (!GTK_WIDGET_VISIBLE(findbar))
    return;

  // TODO(port): Logic for the positioning of the find bar should be factored
  // out of here and browser/views/* and into FindBarController.
  int xposition = allocation->width - findbar->allocation.width - 50;
  if (xposition == findbar->allocation.x) {
    return;
  } else {
    gtk_fixed_move(GTK_FIXED(fixed), findbar, xposition, 0);
  }
}