summaryrefslogtreecommitdiffstats
path: root/lib/shell_qt.cpp
blob: c21bd487a595b87218f790b098c49dd0e7004d29 (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
// Copyright (c) 2012 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 "content/shell/shell.h"

#include <gdk/gdkkeysyms.h>

#include "base/logging.h"
#include "base/strings/string_piece.h"
#include "base/utf_string_conversions.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/renderer_preferences.h"
#include "content/shell/shell_browser_context.h"
#include "content/shell/shell_content_browser_client.h"
#include "raster_window.h"
#include "signal_connector.h"

#include "web_contents_view_qt.h"

#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QToolButton>
#include <QWidget>
#include <QWindow>

namespace content {

void Shell::PlatformInitialize(const gfx::Size& default_window_size)
{
}

void Shell::PlatformCleanUp()
{
}

void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled)
{
}

void Shell::PlatformSetAddressBarURL(const GURL& url)
{
  if (headless_)
    return;

  fprintf(stderr, "Set Address to: %s\n", url.spec().c_str());

  QLineEdit* addressLine = reinterpret_cast<QWidget*>(window_)->findChild<QLineEdit*>("AddressLineEdit");
  addressLine->setText(QString::fromStdString(url.spec()));
}


void Shell::PlatformSetIsLoading(bool loading)
{
    // FIXME: we might want to emit some loadStarted signal here or something...
}

void Shell::PlatformCreateWindow(int width, int height) {
  SizeTo(width, height);

  if (headless_)
    return;

  if (!window_) {

    // Use oxygen as a fallback.
    if (QIcon::themeName().isEmpty())
      QIcon::setThemeName("oxygen");

    QWidget* window = new QWidget;
    window_ = reinterpret_cast<gfx::NativeWindow>(window);

    window->setGeometry(100,100, width, height);

    QVBoxLayout* layout = new QVBoxLayout;

    // Create a widget based address bar.
    QHBoxLayout* addressBar = new QHBoxLayout;

    int buttonWidth = 26;
    QToolButton* backButton = new QToolButton;
    backButton->setIcon(QIcon::fromTheme("go-previous"));
    backButton->setObjectName("BackButton");
    addressBar->addWidget(backButton);

    QToolButton* forwardButton = new QToolButton;
    forwardButton->setIcon(QIcon::fromTheme("go-next"));
    forwardButton->setObjectName("ForwardButton");
    addressBar->addWidget(forwardButton);

    QToolButton* reloadButton = new QToolButton;
    reloadButton->setIcon(QIcon::fromTheme("view-refresh"));
    reloadButton->setObjectName("ReloadButton");
    addressBar->addWidget(reloadButton);

    QLineEdit* lineEdit =  new QLineEdit;
    lineEdit->setObjectName("AddressLineEdit");
    addressBar->addWidget(lineEdit);

    layout->addLayout(addressBar);


    window->setLayout(layout);
    window->show();

    // SignalConnector will act as a proxy for the QObject signals received from
    // m_window. m_window will take ownership of the SignalConnector.
    // The SignalConnector will search the children list of m_window
    // for back/forward/reload buttons and for the address line edit.
    // Therefore the layout must be set and completed before the SignalConnector
    // is created.
    SignalConnector* signalConnector = new SignalConnector(this, window);
  }
}

void Shell::PlatformSetContents()
{
    if (headless_)
    return;

    content::RendererPreferences* rendererPrefs = web_contents_->GetMutableRendererPrefs();
    rendererPrefs->use_custom_colors = true;
    // Qt returns a flash time (the whole cycle) in ms, chromium expects just the interval in seconds
    rendererPrefs->caret_blink_interval = static_cast<double>(qApp->cursorFlashTime())/2000;
    web_contents_->GetRenderViewHost()->SyncRendererPrefs();

    WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(web_contents_->GetView());
    content_view->setWindowContainer(static_cast<void*>(new RasterWindowContainer));
    QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(reinterpret_cast<QWidget*>(window_)->layout());
    if (layout)
        layout->addLayout(static_cast<RasterWindowContainer*>(content_view->windowContainer()));
}

void Shell::SizeTo(int width, int height)
{
  QT_NOT_YET_IMPLEMENTED
}

void Shell::PlatformResizeSubViews()
{
    SizeTo(content_width_, content_height_);
}

void Shell::Close()
{
  if (headless_) {
    delete this;
    return;
  }
}

void Shell::OnBackButtonClicked(GtkWidget* widget) { }

void Shell::OnForwardButtonClicked(GtkWidget* widget) { }

void Shell::OnReloadButtonClicked(GtkWidget* widget) { }

void Shell::OnStopButtonClicked(GtkWidget* widget)
{
  Stop();
}

void Shell::OnURLEntryActivate(GtkWidget* entry) { }

// Callback for when the main window is destroyed.
gboolean Shell::OnWindowDestroyed(GtkWidget* window)
{
  delete this;
  return FALSE;  // Don't stop this message.
}

gboolean Shell::OnCloseWindowKeyPressed(GtkAccelGroup* accel_group, GObject* acceleratable, guint keyval, GdkModifierType modifier)
{
    QT_NOT_YET_IMPLEMENTED
    return TRUE;
}

gboolean Shell::OnNewWindowKeyPressed(GtkAccelGroup* accel_group, GObject* acceleratable, guint keyval, GdkModifierType modifier)
{
  ShellBrowserContext* browser_context = ShellContentBrowserClient::Get()->browser_context();
  Shell::CreateNewWindow(browser_context, GURL(), NULL, MSG_ROUTING_NONE, gfx::Size());
  return TRUE;
}

gboolean Shell::OnHighlightURLView(GtkAccelGroup* accel_group, GObject* acceleratable, guint keyval, GdkModifierType modifier)
{
  return TRUE;
}

void Shell::PlatformSetTitle(const string16& title)
{
  if (headless_)
    return;

  std::string title_utf8 = UTF16ToUTF8(title);
}

}  // namespace content