DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | // |
| 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> |
| 35 | #include <FL/Fl_Window.H> |
| 36 | #include <stdlib.h> |
| 37 | #include "flstring.h" |
| 38 | |
| 39 | #ifdef __APPLE_QUARTZ__ |
| 40 | #include <FL/fl_draw.H> |
| 41 | #endif |
| 42 | |
| 43 | char *Fl_Window::default_xclass_ = 0L; |
| 44 | |
| 45 | void Fl_Window::_Fl_Window() { |
| 46 | type(FL_WINDOW); |
| 47 | box(FL_FLAT_BOX); |
| 48 | if (Fl::scheme_bg_) { |
| 49 | labeltype(FL_NORMAL_LABEL); |
| 50 | align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP); |
| 51 | image(Fl::scheme_bg_); |
| 52 | } else { |
| 53 | labeltype(FL_NO_LABEL); |
| 54 | } |
| 55 | i = 0; |
| 56 | xclass_ = 0; |
| 57 | icon_ = 0; |
| 58 | iconlabel_ = 0; |
| 59 | resizable(0); |
| 60 | size_range_set = 0; |
| 61 | minw = maxw = minh = maxh = 0; |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame] | 62 | no_fullscreen_x = 0; |
| 63 | no_fullscreen_y = 0; |
| 64 | no_fullscreen_w = w(); |
| 65 | no_fullscreen_h = h(); |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 66 | callback((Fl_Callback*)default_callback); |
| 67 | } |
| 68 | |
| 69 | Fl_Window::Fl_Window(int X,int Y,int W, int H, const char *l) |
| 70 | : Fl_Group(X, Y, W, H, l) { |
| 71 | cursor_default = FL_CURSOR_DEFAULT; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 72 | |
| 73 | _Fl_Window(); |
| 74 | set_flag(FORCE_POSITION); |
| 75 | } |
| 76 | |
| 77 | Fl_Window::Fl_Window(int W, int H, const char *l) |
| 78 | // fix common user error of a missing end() with current(0): |
| 79 | : Fl_Group((Fl_Group::current(0),0), 0, W, H, l) { |
| 80 | cursor_default = FL_CURSOR_DEFAULT; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 81 | |
| 82 | _Fl_Window(); |
| 83 | clear_visible(); |
| 84 | } |
| 85 | |
| 86 | Fl_Window *Fl_Widget::window() const { |
| 87 | for (Fl_Widget *o = parent(); o; o = o->parent()) |
| 88 | if (o->type() >= FL_WINDOW) return (Fl_Window*)o; |
| 89 | return 0; |
| 90 | } |
| 91 | /** Gets the x position of the window on the screen */ |
| 92 | int Fl_Window::x_root() const { |
| 93 | Fl_Window *p = window(); |
| 94 | if (p) return p->x_root() + x(); |
| 95 | return x(); |
| 96 | } |
| 97 | /** Gets the y position of the window on the screen */ |
| 98 | int Fl_Window::y_root() const { |
| 99 | Fl_Window *p = window(); |
| 100 | if (p) return p->y_root() + y(); |
| 101 | return y(); |
| 102 | } |
| 103 | |
| 104 | void Fl_Window::draw() { |
| 105 | |
| 106 | // The following is similar to Fl_Group::draw(), but ... |
| 107 | // - we draw the box with x=0 and y=0 instead of x() and y() |
| 108 | // - we don't draw a label |
| 109 | |
| 110 | if (damage() & ~FL_DAMAGE_CHILD) { // draw the entire thing |
| 111 | draw_box(box(),0,0,w(),h(),color()); // draw box with x/y = 0 |
| 112 | } |
| 113 | draw_children(); |
| 114 | |
| 115 | #ifdef __APPLE_QUARTZ__ |
| 116 | // on OS X, windows have no frame. To resize a window, we drag the lower right |
| 117 | // corner. This code draws a little ribbed triangle for dragging. |
| 118 | extern CGContextRef fl_gc; |
| 119 | if (fl_gc && !parent() && resizable() && (!size_range_set || minh!=maxh || minw!=maxw)) { |
| 120 | int dx = Fl::box_dw(box())-Fl::box_dx(box()); |
| 121 | int dy = Fl::box_dh(box())-Fl::box_dy(box()); |
| 122 | if (dx<=0) dx = 1; |
| 123 | if (dy<=0) dy = 1; |
| 124 | int x1 = w()-dx-1, x2 = x1, y1 = h()-dx-1, y2 = y1; |
| 125 | Fl_Color c[4] = { |
| 126 | color(), |
| 127 | fl_color_average(color(), FL_WHITE, 0.7f), |
| 128 | fl_color_average(color(), FL_BLACK, 0.6f), |
| 129 | fl_color_average(color(), FL_BLACK, 0.8f), |
| 130 | }; |
| 131 | int i; |
| 132 | for (i=dx; i<12; i++) { |
| 133 | fl_color(c[i&3]); |
| 134 | fl_line(x1--, y1, x2, y2--); |
| 135 | } |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | # if defined(FLTK_USE_CAIRO) |
| 140 | Fl::cairo_make_current(this); // checkout if an update is necessary |
| 141 | # endif |
| 142 | } |
| 143 | |
| 144 | void Fl_Window::label(const char *name) { |
| 145 | label(name, iconlabel()); |
| 146 | } |
| 147 | |
| 148 | void Fl_Window::copy_label(const char *a) { |
| 149 | if (flags() & COPIED_LABEL) { |
| 150 | free((void *)label()); |
| 151 | clear_flag(COPIED_LABEL); |
| 152 | } |
| 153 | if (a) a = strdup(a); |
| 154 | label(a, iconlabel()); |
| 155 | set_flag(COPIED_LABEL); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void Fl_Window::iconlabel(const char *iname) { |
| 160 | label(label(), iname); |
| 161 | } |
| 162 | |
| 163 | // the Fl::atclose pointer is provided for back compatibility. You |
| 164 | // can now just change the callback for the window instead. |
| 165 | |
| 166 | /** Default callback for window widgets. It hides the window and then calls the default widget callback. */ |
| 167 | void Fl::default_atclose(Fl_Window* window, void* v) { |
| 168 | window->hide(); |
| 169 | Fl_Widget::default_callback(window, v); // put on Fl::read_queue() |
| 170 | } |
| 171 | /** Back compatibility: default window callback handler \see Fl::set_atclose() */ |
| 172 | void (*Fl::atclose)(Fl_Window*, void*) = default_atclose; |
| 173 | /** Back compatibility: Sets the default callback v for win to call on close event */ |
| 174 | void Fl_Window::default_callback(Fl_Window* win, void* v) { |
| 175 | Fl::atclose(win, v); |
| 176 | } |
| 177 | |
| 178 | /** Returns the last window that was made current. \see Fl_Window::make_current() */ |
| 179 | Fl_Window *Fl_Window::current() { |
| 180 | return current_; |
| 181 | } |
| 182 | |
| 183 | /** Returns the default xclass. |
| 184 | |
| 185 | \see Fl_Window::default_xclass(const char *) |
| 186 | |
| 187 | */ |
| 188 | const char *Fl_Window::default_xclass() |
| 189 | { |
| 190 | if (default_xclass_) { |
| 191 | return default_xclass_; |
| 192 | } else { |
| 193 | return "FLTK"; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** Sets the default window xclass. |
| 198 | |
| 199 | The default xclass is used for all windows that don't have their |
| 200 | own xclass set before show() is called. You can change the default |
| 201 | xclass whenever you want, but this only affects windows that are |
| 202 | created (and shown) after this call. |
| 203 | |
| 204 | The given string \p xc is copied. You can use a local variable or |
| 205 | free the string immediately after this call. |
| 206 | |
| 207 | If you don't call this, the default xclass for all windows will be "FLTK". |
| 208 | You can reset the default xclass by specifying NULL for \p xc. |
| 209 | |
| 210 | If you call Fl_Window::xclass(const char *) for any window, then |
| 211 | this also sets the default xclass, unless it has been set before. |
| 212 | |
| 213 | \param[in] xc default xclass for all windows subsequently created |
| 214 | |
| 215 | \see Fl_Window::xclass(const char *) |
| 216 | */ |
| 217 | void Fl_Window::default_xclass(const char *xc) |
| 218 | { |
| 219 | if (default_xclass_) { |
| 220 | free(default_xclass_); |
| 221 | default_xclass_ = 0L; |
| 222 | } |
| 223 | if (xc) { |
| 224 | default_xclass_ = strdup(xc); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** Sets the xclass for this window. |
| 229 | |
| 230 | A string used to tell the system what type of window this is. Mostly |
| 231 | this identifies the picture to draw in the icon. This only works if |
| 232 | called \e before calling show(). |
| 233 | |
| 234 | <I>Under X</I>, this is turned into a XA_WM_CLASS pair by truncating at |
| 235 | the first non-alphanumeric character and capitalizing the first character, |
| 236 | and the second one if the first is 'x'. Thus "foo" turns into "foo, Foo", |
| 237 | and "xprog.1" turns into "xprog, XProg". |
| 238 | |
| 239 | <I>Under Microsoft Windows</I>, this string is used as the name of the |
| 240 | WNDCLASS structure, though it is not clear if this can have any |
| 241 | visible effect. |
| 242 | |
| 243 | \since FLTK 1.3 the passed string is copied. You can use a local |
| 244 | variable or free the string immediately after this call. Note that |
| 245 | FLTK 1.1 stores the \e pointer without copying the string. |
| 246 | |
| 247 | If the default xclass has not yet been set, this also sets the |
| 248 | default xclass for all windows created subsequently. |
| 249 | |
| 250 | \see Fl_Window::default_xclass(const char *) |
| 251 | */ |
| 252 | void Fl_Window::xclass(const char *xc) |
| 253 | { |
| 254 | if (xclass_) { |
| 255 | free(xclass_); |
| 256 | xclass_ = 0L; |
| 257 | } |
| 258 | if (xc) { |
| 259 | xclass_ = strdup(xc); |
| 260 | if (!default_xclass_) { |
| 261 | default_xclass(xc); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** Returns the xclass for this window, or a default. |
| 267 | |
| 268 | \see Fl_Window::default_xclass(const char *) |
| 269 | \see Fl_Window::xclass(const char *) |
| 270 | */ |
| 271 | const char *Fl_Window::xclass() const |
| 272 | { |
| 273 | if (xclass_) { |
| 274 | return xclass_; |
| 275 | } else { |
| 276 | return default_xclass(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** Gets the current icon window target dependent data. */ |
| 281 | const void *Fl_Window::icon() const { |
| 282 | return icon_; |
| 283 | } |
| 284 | |
| 285 | /** Sets the current icon window target dependent data. */ |
| 286 | void Fl_Window::icon(const void * ic) { |
| 287 | icon_ = ic; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | // |
| 292 | // End of "$Id: Fl_Window.cxx 8472 2011-02-25 08:44:47Z AlbrechtS $". |
| 293 | // |