NCBI C++ ToolKit
selection_helper.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: selection_helper.cpp 39189 2017-08-17 15:12:36Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 
37 #include <wx/dcclient.h>
38 #include <wx/txtstrm.h>
39 
41 
42 BEGIN_EVENT_TABLE(CSelectionHelper, wxEvtHandler)
44  EVT_LEFT_DOWN(CSelectionHelper::OnLeftDown)
45  EVT_LEFT_DCLICK(CSelectionHelper::OnLeftDClick)
47 
49 {
50  static CSelectionHelper _instance(2);
51  return _instance;
52 }
53 
54 void CSelectionHelper::Draw(wxDC& dc, const wxRect& updateRect, CTextPanelContext* context)
55 {
56  _ASSERT(m_Item);
58  x_DrawSelectionArea(dc, updateRect, context);
59 }
60 
62 {
64  int w = context->GetWWidth();
65  return wxRect (wxPoint(x-m_Offset*w,0), wxSize(w, m_Item->GetSize().GetHeight()));
66 }
67 
68 void CSelectionHelper::x_DrawSelectionArea(wxDC& dc, const wxRect& updateRect, CTextPanelContext* context)
69 {
70  wxBrush saveBrush = dc.GetBrush();
71  wxPen savePen = dc.GetPen();
72 
73  wxRect rect = m_Item->GetRect();
74  wxRect itemSelRect = x_GetSelectionRect(context);
75 
76  wxPoint dcOrigin = context->GetDeviceOrigin();
77  context->SetDeviceOrigin(wxPoint(dcOrigin.x + updateRect.GetLeft(), dcOrigin.y + updateRect.GetTop()));
78  context->SetDeviceOrigin(dc);
79 
80  itemSelRect = itemSelRect.Intersect(updateRect);
81  itemSelRect.Offset(-updateRect.GetLeft(), -updateRect.GetTop());
82  rect.SetPosition(wxPoint(-updateRect.GetLeft(), -updateRect.GetTop()));
83 
84  if (context->IsItemSelected(m_Item)) {
85  wxColor color(128,128,64);
86  dc.SetBrush(wxBrush(color));
87  dc.SetPen(wxPen(color, 1));
88  } else {
89  wxColor color = dc.GetTextBackground().ChangeLightness(95);
90  dc.SetBrush(wxBrush(color));
91  dc.SetPen(wxPen(color, 1));
92  }
93  dc.DrawRectangle(itemSelRect);
94 
95  if (context->IsItemSelected(m_Item) || context->GetHighlightedItem() == m_Item) {
96  wxColor color(128,128,64);
97  dc.SetPen(wxPen(color, 1));
98  int yt = rect.GetTop();
99  int yb = rect.GetBottom();
100  if (yt < updateRect.GetHeight() && yb > 0) {
101  if (yt >= 0)
102  dc.DrawLine(rect.GetLeft(), yt, rect.GetRight(), yt);
103  if (yb < updateRect.GetHeight())
104  dc.DrawLine(rect.GetLeft(), yb, rect.GetRight(), yb);
105  yt = max(yt, 0);
106  yb = min(yb, updateRect.GetHeight() - 1);
107  dc.DrawLine(rect.GetLeft(), yt, rect.GetLeft(), yb);
108  dc.DrawLine(rect.GetRight(), yt, rect.GetRight(), yb);
109  }
110  }
111 
112  dc.SetBrush(saveBrush);
113  dc.SetPen(savePen);
114 
115  context->SetDeviceOrigin(dcOrigin);
116 }
117 
118 void CSelectionHelper::OnMouseMove(wxMouseEvent& event)
119 {
120  CTextItemPanel* panel = static_cast<CTextItemPanel*>(event.GetEventObject());
122 
123  if (!panel->Selecting()) {
124  wxRect itemSelRect = x_GetSelectionRect(context);
125 
126  if (itemSelRect.Contains(event.m_x, event.m_y)) {
127  if (context->GetHighlightedItem() != m_Item) {
128  context->SetHighlightedItem(m_Item);
129  panel->Refresh();
130  }
131  panel->SetCursor(wxCursor(wxCURSOR_ARROW));
132  return;
133  }
134  else {
135  if (context->GetHighlightedItem() != 0) {
136  context->SetHighlightedItem(0);
137  panel->Refresh();
138  }
139  panel->SetCursor(wxCursor(wxCURSOR_IBEAM));
140  }
141  }
142  event.Skip();
143 }
144 
145 void CSelectionHelper::OnLeftDown(wxMouseEvent& event)
146 {
147  CTextItemPanel* panel = static_cast<CTextItemPanel*>(event.GetEventObject());
149 
150  wxRect itemSelRect = x_GetSelectionRect(context);
151  if (itemSelRect.Contains(event.m_x, event.m_y)) {
152  context->SelectItem(m_Item, event.ControlDown(), event.ShiftDown());
153  } else {
154  wxRect leftSpace(0, itemSelRect.GetTop(), itemSelRect.GetRight(), itemSelRect.GetHeight());
155  if (leftSpace.Contains(event.m_x, event.m_y))
156  context->SelectItem(0, event.ControlDown(), event.ShiftDown());
157  else
158  event.Skip();
159  }
160 }
161 
162 void CSelectionHelper::OnLeftDClick(wxMouseEvent& event)
163 {
164  CTextItemPanel* panel = static_cast<CTextItemPanel*>(event.GetEventObject());
166 
167  wxRect itemSelRect = (context->IsEditingEnabled() || event.ControlDown()) ?
168  wxRect (wxPoint(0,0), m_Item->GetSize()) : x_GetSelectionRect(context);
169 
170  if (itemSelRect.Contains(event.m_x, event.m_y)) {
171  context->SelectItem(m_Item, false, event.ShiftDown());
172  context->EditItem(m_Item, false, event.ShiftDown());
173  }
174  else
175  event.Skip();
176 }
177 
void OnMouseMove(wxMouseEvent &event)
wxRect x_GetSelectionRect(CTextPanelContext *context) const
void x_DrawSelectionArea(wxDC &dc, const wxRect &updateRect, CTextPanelContext *context)
void OnLeftDClick(wxMouseEvent &event)
void OnLeftDown(wxMouseEvent &event)
void Draw(wxDC &dc, const wxRect &updateRect, CTextPanelContext *context)
bool Selecting() const
CTextPanelContext * GetContext()
virtual wxSize GetSize() const =0
virtual int GetTextLeftMargin(CTextPanelContext *context) const =0
virtual wxRect GetRect() const
Definition: text_item.hpp:83
virtual const CConstRef< CObject > GetAssosiatedObject() const
Definition: text_item.hpp:109
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
n background color
END_EVENT_TABLE()
T max(T x_, T y_)
T min(T x_, T y_)
#define _ASSERT
static CS_CONTEXT * context
Definition: will_convert.c:21
Modified on Thu May 30 12:21:55 2024 by modify_doxy.py rev. 669887