]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
Minor DC correction.
[wxWidgets.git] / user / wxLayout / wxLayout.cpp
1 /*
2 * Program: wxLayout
3 *
4 * Author: Karsten Ballüder
5 *
6 * Copyright: (C) 1998, Karsten Ballüder <Ballueder@usa.net>
7 *
8 */
9
10 #ifdef __GNUG__
11 #pragma implementation "wxLayout.h"
12 #endif
13
14 #include "wx/wxprec.h"
15 #ifdef __BORLANDC__
16 # pragma hdrstop
17 #endif
18
19 #include "wxLayout.h"
20 #include "wx/textfile.h"
21
22
23 #include "Micon.xpm"
24
25
26 //-----------------------------------------------------------------------------
27 // main program
28 //-----------------------------------------------------------------------------
29
30 IMPLEMENT_APP(MyApp)
31
32 //-----------------------------------------------------------------------------
33 // MyFrame
34 //-----------------------------------------------------------------------------
35
36 enum ids{ ID_ADD_SAMPLE = 1, ID_CLEAR, ID_PRINT,
37 ID_PRINT_SETUP, ID_PAGE_SETUP, ID_PREVIEW, ID_PRINT_PS,
38 ID_PRINT_SETUP_PS, ID_PAGE_SETUP_PS,ID_PREVIEW_PS,
39 ID_WRAP, ID_NOWRAP, ID_PASTE, ID_COPY, ID_CUT, ID_FIND,
40 ID_WXLAYOUT_DEBUG, ID_QUIT, ID_CLICK, ID_HTML, ID_TEXT,
41 ID_TEST, ID_LONG_TEST };
42
43
44 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
45
46 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
47 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
48 EVT_MENU(ID_PREVIEW, MyFrame::OnPrintPreview)
49 EVT_MENU(ID_PRINT_SETUP, MyFrame::OnPrintSetup)
50 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
51 EVT_MENU(ID_PRINT_PS, MyFrame::OnPrintPS)
52 EVT_MENU(ID_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
53 EVT_MENU(ID_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
54 EVT_MENU(ID_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
55 EVT_MENU (-1, MyFrame::OnCommand)
56 EVT_COMMAND (-1,-1, MyFrame::OnCommand)
57 EVT_CHAR ( wxLayoutWindow::OnChar )
58 END_EVENT_TABLE()
59
60
61 MyFrame::MyFrame(void) :
62 wxFrame( (wxFrame *) NULL, -1, (char *) "wxLayout", wxPoint(20,20), wxSize(600,360) )
63 {
64 CreateStatusBar( 1 );
65
66 SetStatusText( "wxLayout by Karsten Ballüder." );
67
68 wxMenuBar *menu_bar = new wxMenuBar();
69
70 wxMenu *file_menu = new wxMenu;
71 file_menu->Append(ID_PRINT, "&Print...", "Print");
72 file_menu->Append(ID_PRINT_SETUP, "Print &Setup...","Setup printer properties");
73 file_menu->Append(ID_PAGE_SETUP, "Page Set&up...", "Page setup");
74 file_menu->Append(ID_PREVIEW, "Print Pre&view", "Preview");
75 #ifdef __WXMSW__
76 file_menu->AppendSeparator();
77 file_menu->Append(ID_PRINT_PS, "Print PostScript...", "Print (PostScript)");
78 file_menu->Append(ID_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
79 file_menu->Append(ID_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
80 file_menu->Append(ID_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
81 #endif
82 file_menu->AppendSeparator();
83 file_menu->Append( ID_TEXT, "Export Text");
84 file_menu->Append( ID_HTML, "Export HTML");
85 file_menu->Append( ID_QUIT, "Exit");
86 menu_bar->Append(file_menu, "File" );
87
88 wxMenu *edit_menu = new wxMenu;
89 edit_menu->Append( ID_CLEAR, "Clear");
90 edit_menu->Append( ID_ADD_SAMPLE, "Example");
91 edit_menu->Append( ID_LONG_TEST, "Add many lines");
92 edit_menu->AppendSeparator();
93 edit_menu->Append(ID_WRAP, "Wrap mode", "Activate wrapping at pixel 200.");
94 edit_menu->Append(ID_NOWRAP, "No-wrap mode", "Deactivate wrapping.");
95 edit_menu->AppendSeparator();
96 edit_menu->Append(ID_COPY, "Copy", "Copy text to clipboard.");
97 edit_menu->Append(ID_CUT, "Cut", "Cut text to clipboard.");
98 edit_menu->Append(ID_PASTE,"Paste", "Paste text from clipboard.");
99 edit_menu->Append(ID_FIND, "Find", "Find text.");
100 menu_bar->Append(edit_menu, "Edit" );
101
102 #ifndef __WXMSW__
103 menu_bar->Show( TRUE );
104 #endif // MSW
105
106 SetMenuBar( menu_bar );
107
108 m_lwin = new wxLayoutWindow(this);
109 m_lwin->SetMouseTracking(true);
110 m_lwin->SetEditable(true);
111 m_lwin->SetWrapMargin(40);
112 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
113 m_lwin->SetFocus();
114 };
115
116 void
117 MyFrame::AddSampleText(wxLayoutList *llist)
118 {
119 llist->Clear(wxSWISS,16,wxNORMAL,wxNORMAL, false);
120 llist->SetFont(-1,-1,-1,-1,-1,"blue");
121 llist->Insert("blue");
122 llist->SetFont(-1,-1,-1,-1,-1,"black");
123 llist->Insert("The quick brown fox jumps over the lazy dog.");
124 llist->LineBreak();
125
126 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
127 llist->Insert("--");
128 llist->LineBreak();
129
130 llist->SetFont(wxROMAN);
131 llist->Insert("The quick brown fox jumps over the lazy dog.");
132 llist->LineBreak();
133 llist->Insert("Hello ");
134 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
135 llist->SetFontWeight(wxBOLD);
136 llist->Insert("World! ");
137 llist->SetFontWeight(wxNORMAL);
138 llist->Insert("The quick brown fox jumps...");
139 llist->LineBreak();
140 llist->Insert("over the lazy dog.");
141 llist->SetFont(-1,-1,-1,-1,true);
142 llist->Insert("underlined");
143 llist->SetFont(-1,-1,-1,-1,false);
144 llist->SetFont(wxROMAN);
145 llist->Insert("This is ");
146 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
147 llist->Insert("and ");
148 llist->SetFont(-1,-1,wxITALIC);
149 llist->Insert("italics ");
150 llist->SetFont(-1,-1,wxNORMAL);
151 llist->LineBreak();
152
153 llist->Insert("and ");
154 llist->SetFont(-1,-1,wxSLANT);
155 llist->Insert("slanted");
156 llist->SetFont(-1,-1,wxNORMAL);
157 llist->Insert(" text.");
158 llist->LineBreak();
159 llist->Insert("and ");
160 llist->SetFont(-1,-1,-1,-1,-1,"blue");
161 llist->Insert("blue");
162 llist->SetFont(-1,-1,-1,-1,-1,"black");
163 llist->Insert(" and ");
164 llist->SetFont(-1,-1,-1,-1,-1,"green","black");
165 llist->Insert("green on black");
166 llist->SetFont(-1,-1,-1,-1,-1,"black","white");
167 llist->Insert(" text.");
168 llist->LineBreak();
169
170 llist->SetFont(-1,-1,wxSLANT);
171 llist->Insert("Slanted");
172 llist->SetFont(-1,-1,wxNORMAL);
173 llist->Insert(" and normal text and ");
174 llist->SetFont(-1,-1,wxSLANT);
175 llist->Insert("slanted");
176 llist->SetFont(-1,-1,wxNORMAL);
177 llist->Insert(" again.");
178 llist->LineBreak();
179
180 // add some more text for testing:
181 llist->Insert("And here the source for the test program:");
182 llist->LineBreak();
183 llist->SetFont(wxTELETYPE,16);
184 char buffer[1024];
185 FILE *in = fopen("wxLayout.cpp","r");
186 if(in)
187 {
188 for(;;)
189 {
190 fgets(buffer,1024,in);
191 if(feof(in))
192 break;
193 llist->Insert(buffer);
194 llist->LineBreak();
195 }
196 }
197 llist->MoveCursorTo(wxPoint(0,0));
198 m_lwin->SetDirty();
199 m_lwin->Refresh();
200 }
201
202 void
203 MyFrame::Clear(void)
204 {
205 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
206 }
207
208
209 void MyFrame::OnCommand( wxCommandEvent &event )
210 {
211 switch (event.GetId())
212 {
213 case ID_QUIT:
214 Close( TRUE );
215 break;
216 case ID_PRINT:
217 {
218 wxPrinter printer;
219 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
220 if (! printer.Print(this, &printout, TRUE))
221 wxMessageBox(
222 _("There was a problem with printing the message:\n"
223 "perhaps your current printer is not set up correctly?"),
224 _("Printing"), wxOK);
225 }
226 break;
227 case ID_NOWRAP:
228 case ID_WRAP:
229 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
230 break;
231 case ID_ADD_SAMPLE:
232 AddSampleText(m_lwin->GetLayoutList());
233 break;
234 case ID_CLEAR:
235 Clear();
236 break;
237 case ID_CLICK:
238 cerr << "Received click event." << endl;
239 break;
240 case ID_PASTE:
241 m_lwin->Paste();
242 m_lwin->Refresh(FALSE);
243 break;
244 case ID_COPY:
245 m_lwin->Copy();
246 m_lwin->Refresh(FALSE);
247 break;
248 case ID_CUT:
249 m_lwin->Cut();
250 m_lwin->Refresh(FALSE);
251 break;
252 case ID_FIND:
253 m_lwin->Find("void");
254 m_lwin->Refresh(FALSE);
255 break;
256 case ID_HTML:
257 {
258 wxLayoutExportObject *export;
259 wxLayoutExportStatus status(m_lwin->GetLayoutList());
260
261 while((export = wxLayoutExport( &status,
262 WXLO_EXPORT_AS_HTML)) != NULL)
263 {
264 if(export->type == WXLO_EXPORT_HTML)
265 cout << *(export->content.text);
266 else
267 cout << "<!--UNKNOWN OBJECT>";
268 delete export;
269 }
270 }
271 break;
272 case ID_TEXT:
273 {
274 wxLayoutExportObject *export;
275 wxLayoutExportStatus status(m_lwin->GetLayoutList());
276
277 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
278 {
279 if(export->type == WXLO_EXPORT_TEXT)
280 cout << *(export->content.text);
281 else
282 cout << "<!--UNKNOWN OBJECT>";
283 delete export;
284 }
285 }
286 break;
287 case ID_LONG_TEST:
288 {
289 wxString line;
290 wxLayoutList *llist = m_lwin->GetLayoutList();
291 for(int i = 1; i < 5000; i++)
292 {
293 line.Printf("This is line number %d.", i);
294 llist->Insert(line);
295 llist->LineBreak();
296 }
297 llist->MoveCursorTo(wxPoint(0,0));
298 m_lwin->SetDirty();
299 m_lwin->Refresh();
300 break;
301 }
302 }
303 };
304
305 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
306 {
307 #ifdef __WXMSW__
308 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
309 #else
310 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
311 #endif
312 wxPrinter printer;
313 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
314 if (! printer.Print(this, &printout, TRUE))
315 wxMessageBox(
316 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
317 "Printing", wxOK);
318 }
319
320 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
321 {
322 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
323
324 #ifdef OS_UNIX
325 wxPostScriptPrinter printer;
326 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
327 printer.Print(this, &printout, TRUE);
328 #endif
329 }
330
331 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
332 {
333 #ifdef __WXMSW__
334 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
335 #else
336 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
337 #endif
338 wxPrintData printData;
339
340 // Pass two printout objects: for preview, and possible printing.
341 wxPrintPreview *preview = new wxPrintPreview(new
342 wxLayoutPrintout(
343 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
344 if (!preview->Ok())
345 {
346 delete preview;
347 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
348 return;
349 }
350
351 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
352 frame->Centre(wxBOTH);
353 frame->Initialize();
354 frame->Show(TRUE);
355 }
356
357 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
358 {
359 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
360
361 wxPrintData printData;
362
363 // Pass two printout objects: for preview, and possible printing.
364 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
365 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
366 frame->Centre(wxBOTH);
367 frame->Initialize();
368 frame->Show(TRUE);
369 }
370
371 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
372 {
373 #ifdef OS_WIN
374 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
375 #else
376 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
377 #endif
378 wxPrintDialog printerDialog(this, & m_PrintData);
379 printerDialog.ShowModal();
380 }
381
382 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
383 {
384 #ifdef __WXMSW__
385 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
386 #else
387 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
388 #endif
389 wxPageSetupData data;
390
391 #ifdef __WXMSW__
392 wxPageSetupDialog pageSetupDialog(this, & data);
393 #else
394 wxGenericPageSetupDialog pageSetupDialog(this, & data);
395 #endif
396 pageSetupDialog.ShowModal();
397
398 data = pageSetupDialog.GetPageSetupData();
399 }
400
401 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
402 {
403 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
404
405 wxPrintData data;
406
407 #ifdef __WXMSW__
408 wxPrintDialog printerDialog(this, & data);
409 #else
410 wxGenericPrintDialog printerDialog(this, & data);
411 #endif
412 printerDialog.ShowModal();
413 }
414
415 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
416 {
417 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
418
419 wxPageSetupData data;
420 #ifdef __WXMSW__
421 wxPageSetupDialog pageSetupDialog(this, & data);
422 #else
423 wxGenericPageSetupDialog pageSetupDialog(this, & data);
424 #endif
425 pageSetupDialog.ShowModal();
426 }
427
428
429 //-----------------------------------------------------------------------------
430 // MyApp
431 //-----------------------------------------------------------------------------
432
433 MyApp::MyApp(void) :
434 wxApp( )
435 {
436 };
437
438 bool MyApp::OnInit(void)
439 {
440 wxFrame *frame = new MyFrame();
441 frame->Show( TRUE );
442 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
443 return TRUE;
444 };
445
446
447
448
449