blob: fe872ec7c5275989560e9a8bc74e0f379022a885 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
2// "$Id: Fl_Window.cxx 8472 2011-02-25 08:44:47Z AlbrechtS $"
3//
4// Window widget class for the Fast Light Tool Kit (FLTK).
5//
6// Copyright 1998-2010 by Bill Spitzak and others.
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Library General Public
10// License as published by the Free Software Foundation; either
11// version 2 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Library General Public License for more details.
17//
18// You should have received a copy of the GNU Library General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21// USA.
22//
23// Please report all bugs and problems on the following page:
24//
25// http://www.fltk.org/str.php
26//
27
28// The Fl_Window is a window in the fltk library.
29// This is the system-independent portions. The huge amount of
30// crap you need to do to communicate with X is in Fl_x.cxx, the
31// equivalent (but totally different) crap for MSWindows is in Fl_win32.cxx
32#include <config.h>
33#include <FL/Fl.H>
34#include <FL/x.H>
Pierre Ossmanab8aeed2012-04-25 14:57:22 +000035#include <FL/Fl_RGB_Image.H>
DRC2ff39b82011-07-28 08:38:59 +000036#include <FL/Fl_Window.H>
37#include <stdlib.h>
38#include "flstring.h"
39
40#ifdef __APPLE_QUARTZ__
41#include <FL/fl_draw.H>
42#endif
43
44char *Fl_Window::default_xclass_ = 0L;
45
46void Fl_Window::_Fl_Window() {
47 type(FL_WINDOW);
48 box(FL_FLAT_BOX);
49 if (Fl::scheme_bg_) {
50 labeltype(FL_NORMAL_LABEL);
51 align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP);
52 image(Fl::scheme_bg_);
53 } else {
54 labeltype(FL_NO_LABEL);
55 }
56 i = 0;
57 xclass_ = 0;
Pierre Ossmanab8aeed2012-04-25 14:57:22 +000058 icon_ = new icon_data;
59 memset(icon_, 0, sizeof(*icon_));
DRC2ff39b82011-07-28 08:38:59 +000060 iconlabel_ = 0;
61 resizable(0);
62 size_range_set = 0;
63 minw = maxw = minh = maxh = 0;
DRC685f17e2011-07-28 09:23:00 +000064 no_fullscreen_x = 0;
65 no_fullscreen_y = 0;
66 no_fullscreen_w = w();
67 no_fullscreen_h = h();
DRC2ff39b82011-07-28 08:38:59 +000068 callback((Fl_Callback*)default_callback);
69}
70
71Fl_Window::Fl_Window(int X,int Y,int W, int H, const char *l)
72: Fl_Group(X, Y, W, H, l) {
73 cursor_default = FL_CURSOR_DEFAULT;
DRC2ff39b82011-07-28 08:38:59 +000074
75 _Fl_Window();
76 set_flag(FORCE_POSITION);
77}
78
79Fl_Window::Fl_Window(int W, int H, const char *l)
80// fix common user error of a missing end() with current(0):
81 : Fl_Group((Fl_Group::current(0),0), 0, W, H, l) {
82 cursor_default = FL_CURSOR_DEFAULT;
DRC2ff39b82011-07-28 08:38:59 +000083
84 _Fl_Window();
85 clear_visible();
86}
87
88Fl_Window *Fl_Widget::window() const {
89 for (Fl_Widget *o = parent(); o; o = o->parent())
90 if (o->type() >= FL_WINDOW) return (Fl_Window*)o;
91 return 0;
92}
93/** Gets the x position of the window on the screen */
94int Fl_Window::x_root() const {
95 Fl_Window *p = window();
96 if (p) return p->x_root() + x();
97 return x();
98}
99/** Gets the y position of the window on the screen */
100int Fl_Window::y_root() const {
101 Fl_Window *p = window();
102 if (p) return p->y_root() + y();
103 return y();
104}
105
106void Fl_Window::draw() {
107
108 // The following is similar to Fl_Group::draw(), but ...
109 // - we draw the box with x=0 and y=0 instead of x() and y()
110 // - we don't draw a label
111
112 if (damage() & ~FL_DAMAGE_CHILD) { // draw the entire thing
113 draw_box(box(),0,0,w(),h(),color()); // draw box with x/y = 0
114 }
115 draw_children();
116
117#ifdef __APPLE_QUARTZ__
118 // on OS X, windows have no frame. To resize a window, we drag the lower right
119 // corner. This code draws a little ribbed triangle for dragging.
120 extern CGContextRef fl_gc;
121 if (fl_gc && !parent() && resizable() && (!size_range_set || minh!=maxh || minw!=maxw)) {
122 int dx = Fl::box_dw(box())-Fl::box_dx(box());
123 int dy = Fl::box_dh(box())-Fl::box_dy(box());
124 if (dx<=0) dx = 1;
125 if (dy<=0) dy = 1;
126 int x1 = w()-dx-1, x2 = x1, y1 = h()-dx-1, y2 = y1;
127 Fl_Color c[4] = {
128 color(),
129 fl_color_average(color(), FL_WHITE, 0.7f),
130 fl_color_average(color(), FL_BLACK, 0.6f),
131 fl_color_average(color(), FL_BLACK, 0.8f),
132 };
133 int i;
134 for (i=dx; i<12; i++) {
135 fl_color(c[i&3]);
136 fl_line(x1--, y1, x2, y2--);
137 }
138 }
139#endif
140
141# if defined(FLTK_USE_CAIRO)
142 Fl::cairo_make_current(this); // checkout if an update is necessary
143# endif
144}
145
146void Fl_Window::label(const char *name) {
147 label(name, iconlabel());
148}
149
150void Fl_Window::copy_label(const char *a) {
151 if (flags() & COPIED_LABEL) {
152 free((void *)label());
153 clear_flag(COPIED_LABEL);
154 }
155 if (a) a = strdup(a);
156 label(a, iconlabel());
157 set_flag(COPIED_LABEL);
158}
159
160
161void Fl_Window::iconlabel(const char *iname) {
162 label(label(), iname);
163}
164
165// the Fl::atclose pointer is provided for back compatibility. You
166// can now just change the callback for the window instead.
167
168/** Default callback for window widgets. It hides the window and then calls the default widget callback. */
169void Fl::default_atclose(Fl_Window* window, void* v) {
170 window->hide();
171 Fl_Widget::default_callback(window, v); // put on Fl::read_queue()
172}
173/** Back compatibility: default window callback handler \see Fl::set_atclose() */
174void (*Fl::atclose)(Fl_Window*, void*) = default_atclose;
175/** Back compatibility: Sets the default callback v for win to call on close event */
176void Fl_Window::default_callback(Fl_Window* win, void* v) {
177 Fl::atclose(win, v);
178}
179
180/** Returns the last window that was made current. \see Fl_Window::make_current() */
181Fl_Window *Fl_Window::current() {
182 return current_;
183}
184
185/** Returns the default xclass.
186
187 \see Fl_Window::default_xclass(const char *)
188
189 */
190const char *Fl_Window::default_xclass()
191{
192 if (default_xclass_) {
193 return default_xclass_;
194 } else {
195 return "FLTK";
196 }
197}
198
199/** Sets the default window xclass.
200
201 The default xclass is used for all windows that don't have their
202 own xclass set before show() is called. You can change the default
203 xclass whenever you want, but this only affects windows that are
204 created (and shown) after this call.
205
206 The given string \p xc is copied. You can use a local variable or
207 free the string immediately after this call.
208
209 If you don't call this, the default xclass for all windows will be "FLTK".
210 You can reset the default xclass by specifying NULL for \p xc.
211
212 If you call Fl_Window::xclass(const char *) for any window, then
213 this also sets the default xclass, unless it has been set before.
214
215 \param[in] xc default xclass for all windows subsequently created
216
217 \see Fl_Window::xclass(const char *)
218*/
219void Fl_Window::default_xclass(const char *xc)
220{
221 if (default_xclass_) {
222 free(default_xclass_);
223 default_xclass_ = 0L;
224 }
225 if (xc) {
226 default_xclass_ = strdup(xc);
227 }
228}
229
230/** Sets the xclass for this window.
231
232 A string used to tell the system what type of window this is. Mostly
233 this identifies the picture to draw in the icon. This only works if
234 called \e before calling show().
235
236 <I>Under X</I>, this is turned into a XA_WM_CLASS pair by truncating at
237 the first non-alphanumeric character and capitalizing the first character,
238 and the second one if the first is 'x'. Thus "foo" turns into "foo, Foo",
239 and "xprog.1" turns into "xprog, XProg".
240
241 <I>Under Microsoft Windows</I>, this string is used as the name of the
242 WNDCLASS structure, though it is not clear if this can have any
243 visible effect.
244
245 \since FLTK 1.3 the passed string is copied. You can use a local
246 variable or free the string immediately after this call. Note that
247 FLTK 1.1 stores the \e pointer without copying the string.
248
249 If the default xclass has not yet been set, this also sets the
250 default xclass for all windows created subsequently.
251
252 \see Fl_Window::default_xclass(const char *)
253*/
254void Fl_Window::xclass(const char *xc)
255{
256 if (xclass_) {
257 free(xclass_);
258 xclass_ = 0L;
259 }
260 if (xc) {
261 xclass_ = strdup(xc);
262 if (!default_xclass_) {
263 default_xclass(xc);
264 }
265 }
266}
267
268/** Returns the xclass for this window, or a default.
269
270 \see Fl_Window::default_xclass(const char *)
271 \see Fl_Window::xclass(const char *)
272*/
273const char *Fl_Window::xclass() const
274{
275 if (xclass_) {
276 return xclass_;
277 } else {
278 return default_xclass();
279 }
280}
281
Pierre Ossmanab8aeed2012-04-25 14:57:22 +0000282void Fl_Window::default_icon(const Fl_RGB_Image *icon) {
283 default_icons(&icon, 1);
284}
285
286void Fl_Window::default_icons(const Fl_RGB_Image **icons, int count) {
287 Fl_X::set_default_icons(icons, count);
288}
289
290void Fl_Window::icon(const Fl_RGB_Image *icon) {
291 icons(&icon, 1);
292}
293
294void Fl_Window::icons(const Fl_RGB_Image **icons, int count) {
295 free_icons();
296
297 if (count > 0) {
298 icon_->icons = new Fl_RGB_Image*[count];
299 icon_->count = count;
300 // FIXME: Fl_RGB_Image lacks const modifiers on methods
301 for (int i = 0;i < count;i++)
302 icon_->icons[i] = (Fl_RGB_Image*)((Fl_RGB_Image*)icons[i])->copy();
303 }
304
305 if (i)
306 i->set_icons();
307}
308
DRC2ff39b82011-07-28 08:38:59 +0000309/** Gets the current icon window target dependent data. */
310const void *Fl_Window::icon() const {
Pierre Ossmanab8aeed2012-04-25 14:57:22 +0000311 return icon_->legacy_icon;
DRC2ff39b82011-07-28 08:38:59 +0000312}
313
314/** Sets the current icon window target dependent data. */
315void Fl_Window::icon(const void * ic) {
Pierre Ossmanab8aeed2012-04-25 14:57:22 +0000316 free_icons();
317 icon_->legacy_icon = ic;
DRC2ff39b82011-07-28 08:38:59 +0000318}
319
Pierre Ossmanab8aeed2012-04-25 14:57:22 +0000320void Fl_Window::free_icons() {
321 int i;
322
323 icon_->legacy_icon = 0L;
324
325 if (icon_->icons) {
326 for (i = 0;i < icon_->count;i++)
327 delete icon_->icons[i];
328 delete [] icon_->icons;
329 icon_->icons = 0L;
330 }
331
332 icon_->count = 0;
333
334#ifdef WIN32
335 if (icon_->big_icon)
336 DestroyIcon(icon_->big_icon);
337 if (icon_->small_icon)
338 DestroyIcon(icon_->small_icon);
339
340 icon_->big_icon = NULL;
341 icon_->small_icon = NULL;
342#endif
343}
DRC2ff39b82011-07-28 08:38:59 +0000344
345//
346// End of "$Id: Fl_Window.cxx 8472 2011-02-25 08:44:47Z AlbrechtS $".
347//