You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
7.9KB

  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. // The Fl_Window is a window in the fltk library.
  28. // This is the system-independent portions. The huge amount of
  29. // crap you need to do to communicate with X is in Fl_x.cxx, the
  30. // equivalent (but totally different) crap for MSWindows is in Fl_win32.cxx
  31. #include <config.h>
  32. #include <FL/Fl.H>
  33. #include <FL/x.H>
  34. #include <FL/Fl_Window.H>
  35. #include <stdlib.h>
  36. #include "flstring.h"
  37. #include <FL/Fl_Cairo.H>
  38. #include <FL/fl_draw.H>
  39. char *Fl_Window::default_xclass_ = 0L;
  40. void Fl_Window::_Fl_Window() {
  41. type(FL_WINDOW);
  42. box(FL_FLAT_BOX);
  43. if (Fl::scheme_bg_) {
  44. align(FL_ALIGN_IMAGE_BACKDROP);
  45. image(Fl::scheme_bg_);
  46. }
  47. labeltype(FL_NO_LABEL);
  48. i = 0;
  49. xclass_ = 0;
  50. icon_ = 0;
  51. iconlabel_ = 0;
  52. resizable(0);
  53. size_range_set = 0;
  54. minw = maxw = minh = maxh = 0;
  55. callback((Fl_Callback*)default_callback);
  56. }
  57. Fl_Window::Fl_Window(int X,int Y,int W, int H, const char *l)
  58. : Fl_Group(X, Y, W, H, l) {
  59. cursor_default = FL_CURSOR_DEFAULT;
  60. cursor_fg = FL_BLACK;
  61. cursor_bg = FL_WHITE;
  62. _Fl_Window();
  63. set_flag(FORCE_POSITION);
  64. }
  65. Fl_Window::Fl_Window(int W, int H, const char *l)
  66. // fix common user error of a missing end() with current(0):
  67. : Fl_Group((Fl_Group::current(0),0), 0, W, H, l) {
  68. cursor_default = FL_CURSOR_DEFAULT;
  69. cursor_fg = FL_BLACK;
  70. cursor_bg = FL_WHITE;
  71. _Fl_Window();
  72. clear_visible();
  73. }
  74. Fl_Window *Fl_Widget::window() const {
  75. for (Fl_Widget *o = parent(); o; o = o->parent())
  76. if (o->type() >= FL_WINDOW) return (Fl_Window*)o;
  77. return 0;
  78. }
  79. /** Gets the x position of the window on the screen */
  80. int Fl_Window::x_root() const {
  81. Fl_Window *p = window();
  82. if (p) return p->x_root() + x();
  83. return x();
  84. }
  85. /** Gets the y position of the window on the screen */
  86. int Fl_Window::y_root() const {
  87. Fl_Window *p = window();
  88. if (p) return p->y_root() + y();
  89. return y();
  90. }
  91. void Fl_Window::draw() {
  92. // The following is similar to Fl_Group::draw(), but ...
  93. // - we draw the box with x=0 and y=0 instead of x() and y()
  94. // - we don't draw a label
  95. // if (damage() & ~FL_DAMAGE_CHILD) { // draw the entire thing
  96. /* always draw the box because the children may be transparent */
  97. draw_box(box(),0,0,w(),h(),color()); // draw box with x/y = 0
  98. // }
  99. draw_children();
  100. if (fl_gc && !parent() && resizable() && (!size_range_set || minh!=maxh || minw!=maxw)) {
  101. int dx = Fl::box_dw(box())-Fl::box_dx(box());
  102. int dy = Fl::box_dh(box())-Fl::box_dy(box());
  103. if (dx<=0) dx = 1;
  104. if (dy<=0) dy = 1;
  105. int x1 = w()-dx-1, x2 = x1, y1 = h()-dx-1, y2 = y1;
  106. Fl_Color c[4] = {
  107. color(),
  108. fl_color_average(color(), FL_WHITE, 0.7f),
  109. fl_color_average(color(), FL_BLACK, 0.6f),
  110. fl_color_average(color(), FL_BLACK, 0.8f),
  111. };
  112. int i;
  113. for (i=dx; i<12; i++) {
  114. fl_color(c[i&3]);
  115. fl_line(x1--, y1, x2, y2--);
  116. }
  117. }
  118. }
  119. void Fl_Window::label(const char *name) {
  120. label(name, iconlabel());
  121. }
  122. void Fl_Window::copy_label(const char *a) {
  123. if (flags() & COPIED_LABEL) {
  124. free((void *)label());
  125. clear_flag(COPIED_LABEL);
  126. }
  127. if (a) a = strdup(a);
  128. label(a, iconlabel());
  129. set_flag(COPIED_LABEL);
  130. }
  131. void Fl_Window::iconlabel(const char *iname) {
  132. label(label(), iname);
  133. }
  134. // the Fl::atclose pointer is provided for back compatibility. You
  135. // can now just change the callback for the window instead.
  136. /** Default callback for window widgets. It hides the window and then calls the default widget callback. */
  137. void Fl::default_atclose(Fl_Window* window, void* v) {
  138. window->hide();
  139. Fl_Widget::default_callback(window, v); // put on Fl::read_queue()
  140. }
  141. /** Back compatibility: default window callback handler \see Fl::set_atclose() */
  142. void (*Fl::atclose)(Fl_Window*, void*) = default_atclose;
  143. /** Back compatibility: Sets the default callback v for win to call on close event */
  144. void Fl_Window::default_callback(Fl_Window* win, void* v) {
  145. Fl::atclose(win, v);
  146. }
  147. /** Returns the last window that was made current. \see Fl_Window::make_current() */
  148. Fl_Window *Fl_Window::current() {
  149. return current_;
  150. }
  151. /** Returns the default xclass.
  152. \see Fl_Window::default_xclass(const char *)
  153. */
  154. const char *Fl_Window::default_xclass()
  155. {
  156. if (default_xclass_) {
  157. return default_xclass_;
  158. } else {
  159. return "FLTK";
  160. }
  161. }
  162. /** Sets the default window xclass.
  163. The default xclass is used for all windows that don't have their
  164. own xclass set before show() is called. You can change the default
  165. xclass whenever you want, but this only affects windows that are
  166. created (and shown) after this call.
  167. The given string \p xc is copied. You can use a local variable or
  168. free the string immediately after this call.
  169. If you don't call this, the default xclass for all windows will be "FLTK".
  170. You can reset the default xclass by specifying NULL for \p xc.
  171. If you call Fl_Window::xclass(const char *) for any window, then
  172. this also sets the default xclass, unless it has been set before.
  173. \param[in] xc default xclass for all windows subsequently created
  174. \see Fl_Window::xclass(const char *)
  175. */
  176. void Fl_Window::default_xclass(const char *xc)
  177. {
  178. if (default_xclass_) {
  179. free(default_xclass_);
  180. default_xclass_ = 0L;
  181. }
  182. if (xc) {
  183. default_xclass_ = strdup(xc);
  184. }
  185. }
  186. /** Sets the xclass for this window.
  187. A string used to tell the system what type of window this is. Mostly
  188. this identifies the picture to draw in the icon. This only works if
  189. called \e before calling show().
  190. <I>Under X</I>, this is turned into a XA_WM_CLASS pair by truncating at
  191. the first non-alphanumeric character and capitalizing the first character,
  192. and the second one if the first is 'x'. Thus "foo" turns into "foo, Foo",
  193. and "xprog.1" turns into "xprog, XProg".
  194. <I>Under Microsoft Windows</I>, this string is used as the name of the
  195. WNDCLASS structure, though it is not clear if this can have any
  196. visible effect.
  197. \since FLTK 1.3 the passed string is copied. You can use a local
  198. variable or free the string immediately after this call. Note that
  199. FLTK 1.1 stores the \e pointer without copying the string.
  200. If the default xclass has not yet been set, this also sets the
  201. default xclass for all windows created subsequently.
  202. \see Fl_Window::default_xclass(const char *)
  203. */
  204. void Fl_Window::xclass(const char *xc)
  205. {
  206. if (xclass_) {
  207. free(xclass_);
  208. xclass_ = 0L;
  209. }
  210. if (xc) {
  211. xclass_ = strdup(xc);
  212. if (!default_xclass_) {
  213. default_xclass(xc);
  214. }
  215. }
  216. }
  217. /** Returns the xclass for this window, or a default.
  218. \see Fl_Window::default_xclass(const char *)
  219. \see Fl_Window::xclass(const char *)
  220. */
  221. const char *Fl_Window::xclass() const
  222. {
  223. if (xclass_) {
  224. return xclass_;
  225. } else {
  226. return default_xclass();
  227. }
  228. }
  229. /** Gets the current icon window target dependent data. */
  230. const void *Fl_Window::icon() const {
  231. return icon_;
  232. }
  233. /** Sets the current icon window target dependent data. */
  234. void Fl_Window::icon(const void * ic) {
  235. icon_ = ic;
  236. }
  237. //
  238. // End of "$Id: Fl_Window.cxx 8472 2011-02-25 08:44:47Z AlbrechtS $".
  239. //