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

Go to the SVN repository for this file.

1 /* $Id: text_item.cpp 38729 2017-06-13 15:50:41Z 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 
38 
39 #include <wx/dcclient.h>
40 
42 
43 #ifdef _DEBUG
45 #endif
46 
48 {
49 public:
50  CCalcSizeStream() : m_LineSize(0), m_LineCount(0), m_MaxLineSize(0) {}
51 
52  virtual StreamHint GetHint() const { return kDisplay; }
53 
54  virtual CStyledTextOStream& operator<< (const string& text) { x_AddChars((int)text.length()); return *this; }
55 
56  virtual int CurrentRowLength() const { return m_LineSize; }
57 
58  virtual void SetStyle(const CTextStyle* WXUNUSED(style)) {}
59  virtual void SetDefaultStyle() {}
60  virtual void Indent (size_t steps) { x_AddChars((int)steps); }
61  virtual void NewLine() { m_LineSize = 0; ++m_LineCount; }
62 
63  wxSize GetSize() const;
64 
65 private:
66  void x_AddChars(int count)
67  {
68  m_LineSize += count;
69  if (m_LineSize > m_MaxLineSize)
70  m_MaxLineSize = m_LineSize;
71  }
75 };
76 
78 {
79  int lineCount = (m_LineCount == 0 && m_MaxLineSize > 0) ? 1 : m_LineCount;
80  return wxSize(m_MaxLineSize, lineCount);
81 }
82 
84 {
85  return context->GetLeftMargin() + context->GetIndentWidth()*GetIndent();
86 }
87 
89 {
90  CCalcSizeStream ostream;
91  x_RenderText(ostream, context);
92  wxSize size = ostream.GetSize();
93 
94  wxSize wSize = context->GetWSize();
95  m_Size = wxSize(size.GetWidth()*wSize.GetWidth(), size.GetHeight()*wSize.GetHeight());
96  m_Size.IncBy(context->GetLeftMargin(), 0);
97  m_LineCount = size.GetHeight();
98 }
99 
100 namespace {
101 class CSelectionModifier : public CDrawTextOStream::IStyleModifier
102 {
103 public:
104  static CSelectionModifier& GetInstance()
105  {
106  static CSelectionModifier _inst;
107  return _inst;
108  }
109  virtual void SetStyle(wxDC& dc)
110  {
111  wxColor background(49, 106, 197);
112  dc.SetBackgroundMode(wxSOLID);
113  dc.SetTextForeground(wxColor(255,255,255));
114  dc.SetTextBackground(background);
115  dc.SetBrush(wxBrush(background));
116  dc.SetPen(wxPen(background));
117  }
118 };
119 class CTestModifier : public CDrawTextOStream::IStyleModifier
120 {
121 public:
122  static CDrawTextOStream::IStyleModifier& GetInstance()
123  {
124  static CTestModifier _inst;
125  return _inst;
126  }
127  virtual void SetStyle(wxDC& dc)
128  {
129  dc.SetBackgroundMode(wxTRANSPARENT);
130  dc.SetTextForeground(wxColor(0,222,0));
131  }
132 };
133 }
134 
135 void CTextItem::Draw(wxDC& dc, const wxRect& updateRect, CTextPanelContext* context)
136 {
137  if (IsSelectable()) {
139  selHelper.SetItem(this);
140  selHelper.Draw(dc, updateRect, context);
141  }
142 
143  CTextBlock block = context->GetPanel()->GetTextSelection().Normalized();
144  block.ShiftRow(-GetStartLine());
145 
146  CTextPosition start = block.GetStart();
147  CTextPosition end = block.GetEnd();
148 
149  TextPosToCollapsed(start, context);
151  block = CTextBlock(start, end);
152 
153  context->SetDeviceOrigin(dc);
154  CDrawTextOStream ostream(dc, context->GetLeftMargin(), 0,
155  dc.GetCharHeight(), context->GetIndentWidth());
156 
157  ostream.AddMarker(block.GetStart().GetRow(), block.GetStart().GetCol(),
158  block.GetEnd().GetRow(), block.GetEnd().GetCol(), &CSelectionModifier::GetInstance());
159  //ostream.AddMarker(1, 7, 2, 3, &CTestModifier::GetInstance());
160 
161  context->InitDrawStream(ostream, GetStartLine());
162 
163  x_RenderText(ostream, context);
164 }
165 
167 {
168  ostream << Indent(GetIndent());
169 }
170 
172 {
173  wxRect rect(wxPoint(0, 0), GetSize());
174  if (rect.Contains(event.GetPosition())) {
175  context.MouseOverItem(this);
176  }
177 
178  if (IsSelectable()) {
180  selHelper.SetItem(this);
181  if (selHelper.ProcessEvent(event))
182  return;
183  }
184  event.Skip();
185 }
186 
187 ITextItem* CTextItem::GetItemByLine(int lineNum, bool /*expanded*/)
188 {
189  if (lineNum >= 0 && lineNum < GetLineCount())
190  return this;
191  return 0;
192 }
193 
static void Indent(FILE *output_stream, int indent_depth, const char *indent)
Definition: util.cpp:659
CAtomicCounter_WithAutoInit –.
Definition: ncbicntr.hpp:120
void x_AddChars(int count)
Definition: text_item.cpp:66
virtual void Indent(size_t steps)
Definition: text_item.cpp:60
virtual int CurrentRowLength() const
Definition: text_item.cpp:56
virtual void SetDefaultStyle()
Definition: text_item.cpp:59
virtual void SetStyle(const CTextStyle *WXUNUSED(style))
Definition: text_item.cpp:58
virtual StreamHint GetHint() const
Definition: text_item.cpp:52
virtual void NewLine()
Definition: text_item.cpp:61
wxSize GetSize() const
Definition: text_item.cpp:77
void AddMarker(int startRow, int startCol, int endRow, int endCol, IStyleModifier *style)
static CSelectionHelper & GetInstance()
void SetItem(ITextItem *item)
void Draw(wxDC &dc, const wxRect &updateRect, CTextPanelContext *context)
virtual CStyledTextOStream & operator<<(const string &text)=0
CTextPosition GetEnd() const
CTextPosition GetStart() const
void ShiftRow(int delta)
size_t m_LineCount
Definition: text_item.hpp:171
virtual int GetStartLine() const
Definition: text_item.hpp:145
virtual void CalcSize(wxDC &dc, CTextPanelContext *context)
Definition: text_item.cpp:88
virtual void x_Indent(CStyledTextOStream &ostream) const
Definition: text_item.cpp:166
virtual size_t GetLineCount() const
Definition: text_item.hpp:148
virtual void TextPosToCollapsed(CTextPosition &WXUNUSED(pos), CTextPanelContext *WXUNUSED(context)) const
Definition: text_item.hpp:162
virtual int GetIndent() const
Definition: text_item.hpp:157
virtual wxSize GetSize() const
Definition: text_item.hpp:140
wxSize m_Size
Definition: text_item.hpp:172
virtual void x_RenderText(CStyledTextOStream &WXUNUSED(ostream), CTextPanelContext *WXUNUSED(context)) const
Definition: text_item.hpp:169
virtual int GetTextLeftMargin(CTextPanelContext *context) const
Definition: text_item.cpp:83
virtual void MouseEvent(wxMouseEvent &event, CTextPanelContext &context)
Definition: text_item.cpp:171
virtual ITextItem * GetItemByLine(int lineNum, bool expanded)
Definition: text_item.cpp:187
virtual void Draw(wxDC &dc, const wxRect &updateRect, CTextPanelContext *context)
Definition: text_item.cpp:135
int GetRow() const
int GetCol() const
virtual bool IsSelectable() const
Definition: text_item.hpp:112
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIWIDGETS_TEXT_WIDGET_EXPORT
Definition: gui_export.h:532
static void text(MDB_val *v)
Definition: mdb_dump.c:62
const struct ncbi::grid::netcache::search::fields::SIZE size
CAtomicCounter_WithAutoInit gITextItemCount
Definition: text_item.cpp:44
static CS_CONTEXT * context
Definition: will_convert.c:21
Modified on Wed Apr 24 14:19:15 2024 by modify_doxy.py rev. 669887