]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
First part of fixes to make this work under MSW.
[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( 2 );
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->SetStatusBar(GetStatusBar(), 0, 1);
110 m_lwin->SetMouseTracking(true);
111 m_lwin->SetEditable(true);
112 m_lwin->SetWrapMargin(40);
113 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
114 m_lwin->SetFocus();
115 };
116
117 void
118 MyFrame::AddSampleText(wxLayoutList *llist)
119 {
120 llist->Clear(wxSWISS,16,wxNORMAL,wxNORMAL, false);
121 llist->SetFont(-1,-1,-1,-1,-1,"blue");
122 llist->Insert("blue");
123 llist->SetFont(-1,-1,-1,-1,-1,"black");
124 llist->Insert("The quick brown fox jumps over the lazy dog.");
125 llist->LineBreak();
126
127 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
128 llist->Insert("--");
129 llist->LineBreak();
130
131 llist->SetFont(wxROMAN);
132 llist->Insert("The quick brown fox jumps over the lazy dog.");
133 llist->LineBreak();
134 llist->Insert("Hello ");
135 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
136 llist->SetFontWeight(wxBOLD);
137 llist->Insert("World! ");
138 llist->SetFontWeight(wxNORMAL);
139 llist->Insert("The quick brown fox jumps...");
140 llist->LineBreak();
141 llist->Insert("over the lazy dog.");
142 llist->SetFont(-1,-1,-1,-1,true);
143 llist->Insert("underlined");
144 llist->SetFont(-1,-1,-1,-1,false);
145 llist->SetFont(wxROMAN);
146 llist->Insert("This is ");
147 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
148 llist->Insert("and ");
149 llist->SetFont(-1,-1,wxITALIC);
150 llist->Insert("italics ");
151 llist->SetFont(-1,-1,wxNORMAL);
152 llist->LineBreak();
153
154 llist->Insert("and ");
155 llist->SetFont(-1,-1,wxSLANT);
156 llist->Insert("slanted");
157 llist->SetFont(-1,-1,wxNORMAL);
158 llist->Insert(" text.");
159 llist->LineBreak();
160 llist->Insert("and ");
161 llist->SetFont(-1,-1,-1,-1,-1,"blue");
162 llist->Insert("blue");
163 llist->SetFont(-1,-1,-1,-1,-1,"black");
164 llist->Insert(" and ");
165 llist->SetFont(-1,-1,-1,-1,-1,"green","black");
166 llist->Insert("green on black");
167 llist->SetFont(-1,-1,-1,-1,-1,"black","white");
168 llist->Insert(" text.");
169 llist->LineBreak();
170
171 llist->SetFont(-1,-1,wxSLANT);
172 llist->Insert("Slanted");
173 llist->SetFont(-1,-1,wxNORMAL);
174 llist->Insert(" and normal text and ");
175 llist->SetFont(-1,-1,wxSLANT);
176 llist->Insert("slanted");
177 llist->SetFont(-1,-1,wxNORMAL);
178 llist->Insert(" again.");
179 llist->LineBreak();
180
181 // add some more text for testing:
182 llist->Insert("And here the source for the test program:");
183 llist->LineBreak();
184 llist->SetFont(wxTELETYPE,16);
185 char buffer[1024];
186 FILE *in = fopen("wxLayout.cpp","r");
187 if(in)
188 {
189 for(;;)
190 {
191 fgets(buffer,1024,in);
192 if(feof(in))
193 break;
194 llist->Insert(buffer);
195 llist->LineBreak();
196 }
197 }
198 llist->MoveCursorTo(wxPoint(0,0));
199 m_lwin->SetDirty();
200 m_lwin->Refresh();
201 }
202
203 void
204 MyFrame::Clear(void)
205 {
206 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
207 }
208
209
210 void MyFrame::OnCommand( wxCommandEvent &event )
211 {
212 switch (event.GetId())
213 {
214 case ID_QUIT:
215 Close( TRUE );
216 break;
217 case ID_PRINT:
218 {
219 wxPrinter printer;
220 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
221 if (! printer.Print(this, &printout, TRUE))
222 wxMessageBox(
223 _("There was a problem with printing the message:\n"
224 "perhaps your current printer is not set up correctly?"),
225 _("Printing"), wxOK);
226 }
227 break;
228 case ID_NOWRAP:
229 case ID_WRAP:
230 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
231 break;
232 case ID_ADD_SAMPLE:
233 AddSampleText(m_lwin->GetLayoutList());
234 break;
235 case ID_CLEAR:
236 Clear();
237 break;
238 case ID_CLICK:
239 cerr << "Received click event." << endl;
240 break;
241 case ID_PASTE:
242 m_lwin->Paste();
243 m_lwin->Refresh(FALSE);
244 break;
245 case ID_COPY:
246 m_lwin->Copy();
247 m_lwin->Refresh(FALSE);
248 break;
249 case ID_CUT:
250 m_lwin->Cut();
251 m_lwin->Refresh(FALSE);
252 break;
253 case ID_FIND:
254 m_lwin->Find("void");
255 m_lwin->Refresh(FALSE);
256 break;
257 case ID_HTML:
258 {
259 wxLayoutExportObject *export;
260 wxLayoutExportStatus status(m_lwin->GetLayoutList());
261
262 while((export = wxLayoutExport( &status,
263 WXLO_EXPORT_AS_HTML)) != NULL)
264 {
265 if(export->type == WXLO_EXPORT_HTML)
266 cout << *(export->content.text);
267 else
268 cout << "<!--UNKNOWN OBJECT>";
269 delete export;
270 }
271 }
272 break;
273 case ID_TEXT:
274 {
275 wxLayoutExportObject *export;
276 wxLayoutExportStatus status(m_lwin->GetLayoutList());
277
278 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
279 {
280 if(export->type == WXLO_EXPORT_TEXT)
281 cout << *(export->content.text);
282 else
283 cout << "<!--UNKNOWN OBJECT>";
284 delete export;
285 }
286 }
287 break;
288 case ID_LONG_TEST:
289 {
290 wxString line;
291 wxLayoutList *llist = m_lwin->GetLayoutList();
292 for(int i = 1; i < 5000; i++)
293 {
294 line.Printf("This is line number %d.", i);
295 llist->Insert(line);
296 llist->LineBreak();
297 }
298 llist->MoveCursorTo(wxPoint(0,0));
299 m_lwin->SetDirty();
300 m_lwin->Refresh();
301 break;
302 }
303 }
304 };
305
306 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
307 {
308 #ifdef __WXMSW__
309 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
310 #else
311 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
312 #endif
313 wxPrinter printer;
314 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
315 if (! printer.Print(this, &printout, TRUE))
316 wxMessageBox(
317 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
318 "Printing", wxOK);
319 }
320
321 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
322 {
323 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
324
325 #ifdef OS_UNIX
326 wxPostScriptPrinter printer;
327 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
328 printer.Print(this, &printout, TRUE);
329 #endif
330 }
331
332 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
333 {
334 #ifdef __WXMSW__
335 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
336 #else
337 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
338 #endif
339 wxPrintData printData;
340
341 // Pass two printout objects: for preview, and possible printing.
342 wxPrintPreview *preview = new wxPrintPreview(new
343 wxLayoutPrintout(
344 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
345 if (!preview->Ok())
346 {
347 delete preview;
348 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
349 return;
350 }
351
352 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
353 frame->Centre(wxBOTH);
354 frame->Initialize();
355 frame->Show(TRUE);
356 }
357
358 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
359 {
360 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
361
362 wxPrintData printData;
363
364 // Pass two printout objects: for preview, and possible printing.
365 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
366 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
367 frame->Centre(wxBOTH);
368 frame->Initialize();
369 frame->Show(TRUE);
370 }
371
372 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
373 {
374 #ifdef OS_WIN
375 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
376 #else
377 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
378 #endif
379 wxPrintDialog printerDialog(this, & m_PrintData);
380 printerDialog.ShowModal();
381 }
382
383 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
384 {
385 #ifdef __WXMSW__
386 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
387 #else
388 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
389 #endif
390 wxPageSetupData data;
391
392 #ifdef __WXMSW__
393 wxPageSetupDialog pageSetupDialog(this, & data);
394 #else
395 wxGenericPageSetupDialog pageSetupDialog(this, & data);
396 #endif
397 pageSetupDialog.ShowModal();
398
399 data = pageSetupDialog.GetPageSetupData();
400 }
401
402 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
403 {
404 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
405
406 wxPrintData data;
407
408 #ifdef __WXMSW__
409 wxPrintDialog printerDialog(this, & data);
410 #else
411 wxGenericPrintDialog printerDialog(this, & data);
412 #endif
413 printerDialog.ShowModal();
414 }
415
416 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
417 {
418 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
419
420 wxPageSetupData data;
421 #ifdef __WXMSW__
422 wxPageSetupDialog pageSetupDialog(this, & data);
423 #else
424 wxGenericPageSetupDialog pageSetupDialog(this, & data);
425 #endif
426 pageSetupDialog.ShowModal();
427 }
428
429
430 //-----------------------------------------------------------------------------
431 // MyApp
432 //-----------------------------------------------------------------------------
433
434 MyApp::MyApp(void) :
435 wxApp( )
436 {
437 };
438
439 bool MyApp::OnInit(void)
440 {
441 wxFrame *frame = new MyFrame();
442 frame->Show( TRUE );
443 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
444 return TRUE;
445 };
446
447
448
449
450