4 * Author: Karsten Ballüder
6 * Copyright: (C) 1998, Karsten Ballüder <Ballueder@usa.net>
11 #pragma implementation "wxLayout.h"
14 #include "wx/wxprec.h"
20 #include "wx/textfile.h"
29 #include "wx/wfstream.h"
30 #include "wx/txtstrm.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
47 ID_ADD_SAMPLE
= 1, ID_CLEAR
, ID_PRINT
,
48 ID_PRINT_SETUP
, ID_PAGE_SETUP
, ID_PREVIEW
, ID_PRINT_PS
,
49 ID_PRINT_SETUP_PS
, ID_PAGE_SETUP_PS
,ID_PREVIEW_PS
,
50 ID_WRAP
, ID_NOWRAP
, ID_PASTE
, ID_COPY
, ID_CUT
,
51 ID_COPY_PRIMARY
, ID_PASTE_PRIMARY
,
53 ID_WXLAYOUT_DEBUG
, ID_QUIT
, ID_CLICK
, ID_HTML
, ID_TEXT
,
54 ID_TEST
, ID_LINEBREAKS_TEST
, ID_LONG_TEST
, ID_URL_TEST
58 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
60 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
61 EVT_MENU(ID_PRINT
, MyFrame::OnPrint
)
62 EVT_MENU(ID_PREVIEW
, MyFrame::OnPrintPreview
)
63 EVT_MENU(ID_PRINT_SETUP
, MyFrame::OnPrintSetup
)
64 EVT_MENU(ID_PAGE_SETUP
, MyFrame::OnPageSetup
)
65 EVT_MENU(ID_PRINT_PS
, MyFrame::OnPrintPS
)
66 EVT_MENU(ID_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
67 EVT_MENU(ID_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
68 EVT_MENU(ID_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
69 EVT_MENU (wxID_ANY
, MyFrame::OnCommand
)
70 EVT_COMMAND (wxID_ANY
,wxID_ANY
, MyFrame::OnCommand
)
71 EVT_CHAR ( wxLayoutWindow::OnChar
)
76 wxFrame( (wxFrame
*) NULL
, wxID_ANY
, _T("wxLayout"),
77 wxDefaultPosition
, wxDefaultSize
)
81 SetStatusText( _T("wxLayout by Karsten Ballueder.") );
82 #endif // wxUSE_STATUSBAR
84 wxMenuBar
*menu_bar
= new wxMenuBar();
86 wxMenu
*file_menu
= new wxMenu
;
87 file_menu
->Append(ID_PRINT
, _T("&Print..."), _T("Print"));
88 file_menu
->Append(ID_PRINT_SETUP
, _T("Print &Setup..."),_T("Setup printer properties"));
89 file_menu
->Append(ID_PAGE_SETUP
, _T("Page Set&up..."), _T("Page setup"));
90 file_menu
->Append(ID_PREVIEW
, _T("Print Pre&view"), _T("Preview"));
92 file_menu
->AppendSeparator();
93 file_menu
->Append(ID_PRINT_PS
, _T("Print PostScript..."), _T("Print (PostScript)"));
94 file_menu
->Append(ID_PRINT_SETUP_PS
, _T("Print Setup PostScript..."), _T("Setup printer properties (PostScript)"));
95 file_menu
->Append(ID_PAGE_SETUP_PS
, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
96 file_menu
->Append(ID_PREVIEW_PS
, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
98 file_menu
->AppendSeparator();
99 file_menu
->Append( ID_TEXT
, _T("Export &Text"));
100 file_menu
->Append( ID_HTML
, _T("Export &HTML"));
101 file_menu
->Append( ID_QUIT
, _T("E&xit"));
102 menu_bar
->Append(file_menu
, _T("&File"));
104 wxMenu
*edit_menu
= new wxMenu
;
105 edit_menu
->Append( ID_CLEAR
, _T("C&lear"));
106 edit_menu
->Append( ID_ADD_SAMPLE
, _T("&Example"));
107 edit_menu
->Append( ID_LONG_TEST
, _T("Add &many lines"));
108 edit_menu
->AppendSeparator();
109 edit_menu
->Append( ID_LINEBREAKS_TEST
, _T("Add &several lines"));
110 edit_menu
->Append( ID_URL_TEST
, _T("Insert an &URL"));
111 edit_menu
->AppendSeparator();
112 edit_menu
->Append(ID_WRAP
, _T("&Wrap mode"), _T("Activate wrapping at pixel 200."));
113 edit_menu
->Append(ID_NOWRAP
, _T("&No-wrap mode"), _T("Deactivate wrapping."));
114 edit_menu
->AppendSeparator();
115 edit_menu
->Append(ID_COPY
, _T("&Copy"), _T("Copy text to clipboard."));
116 edit_menu
->Append(ID_CUT
, _T("Cu&t"), _T("Cut text to clipboard."));
117 edit_menu
->Append(ID_PASTE
,_T("&Paste"), _T("Paste text from clipboard."));
119 edit_menu
->Append(ID_COPY_PRIMARY
, _T("C&opy primary"), _T("Copy text to primary selecton."));
120 edit_menu
->Append(ID_PASTE_PRIMARY
, _T("&Paste primary"), _T("Paste text from primary selection."));
122 edit_menu
->Append(ID_FIND
, _T("&Find"), _T("Find text."));
123 menu_bar
->Append(edit_menu
, _T("&Edit") );
126 menu_bar
->Show(true);
129 SetMenuBar( menu_bar
);
131 m_lwin
= new wxLayoutWindow(this);
133 m_lwin
->SetStatusBar(GetStatusBar(), 0, 1);
134 #endif // wxUSE_STATUSBAR
135 m_lwin
->SetMouseTracking(true);
136 m_lwin
->SetEditable(true);
137 m_lwin
->SetWrapMargin(40);
140 // JACS: under MSW, the window doesn't show the caret initially,
141 // and the following line I added doesn't help either.
142 // going to another window and then back again fixes it.
143 // m_lwin->OnSetFocus(wxFocusEvent());
148 // create and set the background bitmap (this will result in a lattice)
149 static const int sizeBmp
= 10;
150 wxBitmap
*bitmap
= new wxBitmap(sizeBmp
, sizeBmp
);
152 dcMem
.SelectObject( *bitmap
);
153 dcMem
.SetBackground( *wxWHITE_BRUSH
);
156 dcMem
.SetPen( *wxGREEN_PEN
);
157 dcMem
.DrawLine(sizeBmp
/2, 0, sizeBmp
/2, sizeBmp
);
158 dcMem
.DrawLine(0, sizeBmp
/2, sizeBmp
, sizeBmp
/2);
160 dcMem
.SelectObject( wxNullBitmap
);
162 m_lwin
->SetBackgroundBitmap(bitmap
);
166 void MyFrame::AddSampleText(wxLayoutList
*llist
)
168 llist
->Clear(wxSWISS
,16,wxNORMAL
,wxNORMAL
, false);
169 llist
->SetFont(-1,-1,-1,-1,-1,_T("blue"));
170 llist
->Insert(_T("blue"));
173 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"));
174 llist
->Insert(_T("The quick brown fox jumps over the lazy dog."));
177 llist
->SetFont(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false);
178 llist
->Insert(_T("--"));
181 llist
->SetFont(wxROMAN
);
182 llist
->Insert(_T("The quick brown fox jumps over the lazy dog."));
185 llist
->Insert(_T("Hello "));
186 wxBitmap
*icon
= new wxBitmap (wxIcon(Micon_xpm
));
188 llist
->Insert(new wxLayoutObjectIcon(icon
));
189 llist
->SetFontWeight(wxBOLD
);
190 llist
->Insert(_T("World! "));
191 llist
->SetFontWeight(wxNORMAL
);
192 llist
->Insert(_T("The quick brown fox jumps..."));
195 llist
->Insert(_T("over the lazy dog."));
196 llist
->SetFont(-1,-1,-1,-1,true);
197 llist
->Insert(_T("underlined"));
198 llist
->SetFont(-1,-1,-1,-1,false);
199 llist
->SetFont(wxROMAN
);
200 llist
->Insert(_T("This is "));
201 llist
->SetFont(-1,-1,-1,wxBOLD
);
202 llist
->Insert(_T("BOLD "));
203 llist
->SetFont(-1,-1,-1,wxNORMAL
);
204 llist
->Insert(_T("and "));
205 llist
->SetFont(-1,-1,wxITALIC
);
206 llist
->Insert(_T("italics "));
207 llist
->SetFont(-1,-1,wxNORMAL
);
210 llist
->Insert(_T("and "));
211 llist
->SetFont(-1,-1,wxSLANT
);
212 llist
->Insert(_T("slanted"));
213 llist
->SetFont(-1,-1,wxNORMAL
);
214 llist
->Insert(_T(" text."));
217 llist
->Insert(_T("and "));
218 llist
->SetFont(-1,-1,-1,-1,-1,_T("blue"));
219 llist
->Insert(_T("blue"));
220 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"));
221 llist
->Insert(_T(" and "));
222 llist
->SetFont(-1,-1,-1,-1,-1,_T("green"),_T("black"));
223 llist
->Insert(_T("green on black"));
224 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"),_T("white"));
225 llist
->Insert(_T(" text."));
228 llist
->SetFont(-1,-1,wxSLANT
);
229 llist
->Insert(_T("Slanted"));
230 llist
->SetFont(-1,-1,wxNORMAL
);
231 llist
->Insert(_T(" and normal text and "));
232 llist
->SetFont(-1,-1,wxSLANT
);
233 llist
->Insert(_T("slanted"));
234 llist
->SetFont(-1,-1,wxNORMAL
);
235 llist
->Insert(_T(" again."));
238 // add some more text for testing:
239 llist
->Insert(_T("And here the source for the test program:"));
242 llist
->SetFont(wxTELETYPE
,16);
243 llist
->Insert(_T("And here the source for the test program:"));
246 wxTextFile
file(_T("wxLayout.cpp"));
249 for ( wxString s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
252 llist
->Insert(line
.Format(_T("%6u: %s"),file
.GetCurrentLine()+1,s
.c_str()));
257 llist
->MoveCursorTo(wxPoint(0,0));
262 void MyFrame::Clear()
264 wxColour
colBg(0, 0, 0);
266 m_lwin
->Clear(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false, wxRED
, &colBg
);
270 void MyFrame::OnCommand( wxCommandEvent
&event
)
272 switch (event
.GetId())
280 wxLayoutPrintout
printout(m_lwin
->GetLayoutList(),_("M: Printout"));
281 if (! printer
.Print(this, &printout
, true))
283 // Had to remove the split up strings that used to be below, and
284 // put them into one long strong. Otherwise MSVC would give an
285 // error "C2308: concatenating mismatched wide strings" when
286 // building a Unicode version.
289 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
298 m_lwin
->SetWrapMargin(event
.GetId() == ID_NOWRAP
? 0 : 40);
301 AddSampleText(m_lwin
->GetLayoutList());
307 wxLogError( _T("Received click event.") );
311 m_lwin
->Refresh(false);
314 case ID_PASTE_PRIMARY
:
315 // text only from primary:
316 m_lwin
->Paste(false, true);
317 m_lwin
->Refresh(false);
319 case ID_COPY_PRIMARY
:
320 // copy text-only to primary selection:
321 m_lwin
->Copy(false, false, true);
322 m_lwin
->Refresh(false);
326 m_lwin
->Copy(true, true, false);
327 m_lwin
->Refresh(false);
331 m_lwin
->Refresh(false);
335 m_lwin
->Find("void");
336 m_lwin
->Refresh(false);
343 _T("Save As HTML..."),
346 _T("HTML file (*.html)|*.html|Text file (*.txt)|*.txt|Any file (*)|*"),
347 wxSAVE
|wxOVERWRITE_PROMPT
349 if (HTML_dialog
.ShowModal() == wxID_OK
)
351 wxFFileOutputStream
output( HTML_dialog
.GetPath() );
352 wxTextOutputStream
textout( output
);
354 wxLayoutExportObject
*export0
;
356 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
357 while((export0
= wxLayoutExport( &status
, WXLO_EXPORT_AS_HTML
)) != NULL
)
359 if(export0
->type
== WXLO_EXPORT_HTML
)
360 object
= *(export0
->content
.text
);
362 ; // ignore "<!--UNKNOWN OBJECT>";
374 _T("Save As TXT..."),
377 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
378 wxSAVE
|wxOVERWRITE_PROMPT
380 if (TEXT_dialog
.ShowModal() == wxID_OK
)
382 wxFFileOutputStream
output( TEXT_dialog
.GetPath() );
383 wxTextOutputStream
textout( output
);
385 wxLayoutExportObject
*export0
;
387 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
388 while((export0
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
390 if(export0
->type
== WXLO_EXPORT_TEXT
)
391 object
= *(export0
->content
.text
);
393 object
= _T("<!--UNKNOWN OBJECT>");
404 wxLayoutList
*llist
= m_lwin
->GetLayoutList();
405 for(int i
= 1; i
< 300; i
++)
407 line
.Printf(wxT("This is line number %d."), i
);
412 llist
->MoveCursorTo(wxPoint(0,0));
418 case ID_LINEBREAKS_TEST
:
421 m_lwin
->GetLayoutList(),
422 wxT("This is a text\nwith embedded line\nbreaks.\n")
430 // VZ: this doesn't work, of course, but I think it should -
431 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
432 // (instead of doing it manually like M does now)
433 m_lwin
->GetLayoutList()->Insert(_T("http://www.wxwidgets.org/"));
438 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
441 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
443 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
446 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(), _T("Printout from wxLayout"));
447 if (! printer
.Print(this, &printout
, true))
449 _T("There was a problem printing.\nPerhaps your current printer is not set correctly?"),
450 _T("Printing"), wxOK
);
453 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
455 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
458 wxPostScriptPrinter printer
;
459 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"My printout");
460 printer
.Print(this, &printout
, true);
464 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
467 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
469 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
471 wxPrintData printData
;
473 // Pass two printout objects: for preview, and possible printing.
474 wxPrintPreview
*preview
= new wxPrintPreview(new
476 m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
480 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
484 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
485 frame
->Centre(wxBOTH
);
490 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
492 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
494 wxPrintData printData
;
496 // Pass two printout objects: for preview, and possible printing.
497 wxPrintPreview
*preview
= new wxPrintPreview(new wxLayoutPrintout( m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
498 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
499 frame
->Centre(wxBOTH
);
504 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
507 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
509 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
511 wxPrintDialog
printerDialog(this, & m_PrintData
);
512 printerDialog
.ShowModal();
515 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
518 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
520 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
522 wxPageSetupDialogData data
;
524 #if defined(__WXMSW__) || defined(__WXMAC__)
525 wxPageSetupDialog
pageSetupDialog(this, & data
);
527 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
529 pageSetupDialog
.ShowModal();
531 data
= pageSetupDialog
.GetPageSetupDialogData();
534 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
536 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
540 #if defined(__WXMSW__) || defined(__WXMAC__)
541 wxPrintDialog
printerDialog(this, & data
);
543 wxGenericPrintDialog
printerDialog(this, & data
);
545 printerDialog
.ShowModal();
548 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
550 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
552 wxPageSetupData data
;
553 #if defined(__WXMSW__) || defined(__WXMAC__)
554 wxPageSetupDialog
pageSetupDialog(this, & data
);
556 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
558 pageSetupDialog
.ShowModal();
562 //-----------------------------------------------------------------------------
564 //-----------------------------------------------------------------------------
573 wxFrame
*frame
= new MyFrame();
574 wxInitAllImageHandlers();
576 // wxSetAFMPath("/usr/local/src/wxWidgets/misc/afm/");