blob: 5628142ac42dfc8f411882433571c4b8865de2fb [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#include <assert.h>
21#include <stdio.h>
22#include <string.h>
23
Pierre Ossman4ae229f2011-04-15 12:58:31 +000024#include <FL/Fl_Scroll.H>
25
Pierre Ossman5156d5e2011-03-09 09:42:34 +000026#include <rfb/LogWriter.h>
27
28#include "DesktopWindow.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000029#include "i18n.h"
Pierre Ossman89f868a2011-04-11 11:59:31 +000030
Pierre Ossman5156d5e2011-03-09 09:42:34 +000031using namespace rfb;
32
33extern void exit_vncviewer();
34
35static rfb::LogWriter vlog("DesktopWindow");
36
37DesktopWindow::DesktopWindow(int w, int h, const char *name,
38 const rfb::PixelFormat& serverPF,
39 CConn* cc_)
Pierre Ossmand50b3d12011-04-15 07:46:56 +000040 : Fl_Window(w, h)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000041{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000042 // Allow resize
Pierre Ossman343e2e02011-04-15 13:00:12 +000043 size_range(100, 100, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000044
45 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
46 scroll->color(FL_BLACK);
47
48 // Automatically adjust the scroll box to the window
49 resizable(scroll);
50
Pierre Ossmand50b3d12011-04-15 07:46:56 +000051 viewport = new Viewport(w, h, serverPF, cc_);
52
Pierre Ossman4ae229f2011-04-15 12:58:31 +000053 scroll->end();
54
Pierre Ossman5156d5e2011-03-09 09:42:34 +000055 callback(handleClose, this);
56
57 setName(name);
58
Pierre Ossman5156d5e2011-03-09 09:42:34 +000059 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +000060
61 // The window manager might give us an initial window size that is different
62 // than the one we requested, and in those cases we need to manually adjust
63 // the scroll widget for things to behave sanely.
64 if ((w != this->w()) || (h != this->h()))
65 scroll->size(this->w(), this->h());
Pierre Ossman5156d5e2011-03-09 09:42:34 +000066}
67
68
69DesktopWindow::~DesktopWindow()
70{
Pierre Ossmand50b3d12011-04-15 07:46:56 +000071 // FLTK automatically deletes all child widgets, so we shouldn't touch
72 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +000073}
74
75
76void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
77{
Pierre Ossmand50b3d12011-04-15 07:46:56 +000078 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +000079}
80
81
82const rfb::PixelFormat &DesktopWindow::getPreferredPF()
83{
Pierre Ossmand50b3d12011-04-15 07:46:56 +000084 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +000085}
86
87
88// Cursor stuff
89
90void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
91 void* data, void* mask)
92{
93}
94
95
96void DesktopWindow::setName(const char *name)
97{
98 CharArray windowNameStr;
99 windowNameStr.replaceBuf(new char[256]);
100
101 snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), name);
102
103 copy_label(windowNameStr.buf);
104}
105
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000106
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000107void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
108 rdr::U16* rgbs)
109{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000110 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000111}
112
113
114// Copy the areas of the framebuffer that have been changed (damaged)
115// to the displayed window.
116
117void DesktopWindow::updateWindow()
118{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000119 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000120}
121
122
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000123void DesktopWindow::resize(int x, int y, int w, int h)
124{
125 // Deal with some scrolling corner cases:
126 //
127 // a) If the window is larger then the viewport, center the viewport.
128 // b) If the window is smaller than the viewport, make sure there is
129 // no wasted space on the sides.
130 //
131 // FIXME: Doesn't compensate for scroll widget size properly.
132 if (w > viewport->w())
133 viewport->position((w - viewport->w()) / 2, viewport->y());
134 else {
135 if (viewport->x() > 0)
136 viewport->position(0, viewport->y());
137 else if (w > (viewport->x() + viewport->w()))
138 viewport->position(w - viewport->w(), viewport->y());
139 }
140
141 // Same thing for y axis
142 if (h > viewport->h())
143 viewport->position(viewport->x(), (h - viewport->h()) / 2);
144 else {
145 if (viewport->y() > 0)
146 viewport->position(viewport->x(), 0);
147 else if (h > (viewport->y() + viewport->h()))
148 viewport->position(viewport->x(), h - viewport->h());
149 }
150
151 Fl_Window::resize(x, y, w, h);
152}
153
154
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000155void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
156{
157 exit_vncviewer();
158}