]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
Drag'n'Drop works in a basic form
[wxWidgets.git] / user / wxLayout / wxLayout.cpp
1 /*
2 * Program: wxLayout
3 *
4 * Author: Karsten Ballüder
5 *
6 * Copyright: (C) 1998, Karsten Ballüder <Ballueder@usa.net>
7 *
8 */
9
10 #ifdef __GNUG__
11 #pragma implementation "wxLayout.h"
12 #endif
13
14 #include "wx/wxprec.h"
15 #ifdef __BORLANDC__
16 # pragma hdrstop
17 #endif
18
19 #include "wxLayout.h"
20 #include "wx/textfile.h"
21
22
23 #include "Micon.xpm"
24
25
26 //-----------------------------------------------------------------------------
27 // main program
28 //-----------------------------------------------------------------------------
29
30 IMPLEMENT_APP(MyApp)
31
32 //-----------------------------------------------------------------------------
33 // MyFrame
34 //-----------------------------------------------------------------------------
35
36 enum ids{ ID_ADD_SAMPLE = 1, ID_CLEAR, ID_PRINT,
37 ID_PRINT_SETUP, ID_PAGE_SETUP, ID_PREVIEW, ID_PRINT_PS,
38 ID_PRINT_SETUP_PS, ID_PAGE_SETUP_PS,ID_PREVIEW_PS,
39 ID_WRAP, ID_NOWRAP, ID_PASTE,
40 ID_WXLAYOUT_DEBUG, ID_QUIT, ID_CLICK, ID_HTML, ID_TEXT, ID_TEST };
41
42
43 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
44
45 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
46 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
47 EVT_MENU(ID_PREVIEW, MyFrame::OnPrintPreview)
48 EVT_MENU(ID_PRINT_SETUP, MyFrame::OnPrintSetup)
49 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
50 EVT_MENU(ID_PRINT_PS, MyFrame::OnPrintPS)
51 EVT_MENU(ID_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
52 EVT_MENU(ID_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
53 EVT_MENU(ID_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
54 EVT_MENU (-1, MyFrame::OnCommand)
55 EVT_COMMAND (-1,-1, MyFrame::OnCommand)
56 EVT_CHAR ( wxLayoutWindow::OnChar )
57 END_EVENT_TABLE()
58
59
60 MyFrame::MyFrame(void) :
61 wxFrame( (wxFrame *) NULL, -1, (char *) "wxLayout", wxPoint(20,20), wxSize(600,360) )
62 {
63 CreateStatusBar( 1 );
64
65 SetStatusText( "wxLayout by Karsten Ballüder." );
66
67 wxMenuBar *menu_bar = new wxMenuBar();
68
69 wxMenu *file_menu = new wxMenu;
70 file_menu->Append(ID_PRINT, "&Print...", "Print");
71 file_menu->Append(ID_PRINT_SETUP, "Print &Setup...","Setup printer properties");
72 file_menu->Append(ID_PAGE_SETUP, "Page Set&up...", "Page setup");
73 file_menu->Append(ID_PREVIEW, "Print Pre&view", "Preview");
74 #ifdef __WXMSW__
75 file_menu->AppendSeparator();
76 file_menu->Append(ID_PRINT_PS, "Print PostScript...", "Print (PostScript)");
77 file_menu->Append(ID_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
78 file_menu->Append(ID_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
79 file_menu->Append(ID_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
80 #endif
81 file_menu->AppendSeparator();
82 file_menu->Append( ID_TEXT, "Export Text");
83 file_menu->Append( ID_HTML, "Export HTML");
84 file_menu->Append( ID_QUIT, "Exit");
85 menu_bar->Append(file_menu, "File" );
86
87 wxMenu *edit_menu = new wxMenu;
88 edit_menu->Append( ID_CLEAR, "Clear");
89 edit_menu->Append( ID_ADD_SAMPLE, "Example");
90 edit_menu->AppendSeparator();
91 edit_menu->Append(ID_WRAP, "Wrap mode", "Activate wrapping at pixel 200.");
92 edit_menu->Append(ID_NOWRAP, "No-wrap mode", "Deactivate wrapping.");
93 edit_menu->AppendSeparator();
94 edit_menu->Append(ID_PASTE, "Paste", "Paste text from clipboard.");
95 menu_bar->Append(edit_menu, "Edit" );
96
97 #ifndef __WXMSW__
98 menu_bar->Show( TRUE );
99 #endif // MSW
100
101 SetMenuBar( menu_bar );
102
103 m_lwin = new wxLayoutWindow(this);
104 m_lwin->SetMouseTracking(true);
105 m_lwin->SetEditable(true);
106 m_lwin->SetWrapMargin(40);
107 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
108 m_lwin->SetFocus();
109 };
110
111 void
112 MyFrame::AddSampleText(wxLayoutList *llist)
113 {
114 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
115 llist->Insert("--");
116 llist->LineBreak();
117
118 llist->SetFont(wxROMAN);
119 llist->Insert("The quick brown fox jumps over the lazy dog.");
120 llist->LineBreak();
121 llist->Insert("Hello ");
122 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
123 llist->SetFontWeight(wxBOLD);
124 llist->Insert("World! ");
125 llist->SetFontWeight(wxNORMAL);
126 llist->Insert("The quick brown fox jumps...");
127 llist->LineBreak();
128
129 llist->Insert("over the lazy dog.");
130 llist->SetFont(-1,-1,-1,-1,true);
131 llist->Insert("underlined");
132 llist->SetFont(-1,-1,-1,-1,false);
133 llist->SetFont(wxROMAN);
134 llist->Insert("This is ");
135 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
136 llist->Insert("and ");
137 llist->SetFont(-1,-1,wxITALIC);
138 llist->Insert("italics ");
139 llist->SetFont(-1,-1,wxNORMAL);
140 llist->LineBreak();
141
142 llist->Insert("and ");
143 llist->SetFont(-1,-1,wxSLANT);
144 llist->Insert("slanted");
145 llist->SetFont(-1,-1,wxNORMAL);
146 llist->Insert(" text.");
147 llist->LineBreak();
148 llist->Insert("and ");
149 llist->SetFont(-1,-1,-1,-1,-1,"blue");
150 llist->Insert("blue");
151 llist->SetFont(-1,-1,-1,-1,-1,"black");
152 llist->Insert(" and ");
153 llist->SetFont(-1,-1,-1,-1,-1,"green","black");
154 llist->Insert("green on black");
155 llist->SetFont(-1,-1,-1,-1,-1,"black","white");
156 llist->Insert(" text.");
157 llist->LineBreak();
158
159 llist->SetFont(-1,-1,wxSLANT);
160 llist->Insert("Slanted");
161 llist->SetFont(-1,-1,wxNORMAL);
162 llist->Insert(" and normal text and ");
163 llist->SetFont(-1,-1,wxSLANT);
164 llist->Insert("slanted");
165 llist->SetFont(-1,-1,wxNORMAL);
166 llist->Insert(" again.");
167 llist->LineBreak();
168
169 // add some more text for testing:
170 llist->Insert("And here the source for the test program:");
171 llist->LineBreak();
172 llist->SetFont(wxTELETYPE,16);
173 char buffer[1024];
174 FILE *in = fopen("wxLayout.cpp","r");
175 if(in)
176 {
177 for(;;)
178 {
179 fgets(buffer,1024,in);
180 if(feof(in))
181 break;
182 llist->Insert(buffer);
183 llist->LineBreak();
184 }
185 }
186 llist->MoveCursorTo(wxPoint(0,0));
187 m_lwin->SetDirty();
188 m_lwin->Refresh();
189 }
190
191 void
192 MyFrame::Clear(void)
193 {
194 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
195 }
196
197
198 void MyFrame::OnCommand( wxCommandEvent &event )
199 {
200 switch (event.GetId())
201 {
202 case ID_QUIT:
203 Close( TRUE );
204 break;
205 case ID_PRINT:
206 {
207 wxPrinter printer;
208 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
209 if (! printer.Print(this, &printout, TRUE))
210 wxMessageBox(
211 _("There was a problem with printing the message:\n"
212 "perhaps your current printer is not set up correctly?"),
213 _("Printing"), wxOK);
214 }
215 break;
216 case ID_NOWRAP:
217 case ID_WRAP:
218 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
219 break;
220 case ID_ADD_SAMPLE:
221 AddSampleText(m_lwin->GetLayoutList());
222 break;
223 case ID_CLEAR:
224 Clear();
225 break;
226 case ID_CLICK:
227 cerr << "Received click event." << endl;
228 break;
229 case ID_PASTE:
230 m_lwin->Paste();
231 break;
232 case ID_HTML:
233 {
234 wxLayoutExportObject *export;
235 wxLayoutExportStatus status(m_lwin->GetLayoutList());
236
237 while((export = wxLayoutExport( &status,
238 WXLO_EXPORT_AS_HTML)) != NULL)
239 {
240 if(export->type == WXLO_EXPORT_HTML)
241 cout << *(export->content.text);
242 else
243 cout << "<!--UNKNOWN OBJECT>";
244 delete export;
245 }
246 }
247 break;
248 case ID_TEXT:
249 {
250 wxLayoutExportObject *export;
251 wxLayoutExportStatus status(m_lwin->GetLayoutList());
252
253 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
254 {
255 if(export->type == WXLO_EXPORT_TEXT)
256 cout << *(export->content.text);
257 else
258 cout << "<!--UNKNOWN OBJECT>";
259 delete export;
260 }
261 }
262 break;
263 }
264 };
265
266 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
267 {
268 #ifdef __WXMSW__
269 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
270 #else
271 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
272 #endif
273 wxPrinter printer;
274 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
275 if (! printer.Print(this, &printout, TRUE))
276 wxMessageBox(
277 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
278 "Printing", wxOK);
279 }
280
281 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
282 {
283 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
284
285 #ifdef OS_UNIX
286 wxPostScriptPrinter printer;
287 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
288 printer.Print(this, &printout, TRUE);
289 #endif
290 }
291
292 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
293 {
294 #ifdef __WXMSW__
295 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
296 #else
297 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
298 #endif
299 wxPrintData printData;
300
301 // Pass two printout objects: for preview, and possible printing.
302 wxPrintPreview *preview = new wxPrintPreview(new
303 wxLayoutPrintout(
304 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
305 if (!preview->Ok())
306 {
307 delete preview;
308 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
309 return;
310 }
311
312 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
313 frame->Centre(wxBOTH);
314 frame->Initialize();
315 frame->Show(TRUE);
316 }
317
318 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
319 {
320 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
321
322 wxPrintData printData;
323
324 // Pass two printout objects: for preview, and possible printing.
325 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
326 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
327 frame->Centre(wxBOTH);
328 frame->Initialize();
329 frame->Show(TRUE);
330 }
331
332 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
333 {
334 #ifdef OS_WIN
335 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
336 #else
337 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
338 #endif
339 wxPrintDialog printerDialog(this, & m_PrintData);
340 printerDialog.ShowModal();
341 }
342
343 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
344 {
345 #ifdef __WXMSW__
346 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
347 #else
348 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
349 #endif
350 wxPageSetupData data;
351
352 #ifdef __WXMSW__
353 wxPageSetupDialog pageSetupDialog(this, & data);
354 #else
355 wxGenericPageSetupDialog pageSetupDialog(this, & data);
356 #endif
357 pageSetupDialog.ShowModal();
358
359 data = pageSetupDialog.GetPageSetupData();
360 }
361
362 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
363 {
364 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
365
366 wxPrintData data;
367
368 #ifdef __WXMSW__
369 wxPrintDialog printerDialog(this, & data);
370 #else
371 wxGenericPrintDialog printerDialog(this, & data);
372 #endif
373 printerDialog.ShowModal();
374 }
375
376 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
377 {
378 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
379
380 wxPageSetupData data;
381 #ifdef __WXMSW__
382 wxPageSetupDialog pageSetupDialog(this, & data);
383 #else
384 wxGenericPageSetupDialog pageSetupDialog(this, & data);
385 #endif
386 pageSetupDialog.ShowModal();
387 }
388
389
390 //-----------------------------------------------------------------------------
391 // MyApp
392 //-----------------------------------------------------------------------------
393
394 MyApp::MyApp(void) :
395 wxApp( )
396 {
397 };
398
399 bool MyApp::OnInit(void)
400 {
401 wxFrame *frame = new MyFrame();
402 frame->Show( TRUE );
403 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
404 return TRUE;
405 };
406
407
408
409
410