gui_event.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:15k
- /*
- * ===========================================================================
- * PRODUCTION $Log: gui_event.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 21:04:59 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: gui_event.cpp,v 1000.1 2004/06/01 21:04:59 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio
- *
- * File Description:
- * Generic FLTK utilities.
- */
- #include <ncbi_pch.hpp>
- #include <gui/utils/fltk_utils.hpp>
- #include <FL/Fl.H>
- #include <FL/fl_draw.H>
- #include <FL/Fl_Window.H>
- BEGIN_NCBI_SCOPE
- CFltkEvent::TEvent CFltkEvent::GetEvent(void)
- {
- TEvent state = 0;
- //
- // raw state processing
- //
- if (Fl::event_button1()) {
- state |= fLeftMouse;
- }
-
- if (Fl::event_button2()) {
- state |= fMiddleMouse;
- }
- if (Fl::event_button3()) {
- state |= fRightMouse;
- }
- if (Fl::event_shift()) {
- state |= fShift;
- }
-
- if (Fl::event_ctrl()) {
- state |= fCtrl;
- }
-
- if (Fl::event_alt()) {
- state |= fAlt;
- }
-
- if ( (Fl::event_key() == FL_Meta_R && Fl::event_key(FL_Meta_R)) ||
- (Fl::event_key() == FL_Meta_L && Fl::event_key(FL_Meta_L)) ) {
- state |= fMeta;
- }
-
- return state;
- }
- CFltkEvent::EEvent CFltkEvent::GetProcessedEvent()
- {
- TEvent state = GetEvent();
- TEventMap::const_iterator iter = sm_EventMap.find(state);
- if (iter != sm_EventMap.end()) {
- return iter->second;
- }
- return eNoState;
- }
- void CFltkEvent::SetState(EEvent state, TEvent key_combo)
- {
- sm_EventMap[key_combo] = state;
- }
- CFltkEvent::TEvent CFltkEvent::GetState(EEvent state)
- {
- ITERATE (TEventMap, iter, sm_EventMap) {
- if (iter->second == state) {
- return iter->first;
- }
- }
- return 0;
- }
- //
- // set our default interaction modes
- // this is operating-system dependent
- //
- void CFltkEvent::SetOSDefaults()
- {
- #if defined(NCBI_OS_MAC) || defined(NCBI_OS_DARWIN)
- CFltkEvent::SetState(CFltkEvent::eSelectState,
- CFltkEvent::fLeftMouse);
- CFltkEvent::SetState(CFltkEvent::eMultiSelectState,
- CFltkEvent::fShiftLeftMouse);
- CFltkEvent::SetState(CFltkEvent::ePopupState,
- CFltkEvent::fCtrlLeftMouse);
- CFltkEvent::SetState(CFltkEvent::ePanState,
- CFltkEvent::fMetaLeftMouse);
- CFltkEvent::SetState(CFltkEvent::eZoomState,
- CFltkEvent::fAltLeftMouse);
- #elif defined(NCBI_OS_UNIX)
- // select = left mouse click
- CFltkEvent::SetState(CFltkEvent::eSelectState,
- CFltkEvent::fLeftMouse);
- // multiple select = shift left mouse click
- CFltkEvent::SetState(CFltkEvent::eMultiSelectState,
- CFltkEvent::fShiftLeftMouse);
- // pop-up = right mouse button
- CFltkEvent::SetState(CFltkEvent::ePopupState,
- CFltkEvent::fRightMouse);
- // pan = middle mouse
- CFltkEvent::SetState(CFltkEvent::ePanState,
- CFltkEvent::fMiddleMouse);
- // zoom in = alt left mouse
- CFltkEvent::SetState(CFltkEvent::eZoomState,
- CFltkEvent::fCtrlLeftMouse);
- #elif defined(NCBI_OS_MSWIN)
- // select = left mouse click
- CFltkEvent::SetState(CFltkEvent::eSelectState,
- CFltkEvent::fLeftMouse);
- // multiple select = shift left mouse click
- CFltkEvent::SetState(CFltkEvent::eMultiSelectState,
- CFltkEvent::fShiftLeftMouse);
- // pop-up = right mouse button
- CFltkEvent::SetState(CFltkEvent::ePopupState,
- CFltkEvent::fRightMouse);
- // pan = control left mouse
- CFltkEvent::SetState(CFltkEvent::ePanState,
- CFltkEvent::fCtrlLeftMouse);
- // zoom in = alt left mouse
- CFltkEvent::SetState(CFltkEvent::eZoomState,
- CFltkEvent::fAltLeftMouse);
- #endif
- }
- CGUIEvent::CGUIEvent()
- : m_LastFLTKEvent(0),
- m_Modifiers(0)
- {
- }
- int CGUIEvent::GetFLTKEvent() const
- {
- return m_LastFLTKEvent;
- }
- void CGUIEvent::OnFLTKEvent(int event)
- {
- m_LastFLTKEvent = event;
- m_MouseEvent = eNone;
- m_GUISignal = eDefaultSignal;
- switch(event) {
- case FL_KEYDOWN: x_OnKeyPressed(); break;
- case FL_KEYUP: x_OnKeyReleased(); break;
- case FL_UNFOCUS:
- {{
- m_stPressedKeys.clear();
- m_Modifiers = 0;
- }}
- break;
-
- case FL_MOVE: m_MouseEvent = eMouseMove; break;
- case FL_PUSH:
- switch(Fl::event_button()) {
- case FL_LEFT_MOUSE: m_MouseEvent = eLeftMousePush; break;
- case FL_MIDDLE_MOUSE: m_MouseEvent = eMiddleMousePush; break;
- case FL_RIGHT_MOUSE: m_MouseEvent = eRightMousePush; break;
- }
- x_UpdateEventState();
- // generate signal for requesting focus
- break;
- case FL_DRAG:
- switch(Fl::event_button()) {
- case FL_LEFT_MOUSE: m_MouseEvent = eLeftMouseDrag; break;
- case FL_MIDDLE_MOUSE: m_MouseEvent = eMiddleMouseDrag; break;
- case FL_RIGHT_MOUSE: m_MouseEvent = eRightMouseDrag; break;
- }
- x_UpdateEventState();
- break;
- case FL_RELEASE:
- switch(Fl::event_button()) {
- case FL_LEFT_MOUSE: m_MouseEvent = eLeftMouseRelease; break;
- case FL_MIDDLE_MOUSE: m_MouseEvent = eMiddleMouseRelease; break;
- case FL_RIGHT_MOUSE: m_MouseEvent = eRightMouseRelease; break;
- }
- x_UpdateEventState();
- break;
- //case FL_MOUSEWHEEL: res = x_HandleMouseWheel(event); break;
- default:
- break;
- }
-
- // currently we support only one generic key hold at a time
- int key = (m_stPressedKeys.size() == 1) ? *m_stPressedKeys.begin() : 0;
-
-
- // shortcuts have priority
- TShortcutTemplate sh_templ = x_EncodeShortcutTemplate(m_Modifiers, key);
- TTemplToShortcut::const_iterator it = m_TemplToShortcut.find(sh_templ);
- if(it != m_TemplToShortcut.end()) {
- m_GUISignal = it->second;
- }
- else { // handling mouse states/signals
- TModTemplate templ = x_EncodeModTemplate(m_Modifiers, key);
- TTemplToState::const_iterator it = m_TemplToState.find(templ);
- m_GUIState = (it != m_TemplToState.end()) ? it->second : eDefaultState;
- TEventTemplate e_templ= x_EncodeEventTemplate(m_GUIState, m_MouseEvent);
- TTemplToSignal::const_iterator it1 = m_TemplToSignal.find(e_templ);
- if(it1 != m_TemplToSignal.end()) {
- m_GUISignal = it1->second;
- } else {
- switch(m_MouseEvent) {
- case eMouseMove: m_GUISignal = eDefaultSignal; break;
- case eLeftMousePush:
- case eMiddleMousePush:
- case eRightMousePush: m_GUISignal = ePush; break;
- case eLeftMouseDrag:
- case eMiddleMouseDrag:
- case eRightMouseDrag: m_GUISignal = eDrag; break;
- case eLeftMouseRelease:
- case eMiddleMouseRelease:
- case eRightMouseRelease: m_GUISignal = eRelease; break;
- }
- }
- }
- //cout << "nState " << m_GUIState << " Signal " << m_GUISignal;
- }
- CGUIEvent::EGUIState CGUIEvent::GetGUIState() const
- {
- return m_GUIState;
- }
- CGUIEvent::EGUISignal CGUIEvent::GetGUISignal() const
- {
- return m_GUISignal;
- }
- void CGUIEvent::RegisterState(EGUIState state, int mod_flags, int key)
- {
- TModTemplate templ = x_EncodeModTemplate(mod_flags, key);
- m_TemplToState[templ] = state;
- }
- void CGUIEvent::RegisterSignal(EGUISignal signal, EGUIState state, EMouseEvent event)
- {
- TEventTemplate templ = x_EncodeEventTemplate(state, event);
- m_TemplToSignal[templ] = signal;
- }
- void CGUIEvent::RegisterShortcut(EGUISignal signal, int mod_flags, int key)
- {
- TShortcutTemplate templ = x_EncodeShortcutTemplate(mod_flags, key);
- m_TemplToShortcut[templ] = signal;
- }
- void CGUIEvent::StandardConfig()
- {
- RegisterState(CGUIEvent::eSelectState, 0, 0);
- RegisterState(CGUIEvent::eSelectExtState, CGUIEvent::fShift, 0);
- // OS-specific settings
- #if defined(NCBI_OS_MAC) || defined(NCBI_OS_DARWIN)
- RegisterState(CGUIEvent::eSelectIncState, CGUIEvent::fShift, 0);
- RegisterState(CGUIEvent::eSelectIncState, CGUIEvent::fMeta, 0);
- RegisterState(CGUIEvent::ePopupState, CGUIEvent::fCtrl, 0);
- RegisterSignal(CGUIEvent::ePopupSignal, CGUIEvent::ePopupState, CGUIEvent::eLeftMousePush);
- #elif defined(NCBI_OS_UNIX)
- RegisterState(CGUIEvent::eSelectIncState, CGUIEvent::fCtrl, 0);
- RegisterState(CGUIEvent::eSelectExtState, CGUIEvent::fShift | CGUIEvent::fCtrl, 0);
-
- RegisterSignal(CGUIEvent::ePopupSignal, CGUIEvent::eSelectState, CGUIEvent::eRightMouseRelease);
- #elif defined(NCBI_OS_MSWIN)
- RegisterState(CGUIEvent::eSelectIncState, CGUIEvent::fCtrl, 0);
- RegisterState(CGUIEvent::eSelectExtState, CGUIEvent::fShift | CGUIEvent::fCtrl, 0);
- RegisterSignal(CGUIEvent::ePopupSignal, CGUIEvent::eSelectState, CGUIEvent::eRightMouseRelease);
- RegisterSignal(CGUIEvent::eSelectSignal, CGUIEvent::eSelectState, CGUIEvent::eRightMousePush);
- #endif
- RegisterState(CGUIEvent::eSelectAltState, CGUIEvent::fAlt, 0);
-
- RegisterSignal(CGUIEvent::eSelectSignal, CGUIEvent::eSelectState, CGUIEvent::eLeftMousePush);
- RegisterSignal(CGUIEvent::eSelectSignal, CGUIEvent::eSelectIncState, CGUIEvent::eLeftMousePush);
- RegisterSignal(CGUIEvent::eSelectSignal, CGUIEvent::eSelectAltState, CGUIEvent::eLeftMousePush);
- RegisterSignal(CGUIEvent::eSelectSignal, CGUIEvent::eSelectExtState, CGUIEvent::eLeftMousePush);
-
- RegisterState(CGUIEvent::eZoomState, 0, 'z');
- RegisterState(CGUIEvent::eZoomRectState, 0, 'r');
- RegisterState(CGUIEvent::eMarkState, 0, 'm');
- RegisterState(CGUIEvent::ePanState, 0, 'p');
- // ### these signals below should be registered as Shortcuts !
- RegisterState(CGUIEvent::eSelectAllState, CGUIEvent::fCtrl, 'a');
- RegisterState(CGUIEvent::eCopyState, CGUIEvent::fCtrl, 'c');
- RegisterState(CGUIEvent::eCutState, CGUIEvent::fCtrl, 'x');
- RegisterState(CGUIEvent::ePasteState, CGUIEvent::fCtrl, 'v');
- RegisterState(CGUIEvent::eUndoState, CGUIEvent::fCtrl, 'z');
- RegisterState(CGUIEvent::eRedoState, CGUIEvent::fCtrl, 'y');
- RegisterState(CGUIEvent::eDeleteState, 0, FL_Delete);
-
- RegisterShortcut(eMenuActivateSignal, 0, FL_F + 10);
- RegisterShortcut(eZoomInSignal, 0, FL_KP + '+'); // gray +
- RegisterShortcut(eZoomInSignal, 0, '='); // '+'
- RegisterShortcut(eZoomOutSignal, 0, FL_KP + '-'); // gray -
- RegisterShortcut(eZoomOutSignal, 0, '-');
-
- RegisterShortcut(eZoomAllSignal, 0, FL_KP + '*'); // gray *
- RegisterShortcut(eZoomAllSignal, 0, '8'); // '*'
- }
- int CGUIEvent::GetAccelByEvent()
- {
- int accel = 0;
- if(m_stPressedKeys.size() == 1) {
- int key = *m_stPressedKeys.begin();
- accel = toupper(key);
-
- if(m_Modifiers & fShift) {
- accel |= FL_SHIFT;
- }
- if(m_Modifiers & fCtrl) {
- accel |= FL_CTRL;
- }
- if(m_Modifiers & fAlt) {
- accel |= FL_ALT;
- }
- if(m_Modifiers & fMeta) {
- accel |= FL_META;
- }
- }
- return accel;
- }
- CGUIEvent::TModTemplate CGUIEvent::x_EncodeModTemplate(int mod_flags, int key) const
- {
- return (mod_flags << 16) | key;
- }
- CGUIEvent::TEventTemplate CGUIEvent::x_EncodeEventTemplate(EGUIState state, EMouseEvent event) const
- {
- return (state << 16) | (0xFFFF & event);
- }
- CGUIEvent::TModTemplate CGUIEvent::x_EncodeShortcutTemplate(int mod_flags, int key) const
- {
- return (mod_flags << 16) | key;
- }
- void CGUIEvent::x_UpdateEventState()
- {
- int new_state = 0;
- int fl_state = Fl::event_state();
-
- if(fl_state & FL_SHIFT) {
- new_state |= fShift;
- }
- if(fl_state & FL_CTRL) {
- new_state |= fCtrl;
- }
- if(fl_state & FL_ALT) {
- new_state |= fAlt;
- }
- if(fl_state & FL_META) {
- new_state |= fMeta;
- }
- m_Modifiers = new_state;
- }
- void CGUIEvent::x_OnKeyPressed()
- {
- int key = Fl::event_key();
- //cout << "nCGUIEvent::x_OnKeyPressed() " << key;
-
- switch(key)
- {
- case FL_Shift_L:
- case FL_Shift_R: m_Modifiers |= fShift; break;
- case FL_Control_L:
- case FL_Control_R: m_Modifiers |= fCtrl; break;
- case FL_Alt_L:
- case FL_Alt_R: m_Modifiers |= fAlt; break;
- case FL_Meta_L:
- case FL_Meta_R: m_Modifiers |= fMeta; break;
- default: m_stPressedKeys.insert(key); break;
- }
- }
- void CGUIEvent::x_OnKeyReleased()
- {
- int key = Fl::event_key();
- //cout << "nCGUIEvent::x_OnKeyReleased() " << key;
-
- switch(key)
- {
- case FL_Shift_L:
- case FL_Shift_R: m_Modifiers &= ~fShift; break;
- case FL_Control_L:
- case FL_Control_R: m_Modifiers &= ~fCtrl; break;
- case FL_Alt_L:
- case FL_Alt_R: m_Modifiers &= ~fAlt; break;
- case FL_Meta_L:
- case FL_Meta_R: m_Modifiers &= ~fMeta; break;
- default: m_stPressedKeys.erase(key); break;
- }
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: gui_event.cpp,v $
- * Revision 1000.1 2004/06/01 21:04:59 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- *
- * Revision 1.6 2004/05/27 17:19:52 johnson
- * Add right mouse select signal to StandardConfig on mswin
- *
- * Revision 1.5 2004/05/21 22:27:51 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.4 2004/05/13 17:18:12 yazhuk
- * Added support for accelerators
- *
- * Revision 1.3 2004/04/05 14:39:36 yazhuk
- * Implemented support for Shortcuts
- *
- * Revision 1.2 2004/03/23 13:39:54 dicuccio
- * Added new event (eDeleteState)
- *
- * Revision 1.1 2004/03/17 19:51:59 yazhuk
- * Moved Event classes from fltk_utils.cpp
- *
- * ===========================================================================
- */