1// Copyright 2010-2018, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include <Carbon/Carbon.h>
31#include <Cocoa/Cocoa.h>
32#include <objc/message.h>
33
34#include "base/coordinates.h"
35#include "base/logging.h"
36#include "base/mac_util.h"
37#include "base/port.h"
38#include "protocol/commands.pb.h"
39#include "renderer/mac/RendererBaseWindow.h"
40
41
42namespace mozc {
43namespace renderer{
44namespace mac {
45
46RendererBaseWindow::RendererBaseWindow()
47    : window_level_(NSPopUpMenuWindowLevel) {
48}
49
50void RendererBaseWindow::InitWindow() {
51  if (window_.get()) {
52    LOG(ERROR) << "window is already initialized.";
53    return;
54  }
55  const NSUInteger style_mask =
56      NSUtilityWindowMask | NSDocModalWindowMask | NSNonactivatingPanelMask;
57  window_.reset([[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, 1, 1)
58                                           styleMask:style_mask
59                                             backing:NSBackingStoreBuffered
60                                               defer:YES]);
61  ResetView();
62  [window_.get() setContentView:view_.get()];
63  [window_.get() setDisplaysWhenScreenProfileChanges:YES];
64  [window_.get() makeKeyAndOrderFront:nil];
65  [window_.get() setFloatingPanel:YES];
66  [window_.get() setWorksWhenModal:YES];
67  [window_.get() setBackgroundColor:NSColor.whiteColor];
68  [window_.get() setReleasedWhenClosed:NO];
69  [window_.get() setLevel:window_level_];
70  [window_.get() orderOut:window_.get()];
71}
72
73RendererBaseWindow::~RendererBaseWindow() {
74  [window_.get() close];
75  window_.reset();
76}
77
78Size RendererBaseWindow::GetWindowSize() const {
79  if (!window_.get()) {
80    return Size(0, 0);
81  }
82  NSRect rect = [window_.get() frame];
83  return Size(rect.size.width, rect.size.height);
84}
85
86void RendererBaseWindow::Hide() {
87  if (window_.get()) {
88    [window_.get() orderOut:window_.get()];
89  }
90}
91
92void RendererBaseWindow::Show() {
93  if (!window_.get()) {
94    InitWindow();
95  }
96  [window_.get() orderFront:window_.get()];
97}
98
99bool RendererBaseWindow::IsVisible() {
100  if (!window_) {
101    return false;
102  }
103  return ([window_.get() isVisible] == YES);
104}
105
106void RendererBaseWindow::ResetView() {
107  view_.reset([[NSView alloc] initWithFrame:NSMakeRect(0, 0, 1, 1)]);
108}
109
110void RendererBaseWindow::MoveWindow(const NSPoint &point) {
111  NSRect rect = [window_.get() frame];
112  rect.origin.x = point.x;
113  rect.origin.y = point.y;
114  [window_.get() setFrame:rect display:FALSE];
115}
116
117void RendererBaseWindow::ResizeWindow(int32 width, int32 height) {
118  NSRect rect = [window_.get() frame];
119  rect.size.width = width;
120  rect.size.height = height;
121  [window_.get() setFrame:rect display:FALSE];
122}
123
124void RendererBaseWindow::SetWindowLevel(NSInteger window_level) {
125  if (window_level_ != window_level) {
126    window_level_ = window_level;
127    [window_.get() setLevel:window_level_];
128  }
129}
130
131}  // namespace mozc::renderer::mac
132}  // namespace mozc::renderer
133}  // namespace mozc
134