]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
Cursor and insert/delete work much better now, code streamlined, still
[wxWidgets.git] / user / wxLayout / wxlwindow.cpp
1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9 #ifdef __GNUG__
10 # pragma implementation "wxlwindow.h"
11 #endif
12
13 #include "wxlwindow.h"
14
15 #define VAR(x) cout << #x"=" << x << endl;
16
17 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
18 EVT_PAINT (wxLayoutWindow::OnPaint)
19 EVT_CHAR (wxLayoutWindow::OnChar)
20 EVT_LEFT_DOWN(wxLayoutWindow::OnMouse)
21 END_EVENT_TABLE()
22
23 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
24 : wxScrolledWindow(parent)
25 {
26 m_ScrollbarsSet = false;
27 m_EventId = -1;
28 }
29
30 void
31 wxLayoutWindow::OnMouse(wxMouseEvent& event)
32 {
33 if(m_EventId == -1) // nothing to do
34 return;
35
36 m_FindPos.x = event.GetX();
37 m_FindPos.y = event.GetY();
38 m_FoundObject = NULL;
39
40 #ifdef WXLAYOUT_DEBUG
41 cerr << "OnMouse: " << m_FindPos.x << ',' << m_FindPos.y << endl;
42 #endif
43 Refresh();
44 if(m_FoundObject)
45 {
46 if(m_EventId != -1)
47 {
48 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, m_EventId);
49 commandEvent.SetEventObject( this );
50 commandEvent.SetClientData((char *)m_FoundObject);
51 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
52 GetEventHandler()->ProcessEvent(commandEvent);
53 }
54 }
55 }
56
57 /*
58 * some simple keyboard handling
59 */
60 void
61 wxLayoutWindow::OnChar(wxKeyEvent& event)
62 {
63 if(! m_llist.IsEditable()) // do nothing
64 {
65 event.Skip();
66 return;
67 }
68
69 long keyCode = event.KeyCode();
70 wxPoint p;
71
72 switch(event.KeyCode())
73 {
74 case WXK_RIGHT:
75 m_llist.MoveCursor(1);
76 break;
77 case WXK_LEFT:
78 m_llist.MoveCursor(-1);
79 break;
80 case WXK_UP:
81 m_llist.MoveCursor(0,-1);
82 break;
83 case WXK_DOWN:
84 m_llist.MoveCursor(0,1);
85 break;
86 case WXK_PRIOR:
87 m_llist.MoveCursor(0,-20);
88 break;
89 case WXK_NEXT:
90 m_llist.MoveCursor(0,20);
91 break;
92 case WXK_HOME:
93 p = m_llist.GetCursor();
94 p.x = 0;
95 m_llist.SetCursor(p);
96 break;
97 case WXK_END:
98 p = m_llist.GetCursor();
99 p.x = m_llist.GetLineLength(m_llist.FindCurrentObject(NULL));
100 m_llist.SetCursor(p);
101 break;
102 case WXK_DELETE :
103 m_llist.Delete(1);
104 break;
105 case WXK_BACK: // backspace
106 m_llist.MoveCursor(-1);
107 m_llist.Delete(1);
108 break;
109 case WXK_RETURN:
110 m_llist.LineBreak();
111 break;
112 #ifdef WXLAYOUT_DEBUG
113 case WXK_F1:
114 m_llist.Debug();
115 break;
116 #endif
117 default:
118 if(keyCode < 256 && keyCode >= 32)
119 {
120 String tmp;
121 tmp += keyCode;
122 m_llist.Insert(tmp);
123 }
124 break;
125 }
126 Refresh();
127 UpdateScrollbars();
128 }
129
130 void
131 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)w) // or: OnDraw(wxDC& dc)
132 {
133 wxPaintDC dc( this ); // only when used as OnPaint for OnDraw we
134 PrepareDC( dc ); // can skip the first two lines
135
136 if(m_EventId != -1) // look for keyclicks
137 m_FoundObject = m_llist.Draw(dc,true,m_FindPos);
138 else
139 m_llist.Draw(dc);
140 if(! m_ScrollbarsSet)
141 {
142 m_ScrollbarsSet = true; // avoid recursion
143 UpdateScrollbars();
144 }
145 }
146
147 void
148 wxLayoutWindow::UpdateScrollbars(void)
149 {
150 CoordType
151 max_x, max_y, lineHeight;
152
153 m_llist.GetSize(&max_x, &max_y, &lineHeight);
154 SetScrollbars(10, lineHeight, max_x/10+1, max_y/lineHeight+1);
155 EnableScrolling(true,true);
156 }
157
158 void
159 wxLayoutWindow::Print(void)
160 {
161 VAR(wxThePrintSetupData);
162
163 wxPostScriptDC dc("layout.ps",true,this);
164 if (dc.Ok() && dc.StartDoc((char *)_("Printing message...")))
165 {
166 //dc.SetUserScale(1.0, 1.0);
167 m_llist.Draw(dc);
168 dc.EndDoc();
169 }
170 }