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, "wxLayout",
74 wxPoint(880,100), wxSize(256,256) )
78 SetStatusText( "wxLayout by Karsten Ballüder." );
80 wxMenuBar
*menu_bar
= new wxMenuBar();
82 wxMenu
*file_menu
= new wxMenu
;
83 file_menu
->Append(ID_PRINT
, "&Print...", "Print");
84 file_menu
->Append(ID_PRINT_SETUP
, "Print &Setup...","Setup printer properties");
85 file_menu
->Append(ID_PAGE_SETUP
, "Page Set&up...", "Page setup");
86 file_menu
->Append(ID_PREVIEW
, "Print Pre&view", "Preview");
88 file_menu
->AppendSeparator();
89 file_menu
->Append(ID_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
90 file_menu
->Append(ID_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
91 file_menu
->Append(ID_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
92 file_menu
->Append(ID_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
94 file_menu
->AppendSeparator();
95 file_menu
->Append( ID_TEXT
, "Export &Text");
96 file_menu
->Append( ID_HTML
, "Export &HTML");
97 file_menu
->Append( ID_QUIT
, "E&xit");
98 menu_bar
->Append(file_menu
, "&File" );
100 wxMenu
*edit_menu
= new wxMenu
;
101 edit_menu
->Append( ID_CLEAR
, "C&lear");
102 edit_menu
->Append( ID_ADD_SAMPLE
, "&Example");
103 edit_menu
->Append( ID_LONG_TEST
, "Add &many lines");
104 edit_menu
->AppendSeparator();
105 edit_menu
->Append( ID_LINEBREAKS_TEST
, "Add &several lines");
106 edit_menu
->Append( ID_URL_TEST
, "Insert an &URL");
107 edit_menu
->AppendSeparator();
108 edit_menu
->Append(ID_WRAP
, "&Wrap mode", "Activate wrapping at pixel 200.");
109 edit_menu
->Append(ID_NOWRAP
, "&No-wrap mode", "Deactivate wrapping.");
110 edit_menu
->AppendSeparator();
111 edit_menu
->Append(ID_COPY
, "&Copy", "Copy text to clipboard.");
112 edit_menu
->Append(ID_CUT
, "Cu&t", "Cut text to clipboard.");
113 edit_menu
->Append(ID_PASTE
,"&Paste", "Paste text from clipboard.");
115 edit_menu
->Append(ID_COPY_PRIMARY
, "C&opy primary", "Copy text to primary selecton.");
116 edit_menu
->Append(ID_PASTE_PRIMARY
,"&Paste primary", "Paste text from primary selection.");
118 edit_menu
->Append(ID_FIND
, "&Find", "Find text.");
119 menu_bar
->Append(edit_menu
, "&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,"blue");
165 llist
->Insert("blue");
166 llist
->SetFont(-1,-1,-1,-1,-1,"black");
167 llist
->Insert("The quick brown fox jumps over the lazy dog.");
170 llist
->SetFont(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false);
174 llist
->SetFont(wxROMAN
);
175 llist
->Insert("The quick brown fox jumps over the lazy dog.");
177 llist
->Insert("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("World! ");
185 llist
->SetFontWeight(wxNORMAL
);
186 llist
->Insert("The quick brown fox jumps...");
188 llist
->Insert("over the lazy dog.");
189 llist
->SetFont(-1,-1,-1,-1,true);
190 llist
->Insert("underlined");
191 llist
->SetFont(-1,-1,-1,-1,false);
192 llist
->SetFont(wxROMAN
);
193 llist
->Insert("This is ");
194 llist
->SetFont(-1,-1,-1,wxBOLD
); llist
->Insert("BOLD "); llist
->SetFont(-1,-1,-1,wxNORMAL
);
195 llist
->Insert("and ");
196 llist
->SetFont(-1,-1,wxITALIC
);
197 llist
->Insert("italics ");
198 llist
->SetFont(-1,-1,wxNORMAL
);
200 llist
->Insert("and ");
201 llist
->SetFont(-1,-1,wxSLANT
);
202 llist
->Insert("slanted");
203 llist
->SetFont(-1,-1,wxNORMAL
);
204 llist
->Insert(" text.");
206 llist
->Insert("and ");
207 llist
->SetFont(-1,-1,-1,-1,-1,"blue");
208 llist
->Insert("blue");
209 llist
->SetFont(-1,-1,-1,-1,-1,"black");
210 llist
->Insert(" and ");
211 llist
->SetFont(-1,-1,-1,-1,-1,"green","black");
212 llist
->Insert("green on black");
213 llist
->SetFont(-1,-1,-1,-1,-1,"black","white");
214 llist
->Insert(" text.");
217 llist
->SetFont(-1,-1,wxSLANT
);
218 llist
->Insert("Slanted");
219 llist
->SetFont(-1,-1,wxNORMAL
);
220 llist
->Insert(" and normal text and ");
221 llist
->SetFont(-1,-1,wxSLANT
);
222 llist
->Insert("slanted");
223 llist
->SetFont(-1,-1,wxNORMAL
);
224 llist
->Insert(" again.");
227 // add some more text for testing:
228 llist
->Insert("And here the source for the test program:");
230 llist
->SetFont(wxTELETYPE
,16);
231 llist
->Insert("And here the source for the test program:");
233 llist
->Insert("And here the source for the test program:");
235 llist
->Insert("And here the source for the test program:");
238 FILE *in
= fopen("wxLayout.cpp","r");
243 fgets(buffer
,1024,in
);
246 wxLayoutImportText(llist
, buffer
);
250 llist
->MoveCursorTo(wxPoint(0,0));
258 wxColour
colBg(0, 0, 0);
260 m_lwin
->Clear(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false, wxRED
, &colBg
);
264 void MyFrame::OnCommand( wxCommandEvent
&event
)
266 switch (event
.GetId())
274 wxLayoutPrintout
printout(m_lwin
->GetLayoutList(),_("M: Printout"));
275 if (! printer
.Print(this, &printout
, TRUE
))
277 _("There was a problem with printing the message:\n"
278 "perhaps your current printer is not set up correctly?"),
279 _("Printing"), wxOK
);
284 m_lwin
->SetWrapMargin(event
.GetId() == ID_NOWRAP
? 0 : 40);
287 AddSampleText(m_lwin
->GetLayoutList());
293 cerr
<< "Received click event." << endl
;
297 m_lwin
->Refresh(FALSE
);
300 case ID_PASTE_PRIMARY
:
301 // text only from primary:
302 m_lwin
->Paste(FALSE
, TRUE
);
303 m_lwin
->Refresh(FALSE
);
305 case ID_COPY_PRIMARY
:
306 // copy text-only to primary selection:
307 m_lwin
->Copy(FALSE
,FALSE
,TRUE
);
308 m_lwin
->Refresh(FALSE
);
312 m_lwin
->Copy(TRUE
,TRUE
,FALSE
);
313 m_lwin
->Refresh(FALSE
);
317 m_lwin
->Refresh(FALSE
);
321 m_lwin
->Find("void");
322 m_lwin
->Refresh(FALSE
);
327 wxLayoutExportObject
*export0
;
328 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
330 cout
<< "<HTML>" << endl
;
331 while((export0
= wxLayoutExport( &status
,
332 WXLO_EXPORT_AS_HTML
)) != NULL
)
334 if(export0
->type
== WXLO_EXPORT_HTML
)
335 cout
<< *(export0
->content
.text
);
337 ; // ignore itcout << "<!--UNKNOWN OBJECT>";
344 wxLayoutExportObject
*export0
;
345 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
347 while((export0
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
349 if(export0
->type
== WXLO_EXPORT_TEXT
)
350 cout
<< *(export0
->content
.text
);
352 cout
<< "<!--UNKNOWN OBJECT>";
360 wxLayoutList
*llist
= m_lwin
->GetLayoutList();
361 for(int i
= 1; i
< 300; i
++)
363 line
.Printf("This is line number %d.", i
);
367 llist
->MoveCursorTo(wxPoint(0,0));
373 case ID_LINEBREAKS_TEST
:
374 wxLayoutImportText(m_lwin
->GetLayoutList(),
376 "with embedded line\n"
383 // VZ: this doesn't work, of course, but I think it should -
384 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
385 // (instead of doing it manually like M does now)
386 m_lwin
->GetLayoutList()->Insert("http://www.wxwindows.org/");
390 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
393 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
395 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
398 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"Printout from wxLayout");
399 if (! printer
.Print(this, &printout
, TRUE
))
401 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
405 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
407 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
410 wxPostScriptPrinter printer
;
411 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"My printout");
412 printer
.Print(this, &printout
, TRUE
);
416 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
419 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
421 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
423 wxPrintData printData
;
425 // Pass two printout objects: for preview, and possible printing.
426 wxPrintPreview
*preview
= new wxPrintPreview(new
428 m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
432 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
436 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
437 frame
->Centre(wxBOTH
);
442 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
444 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
446 wxPrintData printData
;
448 // Pass two printout objects: for preview, and possible printing.
449 wxPrintPreview
*preview
= new wxPrintPreview(new wxLayoutPrintout( m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
450 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
451 frame
->Centre(wxBOTH
);
456 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
459 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
461 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
463 wxPrintDialog
printerDialog(this, & m_PrintData
);
464 printerDialog
.ShowModal();
467 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
470 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
472 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
474 wxPageSetupData data
;
476 #if defined(__WXMSW__) || defined(__WXMAC__)
477 wxPageSetupDialog
pageSetupDialog(this, & data
);
479 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
481 pageSetupDialog
.ShowModal();
483 data
= pageSetupDialog
.GetPageSetupData();
486 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
488 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
492 #if defined(__WXMSW__) || defined(__WXMAC__)
493 wxPrintDialog
printerDialog(this, & data
);
495 wxGenericPrintDialog
printerDialog(this, & data
);
497 printerDialog
.ShowModal();
500 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
502 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
504 wxPageSetupData data
;
505 #if defined(__WXMSW__) || defined(__WXMAC__)
506 wxPageSetupDialog
pageSetupDialog(this, & data
);
508 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
510 pageSetupDialog
.ShowModal();
514 //-----------------------------------------------------------------------------
516 //-----------------------------------------------------------------------------
523 bool MyApp::OnInit(void)
525 wxFrame
*frame
= new MyFrame();
526 wxInitAllImageHandlers();
528 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");