blob: bad3936b8ec2a2034ff766d67cb3d33a68d6ae38 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
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>
DRC685f17e2011-07-28 09:23:00 +000036#include <FL/Fl_Pixmap.H>
37#include <FL/Fl_RGB_Image.H>
DRC2ff39b82011-07-28 08:38:59 +000038#include <FL/x.H>
DRC2ff39b82011-07-28 08:38:59 +000039#include <FL/fl_draw.H>
40
DRC685f17e2011-07-28 09:23:00 +000041#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
DRC2ff39b82011-07-28 08:38:59 +000047/**
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 */
DRC685f17e2011-07-28 09:23:00 +000051void fl_cursor(Fl_Cursor c) {
52 if (Fl::first_window()) Fl::first_window()->cursor(c);
53}
54
55/* For back compatibility only. */
DRC2ff39b82011-07-28 08:38:59 +000056void fl_cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) {
DRC685f17e2011-07-28 09:23:00 +000057 fl_cursor(c);
DRC2ff39b82011-07-28 08:38:59 +000058}
DRC685f17e2011-07-28 09:23:00 +000059
60
DRC2ff39b82011-07-28 08:38:59 +000061/**
DRC685f17e2011-07-28 09:23:00 +000062 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.
DRC2ff39b82011-07-28 08:38:59 +000064
DRC685f17e2011-07-28 09:23:00 +000065 \see cursor(const Fl_RGB_Image*, int, int), default_cursor()
DRC2ff39b82011-07-28 08:38:59 +000066*/
DRC685f17e2011-07-28 09:23:00 +000067void Fl_Window::default_cursor(Fl_Cursor c) {
DRC2ff39b82011-07-28 08:38:59 +000068 cursor_default = c;
DRC685f17e2011-07-28 09:23:00 +000069 cursor(c);
DRC2ff39b82011-07-28 08:38:59 +000070}
71
DRC2ff39b82011-07-28 08:38:59 +000072
DRC685f17e2011-07-28 09:23:00 +000073void fallback_cursor(Fl_Window *w, Fl_Cursor c) {
74 const char **xpm;
75 int hotx, hoty;
DRC2ff39b82011-07-28 08:38:59 +000076
DRC685f17e2011-07-28 09:23:00 +000077 // 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
120void Fl_Window::cursor(Fl_Cursor c) {
121 int ret;
122
DRC2ff39b82011-07-28 08:38:59 +0000123 // the cursor must be set for the top level window, not for subwindows
124 Fl_Window *w = window(), *toplevel = this;
DRC685f17e2011-07-28 09:23:00 +0000125
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)
DRC2ff39b82011-07-28 08:38:59 +0000137 c = cursor_default;
DRC2ff39b82011-07-28 08:38:59 +0000138
DRC685f17e2011-07-28 09:23:00 +0000139 if (!i)
140 return;
DRC2ff39b82011-07-28 08:38:59 +0000141
DRC685f17e2011-07-28 09:23:00 +0000142 ret = i->set_cursor(c);
143 if (ret)
144 return;
145
146 fallback_cursor(this, c);
DRC2ff39b82011-07-28 08:38:59 +0000147}
148
DRC685f17e2011-07-28 09:23:00 +0000149/**
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.
DRC2ff39b82011-07-28 08:38:59 +0000154
DRC685f17e2011-07-28 09:23:00 +0000155 The default cursor will be used if the provided image cannot be used
156 as a cursor.
DRC2ff39b82011-07-28 08:38:59 +0000157
DRC685f17e2011-07-28 09:23:00 +0000158 \see cursor(Fl_Cursor), default_cursor()
159*/
160void Fl_Window::cursor(const Fl_RGB_Image *image, int hotx, int hoty) {
161 int ret;
DRC2ff39b82011-07-28 08:38:59 +0000162
DRC685f17e2011-07-28 09:23:00 +0000163 // the cursor must be set for the top level window, not for subwindows
164 Fl_Window *w = window(), *toplevel = this;
DRC2ff39b82011-07-28 08:38:59 +0000165
DRC685f17e2011-07-28 09:23:00 +0000166 while (w) {
167 toplevel = w;
168 w = w->window();
DRC2ff39b82011-07-28 08:38:59 +0000169 }
170
DRC685f17e2011-07-28 09:23:00 +0000171 if (toplevel != this) {
172 toplevel->cursor(image, hotx, hoty);
173 return;
DRC2ff39b82011-07-28 08:38:59 +0000174 }
DRC2ff39b82011-07-28 08:38:59 +0000175
DRC685f17e2011-07-28 09:23:00 +0000176 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}
DRC2ff39b82011-07-28 08:38:59 +0000185
186//
187// End of "$Id: fl_cursor.cxx 8055 2010-12-18 22:31:01Z manolo $".
188//