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>
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
38 ID_ADD_SAMPLE
= 1, ID_CLEAR
, ID_PRINT
,
39 ID_PRINT_SETUP
, ID_PAGE_SETUP
, ID_PREVIEW
, ID_PRINT_PS
,
40 ID_PRINT_SETUP_PS
, ID_PAGE_SETUP_PS
,ID_PREVIEW_PS
,
41 ID_WRAP
, ID_NOWRAP
, ID_PASTE
, ID_COPY
, ID_CUT
,
44 ID_WXLAYOUT_DEBUG
, ID_QUIT
, ID_CLICK
, ID_HTML
, ID_TEXT
,
45 ID_TEST
, ID_LINEBREAKS_TEST
, ID_LONG_TEST
, ID_URL_TEST
49 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
51 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
52 EVT_MENU(ID_PRINT
, MyFrame::OnPrint
)
53 EVT_MENU(ID_PREVIEW
, MyFrame::OnPrintPreview
)
54 EVT_MENU(ID_PRINT_SETUP
, MyFrame::OnPrintSetup
)
55 EVT_MENU(ID_PAGE_SETUP
, MyFrame::OnPageSetup
)
56 EVT_MENU(ID_PRINT_PS
, MyFrame::OnPrintPS
)
57 EVT_MENU(ID_PREVIEW_PS
, MyFrame::OnPrintPreviewPS
)
58 EVT_MENU(ID_PRINT_SETUP_PS
, MyFrame::OnPrintSetupPS
)
59 EVT_MENU(ID_PAGE_SETUP_PS
, MyFrame::OnPageSetupPS
)
60 EVT_MENU (-1, MyFrame::OnCommand
)
61 EVT_COMMAND (-1,-1, MyFrame::OnCommand
)
62 EVT_CHAR ( wxLayoutWindow::OnChar
)
66 MyFrame::MyFrame(void) :
67 wxFrame( (wxFrame
*) NULL
, -1, "wxLayout",
68 wxPoint(880,100), wxSize(256,256) )
72 SetStatusText( "wxLayout by Karsten Ballüder." );
74 wxMenuBar
*menu_bar
= new wxMenuBar();
76 wxMenu
*file_menu
= new wxMenu
;
77 file_menu
->Append(ID_PRINT
, "&Print...", "Print");
78 file_menu
->Append(ID_PRINT_SETUP
, "Print &Setup...","Setup printer properties");
79 file_menu
->Append(ID_PAGE_SETUP
, "Page Set&up...", "Page setup");
80 file_menu
->Append(ID_PREVIEW
, "Print Pre&view", "Preview");
82 file_menu
->AppendSeparator();
83 file_menu
->Append(ID_PRINT_PS
, "Print PostScript...", "Print (PostScript)");
84 file_menu
->Append(ID_PRINT_SETUP_PS
, "Print Setup PostScript...", "Setup printer properties (PostScript)");
85 file_menu
->Append(ID_PAGE_SETUP_PS
, "Page Setup PostScript...", "Page setup (PostScript)");
86 file_menu
->Append(ID_PREVIEW_PS
, "Print Preview PostScript", "Preview (PostScript)");
88 file_menu
->AppendSeparator();
89 file_menu
->Append( ID_TEXT
, "Export &Text");
90 file_menu
->Append( ID_HTML
, "Export &HTML");
91 file_menu
->Append( ID_QUIT
, "E&xit");
92 menu_bar
->Append(file_menu
, "&File" );
94 wxMenu
*edit_menu
= new wxMenu
;
95 edit_menu
->Append( ID_CLEAR
, "C&lear");
96 edit_menu
->Append( ID_ADD_SAMPLE
, "&Example");
97 edit_menu
->Append( ID_LONG_TEST
, "Add &many lines");
98 edit_menu
->AppendSeparator();
99 edit_menu
->Append( ID_LINEBREAKS_TEST
, "Add &several lines");
100 edit_menu
->Append( ID_URL_TEST
, "Insert an &URL");
101 edit_menu
->AppendSeparator();
102 edit_menu
->Append(ID_WRAP
, "&Wrap mode", "Activate wrapping at pixel 200.");
103 edit_menu
->Append(ID_NOWRAP
, "&No-wrap mode", "Deactivate wrapping.");
104 edit_menu
->AppendSeparator();
105 edit_menu
->Append(ID_COPY
, "&Copy", "Copy text to clipboard.");
106 edit_menu
->Append(ID_CUT
, "Cu&t", "Cut text to clipboard.");
108 edit_menu
->Append(ID_PASTE
,"&Paste", "Paste text from clipboard.");
110 edit_menu
->Append(ID_PASTE_PRIMARY
,"&Paste primary", "Paste text from primary selection.");
111 edit_menu
->Append(ID_FIND
, "&Find", "Find text.");
112 menu_bar
->Append(edit_menu
, "&Edit" );
115 menu_bar
->Show( TRUE
);
118 SetMenuBar( menu_bar
);
120 m_lwin
= new wxLayoutWindow(this);
121 m_lwin
->SetStatusBar(GetStatusBar(), 0, 1);
122 m_lwin
->SetMouseTracking(true);
123 m_lwin
->SetEditable(true);
124 m_lwin
->SetWrapMargin(40);
129 // create and set the background bitmap (this will result in a lattice)
130 static const int sizeBmp
= 10;
131 wxBitmap
*bitmap
= new wxBitmap(sizeBmp
, sizeBmp
);
133 dcMem
.SelectObject( *bitmap
);
134 dcMem
.SetBackground( *wxWHITE_BRUSH
);
137 dcMem
.SetPen( *wxGREEN_PEN
);
138 dcMem
.DrawLine(sizeBmp
/2, 0, sizeBmp
/2, sizeBmp
);
139 dcMem
.DrawLine(0, sizeBmp
/2, sizeBmp
, sizeBmp
/2);
141 dcMem
.SelectObject( wxNullBitmap
);
143 m_lwin
->SetBackgroundBitmap(bitmap
);
148 MyFrame::AddSampleText(wxLayoutList
*llist
)
150 llist
->Clear(wxSWISS
,16,wxNORMAL
,wxNORMAL
, false);
151 llist
->SetFont(-1,-1,-1,-1,-1,"blue");
152 llist
->Insert("blue");
153 llist
->SetFont(-1,-1,-1,-1,-1,"black");
154 llist
->Insert("The quick brown fox jumps over the lazy dog.");
157 llist
->SetFont(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false);
161 llist
->SetFont(wxROMAN
);
162 llist
->Insert("The quick brown fox jumps over the lazy dog.");
164 llist
->Insert("Hello ");
165 llist
->Insert(new wxLayoutObjectIcon(new wxICON(Micon
)));
166 llist
->SetFontWeight(wxBOLD
);
167 llist
->Insert("World! ");
168 llist
->SetFontWeight(wxNORMAL
);
169 llist
->Insert("The quick brown fox jumps...");
171 llist
->Insert("over the lazy dog.");
172 llist
->SetFont(-1,-1,-1,-1,true);
173 llist
->Insert("underlined");
174 llist
->SetFont(-1,-1,-1,-1,false);
175 llist
->SetFont(wxROMAN
);
176 llist
->Insert("This is ");
177 llist
->SetFont(-1,-1,-1,wxBOLD
); llist
->Insert("BOLD "); llist
->SetFont(-1,-1,-1,wxNORMAL
);
178 llist
->Insert("and ");
179 llist
->SetFont(-1,-1,wxITALIC
);
180 llist
->Insert("italics ");
181 llist
->SetFont(-1,-1,wxNORMAL
);
183 llist
->Insert("and ");
184 llist
->SetFont(-1,-1,wxSLANT
);
185 llist
->Insert("slanted");
186 llist
->SetFont(-1,-1,wxNORMAL
);
187 llist
->Insert(" text.");
189 llist
->Insert("and ");
190 llist
->SetFont(-1,-1,-1,-1,-1,"blue");
191 llist
->Insert("blue");
192 llist
->SetFont(-1,-1,-1,-1,-1,"black");
193 llist
->Insert(" and ");
194 llist
->SetFont(-1,-1,-1,-1,-1,"green","black");
195 llist
->Insert("green on black");
196 llist
->SetFont(-1,-1,-1,-1,-1,"black","white");
197 llist
->Insert(" text.");
200 llist
->SetFont(-1,-1,wxSLANT
);
201 llist
->Insert("Slanted");
202 llist
->SetFont(-1,-1,wxNORMAL
);
203 llist
->Insert(" and normal text and ");
204 llist
->SetFont(-1,-1,wxSLANT
);
205 llist
->Insert("slanted");
206 llist
->SetFont(-1,-1,wxNORMAL
);
207 llist
->Insert(" again.");
210 // add some more text for testing:
211 llist
->Insert("And here the source for the test program:");
213 llist
->SetFont(wxTELETYPE
,16);
214 llist
->Insert("And here the source for the test program:");
216 llist
->Insert("And here the source for the test program:");
218 llist
->Insert("And here the source for the test program:");
221 FILE *in
= fopen("wxLayout.cpp","r");
226 fgets(buffer
,1024,in
);
229 wxLayoutImportText(llist
, buffer
);
233 llist
->MoveCursorTo(wxPoint(0,0));
241 wxColour
colBg(0, 0, 0);
243 m_lwin
->Clear(wxROMAN
,16,wxNORMAL
,wxNORMAL
, false, wxRED
, &colBg
);
247 void MyFrame::OnCommand( wxCommandEvent
&event
)
249 switch (event
.GetId())
257 wxLayoutPrintout
printout(m_lwin
->GetLayoutList(),_("M: Printout"));
258 if (! printer
.Print(this, &printout
, TRUE
))
260 _("There was a problem with printing the message:\n"
261 "perhaps your current printer is not set up correctly?"),
262 _("Printing"), wxOK
);
267 m_lwin
->SetWrapMargin(event
.GetId() == ID_NOWRAP
? 0 : 40);
270 AddSampleText(m_lwin
->GetLayoutList());
276 cerr
<< "Received click event." << endl
;
280 m_lwin
->Refresh(FALSE
);
283 case ID_PASTE_PRIMARY
:
285 m_lwin
->Refresh(FALSE
);
290 m_lwin
->Refresh(FALSE
);
294 m_lwin
->Refresh(FALSE
);
297 m_lwin
->Find("void");
298 m_lwin
->Refresh(FALSE
);
302 wxLayoutExportObject
*export
;
303 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
305 while((export
= wxLayoutExport( &status
,
306 WXLO_EXPORT_AS_HTML
)) != NULL
)
308 if(export
->type
== WXLO_EXPORT_HTML
)
309 cout
<< *(export
->content
.text
);
311 cout
<< "<!--UNKNOWN OBJECT>";
318 wxLayoutExportObject
*export
;
319 wxLayoutExportStatus
status(m_lwin
->GetLayoutList());
321 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
323 if(export
->type
== WXLO_EXPORT_TEXT
)
324 cout
<< *(export
->content
.text
);
326 cout
<< "<!--UNKNOWN OBJECT>";
334 wxLayoutList
*llist
= m_lwin
->GetLayoutList();
335 for(int i
= 1; i
< 300; i
++)
337 line
.Printf("This is line number %d.", i
);
341 llist
->MoveCursorTo(wxPoint(0,0));
347 case ID_LINEBREAKS_TEST
:
348 wxLayoutImportText(m_lwin
->GetLayoutList(),
350 "with embedded line\n"
357 // VZ: this doesn't work, of course, but I think it should -
358 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
359 // (instead of doing it manually like M does now)
360 m_lwin
->GetLayoutList()->Insert("http://www.wxwindows.org/");
364 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
367 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
369 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
372 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"Printout from wxLayout");
373 if (! printer
.Print(this, &printout
, TRUE
))
375 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
379 void MyFrame::OnPrintPS(wxCommandEvent
& WXUNUSED(event
))
381 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
384 wxPostScriptPrinter printer
;
385 wxLayoutPrintout
printout( m_lwin
->GetLayoutList(),"My printout");
386 printer
.Print(this, &printout
, TRUE
);
390 void MyFrame::OnPrintPreview(wxCommandEvent
& WXUNUSED(event
))
393 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
395 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
397 wxPrintData printData
;
399 // Pass two printout objects: for preview, and possible printing.
400 wxPrintPreview
*preview
= new wxPrintPreview(new
402 m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
406 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK
);
410 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
411 frame
->Centre(wxBOTH
);
416 void MyFrame::OnPrintPreviewPS(wxCommandEvent
& WXUNUSED(event
))
418 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
420 wxPrintData printData
;
422 // Pass two printout objects: for preview, and possible printing.
423 wxPrintPreview
*preview
= new wxPrintPreview(new wxLayoutPrintout( m_lwin
->GetLayoutList()), new wxLayoutPrintout( m_lwin
->GetLayoutList()), & printData
);
424 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
425 frame
->Centre(wxBOTH
);
430 void MyFrame::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
433 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
435 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
437 wxPrintDialog
printerDialog(this, & m_PrintData
);
438 printerDialog
.ShowModal();
441 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
444 wxGetApp().SetPrintMode(wxPRINT_WINDOWS
);
446 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
448 wxPageSetupData data
;
451 wxPageSetupDialog
pageSetupDialog(this, & data
);
453 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
455 pageSetupDialog
.ShowModal();
457 data
= pageSetupDialog
.GetPageSetupData();
460 void MyFrame::OnPrintSetupPS(wxCommandEvent
& WXUNUSED(event
))
462 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
467 wxPrintDialog
printerDialog(this, & data
);
469 wxGenericPrintDialog
printerDialog(this, & data
);
471 printerDialog
.ShowModal();
474 void MyFrame::OnPageSetupPS(wxCommandEvent
& WXUNUSED(event
))
476 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT
);
478 wxPageSetupData data
;
480 wxPageSetupDialog
pageSetupDialog(this, & data
);
482 wxGenericPageSetupDialog
pageSetupDialog(this, & data
);
484 pageSetupDialog
.ShowModal();
488 //-----------------------------------------------------------------------------
490 //-----------------------------------------------------------------------------
497 bool MyApp::OnInit(void)
499 wxFrame
*frame
= new MyFrame();
501 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");