]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
added printing
[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, -1, wxDefaultPosition, wxDefaultSize,
25 wxHSCROLL | wxVSCROLL | wxBORDER)
26
27 {
28 m_ScrollbarsSet = false;
29 m_EventId = -1;
30 }
31
32 #ifdef __WXMSW__
33 long
34 wxLayoutWindow::MSWGetDlgCode()
35 {
36 // if we don't return this, we won't get OnChar() events
37 return DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_WANTMESSAGE;
38 }
39 #endif //MSW
40
41
42 void
43 wxLayoutWindow::OnMouse(wxMouseEvent& event)
44 {
45 SetFocus();
46
47 if(m_EventId == -1) // nothing to do
48 return;
49
50 m_FindPos.x = event.GetX();
51 m_FindPos.y = event.GetY();
52 m_FoundObject = NULL;
53
54 #ifdef WXLAYOUT_DEBUG
55 //doesn't work, undefined functions
56 //wxLogTrace("OnMouse: (%d, %d)", m_FindPos.x, m_FindPos.y);
57 #endif
58 Refresh();
59 if(m_FoundObject)
60 {
61 if(m_EventId != -1)
62 {
63 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, m_EventId);
64 commandEvent.SetEventObject( this );
65 commandEvent.SetClientData((char *)m_FoundObject);
66 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
67 GetEventHandler()->ProcessEvent(commandEvent);
68 }
69 }
70 }
71
72 /*
73 * some simple keyboard handling
74 */
75 void
76 wxLayoutWindow::OnChar(wxKeyEvent& event)
77 {
78 if(! m_llist.IsEditable()) // do nothing
79 {
80 event.Skip();
81 return;
82 }
83
84 long keyCode = event.KeyCode();
85 wxPoint p;
86 CoordType help;
87
88 switch(event.KeyCode())
89 {
90 case WXK_RIGHT:
91 m_llist.MoveCursor(1);
92 break;
93 case WXK_LEFT:
94 m_llist.MoveCursor(-1);
95 break;
96 case WXK_UP:
97 m_llist.MoveCursor(0,-1);
98 break;
99 case WXK_DOWN:
100 m_llist.MoveCursor(0,1);
101 break;
102 case WXK_PRIOR:
103 m_llist.MoveCursor(0,-20);
104 break;
105 case WXK_NEXT:
106 m_llist.MoveCursor(0,20);
107 break;
108 case WXK_HOME:
109 p = m_llist.GetCursor();
110 p.x = 0;
111 m_llist.SetCursor(p);
112 break;
113 case WXK_END:
114 p = m_llist.GetCursor();
115 p.x = m_llist.GetLineLength(m_llist.FindCurrentObject(NULL));
116 m_llist.SetCursor(p);
117 break;
118 case WXK_DELETE :
119 if(event.ControlDown()) // delete to end of line
120 {
121 help = m_llist.GetLineLength(
122 m_llist.FindCurrentObject(NULL))
123 - m_llist.GetCursor().x;
124 m_llist.Delete(help ? help : 1);
125 }
126 else
127 m_llist.Delete(1);
128 break;
129 case WXK_BACK: // backspace
130 if(m_llist.MoveCursor(-1))
131 m_llist.Delete(1);
132 break;
133 case WXK_RETURN:
134 m_llist.LineBreak();
135 break;
136 #ifdef WXLAYOUT_DEBUG
137 case WXK_F1:
138 m_llist.Debug();
139 break;
140 #endif
141 default:
142 if(keyCode < 256 && keyCode >= 32)
143 {
144 String tmp;
145 tmp += keyCode;
146 m_llist.Insert(tmp);
147 }
148 break;
149 }
150 Refresh();
151 UpdateScrollbars();
152 }
153
154 void
155 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)w) // or: OnDraw(wxDC& dc)
156 {
157 wxPaintDC dc( this ); // only when used as OnPaint for OnDraw we
158 PrepareDC( dc ); // can skip the first two lines
159
160 if(m_EventId != -1) // look for keyclicks
161 m_FoundObject = m_llist.Draw(dc,true,m_FindPos);
162 else
163 m_llist.Draw(dc);
164 if(! m_ScrollbarsSet)
165 {
166 m_ScrollbarsSet = true; // avoid recursion
167 UpdateScrollbars();
168 }
169 }
170
171 void
172 wxLayoutWindow::UpdateScrollbars(void)
173 {
174 CoordType
175 max_x, max_y, lineHeight;
176
177 m_llist.GetSize(&max_x, &max_y, &lineHeight);
178 SetScrollbars(10, lineHeight, max_x/10+1, max_y/lineHeight+1);
179 EnableScrolling(true,true);
180 }
181
182 void
183 wxLayoutWindow::Print(void)
184 {
185 VAR(wxThePrintSetupData);
186
187 wxPostScriptDC dc("layout.ps",true,this);
188 if (dc.Ok() && dc.StartDoc((char *)_("Printing message...")))
189 {
190 //dc.SetUserScale(1.0, 1.0);
191 m_llist.Draw(dc);
192 dc.EndDoc();
193 }
194 }