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>
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
44 ID_ADD_SAMPLE
= 1, ID_CLEAR
, ID_PRINT
,
45 ID_PRINT_SETUP
, ID_PAGE_SETUP
, ID_PREVIEW
, ID_PRINT_PS
,
46 ID_PRINT_SETUP_PS
, ID_PAGE_SETUP_PS
,ID_PREVIEW_PS
,
47 ID_WRAP
, ID_NOWRAP
, ID_PASTE
, ID_COPY
, ID_CUT
,
48 ID_COPY_PRIMARY
, ID_PASTE_PRIMARY
,
50 ID_WXLAYOUT_DEBUG
, ID_QUIT
, ID_CLICK
, ID_HTML
, ID_TEXT
,
51 ID_TEST
, ID_LINEBREAKS_TEST
, ID_LONG_TEST
, ID_URL_TEST
55 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
57 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
58 EVT_MENU(ID_PRINT
, MyFrame::OnPrint
)
59 EVT_MENU(ID_PREVIEW
, MyFrame::OnPrintPreview
)
60 EVT_MENU(ID_PRINT_SETUP
, MyFrame::OnPrintSetup
)
61 EVT_MENU(ID_PAGE_SETUP
, MyFrame::OnPageSetup
)
62 EVT_MENU(ID_PRINT_PS
, MyFrame::OnPrintPS
)
63 EVT_MENU(ID_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
64 EVT_MENU(ID_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
65 EVT_MENU(ID_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
66 EVT_MENU (-1, MyFrame::OnCommand
)
67 EVT_COMMAND (-1,-1, MyFrame::OnCommand
)
68 EVT_CHAR ( wxLayoutWindow::OnChar
)
72 MyFrame::MyFrame(void) :
73 wxFrame( (wxFrame
*) NULL
, -1, _T("wxLayout"),
74 wxPoint(880,100), wxSize(256,256) )
78 SetStatusText( _T("wxLayout by Karsten Ballüder.") );
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);
128 m_lwin
->SetStatusBar(GetStatusBar(), 0, 1);
129 m_lwin
->SetMouseTracking(true);
130 m_lwin
->SetEditable(true);
131 m_lwin
->SetWrapMargin(40);
134 // JACS: under MSW, the window doesn't show the caret initially,
135 // and the following line I added doesn't help either.
136 // going to another window and then back again fixes it.
137 // m_lwin->OnSetFocus(wxFocusEvent());
142 // create and set the background bitmap (this will result in a lattice)
143 static const int sizeBmp
= 10;
144 wxBitmap
*bitmap
= new wxBitmap(sizeBmp
, sizeBmp
);
146 dcMem
.SelectObject( *bitmap
);
147 dcMem
.SetBackground( *wxWHITE_BRUSH
);
150 dcMem
.SetPen( *wxGREEN_PEN
);
151 dcMem
.DrawLine(sizeBmp
/2, 0, sizeBmp
/2, sizeBmp
);
152 dcMem
.DrawLine(0, sizeBmp
/2, sizeBmp
, sizeBmp
/2);
154 dcMem
.SelectObject( wxNullBitmap
);
156 m_lwin
->SetBackgroundBitmap(bitmap
);
161 MyFrame::AddSampleText(wxLayoutList
*llist
)
163 llist
->Clear(wxSWISS
,16,wxNORMAL
,wxNORMAL
, false);
164 llist
->SetFont(-1,-1,-1,-1,-1,_T("blue"));
165 llist
->Insert(_T("blue"));
166 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"));
167 llist
->Insert(_T("The quick brown fox jumps over the lazy dog."));
170 llist
->SetFont(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false);
171 llist
->Insert(_T("--"));
174 llist
->SetFont(wxROMAN
);
175 llist
->Insert(_T("The quick brown fox jumps over the lazy dog."));
177 llist
->Insert(_T("Hello "));
179 llist
->Insert(new wxLayoutObjectIcon(new wxICON(Micon
)));
181 llist
->Insert(new wxLayoutObjectIcon(new wxBitmap (wxICON(Micon
))));
183 llist
->SetFontWeight(wxBOLD
);
184 llist
->Insert(_T("World! "));
185 llist
->SetFontWeight(wxNORMAL
);
186 llist
->Insert(_T("The quick brown fox jumps..."));
188 llist
->Insert(_T("over the lazy dog."));
189 llist
->SetFont(-1,-1,-1,-1,true);
190 llist
->Insert(_T("underlined"));
191 llist
->SetFont(-1,-1,-1,-1,false);
192 llist
->SetFont(wxROMAN
);
193 llist
->Insert(_T("This is "));
194 llist
->SetFont(-1,-1,-1,wxBOLD
); llist
->Insert(_T("BOLD ")); llist
->SetFont(-1,-1,-1,wxNORMAL
);
195 llist
->Insert(_T("and "));
196 llist
->SetFont(-1,-1,wxITALIC
);
197 llist
->Insert(_T("italics "));
198 llist
->SetFont(-1,-1,wxNORMAL
);
200 llist
->Insert(_T("and "));
201 llist
->SetFont(-1,-1,wxSLANT
);
202 llist
->Insert(_T("slanted"));
203 llist
->SetFont(-1,-1,wxNORMAL
);
204 llist
->Insert(_T(" text."));
206 llist
->Insert(_T("and "));
207 llist
->SetFont(-1,-1,-1,-1,-1,_T("blue"));
208 llist
->Insert(_T("blue"));
209 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"));
210 llist
->Insert(_T(" and "));
211 llist
->SetFont(-1,-1,-1,-1,-1,_T("green"),_T("black"));
212 llist
->Insert(_T("green on black"));
213 llist
->SetFont(-1,-1,-1,-1,-1,_T("black"),_T("white"));
214 llist
->Insert(_T(" text."));
217 llist
->SetFont(-1,-1,wxSLANT
);
218 llist
->Insert(_T("Slanted"));
219 llist
->SetFont(-1,-1,wxNORMAL
);
220 llist
->Insert(_T(" and normal text and "));
221 llist
->SetFont(-1,-1,wxSLANT
);
222 llist
->Insert(_T("slanted"));
223 llist
->SetFont(-1,-1,wxNORMAL
);
224 llist
->Insert(_T(" again."));
227 // add some more text for testing:
228 llist
->Insert(_T("And here the source for the test program:"));
230 llist
->SetFont(wxTELETYPE
,16);
231 llist
->Insert(_T("And here the source for the test program:"));
233 llist
->Insert(_T("And here the source for the test program:"));
235 llist
->Insert(_T("And here the source for the test program:"));
237 wxFile
file( _T("wxLayout.cpp") );
238 if ( file
.IsOpened() )
240 off_t len
= file
.Length();
241 wxChar
*data
= (wxChar
*)malloc(2*len
);
242 if ( file
.Read(data
, len
) == len
)
244 wxLayoutImportText(llist
, data
);
249 llist
->MoveCursorTo(wxPoint(0,0));
257 wxColour
colBg(0, 0, 0);
259 m_lwin
->Clear(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false, wxRED
, &colBg
);
263 void MyFrame::OnCommand( wxCommandEvent
&event
)
265 switch (event
.GetId())
273 wxLayoutPrintout
printout(m_lwin
->GetLayoutList(),_("M: Printout"));
274 if (! printer
.Print(this, &printout
, TRUE
))
276 // Had to remove the split up strings that used to be below, and
277 // put them into one long strong. Otherwise MSVC would give an
278 // error "C2308: concatenating mismatched wide strings" when
279 // building a Unicode version.
282 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
291 m_lwin
->SetWrapMargin(event
.GetId() == ID_NOWRAP
? 0 : 40);
294 AddSampleText(m_lwin
->GetLayoutList());
300 cerr
<< "Received click event." << endl
;
304 m_lwin
->Refresh(FALSE
);
307 case ID_PASTE_PRIMARY
:
308 // text only from primary:
309 m_lwin
->Paste(FALSE
, TRUE
);
310 m_lwin
->Refresh(FALSE
);
312 case ID_COPY_PRIMARY
:
313 // copy text-only to primary selection:
314 m_lwin
->Copy(FALSE
,FALSE
,TRUE
);
315 m_lwin
->Refresh(FALSE
);
319 m_lwin
->Copy(TRUE
,TRUE
,FALSE
);
320 m_lwin
->Refresh(FALSE
);
324 m_lwin
->Refresh(FALSE
);
328 m_lwin
->Find("void");
329 m_lwin
->Refresh(FALSE
);
334 wxLayoutExportObject
*export0
;
335 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
337 cout
<< "<HTML>" << endl
;
338 while((export0
= wxLayoutExport( &status
,
339 WXLO_EXPORT_AS_HTML
)) != NULL
)
341 if(export0
->type
== WXLO_EXPORT_HTML
)
342 cout
<< *(export0
->content
.text
);
344 ; // ignore itcout << "<!--UNKNOWN OBJECT>";
353 wxLayoutExportObject
*export0
;
354 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
356 while((export0
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
358 if(export0
->type
== WXLO_EXPORT_TEXT
)
359 cout
<< *(export0
->content
.text
);
361 cout
<< "<!--UNKNOWN OBJECT>";
373 wxLayoutList
*llist
= m_lwin
->GetLayoutList();
374 for(int i
= 1; i
< 300; i
++)
376 line
.Printf(wxT("This is line number %d."), i
);
381 llist
->MoveCursorTo(wxPoint(0,0));
387 case ID_LINEBREAKS_TEST
:
390 m_lwin
->GetLayoutList(),
391 wxT("This is a text\nwith embedded line\nbreaks.\n")
399 // VZ: this doesn't work, of course, but I think it should -
400 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
401 // (instead of doing it manually like M does now)
402 m_lwin
->GetLayoutList()->Insert(_T("http://www.wxwindows.org/"));
406 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
409 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
411 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
414 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(), _T("Printout from wxLayout"));
415 if (! printer
.Print(this, &printout
, TRUE
))
417 _T("There was a problem printing.\nPerhaps your current printer is not set correctly?"),
418 _T("Printing"), wxOK
);
421 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
423 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
426 wxPostScriptPrinter printer
;
427 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"My printout");
428 printer
.Print(this, &printout
, TRUE
);
432 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
435 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
437 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
439 wxPrintData printData
;
441 // Pass two printout objects: for preview, and possible printing.
442 wxPrintPreview
*preview
= new wxPrintPreview(new
444 m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
448 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK
);
452 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
453 frame
->Centre(wxBOTH
);
458 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
460 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
462 wxPrintData printData
;
464 // Pass two printout objects: for preview, and possible printing.
465 wxPrintPreview
*preview
= new wxPrintPreview(new wxLayoutPrintout( m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
466 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
467 frame
->Centre(wxBOTH
);
472 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
475 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
477 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
479 wxPrintDialog
printerDialog(this, & m_PrintData
);
480 printerDialog
.ShowModal();
483 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
486 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
488 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
490 wxPageSetupData data
;
492 #if defined(__WXMSW__) || defined(__WXMAC__)
493 wxPageSetupDialog
pageSetupDialog(this, & data
);
495 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
497 pageSetupDialog
.ShowModal();
499 data
= pageSetupDialog
.GetPageSetupData();
502 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
504 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
508 #if defined(__WXMSW__) || defined(__WXMAC__)
509 wxPrintDialog
printerDialog(this, & data
);
511 wxGenericPrintDialog
printerDialog(this, & data
);
513 printerDialog
.ShowModal();
516 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
518 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
520 wxPageSetupData data
;
521 #if defined(__WXMSW__) || defined(__WXMAC__)
522 wxPageSetupDialog
pageSetupDialog(this, & data
);
524 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
526 pageSetupDialog
.ShowModal();
530 //-----------------------------------------------------------------------------
532 //-----------------------------------------------------------------------------
539 bool MyApp::OnInit(void)
541 wxFrame
*frame
= new MyFrame();
542 wxInitAllImageHandlers();
544 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");