Fl.cxx 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. //
  2. // "$Id: Fl.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Main event handling code 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. // warning: the Apple Quartz version still uses some Quickdraw calls,
  28. // mostly to get around the single active context in QD and
  29. // to implement clipping. This should be changed into pure
  30. // Quartz calls in the near future.
  31. #include "config.h"
  32. #include <FL/Fl.H>
  33. #include <FL/Fl_Window.H>
  34. #include <FL/x.H>
  35. #include <FL/Fl_Tooltip.H>
  36. #include <ctype.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include "flstring.h"
  40. #if defined(__APPLE__)
  41. #import <Cocoa/Cocoa.h>
  42. #endif
  43. #if defined(DEBUG) || defined(DEBUG_WATCH)
  44. # include <stdio.h>
  45. #endif // DEBUG || DEBUG_WATCH
  46. #ifdef WIN32
  47. # include <ole2.h>
  48. void fl_free_fonts(void);
  49. HBRUSH fl_brush_action(int action);
  50. void fl_cleanup_pens(void);
  51. void fl_release_dc(HWND,HDC);
  52. void fl_cleanup_dc_list(void);
  53. #endif // WIN32
  54. //
  55. // Globals...
  56. //
  57. #ifndef FL_DOXYGEN
  58. Fl_Widget *Fl::belowmouse_,
  59. *Fl::pushed_,
  60. *Fl::focus_,
  61. *Fl::selection_owner_;
  62. int Fl::damage_,
  63. Fl::e_number,
  64. Fl::e_x,
  65. Fl::e_y,
  66. Fl::e_x_root,
  67. Fl::e_y_root,
  68. Fl::e_dx,
  69. Fl::e_dy,
  70. Fl::e_state,
  71. Fl::e_clicks,
  72. Fl::e_is_click,
  73. Fl::e_keysym,
  74. Fl::e_original_keysym,
  75. Fl::scrollbar_size_ = 16;
  76. char *Fl::e_text = (char *)"";
  77. int Fl::e_length;
  78. int Fl::visible_focus_ = 1,
  79. Fl::dnd_text_ops_ = 1;
  80. unsigned char Fl::options_[] = { 0, 0 };
  81. unsigned char Fl::options_read_ = 0;
  82. Fl_Window *fl_xfocus; // which window X thinks has focus
  83. Fl_Window *fl_xmousewin;// which window X thinks has FL_ENTER
  84. Fl_Window *Fl::grab_; // most recent Fl::grab()
  85. Fl_Window *Fl::modal_; // topmost modal() window
  86. #endif // FL_DOXYGEN
  87. //
  88. // 'Fl::version()' - Return the API version number...
  89. //
  90. double
  91. /**
  92. Returns the compiled-in value of the FL_VERSION constant. This
  93. is useful for checking the version of a shared library.
  94. */
  95. Fl::version() {
  96. return FL_VERSION;
  97. }
  98. /**
  99. Gets the default scrollbar size used by
  100. Fl_Browser_,
  101. Fl_Help_View,
  102. Fl_Scroll, and
  103. Fl_Text_Display widgets.
  104. \returns The default size for widget scrollbars, in pixels.
  105. */
  106. int Fl::scrollbar_size() {
  107. return scrollbar_size_;
  108. }
  109. /**
  110. Sets the default scrollbar size that is used by the
  111. Fl_Browser_,
  112. Fl_Help_View,
  113. Fl_Scroll, and
  114. Fl_Text_Display widgets.
  115. \param[in] W The new default size for widget scrollbars, in pixels.
  116. */
  117. void Fl::scrollbar_size(int W) {
  118. scrollbar_size_ = W;
  119. }
  120. /**
  121. Returns whether or not the mouse event is inside the given rectangle.
  122. Returns non-zero if the current event_x and event_y
  123. put it inside the widget or inside an arbitrary bounding box. You
  124. should always call this rather than doing your own comparison so you
  125. are consistent about edge effects.
  126. */
  127. int Fl::event_inside(int xx,int yy,int ww,int hh) /*const*/ {
  128. int mx = e_x - xx;
  129. int my = e_y - yy;
  130. return (mx >= 0 && mx < ww && my >= 0 && my < hh);
  131. }
  132. /** Returns whether or not the mouse event is inside the given widget.
  133. Returns non-zero if the current event_x and event_y
  134. put it inside the widget or inside an arbitrary bounding box. You
  135. should always call this rather than doing your own comparison so you
  136. are consistent about edge effects.
  137. */
  138. int Fl::event_inside(const Fl_Widget *o) /*const*/ {
  139. int mx = e_x - o->x();
  140. int my = e_y - o->y();
  141. return (mx >= 0 && mx < o->w() && my >= 0 && my < o->h());
  142. }
  143. //
  144. //
  145. // timer support
  146. //
  147. #ifdef WIN32
  148. /// implementation in Fl_win32.cxx
  149. #elif defined(__APPLE__)
  150. /// implementation in Fl_mac.cxx
  151. #else
  152. //
  153. // X11 timers
  154. //
  155. ////////////////////////////////////////////////////////////////
  156. // Timeouts are stored in a sorted list, so only the first one needs
  157. // to be checked to see if any should be called.
  158. struct Timeout {
  159. double time;
  160. void (*cb)(void*);
  161. void* arg;
  162. Timeout* next;
  163. };
  164. static Timeout* first_timeout, *free_timeout;
  165. static int first_timeout_count, free_timeout_count;
  166. #include <sys/time.h>
  167. // I avoid the overhead of getting the current time when we have no
  168. // timeouts by setting this flag instead of getting the time.
  169. // In this case calling elapse_timeouts() does nothing, but records
  170. // the current time, and the next call will actualy elapse time.
  171. static char reset_clock = 1;
  172. static void elapse_timeouts() {
  173. static struct timeval prevclock;
  174. struct timeval newclock;
  175. gettimeofday(&newclock, NULL);
  176. double elapsed = newclock.tv_sec - prevclock.tv_sec +
  177. (newclock.tv_usec - prevclock.tv_usec)/1000000.0;
  178. prevclock.tv_sec = newclock.tv_sec;
  179. prevclock.tv_usec = newclock.tv_usec;
  180. if (reset_clock) {
  181. reset_clock = 0;
  182. } else if (elapsed > 0) {
  183. for (Timeout* t = first_timeout; t; t = t->next) t->time -= elapsed;
  184. }
  185. }
  186. // Continuously-adjusted error value, this is a number <= 0 for how late
  187. // we were at calling the last timeout. This appears to make repeat_timeout
  188. // very accurate even when processing takes a significant portion of the
  189. // time interval:
  190. static double missed_timeout_by;
  191. void Fl::add_timeout(double time, Fl_Timeout_Handler cb, void *argp) {
  192. elapse_timeouts();
  193. repeat_timeout(time, cb, argp);
  194. }
  195. void Fl::repeat_timeout(double time, Fl_Timeout_Handler cb, void *argp) {
  196. time += missed_timeout_by; if (time < -.05) time = 0;
  197. Timeout* t = free_timeout;
  198. if (t) {
  199. free_timeout = t->next;
  200. --free_timeout_count;
  201. } else {
  202. t = new Timeout;
  203. }
  204. t->time = time;
  205. t->cb = cb;
  206. t->arg = argp;
  207. // insert-sort the new timeout:
  208. Timeout** p = &first_timeout;
  209. while (*p && (*p)->time <= time) p = &((*p)->next);
  210. t->next = *p;
  211. *p = t;
  212. }
  213. /**
  214. Returns true if the timeout exists and has not been called yet.
  215. */
  216. int Fl::has_timeout(Fl_Timeout_Handler cb, void *argp) {
  217. for (Timeout* t = first_timeout; t; t = t->next)
  218. if (t->cb == cb && t->arg == argp) return 1;
  219. return 0;
  220. }
  221. /**
  222. Removes a timeout callback. It is harmless to remove a timeout
  223. callback that no longer exists.
  224. */
  225. void Fl::remove_timeout(Fl_Timeout_Handler cb, void *argp) {
  226. // This version removes all matching timeouts, not just the first one.
  227. // This may change in the future.
  228. for (Timeout** p = &first_timeout; *p;) {
  229. Timeout* t = *p;
  230. if (t->cb == cb && (t->arg == argp || !argp)) {
  231. *p = t->next;
  232. t->next = free_timeout;
  233. free_timeout = t;
  234. } else {
  235. p = &(t->next);
  236. }
  237. }
  238. }
  239. #endif
  240. ////////////////////////////////////////////////////////////////
  241. // Checks are just stored in a list. They are called in the reverse
  242. // order that they were added (this may change in the future).
  243. // This is a bit messy because I want to allow checks to be added,
  244. // removed, and have wait() called from inside them, to do this
  245. // next_check points at the next unprocessed one for the outermost
  246. // call to Fl::wait().
  247. struct Check {
  248. void (*cb)(void*);
  249. void* arg;
  250. Check* next;
  251. };
  252. static Check *first_check, *next_check, *free_check;
  253. /**
  254. FLTK will call this callback just before it flushes the display and
  255. waits for events. This is different than an idle callback because it
  256. is only called once, then FLTK calls the system and tells it not to
  257. return until an event happens.
  258. This can be used by code that wants to monitor the
  259. application's state, such as to keep a display up to date. The
  260. advantage of using a check callback is that it is called only when no
  261. events are pending. If events are coming in quickly, whole blocks of
  262. them will be processed before this is called once. This can save
  263. significant time and avoid the application falling behind the events.
  264. Sample code:
  265. \code
  266. bool state_changed; // anything that changes the display turns this on
  267. void callback(void*) {
  268. if (!state_changed) return;
  269. state_changed = false;
  270. do_expensive_calculation();
  271. widget-&gt;redraw();
  272. }
  273. main() {
  274. Fl::add_check(callback);
  275. return Fl::run();
  276. }
  277. \endcode
  278. */
  279. void Fl::add_check(Fl_Timeout_Handler cb, void *argp) {
  280. Check* t = free_check;
  281. if (t) free_check = t->next;
  282. else t = new Check;
  283. t->cb = cb;
  284. t->arg = argp;
  285. t->next = first_check;
  286. if (next_check == first_check) next_check = t;
  287. first_check = t;
  288. }
  289. /**
  290. Removes a check callback. It is harmless to remove a check
  291. callback that no longer exists.
  292. */
  293. void Fl::remove_check(Fl_Timeout_Handler cb, void *argp) {
  294. for (Check** p = &first_check; *p;) {
  295. Check* t = *p;
  296. if (t->cb == cb && t->arg == argp) {
  297. if (next_check == t) next_check = t->next;
  298. *p = t->next;
  299. t->next = free_check;
  300. free_check = t;
  301. } else {
  302. p = &(t->next);
  303. }
  304. }
  305. }
  306. /**
  307. Returns 1 if the check exists and has not been called yet, 0 otherwise.
  308. */
  309. int Fl::has_check(Fl_Timeout_Handler cb, void *argp) {
  310. for (Check** p = &first_check; *p;) {
  311. Check* t = *p;
  312. if (t->cb == cb && t->arg == argp) {
  313. return 1;
  314. } else {
  315. p = &(t->next);
  316. }
  317. }
  318. return 0;
  319. }
  320. static void run_checks()
  321. {
  322. // checks are a bit messy so that add/remove and wait may be called
  323. // from inside them without causing an infinite loop:
  324. if (next_check == first_check) {
  325. while (next_check) {
  326. Check* checkp = next_check;
  327. next_check = checkp->next;
  328. (checkp->cb)(checkp->arg);
  329. }
  330. next_check = first_check;
  331. }
  332. }
  333. #ifndef WIN32
  334. static char in_idle;
  335. #endif
  336. ////////////////////////////////////////////////////////////////
  337. // wait/run/check/ready:
  338. void (*Fl::idle)(); // see Fl_add_idle.cxx for the add/remove functions
  339. extern int fl_ready(); // in Fl_<platform>.cxx
  340. extern int fl_wait(double time); // in Fl_<platform>.cxx
  341. /**
  342. See int wait()
  343. */
  344. double Fl::wait(double time_to_wait) {
  345. // delete all widgets that were listed during callbacks
  346. do_widget_deletion();
  347. #ifdef WIN32
  348. return fl_wait(time_to_wait);
  349. #elif defined(__APPLE__)
  350. run_checks();
  351. if (idle) {
  352. if (!in_idle) {
  353. in_idle = 1;
  354. idle();
  355. in_idle = 0;
  356. }
  357. // the idle function may turn off idle, we can then wait:
  358. if (idle) time_to_wait = 0.0;
  359. }
  360. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  361. flush();
  362. if (idle && !in_idle) // 'idle' may have been set within flush()
  363. time_to_wait = 0.0;
  364. double retval = fl_wait(time_to_wait);
  365. [pool release];
  366. return retval;
  367. #else
  368. if (first_timeout) {
  369. elapse_timeouts();
  370. Timeout *t;
  371. while ((t = first_timeout)) {
  372. if (t->time > 0) break;
  373. // The first timeout in the array has expired.
  374. missed_timeout_by = t->time;
  375. // We must remove timeout from array before doing the callback:
  376. void (*cb)(void*) = t->cb;
  377. void *argp = t->arg;
  378. first_timeout = t->next;
  379. t->next = free_timeout;
  380. free_timeout = t;
  381. ++free_timeout_count;
  382. --first_timeout_count;
  383. // Now it is safe for the callback to do add_timeout:
  384. cb(argp);
  385. }
  386. } else {
  387. reset_clock = 1; // we are not going to check the clock
  388. }
  389. run_checks();
  390. // if (idle && !fl_ready()) {
  391. if (idle) {
  392. if (!in_idle) {
  393. in_idle = 1;
  394. idle();
  395. in_idle = 0;
  396. }
  397. // the idle function may turn off idle, we can then wait:
  398. if (idle) time_to_wait = 0.0;
  399. }
  400. if (first_timeout && first_timeout->time < time_to_wait)
  401. time_to_wait = first_timeout->time;
  402. if (time_to_wait <= 0.0) {
  403. // do flush second so that the results of events are visible:
  404. int ret = fl_wait(0.0);
  405. flush();
  406. return ret;
  407. } else {
  408. // do flush first so that user sees the display:
  409. flush();
  410. if (idle && !in_idle) // 'idle' may have been set within flush()
  411. time_to_wait = 0.0;
  412. return fl_wait(time_to_wait);
  413. }
  414. #endif
  415. }
  416. #define FOREVER 1e20
  417. /**
  418. As long as any windows are displayed this calls Fl::wait()
  419. repeatedly. When all the windows are closed it returns zero
  420. (supposedly it would return non-zero on any errors, but FLTK calls
  421. exit directly for these). A normal program will end main()
  422. with return Fl::run();.
  423. */
  424. int Fl::run() {
  425. while (Fl_X::first) wait(FOREVER);
  426. return 0;
  427. }
  428. #ifdef WIN32
  429. // Function to initialize COM/OLE for usage. This must be done only once.
  430. // We define a flag to register whether we called it:
  431. static char oleInitialized = 0;
  432. // This calls the Windows function OleInitialize() exactly once.
  433. void fl_OleInitialize() {
  434. if (!oleInitialized) {
  435. OleInitialize(0L);
  436. oleInitialized = 1;
  437. }
  438. }
  439. // This calls the Windows function OleUninitialize() only, if
  440. // OleInitialize has been called before.
  441. void fl_OleUninitialize() {
  442. if (oleInitialized) {
  443. OleUninitialize();
  444. oleInitialized = 0;
  445. }
  446. }
  447. class Fl_Win32_At_Exit {
  448. public:
  449. Fl_Win32_At_Exit() { }
  450. ~Fl_Win32_At_Exit() {
  451. fl_free_fonts(); // do some WIN32 cleanup
  452. fl_cleanup_pens();
  453. fl_OleUninitialize();
  454. fl_brush_action(1);
  455. fl_cleanup_dc_list();
  456. }
  457. };
  458. static Fl_Win32_At_Exit win32_at_exit;
  459. #endif
  460. /**
  461. Waits until "something happens" and then returns. Call this
  462. repeatedly to "run" your program. You can also check what happened
  463. each time after this returns, which is quite useful for managing
  464. program state.
  465. What this really does is call all idle callbacks, all elapsed
  466. timeouts, call Fl::flush() to get the screen to update, and
  467. then wait some time (zero if there are idle callbacks, the shortest of
  468. all pending timeouts, or infinity), for any events from the user or
  469. any Fl::add_fd() callbacks. It then handles the events and
  470. calls the callbacks and then returns.
  471. The return value of the first form is non-zero if there are
  472. any visible windows - this may change in future versions of
  473. FLTK.
  474. The second form waits a maximum of <i>time</i>
  475. seconds. <i>It can return much sooner if something happens.</i>
  476. The return value is positive if an event or fd happens before the
  477. time elapsed. It is zero if nothing happens (on Win32 this will only
  478. return zero if <i>time</i> is zero). It is negative if an error
  479. occurs (this will happen on UNIX if a signal happens).
  480. */
  481. int Fl::wait() {
  482. if (!Fl_X::first) return 0;
  483. wait(FOREVER);
  484. return Fl_X::first != 0; // return true if there is a window
  485. }
  486. /**
  487. Same as Fl::wait(0). Calling this during a big calculation
  488. will keep the screen up to date and the interface responsive:
  489. \code
  490. while (!calculation_done()) {
  491. calculate();
  492. Fl::check();
  493. if (user_hit_abort_button()) break;
  494. }
  495. \endcode
  496. The returns non-zero if any windows are displayed, and 0 if no
  497. windows are displayed (this is likely to change in future versions of
  498. FLTK).
  499. */
  500. int Fl::check() {
  501. wait(0.0);
  502. return Fl_X::first != 0; // return true if there is a window
  503. }
  504. /**
  505. This is similar to Fl::check() except this does \e not
  506. call Fl::flush() or any callbacks, which is useful if your
  507. program is in a state where such callbacks are illegal. This returns
  508. true if Fl::check() would do anything (it will continue to
  509. return true until you call Fl::check() or Fl::wait()).
  510. \code
  511. while (!calculation_done()) {
  512. calculate();
  513. if (Fl::ready()) {
  514. do_expensive_cleanup();
  515. Fl::check();
  516. if (user_hit_abort_button()) break;
  517. }
  518. }
  519. \endcode
  520. */
  521. int Fl::ready() {
  522. #if ! defined( WIN32 ) && ! defined(__APPLE__)
  523. if (first_timeout) {
  524. elapse_timeouts();
  525. if (first_timeout->time <= 0) return 1;
  526. } else {
  527. reset_clock = 1;
  528. }
  529. #endif
  530. return fl_ready();
  531. }
  532. ////////////////////////////////////////////////////////////////
  533. // Window list management:
  534. #ifndef FL_DOXYGEN
  535. Fl_X* Fl_X::first;
  536. #endif
  537. Fl_Window* fl_find(Window xid) {
  538. Fl_X *window;
  539. for (Fl_X **pp = &Fl_X::first; (window = *pp); pp = &window->next)
  540. #if defined(WIN32) || defined(USE_X11)
  541. if (window->xid == xid)
  542. #elif defined(__APPLE_QUARTZ__)
  543. if (window->xid == xid && !window->w->window())
  544. #else
  545. # error unsupported platform
  546. #endif // __APPLE__
  547. {
  548. if (window != Fl_X::first && !Fl::modal()) {
  549. // make this window be first to speed up searches
  550. // this is not done if modal is true to avoid messing up modal stack
  551. *pp = window->next;
  552. window->next = Fl_X::first;
  553. Fl_X::first = window;
  554. }
  555. return window->w;
  556. }
  557. return 0;
  558. }
  559. /**
  560. Returns the first top-level window in the list of shown() windows. If
  561. a modal() window is shown this is the top-most modal window, otherwise
  562. it is the most recent window to get an event.
  563. The second form sets the window that is returned by
  564. first_window. The window is removed from wherever it is in the
  565. list and inserted at the top. This is not done if Fl::modal()
  566. is on or if the window is not shown(). Because the first window
  567. is used to set the "parent" of modal windows, this is often
  568. useful.
  569. */
  570. Fl_Window* Fl::first_window() {
  571. Fl_X* i = Fl_X::first;
  572. return i ? i->w : 0;
  573. }
  574. /**
  575. Returns the next top-level window in the list of shown() windows. You can
  576. use this call to iterate through all the windows that are shown().
  577. */
  578. Fl_Window* Fl::next_window(const Fl_Window* window) {
  579. Fl_X* i = Fl_X::i(window)->next;
  580. return i ? i->w : 0;
  581. }
  582. /**
  583. See Fl_Window* first_window()
  584. */
  585. void Fl::first_window(Fl_Window* window) {
  586. if (!window || !window->shown()) return;
  587. fl_find(fl_xid(window));
  588. }
  589. /**
  590. Redraws all widgets.
  591. */
  592. void Fl::redraw() {
  593. for (Fl_X* i = Fl_X::first; i; i = i->next) i->w->redraw();
  594. }
  595. /**
  596. Causes all the windows that need it to be redrawn and graphics forced
  597. out through the pipes.
  598. This is what wait() does before looking for events.
  599. Note: in multi-threaded applications you should only call Fl::flush()
  600. from the main thread. If a child thread needs to trigger a redraw event,
  601. it should instead call Fl::awake() to get the main thread to process the
  602. event queue.
  603. */
  604. void Fl::flush() {
  605. if (damage()) {
  606. damage_ = 0;
  607. for (Fl_X* i = Fl_X::first; i; i = i->next) {
  608. if (i->wait_for_expose) {damage_ = 1; continue;}
  609. Fl_Window* wi = i->w;
  610. if (!wi->visible_r()) continue;
  611. if (wi->damage()) {i->flush(); wi->clear_damage();}
  612. // destroy damage regions for windows that don't use them:
  613. if (i->region) {XDestroyRegion(i->region); i->region = 0;}
  614. }
  615. }
  616. #if defined(USE_X11)
  617. if (fl_display) XFlush(fl_display);
  618. #elif defined(WIN32)
  619. GdiFlush();
  620. #elif defined (__APPLE_QUARTZ__)
  621. if (fl_gc)
  622. CGContextFlush(fl_gc);
  623. #else
  624. # error unsupported platform
  625. #endif
  626. }
  627. ////////////////////////////////////////////////////////////////
  628. // Event handlers:
  629. struct handler_link {
  630. int (*handle)(int);
  631. handler_link *next;
  632. };
  633. static handler_link *handlers = 0;
  634. /**
  635. Install a function to parse unrecognized events. If FLTK cannot
  636. figure out what to do with an event, it calls each of these functions
  637. (most recent first) until one of them returns non-zero. If none of
  638. them returns non zero then the event is ignored. Events that cause
  639. this to be called are:
  640. - FL_SHORTCUT events that are not recognized by any widget.
  641. This lets you provide global shortcut keys.
  642. - System events that FLTK does not recognize. See fl_xevent.
  643. - \e Some other events when the widget FLTK selected returns
  644. zero from its handle() method. Exactly which ones may change
  645. in future versions, however.
  646. */
  647. void Fl::add_handler(Fl_Event_Handler ha) {
  648. handler_link *l = new handler_link;
  649. l->handle = ha;
  650. l->next = handlers;
  651. handlers = l;
  652. }
  653. /**
  654. Removes a previously added event handler.
  655. */
  656. void Fl::remove_handler(Fl_Event_Handler ha) {
  657. handler_link *l, *p;
  658. // Search for the handler in the list...
  659. for (l = handlers, p = 0; l && l->handle != ha; p = l, l = l->next);
  660. if (l) {
  661. // Found it, so remove it from the list...
  662. if (p) p->next = l->next;
  663. else handlers = l->next;
  664. // And free the record...
  665. delete l;
  666. }
  667. }
  668. int (*fl_local_grab)(int); // used by fl_dnd.cxx
  669. static int send_handlers(int e) {
  670. for (const handler_link *hl = handlers; hl; hl = hl->next)
  671. if (hl->handle(e)) return 1;
  672. return 0;
  673. }
  674. ////////////////////////////////////////////////////////////////
  675. Fl_Widget* fl_oldfocus; // kludge for Fl_Group...
  676. /**
  677. Sets the widget that will receive FL_KEYBOARD events.
  678. If you change Fl::focus(), the previous widget and all
  679. parents (that don't contain the new widget) are sent FL_UNFOCUS
  680. events. Changing the focus does \e not send FL_FOCUS to
  681. this or any widget, because sending FL_FOCUS is supposed to
  682. \e test if the widget wants the focus (by it returning non-zero from
  683. handle()).
  684. \sa Fl_Widget::take_focus()
  685. */
  686. void Fl::focus(Fl_Widget *o) {
  687. if (o && !o->visible_focus()) return;
  688. if (grab()) return; // don't do anything while grab is on
  689. Fl_Widget *p = focus_;
  690. if (o != p) {
  691. Fl::compose_reset();
  692. focus_ = o;
  693. // make sure that fl_xfocus is set to the top level window
  694. // of this widget, or fl_fix_focus will clear our focus again
  695. if (o) {
  696. Fl_Window *win = 0, *w1 = o->window();
  697. while (w1) { win=w1; w1=win->window(); }
  698. if (win) fl_xfocus = win;
  699. }
  700. // take focus from the old focused window
  701. fl_oldfocus = 0;
  702. int old_event = e_number;
  703. e_number = FL_UNFOCUS;
  704. for (; p; p = p->parent()) {
  705. p->handle(FL_UNFOCUS);
  706. fl_oldfocus = p;
  707. }
  708. e_number = old_event;
  709. }
  710. }
  711. static char dnd_flag = 0; // make 'belowmouse' send DND_LEAVE instead of LEAVE
  712. /**
  713. Sets the widget that is below the mouse. This is for
  714. highlighting buttons. It is not used to send FL_PUSH or
  715. FL_MOVE directly, for several obscure reasons, but those events
  716. typically go to this widget. This is also the first widget tried for
  717. FL_SHORTCUT events.
  718. If you change the belowmouse widget, the previous one and all
  719. parents (that don't contain the new widget) are sent FL_LEAVE
  720. events. Changing this does \e not send FL_ENTER to this
  721. or any widget, because sending FL_ENTER is supposed to \e test
  722. if the widget wants the mouse (by it returning non-zero from
  723. handle()).
  724. */
  725. void Fl::belowmouse(Fl_Widget *o) {
  726. if (grab()) return; // don't do anything while grab is on
  727. Fl_Widget *p = belowmouse_;
  728. if (o != p) {
  729. belowmouse_ = o;
  730. int old_event = e_number;
  731. e_number = dnd_flag ? FL_DND_LEAVE : FL_LEAVE;
  732. for (; p && !p->contains(o); p = p->parent()) {
  733. p->handle(e_number);
  734. }
  735. e_number = old_event;
  736. }
  737. }
  738. /**
  739. Sets the widget that is being pushed. FL_DRAG or
  740. FL_RELEASE (and any more FL_PUSH) events will be sent to
  741. this widget.
  742. If you change the pushed widget, the previous one and all parents
  743. (that don't contain the new widget) are sent FL_RELEASE
  744. events. Changing this does \e not send FL_PUSH to this
  745. or any widget, because sending FL_PUSH is supposed to \e test
  746. if the widget wants the mouse (by it returning non-zero from
  747. handle()).
  748. */
  749. void Fl::pushed(Fl_Widget *o) {
  750. pushed_ = o;
  751. }
  752. static void nothing(Fl_Widget *) {}
  753. void (*Fl_Tooltip::enter)(Fl_Widget *) = nothing;
  754. void (*Fl_Tooltip::exit)(Fl_Widget *) = nothing;
  755. // Update modal(), focus() and other state according to system state,
  756. // and send FL_ENTER, FL_LEAVE, FL_FOCUS, and/or FL_UNFOCUS events.
  757. // This is the only function that produces these events in response
  758. // to system activity.
  759. // This is called whenever a window is added or hidden, and whenever
  760. // X says the focus or mouse window have changed.
  761. void fl_fix_focus() {
  762. #ifdef DEBUG
  763. puts("fl_fix_focus();");
  764. #endif // DEBUG
  765. if (Fl::grab()) return; // don't do anything while grab is on.
  766. // set focus based on Fl::modal() and fl_xfocus
  767. Fl_Widget* w = fl_xfocus;
  768. if (w) {
  769. int saved = Fl::e_keysym;
  770. if (Fl::e_keysym < (FL_Button + FL_LEFT_MOUSE) ||
  771. Fl::e_keysym > (FL_Button + FL_RIGHT_MOUSE))
  772. Fl::e_keysym = 0; // make sure widgets don't think a keystroke moved focus
  773. while (w->parent()) w = w->parent();
  774. if (Fl::modal()) w = Fl::modal();
  775. if (!w->contains(Fl::focus()))
  776. if (!w->take_focus()) Fl::focus(w);
  777. Fl::e_keysym = saved;
  778. } else
  779. Fl::focus(0);
  780. // MRS: Originally we checked the button state, but a user reported that it
  781. // broke click-to-focus in FLWM?!?
  782. // if (!(Fl::event_state() & 0x7f00000 /*FL_BUTTONS*/)) {
  783. if (!Fl::pushed()) {
  784. // set belowmouse based on Fl::modal() and fl_xmousewin:
  785. w = fl_xmousewin;
  786. if (w) {
  787. if (Fl::modal()) w = Fl::modal();
  788. if (!w->contains(Fl::belowmouse())) {
  789. int old_event = Fl::e_number;
  790. w->handle(Fl::e_number = FL_ENTER);
  791. Fl::e_number = old_event;
  792. if (!w->contains(Fl::belowmouse())) Fl::belowmouse(w);
  793. } else {
  794. // send a FL_MOVE event so the enter/leave state is up to date
  795. Fl::e_x = Fl::e_x_root-fl_xmousewin->x();
  796. Fl::e_y = Fl::e_y_root-fl_xmousewin->y();
  797. int old_event = Fl::e_number;
  798. w->handle(Fl::e_number = FL_MOVE);
  799. Fl::e_number = old_event;
  800. }
  801. } else {
  802. Fl::belowmouse(0);
  803. Fl_Tooltip::enter(0);
  804. }
  805. }
  806. }
  807. #ifndef WIN32
  808. extern Fl_Widget *fl_selection_requestor; // from Fl_x.cxx
  809. #endif
  810. // This function is called by ~Fl_Widget() and by Fl_Widget::deactivate
  811. // and by Fl_Widget::hide(). It indicates that the widget does not want
  812. // to receive any more events, and also removes all global variables that
  813. // point at the widget.
  814. // I changed this from the 1.0.1 behavior, the older version could send
  815. // FL_LEAVE or FL_UNFOCUS events to the widget. This appears to not be
  816. // desirable behavior and caused flwm to crash.
  817. void fl_throw_focus(Fl_Widget *o) {
  818. #ifdef DEBUG
  819. printf("fl_throw_focus(o=%p)\n", o);
  820. #endif // DEBUG
  821. if (o->contains(Fl::pushed())) Fl::pushed_ = 0;
  822. #ifndef WIN32
  823. if (o->contains(fl_selection_requestor)) fl_selection_requestor = 0;
  824. #endif
  825. if (o->contains(Fl::belowmouse())) Fl::belowmouse_ = 0;
  826. if (o->contains(Fl::focus())) Fl::focus_ = 0;
  827. if (o == fl_xfocus) fl_xfocus = 0;
  828. if (o == Fl_Tooltip::current()) Fl_Tooltip::current(0);
  829. if (o == fl_xmousewin) fl_xmousewin = 0;
  830. Fl_Tooltip::exit(o);
  831. fl_fix_focus();
  832. }
  833. ////////////////////////////////////////////////////////////////
  834. // Call to->handle but first replace the mouse x/y with the correct
  835. // values to account for nested X windows. 'window' is the outermost
  836. // window the event was posted to by X:
  837. static int send(int event, Fl_Widget* to, Fl_Window* window) {
  838. int dx, dy;
  839. int old_event = Fl::e_number;
  840. if (window) {
  841. dx = window->x();
  842. dy = window->y();
  843. } else {
  844. dx = dy = 0;
  845. }
  846. for (const Fl_Widget* w = to; w; w = w->parent())
  847. if (w->type()>=FL_WINDOW) {dx -= w->x(); dy -= w->y();}
  848. int save_x = Fl::e_x; Fl::e_x += dx;
  849. int save_y = Fl::e_y; Fl::e_y += dy;
  850. int ret = to->handle(Fl::e_number = event);
  851. Fl::e_number = old_event;
  852. Fl::e_y = save_y;
  853. Fl::e_x = save_x;
  854. return ret;
  855. }
  856. int Fl::handle(int e, Fl_Window* window)
  857. /**
  858. Sends the event to a window for processing. Returns non-zero if any
  859. widget uses the event.
  860. */
  861. {
  862. e_number = e;
  863. if (fl_local_grab) return fl_local_grab(e);
  864. Fl_Widget* wi = window;
  865. switch (e) {
  866. case FL_CLOSE:
  867. if ( grab() || (modal() && window != modal()) ) return 0;
  868. wi->do_callback();
  869. return 1;
  870. case FL_SHOW:
  871. wi->Fl_Widget::show(); // this calls Fl_Widget::show(), not Fl_Window::show()
  872. return 1;
  873. case FL_HIDE:
  874. wi->Fl_Widget::hide(); // this calls Fl_Widget::hide(), not Fl_Window::hide()
  875. return 1;
  876. case FL_PUSH:
  877. #ifdef DEBUG
  878. printf("Fl::handle(e=%d, window=%p);\n", e, window);
  879. #endif // DEBUG
  880. if (grab()) wi = grab();
  881. else if (modal() && wi != modal()) return 0;
  882. pushed_ = wi;
  883. Fl_Tooltip::current(wi);
  884. if (send(e, wi, window)) return 1;
  885. // raise windows that are clicked on:
  886. window->show();
  887. return 1;
  888. case FL_DND_ENTER:
  889. case FL_DND_DRAG:
  890. dnd_flag = 1;
  891. break;
  892. case FL_DND_LEAVE:
  893. dnd_flag = 1;
  894. belowmouse(0);
  895. dnd_flag = 0;
  896. return 1;
  897. case FL_DND_RELEASE:
  898. wi = belowmouse();
  899. break;
  900. case FL_MOVE:
  901. case FL_DRAG:
  902. fl_xmousewin = window; // this should already be set, but just in case.
  903. if (pushed()) {
  904. wi = pushed();
  905. if (grab()) wi = grab();
  906. e_number = e = FL_DRAG;
  907. break;
  908. }
  909. if (modal() && wi != modal()) wi = 0;
  910. if (grab()) wi = grab();
  911. {Fl_Widget* pbm = belowmouse();
  912. int ret = (wi && send(e, wi, window));
  913. if (pbm != belowmouse()) {
  914. #ifdef DEBUG
  915. printf("Fl::handle(e=%d, window=%p);\n", e, window);
  916. #endif // DEBUG
  917. Fl_Tooltip::enter(belowmouse());
  918. }
  919. return ret;}
  920. case FL_RELEASE: {
  921. // printf("FL_RELEASE: window=%p, pushed() = %p, grab() = %p, modal() = %p\n",
  922. // window, pushed(), grab(), modal());
  923. if (grab()) {
  924. wi = grab();
  925. pushed_ = 0; // must be zero before callback is done!
  926. } else if (pushed()) {
  927. wi = pushed();
  928. pushed_ = 0; // must be zero before callback is done!
  929. } else if (modal() && wi != modal()) return 0;
  930. int r = send(e, wi, window);
  931. fl_fix_focus();
  932. return r;}
  933. case FL_UNFOCUS:
  934. window = 0;
  935. case FL_FOCUS:
  936. fl_xfocus = window;
  937. fl_fix_focus();
  938. return 1;
  939. case FL_KEYUP:
  940. // Send the key-up to the current focus. This is not
  941. // always the same widget that received the corresponding
  942. // FL_KEYBOARD event because focus may have changed.
  943. // Sending the KEYUP to the right KEYDOWN is possible, but
  944. // would require that we track the KEYDOWN for every possible
  945. // key stroke (users may hold down multiple keys!) and then
  946. // make sure that the widget still exists before sending
  947. // a KEYUP there. I believe that the current solution is
  948. // "close enough".
  949. for (wi = grab() ? grab() : focus(); wi; wi = wi->parent())
  950. if (send(FL_KEYUP, wi, window)) return 1;
  951. return 0;
  952. case FL_KEYBOARD:
  953. #ifdef DEBUG
  954. printf("Fl::handle(e=%d, window=%p);\n", e, window);
  955. #endif // DEBUG
  956. Fl_Tooltip::enter((Fl_Widget*)0);
  957. fl_xfocus = window; // this should not happen! But maybe it does:
  958. // Try it as keystroke, sending it to focus and all parents:
  959. for (wi = grab() ? grab() : focus(); wi; wi = wi->parent())
  960. if (send(FL_KEYBOARD, wi, window)) return 1;
  961. // recursive call to try shortcut:
  962. if (handle(FL_SHORTCUT, window)) return 1;
  963. // and then try a shortcut with the case of the text swapped, by
  964. // changing the text and falling through to FL_SHORTCUT case:
  965. {unsigned char* c = (unsigned char*)event_text(); // cast away const
  966. if (!isalpha(*c)) return 0;
  967. *c = isupper(*c) ? tolower(*c) : toupper(*c);}
  968. e_number = e = FL_SHORTCUT;
  969. case FL_SHORTCUT:
  970. if (grab()) {wi = grab(); break;} // send it to grab window
  971. // Try it as shortcut, sending to mouse widget and all parents:
  972. wi = belowmouse();
  973. if (!wi) {
  974. wi = modal();
  975. if (!wi) wi = window;
  976. } else if (wi->window() != first_window()) {
  977. if (send(FL_SHORTCUT, first_window(), first_window())) return 1;
  978. }
  979. for (; wi; wi = wi->parent()) {
  980. if (send(FL_SHORTCUT, wi, wi->window())) return 1;
  981. }
  982. // try using add_handle() functions:
  983. if (send_handlers(FL_SHORTCUT)) return 1;
  984. // make Escape key close windows:
  985. if (event_key()==FL_Escape) {
  986. wi = modal(); if (!wi) wi = window;
  987. wi->do_callback();
  988. return 1;
  989. }
  990. return 0;
  991. case FL_ENTER:
  992. #ifdef DEBUG
  993. printf("Fl::handle(e=%d, window=%p);\n", e, window);
  994. #endif // DEBUG
  995. fl_xmousewin = window;
  996. fl_fix_focus();
  997. Fl_Tooltip::enter(belowmouse());
  998. return 1;
  999. case FL_LEAVE:
  1000. #ifdef DEBUG
  1001. printf("Fl::handle(e=%d, window=%p);\n", e, window);
  1002. #endif // DEBUG
  1003. if (!pushed_) {
  1004. belowmouse(0);
  1005. Fl_Tooltip::enter(0);
  1006. }
  1007. if (window == fl_xmousewin) {fl_xmousewin = 0; fl_fix_focus();}
  1008. return 1;
  1009. case FL_MOUSEWHEEL:
  1010. fl_xfocus = window; // this should not happen! But maybe it does:
  1011. // Try sending it to the "grab" first
  1012. if (grab() && grab()!=modal() && grab()!=window) {
  1013. if (send(FL_MOUSEWHEEL, grab(), window)) return 1;
  1014. }
  1015. // Now try sending it to the "modal" window
  1016. if (modal()) {
  1017. send(FL_MOUSEWHEEL, modal(), window);
  1018. return 1;
  1019. }
  1020. // Finally try sending it to the window, the event occured in
  1021. if (send(FL_MOUSEWHEEL, window, window)) return 1;
  1022. default:
  1023. break;
  1024. }
  1025. if (wi && send(e, wi, window)) {
  1026. dnd_flag = 0;
  1027. return 1;
  1028. }
  1029. dnd_flag = 0;
  1030. return send_handlers(e);
  1031. }
  1032. ////////////////////////////////////////////////////////////////
  1033. // hide() destroys the X window, it does not do unmap!
  1034. #if !defined(WIN32) && USE_XFT
  1035. extern void fl_destroy_xft_draw(Window);
  1036. #endif
  1037. void Fl_Window::hide() {
  1038. clear_visible();
  1039. if (!shown()) return;
  1040. // remove from the list of windows:
  1041. Fl_X* ip = i;
  1042. Fl_X** pp = &Fl_X::first;
  1043. for (; *pp != ip; pp = &(*pp)->next) if (!*pp) return;
  1044. *pp = ip->next;
  1045. #ifdef __APPLE__
  1046. MacUnlinkWindow(ip);
  1047. // MacOS X manages a single pointer per application. Make sure that hiding
  1048. // a toplevel window will not leave us with some random pointer shape, or
  1049. // worst case, an invisible pointer
  1050. if (!parent()) cursor(FL_CURSOR_DEFAULT);
  1051. #endif
  1052. i = 0;
  1053. // recursively remove any subwindows:
  1054. for (Fl_X *wi = Fl_X::first; wi;) {
  1055. Fl_Window* W = wi->w;
  1056. if (W->window() == this) {
  1057. W->hide();
  1058. W->set_visible();
  1059. wi = Fl_X::first;
  1060. } else wi = wi->next;
  1061. }
  1062. if (this == Fl::modal_) { // we are closing the modal window, find next one:
  1063. Fl_Window* W;
  1064. for (W = Fl::first_window(); W; W = Fl::next_window(W))
  1065. if (W->modal()) break;
  1066. Fl::modal_ = W;
  1067. }
  1068. // Make sure no events are sent to this window:
  1069. fl_throw_focus(this);
  1070. handle(FL_HIDE);
  1071. #if defined(WIN32)
  1072. // this little trick keeps the current clipboard alive, even if we are about
  1073. // to destroy the window that owns the selection.
  1074. if (GetClipboardOwner()==ip->xid) {
  1075. Fl_Window *w1 = Fl::first_window();
  1076. if (w1 && OpenClipboard(fl_xid(w1))) {
  1077. EmptyClipboard();
  1078. SetClipboardData(CF_TEXT, NULL);
  1079. CloseClipboard();
  1080. }
  1081. }
  1082. // Send a message to myself so that I'll get out of the event loop...
  1083. PostMessage(ip->xid, WM_APP, 0, 0);
  1084. if (ip->private_dc) fl_release_dc(ip->xid, ip->private_dc);
  1085. if (ip->xid == fl_window && fl_gc) {
  1086. fl_release_dc(fl_window, fl_gc);
  1087. fl_window = (HWND)-1;
  1088. fl_gc = 0;
  1089. # ifdef USE_CAIRO
  1090. if (Fl::cairo_autolink_context()) Fl::cairo_make_current((Fl_Window*) 0);
  1091. # endif
  1092. }
  1093. #elif defined(__APPLE_QUARTZ__)
  1094. Fl_X::q_release_context(ip);
  1095. if ( ip->xid == fl_window && !parent() )
  1096. fl_window = 0;
  1097. #endif
  1098. if (ip->region) XDestroyRegion(ip->region);
  1099. #if defined(USE_X11)
  1100. # if USE_XFT
  1101. fl_destroy_xft_draw(ip->xid);
  1102. # endif
  1103. XDestroyWindow(fl_display, ip->xid);
  1104. #elif defined(WIN32)
  1105. // this little trickery seems to avoid the popup window stacking problem
  1106. HWND p = GetForegroundWindow();
  1107. if (p==GetParent(ip->xid)) {
  1108. ShowWindow(ip->xid, SW_HIDE);
  1109. ShowWindow(p, SW_SHOWNA);
  1110. }
  1111. XDestroyWindow(fl_display, ip->xid);
  1112. #elif defined(__APPLE_QUARTZ__)
  1113. MacDestroyWindow(this, ip->xid);
  1114. #else
  1115. # error unsupported platform
  1116. #endif
  1117. #ifdef WIN32
  1118. // Try to stop the annoying "raise another program" behavior
  1119. if (non_modal() && Fl::first_window() && Fl::first_window()->shown())
  1120. Fl::first_window()->show();
  1121. #endif
  1122. delete ip;
  1123. }
  1124. Fl_Window::~Fl_Window() {
  1125. hide();
  1126. if (xclass_) {
  1127. free(xclass_);
  1128. }
  1129. }
  1130. // FL_SHOW and FL_HIDE are called whenever the visibility of this widget
  1131. // or any parent changes. We must correctly map/unmap the system's window.
  1132. // For top-level windows it is assumed the window has already been
  1133. // mapped or unmapped!!! This is because this should only happen when
  1134. // Fl_Window::show() or Fl_Window::hide() is called, or in response to
  1135. // iconize/deiconize events from the system.
  1136. int Fl_Window::handle(int ev)
  1137. {
  1138. if (parent()) {
  1139. switch (ev) {
  1140. case FL_SHOW:
  1141. if (!shown()) show();
  1142. else {
  1143. #if defined(USE_X11) || defined(WIN32)
  1144. XMapWindow(fl_display, fl_xid(this)); // extra map calls are harmless
  1145. #elif defined(__APPLE_QUARTZ__)
  1146. MacMapWindow(this, i->xid);
  1147. #else
  1148. # error unsupported platform
  1149. #endif // __APPLE__
  1150. }
  1151. break;
  1152. case FL_HIDE:
  1153. if (shown()) {
  1154. // Find what really turned invisible, if is was a parent window
  1155. // we do nothing. We need to avoid unnecessary unmap calls
  1156. // because they cause the display to blink when the parent is
  1157. // remapped. However if this or any intermediate non-window
  1158. // widget has really had hide() called directly on it, we must
  1159. // unmap because when the parent window is remapped we don't
  1160. // want to reappear.
  1161. if (visible()) {
  1162. Fl_Widget* p = parent(); for (;p->visible();p = p->parent()) {}
  1163. if (p->type() >= FL_WINDOW) break; // don't do the unmap
  1164. }
  1165. #if defined(USE_X11) || defined(WIN32)
  1166. XUnmapWindow(fl_display, fl_xid(this));
  1167. #elif defined(__APPLE_QUARTZ__)
  1168. MacUnmapWindow(this, i->xid);
  1169. #else
  1170. # error platform unsupported
  1171. #endif
  1172. }
  1173. break;
  1174. }
  1175. // } else if (ev == FL_FOCUS || ev == FL_UNFOCUS) {
  1176. // Fl_Tooltip::exit(Fl_Tooltip::current());
  1177. }
  1178. return Fl_Group::handle(ev);
  1179. }
  1180. ////////////////////////////////////////////////////////////////
  1181. // Back compatibility cut & paste functions for fltk 1.1 only:
  1182. /** Back-compatibility only: The single-argument call can be used to
  1183. move the selection to another widget or to set the owner to
  1184. NULL, without changing the actual text of the
  1185. selection. FL_SELECTIONCLEAR is sent to the previous
  1186. selection owner, if any.
  1187. <i>Copying the buffer every time the selection is changed is
  1188. obviously wasteful, especially for large selections. An interface will
  1189. probably be added in a future version to allow the selection to be made
  1190. by a callback function. The current interface will be emulated on top
  1191. of this.</i>
  1192. */
  1193. void Fl::selection_owner(Fl_Widget *owner) {selection_owner_ = owner;}
  1194. /**
  1195. Changes the current selection. The block of text is
  1196. copied to an internal buffer by FLTK (be careful if doing this in
  1197. response to an FL_PASTE as this \e may be the same buffer
  1198. returned by event_text()). The selection_owner()
  1199. widget is set to the passed owner.
  1200. */
  1201. void Fl::selection(Fl_Widget &owner, const char* text, int len) {
  1202. selection_owner_ = &owner;
  1203. Fl::copy(text, len, 0);
  1204. }
  1205. /** Backward compatibility only:
  1206. Set things up so the receiver widget will be called with an FL_PASTE event some
  1207. time in the future for the specified clipboard. The reciever
  1208. should be prepared to be called \e directly by this, or for
  1209. it to happen \e later, or possibly <i>not at all</i>. This
  1210. allows the window system to take as long as necessary to retrieve
  1211. the paste buffer (or even to screw up completely) without complex
  1212. and error-prone synchronization code in FLTK.
  1213. \see Fl::paste(Fl_Widget &receiver, int clipboard)
  1214. */
  1215. void Fl::paste(Fl_Widget &receiver) {
  1216. Fl::paste(receiver, 0);
  1217. }
  1218. ////////////////////////////////////////////////////////////////
  1219. #include <FL/fl_draw.H>
  1220. void Fl_Widget::redraw() {
  1221. damage(FL_DAMAGE_ALL);
  1222. }
  1223. void Fl_Widget::redraw_label() {
  1224. if (window()) {
  1225. if (box() == FL_NO_BOX) {
  1226. // Widgets with the FL_NO_BOX boxtype need a parent to
  1227. // redraw, since it is responsible for redrawing the
  1228. // background...
  1229. int X = x() > 0 ? x() - 1 : 0;
  1230. int Y = y() > 0 ? y() - 1 : 0;
  1231. window()->damage(FL_DAMAGE_ALL, X, Y, w() + 2, h() + 2);
  1232. }
  1233. if (align() && !(align() & FL_ALIGN_INSIDE) && window()->shown()) {
  1234. // If the label is not inside the widget, compute the location of
  1235. // the label and redraw the window within that bounding box...
  1236. int W = 0, H = 0;
  1237. label_.measure(W, H);
  1238. W += 5; // Add a little to the size of the label to cover overflow
  1239. H += 5;
  1240. // FIXME:
  1241. // This assumes the measure() returns the correct outline, which it does
  1242. // not in all possible cases of alignment combinedwith image and symbols.
  1243. switch (align() & 0x0f) {
  1244. case FL_ALIGN_TOP_LEFT:
  1245. window()->damage(FL_DAMAGE_EXPOSE, x(), y()-H, W, H); break;
  1246. case FL_ALIGN_TOP:
  1247. window()->damage(FL_DAMAGE_EXPOSE, x()+(w()-W)/2, y()-H, W, H); break;
  1248. case FL_ALIGN_TOP_RIGHT:
  1249. window()->damage(FL_DAMAGE_EXPOSE, x()+w()-W, y()-H, W, H); break;
  1250. case FL_ALIGN_LEFT_TOP:
  1251. window()->damage(FL_DAMAGE_EXPOSE, x()-W, y(), W, H); break;
  1252. case FL_ALIGN_RIGHT_TOP:
  1253. window()->damage(FL_DAMAGE_EXPOSE, x()+w(), y(), W, H); break;
  1254. case FL_ALIGN_LEFT:
  1255. window()->damage(FL_DAMAGE_EXPOSE, x()-W, y()+(h()-H)/2, W, H); break;
  1256. case FL_ALIGN_RIGHT:
  1257. window()->damage(FL_DAMAGE_EXPOSE, x()+w(), y()+(h()-H)/2, W, H); break;
  1258. case FL_ALIGN_LEFT_BOTTOM:
  1259. window()->damage(FL_DAMAGE_EXPOSE, x()-W, y()+h()-H, W, H); break;
  1260. case FL_ALIGN_RIGHT_BOTTOM:
  1261. window()->damage(FL_DAMAGE_EXPOSE, x()+w(), y()+h()-H, W, H); break;
  1262. case FL_ALIGN_BOTTOM_LEFT:
  1263. window()->damage(FL_DAMAGE_EXPOSE, x(), y()+h(), W, H); break;
  1264. case FL_ALIGN_BOTTOM:
  1265. window()->damage(FL_DAMAGE_EXPOSE, x()+(w()-W)/2, y()+h(), W, H); break;
  1266. case FL_ALIGN_BOTTOM_RIGHT:
  1267. window()->damage(FL_DAMAGE_EXPOSE, x()+w()-W, y()+h(), W, H); break;
  1268. default:
  1269. window()->damage(FL_DAMAGE_ALL); break;
  1270. }
  1271. } else {
  1272. // The label is inside the widget, so just redraw the widget itself...
  1273. damage(FL_DAMAGE_ALL);
  1274. }
  1275. }
  1276. }
  1277. void Fl_Widget::damage(uchar fl) {
  1278. if (type() < FL_WINDOW) {
  1279. // damage only the rectangle covered by a child widget:
  1280. damage(fl, x(), y(), w(), h());
  1281. } else {
  1282. // damage entire window by deleting the region:
  1283. Fl_X* i = Fl_X::i((Fl_Window*)this);
  1284. if (!i) return; // window not mapped, so ignore it
  1285. if (i->region) {XDestroyRegion(i->region); i->region = 0;}
  1286. damage_ |= fl;
  1287. Fl::damage(FL_DAMAGE_CHILD);
  1288. }
  1289. }
  1290. void Fl_Widget::damage(uchar fl, int X, int Y, int W, int H) {
  1291. Fl_Widget* wi = this;
  1292. // mark all parent widgets between this and window with FL_DAMAGE_CHILD:
  1293. while (wi->type() < FL_WINDOW) {
  1294. wi->damage_ |= fl;
  1295. wi = wi->parent();
  1296. if (!wi) return;
  1297. fl = FL_DAMAGE_CHILD;
  1298. }
  1299. Fl_X* i = Fl_X::i((Fl_Window*)wi);
  1300. if (!i) return; // window not mapped, so ignore it
  1301. // clip the damage to the window and quit if none:
  1302. if (X < 0) {W += X; X = 0;}
  1303. if (Y < 0) {H += Y; Y = 0;}
  1304. if (W > wi->w()-X) W = wi->w()-X;
  1305. if (H > wi->h()-Y) H = wi->h()-Y;
  1306. if (W <= 0 || H <= 0) return;
  1307. if (!X && !Y && W==wi->w() && H==wi->h()) {
  1308. // if damage covers entire window delete region:
  1309. wi->damage(fl);
  1310. return;
  1311. }
  1312. if (wi->damage()) {
  1313. // if we already have damage we must merge with existing region:
  1314. if (i->region) {
  1315. #if defined(USE_X11)
  1316. XRectangle R;
  1317. R.x = X; R.y = Y; R.width = W; R.height = H;
  1318. XUnionRectWithRegion(&R, i->region, i->region);
  1319. #elif defined(WIN32)
  1320. Fl_Region R = XRectangleRegion(X, Y, W, H);
  1321. CombineRgn(i->region, i->region, R, RGN_OR);
  1322. XDestroyRegion(R);
  1323. #elif defined(__APPLE_QUARTZ__)
  1324. CGRect arg = fl_cgrectmake_cocoa(X, Y, W, H);
  1325. int j; // don't add a rectangle totally inside the Fl_Region
  1326. for(j = 0; j < i->region->count; j++) {
  1327. if(CGRectContainsRect(i->region->rects[j], arg)) break;
  1328. }
  1329. if( j >= i->region->count) {
  1330. i->region->rects = (CGRect*)realloc(i->region->rects, (++(i->region->count)) * sizeof(CGRect));
  1331. i->region->rects[i->region->count - 1] = arg;
  1332. }
  1333. #else
  1334. # error unsupported platform
  1335. #endif
  1336. }
  1337. wi->damage_ |= fl;
  1338. } else {
  1339. // create a new region:
  1340. if (i->region) XDestroyRegion(i->region);
  1341. i->region = XRectangleRegion(X,Y,W,H);
  1342. wi->damage_ = fl;
  1343. }
  1344. Fl::damage(FL_DAMAGE_CHILD);
  1345. }
  1346. void Fl_Window::flush() {
  1347. make_current();
  1348. //if (damage() == FL_DAMAGE_EXPOSE && can_boxcheat(box())) fl_boxcheat = this;
  1349. fl_clip_region(i->region); i->region = 0;
  1350. draw();
  1351. }
  1352. #ifdef WIN32
  1353. # include "Fl_win32.cxx"
  1354. #elif defined(__APPLE__)
  1355. # include "Fl_cocoa.mm"
  1356. #endif
  1357. //
  1358. // The following methods allow callbacks to schedule the deletion of
  1359. // widgets at "safe" times.
  1360. //
  1361. static int num_dwidgets = 0, alloc_dwidgets = 0;
  1362. static Fl_Widget **dwidgets = 0;
  1363. /**
  1364. Schedules a widget for deletion at the next call to the event loop.
  1365. Use this method to delete a widget inside a callback function.
  1366. To avoid early deletion of widgets, this function should be called
  1367. toward the end of a callback and only after any call to the event
  1368. loop (Fl::wait(), Fl::flush(), Fl::check(), fl_ask(), etc.).
  1369. When deleting groups or windows, you must only delete the group or
  1370. window widget and not the individual child widgets.
  1371. \since FLTK 1.3 it is not necessary to remove widgets from their parent
  1372. groups or windows before calling this, because it will be done in the
  1373. widget's destructor, but it is not a failure to do this nevertheless.
  1374. \note In FLTK 1.1 you \b must remove widgets from their parent group
  1375. (or window) before deleting them.
  1376. \see Fl_Widget::~Fl_Widget()
  1377. */
  1378. void Fl::delete_widget(Fl_Widget *wi) {
  1379. if (!wi) return;
  1380. if (num_dwidgets >= alloc_dwidgets) {
  1381. Fl_Widget **temp;
  1382. temp = new Fl_Widget *[alloc_dwidgets + 10];
  1383. if (alloc_dwidgets) {
  1384. memcpy(temp, dwidgets, alloc_dwidgets * sizeof(Fl_Widget *));
  1385. delete[] dwidgets;
  1386. }
  1387. dwidgets = temp;
  1388. alloc_dwidgets += 10;
  1389. }
  1390. dwidgets[num_dwidgets] = wi;
  1391. num_dwidgets ++;
  1392. }
  1393. /**
  1394. Deletes widgets previously scheduled for deletion.
  1395. This is for internal use only. You should never call this directly.
  1396. Fl::do_widget_deletion() is called from the FLTK event loop or whenever
  1397. you call Fl::wait(). The previously scheduled widgets are deleted in the
  1398. same order they were scheduled by calling Fl::delete_widget().
  1399. \see Fl::delete_widget(Fl_Widget *wi)
  1400. */
  1401. void Fl::do_widget_deletion() {
  1402. if (!num_dwidgets) return;
  1403. for (int i = 0; i < num_dwidgets; i ++)
  1404. delete dwidgets[i];
  1405. num_dwidgets = 0;
  1406. }
  1407. static Fl_Widget ***widget_watch = 0;
  1408. static int num_widget_watch = 0;
  1409. static int max_widget_watch = 0;
  1410. /**
  1411. Adds a widget pointer to the widget watch list.
  1412. \note Internal use only, please use class Fl_Widget_Tracker instead.
  1413. This can be used, if it is possible that a widget might be deleted during
  1414. a callback or similar function. The widget pointer must be added to the
  1415. watch list before calling the callback. After the callback the widget
  1416. pointer can be queried, if it is NULL. \e If it is NULL, then the widget has been
  1417. deleted during the callback and must not be accessed anymore. If the widget
  1418. pointer is \e not NULL, then the widget has not been deleted and can be accessed
  1419. safely.
  1420. After accessing the widget, the widget pointer must be released from the
  1421. watch list by calling Fl::release_widget_pointer().
  1422. Example for a button that is clicked (from its handle() method):
  1423. \code
  1424. Fl_Widget *wp = this; // save 'this' in a pointer variable
  1425. Fl::watch_widget_pointer(wp); // add the pointer to the watch list
  1426. set_changed(); // set the changed flag
  1427. do_callback(); // call the callback
  1428. if (!wp) { // the widget has been deleted
  1429. // DO NOT ACCESS THE DELETED WIDGET !
  1430. } else { // the widget still exists
  1431. clear_changed(); // reset the changed flag
  1432. }
  1433. Fl::release_widget_pointer(wp); // remove the pointer from the watch list
  1434. \endcode
  1435. This works, because all widgets call Fl::clear_widget_pointer() in their
  1436. destructors.
  1437. \see Fl::release_widget_pointer()
  1438. \see Fl::clear_widget_pointer()
  1439. An easier and more convenient method to control widget deletion during
  1440. callbacks is to use the class Fl_Widget_Tracker with a local (automatic)
  1441. variable.
  1442. \see class Fl_Widget_Tracker
  1443. */
  1444. void Fl::watch_widget_pointer(Fl_Widget *&w)
  1445. {
  1446. Fl_Widget **wp = &w;
  1447. int i;
  1448. for (i=0; i<num_widget_watch; ++i) {
  1449. if (widget_watch[i]==wp) return;
  1450. }
  1451. if (num_widget_watch==max_widget_watch) {
  1452. max_widget_watch += 8;
  1453. widget_watch = (Fl_Widget***)realloc(widget_watch, sizeof(Fl_Widget**)*max_widget_watch);
  1454. }
  1455. widget_watch[num_widget_watch++] = wp;
  1456. #ifdef DEBUG_WATCH
  1457. printf ("\nwatch_widget_pointer: (%d/%d) %8p => %8p\n",
  1458. num_widget_watch,num_widget_watch,wp,*wp);
  1459. fflush(stdout);
  1460. #endif // DEBUG_WATCH
  1461. }
  1462. /**
  1463. Releases a widget pointer from the watch list.
  1464. This is used to remove a widget pointer that has been added to the watch list
  1465. with Fl::watch_widget_pointer(), when it is not needed anymore.
  1466. \note Internal use only, please use class Fl_Widget_Tracker instead.
  1467. \see Fl::watch_widget_pointer()
  1468. */
  1469. void Fl::release_widget_pointer(Fl_Widget *&w)
  1470. {
  1471. Fl_Widget **wp = &w;
  1472. int i,j=0;
  1473. for (i=0; i<num_widget_watch; ++i) {
  1474. if (widget_watch[i]!=wp) {
  1475. if (j<i) widget_watch[j] = widget_watch[i]; // fill gap
  1476. j++;
  1477. }
  1478. #ifdef DEBUG_WATCH
  1479. else { // found widget pointer
  1480. printf ("release_widget_pointer: (%d/%d) %8p => %8p\n",
  1481. i+1,num_widget_watch,wp,*wp);
  1482. }
  1483. #endif //DEBUG_WATCH
  1484. }
  1485. num_widget_watch = j;
  1486. #ifdef DEBUG_WATCH
  1487. printf (" num_widget_watch = %d\n\n",num_widget_watch);
  1488. fflush(stdout);
  1489. #endif // DEBUG_WATCH
  1490. return;
  1491. }
  1492. /**
  1493. Clears a widget pointer \e in the watch list.
  1494. This is called when a widget is destroyed (by its destructor). You should never
  1495. call this directly.
  1496. \note Internal use only !
  1497. This method searches the widget watch list for pointers to the widget and
  1498. clears each pointer that points to it. Widget pointers can be added to the
  1499. widget watch list by calling Fl::watch_widget_pointer() or by using the
  1500. helper class Fl_Widget_Tracker (recommended).
  1501. \see Fl::watch_widget_pointer()
  1502. \see class Fl_Widget_Tracker
  1503. */
  1504. void Fl::clear_widget_pointer(Fl_Widget const *w)
  1505. {
  1506. if (w==0L) return;
  1507. int i;
  1508. for (i=0; i<num_widget_watch; ++i) {
  1509. if (widget_watch[i] && *widget_watch[i]==w) {
  1510. *widget_watch[i] = 0L;
  1511. }
  1512. }
  1513. }
  1514. /**
  1515. \brief User interface options management.
  1516. This function needs to be documented in more detail. It can be used for more
  1517. optional settings, such as using a native file chooser instead of the FLTK one
  1518. wherever possible, disabeling tooltips, disabeling visible focus, disabeling
  1519. FLTK file chooser preview, etc. .
  1520. There should be a command line option interface.
  1521. There should be an application that manages options system wide, per user, and
  1522. per application.
  1523. */
  1524. bool Fl::option(Fl_Option o)
  1525. {
  1526. if (!options_read_) {
  1527. int tmp;
  1528. { // first, read the system wide preferences
  1529. Fl_Preferences prefs(Fl_Preferences::SYSTEM, "fltk.org", "fltk");
  1530. Fl_Preferences opt(prefs, "options");
  1531. prefs.get("ArrowFocus", tmp, 0); options_[OPTION_ARROW_FOCUS] = tmp;
  1532. prefs.get("NativeFilechooser", tmp, 0); options_[OPTION_NATIVE_FILECHOOSER] = tmp;
  1533. }
  1534. { // next, check the user preferences
  1535. Fl_Preferences prefs(Fl_Preferences::USER, "fltk.org", "fltk");
  1536. Fl_Preferences opt(prefs, "options");
  1537. prefs.get("ArrowFocus", tmp, 0); options_[OPTION_ARROW_FOCUS] = tmp;
  1538. prefs.get("NativeFilechooser", tmp, 0); options_[OPTION_NATIVE_FILECHOOSER] = tmp;
  1539. }
  1540. { // now, if the developer has registered this app, we could as for per-application preferences
  1541. }
  1542. options_read_ = 1;
  1543. }
  1544. if (o<0 || o>=OPTION_LAST)
  1545. return false;
  1546. return (bool)options_[o];
  1547. }
  1548. // Helper class Fl_Widget_Tracker
  1549. /**
  1550. The constructor adds a widget to the watch list.
  1551. */
  1552. Fl_Widget_Tracker::Fl_Widget_Tracker(Fl_Widget *wi) {
  1553. wp_ = wi;
  1554. Fl::watch_widget_pointer(wp_); // add pointer to watch list
  1555. }
  1556. /**
  1557. The destructor removes a widget from the watch list.
  1558. */
  1559. Fl_Widget_Tracker::~Fl_Widget_Tracker() {
  1560. Fl::release_widget_pointer(wp_); // remove pointer from watch list
  1561. }
  1562. //
  1563. // End of "$Id: Fl.cxx 7903 2010-11-28 21:06:39Z matt $".
  1564. //