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.

2081 lines
62KB

  1. //
  2. // "$Id: Fl_win32.cxx 8759 2011-05-30 12:33:51Z manolo $"
  3. //
  4. // WIN32-specific 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. // This file contains win32-specific code for fltk which is always linked
  28. // in. Search other files for "WIN32" or filenames ending in _win32.cxx
  29. // for other system-specific code.
  30. // This file must be #include'd in Fl.cxx and not compiled separately.
  31. #ifndef FL_DOXYGEN
  32. #include <FL/Fl.H>
  33. #include <FL/fl_utf8.h>
  34. #include <FL/Fl_Window.H>
  35. #include <FL/fl_draw.H>
  36. #include <FL/Enumerations.H>
  37. #include <FL/Fl_Tooltip.H>
  38. #include <FL/Fl_Paged_Device.H>
  39. #include "flstring.h"
  40. #include "Fl_Font.H"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <sys/types.h>
  44. #include <time.h>
  45. #ifdef __CYGWIN__
  46. # include <sys/time.h>
  47. # include <unistd.h>
  48. #endif
  49. #if !defined(NO_TRACK_MOUSE)
  50. # include <commctrl.h> // TrackMouseEvent
  51. // fabien: Ms Visual Studio >= 2003 permit embedded lib reference
  52. // that makes fltk use easier as only fltk libs are now requested
  53. // This idea could be extended to fltk libs themselves,
  54. // implementer should then care about DLL linkage flags ...
  55. # if (_MSC_VER>=1310)
  56. # pragma comment (lib, "comctl32.lib")
  57. # endif
  58. #endif
  59. #if defined(__GNUC__)
  60. # include <wchar.h>
  61. #endif
  62. #include <ole2.h>
  63. #include <shellapi.h>
  64. #include "aimm.h"
  65. //
  66. // USE_ASYNC_SELECT - define it if you have WSAAsyncSelect()...
  67. // USE_ASYNC_SELECT is OBSOLETED in 1.3 for the following reasons:
  68. /*
  69. This feature was supposed to provide an efficient alternative to the current
  70. polling method, but as it has been discussed (Thanks Albrecht!) :
  71. - the async mode would imply to change the socket to non blocking mode.
  72. This can have unexpected side effects for 3rd party apps, especially
  73. if it is set on-the-fly when socket service is really needed, as it is
  74. done today and on purpose, but still the 3rd party developer wouldn't easily
  75. control the sequencing of socket operations.
  76. - Finer granularity of events furthered by the async select is a plus only
  77. for socket 3rd party impl., it is simply not needed for the 'light' fltk
  78. use we make of wsock, so here it would also be a bad point, because of all
  79. the logic add-ons necessary for using this functionality, without a clear
  80. benefit.
  81. So async mode select would not add benefits to fltk, worse, it can slowdown
  82. fltk because of this finer granularity and instrumentation code to be added
  83. for async mode proper operation, not mentioning the side effects...
  84. */
  85. static Fl_GDI_Graphics_Driver fl_gdi_driver;
  86. static Fl_Display_Device fl_gdi_display(&fl_gdi_driver);
  87. FL_EXPORT Fl_Graphics_Driver *fl_graphics_driver = (Fl_Graphics_Driver*)&fl_gdi_driver; // the current target driver of graphics operations
  88. Fl_Surface_Device* Fl_Surface_Device::_surface = (Fl_Surface_Device*)&fl_gdi_display; // the current target surface of graphics operations
  89. Fl_Display_Device *Fl_Display_Device::_display = &fl_gdi_display; // the platform display
  90. // dynamic wsock dll handling api:
  91. #if defined(__CYGWIN__) && !defined(SOCKET)
  92. # define SOCKET int
  93. #endif
  94. // note: winsock2.h has been #include'd in Fl.cxx
  95. #define WSCK_DLL_NAME "WS2_32.DLL"
  96. typedef int (WINAPI* fl_wsk_select_f)(int, fd_set*, fd_set*, fd_set*, const struct timeval*);
  97. typedef int (WINAPI* fl_wsk_fd_is_set_f)(SOCKET, fd_set *);
  98. static HMODULE s_wsock_mod = 0;
  99. static fl_wsk_select_f s_wsock_select = 0;
  100. static fl_wsk_fd_is_set_f fl_wsk_fd_is_set = 0;
  101. static HMODULE get_wsock_mod() {
  102. if (!s_wsock_mod) {
  103. s_wsock_mod = LoadLibrary(WSCK_DLL_NAME);
  104. if (s_wsock_mod==NULL)
  105. Fl::fatal("FLTK Lib Error: %s file not found! Please check your winsock dll accessibility.\n",WSCK_DLL_NAME);
  106. s_wsock_select = (fl_wsk_select_f) GetProcAddress(s_wsock_mod, "select");
  107. fl_wsk_fd_is_set = (fl_wsk_fd_is_set_f) GetProcAddress(s_wsock_mod, "__WSAFDIsSet");
  108. }
  109. return s_wsock_mod;
  110. }
  111. /*
  112. * Dynamic linking of imm32.dll
  113. * This library is only needed for a hand full (four ATM) functions relating to
  114. * international text rendering and locales. Dynamically loading reduces initial
  115. * size and link dependencies.
  116. */
  117. static HMODULE s_imm_module = 0;
  118. typedef HIMC (WINAPI* flTypeImmGetContext)(HWND);
  119. static flTypeImmGetContext flImmGetContext = 0;
  120. typedef BOOL (WINAPI* flTypeImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
  121. static flTypeImmSetCompositionWindow flImmSetCompositionWindow = 0;
  122. typedef BOOL (WINAPI* flTypeImmReleaseContext)(HWND, HIMC);
  123. static flTypeImmReleaseContext flImmReleaseContext = 0;
  124. typedef BOOL (WINAPI* flTypeImmIsIME)(HKL);
  125. static flTypeImmIsIME flImmIsIME = 0;
  126. static HMODULE get_imm_module() {
  127. if (!s_imm_module) {
  128. s_imm_module = LoadLibrary("IMM32.DLL");
  129. if (!s_imm_module)
  130. Fl::fatal("FLTK Lib Error: IMM32.DLL file not found!\n\n"
  131. "Please check your input method manager library accessibility.");
  132. flImmGetContext = (flTypeImmGetContext)GetProcAddress(s_imm_module, "ImmGetContext");
  133. flImmSetCompositionWindow = (flTypeImmSetCompositionWindow)GetProcAddress(s_imm_module, "ImmSetCompositionWindow");
  134. flImmReleaseContext = (flTypeImmReleaseContext)GetProcAddress(s_imm_module, "ImmReleaseContext");
  135. flImmIsIME = (flTypeImmIsIME)GetProcAddress(s_imm_module, "ImmIsIME");
  136. }
  137. return s_imm_module;
  138. }
  139. // USE_TRACK_MOUSE - define NO_TRACK_MOUSE if you don't have
  140. // TrackMouseEvent()...
  141. //
  142. // Now (Dec. 2008) we can assume that current Cygwin/MinGW versions
  143. // support the TrackMouseEvent() function, but WinCE obviously doesn't
  144. // support it (STR 2095). Therefore, USE_TRACK_MOUSE is enabled by
  145. // default, but you can disable it by defining NO_TRACK_MOUSE.
  146. //
  147. // TrackMouseEvent is only used to support window leave notifications
  148. // under Windows. It can be used to get FL_LEAVE events, when the
  149. // mouse leaves the _main_ application window (FLTK detects subwindow
  150. // leave events by using normal move events).
  151. //
  152. // Implementation note: If the mouse cursor leaves one subwindow and
  153. // enters another window, then Windows sends a WM_MOUSEMOVE message to
  154. // the new window before it sends a WM_MOUSELEAVE message to the old
  155. // (just left) window. We save the current mouse window in a static variable,
  156. // and if we get a WM_MOUSELEAVE event for the current mouse window, this
  157. // means that the top level window has been left (otherwise we would have
  158. // got another WM_MOUSEMOVE message before).
  159. // #define NO_TRACK_MOUSE
  160. #if !defined(NO_TRACK_MOUSE)
  161. # define USE_TRACK_MOUSE
  162. #endif // NO_TRACK_MOUSE
  163. static Fl_Window *track_mouse_win=0; // current TrackMouseEvent() window
  164. // USE_CAPTURE_MOUSE_WIN - this must be defined for TrackMouseEvent to work
  165. // correctly with subwindows - otherwise a single mouse click and release
  166. // (without a move) would generate phantom leave events.
  167. // This defines, if the current mouse window (maybe a subwindow) or the
  168. // main window should get mouse events after pushing (and holding) a mouse
  169. // button, i.e. when dragging the mouse. This is done by calling SetCapture
  170. // (see below).
  171. #ifdef USE_TRACK_MOUSE
  172. #define USE_CAPTURE_MOUSE_WIN
  173. #endif // USE_TRACK_MOUSE
  174. //
  175. // WM_SYNCPAINT is an "undocumented" message, which is finally defined in
  176. // VC++ 6.0.
  177. //
  178. #ifndef WM_SYNCPAINT
  179. # define WM_SYNCPAINT 0x0088
  180. #endif
  181. #ifndef WM_MOUSELEAVE
  182. # define WM_MOUSELEAVE 0x02a3
  183. #endif
  184. #ifndef WM_MOUSEWHEEL
  185. # define WM_MOUSEWHEEL 0x020a
  186. #endif
  187. #ifndef WHEEL_DELTA
  188. # define WHEEL_DELTA 120 // according to MSDN.
  189. #endif
  190. //
  191. // WM_FLSELECT is the user-defined message that we get when one of
  192. // the sockets has pending data, etc.
  193. //
  194. #define WM_FLSELECT (WM_APP+1) // WM_APP is used for hide-window
  195. ////////////////////////////////////////////////////////////////
  196. // interface to poll/select call:
  197. // fd's are only implemented for sockets. Microsoft Windows does not
  198. // have a unified IO system, so it doesn't support select() on files,
  199. // devices, or pipes...
  200. //
  201. // Microsoft provides the Berkeley select() call and an asynchronous
  202. // select function that sends a WIN32 message when the select condition
  203. // exists. However, we don't try to use the asynchronous WSAAsyncSelect()
  204. // any more for good reasons (see above).
  205. //
  206. // A.S. Dec 2009: We got reports that current winsock2.h files define
  207. // POLLIN, POLLOUT, and POLLERR with conflicting values WRT what we
  208. // used before (STR #2301). Therefore we must not use these values
  209. // for our internal purposes, but use FL_READ, FL_WRITE, and
  210. // FL_EXCEPT, as defined for use in Fl::add_fd().
  211. //
  212. static int maxfd = 0;
  213. static fd_set fdsets[3];
  214. extern IDropTarget *flIDropTarget;
  215. static int nfds = 0;
  216. static int fd_array_size = 0;
  217. static struct FD {
  218. int fd;
  219. short events;
  220. void (*cb)(int, void*);
  221. void* arg;
  222. } *fd = 0;
  223. extern unsigned int fl_codepage;
  224. void fl_reset_spot()
  225. {
  226. }
  227. void fl_set_spot(int font, int size, int X, int Y, int W, int H, Fl_Window *win)
  228. {
  229. if (!win) return;
  230. Fl_Window* tw = win;
  231. while (tw->parent()) tw = tw->window(); // find top level window
  232. get_imm_module();
  233. HIMC himc = flImmGetContext(fl_xid(tw));
  234. if (himc) {
  235. COMPOSITIONFORM cfs;
  236. cfs.dwStyle = CFS_POINT;
  237. cfs.ptCurrentPos.x = X;
  238. cfs.ptCurrentPos.y = Y - tw->labelsize();
  239. MapWindowPoints(fl_xid(win), fl_xid(tw), &cfs.ptCurrentPos, 1);
  240. flImmSetCompositionWindow(himc, &cfs);
  241. flImmReleaseContext(fl_xid(tw), himc);
  242. }
  243. }
  244. void fl_set_status(int x, int y, int w, int h)
  245. {
  246. }
  247. void Fl::add_fd(int n, int events, void (*cb)(int, void*), void *v) {
  248. remove_fd(n,events);
  249. int i = nfds++;
  250. if (i >= fd_array_size) {
  251. fd_array_size = 2*fd_array_size+1;
  252. fd = (FD*)realloc(fd, fd_array_size*sizeof(FD));
  253. }
  254. fd[i].fd = n;
  255. fd[i].events = (short)events;
  256. fd[i].cb = cb;
  257. fd[i].arg = v;
  258. if (events & FL_READ) FD_SET((unsigned)n, &fdsets[0]);
  259. if (events & FL_WRITE) FD_SET((unsigned)n, &fdsets[1]);
  260. if (events & FL_EXCEPT) FD_SET((unsigned)n, &fdsets[2]);
  261. if (n > maxfd) maxfd = n;
  262. }
  263. void Fl::add_fd(int fd, void (*cb)(int, void*), void* v) {
  264. Fl::add_fd(fd, FL_READ, cb, v);
  265. }
  266. void Fl::remove_fd(int n, int events) {
  267. int i,j;
  268. for (i=j=0; i<nfds; i++) {
  269. if (fd[i].fd == n) {
  270. short e = fd[i].events & ~events;
  271. if (!e) continue; // if no events left, delete this fd
  272. fd[i].events = e;
  273. }
  274. // move it down in the array if necessary:
  275. if (j<i) {
  276. fd[j]=fd[i];
  277. }
  278. j++;
  279. }
  280. nfds = j;
  281. if (events & FL_READ) FD_CLR(unsigned(n), &fdsets[0]);
  282. if (events & FL_WRITE) FD_CLR(unsigned(n), &fdsets[1]);
  283. if (events & FL_EXCEPT) FD_CLR(unsigned(n), &fdsets[2]);
  284. }
  285. void Fl::remove_fd(int n) {
  286. remove_fd(n, -1);
  287. }
  288. // these pointers are set by the Fl::lock() function:
  289. static void nothing() {}
  290. void (*fl_lock_function)() = nothing;
  291. void (*fl_unlock_function)() = nothing;
  292. static void* thread_message_;
  293. void* Fl::thread_message() {
  294. void* r = thread_message_;
  295. thread_message_ = 0;
  296. return r;
  297. }
  298. IActiveIMMApp *fl_aimm = NULL;
  299. MSG fl_msg;
  300. // This is never called with time_to_wait < 0.0.
  301. // It *should* return negative on error, 0 if nothing happens before
  302. // timeout, and >0 if any callbacks were done. This version only
  303. // returns zero if nothing happens during a 0.0 timeout, otherwise
  304. // it returns 1.
  305. int fl_wait(double time_to_wait) {
  306. int have_message = 0;
  307. run_checks();
  308. // idle processing
  309. static char in_idle;
  310. if (Fl::idle && !in_idle) {
  311. in_idle = 1;
  312. Fl::idle();
  313. in_idle = 0;
  314. }
  315. if (nfds) {
  316. // For WIN32 we need to poll for socket input FIRST, since
  317. // the event queue is not something we can select() on...
  318. timeval t;
  319. t.tv_sec = 0;
  320. t.tv_usec = 0;
  321. fd_set fdt[3];
  322. memcpy(fdt, fdsets, sizeof fdt); // one shot faster fdt init
  323. if (get_wsock_mod()&& s_wsock_select(maxfd+1,&fdt[0],&fdt[1],&fdt[2],&t)) {
  324. // We got something - do the callback!
  325. for (int i = 0; i < nfds; i ++) {
  326. SOCKET f = fd[i].fd;
  327. short revents = 0;
  328. if (fl_wsk_fd_is_set(f, &fdt[0])) revents |= FL_READ;
  329. if (fl_wsk_fd_is_set(f, &fdt[1])) revents |= FL_WRITE;
  330. if (fl_wsk_fd_is_set(f, &fdt[2])) revents |= FL_EXCEPT;
  331. if (fd[i].events & revents) fd[i].cb(f, fd[i].arg);
  332. }
  333. time_to_wait = 0.0; // just peek for any messages
  334. } else {
  335. // we need to check them periodically, so set a short timeout:
  336. if (time_to_wait > .001) time_to_wait = .001;
  337. }
  338. }
  339. if (Fl::idle || Fl::damage())
  340. time_to_wait = 0.0;
  341. // if there are no more windows and this timer is set
  342. // to FOREVER, continue through or look up indefinitely
  343. if (!Fl::first_window() && time_to_wait==1e20)
  344. time_to_wait = 0.0;
  345. fl_unlock_function();
  346. time_to_wait = (time_to_wait > 10000 ? 10000 : time_to_wait);
  347. int t_msec = (int) (time_to_wait * 1000.0 + 0.5);
  348. MsgWaitForMultipleObjects(0, NULL, FALSE, t_msec, QS_ALLINPUT);
  349. fl_lock_function();
  350. // Execute the message we got, and all other pending messages:
  351. // have_message = PeekMessage(&fl_msg, NULL, 0, 0, PM_REMOVE);
  352. have_message = PeekMessageW(&fl_msg, NULL, 0, 0, PM_REMOVE);
  353. if (have_message > 0) {
  354. while (have_message != 0 && have_message != -1) {
  355. if (fl_msg.message == fl_wake_msg) {
  356. // Used for awaking wait() from another thread
  357. thread_message_ = (void*)fl_msg.wParam;
  358. Fl_Awake_Handler func;
  359. void *data;
  360. while (Fl::get_awake_handler_(func, data)==0) {
  361. func(data);
  362. }
  363. }
  364. TranslateMessage(&fl_msg);
  365. DispatchMessageW(&fl_msg);
  366. have_message = PeekMessageW(&fl_msg, NULL, 0, 0, PM_REMOVE);
  367. }
  368. }
  369. Fl::flush();
  370. // This should return 0 if only timer events were handled:
  371. return 1;
  372. }
  373. // fl_ready() is just like fl_wait(0.0) except no callbacks are done:
  374. int fl_ready() {
  375. if (PeekMessage(&fl_msg, NULL, 0, 0, PM_NOREMOVE)) return 1;
  376. if (!nfds) return 0;
  377. timeval t;
  378. t.tv_sec = 0;
  379. t.tv_usec = 0;
  380. fd_set fdt[3];
  381. memcpy(fdt, fdsets, sizeof fdt);
  382. return get_wsock_mod() ? s_wsock_select(0,&fdt[0],&fdt[1],&fdt[2],&t) : 0;
  383. }
  384. ////////////////////////////////////////////////////////////////
  385. int Fl::x()
  386. {
  387. RECT r;
  388. SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  389. return r.left;
  390. }
  391. int Fl::y()
  392. {
  393. RECT r;
  394. SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  395. return r.top;
  396. }
  397. int Fl::h()
  398. {
  399. RECT r;
  400. SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  401. return r.bottom - r.top;
  402. }
  403. int Fl::w()
  404. {
  405. RECT r;
  406. SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
  407. return r.right - r.left;
  408. }
  409. void Fl::get_mouse(int &x, int &y) {
  410. POINT p;
  411. GetCursorPos(&p);
  412. x = p.x;
  413. y = p.y;
  414. }
  415. ////////////////////////////////////////////////////////////////
  416. // code used for selections:
  417. char *fl_selection_buffer[2];
  418. int fl_selection_length[2];
  419. int fl_selection_buffer_length[2];
  420. char fl_i_own_selection[2];
  421. UINT fl_get_lcid_codepage(LCID id)
  422. {
  423. char buf[8];
  424. buf[GetLocaleInfo(id, LOCALE_IDEFAULTANSICODEPAGE, buf, 8)] = 0;
  425. return atol(buf);
  426. }
  427. // Convert \n -> \r\n
  428. class Lf2CrlfConvert {
  429. char *out;
  430. int outlen;
  431. public:
  432. Lf2CrlfConvert(const char *in, int inlen) {
  433. outlen = 0;
  434. const char *i;
  435. char *o;
  436. int lencount;
  437. // Predict size of \r\n conversion buffer
  438. for ( i=in, lencount = inlen; lencount--; ) {
  439. if ( *i == '\r' && *(i+1) == '\n' ) // leave \r\n untranslated
  440. { i+=2; outlen+=2; }
  441. else if ( *i == '\n' ) // \n by itself? leave room to insert \r
  442. { i++; outlen+=2; }
  443. else
  444. { ++i; ++outlen; }
  445. }
  446. // Alloc conversion buffer + NULL
  447. out = new char[outlen+1];
  448. // Handle \n -> \r\n conversion
  449. for ( i=in, o=out, lencount = inlen; lencount--; ) {
  450. if ( *i == '\r' && *(i+1) == '\n' ) // leave \r\n untranslated
  451. { *o++ = *i++; *o++ = *i++; }
  452. else if ( *i == '\n' ) // \n by itself? insert \r
  453. { *o++ = '\r'; *o++ = *i++; }
  454. else
  455. { *o++ = *i++; }
  456. }
  457. *o++ = 0;
  458. }
  459. ~Lf2CrlfConvert() {
  460. delete[] out;
  461. }
  462. int GetLength() const { return(outlen); }
  463. const char* GetValue() const { return(out); }
  464. };
  465. // call this when you create a selection:
  466. void Fl::copy(const char *stuff, int len, int clipboard) {
  467. if (!stuff || len<0) return;
  468. // Convert \n -> \r\n (for old apps like Notepad, DOS)
  469. Lf2CrlfConvert buf(stuff, len);
  470. len = buf.GetLength();
  471. stuff = buf.GetValue();
  472. if (len+1 > fl_selection_buffer_length[clipboard]) {
  473. delete[] fl_selection_buffer[clipboard];
  474. fl_selection_buffer[clipboard] = new char[len+100];
  475. fl_selection_buffer_length[clipboard] = len+100;
  476. }
  477. memcpy(fl_selection_buffer[clipboard], stuff, len);
  478. fl_selection_buffer[clipboard][len] = 0; // needed for direct paste
  479. fl_selection_length[clipboard] = len;
  480. if (clipboard) {
  481. // set up for "delayed rendering":
  482. if (OpenClipboard(NULL)) {
  483. // if the system clipboard works, use it
  484. int utf16_len = fl_utf8toUtf16(fl_selection_buffer[clipboard], fl_selection_length[clipboard], 0, 0);
  485. EmptyClipboard();
  486. HGLOBAL hMem = GlobalAlloc(GHND, utf16_len * 2 + 2); // moveable and zero'ed mem alloc.
  487. LPVOID memLock = GlobalLock(hMem);
  488. fl_utf8toUtf16(fl_selection_buffer[clipboard], fl_selection_length[clipboard], (unsigned short*) memLock, utf16_len + 1);
  489. GlobalUnlock(hMem);
  490. SetClipboardData(CF_UNICODETEXT, hMem);
  491. CloseClipboard();
  492. GlobalFree(hMem);
  493. fl_i_own_selection[clipboard] = 0;
  494. } else {
  495. // only if it fails, instruct paste() to use the internal buffers
  496. fl_i_own_selection[clipboard] = 1;
  497. }
  498. }
  499. }
  500. // Call this when a "paste" operation happens:
  501. void Fl::paste(Fl_Widget &receiver, int clipboard) {
  502. if (!clipboard || fl_i_own_selection[clipboard]) {
  503. // We already have it, do it quickly without window server.
  504. // Notice that the text is clobbered if set_selection is
  505. // called in response to FL_PASTE!
  506. // Convert \r\n -> \n
  507. char *i = fl_selection_buffer[clipboard];
  508. if (i==0L) {
  509. Fl::e_text = 0;
  510. return;
  511. }
  512. Fl::e_text = new char[fl_selection_length[clipboard]+1];
  513. char *o = Fl::e_text;
  514. while (*i) {
  515. if ( *i == '\r' && *(i+1) == '\n') i++;
  516. else *o++ = *i++;
  517. }
  518. *o = 0;
  519. Fl::e_length = o - Fl::e_text;
  520. receiver.handle(FL_PASTE);
  521. delete [] Fl::e_text;
  522. Fl::e_text = 0;
  523. } else {
  524. if (!OpenClipboard(NULL)) return;
  525. HANDLE h = GetClipboardData(CF_UNICODETEXT);
  526. if (h) {
  527. wchar_t *memLock = (wchar_t*) GlobalLock(h);
  528. int utf16_len = wcslen(memLock);
  529. Fl::e_text = (char*) malloc (utf16_len * 4 + 1);
  530. int utf8_len = fl_utf8fromwc(Fl::e_text, utf16_len * 4, memLock, utf16_len);
  531. *(Fl::e_text + utf8_len) = 0;
  532. LPSTR a,b;
  533. a = b = Fl::e_text;
  534. while (*a) { // strip the CRLF pairs ($%$#@^)
  535. if (*a == '\r' && a[1] == '\n') a++;
  536. else *b++ = *a++;
  537. }
  538. *b = 0;
  539. Fl::e_length = b - Fl::e_text;
  540. receiver.handle(FL_PASTE);
  541. GlobalUnlock(h);
  542. free(Fl::e_text);
  543. Fl::e_text = 0;
  544. }
  545. CloseClipboard();
  546. }
  547. }
  548. ////////////////////////////////////////////////////////////////
  549. char fl_is_ime = 0;
  550. void fl_get_codepage()
  551. {
  552. HKL hkl = GetKeyboardLayout(0);
  553. TCHAR ld[8];
  554. GetLocaleInfo (LOWORD(hkl), LOCALE_IDEFAULTANSICODEPAGE, ld, 6);
  555. DWORD ccp = atol(ld);
  556. fl_is_ime = 0;
  557. fl_codepage = ccp;
  558. if (fl_aimm) {
  559. fl_aimm->GetCodePageA(GetKeyboardLayout(0), &fl_codepage);
  560. } else if (get_imm_module() && flImmIsIME(hkl)) {
  561. fl_is_ime = 1;
  562. }
  563. }
  564. HWND fl_capture;
  565. static int mouse_event(Fl_Window *window, int what, int button,
  566. WPARAM wParam, LPARAM lParam)
  567. {
  568. static int px, py, pmx, pmy;
  569. POINT pt;
  570. Fl::e_x = pt.x = (signed short)LOWORD(lParam);
  571. Fl::e_y = pt.y = (signed short)HIWORD(lParam);
  572. ClientToScreen(fl_xid(window), &pt);
  573. Fl::e_x_root = pt.x;
  574. Fl::e_y_root = pt.y;
  575. #ifdef USE_CAPTURE_MOUSE_WIN
  576. Fl_Window *mouse_window = window; // save "mouse window"
  577. #endif
  578. while (window->parent()) {
  579. Fl::e_x += window->x();
  580. Fl::e_y += window->y();
  581. window = window->window();
  582. }
  583. ulong state = Fl::e_state & 0xff0000; // keep shift key states
  584. #if 0
  585. // mouse event reports some shift flags, perhaps save them?
  586. if (wParam & MK_SHIFT) state |= FL_SHIFT;
  587. if (wParam & MK_CONTROL) state |= FL_CTRL;
  588. #endif
  589. if (wParam & MK_LBUTTON) state |= FL_BUTTON1;
  590. if (wParam & MK_MBUTTON) state |= FL_BUTTON2;
  591. if (wParam & MK_RBUTTON) state |= FL_BUTTON3;
  592. Fl::e_state = state;
  593. switch (what) {
  594. case 1: // double-click
  595. if (Fl::e_is_click) {Fl::e_clicks++; goto J1;}
  596. case 0: // single-click
  597. Fl::e_clicks = 0;
  598. J1:
  599. #ifdef USE_CAPTURE_MOUSE_WIN
  600. if (!fl_capture) SetCapture(fl_xid(mouse_window)); // use mouse window
  601. #else
  602. if (!fl_capture) SetCapture(fl_xid(window)); // use main window
  603. #endif
  604. Fl::e_keysym = FL_Button + button;
  605. Fl::e_is_click = 1;
  606. px = pmx = Fl::e_x_root; py = pmy = Fl::e_y_root;
  607. return Fl::handle(FL_PUSH,window);
  608. case 2: // release:
  609. if (!fl_capture) ReleaseCapture();
  610. Fl::e_keysym = FL_Button + button;
  611. return Fl::handle(FL_RELEASE,window);
  612. case 3: // move:
  613. default: // avoid compiler warning
  614. // MSWindows produces extra events even if mouse does not move, ignore em:
  615. if (Fl::e_x_root == pmx && Fl::e_y_root == pmy) return 1;
  616. pmx = Fl::e_x_root; pmy = Fl::e_y_root;
  617. if (abs(Fl::e_x_root-px)>5 || abs(Fl::e_y_root-py)>5) Fl::e_is_click = 0;
  618. return Fl::handle(FL_MOVE,window);
  619. }
  620. }
  621. // convert a MSWindows VK_x to an Fltk (X) Keysym:
  622. // See also the inverse converter in Fl_get_key_win32.cxx
  623. // This table is in numeric order by VK:
  624. static const struct {unsigned short vk, fltk, extended;} vktab[] = {
  625. {VK_BACK, FL_BackSpace},
  626. {VK_TAB, FL_Tab},
  627. {VK_CLEAR, FL_KP+'5', 0xff0b/*XK_Clear*/},
  628. {VK_RETURN, FL_Enter, FL_KP_Enter},
  629. {VK_SHIFT, FL_Shift_L, FL_Shift_R},
  630. {VK_CONTROL, FL_Control_L, FL_Control_R},
  631. {VK_MENU, FL_Alt_L, FL_Alt_R},
  632. {VK_PAUSE, FL_Pause},
  633. {VK_CAPITAL, FL_Caps_Lock},
  634. {VK_ESCAPE, FL_Escape},
  635. {VK_SPACE, ' '},
  636. {VK_PRIOR, FL_KP+'9', FL_Page_Up},
  637. {VK_NEXT, FL_KP+'3', FL_Page_Down},
  638. {VK_END, FL_KP+'1', FL_End},
  639. {VK_HOME, FL_KP+'7', FL_Home},
  640. {VK_LEFT, FL_KP+'4', FL_Left},
  641. {VK_UP, FL_KP+'8', FL_Up},
  642. {VK_RIGHT, FL_KP+'6', FL_Right},
  643. {VK_DOWN, FL_KP+'2', FL_Down},
  644. {VK_SNAPSHOT, FL_Print}, // does not work on NT
  645. {VK_INSERT, FL_KP+'0', FL_Insert},
  646. {VK_DELETE, FL_KP+'.', FL_Delete},
  647. {VK_LWIN, FL_Meta_L},
  648. {VK_RWIN, FL_Meta_R},
  649. {VK_APPS, FL_Menu},
  650. {VK_SLEEP, FL_Sleep},
  651. {VK_MULTIPLY, FL_KP+'*'},
  652. {VK_ADD, FL_KP+'+'},
  653. {VK_SUBTRACT, FL_KP+'-'},
  654. {VK_DECIMAL, FL_KP+'.'},
  655. {VK_DIVIDE, FL_KP+'/'},
  656. {VK_NUMLOCK, FL_Num_Lock},
  657. {VK_SCROLL, FL_Scroll_Lock},
  658. # if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
  659. {VK_BROWSER_BACK, FL_Back},
  660. {VK_BROWSER_FORWARD, FL_Forward},
  661. {VK_BROWSER_REFRESH, FL_Refresh},
  662. {VK_BROWSER_STOP, FL_Stop},
  663. {VK_BROWSER_SEARCH, FL_Search},
  664. {VK_BROWSER_FAVORITES, FL_Favorites},
  665. {VK_BROWSER_HOME, FL_Home_Page},
  666. {VK_VOLUME_MUTE, FL_Volume_Mute},
  667. {VK_VOLUME_DOWN, FL_Volume_Down},
  668. {VK_VOLUME_UP, FL_Volume_Up},
  669. {VK_MEDIA_NEXT_TRACK, FL_Media_Next},
  670. {VK_MEDIA_PREV_TRACK, FL_Media_Prev},
  671. {VK_MEDIA_STOP, FL_Media_Stop},
  672. {VK_MEDIA_PLAY_PAUSE, FL_Media_Play},
  673. {VK_LAUNCH_MAIL, FL_Mail},
  674. #endif
  675. {0xba, ';'},
  676. {0xbb, '='},
  677. {0xbc, ','},
  678. {0xbd, '-'},
  679. {0xbe, '.'},
  680. {0xbf, '/'},
  681. {0xc0, '`'},
  682. {0xdb, '['},
  683. {0xdc, '\\'},
  684. {0xdd, ']'},
  685. {0xde, '\''}
  686. };
  687. static int ms2fltk(int vk, int extended) {
  688. static unsigned short vklut[256];
  689. static unsigned short extendedlut[256];
  690. if (!vklut[1]) { // init the table
  691. unsigned int i;
  692. for (i = 0; i < 256; i++) vklut[i] = tolower(i);
  693. for (i=VK_F1; i<=VK_F16; i++) vklut[i] = i+(FL_F-(VK_F1-1));
  694. for (i=VK_NUMPAD0; i<=VK_NUMPAD9; i++) vklut[i] = i+(FL_KP+'0'-VK_NUMPAD0);
  695. for (i = 0; i < sizeof(vktab)/sizeof(*vktab); i++) {
  696. vklut[vktab[i].vk] = vktab[i].fltk;
  697. extendedlut[vktab[i].vk] = vktab[i].extended;
  698. }
  699. for (i = 0; i < 256; i++) if (!extendedlut[i]) extendedlut[i] = vklut[i];
  700. }
  701. return extended ? extendedlut[vk] : vklut[vk];
  702. }
  703. #if USE_COLORMAP
  704. extern HPALETTE fl_select_palette(void); // in fl_color_win32.cxx
  705. #endif
  706. /////////////////////////////////////////////////////////////////////////////
  707. /// Win32 timers
  708. ///
  709. struct Win32Timer
  710. {
  711. UINT_PTR handle;
  712. Fl_Timeout_Handler callback;
  713. void *data;
  714. };
  715. static Win32Timer* win32_timers;
  716. static int win32_timer_alloc;
  717. static int win32_timer_used;
  718. static HWND s_TimerWnd;
  719. static void realloc_timers()
  720. {
  721. if (win32_timer_alloc == 0) {
  722. win32_timer_alloc = 8;
  723. }
  724. win32_timer_alloc *= 2;
  725. Win32Timer* new_timers = new Win32Timer[win32_timer_alloc];
  726. memset(new_timers, 0, sizeof(Win32Timer) * win32_timer_used);
  727. memcpy(new_timers, win32_timers, sizeof(Win32Timer) * win32_timer_used);
  728. Win32Timer* delete_me = win32_timers;
  729. win32_timers = new_timers;
  730. delete [] delete_me;
  731. }
  732. static void delete_timer(Win32Timer& t)
  733. {
  734. KillTimer(s_TimerWnd, t.handle);
  735. memset(&t, 0, sizeof(Win32Timer));
  736. }
  737. /// END TIMERS
  738. /////////////////////////////////////////////////////////////////////////////
  739. static Fl_Window* resize_bug_fix;
  740. extern void fl_save_pen(void);
  741. extern void fl_restore_pen(void);
  742. static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  743. {
  744. // Copy the message to fl_msg so add_handler code can see it, it is
  745. // already there if this is called by DispatchMessage, but not if
  746. // Windows calls this directly.
  747. fl_msg.hwnd = hWnd;
  748. fl_msg.message = uMsg;
  749. fl_msg.wParam = wParam;
  750. fl_msg.lParam = lParam;
  751. //fl_msg.time = ???
  752. //fl_msg.pt = ???
  753. //fl_msg.lPrivate = ???
  754. Fl_Window *window = fl_find(hWnd);
  755. if (window) switch (uMsg) {
  756. case WM_QUIT: // this should not happen?
  757. Fl::fatal("WM_QUIT message");
  758. case WM_CLOSE: // user clicked close box
  759. Fl::handle(FL_CLOSE, window);
  760. PostQuitMessage(0);
  761. return 0;
  762. case WM_SYNCPAINT :
  763. case WM_NCPAINT :
  764. case WM_ERASEBKGND :
  765. // Andreas Weitl - WM_SYNCPAINT needs to be passed to DefWindowProc
  766. // so that Windows can generate the proper paint messages...
  767. // Similarly, WM_NCPAINT and WM_ERASEBKGND need this, too...
  768. break;
  769. case WM_PAINT: {
  770. Fl_Region R;
  771. Fl_X *i = Fl_X::i(window);
  772. i->wait_for_expose = 0;
  773. char redraw_whole_window = false;
  774. if (!i->region && window->damage()) {
  775. // Redraw the whole window...
  776. i->region = CreateRectRgn(0, 0, window->w(), window->h());
  777. redraw_whole_window = true;
  778. }
  779. // We need to merge WIN32's damage into FLTK's damage.
  780. R = CreateRectRgn(0,0,0,0);
  781. int r = GetUpdateRgn(hWnd,R,0);
  782. if (r==NULLREGION && !redraw_whole_window) {
  783. break;
  784. }
  785. if (i->region) {
  786. // Also tell WIN32 that we are drawing someplace else as well...
  787. CombineRgn(i->region, i->region, R, RGN_OR);
  788. XDestroyRegion(R);
  789. } else {
  790. i->region = R;
  791. }
  792. if (window->type() == FL_DOUBLE_WINDOW) ValidateRgn(hWnd,0);
  793. else ValidateRgn(hWnd,i->region);
  794. window->clear_damage((uchar)(window->damage()|FL_DAMAGE_EXPOSE));
  795. // These next two statements should not be here, so that all update
  796. // is deferred until Fl::flush() is called during idle. However WIN32
  797. // apparently is very unhappy if we don't obey it and draw right now.
  798. // Very annoying!
  799. fl_GetDC(hWnd); // Make sure we have a DC for this window...
  800. fl_save_pen();
  801. i->flush();
  802. fl_restore_pen();
  803. window->clear_damage();
  804. } return 0;
  805. case WM_LBUTTONDOWN: mouse_event(window, 0, 1, wParam, lParam); return 0;
  806. case WM_LBUTTONDBLCLK:mouse_event(window, 1, 1, wParam, lParam); return 0;
  807. case WM_LBUTTONUP: mouse_event(window, 2, 1, wParam, lParam); return 0;
  808. case WM_MBUTTONDOWN: mouse_event(window, 0, 2, wParam, lParam); return 0;
  809. case WM_MBUTTONDBLCLK:mouse_event(window, 1, 2, wParam, lParam); return 0;
  810. case WM_MBUTTONUP: mouse_event(window, 2, 2, wParam, lParam); return 0;
  811. case WM_RBUTTONDOWN: mouse_event(window, 0, 3, wParam, lParam); return 0;
  812. case WM_RBUTTONDBLCLK:mouse_event(window, 1, 3, wParam, lParam); return 0;
  813. case WM_RBUTTONUP: mouse_event(window, 2, 3, wParam, lParam); return 0;
  814. case WM_MOUSEMOVE:
  815. #ifdef USE_TRACK_MOUSE
  816. if (track_mouse_win != window) {
  817. TRACKMOUSEEVENT tme;
  818. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  819. tme.dwFlags = TME_LEAVE;
  820. tme.hwndTrack = hWnd;
  821. _TrackMouseEvent(&tme);
  822. track_mouse_win = window;
  823. }
  824. #endif // USE_TRACK_MOUSE
  825. mouse_event(window, 3, 0, wParam, lParam);
  826. return 0;
  827. case WM_MOUSELEAVE:
  828. if (track_mouse_win == window) { // we left the top level window !
  829. Fl_Window *tw = window;
  830. while (tw->parent()) tw = tw->window(); // find top level window
  831. Fl::belowmouse(0);
  832. Fl::handle(FL_LEAVE, tw);
  833. }
  834. track_mouse_win = 0; // force TrackMouseEvent() restart
  835. break;
  836. case WM_SETFOCUS:
  837. Fl::handle(FL_FOCUS, window);
  838. break;
  839. case WM_KILLFOCUS:
  840. Fl::handle(FL_UNFOCUS, window);
  841. Fl::flush(); // it never returns to main loop when deactivated...
  842. break;
  843. case WM_SHOWWINDOW:
  844. if (!window->parent()) {
  845. Fl::handle(wParam ? FL_SHOW : FL_HIDE, window);
  846. }
  847. break;
  848. case WM_ACTIVATEAPP:
  849. // From eric@vfx.sel.sony.com, we should process WM_ACTIVATEAPP
  850. // messages to restore the correct state of the shift/ctrl/alt/lock
  851. // keys... Added control, shift, alt, and meta keys, and changed
  852. // to use GetAsyncKeyState and do it when wParam is 1
  853. // (that means we have focus...)
  854. if (wParam)
  855. {
  856. ulong state = 0;
  857. if (GetAsyncKeyState(VK_CAPITAL)) state |= FL_CAPS_LOCK;
  858. if (GetAsyncKeyState(VK_NUMLOCK)) state |= FL_NUM_LOCK;
  859. if (GetAsyncKeyState(VK_SCROLL)) state |= FL_SCROLL_LOCK;
  860. if (GetAsyncKeyState(VK_CONTROL)&~1) state |= FL_CTRL;
  861. if (GetAsyncKeyState(VK_SHIFT)&~1) state |= FL_SHIFT;
  862. if (GetAsyncKeyState(VK_MENU)) state |= FL_ALT;
  863. if ((GetAsyncKeyState(VK_LWIN)|GetAsyncKeyState(VK_RWIN))&~1) state |= FL_META;
  864. Fl::e_state = state;
  865. return 0;
  866. }
  867. break;
  868. case WM_INPUTLANGCHANGE:
  869. fl_get_codepage();
  870. break;
  871. case WM_IME_COMPOSITION:
  872. // if (!fl_is_nt4() && lParam & GCS_RESULTCLAUSE) {
  873. // HIMC himc = ImmGetContext(hWnd);
  874. // wlen = ImmGetCompositionStringW(himc, GCS_RESULTSTR,
  875. // wbuf, sizeof(wbuf)) / sizeof(short);
  876. // if (wlen < 0) wlen = 0;
  877. // wbuf[wlen] = 0;
  878. // ImmReleaseContext(hWnd, himc);
  879. // }
  880. break;
  881. case WM_KEYDOWN:
  882. case WM_SYSKEYDOWN:
  883. case WM_KEYUP:
  884. case WM_SYSKEYUP:
  885. // save the keysym until we figure out the characters:
  886. Fl::e_keysym = Fl::e_original_keysym = ms2fltk(wParam,lParam&(1<<24));
  887. // See if TranslateMessage turned it into a WM_*CHAR message:
  888. if (PeekMessageW(&fl_msg, hWnd, WM_CHAR, WM_SYSDEADCHAR, PM_REMOVE))
  889. {
  890. uMsg = fl_msg.message;
  891. wParam = fl_msg.wParam;
  892. lParam = fl_msg.lParam;
  893. }
  894. case WM_DEADCHAR:
  895. case WM_SYSDEADCHAR:
  896. case WM_CHAR:
  897. case WM_SYSCHAR: {
  898. ulong state = Fl::e_state & 0xff000000; // keep the mouse button state
  899. // if GetKeyState is expensive we might want to comment some of these out:
  900. if (GetKeyState(VK_SHIFT)&~1) state |= FL_SHIFT;
  901. if (GetKeyState(VK_CAPITAL)) state |= FL_CAPS_LOCK;
  902. if (GetKeyState(VK_CONTROL)&~1) state |= FL_CTRL;
  903. // Alt gets reported for the Alt-GR switch on foreign keyboards.
  904. // so we need to check the event as well to get it right:
  905. if ((lParam&(1<<29)) //same as GetKeyState(VK_MENU)
  906. && uMsg != WM_CHAR) state |= FL_ALT;
  907. if (GetKeyState(VK_NUMLOCK)) state |= FL_NUM_LOCK;
  908. if ((GetKeyState(VK_LWIN)|GetKeyState(VK_RWIN))&~1) {
  909. // WIN32 bug? GetKeyState returns garbage if the user hit the
  910. // meta key to pop up start menu. Sigh.
  911. if ((GetAsyncKeyState(VK_LWIN)|GetAsyncKeyState(VK_RWIN))&~1)
  912. state |= FL_META;
  913. }
  914. if (GetKeyState(VK_SCROLL)) state |= FL_SCROLL_LOCK;
  915. Fl::e_state = state;
  916. static char buffer[1024];
  917. if (uMsg == WM_CHAR || uMsg == WM_SYSCHAR) {
  918. xchar u = (xchar) wParam;
  919. // Fl::e_length = fl_unicode2utf(&u, 1, buffer);
  920. Fl::e_length = fl_utf8fromwc(buffer, 1024, &u, 1);
  921. buffer[Fl::e_length] = 0;
  922. } else if (Fl::e_keysym >= FL_KP && Fl::e_keysym <= FL_KP_Last) {
  923. if (state & FL_NUM_LOCK) {
  924. // Convert to regular keypress...
  925. buffer[0] = Fl::e_keysym-FL_KP;
  926. Fl::e_length = 1;
  927. } else {
  928. // Convert to special keypress...
  929. buffer[0] = 0;
  930. Fl::e_length = 0;
  931. switch (Fl::e_keysym) {
  932. case FL_KP + '0' :
  933. Fl::e_keysym = FL_Insert;
  934. break;
  935. case FL_KP + '1' :
  936. Fl::e_keysym = FL_End;
  937. break;
  938. case FL_KP + '2' :
  939. Fl::e_keysym = FL_Down;
  940. break;
  941. case FL_KP + '3' :
  942. Fl::e_keysym = FL_Page_Down;
  943. break;
  944. case FL_KP + '4' :
  945. Fl::e_keysym = FL_Left;
  946. break;
  947. case FL_KP + '6' :
  948. Fl::e_keysym = FL_Right;
  949. break;
  950. case FL_KP + '7' :
  951. Fl::e_keysym = FL_Home;
  952. break;
  953. case FL_KP + '8' :
  954. Fl::e_keysym = FL_Up;
  955. break;
  956. case FL_KP + '9' :
  957. Fl::e_keysym = FL_Page_Up;
  958. break;
  959. case FL_KP + '.' :
  960. Fl::e_keysym = FL_Delete;
  961. break;
  962. case FL_KP + '/' :
  963. case FL_KP + '*' :
  964. case FL_KP + '-' :
  965. case FL_KP + '+' :
  966. buffer[0] = Fl::e_keysym-FL_KP;
  967. Fl::e_length = 1;
  968. break;
  969. }
  970. }
  971. } else if ((lParam & (1<<31))==0) {
  972. #ifdef FLTK_PREVIEW_DEAD_KEYS
  973. if ((lParam & (1<<24))==0) { // clear if dead key (always?)
  974. xchar u = (xchar) wParam;
  975. Fl::e_length = fl_utf8fromwc(buffer, 1024, &u, 1);
  976. buffer[Fl::e_length] = 0;
  977. } else { // set if "extended key" (never printable?)
  978. buffer[0] = 0;
  979. Fl::e_length = 0;
  980. }
  981. #else
  982. buffer[0] = 0;
  983. Fl::e_length = 0;
  984. #endif
  985. }
  986. Fl::e_text = buffer;
  987. if (lParam & (1<<31)) { // key up events.
  988. if (Fl::handle(FL_KEYUP, window)) return 0;
  989. break;
  990. }
  991. // for (int i = lParam&0xff; i--;)
  992. while (window->parent()) window = window->window();
  993. if (Fl::handle(FL_KEYBOARD,window)) {
  994. if (uMsg==WM_DEADCHAR || uMsg==WM_SYSDEADCHAR)
  995. Fl::compose_state = 1;
  996. return 0;
  997. }
  998. break;}
  999. case WM_MOUSEWHEEL: {
  1000. static int delta = 0; // running total of all motion
  1001. delta += (SHORT)(HIWORD(wParam));
  1002. Fl::e_dy = -delta / WHEEL_DELTA;
  1003. delta += Fl::e_dy * WHEEL_DELTA;
  1004. if (Fl::e_dy) Fl::handle(FL_MOUSEWHEEL, window);
  1005. return 0;
  1006. }
  1007. case WM_GETMINMAXINFO:
  1008. Fl_X::i(window)->set_minmax((LPMINMAXINFO)lParam);
  1009. break;
  1010. case WM_SIZE:
  1011. if (!window->parent()) {
  1012. if (wParam == SIZE_MINIMIZED || wParam == SIZE_MAXHIDE) {
  1013. Fl::handle(FL_HIDE, window);
  1014. } else {
  1015. Fl::handle(FL_SHOW, window);
  1016. resize_bug_fix = window;
  1017. window->size(LOWORD(lParam), HIWORD(lParam));
  1018. }
  1019. }
  1020. break;
  1021. case WM_MOVE: {
  1022. resize_bug_fix = window;
  1023. int nx = LOWORD(lParam);
  1024. int ny = HIWORD(lParam);
  1025. if (nx & 0x8000) nx -= 65536;
  1026. if (ny & 0x8000) ny -= 65536;
  1027. window->position(nx, ny); }
  1028. break;
  1029. case WM_SETCURSOR:
  1030. if (LOWORD(lParam) == HTCLIENT) {
  1031. while (window->parent()) window = window->window();
  1032. SetCursor(Fl_X::i(window)->cursor);
  1033. return 0;
  1034. }
  1035. break;
  1036. #if USE_COLORMAP
  1037. case WM_QUERYNEWPALETTE :
  1038. fl_GetDC(hWnd);
  1039. if (fl_select_palette()) InvalidateRect(hWnd, NULL, FALSE);
  1040. break;
  1041. case WM_PALETTECHANGED:
  1042. fl_GetDC(hWnd);
  1043. if ((HWND)wParam != hWnd && fl_select_palette()) UpdateColors(fl_gc);
  1044. break;
  1045. case WM_CREATE :
  1046. fl_GetDC(hWnd);
  1047. fl_select_palette();
  1048. break;
  1049. #endif
  1050. case WM_DESTROYCLIPBOARD:
  1051. fl_i_own_selection[1] = 0;
  1052. return 1;
  1053. case WM_RENDERALLFORMATS:
  1054. fl_i_own_selection[1] = 0;
  1055. // Windoze seems unhappy unless I do these two steps. Documentation
  1056. // seems to vary on whether opening the clipboard is necessary or
  1057. // is in fact wrong:
  1058. CloseClipboard();
  1059. OpenClipboard(NULL);
  1060. // fall through...
  1061. case WM_RENDERFORMAT: {
  1062. HANDLE h;
  1063. // int l = fl_utf_nb_char((unsigned char*)fl_selection_buffer[1], fl_selection_length[1]);
  1064. int l = fl_utf8toUtf16(fl_selection_buffer[1], fl_selection_length[1], NULL, 0); // Pass NULL buffer to query length required
  1065. h = GlobalAlloc(GHND, (l+1) * sizeof(unsigned short));
  1066. if (h) {
  1067. unsigned short *g = (unsigned short*) GlobalLock(h);
  1068. // fl_utf2unicode((unsigned char *)fl_selection_buffer[1], fl_selection_length[1], (xchar*)g);
  1069. l = fl_utf8toUtf16(fl_selection_buffer[1], fl_selection_length[1], g, (l+1));
  1070. g[l] = 0;
  1071. GlobalUnlock(h);
  1072. SetClipboardData(CF_UNICODETEXT, h);
  1073. }
  1074. // Windoze also seems unhappy if I don't do this. Documentation very
  1075. // unclear on what is correct:
  1076. if (fl_msg.message == WM_RENDERALLFORMATS) CloseClipboard();
  1077. return 1;}
  1078. default:
  1079. if (Fl::handle(0,0)) return 0;
  1080. break;
  1081. }
  1082. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  1083. }
  1084. ////////////////////////////////////////////////////////////////
  1085. // This function gets the dimensions of the top/left borders and
  1086. // the title bar, if there is one, based on the FL_BORDER, FL_MODAL
  1087. // and FL_NONMODAL flags, and on the window's size range.
  1088. // It returns the following values:
  1089. //
  1090. // value | border | title bar
  1091. // 0 | none | no
  1092. // 1 | fix | yes
  1093. // 2 | size | yes
  1094. int Fl_X::fake_X_wm(const Fl_Window* w,int &X,int &Y, int &bt,int &bx, int &by) {
  1095. int W, H, xoff, yoff, dx, dy;
  1096. int ret = bx = by = bt = 0;
  1097. int fallback = 1;
  1098. if (!w->parent()) {
  1099. HWND hwnd = fl_xid(w);
  1100. if (hwnd) {
  1101. // The block below calculates the window borders by requesting the
  1102. // required decorated window rectangle for a desired client rectangle.
  1103. // If any part of the function above fails, we will drop to a
  1104. // fallback to get the best guess which is always available.
  1105. HWND hwnd = fl_xid(w);
  1106. // request the style flags of this window, as WIN32 sees them
  1107. LONG style = GetWindowLong(hwnd, GWL_STYLE);
  1108. LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  1109. RECT r;
  1110. r.left = w->x();
  1111. r.top = w->y();
  1112. r.right = w->x()+w->w();
  1113. r.bottom = w->y()+w->h();
  1114. // get the decoration rectangle for the desired client rectangle
  1115. BOOL ok = AdjustWindowRectEx(&r, style, FALSE, exstyle);
  1116. if (ok) {
  1117. X = r.left;
  1118. Y = r.top;
  1119. W = r.right - r.left;
  1120. H = r.bottom - r.top;
  1121. bx = w->x() - r.left;
  1122. by = r.bottom - w->y() - w->h(); // height of the bootm frame
  1123. bt = w->y() - r.top - by; // height of top caption bar
  1124. xoff = bx;
  1125. yoff = by + bt;
  1126. dx = W - w->w();
  1127. dy = H - w->h();
  1128. if (w->size_range_set && (w->maxw != w->minw || w->maxh != w->minh))
  1129. ret = 2;
  1130. else
  1131. ret = 1;
  1132. fallback = 0;
  1133. }
  1134. }
  1135. }
  1136. // This is the original (pre 1.1.7) routine to calculate window border sizes.
  1137. if (fallback) {
  1138. if (w->border() && !w->parent()) {
  1139. if (w->size_range_set && (w->maxw != w->minw || w->maxh != w->minh)) {
  1140. ret = 2;
  1141. bx = GetSystemMetrics(SM_CXSIZEFRAME);
  1142. by = GetSystemMetrics(SM_CYSIZEFRAME);
  1143. } else {
  1144. ret = 1;
  1145. bx = GetSystemMetrics(SM_CXFIXEDFRAME);
  1146. by = GetSystemMetrics(SM_CYFIXEDFRAME);
  1147. }
  1148. bt = GetSystemMetrics(SM_CYCAPTION);
  1149. }
  1150. //The coordinates of the whole window, including non-client area
  1151. xoff = bx;
  1152. yoff = by + bt;
  1153. dx = 2*bx;
  1154. dy = 2*by + bt;
  1155. X = w->x()-xoff;
  1156. Y = w->y()-yoff;
  1157. W = w->w()+dx;
  1158. H = w->h()+dy;
  1159. }
  1160. //Proceed to positioning the window fully inside the screen, if possible
  1161. //Make border's lower right corner visible
  1162. int scr_x, scr_y, scr_w, scr_h;
  1163. Fl::screen_xywh(scr_x, scr_y, scr_w, scr_h, X, Y);
  1164. if (scr_x+scr_w < X+W) X = scr_x+scr_w - W;
  1165. if (scr_y+scr_h < Y+H) Y = scr_y+scr_h - H;
  1166. //Make border's upper left corner visible
  1167. if (X<scr_x) X = scr_x;
  1168. if (Y<scr_y) Y = scr_y;
  1169. //Make client area's lower right corner visible
  1170. if (scr_x+scr_w < X+dx+ w->w()) X = scr_x+scr_w - w->w() - dx;
  1171. if (scr_y+scr_h < Y+dy+ w->h()) Y = scr_y+scr_h - w->h() - dy;
  1172. //Make client area's upper left corner visible
  1173. if (X+xoff < scr_x) X = scr_x-xoff;
  1174. if (Y+yoff < scr_y) Y = scr_y-yoff;
  1175. //Return the client area's top left corner in (X,Y)
  1176. X+=xoff;
  1177. Y+=yoff;
  1178. return ret;
  1179. }
  1180. ////////////////////////////////////////////////////////////////
  1181. void Fl_Window::resize(int X,int Y,int W,int H) {
  1182. UINT flags = SWP_NOSENDCHANGING | SWP_NOZORDER
  1183. | SWP_NOACTIVATE | SWP_NOOWNERZORDER;
  1184. int is_a_resize = (W != w() || H != h());
  1185. int resize_from_program = (this != resize_bug_fix);
  1186. if (!resize_from_program) resize_bug_fix = 0;
  1187. if (X != x() || Y != y()) {
  1188. force_position(1);
  1189. } else {
  1190. if (!is_a_resize) return;
  1191. flags |= SWP_NOMOVE;
  1192. }
  1193. if (is_a_resize) {
  1194. Fl_Group::resize(X,Y,W,H);
  1195. if (visible_r()) {
  1196. redraw();
  1197. // only wait for exposure if this window has a size - a window
  1198. // with no width or height will never get an exposure event
  1199. if (i && W>0 && H>0)
  1200. i->wait_for_expose = 1;
  1201. }
  1202. } else {
  1203. x(X); y(Y);
  1204. flags |= SWP_NOSIZE;
  1205. }
  1206. if (!border()) flags |= SWP_NOACTIVATE;
  1207. if (resize_from_program && shown()) {
  1208. if (!resizable()) size_range(w(),h(),w(),h());
  1209. int dummy_x, dummy_y, bt, bx, by;
  1210. //Ignore window managing when resizing, so that windows (and more
  1211. //specifically menus) can be moved offscreen.
  1212. if (Fl_X::fake_X_wm(this, dummy_x, dummy_y, bt, bx, by)) {
  1213. X -= bx;
  1214. Y -= by+bt;
  1215. W += 2*bx;
  1216. H += 2*by+bt;
  1217. }
  1218. // avoid zero size windows. A zero sized window on Win32
  1219. // will cause continouly new redraw events.
  1220. if (W<=0) W = 1;
  1221. if (H<=0) H = 1;
  1222. SetWindowPos(i->xid, 0, X, Y, W, H, flags);
  1223. }
  1224. }
  1225. ////////////////////////////////////////////////////////////////
  1226. /*
  1227. * This silly little class remembers the name of all window classes
  1228. * we register to avoid double registration. It has the added bonus
  1229. * of freeing everything on application close as well.
  1230. */
  1231. class NameList {
  1232. public:
  1233. NameList() { name = (char**)malloc(sizeof(char**)); NName = 1; nName = 0; }
  1234. ~NameList() {
  1235. int i;
  1236. for (i=0; i<nName; i++) free(name[i]);
  1237. if (name) free(name);
  1238. }
  1239. void add_name(const char *n) {
  1240. if (NName==nName) {
  1241. NName += 5;
  1242. name = (char**)realloc(name, NName * sizeof(char*));
  1243. }
  1244. name[nName++] = strdup(n);
  1245. }
  1246. char has_name(const char *n) {
  1247. int i;
  1248. for (i=0; i<nName; i++) {
  1249. if (strcmp(name[i], n)==0) return 1;
  1250. }
  1251. return 0;
  1252. }
  1253. private:
  1254. char **name;
  1255. int nName, NName;
  1256. };
  1257. void fl_fix_focus(); // in Fl.cxx
  1258. char fl_show_iconic; // hack for Fl_Window::iconic()
  1259. // int fl_background_pixel = -1; // color to use for background
  1260. HCURSOR fl_default_cursor;
  1261. UINT fl_wake_msg = 0;
  1262. int fl_disable_transient_for; // secret method of removing TRANSIENT_FOR
  1263. Fl_X* Fl_X::make(Fl_Window* w) {
  1264. Fl_Group::current(0); // get rid of very common user bug: forgot end()
  1265. // if the window is a subwindow and our parent is not mapped yet, we
  1266. // mark this window visible, so that mapping the parent at a later
  1267. // point in time will call this function again to finally map the subwindow.
  1268. if (w->parent() && !Fl_X::i(w->window())) {
  1269. w->set_visible();
  1270. return 0L;
  1271. }
  1272. static NameList class_name_list;
  1273. static const char *first_class_name = 0L;
  1274. const char *class_name = w->xclass();
  1275. if (!class_name) class_name = first_class_name; // reuse first class name used
  1276. if (!class_name) class_name = "FLTK"; // default to create a "FLTK" WNDCLASS
  1277. if (!first_class_name) {
  1278. first_class_name = class_name;
  1279. }
  1280. wchar_t class_namew[100]; // (limited) buffer for Windows class name
  1281. // convert UTF-8 class_name to wchar_t for RegisterClassExW and CreateWindowExW
  1282. fl_utf8toUtf16(class_name,strlen(class_name), // in
  1283. (unsigned short*)class_namew, // out
  1284. sizeof(class_namew)/sizeof(wchar_t)); // max. size
  1285. if (!class_name_list.has_name(class_name)) {
  1286. WNDCLASSEXW wcw;
  1287. memset(&wcw, 0, sizeof(wcw));
  1288. wcw.cbSize = sizeof(WNDCLASSEXW);
  1289. // Documentation states a device context consumes about 800 bytes
  1290. // of memory... so who cares? If 800 bytes per window is what it
  1291. // takes to speed things up, I'm game.
  1292. //wc.style = CS_HREDRAW | CS_VREDRAW | CS_CLASSDC | CS_DBLCLKS;
  1293. wcw.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  1294. wcw.lpfnWndProc = (WNDPROC)WndProc;
  1295. wcw.cbClsExtra = wcw.cbWndExtra = 0;
  1296. wcw.hInstance = fl_display;
  1297. if (!w->icon())
  1298. w->icon((void *)LoadIcon(NULL, IDI_APPLICATION));
  1299. wcw.hIcon = wcw.hIconSm = (HICON)w->icon();
  1300. wcw.hCursor = fl_default_cursor = LoadCursor(NULL, IDC_ARROW);
  1301. //uchar r,g,b; Fl::get_color(FL_GRAY,r,g,b);
  1302. //wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(r,g,b));
  1303. wcw.hbrBackground = NULL;
  1304. wcw.lpszMenuName = NULL;
  1305. wcw.lpszClassName = class_namew;
  1306. RegisterClassExW(&wcw);
  1307. class_name_list.add_name(class_name);
  1308. }
  1309. const wchar_t* message_namew = L"FLTK::ThreadWakeup";
  1310. if (!fl_wake_msg) fl_wake_msg = RegisterWindowMessageW(message_namew);
  1311. HWND parent;
  1312. DWORD style = WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  1313. DWORD styleEx = WS_EX_LEFT;
  1314. int xp = w->x();
  1315. int yp = w->y();
  1316. int wp = w->w();
  1317. int hp = w->h();
  1318. int showit = 1;
  1319. if (w->parent()) {
  1320. style |= WS_CHILD;
  1321. styleEx |= WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT;
  1322. parent = fl_xid(w->window());
  1323. } else {
  1324. if (!w->size_range_set) {
  1325. if (w->resizable()) {
  1326. Fl_Widget *o = w->resizable();
  1327. int minw = o->w(); if (minw > 100) minw = 100;
  1328. int minh = o->h(); if (minh > 100) minh = 100;
  1329. w->size_range(w->w() - o->w() + minw, w->h() - o->h() + minh, 0, 0);
  1330. } else {
  1331. w->size_range(w->w(), w->h(), w->w(), w->h());
  1332. }
  1333. }
  1334. styleEx |= WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT;
  1335. int xwm = xp , ywm = yp , bt, bx, by;
  1336. switch (fake_X_wm(w, xwm, ywm, bt, bx, by)) {
  1337. // No border (used for menus)
  1338. case 0: style |= WS_POPUP;
  1339. styleEx |= WS_EX_TOOLWINDOW;
  1340. break;
  1341. // Thin border and title bar
  1342. case 1: style |= WS_DLGFRAME | WS_CAPTION; break;
  1343. // Thick, resizable border and title bar, with maximize button
  1344. case 2: style |= WS_THICKFRAME | WS_MAXIMIZEBOX | WS_CAPTION ; break;
  1345. }
  1346. if (by+bt) {
  1347. if (!w->modal()) style |= WS_SYSMENU | WS_MINIMIZEBOX;
  1348. wp += 2*bx;
  1349. hp += 2*by+bt;
  1350. }
  1351. if (!w->force_position()) {
  1352. xp = yp = CW_USEDEFAULT;
  1353. } else {
  1354. if (!Fl::grab()) {
  1355. xp = xwm; yp = ywm;
  1356. w->x(xp);w->y(yp);
  1357. }
  1358. xp -= bx;
  1359. yp -= by+bt;
  1360. }
  1361. parent = 0;
  1362. if (w->non_modal() && Fl_X::first && !fl_disable_transient_for) {
  1363. // find some other window to be "transient for":
  1364. Fl_Window* w = Fl_X::first->w;
  1365. while (w->parent()) w = w->window();
  1366. parent = fl_xid(w);
  1367. if (!w->visible()) showit = 0;
  1368. } else if (Fl::grab()) parent = fl_xid(Fl::grab());
  1369. }
  1370. Fl_X* x = new Fl_X;
  1371. x->other_xid = 0;
  1372. x->setwindow(w);
  1373. x->region = 0;
  1374. x->private_dc = 0;
  1375. x->cursor = fl_default_cursor;
  1376. if (!fl_codepage) fl_get_codepage();
  1377. WCHAR *lab = NULL;
  1378. if (w->label()) {
  1379. int l = strlen(w->label());
  1380. // lab = (WCHAR*) malloc((l + 1) * sizeof(short));
  1381. // l = fl_utf2unicode((unsigned char*)w->label(), l, (xchar*)lab);
  1382. // lab[l] = 0;
  1383. unsigned wlen = fl_utf8toUtf16(w->label(), l, NULL, 0); // Pass NULL to query length
  1384. wlen++;
  1385. lab = (WCHAR *) malloc(sizeof(WCHAR)*wlen);
  1386. wlen = fl_utf8toUtf16(w->label(), l, (unsigned short*)lab, wlen);
  1387. lab[wlen] = 0;
  1388. }
  1389. x->xid = CreateWindowExW(
  1390. styleEx,
  1391. class_namew, lab, style,
  1392. xp, yp, wp, hp,
  1393. parent,
  1394. NULL, // menu
  1395. fl_display,
  1396. NULL // creation parameters
  1397. );
  1398. if (lab) free(lab);
  1399. x->next = Fl_X::first;
  1400. Fl_X::first = x;
  1401. x->wait_for_expose = 1;
  1402. if (fl_show_iconic) {showit = 0; fl_show_iconic = 0;}
  1403. if (showit) {
  1404. w->set_visible();
  1405. int old_event = Fl::e_number;
  1406. w->handle(Fl::e_number = FL_SHOW); // get child windows to appear
  1407. Fl::e_number = old_event;
  1408. w->redraw(); // force draw to happen
  1409. }
  1410. // If we've captured the mouse, we dont want to activate any
  1411. // other windows from the code, or we lose the capture.
  1412. ShowWindow(x->xid, !showit ? SW_SHOWMINNOACTIVE :
  1413. (Fl::grab() || (style & WS_POPUP)) ? SW_SHOWNOACTIVATE : SW_SHOWNORMAL);
  1414. // Register all windows for potential drag'n'drop operations
  1415. fl_OleInitialize();
  1416. RegisterDragDrop(x->xid, flIDropTarget);
  1417. if (!fl_aimm) {
  1418. CoCreateInstance(CLSID_CActiveIMM, NULL, CLSCTX_INPROC_SERVER,
  1419. IID_IActiveIMMApp, (void**) &fl_aimm);
  1420. if (fl_aimm) {
  1421. fl_aimm->Activate(TRUE);
  1422. }
  1423. }
  1424. if (w->modal()) {Fl::modal_ = w; fl_fix_focus();}
  1425. return x;
  1426. }
  1427. /////////////////////////////////////////////////////////////////////////////
  1428. /// Win32 timers
  1429. ///
  1430. static LRESULT CALLBACK s_TimerProc(HWND hwnd, UINT msg,
  1431. WPARAM wParam, LPARAM lParam)
  1432. {
  1433. switch (msg) {
  1434. case WM_TIMER:
  1435. {
  1436. unsigned int id = wParam - 1;
  1437. if (id < (unsigned int)win32_timer_used && win32_timers[id].handle) {
  1438. Fl_Timeout_Handler cb = win32_timers[id].callback;
  1439. void* data = win32_timers[id].data;
  1440. delete_timer(win32_timers[id]);
  1441. if (cb) {
  1442. (*cb)(data);
  1443. }
  1444. }
  1445. }
  1446. return 0;
  1447. default:
  1448. break;
  1449. }
  1450. return DefWindowProc(hwnd, msg, wParam, lParam);
  1451. }
  1452. void Fl::add_timeout(double time, Fl_Timeout_Handler cb, void* data)
  1453. {
  1454. repeat_timeout(time, cb, data);
  1455. }
  1456. void Fl::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data)
  1457. {
  1458. int timer_id = -1;
  1459. for (int i = 0; i < win32_timer_used; ++i) {
  1460. if ( !win32_timers[i].handle ) {
  1461. timer_id = i;
  1462. break;
  1463. }
  1464. }
  1465. if (timer_id == -1) {
  1466. if (win32_timer_used == win32_timer_alloc) {
  1467. realloc_timers();
  1468. }
  1469. timer_id = win32_timer_used++;
  1470. }
  1471. unsigned int elapsed = (unsigned int)(time * 1000);
  1472. if ( !s_TimerWnd ) {
  1473. const char* timer_class = "FLTimer";
  1474. WNDCLASSEX wc;
  1475. memset(&wc, 0, sizeof(wc));
  1476. wc.cbSize = sizeof (wc);
  1477. wc.style = CS_CLASSDC;
  1478. wc.lpfnWndProc = (WNDPROC)s_TimerProc;
  1479. wc.hInstance = fl_display;
  1480. wc.lpszClassName = timer_class;
  1481. /*ATOM atom =*/ RegisterClassEx(&wc);
  1482. // create a zero size window to handle timer events
  1483. s_TimerWnd = CreateWindowEx(WS_EX_LEFT | WS_EX_TOOLWINDOW,
  1484. timer_class, "",
  1485. WS_POPUP,
  1486. 0, 0, 0, 0,
  1487. NULL, NULL, fl_display, NULL);
  1488. // just in case this OS won't let us create a 0x0 size window:
  1489. if (!s_TimerWnd)
  1490. s_TimerWnd = CreateWindowEx(WS_EX_LEFT | WS_EX_TOOLWINDOW,
  1491. timer_class, "",
  1492. WS_POPUP,
  1493. 0, 0, 1, 1,
  1494. NULL, NULL, fl_display, NULL);
  1495. ShowWindow(s_TimerWnd, SW_SHOWNOACTIVATE);
  1496. }
  1497. win32_timers[timer_id].callback = cb;
  1498. win32_timers[timer_id].data = data;
  1499. win32_timers[timer_id].handle =
  1500. SetTimer(s_TimerWnd, timer_id + 1, elapsed, NULL);
  1501. }
  1502. int Fl::has_timeout(Fl_Timeout_Handler cb, void* data)
  1503. {
  1504. for (int i = 0; i < win32_timer_used; ++i) {
  1505. Win32Timer& t = win32_timers[i];
  1506. if (t.handle && t.callback == cb && t.data == data) {
  1507. return 1;
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. void Fl::remove_timeout(Fl_Timeout_Handler cb, void* data)
  1513. {
  1514. int i;
  1515. for (i = 0; i < win32_timer_used; ++i) {
  1516. Win32Timer& t = win32_timers[i];
  1517. if (t.handle && t.callback == cb &&
  1518. (t.data == data || data == NULL)) {
  1519. delete_timer(t);
  1520. }
  1521. }
  1522. }
  1523. /// END TIMERS
  1524. /////////////////////////////////////////////////////////////////////////////
  1525. ////////////////////////////////////////////////////////////////
  1526. HINSTANCE fl_display = GetModuleHandle(NULL);
  1527. void Fl_Window::size_range_() {
  1528. size_range_set = 1;
  1529. }
  1530. void Fl_X::set_minmax(LPMINMAXINFO minmax)
  1531. {
  1532. int td, wd, hd, dummy_x, dummy_y;
  1533. fake_X_wm(w, dummy_x, dummy_y, td, wd, hd);
  1534. wd *= 2;
  1535. hd *= 2;
  1536. hd += td;
  1537. minmax->ptMinTrackSize.x = w->minw + wd;
  1538. minmax->ptMinTrackSize.y = w->minh + hd;
  1539. if (w->maxw) {
  1540. minmax->ptMaxTrackSize.x = w->maxw + wd;
  1541. minmax->ptMaxSize.x = w->maxw + wd;
  1542. }
  1543. if (w->maxh) {
  1544. minmax->ptMaxTrackSize.y = w->maxh + hd;
  1545. minmax->ptMaxSize.y = w->maxh + hd;
  1546. }
  1547. }
  1548. ////////////////////////////////////////////////////////////////
  1549. #include <FL/filename.H> // need so FL_EXPORT fl_filename_name works
  1550. // returns pointer to the filename, or null if name ends with '/'
  1551. const char *fl_filename_name(const char *name) {
  1552. const char *p,*q;
  1553. if (!name) return (0);
  1554. q = name;
  1555. if (q[0] && q[1]==':') q += 2; // skip leading drive letter
  1556. for (p = q; *p; p++) if (*p == '/' || *p == '\\') q = p+1;
  1557. return q;
  1558. }
  1559. void Fl_Window::label(const char *name,const char *iname) {
  1560. Fl_Widget::label(name);
  1561. iconlabel_ = iname;
  1562. if (shown() && !parent()) {
  1563. if (!name) name = "";
  1564. int l = strlen(name);
  1565. // WCHAR *lab = (WCHAR*) malloc((l + 1) * sizeof(short));
  1566. // l = fl_utf2unicode((unsigned char*)name, l, (xchar*)lab);
  1567. unsigned wlen = fl_utf8toUtf16(name, l, NULL, 0); // Pass NULL to query length
  1568. wlen++;
  1569. unsigned short * lab = (unsigned short*)malloc(sizeof(unsigned short)*wlen);
  1570. wlen = fl_utf8toUtf16(name, l, lab, wlen);
  1571. lab[wlen] = 0;
  1572. SetWindowTextW(i->xid, (WCHAR *)lab);
  1573. free(lab);
  1574. }
  1575. }
  1576. ////////////////////////////////////////////////////////////////
  1577. // Implement the virtual functions for the base Fl_Window class:
  1578. // If the box is a filled rectangle, we can make the redisplay *look*
  1579. // faster by using X's background pixel erasing. We can make it
  1580. // actually *be* faster by drawing the frame only, this is done by
  1581. // setting fl_boxcheat, which is seen by code in fl_drawbox.cxx:
  1582. // For WIN32 it looks like all windows share a background color, so
  1583. // I use FL_GRAY for this and only do this cheat for windows that are
  1584. // that color.
  1585. // Actually it is totally disabled.
  1586. // Fl_Widget *fl_boxcheat;
  1587. //static inline int can_boxcheat(uchar b) {return (b==1 || (b&2) && b<=15);}
  1588. void Fl_Window::show() {
  1589. image(Fl::scheme_bg_);
  1590. if (Fl::scheme_bg_) {
  1591. labeltype(FL_NORMAL_LABEL);
  1592. align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP);
  1593. } else {
  1594. labeltype(FL_NO_LABEL);
  1595. }
  1596. Fl_Tooltip::exit(this);
  1597. if (!shown()) {
  1598. // if (can_boxcheat(box())) fl_background_pixel = fl_xpixel(color());
  1599. Fl_X::make(this);
  1600. } else {
  1601. // Once again, we would lose the capture if we activated the window.
  1602. if (IsIconic(i->xid)) OpenIcon(i->xid);
  1603. if (!fl_capture) BringWindowToTop(i->xid);
  1604. //ShowWindow(i->xid,fl_capture?SW_SHOWNOACTIVATE:SW_RESTORE);
  1605. }
  1606. #ifdef USE_PRINT_BUTTON
  1607. void preparePrintFront(void);
  1608. preparePrintFront();
  1609. #endif
  1610. }
  1611. Fl_Window *Fl_Window::current_;
  1612. // the current context
  1613. HDC fl_gc = 0;
  1614. // the current window handle, initially set to -1 so we can correctly
  1615. // allocate fl_GetDC(0)
  1616. HWND fl_window = NULL;
  1617. // Here we ensure only one GetDC is ever in place.
  1618. HDC fl_GetDC(HWND w) {
  1619. if (fl_gc) {
  1620. if (w == fl_window && fl_window != NULL) return fl_gc;
  1621. if (fl_window) fl_release_dc(fl_window, fl_gc); // ReleaseDC
  1622. }
  1623. fl_gc = GetDC(w);
  1624. fl_save_dc(w, fl_gc);
  1625. fl_window = w;
  1626. // calling GetDC seems to always reset these: (?)
  1627. SetTextAlign(fl_gc, TA_BASELINE|TA_LEFT);
  1628. SetBkMode(fl_gc, TRANSPARENT);
  1629. return fl_gc;
  1630. }
  1631. // make X drawing go into this window (called by subclass flush() impl.)
  1632. void Fl_Window::make_current() {
  1633. fl_GetDC(fl_xid(this));
  1634. #if USE_COLORMAP
  1635. // Windows maintains a hardware and software color palette; the
  1636. // SelectPalette() call updates the current soft->hard mapping
  1637. // for all drawing calls, so we must select it here before any
  1638. // code does any drawing...
  1639. fl_select_palette();
  1640. #endif // USE_COLORMAP
  1641. current_ = this;
  1642. fl_clip_region(0);
  1643. }
  1644. /* Make sure that all allocated fonts are released. This works only if
  1645. Fl::run() is allowed to exit by closing all windows. Calling 'exit(int)'
  1646. will not automatically free any fonts. */
  1647. void fl_free_fonts(void)
  1648. {
  1649. // remove the Fl_Font_Descriptor chains
  1650. int i;
  1651. Fl_Fontdesc * s;
  1652. Fl_Font_Descriptor * f;
  1653. Fl_Font_Descriptor * ff;
  1654. for (i=0; i<FL_FREE_FONT; i++) {
  1655. s = fl_fonts + i;
  1656. for (f=s->first; f; f=ff) {
  1657. ff = f->next;
  1658. delete f;
  1659. s->first = ff;
  1660. }
  1661. }
  1662. }
  1663. ///////////////////////////////////////////////////////////////////////
  1664. //
  1665. // The following routines help fix a problem with the leaking of Windows
  1666. // Device Context (DC) objects. The 'proper' protocol is for a program to
  1667. // acquire a DC, save its state, do the modifications needed for drawing,
  1668. // perform the drawing, restore the initial state, and release the DC. In
  1669. // FLTK, the save and restore steps have previously been omitted and DCs are
  1670. // not properly released, leading to a great number of DC leaks. As some
  1671. // Windows "OSs" will hang when any process exceeds roughly 10,000 GDI objects,
  1672. // it is important to control GDI leaks, which are much more important than memory
  1673. // leaks. The following struct, global variable, and routines help implement
  1674. // the above protocol for those cases where the GetDC and RestoreDC are not in
  1675. // the same routine. For each GetDC, fl_save_dc is used to create an entry in
  1676. // a linked list that saves the window handle, the DC handle, and the initial
  1677. // state. When the DC is to be released, 'fl_release_dc' is called. It restores
  1678. // the initial state and releases the DC. When the program exits, 'fl_cleanup_dc_list'
  1679. // frees any remaining nodes in the list.
  1680. struct Win_DC_List { // linked list
  1681. HWND window; // window handle
  1682. HDC dc; // device context handle
  1683. int saved_dc; // initial state of DC
  1684. Win_DC_List * next; // pointer to next item
  1685. };
  1686. static Win_DC_List * win_DC_list = 0;
  1687. void fl_save_dc( HWND w, HDC dc) {
  1688. Win_DC_List * t;
  1689. t = new Win_DC_List;
  1690. t->window = w;
  1691. t->dc = dc;
  1692. t->saved_dc = SaveDC(dc);
  1693. if (win_DC_list)
  1694. t->next = win_DC_list;
  1695. else
  1696. t->next = NULL;
  1697. win_DC_list = t;
  1698. }
  1699. void fl_release_dc(HWND w, HDC dc) {
  1700. Win_DC_List * t= win_DC_list;
  1701. Win_DC_List * prev = 0;
  1702. if (!t)
  1703. return;
  1704. do {
  1705. if (t->dc == dc) {
  1706. RestoreDC(dc, t->saved_dc);
  1707. ReleaseDC(w, dc);
  1708. if (!prev) {
  1709. win_DC_list = t->next; // delete first item
  1710. } else {
  1711. prev->next = t->next; // one in the middle
  1712. }
  1713. delete (t);
  1714. return;
  1715. }
  1716. prev = t;
  1717. t = t->next;
  1718. } while (t);
  1719. }
  1720. void fl_cleanup_dc_list(void) { // clean up the list
  1721. Win_DC_List * t = win_DC_list;
  1722. if (!t)return;
  1723. do {
  1724. RestoreDC(t->dc, t->saved_dc);
  1725. ReleaseDC(t->window, t->dc);
  1726. win_DC_list = t->next;
  1727. delete (t);
  1728. t = win_DC_list;
  1729. } while(t);
  1730. }
  1731. Fl_Region XRectangleRegion(int x, int y, int w, int h) {
  1732. if (Fl_Surface_Device::surface()->class_name() == Fl_Display_Device::class_id) return CreateRectRgn(x,y,x+w,y+h);
  1733. // because rotation may apply, the rectangle becomes a polygon in device coords
  1734. POINT pt[4] = { {x, y}, {x + w, y}, {x + w, y + h}, {x, y + h} };
  1735. LPtoDP(fl_gc, pt, 4);
  1736. return CreatePolygonRgn(pt, 4, ALTERNATE);
  1737. }
  1738. Window fl_xid_(const Fl_Window *w) {
  1739. Fl_X *temp = Fl_X::i(w);
  1740. return temp ? temp->xid : 0;
  1741. }
  1742. int Fl_Window::decorated_w()
  1743. {
  1744. if (!shown() || parent() || !border() || !visible()) return w();
  1745. int X, Y, bt, bx, by;
  1746. Fl_X::fake_X_wm(this, X, Y, bt, bx, by);
  1747. return w() + 2 * bx;
  1748. }
  1749. int Fl_Window::decorated_h()
  1750. {
  1751. if (!shown() || parent() || !border() || !visible()) return h();
  1752. int X, Y, bt, bx, by;
  1753. Fl_X::fake_X_wm(this, X, Y, bt, bx, by);
  1754. return h() + bt + 2 * by;
  1755. }
  1756. void Fl_Paged_Device::print_window(Fl_Window *win, int x_offset, int y_offset)
  1757. {
  1758. if (!win->shown() || win->parent() || !win->border() || !win->visible()) {
  1759. this->print_widget(win, x_offset, y_offset);
  1760. return;
  1761. }
  1762. int X, Y, bt, bx, by, ww, wh; // compute the window border sizes
  1763. Fl_X::fake_X_wm(win, X, Y, bt, bx, by);
  1764. ww = win->w() + 2 * bx;
  1765. wh = win->h() + bt + 2 * by;
  1766. Fl_Display_Device::display_device()->set_current(); // make window current
  1767. win->show();
  1768. Fl::check();
  1769. win->make_current();
  1770. HDC save_gc = fl_gc;
  1771. fl_gc = GetDC(NULL); // get the screen device context
  1772. // capture the 4 window sides from screen
  1773. RECT r; GetWindowRect(fl_window, &r);
  1774. uchar *top_image = fl_read_image(NULL, r.left, r.top, ww, bt + by);
  1775. uchar *left_image = fl_read_image(NULL, r.left, r.top, bx, wh);
  1776. uchar *right_image = fl_read_image(NULL, r.right - bx, r.top, bx, wh);
  1777. uchar *bottom_image = fl_read_image(NULL, r.left, r.bottom-by, ww, by);
  1778. ReleaseDC(NULL, fl_gc); fl_gc = save_gc;
  1779. this->set_current();
  1780. // print the 4 window sides
  1781. fl_draw_image(top_image, x_offset, y_offset, ww, bt + by, 3);
  1782. fl_draw_image(left_image, x_offset, y_offset, bx, wh, 3);
  1783. fl_draw_image(right_image, x_offset + win->w() + bx, y_offset, bx, wh, 3);
  1784. fl_draw_image(bottom_image, x_offset, y_offset + win->h() + bt + by, ww, by, 3);
  1785. delete[] top_image;
  1786. delete[] left_image;
  1787. delete[] right_image;
  1788. delete[] bottom_image;
  1789. // print the window inner part
  1790. this->print_widget(win, x_offset + bx, y_offset + bt + by);
  1791. fl_gc = GetDC(fl_xid(win));
  1792. ReleaseDC(fl_xid(win), fl_gc);
  1793. }
  1794. #ifdef USE_PRINT_BUTTON
  1795. // to test the Fl_Printer class creating a "Print front window" button in a separate window
  1796. // contains also preparePrintFront call above
  1797. #include <FL/Fl_Printer.H>
  1798. #include <FL/Fl_Button.H>
  1799. void printFront(Fl_Widget *o, void *data)
  1800. {
  1801. Fl_Printer printer;
  1802. o->window()->hide();
  1803. Fl_Window *win = Fl::first_window();
  1804. if(!win) return;
  1805. int w, h;
  1806. if( printer.start_job(1) ) { o->window()->show(); return; }
  1807. if( printer.start_page() ) { o->window()->show(); return; }
  1808. printer.printable_rect(&w,&h);
  1809. int wh, ww;
  1810. wh = win->decorated_h();
  1811. ww = win->decorated_w();
  1812. // scale the printer device so that the window fits on the page
  1813. float scale = 1;
  1814. if (ww > w || wh > h) {
  1815. scale = (float)w/ww;
  1816. if ((float)h/wh < scale) scale = (float)h/wh;
  1817. printer.scale(scale, scale);
  1818. }
  1819. // #define ROTATE 20.0
  1820. #ifdef ROTATE
  1821. printer.scale(scale * 0.8, scale * 0.8);
  1822. printer.printable_rect(&w, &h);
  1823. printer.origin(w/2, h/2 );
  1824. printer.rotate(ROTATE);
  1825. printer.print_widget( win, - win->w()/2, - win->h()/2 );
  1826. //printer.print_window_part( win, 0,0, win->w(), win->h(), - win->w()/2, - win->h()/2 );
  1827. #else
  1828. printer.print_window(win);
  1829. #endif
  1830. printer.end_page();
  1831. printer.end_job();
  1832. o->window()->show();
  1833. }
  1834. void preparePrintFront(void)
  1835. {
  1836. static BOOL first=TRUE;
  1837. if(!first) return;
  1838. first=FALSE;
  1839. static Fl_Window w(0,0,120,30);
  1840. static Fl_Button b(0,0,w.w(),w.h(), "Print front window");
  1841. b.callback(printFront);
  1842. w.end();
  1843. w.show();
  1844. }
  1845. #endif // USE_PRINT_BUTTON
  1846. #endif // FL_DOXYGEN
  1847. //
  1848. // End of "$Id: Fl_win32.cxx 8759 2011-05-30 12:33:51Z manolo $".
  1849. //