Mercurial > hg > Applications > tvnjviewer
comparison src/viewer_swing/java/com/glavsoft/viewer/UiSettings.java @ 0:daa24f8a557b
TightVNC original
author | YU |
---|---|
date | Thu, 11 Sep 2014 07:30:03 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:daa24f8a557b |
---|---|
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC. | |
2 // All rights reserved. | |
3 // | |
4 //------------------------------------------------------------------------- | |
5 // This file is part of the TightVNC software. Please visit our Web site: | |
6 // | |
7 // http://www.tightvnc.com/ | |
8 // | |
9 // This program is free software; you can redistribute it and/or modify | |
10 // it under the terms of the GNU General Public License as published by | |
11 // the Free Software Foundation; either version 2 of the License, or | |
12 // (at your option) any later version. | |
13 // | |
14 // This program is distributed in the hope that it will be useful, | |
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 // GNU General Public License for more details. | |
18 // | |
19 // You should have received a copy of the GNU General Public License along | |
20 // with this program; if not, write to the Free Software Foundation, Inc., | |
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 //------------------------------------------------------------------------- | |
23 // | |
24 | |
25 package com.glavsoft.viewer; | |
26 | |
27 import com.glavsoft.core.SettingsChangedEvent; | |
28 import com.glavsoft.rfb.IChangeSettingsListener; | |
29 import com.glavsoft.viewer.swing.LocalMouseCursorShape; | |
30 | |
31 import java.text.DecimalFormat; | |
32 import java.text.NumberFormat; | |
33 import java.util.LinkedList; | |
34 import java.util.List; | |
35 | |
36 /** | |
37 * @author dime at tightvnc.com | |
38 */ | |
39 public class UiSettings { | |
40 | |
41 public static final int MIN_SCALE_PERCENT = 10; | |
42 public static final int MAX_SCALE_PERCENT = 500; | |
43 private static final int SCALE_PERCENT_ZOOMING_STEP = 10; | |
44 | |
45 @SuppressWarnings("PointlessBitwiseExpression") | |
46 public static final int CHANGED_SCALE_FACTOR = 1 << 0; | |
47 public static final int CHANGED_MOUSE_CURSOR_SHAPE = 1 << 1; | |
48 public static final int CHANGED_FULL_SCREEN = 1 << 2; | |
49 | |
50 private final List<IChangeSettingsListener> listeners = new LinkedList<IChangeSettingsListener>(); | |
51 private int changedSettingsMask = 0; | |
52 | |
53 private final UiSettingsData uiSettingsData; | |
54 public boolean showControls = true; | |
55 | |
56 public UiSettings() { | |
57 uiSettingsData = new UiSettingsData(); | |
58 changedSettingsMask = 0; | |
59 } | |
60 | |
61 public UiSettings(UiSettings uiSettings) { | |
62 uiSettingsData = new UiSettingsData( | |
63 uiSettings.getScalePercent(), uiSettings.getMouseCursorShape(), uiSettings.isFullScreen()); | |
64 this.changedSettingsMask = uiSettings.changedSettingsMask; | |
65 } | |
66 | |
67 public double getScaleFactor() { | |
68 return uiSettingsData.getScalePercent() / 100.; | |
69 } | |
70 | |
71 public void setScalePercent(double scalePercent) { | |
72 if (this.uiSettingsData.setScalePercent(scalePercent)) { | |
73 changedSettingsMask |= CHANGED_SCALE_FACTOR; | |
74 } | |
75 } | |
76 | |
77 public void addListener(IChangeSettingsListener listener) { | |
78 listeners.add(listener); | |
79 } | |
80 | |
81 void fireListeners() { | |
82 if (null == listeners) return; | |
83 final SettingsChangedEvent event = new SettingsChangedEvent(new UiSettings(this)); | |
84 changedSettingsMask = 0; | |
85 for (IChangeSettingsListener listener : listeners) { | |
86 listener.settingsChanged(event); | |
87 } | |
88 } | |
89 | |
90 public void zoomOut() { | |
91 double oldScaleFactor = uiSettingsData.getScalePercent(); | |
92 double scaleFactor = (int)(this.uiSettingsData.getScalePercent() / SCALE_PERCENT_ZOOMING_STEP) * SCALE_PERCENT_ZOOMING_STEP; | |
93 if (scaleFactor == oldScaleFactor) { | |
94 scaleFactor -= SCALE_PERCENT_ZOOMING_STEP; | |
95 } | |
96 if (scaleFactor < MIN_SCALE_PERCENT) { | |
97 scaleFactor = MIN_SCALE_PERCENT; | |
98 } | |
99 setScalePercent(scaleFactor); | |
100 fireListeners(); | |
101 } | |
102 | |
103 public void zoomIn() { | |
104 double scaleFactor = (int)(this.uiSettingsData.getScalePercent() / SCALE_PERCENT_ZOOMING_STEP) * SCALE_PERCENT_ZOOMING_STEP + SCALE_PERCENT_ZOOMING_STEP; | |
105 if (scaleFactor > MAX_SCALE_PERCENT) { | |
106 scaleFactor = MAX_SCALE_PERCENT; | |
107 } | |
108 setScalePercent(scaleFactor); | |
109 fireListeners(); | |
110 } | |
111 | |
112 public void zoomAsIs() { | |
113 setScalePercent(100); | |
114 fireListeners(); | |
115 } | |
116 | |
117 public void zoomToFit(int containerWidth, int containerHeight, int fbWidth, int fbHeight) { | |
118 int scalePromille = Math.min(1000 * containerWidth / fbWidth, | |
119 1000 * containerHeight / fbHeight); | |
120 while (fbWidth * scalePromille / 1000. > containerWidth || | |
121 fbHeight * scalePromille / 1000. > containerHeight) { | |
122 scalePromille -= 1; | |
123 } | |
124 setScalePercent(scalePromille / 10.); | |
125 fireListeners(); | |
126 } | |
127 | |
128 public boolean isChangedMouseCursorShape() { | |
129 return (changedSettingsMask & CHANGED_MOUSE_CURSOR_SHAPE) == CHANGED_MOUSE_CURSOR_SHAPE; | |
130 } | |
131 | |
132 public static boolean isUiSettingsChangedFired(SettingsChangedEvent event) { | |
133 return event.getSource() instanceof UiSettings; | |
134 } | |
135 | |
136 public double getScalePercent() { | |
137 return uiSettingsData.getScalePercent(); | |
138 } | |
139 | |
140 public String getScalePercentFormatted() { | |
141 NumberFormat numberFormat = new DecimalFormat("###.#"); | |
142 return numberFormat.format(uiSettingsData.getScalePercent()); | |
143 } | |
144 | |
145 public LocalMouseCursorShape getMouseCursorShape() { | |
146 return uiSettingsData.getMouseCursorShape(); | |
147 } | |
148 | |
149 public void setMouseCursorShape(LocalMouseCursorShape mouseCursorShape) { | |
150 if (this.uiSettingsData.setMouseCursorShape(mouseCursorShape)) { | |
151 changedSettingsMask |= CHANGED_MOUSE_CURSOR_SHAPE; | |
152 fireListeners(); | |
153 } | |
154 } | |
155 | |
156 public void copyDataFrom(UiSettingsData other) { | |
157 copyDataFrom(other, 0); | |
158 } | |
159 public void copyDataFrom(UiSettingsData other, int mask) { | |
160 if (null == other) return; | |
161 if ((mask & CHANGED_SCALE_FACTOR) == 0) uiSettingsData.setScalePercent(other.getScalePercent()); | |
162 if ((mask & CHANGED_MOUSE_CURSOR_SHAPE) == 0) uiSettingsData.setMouseCursorShape(other.getMouseCursorShape()); | |
163 if ((mask & CHANGED_FULL_SCREEN) == 0) uiSettingsData.setFullScreen(other.isFullScreen()); | |
164 } | |
165 | |
166 public void setFullScreen(boolean isFullScreen) { | |
167 if (uiSettingsData.setFullScreen(isFullScreen)) { | |
168 changedSettingsMask |= CHANGED_FULL_SCREEN; | |
169 fireListeners(); | |
170 } | |
171 } | |
172 | |
173 public boolean isFullScreen() { | |
174 return uiSettingsData.isFullScreen(); | |
175 } | |
176 | |
177 public UiSettingsData getData() { | |
178 return uiSettingsData; | |
179 } | |
180 | |
181 @Override | |
182 public String toString() { | |
183 return "UiSettings{" + | |
184 "scalePercent=" + uiSettingsData.getScalePercent() + | |
185 ", fullScreen=" + uiSettingsData.isFullScreen() + | |
186 ", mouseCursorShape=" + uiSettingsData.getMouseCursorShape() + | |
187 '}'; | |
188 } | |
189 | |
190 } |