4 * Author: Karsten Ballüder
6 * Copyright: (C) 1998, Karsten Ballüder <Ballueder@usa.net>
10 #include "wx/wxprec.h"
16 #include "wx/textfile.h"
25 #include "wx/wfstream.h"
26 #include "wx/txtstrm.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
43 ID_ADD_SAMPLE
= 1, ID_CLEAR
, ID_PRINT
,
44 ID_PRINT_SETUP
, ID_PAGE_SETUP
, ID_PREVIEW
, ID_PRINT_PS
,
45 ID_PRINT_SETUP_PS
, ID_PAGE_SETUP_PS
,ID_PREVIEW_PS
,
46 ID_WRAP
, ID_NOWRAP
, ID_PASTE
, ID_COPY
, ID_CUT
,
47 ID_COPY_PRIMARY
, ID_PASTE_PRIMARY
,
49 ID_WXLAYOUT_DEBUG
, ID_QUIT
, ID_CLICK
, ID_HTML
, ID_TEXT
,
50 ID_TEST
, ID_LINEBREAKS_TEST
, ID_LONG_TEST
, ID_URL_TEST
54 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
56 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
57 EVT_MENU(ID_PRINT
, MyFrame::OnPrint
)
58 EVT_MENU(ID_PREVIEW
, MyFrame::OnPrintPreview
)
59 EVT_MENU(ID_PRINT_SETUP
, MyFrame::OnPrintSetup
)
60 EVT_MENU(ID_PAGE_SETUP
, MyFrame::OnPageSetup
)
61 EVT_MENU(ID_PRINT_PS
, MyFrame::OnPrintPS
)
62 EVT_MENU(ID_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
63 EVT_MENU(ID_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
64 EVT_MENU(ID_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
65 EVT_MENU (wxID_ANY
, MyFrame::OnCommand
)
66 EVT_COMMAND (wxID_ANY
,wxID_ANY
, MyFrame::OnCommand
)
67 EVT_CHAR ( wxLayoutWindow::OnChar
)
72 wxFrame( (wxFrame
*) NULL
, wxID_ANY
, _T("wxLayout"),
73 wxDefaultPosition
, wxDefaultSize
)
77 SetStatusText( _T("wxLayout by Karsten Ballueder.") );
78 #endif // wxUSE_STATUSBAR
80 wxMenuBar
*menu_bar
= new wxMenuBar();
82 wxMenu
*file_menu
= new wxMenu
;
83 file_menu
->Append(ID_PRINT
, _T("&Print..."), _T("Print"));
84 file_menu
->Append(ID_PRINT_SETUP
, _T("Print &Setup..."),_T("Setup printer properties"));
85 file_menu
->Append(ID_PAGE_SETUP
, _T("Page Set&up..."), _T("Page setup"));
86 file_menu
->Append(ID_PREVIEW
, _T("Print Pre&view"), _T("Preview"));
88 file_menu
->AppendSeparator();
89 file_menu
->Append(ID_PRINT_PS
, _T("Print PostScript..."), _T("Print (PostScript)"));
90 file_menu
->Append(ID_PRINT_SETUP_PS
, _T("Print Setup PostScript..."), _T("Setup printer properties (PostScript)"));
91 file_menu
->Append(ID_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
92 file_menu
->Append(ID_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
94 file_menu
->AppendSeparator();
95 file_menu
->Append( ID_TEXT
, _T("Export &Text"));
96 file_menu
->Append( ID_HTML
, _T("Export &HTML"));
97 file_menu
->Append( ID_QUIT
, _T("E&xit"));
98 menu_bar
->Append(file_menu
, _T("&File"));
100 wxMenu
*edit_menu
= new wxMenu
;
101 edit_menu
->Append( ID_CLEAR
, _T("C&lear"));
102 edit_menu
->Append( ID_ADD_SAMPLE
, _T("&Example"));
103 edit_menu
->Append( ID_LONG_TEST
, _T("Add &many lines"));
104 edit_menu
->AppendSeparator();
105 edit_menu
->Append( ID_LINEBREAKS_TEST
, _T("Add &several lines"));
106 edit_menu
->Append( ID_URL_TEST
, _T("Insert an &URL"));
107 edit_menu
->AppendSeparator();
108 edit_menu
->Append(ID_WRAP
, _T("&Wrap mode"), _T("Activate wrapping at pixel 200."));
109 edit_menu
->Append(ID_NOWRAP
, _T("&No-wrap mode"), _T("Deactivate wrapping."));
110 edit_menu
->AppendSeparator();
111 edit_menu
->Append(ID_COPY
, _T("&Copy"), _T("Copy text to clipboard."));
112 edit_menu
->Append(ID_CUT
, _T("Cu&t"), _T("Cut text to clipboard."));
113 edit_menu
->Append(ID_PASTE
,_T("&Paste"), _T("Paste text from clipboard."));
115 edit_menu
->Append(ID_COPY_PRIMARY
, _T("C&opy primary"), _T("Copy text to primary selecton."));
116 edit_menu
->Append(ID_PASTE_PRIMARY
, _T("&Paste primary"), _T("Paste text from primary selection."));
118 edit_menu
->Append(ID_FIND
, _T("&Find"), _T("Find text."));
119 menu_bar
->Append(edit_menu
, _T("&Edit") );
122 menu_bar
->Show(true);
125 SetMenuBar( menu_bar
);
127 m_lwin
= new wxLayoutWindow(this);
129 m_lwin
->SetStatusBar(GetStatusBar(), 0, 1);
130 #endif // wxUSE_STATUSBAR
131 m_lwin
->SetMouseTracking(true);
132 m_lwin
->SetEditable(true);
133 m_lwin
->SetWrapMargin(40);
136 // JACS: under MSW, the window doesn't show the caret initially,
137 // and the following line I added doesn't help either.
138 // going to another window and then back again fixes it.
139 // m_lwin->OnSetFocus(wxFocusEvent());
144 // create and set the background bitmap (this will result in a lattice)
145 static const int sizeBmp
= 10;
146 wxBitmap
*bitmap
= new wxBitmap(sizeBmp
, sizeBmp
);
148 dcMem
.SelectObject( *bitmap
);
149 dcMem
.SetBackground( *wxWHITE_BRUSH
);
152 dcMem
.SetPen( *wxGREEN_PEN
);
153 dcMem
.DrawLine(sizeBmp
/2, 0, sizeBmp
/2, sizeBmp
);
154 dcMem
.DrawLine(0, sizeBmp
/2, sizeBmp
, sizeBmp
/2);
156 dcMem
.SelectObject( wxNullBitmap
);
158 m_lwin
->SetBackgroundBitmap(bitmap
);
162 void MyFrame::AddSampleText(wxLayoutList
*llist
)
164 llist
->Clear(wxSWISS
,16,wxNORMAL
,wxNORMAL
, false);
165 llist
->SetFont(-1,-1,-1,-1,-1,_T("blue"));
166 llist
->Insert(_T("blue"));
169 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"));
170 llist
->Insert(_T("The quick brown fox jumps over the lazy dog."));
173 llist
->SetFont(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false);
174 llist
->Insert(_T("--"));
177 llist
->SetFont(wxROMAN
);
178 llist
->Insert(_T("The quick brown fox jumps over the lazy dog."));
181 llist
->Insert(_T("Hello "));
182 wxBitmap
*icon
= new wxBitmap (wxIcon(Micon_xpm
));
184 llist
->Insert(new wxLayoutObjectIcon(icon
));
185 llist
->SetFontWeight(wxBOLD
);
186 llist
->Insert(_T("World! "));
187 llist
->SetFontWeight(wxNORMAL
);
188 llist
->Insert(_T("The quick brown fox jumps..."));
191 llist
->Insert(_T("over the lazy dog."));
192 llist
->SetFont(-1,-1,-1,-1,true);
193 llist
->Insert(_T("underlined"));
194 llist
->SetFont(-1,-1,-1,-1,false);
195 llist
->SetFont(wxROMAN
);
196 llist
->Insert(_T("This is "));
197 llist
->SetFont(-1,-1,-1,wxBOLD
);
198 llist
->Insert(_T("BOLD "));
199 llist
->SetFont(-1,-1,-1,wxNORMAL
);
200 llist
->Insert(_T("and "));
201 llist
->SetFont(-1,-1,wxITALIC
);
202 llist
->Insert(_T("italics "));
203 llist
->SetFont(-1,-1,wxNORMAL
);
206 llist
->Insert(_T("and "));
207 llist
->SetFont(-1,-1,wxSLANT
);
208 llist
->Insert(_T("slanted"));
209 llist
->SetFont(-1,-1,wxNORMAL
);
210 llist
->Insert(_T(" text."));
213 llist
->Insert(_T("and "));
214 llist
->SetFont(-1,-1,-1,-1,-1,_T("blue"));
215 llist
->Insert(_T("blue"));
216 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"));
217 llist
->Insert(_T(" and "));
218 llist
->SetFont(-1,-1,-1,-1,-1,_T("green"),_T("black"));
219 llist
->Insert(_T("green on black"));
220 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"),_T("white"));
221 llist
->Insert(_T(" text."));
224 llist
->SetFont(-1,-1,wxSLANT
);
225 llist
->Insert(_T("Slanted"));
226 llist
->SetFont(-1,-1,wxNORMAL
);
227 llist
->Insert(_T(" and normal text and "));
228 llist
->SetFont(-1,-1,wxSLANT
);
229 llist
->Insert(_T("slanted"));
230 llist
->SetFont(-1,-1,wxNORMAL
);
231 llist
->Insert(_T(" again."));
234 // add some more text for testing:
235 llist
->Insert(_T("And here the source for the test program:"));
238 llist
->SetFont(wxTELETYPE
,16);
239 llist
->Insert(_T("And here the source for the test program:"));
242 wxTextFile
file(_T("wxLayout.cpp"));
245 for ( wxString s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
248 llist
->Insert(line
.Format(_T("%6u: %s"),file
.GetCurrentLine()+1,s
.c_str()));
253 llist
->MoveCursorTo(wxPoint(0,0));
258 void MyFrame::Clear()
260 wxColour
colBg(0, 0, 0);
262 m_lwin
->Clear(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false, wxRED
, &colBg
);
266 void MyFrame::OnCommand( wxCommandEvent
&event
)
268 switch (event
.GetId())
276 wxLayoutPrintout
printout(m_lwin
->GetLayoutList(),_("M: Printout"));
277 if (! printer
.Print(this, &printout
, true))
279 // Had to remove the split up strings that used to be below, and
280 // put them into one long strong. Otherwise MSVC would give an
281 // error "C2308: concatenating mismatched wide strings" when
282 // building a Unicode version.
285 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
294 m_lwin
->SetWrapMargin(event
.GetId() == ID_NOWRAP
? 0 : 40);
297 AddSampleText(m_lwin
->GetLayoutList());
303 wxLogError( _T("Received click event.") );
307 m_lwin
->Refresh(false);
310 case ID_PASTE_PRIMARY
:
311 // text only from primary:
312 m_lwin
->Paste(false, true);
313 m_lwin
->Refresh(false);
315 case ID_COPY_PRIMARY
:
316 // copy text-only to primary selection:
317 m_lwin
->Copy(false, false, true);
318 m_lwin
->Refresh(false);
322 m_lwin
->Copy(true, true, false);
323 m_lwin
->Refresh(false);
327 m_lwin
->Refresh(false);
331 m_lwin
->Find("void");
332 m_lwin
->Refresh(false);
339 _T("Save As HTML..."),
342 _T("HTML file (*.html)|*.html|Text file (*.txt)|*.txt|Any file (*)|*"),
343 wxSAVE
|wxOVERWRITE_PROMPT
345 if (HTML_dialog
.ShowModal() == wxID_OK
)
347 wxFFileOutputStream
output( HTML_dialog
.GetPath() );
348 wxTextOutputStream
textout( output
);
350 wxLayoutExportObject
*export0
;
352 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
353 while((export0
= wxLayoutExport( &status
, WXLO_EXPORT_AS_HTML
)) != NULL
)
355 if(export0
->type
== WXLO_EXPORT_HTML
)
356 object
= *(export0
->content
.text
);
358 ; // ignore "<!--UNKNOWN OBJECT>";
370 _T("Save As TXT..."),
373 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
374 wxSAVE
|wxOVERWRITE_PROMPT
376 if (TEXT_dialog
.ShowModal() == wxID_OK
)
378 wxFFileOutputStream
output( TEXT_dialog
.GetPath() );
379 wxTextOutputStream
textout( output
);
381 wxLayoutExportObject
*export0
;
383 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
384 while((export0
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
386 if(export0
->type
== WXLO_EXPORT_TEXT
)
387 object
= *(export0
->content
.text
);
389 object
= _T("<!--UNKNOWN OBJECT>");
400 wxLayoutList
*llist
= m_lwin
->GetLayoutList();
401 for(int i
= 1; i
< 300; i
++)
403 line
.Printf(wxT("This is line number %d."), i
);
408 llist
->MoveCursorTo(wxPoint(0,0));
414 case ID_LINEBREAKS_TEST
:
417 m_lwin
->GetLayoutList(),
418 wxT("This is a text\nwith embedded line\nbreaks.\n")
426 // VZ: this doesn't work, of course, but I think it should -
427 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
428 // (instead of doing it manually like M does now)
429 m_lwin
->GetLayoutList()->Insert(_T("http://www.wxwidgets.org/"));
434 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
437 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
439 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
442 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(), _T("Printout from wxLayout"));
443 if (! printer
.Print(this, &printout
, true))
445 _T("There was a problem printing.\nPerhaps your current printer is not set correctly?"),
446 _T("Printing"), wxOK
);
449 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
451 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
454 wxPostScriptPrinter printer
;
455 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"My printout");
456 printer
.Print(this, &printout
, true);
460 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
463 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
465 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
467 wxPrintData printData
;
469 // Pass two printout objects: for preview, and possible printing.
470 wxPrintPreview
*preview
= new wxPrintPreview(new
472 m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
476 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
480 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
481 frame
->Centre(wxBOTH
);
486 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
488 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
490 wxPrintData printData
;
492 // Pass two printout objects: for preview, and possible printing.
493 wxPrintPreview
*preview
= new wxPrintPreview(new wxLayoutPrintout( m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
494 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
495 frame
->Centre(wxBOTH
);
500 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
503 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
505 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
507 wxPrintDialog
printerDialog(this, & m_PrintData
);
508 printerDialog
.ShowModal();
511 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
514 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
516 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
518 wxPageSetupDialogData data
;
520 #if defined(__WXMSW__) || defined(__WXMAC__)
521 wxPageSetupDialog
pageSetupDialog(this, & data
);
523 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
525 pageSetupDialog
.ShowModal();
527 data
= pageSetupDialog
.GetPageSetupDialogData();
530 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
532 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
536 #if defined(__WXMSW__) || defined(__WXMAC__)
537 wxPrintDialog
printerDialog(this, & data
);
539 wxGenericPrintDialog
printerDialog(this, & data
);
541 printerDialog
.ShowModal();
544 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
546 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
548 wxPageSetupData data
;
549 #if defined(__WXMSW__) || defined(__WXMAC__)
550 wxPageSetupDialog
pageSetupDialog(this, & data
);
552 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
554 pageSetupDialog
.ShowModal();
558 //-----------------------------------------------------------------------------
560 //-----------------------------------------------------------------------------
569 wxFrame
*frame
= new MyFrame();
570 wxInitAllImageHandlers();
572 // wxSetAFMPath("/usr/local/src/wxWidgets/misc/afm/");