DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | // |
| 2 | // "$Id: fl_cursor.cxx 8055 2010-12-18 22:31:01Z manolo $" |
| 3 | // |
| 4 | // Mouse cursor support 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 | // Change the current cursor. |
| 29 | // Under X the cursor is attached to the X window. I tried to hide |
| 30 | // this and pretend that changing the cursor is a drawing function. |
| 31 | // This avoids a field in the Fl_Window, and I suspect is more |
| 32 | // portable to other systems. |
| 33 | |
| 34 | #include <FL/Fl.H> |
| 35 | #include <FL/Fl_Window.H> |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 36 | #include <FL/Fl_Pixmap.H> |
| 37 | #include <FL/Fl_RGB_Image.H> |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 38 | #include <FL/x.H> |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 39 | #include <FL/fl_draw.H> |
| 40 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 41 | #include "fl_cursor_wait.xpm" |
| 42 | #include "fl_cursor_help.xpm" |
| 43 | #include "fl_cursor_nwse.xpm" |
| 44 | #include "fl_cursor_nesw.xpm" |
| 45 | #include "fl_cursor_none.xpm" |
| 46 | |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 47 | /** |
| 48 | Sets the cursor for the current window to the specified shape and colors. |
| 49 | The cursors are defined in the <FL/Enumerations.H> header file. |
| 50 | */ |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 51 | void fl_cursor(Fl_Cursor c) { |
| 52 | if (Fl::first_window()) Fl::first_window()->cursor(c); |
| 53 | } |
| 54 | |
| 55 | /* For back compatibility only. */ |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 56 | void fl_cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) { |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 57 | fl_cursor(c); |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 58 | } |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 59 | |
| 60 | |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 61 | /** |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 62 | Sets the default window cursor. This is the cursor that will be used |
| 63 | after the mouse pointer leaves a widget with a custom cursor set. |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 64 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 65 | \see cursor(const Fl_RGB_Image*, int, int), default_cursor() |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 66 | */ |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 67 | void Fl_Window::default_cursor(Fl_Cursor c) { |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 68 | cursor_default = c; |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 69 | cursor(c); |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 72 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 73 | void fallback_cursor(Fl_Window *w, Fl_Cursor c) { |
| 74 | const char **xpm; |
| 75 | int hotx, hoty; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 76 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 77 | // The standard arrow is our final fallback, so something is broken |
| 78 | // if we get called back here with that as an argument. |
| 79 | if (c == FL_CURSOR_ARROW) |
| 80 | return; |
| 81 | |
| 82 | switch (c) { |
| 83 | case FL_CURSOR_WAIT: |
| 84 | xpm = (const char**)fl_cursor_wait_xpm; |
| 85 | hotx = 8; |
| 86 | hoty = 15; |
| 87 | break; |
| 88 | case FL_CURSOR_HELP: |
| 89 | xpm = (const char**)fl_cursor_help_xpm; |
| 90 | hotx = 1; |
| 91 | hoty = 3; |
| 92 | break; |
| 93 | case FL_CURSOR_NWSE: |
| 94 | xpm = (const char**)fl_cursor_nwse_xpm; |
| 95 | hotx = 7; |
| 96 | hoty = 7; |
| 97 | break; |
| 98 | case FL_CURSOR_NESW: |
| 99 | xpm = (const char**)fl_cursor_nesw_xpm; |
| 100 | hotx = 7; |
| 101 | hoty = 7; |
| 102 | break; |
| 103 | case FL_CURSOR_NONE: |
| 104 | xpm = (const char**)fl_cursor_none_xpm; |
| 105 | hotx = 0; |
| 106 | hoty = 0; |
| 107 | break; |
| 108 | default: |
| 109 | w->cursor(FL_CURSOR_ARROW); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | Fl_Pixmap pxm(xpm); |
| 114 | Fl_RGB_Image image(&pxm); |
| 115 | |
| 116 | w->cursor(&image, hotx, hoty); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void Fl_Window::cursor(Fl_Cursor c) { |
| 121 | int ret; |
| 122 | |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 123 | // the cursor must be set for the top level window, not for subwindows |
| 124 | Fl_Window *w = window(), *toplevel = this; |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 125 | |
| 126 | while (w) { |
| 127 | toplevel = w; |
| 128 | w = w->window(); |
| 129 | } |
| 130 | |
| 131 | if (toplevel != this) { |
| 132 | toplevel->cursor(c); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (c == FL_CURSOR_DEFAULT) |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 137 | c = cursor_default; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 138 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 139 | if (!i) |
| 140 | return; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 141 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 142 | ret = i->set_cursor(c); |
| 143 | if (ret) |
| 144 | return; |
| 145 | |
| 146 | fallback_cursor(this, c); |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 147 | } |
| 148 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 149 | /** |
| 150 | Changes the cursor for this window. This always calls the system, if |
| 151 | you are changing the cursor a lot you may want to keep track of how |
| 152 | you set it in a static variable and call this only if the new cursor |
| 153 | is different. |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 154 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 155 | The default cursor will be used if the provided image cannot be used |
| 156 | as a cursor. |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 157 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 158 | \see cursor(Fl_Cursor), default_cursor() |
| 159 | */ |
| 160 | void Fl_Window::cursor(const Fl_RGB_Image *image, int hotx, int hoty) { |
| 161 | int ret; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 162 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 163 | // the cursor must be set for the top level window, not for subwindows |
| 164 | Fl_Window *w = window(), *toplevel = this; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 165 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 166 | while (w) { |
| 167 | toplevel = w; |
| 168 | w = w->window(); |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 169 | } |
| 170 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 171 | if (toplevel != this) { |
| 172 | toplevel->cursor(image, hotx, hoty); |
| 173 | return; |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 174 | } |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 175 | |
DRC | 685f17e | 2011-07-28 09:23:00 +0000 | [diff] [blame^] | 176 | if (!i) |
| 177 | return; |
| 178 | |
| 179 | ret = i->set_cursor(image, hotx, hoty); |
| 180 | if (ret) |
| 181 | return; |
| 182 | |
| 183 | cursor(FL_CURSOR_DEFAULT); |
| 184 | } |
DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 185 | |
| 186 | // |
| 187 | // End of "$Id: fl_cursor.cxx 8055 2010-12-18 22:31:01Z manolo $". |
| 188 | // |