Debian Codegraph @

src/af/xap/win/xap_Win32FrameImpl.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 1998-2000 AbiSource, Inc.
5 * Copyright (C) 2002
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "xap_Win32FrameImpl.h"
28 #include "commctrl.h"
29 #include <limits.h> /* for INT_MAX */
30
31 #include "zmouse.h"
32 #ifdef __MINGW32__
33 #include "winezmouse.h"
34 #endif
35
36 #include "ut_debugmsg.h"
37 #include "ut_path.h"
38 #include "ut_Win32OS.h"
39 #include "ut_Win32LocaleString.h"
40 #include "xap_Win32DialogBase.h"
41 #include "xap_ViewListener.h"
42 #include "ev_EditMethod.h"
43 #include "xav_View.h"
44 #include "xap_Prefs.h"
45 #include "ie_impGraphic.h"
46 #include "fv_View.h"
47 #include "ap_Win32App.h"
48 #include "fg_Graphic.h"
49 #include "gr_Win32Graphics.h"
50
51
52 #ifdef _MSC_VER
53 #pragma warning(disable: 4355) // 'this' used in base member initializer list
54 #endif
55
56 #ifdef STRICT
57 #define WHICHPROC WNDPROC
58 #else
59 #define WHICHPROC FARPROC
60 #endif
61
62 /* Defines not present in old header files*/
63
64 #ifndef TBN_FIRST
65 #define TBN_FIRST (0U-700U) // toolbar
66 #endif
67
68 #ifndef TBN_DROPDOWN
69 #define TBN_DROPDOWN (TBN_FIRST - 10)
70 #endif
71
72 #ifndef TBDDRET_DEFAULT
73 #define TBDDRET_DEFAULT 0
74 #endif
75
76 WHICHPROC s_oldRedBar;
77
78 // Where the heck is this function????
79 // TODO Fix the following header file. It seems to be incomplete
80 // TODO #include <ap_EditMethods.h>
81 // TODO In the mean time, define the needed function by hand
82 extern XAP_Dialog_MessageBox::tAnswer s_CouldNotLoadFileMessage(XAP_Frame * pFrame, const char * pNewFile, UT_Error errorCode);
83
84 /*
85 Owner drawn for child font combo box
86 */
87 LRESULT CALLBACK s_rebarWndProc( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
88 {
89 switch (uMessage)
90 {
91 case WM_DRAWITEM:
92 {
93 DRAWITEMSTRUCT* pDrawItem = (DRAWITEMSTRUCT*)lParam;
94 SendMessageW(pDrawItem->hwndItem, WM_DRAWITEM, wParam, lParam);
95 return TRUE;
96 }
97
98 case WM_MEASUREITEM:
99 {
100 MEASUREITEMSTRUCT* mesure = (MEASUREITEMSTRUCT*) lParam;
101
102 if (mesure->CtlType==ODT_COMBOBOX)
103 {
104 mesure->itemHeight = 16;
105 return TRUE;
106 }
107 break;
108 }
109
110 default:
111 break;
112 }
113
114 return CallWindowProcW(s_oldRedBar, hWnd, uMessage, wParam, lParam);
115 }
116
117
118
119 XAP_Win32FrameImpl::XAP_Win32FrameImpl(XAP_Frame *pFrame) :
120 XAP_FrameImpl(pFrame),
121 m_hwndFrame(NULL),
122 m_hwndRebar(NULL),
123 m_hwndContainer(NULL),
124 m_hwndStatusBar(NULL),
125 m_dialogFactory(XAP_App::getApp(), pFrame), /* note: pFrame->getApp() not initialized properly yet! */
126 m_pWin32Menu(NULL),
127 m_pWin32Popup(NULL),
128 m_iBarHeight(0),
129 m_iStatusBarHeight(0),
130 m_iRealSizeHeight(0),
131 m_iRealSizeWidth(0),
132 m_iWindowStateBeforeFS(SW_SHOWNORMAL),
133 m_iWindowXBeforeFS(0),
134 m_iWindowYBeforeFS(0),
135 m_iWindowHeightBeforeFS(0),
136 m_iWindowWidthBeforeFS(0),
137 m_mouseWheelMessage(0),
138 m_iSizeWidth(0),
139 m_iSizeHeight(0)
140 {
141 }
142
143 XAP_Win32FrameImpl::~XAP_Win32FrameImpl(void)
144 {
145 // only delete the things we created...
146
147 DELETEP(m_pWin32Menu);
148 DELETEP(m_pWin32Popup);
149
150 // have to reset the window long, so our message functions do not
151 // try to derefernce it
152 SetWindowLongPtrW(m_hwndFrame, GWLP_USERDATA,0);
153 }
154
155
156 #if 0
157 void XAP_Win32FrameImpl::_startViewAutoUpdater(void) {}
158 static void XAP_Win32FrameImpl::viewAutoUpdater(UT_Worker *wkr) {}
159 #endif
160
161
162 bool XAP_Win32FrameImpl::_updateTitle(void)
163 {
164 UT_return_val_if_fail(m_hwndFrame, false);
165 XAP_Frame *pFrame = getFrame();
166 UT_return_val_if_fail(pFrame, false);
167
168
169 if (!XAP_FrameImpl::_updateTitle())
170 {
171 // no relevant change, so skip it
172 return false;
173 }
174
175 UT_UTF8String sTmp = pFrame->getTitle();
176 sTmp += " - ";
177 sTmp += XAP_App::getApp()->getApplicationTitleForTitleBar();
178 XAP_Win32DialogBase::setWindowText (m_hwndFrame, sTmp.utf8_str());
179
180 return true;
181 }
182
183 void XAP_Win32FrameImpl::_initialize(void)
184 {
185 // we assume AP_{FE}Frame has already called XAP_Frame::initialize(...);
186
187 // get a handle to our keyboard binding mechanism
188 // and to our mouse binding mechanism.
189
190 EV_EditEventMapper * pEEM = XAP_App::getApp()->getEditEventMapper();
191 UT_return_if_fail(pEEM);
192
193 m_pKeyboard = new ev_Win32Keyboard(pEEM);
194 UT_return_if_fail(m_pKeyboard);
195
196 m_pMouse = new EV_Win32Mouse(pEEM);
197 UT_return_if_fail(m_pMouse);
198 }
199
200 void XAP_Win32FrameImpl::_createTopLevelWindow(void)
201 {
202 RECT r;
203 UT_uint32 iHeight, iWidth;
204 UT_sint32 iPosX, iPosY;
205 static bool firstWindow = true; /* position only 1st window! */
206
207 // create a top-level window for us.
208 // get the default window size from preferences or something.
209 // should set size for all, but position only on 1st created
210 // TODO determine where to save & restore from Window flag (since
211 // we can't use the geometry flag (its some other junk about validity of pos & size)
212 // so we can properly restore Maximized/Minimized/Normal mode windows
213
214 // get window width & height from preferences
215 UT_uint32 t_flag; // dummy variable
216 if ( !(XAP_App::getApp()->getGeometry(&iPosX,&iPosY,&iWidth,&iHeight,&t_flag)) ||
217 !((iWidth > 0) && (iHeight > 0)) )
218 {
219 UT_DEBUGMSG(("Unable to obtain saved geometry, using window defaults!\n"));
220 iWidth = CW_USEDEFAULT;
221 iHeight = CW_USEDEFAULT;
222 iPosX = CW_USEDEFAULT;
223 iPosY = CW_USEDEFAULT;
224 } else {
225 // Ensure the window fits current desktop area
226 RECT rcDesktop, rcWindow;
227 #if (_WIN32_WINNT >= 0x0500)
228 rcDesktop.left=rcDesktop.right=0;
229
230 if (GetSystemMetrics(SM_CMONITORS)>1) {
231 HMONITOR m;
232 MONITORINFO mif;
233 SetRect(&rcWindow,iPosX,iPosY,iPosX+iWidth,iPosY+iHeight);
234 if ((m = MonitorFromRect(&rcWindow, MONITOR_DEFAULTTONEAREST))) {;
235 mif.cbSize=sizeof(MONITORINFO);
236 if (GetMonitorInfoW(m,&mif)) {
237 rcDesktop=mif.rcWork;
238 }
239 }
240 }
241
242 if (rcDesktop.left==rcDesktop.right)
243 #endif
244 if (!SystemParametersInfoW(SPI_GETWORKAREA,0,&rcDesktop,0))
245 SetRect(&rcDesktop,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
246
247 if (iWidth > (rcDesktop.right-rcDesktop.left)) iWidth=rcDesktop.right-rcDesktop.left;
248 if (iHeight > (rcDesktop.bottom-rcDesktop.top)) iHeight=rcDesktop.bottom-rcDesktop.top;
249
250 if (iPosX+iWidth > rcDesktop.right) iPosX=rcDesktop.right-iWidth;
251 if (iPosY+iHeight > rcDesktop.bottom) iPosY=rcDesktop.bottom-iHeight;
252
253 if (iPosX < rcDesktop.left) iPosX=rcDesktop.left;
254 if (iPosY < rcDesktop.top) iPosY=rcDesktop.top;
255 }
256 /* let Windows(R) place the Window for all but 1st one, for stairstep effect */
257 if (!firstWindow)
258 {
259 iPosX = CW_USEDEFAULT;
260 iPosY = CW_USEDEFAULT;
261 }
262 else firstWindow = false;
263
264 UT_DEBUGMSG(("KJD: Window Frame should be %d x %d [width x height]\n", iWidth, iHeight));
265
266
267 XAP_Win32App *pWin32App = static_cast<XAP_Win32App *>(XAP_App::getApp());
268
269 UT_Win32LocaleString str, title;
270 str.fromASCII (pWin32App->getApplicationName());
271 title.fromASCII (pWin32App->getApplicationTitleForTitleBar());
272
273 m_hwndFrame = UT_CreateWindowEx(0L, str.c_str(), title.c_str(),
274 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
275 iPosX, iPosY, iWidth, iHeight,
276 NULL, NULL, pWin32App->getInstance(), NULL);
277 UT_ASSERT(m_hwndFrame);
278
279
280 // bind this frame to its window
281 // WARNING: We assume in many places this refers to a XAP_Frame or descendant!!!
282 //SetWindowLongPtrW(m_hwndFrame, GWLP_USERDATA,(LONG_PTR)this);
283 SetWindowLongPtrW(m_hwndFrame, GWLP_USERDATA,(LONG_PTR)getFrame());
284
285 #ifndef UNICODE
286 // remove this when we are a true unicode app
287 m_mouseWheelMessage = RegisterWindowMessageW(L"MSWHEEL_ROLLMSG");
288 #else
289 m_mouseWheelMessage = RegisterWindowMessageW(MSH_MOUSEWHEEL);
290 #endif
291
292 // synthesize a menu from the info in our
293 // base class and install it into the window.
294 m_pWin32Menu = new EV_Win32MenuBar(pWin32App,
295 XAP_App::getApp()->getEditEventMapper(),
296 m_szMenuLayoutName,
297 m_szMenuLabelSetName);
298 UT_return_if_fail(m_pWin32Menu);
299 UT_DebugOnly<bool> bResult = m_pWin32Menu->synthesizeMenuBar(getFrame());
300 UT_ASSERT(bResult);
301
302 HMENU oldMenu = GetMenu(m_hwndFrame);
303 if (SetMenu(m_hwndFrame, m_pWin32Menu->getMenuHandle()))
304 {
305 DrawMenuBar(m_hwndFrame);
306 if (oldMenu)
307 DestroyMenu(oldMenu);
308 }
309
310 // create a rebar container for all the toolbars
311 m_hwndRebar = UT_CreateWindowEx(0L, REBARCLASSNAMEW, NULL,
312 WS_VISIBLE | WS_BORDER | WS_CHILD | WS_CLIPCHILDREN |
313 WS_CLIPSIBLINGS | CCS_NODIVIDER | CCS_NOPARENTALIGN |
314 RBS_VARHEIGHT | RBS_BANDBORDERS,
315 0, 0, 0, 0,
316 m_hwndFrame, NULL, pWin32App->getInstance(), NULL);
317 UT_ASSERT(m_hwndRebar);
318
319 /* override the window procedure*/
320 s_oldRedBar = (WHICHPROC)GetWindowLongPtrW(m_hwndRebar, GWLP_WNDPROC);
321 SetWindowLongPtrW(m_hwndRebar, GWLP_WNDPROC, (LONG_PTR)s_rebarWndProc);
322
323 // create a toolbar instance for each toolbar listed in our base class.
324
325 _createToolbars();
326
327 // figure out how much room is left for the child
328 GetClientRect(m_hwndFrame, &r);
329 iHeight = r.bottom - r.top;
330 iWidth = r.right - r.left;
331
332 m_iSizeWidth = iWidth;
333 m_iSizeHeight = iHeight;
334
335 // force rebar to resize itself
336 // TODO for some reason, we give REBAR the height of the FRAME
337 // TODO and let it decide how much it actually needs....
338 if( m_hwndRebar != NULL )
339 {
340 MoveWindow(m_hwndRebar, 0, 0, iWidth, iHeight, TRUE);
341
342 GetClientRect(m_hwndRebar, &r);
343 m_iBarHeight = r.bottom - r.top + 6;
344
345 UT_ASSERT(iHeight > m_iBarHeight);
346 iHeight -= m_iBarHeight;
347 }
348 else
349 m_iBarHeight = 0;
350
351 m_hwndContainer = _createDocumentWindow(getFrame(), m_hwndFrame, 0, m_iBarHeight, iWidth, iHeight);
352
353 // Let the app-specific frame code create the status bar
354 // if it wants to. we will put it below the document
355 // window (a peer with toolbars and the overall sunkenbox)
356 // so that it will appear outside of the scrollbars.
357
358 m_hwndStatusBar = _createStatusBarWindow(getFrame(), m_hwndFrame,0,m_iBarHeight+iHeight,iWidth);
359 GetClientRect(m_hwndStatusBar,&r);
360 m_iStatusBarHeight = r.bottom;
361
362
363 // Register drag and drop data and files
364 m_dropTarget.setFrame(getFrame());
365 RegisterDragDrop(m_hwndFrame, &m_dropTarget);
366
367 return;
368 }
369
370 bool XAP_Win32FrameImpl::_close(void)
371 {
372 UT_return_val_if_fail(m_hwndFrame, false);
373
374 // NOTE: This may not be the proper place, but it does mean that the
375 // last window closed is the one that the window state is stored from.
376 WINDOWPLACEMENT wndPlacement;
377 wndPlacement.length = sizeof(WINDOWPLACEMENT); // must do
378 if (GetWindowPlacement(m_hwndFrame, &wndPlacement))
379 {
380 UT_uint32 nFlags = PREF_FLAG_GEOMETRY_POS | PREF_FLAG_GEOMETRY_SIZE;
381
382 if (wndPlacement.showCmd == SW_SHOWMAXIMIZED)
383 nFlags |= PREF_FLAG_GEOMETRY_MAXIMIZED;
384
385 XAP_App::getApp()->setGeometry(wndPlacement.rcNormalPosition.left,
386 wndPlacement.rcNormalPosition.top,
387 wndPlacement.rcNormalPosition.right - wndPlacement.rcNormalPosition.left,
388 wndPlacement.rcNormalPosition.bottom - wndPlacement.rcNormalPosition.top,
389 nFlags);
390 }
391 else
392 {
393 // if failed to get placement then invalidate stored settings
394 XAP_App::getApp()->setGeometry(0,0,0,0,0);
395 }
396
397 RevokeDragDrop(m_hwndFrame);
398
399
400 // NOTE: this should only be called from the closeWindow edit method
401 DestroyWindow(m_hwndFrame);
402 m_hwndFrame = NULL;
403
404 return true;
405 }
406
407 bool XAP_Win32FrameImpl::_raise(void)
408 {
409 UT_return_val_if_fail(m_hwndFrame, false);
410 BringWindowToTop(m_hwndFrame);
411 return true;
412 }
413
414 bool XAP_Win32FrameImpl::_show(void)
415 {
416 UT_return_val_if_fail(m_hwndFrame, false);
417
418 ShowWindow(m_hwndFrame, SW_SHOW);
419 UpdateWindow(m_hwndFrame);
420
421 return true;
422 }
423
424
425 XAP_DialogFactory * XAP_Win32FrameImpl::_getDialogFactory(void)
426 {
427 return &m_dialogFactory;
428 }
429
430 EV_Toolbar * XAP_Win32FrameImpl::_newToolbar(XAP_Frame *frame, const char *szLayout, const char *szLanguage)
431 {
432 EV_Win32Toolbar *result = new EV_Win32Toolbar(static_cast<XAP_Win32App *>(XAP_App::getApp()),
433 frame,
434 szLayout, szLanguage);
435 // for now, position each one manually
436 // TODO: put 'em all in a rebar instead
437 HWND hwndBar = result->getWindow();
438 if (!hwndBar)
439 return result;
440
441 RECT rcClient;
442 GetClientRect(hwndBar, &rcClient);
443 const UT_uint32 iHeight = rcClient.bottom - rcClient.top;
444
445 m_iBarHeight += iHeight;
446
447 return result;
448 }
449
450 EV_Menu* XAP_Win32FrameImpl::_getMainMenu(void)
451 {
452 return m_pWin32Menu;
453 }
454
455 // Useful to refresh the size of the Frame. For instance,
456 // when the user selects hide statusbar, the Frame has to be
457 // resized in order to fill the gap leaved by the statusbar
458 void XAP_Win32FrameImpl::_queue_resize(void)
459 {
460 ::SendMessageW(m_hwndFrame, WM_SIZE, 0, MAKELONG(m_iRealSizeWidth, m_iRealSizeHeight));
461 }
462
463
464 bool XAP_Win32FrameImpl::_runModalContextMenu(AV_View * pView, const char * szMenuName,
465 UT_sint32 x, UT_sint32 y)
466 {
467 bool bResult = false;
468
469 UT_return_val_if_fail((m_pWin32Popup==NULL), false);
470
471 x = pView->getGraphics()->tdu(x);
472 y = pView->getGraphics()->tdu(y);
473
474 m_pWin32Popup = new EV_Win32MenuPopup(static_cast<XAP_Win32App*>(XAP_App::getApp()),szMenuName,m_szMenuLabelSetName);
475 m_pWin32Popup->setTrackMenu(true);
476
477 if (m_pWin32Popup && m_pWin32Popup->synthesizeMenuPopup(getFrame()))
478 {
479
480 UT_DEBUGMSG(("ContextMenu: %s at [%d,%d]\n",szMenuName,x,y));
481
482 _translateDocumentToScreen(x,y);
483
484 TrackPopupMenu(m_pWin32Popup->getMenuHandle(),
485 TPM_TOPALIGN | TPM_RIGHTBUTTON,
486 x,y,0,m_hwndFrame,NULL);
487
488 // the popup steals our capture, so we need to reset our counter.
489 EV_Win32Mouse *pWin32Mouse = static_cast<EV_Win32Mouse *>(m_pMouse);
490 pWin32Mouse->reset();
491 }
492
493 DELETEP(m_pWin32Popup);
494 return bResult;
495 }
496
497 void XAP_Win32FrameImpl::_setFullScreen(bool isFullScreen)
498 {
499 XAP_Frame * pFrame = XAP_App::getApp()->getLastFocussedFrame();
500 HWND hwndFrame = static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow();
501
502 // Get the window's style so we can add or remove the titlebar later
503 long hStyle = GetWindowLongPtrW(hwndFrame, GWL_STYLE);
504
505 if (isFullScreen)
506 {
507 // Save window state prior to fullscreen (are we maximized?)
508 WINDOWPLACEMENT wndPlacement;
509 wndPlacement.length = sizeof(WINDOWPLACEMENT);
510 if (GetWindowPlacement(hwndFrame, &wndPlacement))
511 {
512 m_iWindowStateBeforeFS = wndPlacement.showCmd;
513 }
514 else
515 {
516 // Couldn't retrieve windowplacement info
517 // Assume we were at Normal (non-maximized) state
518 m_iWindowStateBeforeFS = SW_SHOWNORMAL;
519 }
520
521 // Save window dimensions, as well
522 RECT rc;
523
524 if(!GetWindowRect(hwndFrame, &rc))
525 {
526 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
527 // try to pick some sane defaults that will work with any monitor
528 m_iWindowXBeforeFS = 0;
529 m_iWindowYBeforeFS = 0;
530 m_iWindowHeightBeforeFS = 400;
531 m_iWindowWidthBeforeFS = 400;
532 }
533 else
534 {
535 m_iWindowXBeforeFS = rc.left;
536 m_iWindowYBeforeFS = rc.top;
537 m_iWindowHeightBeforeFS = rc.bottom - rc.top;
538 m_iWindowWidthBeforeFS = rc.right - rc.left;
539 }
540
541 }
542
543 // Add or remove title-bar and border
544 SetWindowLongPtrW(hwndFrame, GWL_STYLE, isFullScreen ? hStyle & ~WS_CAPTION : hStyle | WS_CAPTION);
545
546 // We hide the window before maximizing
547 // to ensure it displays with the proper geometry
548 // and to bypass the window resizing animation
549 ShowWindow(hwndFrame, SW_HIDE);
550
551 ShowWindow(hwndFrame, isFullScreen ? SW_SHOWMAXIMIZED : m_iWindowStateBeforeFS);
552
553 // TODO: does the following code work well with multiple monitors?
554 if(isFullScreen)
555 {
556 int width = GetSystemMetrics(SM_CXSCREEN);
557 int height = GetSystemMetrics(SM_CYSCREEN);
558
559 if((height > 0) && (width > 0))
560 {
561 if(!SetWindowPos(hwndFrame, 0, 0, 0, width, height, 0))
562 {
563 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
564 }
565 }
566 else
567 {
568 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
569 }
570 }
571 else
572 {
573 if(!SetWindowPos(hwndFrame, 0, m_iWindowXBeforeFS, m_iWindowYBeforeFS, m_iWindowWidthBeforeFS, m_iWindowHeightBeforeFS, 0))
574 {
575 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
576 }
577 }
578
579 UpdateWindow(hwndFrame);
580 }
581
582
583
584
585 void XAP_Win32FrameImpl::_nullUpdate (void) const
586 {
587 MSG msg;
588 for( int i = 0 ; i < 10 ; i++ )
589 {
590 if( PeekMessageW( &msg, (HWND) NULL, 0, 0, PM_REMOVE) )
591 {
592 DispatchMessageW(&msg);
593 }
594 }
595 }
596
597 void XAP_Win32FrameImpl::_setCursor(GR_Graphics::Cursor /*cursor*/)
598 {
599 FV_View* pView = (FV_View *) getFrame()->getCurrentView();
600
601 // this is legitimate, when we first load, the frame has no view
602 if(!pView)
603 return;
604
605 GR_Win32Graphics * pG = (GR_Win32Graphics*)pView->getGraphics();
606 UT_return_if_fail( pG );
607
608 pG->handleSetCursorMessage();
609 }
610
611 UT_RGBColor XAP_Win32FrameImpl::getColorSelBackground () const
612 {
613 DWORD dwResult = GetSysColor(COLOR_HIGHLIGHT);
614 static UT_RGBColor clr((unsigned char)GetRValue(dwResult),
615 (unsigned char)GetGValue(dwResult),
616 (unsigned char)GetBValue(dwResult));
617
618 return clr;
619 }
620
621 UT_RGBColor XAP_Win32FrameImpl::getColorSelForeground () const
622 {
623 DWORD dwResult = GetSysColor(COLOR_HIGHLIGHTTEXT);
624 static UT_RGBColor clr((unsigned char)GetRValue(dwResult),
625 (unsigned char)GetGValue(dwResult),
626 (unsigned char)GetBValue(dwResult));
627
628 return clr;
629 }
630
631 #define MAXAPPNAME 256
632 bool XAP_Win32FrameImpl::_RegisterClass(XAP_Win32App * app)
633 {
634 ATOM a = UT_RegisterClassEx(CS_DBLCLKS, XAP_Win32FrameImpl::_FrameWndProc, app->getInstance(),
635 app->getIcon(), LoadCursor(NULL,IDC_ARROW), (HBRUSH)(COLOR_BTNFACE+1), app->getSmallIcon(),
636 NULL, /*app->getApplicationName()*/ L"Abiword");
637
638 UT_return_val_if_fail(a, false);
639
640 return true;
641 }
642
643 /*!
644 * This code is used by the dynamic menu API to rebuild the menus after a
645 * a change in the menu structure.
646 */
647 void XAP_Win32FrameImpl::_rebuildMenus(void)
648 {
649
650 // FIXME - This should do as advertized! RP
651 /*
652 // destroy old menu
653
654 m_pWin32Menu->destroy();
655 DELETEP(m_pWin32Menu);
656
657 // build new one.
658 m_pWin32Menu = new EV_Win32MenuBar(static_cast<XAP_Win32App*>(XAP_App::getApp()), getFrame(),
659 m_szMenuLayoutName,
660 m_szMenuLabelSetName);
661 UT_ASSERT(m_pUnixMenu);
662 bool bResult = m_pUnixMenu->rebuildMenuBar();
663 UT_ASSERT(bResult);
664 */
665
666 return;
667 /* Unix version:
668 // destroy old menu
669 m_pUnixMenu->destroy();
670 DELETEP(m_pUnixMenu);
671
672 // build new one.
673 m_pUnixMenu = new EV_UnixMenuBar(static_cast<XAP_UnixApp*>(XAP_App::getApp()), getFrame(),
674 m_szMenuLayoutName,
675 m_szMenuLabelSetName);
676 UT_ASSERT(m_pUnixMenu);
677 bool bResult = m_pUnixMenu->rebuildMenuBar();
678 UT_ASSERT(bResult);
679 */
680 }
681
682 /*****************************************************************/
683
684
685 LRESULT CALLBACK XAP_Win32FrameImpl::_FrameWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
686 {
687 XAP_Frame * f = (XAP_Frame*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
688 if (!f)
689 {
690 return UT_DefWindowProc(hwnd,iMsg,wParam,lParam);
691 }
692
693 XAP_Win32FrameImpl * fimpl = static_cast<XAP_Win32FrameImpl *>(f->getFrameImpl());
694 UT_return_val_if_fail(fimpl, UT_DefWindowProc(hwnd,iMsg,wParam,lParam));
695
696 AV_View * pView = NULL;
697
698 pView = f->getCurrentView();
699
700 if(iMsg == fimpl->m_mouseWheelMessage)
701 {
702 wParam = MAKEWPARAM(0, (short)(int)wParam);
703 return SendMessageW(hwnd, WM_MOUSEWHEEL, wParam, lParam);
704 }
705
706 switch (iMsg)
707 {
708
709 case WM_SETFOCUS:
710 {
711 // on set focus we want to make sure that the kbd layout
712 // is the same as it was before we lost focus. If the user
713 // was in some other application and changed the layout in
714 // there to something else and now returns to AW then we
715 // should not be in that different layout (this should not
716 // happen as the layouts are supposed to be thread
717 // specific, but I noted that sometimes even thought we
718 // have the correct layout, the icon on the task bar is
719 // out of sync with our layout; this should fix that).
720 XAP_Win32App *pWin32App = static_cast<XAP_Win32App *>(XAP_App::getApp());
721 UT_ASSERT( pWin32App );
722
723 if(pWin32App)
724 {
725 ActivateKeyboardLayout(pWin32App->getHKL(),0);
726 }
727
728
729 // fall through ...
730 }
731
732 case WM_EXITMENULOOP:
733 if (pView)
734 {
735 pView->focusChange(AV_FOCUS_HERE);
736 SetFocus(fimpl->m_hwndContainer);
737 }
738 return 0;
739
740 case WM_ENTERMENULOOP:
741 case WM_KILLFOCUS:
742 if (pView)
743 {
744 pView->focusChange(AV_FOCUS_NONE);
745 }
746 return 0;
747
748 case WM_CREATE:
749 return 0;
750
751 case WM_COMMAND:
752 if (fimpl->m_pWin32Popup)
753 {
754 if (fimpl->m_pWin32Popup->onCommand(pView,hwnd,wParam))
755 return 0;
756 }
757 else if (fimpl->m_pWin32Menu->onCommand(pView,hwnd,wParam))
758 {
759 return 0;
760 }
761 else if (HIWORD(wParam) == 0)
762 {
763 // after menu passes on it, give each of the toolbars a chance
764 UT_uint32 nrToolbars, k;
765 nrToolbars = fimpl->m_vecToolbars.getItemCount();
766 for (k=0; k < nrToolbars; k++)
767 {
768 EV_Win32Toolbar * t = (EV_Win32Toolbar *)fimpl->m_vecToolbars.getNthItem(k);
769 XAP_Toolbar_Id id = t->ItemIdFromWmCommand(LOWORD(wParam));
770 if (t->toolbarEvent(id))
771 return 0;
772 }
773 }
774 return UT_DefWindowProc(hwnd,iMsg,wParam,lParam);
775
776 case WM_INITMENU:
777 if (fimpl->m_pWin32Popup)
778 {
779 if (fimpl->m_pWin32Popup->onInitMenu(f,pView,hwnd,(HMENU)wParam))
780 return 0;
781 }
782 else if (fimpl->m_pWin32Menu->onInitMenu(f,pView,hwnd,(HMENU)wParam))
783 return 0;
784 return UT_DefWindowProc(hwnd,iMsg,wParam,lParam);
785
786 case WM_MEASUREITEM:
787 {
788
789 if (fimpl->m_pWin32Popup)
790 (fimpl->m_pWin32Popup->onMeasureItem(hwnd, wParam,lParam));
791 else
792 fimpl->m_pWin32Menu->onMeasureItem(hwnd, wParam,lParam);
793
794 return 0;
795 }
796
797 case WM_MENUCHAR:
798 {
799 if (fimpl->m_pWin32Popup)
800 return (fimpl->m_pWin32Popup->onMenuChar(hwnd, wParam,lParam));
801 else
802 return fimpl->m_pWin32Menu->onMenuChar(hwnd, wParam,lParam);
803
804 }
805
806 case WM_DRAWITEM:
807 {
808
809 if (fimpl->m_pWin32Popup)
810 {
811 fimpl->m_pWin32Popup->onDrawItem(hwnd,wParam,lParam);
812 }
813 else
814 fimpl->m_pWin32Menu->onDrawItem(hwnd,wParam,lParam);
815
816 return 0;
817 }
818
819 case WM_MENUSELECT:
820 {
821 if (fimpl->m_pWin32Popup)
822 {
823 fimpl->m_pWin32Popup->onMenuSelect(f,pView,hwnd,(HMENU)lParam,wParam);
824 }
825 else
826 fimpl->m_pWin32Menu->onMenuSelect(f,pView,hwnd,(HMENU)lParam,wParam);
827
828 return 0;
829
830 }
831
832 case WM_NOTIFY:
833 switch (((LPNMHDR) lParam)->code)
834 {
835
836 case TBN_DROPDOWN:
837 {
838 HWND hWnd = ((LPNMHDR) lParam)->hwndFrom;
839 EV_Win32Toolbar * t = (EV_Win32Toolbar *)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
840 t->onDropArrow(((LPNMTOOLBARW) lParam)->iItem);
841 Sleep(500); /* At least, half second where the arrow is shown as pressed*/
842 return TBDDRET_DEFAULT; /* Windows restores the pushed button*/
843 }
844
845 case TTN_NEEDTEXTW:
846 {
847 UT_uint32 nrToolbars, k;
848 nrToolbars = fimpl->m_vecToolbars.getItemCount();
849 for (k=0; k < nrToolbars; k++)
850 {
851 EV_Win32Toolbar * t = (EV_Win32Toolbar *)fimpl->m_vecToolbars.getNthItem(k);
852 if (t->getToolTip(lParam))
853 break;
854 }
855 }
856 break;
857
858 case RBN_HEIGHTCHANGE:
859 {
860 RECT r;
861 GetClientRect(fimpl->m_hwndFrame, &r);
862 int nWidth = r.right - r.left;
863 int nHeight = r.bottom - r.top;
864
865 GetClientRect(fimpl->m_hwndRebar, &r);
866 fimpl->m_iBarHeight = r.bottom - r.top + 6;
867
868 if (fimpl->m_hwndContainer)
869 {
870 // leave room for the toolbars
871 nHeight -= fimpl->m_iBarHeight;
872
873 MoveWindow(fimpl->m_hwndContainer, 0, fimpl->m_iBarHeight, nWidth, nHeight, TRUE);
874 }
875
876 f->queue_resize();
877 }
878 break;
879
880 case NM_CUSTOMDRAW:
881 {
882 LPNMCUSTOMDRAW pNMcd = (LPNMCUSTOMDRAW)lParam;
883 UT_uint32 nrToolbars, k;
884 nrToolbars = fimpl->m_vecToolbars.getItemCount();
885 for (k=0; k < nrToolbars; k++)
886 {
887 EV_Win32Toolbar * t = (EV_Win32Toolbar *)fimpl->m_vecToolbars.getNthItem(k);
888 if( t && (pNMcd->hdr.hwndFrom == t->getWindow()) )
889 {
890 if( pNMcd->dwDrawStage == CDDS_PREPAINT )
891 {
892 return CDRF_NOTIFYPOSTPAINT;
893 }
894 if( pNMcd->dwDrawStage == CDDS_POSTPAINT )
895 {
896 RECT rc;
897 HBRUSH hBr = NULL;
898
899 rc.top = pNMcd->rc.top;
900 rc.bottom = pNMcd->rc.bottom;
901 hBr = GetSysColorBrush( COLOR_3DFACE );
902
903 HWND hWndChild = FindWindowExW( pNMcd->hdr.hwndFrom, NULL, NULL, NULL );
904 while( hWndChild != NULL )
905 {
906 RECT rcChild;
907 POINT pt;
908 GetWindowRect( hWndChild, &rcChild );
909 pt.x = rcChild.left;
910 pt.y = rcChild.top;
911 ScreenToClient( pNMcd->hdr.hwndFrom, &pt );
912 rc.left = pt.x;
913 pt.x = rcChild.right;
914 pt.y = rcChild.bottom;
915 ScreenToClient( pNMcd->hdr.hwndFrom, &pt );
916 rc.right = pt.x;
917 FillRect( pNMcd->hdc, &rc, hBr );
918 hWndChild = FindWindowExW( pNMcd->hdr.hwndFrom, hWndChild, NULL, NULL );
919 }
920
921 /* Don't delete hBr since it was obtained using GetSysColorBrush, so System owned */
922 }
923 break;
924 }
925 }
926 }
927 break;
928
929 // Process other notifications here
930 default:
931 break;
932 } /* switch (((LPNMHDR) lParam)->code) */
933 break;
934
935 case WM_SIZE:
936 {
937
938 int nWidth = LOWORD(lParam);
939 int nHeight = HIWORD(lParam);
940
941 UT_DEBUGMSG ((("xap_Win32Frame::WM_SIZE %u - %u\n"), nWidth, nHeight));
942
943 if( pView && !pView->isLayoutFilling() )
944 {
945 fimpl->m_iRealSizeHeight = nHeight;
946 fimpl->m_iRealSizeWidth = nWidth;
947
948 fimpl->_startViewAutoUpdater();
949
950 if (nWidth != (int) fimpl->m_iSizeWidth && fimpl->m_hwndRebar != NULL)
951 {
952 MoveWindow(fimpl->m_hwndRebar, 0, 0, nWidth, fimpl->m_iBarHeight, TRUE);
953 }
954
955 // leave room for the toolbars and the status bar
956 nHeight -= fimpl->m_iBarHeight;
957
958 if (::IsWindowVisible(fimpl->m_hwndStatusBar))
959 nHeight -= fimpl->m_iStatusBarHeight;
960
961
962 if (fimpl->m_hwndStatusBar)
963 MoveWindow(fimpl->m_hwndStatusBar, 0, fimpl->m_iBarHeight+nHeight, nWidth, fimpl->m_iStatusBarHeight, TRUE);
964
965 if (fimpl->m_hwndContainer)
966 MoveWindow(fimpl->m_hwndContainer, 0, fimpl->m_iBarHeight, nWidth, nHeight, TRUE);
967
968 fimpl->m_iSizeWidth = nWidth;
969 fimpl->m_iSizeHeight = nHeight;
970
971 f->updateZoom();
972 }
973
974 return 0;
975 }
976
977 case WM_CLOSE:
978 {
979 XAP_App * pApp = XAP_App::getApp();
980 UT_return_val_if_fail(pApp,0);
981
982 const EV_EditMethodContainer * pEMC = pApp->getEditMethodContainer();
983 UT_return_val_if_fail(pEMC,0);
984
985 EV_EditMethod * pEM = pEMC->findEditMethodByName("closeWindowX");
986 UT_ASSERT_HARMLESS(pEM); // make sure it's bound to something
987
988 if (pEM)
989 {
990 pEM->Fn(pView,NULL);
991 return 0;
992 }
993
994 // let the window be destroyed
995 break;
996 }
997
998 case WM_INPUTLANGCHANGE:
999 {
1000 UT_DEBUGMSG(("XAP_Win32FrameImpl::_FrameWndProc: received input language change\n"));
1001
1002 // This will remap the static tables used by all frames.
1003 // (see the comment in ev_Win32Keyboard.cpp.)
1004 ev_Win32Keyboard *pWin32Keyboard = static_cast<ev_Win32Keyboard *>(fimpl->m_pKeyboard);
1005 pWin32Keyboard->remapKeyboard((HKL)lParam);
1006
1007 // set the language
1008 XAP_Win32App *pWin32App = static_cast<XAP_Win32App *>(XAP_App::getApp());
1009 UT_ASSERT_HARMLESS( pWin32App );
1010
1011 if(pWin32App)
1012 pWin32App->setKbdLanguage((HKL)lParam);
1013
1014 // Do not propagate this message.
1015
1016 return 1; //DefWindowProc(hwnd, iMsg, wParam, lParam);
1017 }
1018
1019 case WM_MOUSEWHEEL:
1020 {
1021 return SendMessageW(fimpl->m_hwndContainer, iMsg, wParam, lParam);
1022 }
1023
1024 case WM_SYSCOLORCHANGE:
1025 {
1026 if (fimpl->m_hwndRebar)
1027 {
1028 SendMessageW(fimpl->m_hwndRebar,WM_SYSCOLORCHANGE,0,0);
1029
1030 REBARBANDINFOW rbbi;
1031 memset(&rbbi, 0, sizeof(rbbi));
1032
1033 rbbi.cbSize = sizeof(REBARBANDINFO);
1034 rbbi.fMask = RBBIM_COLORS;
1035 rbbi.clrFore = GetSysColor(COLOR_BTNTEXT);
1036 rbbi.clrBack = GetSysColor(COLOR_BTNFACE);
1037
1038 UT_uint32 nrToolbars = fimpl->m_vecToolbars.getItemCount();
1039 for (UT_uint32 k=0; k < nrToolbars; k++)
1040 SendMessageW(fimpl->m_hwndRebar, RB_SETBANDINFO,k,(LPARAM)&rbbi);
1041 }
1042
1043 if (fimpl->m_hwndContainer)
1044 SendMessageW(fimpl->m_hwndContainer,WM_SYSCOLORCHANGE,0,0);
1045 if (fimpl->m_hwndStatusBar)
1046 SendMessageW(fimpl->m_hwndStatusBar,WM_SYSCOLORCHANGE,0,0);
1047 return 0;
1048 }
1049
1050 case WM_DROPFILES: //TODO: CHECK
1051 {
1052 HDROP hDrop = (HDROP) wParam;
1053 // How many files were dropped?
1054 int count = DragQueryFileW(hDrop, 0xFFFFFFFF, NULL, 0);
1055 WCHAR szFileName[PATH_MAX];
1056 UT_Win32LocaleString str;
1057 int i,pathlength;
1058 for (i=0; i<count; i++)
1059 {
1060 pathlength = DragQueryFileW(hDrop, i, NULL, 0);
1061 if (pathlength < PATH_MAX)
1062 {
1063 DragQueryFileW(hDrop, i, szFileName, PATH_MAX);
1064 XAP_App * pApp = XAP_App::getApp();
1065 UT_return_val_if_fail(pApp, 0);
1066 FV_View* pCurrentView = (FV_View *) f->getCurrentView();
1067 XAP_Frame * pNewFrame = 0;
1068 IEGraphicFileType iegft = IEGFT_Unknown;
1069 IE_ImpGraphic *pIEG;
1070 FG_Graphic* pFG;
1071 UT_Error errorCode = UT_ERROR;
1072
1073 /*
1074 The user may be dropping any kind of file
1075 Check first if the file is a graphic. If it's a graphic we insert it
1076 in the document, if not we assume that it's a document
1077 */
1078 // If there is no import graphic, it's a document...
1079 str.fromLocale(szFileName);
1080 char * uri = UT_go_filename_to_uri(str.utf8_str().utf8_str());
1081 if(uri)
1082 errorCode = IE_ImpGraphic::constructImporter(uri, iegft, &pIEG);
1083
1084 if(errorCode == UT_OK)
1085 {
1086 errorCode = pIEG->importGraphic(uri, &pFG);
1087
1088 DELETEP(pIEG);
1089 if(errorCode == UT_OK && pFG)
1090 {
1091 errorCode = pCurrentView->cmdInsertGraphic(pFG);
1092 DELETEP(pFG);
1093 }
1094 }
1095
1096 if (errorCode != UT_OK)
1097 {
1098 // Check if the current document is empty.
1099 if (f->isDirty() || f->getFilename() ||
1100 (f->getViewNumber() > 0))
1101 {
1102 pNewFrame = pApp->newFrame();
1103 if (pNewFrame == NULL)
1104 {
1105 errorCode = UT_ERROR;
1106 f->setStatusMessage("Could not open another window");
1107 g_free(uri);
1108 return 0;
1109 }
1110 }
1111 else
1112 {
1113 pNewFrame = f;
1114 }
1115
1116 if(uri)
1117 errorCode = pNewFrame->loadDocument(uri, IEFT_Unknown);
1118
1119 if (errorCode != UT_OK)
1120 {
1121 if (f != pNewFrame)
1122 pNewFrame->close();
1123 }
1124 else
1125 {
1126 pNewFrame->show();
1127 }
1128 }
1129
1130 if (errorCode != UT_OK)
1131 {
1132 s_CouldNotLoadFileMessage(f, uri, errorCode);
1133 }
1134 g_free(uri);
1135 }
1136 else
1137 {
1138 }
1139 }
1140 DragFinish(hDrop);
1141 }
1142 return 0;
1143
1144 case WM_DESTROY:
1145 return 0;
1146
1147 } /* switch (iMsg) */
1148
1149 return UT_DefWindowProc(hwnd, iMsg, wParam, lParam);
1150 }