1 // Sample code to demonstrate use of the Native Printer Code
2 //
3 // This pops up an MDI window, but the important part is not at all
4 // anything displayed - it is that there is a FILE/PRINT menu item
5 // and when activated it prints a sample page with lines and text.
6 //
7 
8 // This code came with FXDCNativePrinter.cpp. There have been
9 // slight adjustments to allow it to run with either FOX 1.0 or FOX 1.1
10 
11 // As originally supplied this files did not contain any copyright or
12 // author information. Its author (Manuel <address@te...> expained
13 // in an e-mail:
14 //
15 //"c) Including it in FOX: I havent put any copyright notice on the code so it
16 //  could be used for any purpose. If Jeroen wants to incorporate it or a
17 //  derivative work, it is fine with me."
18 //
19 // and on that basis it seems reasonable to re-circulate this slightly
20 // updated version as usable subject to anything Manuel wants to say but
21 // at least in line with the license terms of the main FOX toolkit.
22 
23 
24 
25 /* $Id: FXPrintSample.cpp 3297 2015-12-14 20:30:04Z arthurcnorman $ /*
26 
27 
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <signal.h>
34 
35 #include <fx.h>
36 
37 #include "FXDCNativePrinter.h"
38 
39 FXPrinter printer;
40 
41 #define CLOCKTIMER 500
42 
43 const unsigned char penguin[]={
44 0x47,0x49,0x46,0x38,0x37,0x61,0x10,0x00,0x12,0x00,0xf2,0x00,0x00,0xb2,0xc0,0xdc,
45 0x80,0x80,0x80,0x00,0x00,0x00,0xc0,0xc0,0xc0,0x10,0x10,0x10,0xff,0xff,0xff,0xe0,
46 0xa0,0x08,0x00,0x00,0x00,0x2c,0x00,0x00,0x00,0x00,0x10,0x00,0x12,0x00,0x00,0x03,
47 0x53,0x08,0xba,0x21,0x12,0x2b,0xc6,0xe6,0x9e,0x94,0x62,0x64,0x77,0xa3,0x20,0x4e,
48 0x21,0x74,0x8b,0x60,0x9c,0x1a,0xa9,0x98,0xa8,0x45,0xb2,0x85,0x38,0x76,0x4f,0x6c,
49 0xbb,0x93,0x60,0xdb,0x0d,0xe4,0xd9,0x83,0x1d,0xe7,0x57,0x18,0x04,0x6f,0xb8,0x4c,
50 0xec,0x88,0x9c,0x01,0x0c,0x47,0x66,0xac,0xa2,0x38,0x19,0x76,0x36,0x83,0xc3,0xf0,
51 0xb4,0x5e,0x77,0x03,0xaf,0xf8,0x7b,0x13,0x77,0xad,0xd3,0xad,0x75,0x61,0xa5,0x54,
52 0x02,0x27,0x45,0x02,0x00,0x3b
53 };
54 
55 
56 
57 //////////////////////////////////////////////////////////////////////////////////////////////
58 
59 // Mini application object
60 class FXAPI MainWindow : public FXMainWindow {
61   FXDECLARE(MainWindow)
62 
63 protected:
64   FXMenuBar         *menubar;
65   FXMDIClient       *mdiclient;               // MDI Client area
66   FXMDIMenu         *mdimenu;                 // MDI Window Menu
67   FXGIFIcon         *mdiicon;                 // MDI Icon
68   FXMenuPane        *filemenu;
69   FXMenuPane        *windowmenu;
70   FXMenuPane        *editmenu;
71   FXMenuPane        *popupmenu;               // Context menu
72 
73   FXToolBarShell    *dragshell1,*dragshell2;
74   FXToolBar         *toolbar;
75   FXStatusBar       *statusbar;
76 
77   FXMenuPane        *helpmenu;
78   FXFont            *font;
79 
80 protected:
81   MainWindow(){}
82 
83 public:
84 
85   // We define ID's, starting from the last one used by the base class+1.
86   // This way, we know the ID's are all unique for this particular target.
87   enum {
88     ID_ABOUT=FXMainWindow::ID_LAST,
89     ID_NEW,
90     ID_SAVE,
91     ID_OPEN,
92     ID_CLOSE,
93     ID_QUIT,
94     ID_PRINTSETUP,
95     ID_PRINT,
96     ID_LAST
97     };
98 
99   // message handlers
100   long onCmdAbout(FXObject*,FXSelector,void*);
101   long onCmdNew(FXObject*,FXSelector,void*);
102   long onCmdSave(FXObject*,FXSelector,void*);
103   long onCmdOpen(FXObject*,FXSelector,void*);
104   long onCmdClose(FXObject*,FXSelector,void*);
105   long onCmdQuit(FXObject*,FXSelector,void*);
106   long onCmdPrintSetup(FXObject*,FXSelector,void*);
107   long onCmdPrint(FXObject*,FXSelector,void*);
108 public:
109   MainWindow(FXApp* a);
110   virtual void create();
111   virtual ~MainWindow();
112   };
113 
114 
115 // Map
116 FXDEFMAP(MainWindow) MainWindowMap[]={
117   //________Message_Type____________ID___________________Message_Handler________
118   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_ABOUT,     MainWindow::onCmdAbout),
119   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_NEW,       MainWindow::onCmdNew),
120   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_SAVE,      MainWindow::onCmdSave),
121   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_OPEN,      MainWindow::onCmdOpen),
122   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_PRINTSETUP,MainWindow::onCmdPrintSetup),
123   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_PRINT,     MainWindow::onCmdPrint),
124   FXMAPFUNC(SEL_SIGNAL,   MainWindow::ID_QUIT,      MainWindow::onCmdQuit),
125   FXMAPFUNC(SEL_CLOSE,    0,                        MainWindow::onCmdQuit),
126   FXMAPFUNC(SEL_COMMAND,  MainWindow::ID_QUIT,      MainWindow::onCmdQuit),
127   };
128 
129 
130 // Object implementation
131 FXIMPLEMENT(MainWindow,FXMainWindow,MainWindowMap,ARRAYNUMBER(MainWindowMap))
132 
133 
134 /*******************************************************************************/
135 
136 // Start the whole thing
main(int argc,char * argv[])137 int main(int argc,char *argv[]){
138 
139   // Make application
140   FXApp application("MDI skeleton","MDI skeleton");
141 
142   // Open display
143   application.init(argc,argv);
144 
145   // Make window
146   MainWindow *w=new MainWindow(&application);
147 
148   // Create app
149   application.create();
150   w->setFocus();
151 
152   // Run
153   return application.run();
154   }
155 
156 
157 // Make some windows
MainWindow(FXApp * a)158 MainWindow::MainWindow(FXApp* a):
159     FXMainWindow(a,"MDI Widget Test",NULL,NULL,DECOR_ALL,0,0,800,600){
160   FXMDIChild *mdichild;
161   FXScrollWindow *scrollwindow;
162   FXButton *btn;
163 
164   // Initialize printer selection with default values
165   {
166   FXPrintDialog dlg(this,"");
167     // Do NOT show dialog. Just get its default printer
168     dlg.getPrinter( printer );
169   }
170 
171   // Make menu bar
172   FXToolBarShell* dragshell1=new FXToolBarShell(this,FRAME_RAISED|FRAME_THICK);
173   menubar=new FXMenuBar(this,dragshell1,LAYOUT_SIDE_TOP|LAYOUT_FILL_X);
174   new FXToolBarGrip(menubar,menubar,FXMenuBar::ID_TOOLBARGRIP,
175                     TOOLBARGRIP_DOUBLE);
176 
177   // Tool bar
178   FXToolBarShell* dragshell2=new FXToolBarShell(this,FRAME_RAISED|FRAME_THICK);
179   toolbar=new FXToolBar(this,dragshell2,
180       LAYOUT_SIDE_TOP|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH|PACK_UNIFORM_HEIGHT);
181   new FXToolBarGrip(toolbar,toolbar,
182                     FXToolBar::ID_TOOLBARGRIP,TOOLBARGRIP_DOUBLE);
183 
184 
185   // Status bar
186   statusbar=new FXStatusBar(this,
187       LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER);
188 
189   // MDI Client
190   mdiclient=new FXMDIClient(this,LAYOUT_FILL_X|LAYOUT_FILL_Y);
191 
192   // Icon for MDI Child
193   mdiicon=new FXGIFIcon(getApp(),penguin);
194 
195 
196   // Make MDI Menu
197   mdimenu=new FXMDIMenu(this,mdiclient);
198 
199   // MDI buttons in menu:- note the message ID's!!!!!
200   // Normally, MDI commands are simply sensitized or desensitized;
201   // Under the menubar, however, they're hidden if the MDI Client is
202   // not maximized.  To do this, they must have different ID's.
203   new FXMDIWindowButton(menubar,NULL,mdiclient,
204          FXMDIClient::ID_MDI_MENUWINDOW,LAYOUT_LEFT);
205   new FXMDIDeleteButton(menubar,mdiclient,
206          FXMDIClient::ID_MDI_MENUCLOSE,FRAME_RAISED|LAYOUT_RIGHT);
207   new FXMDIRestoreButton(menubar,mdiclient,
208          FXMDIClient::ID_MDI_MENURESTORE,FRAME_RAISED|LAYOUT_RIGHT);
209   new FXMDIMinimizeButton(menubar,mdiclient,
210          FXMDIClient::ID_MDI_MENUMINIMIZE,FRAME_RAISED|LAYOUT_RIGHT);
211 
212   // File menu
213   filemenu=new FXMenuPane(this);
214   new FXMenuCommand(filemenu,"&New\tCtl-N\tNew document",NULL,this,ID_NEW);
215   new FXMenuCommand(filemenu,"&Open\tCtl-O\tOpen document",NULL,this,ID_OPEN);
216   new FXMenuCommand(filemenu,"&Close\t\tClose document",NULL,
217                     mdiclient,FXMDIClient::ID_MDI_CLOSE);
218   new FXMenuSeparator(filemenu);
219   new FXMenuCommand(filemenu,"&Print setup\t\tSelect printer and paper",NULL,
220                     this,ID_PRINTSETUP);
221   new FXMenuCommand(filemenu,"&Print\t\tPrint document",NULL,this,ID_PRINT);
222   new FXMenuSeparator(filemenu);
223 
224   new FXMenuCommand(filemenu,"&Exit\tAlt-F4\tGo pickup some girls",NULL,
225                     this,ID_QUIT,0);
226   new FXMenuTitle(menubar,"&Files",NULL,filemenu);
227 
228   // Window menu
229   windowmenu=new FXMenuPane(this);
230   new FXMenuCommand(windowmenu,"Tile &Horizontally",NULL,
231       mdiclient,FXMDIClient::ID_MDI_TILEHORIZONTAL);
232   new FXMenuCommand(windowmenu,"Tile &Vertically",NULL,
233       mdiclient,FXMDIClient::ID_MDI_TILEVERTICAL);
234   new FXMenuCommand(windowmenu,"C&ascade",NULL,
235       mdiclient,FXMDIClient::ID_MDI_CASCADE);
236   new FXMenuCommand(windowmenu,"&Close",NULL,
237       mdiclient,FXMDIClient::ID_MDI_CLOSE);
238 //
239 // The next lines were in the sample app but I can not find
240 // ID_CLOSE_ALL_DOCUMENTS anywhere in the FOX header files...
241 //
242 
243 //new FXMenuCommand(windowmenu,"Clo&se All",NULL,
244 //    mdiclient,FXMDIClient::ID_CLOSE_ALL_DOCUMENTS);
245   FXMenuSeparator* sep1=new FXMenuSeparator(windowmenu);
246   sep1->setTarget(mdiclient);
247   sep1->setSelector(FXMDIClient::ID_MDI_ANY);
248   new FXMenuCommand(windowmenu,NULL,NULL,mdiclient,FXMDIClient::ID_MDI_1);
249   new FXMenuCommand(windowmenu,NULL,NULL,mdiclient,FXMDIClient::ID_MDI_2);
250   new FXMenuCommand(windowmenu,NULL,NULL,mdiclient,FXMDIClient::ID_MDI_3);
251   new FXMenuCommand(windowmenu,NULL,NULL,mdiclient,FXMDIClient::ID_MDI_4);
252   new FXMenuTitle(menubar,"&Windows",NULL,windowmenu);
253 
254   // Help menu
255   helpmenu=new FXMenuPane(this);
256   new FXMenuCommand(helpmenu,"&About...",NULL,this,ID_ABOUT,0);
257   new FXMenuTitle(menubar,"&Help",NULL,helpmenu,LAYOUT_RIGHT);
258   getApp()->addSignal(SIGINT,mdiclient,MainWindow::ID_QUIT);
259 
260 
261   // Font
262 #if (FOX_MINOR<=4)
263   font = new FXFont(getApp(), "", 10,  FONTWEIGHT_NORMAL, FONTSLANT_REGULAR,
264       FONTENCODING_DEFAULT, FONTSETWIDTH_DONTCARE, FONTPITCH_FIXED);
265 #else
266   font = new FXFont(getApp(), "", 10,  FXFont::Normal, FXFont::Straight,
267       FONTENCODING_DEFAULT, 0, FXFont::Fixed);
268 #endif
269 
270   // Right-click popup menu
271   popupmenu=new FXMenuPane(this);
272   new FXMenuCommand(popupmenu,"Test",NULL,this,ID_QUIT);
273 
274   //Fill toolbar
275    new FXButton(toolbar,"Open\t\tOpen new document",mdiicon,
276       this,ID_NEW,
277       ICON_ABOVE_TEXT|BUTTON_TOOLBAR|FRAME_RAISED|LAYOUT_TOP|LAYOUT_LEFT);
278   }
279 
280 
281 // Clean up
~MainWindow()282 MainWindow::~MainWindow(){
283   delete helpmenu;
284   delete windowmenu;
285   delete filemenu;
286   delete font;
287   delete popupmenu;
288   }
289 
290 // Start
create()291 void MainWindow::create(){
292   FXMainWindow::create();
293   show(PLACEMENT_SCREEN);
294   }
295 
296 
297 // About
onCmdAbout(FXObject *,FXSelector,void *)298 long MainWindow::onCmdAbout(FXObject*,FXSelector,void*){
299   FXMessageBox::information(this,MBOX_OK,"About", "Sample App");
300   return 1;
301   }
302 
onCmdNew(FXObject *,FXSelector,void *)303 long MainWindow::onCmdNew(FXObject*,FXSelector,void*)
304 {
305   return 1;
306 }
307 
onCmdSave(FXObject *,FXSelector,void *)308 long MainWindow::onCmdSave(FXObject*,FXSelector,void*)
309 {
310   return 1;
311 }
312 
onCmdOpen(FXObject *,FXSelector,void *)313 long MainWindow::onCmdOpen(FXObject*,FXSelector,void*)
314 {
315   return 1;
316 }
317 
onCmdClose(FXObject *,FXSelector,void *)318 long MainWindow::onCmdClose(FXObject*,FXSelector,void*)
319 {
320   return 1;
321 }
322 
onCmdQuit(FXObject * o,FXSelector sel,void * ptr)323 long MainWindow::onCmdQuit(FXObject*o,FXSelector sel,void*ptr)
324 {
325   mdiclient->forallWindows(this,MKUINT(0,SEL_CLOSE),0);
326   if  ( ((sel>>16)&0xFFFF)==SEL_CLOSE  )
327   {
328     FXTopWindow::handle(o,sel,ptr);
329   }
330   getApp()->handle(this, MKUINT(FXApp::ID_QUIT,SEL_COMMAND),ptr);
331 
332   return 1;
333 }
334 
onCmdPrintSetup(FXObject *,FXSelector,void *)335 long MainWindow::onCmdPrintSetup(FXObject*,FXSelector,void*)
336 {
337     FXPrintDialog dlg(this,"");
338     dlg.setPrinter( printer );  // Use last printer selection
339     dlg.execute();
340     dlg.getPrinter( printer );
341 
342     return 1;
343 }
344 
onCmdPrint(FXObject *,FXSelector,void *)345 long MainWindow::onCmdPrint(FXObject*,FXSelector,void*)
346 {
347 FXDCNativePrinter prt(getApp());
348 FXFont *font10, *font16;
349 FXint w,h;
350 char string[]="Hello world!";
351 
352     prt.beginPrint(printer);
353     font10 = prt.fntGenerateFont("helvetica", 10);
354     font10->create();
355     font16 = prt.fntGenerateFont("helvetica", 16);
356     font16->create();
357     prt.beginPage(1);
358 
359     /* I want to use tenths of millimeter (2.54 cm per inch) */
360     prt.setHorzUnitsInch( 254 );
361     prt.setVertUnitsInch( 254 );
362 
363     prt.setForeground( FXRGB(0,0,0) );
364 
365     /* WARNING: The concept of border is subject to change. Right now, it is
366     ** not clear if the border is the physical paper border, or the printable
367     ** area.
368     ** The later is printer-dependant .
369     ** Paper size (width x height) is the physical paper size
370     */
371 
372 
373 
374     /* Draw a frame around the page, 2 cm from each border of the paper.
375     ** This only takes into account your selected paper size, regardles of
376     **  the actual physical size of the paper you feed to the printer.
377     */
378     prt.drawRectangle(200,200,prt.getPageWidth()-400,prt.getPageHeight()-400);
379 
380     /* Draw a frame exactly at page boundaries */
381     prt.drawRectangle( 0,0, prt.getPageWidth()-1, prt.getPageHeight()-1 );
382 
383     /* draw a 10cm horizontal line 3cm from the top-left border */
384     prt.drawLine(300,300, 1300,300 );
385     /* Put some text over the line */
386     prt.setTextFont( font10 );
387     prt.drawText(300,300, string, strlen(string) );
388 
389     /* draw another line 2cm below the other */
390     prt.drawLine(300,500, 1300,500 );
391     /* Put some text over the line */
392     prt.setTextFont( font16 );
393     prt.drawText(300,500, string, strlen(string) );
394 
395     /* Draw some aligned text */
396     prt.drawLine(1000, 150, 1000, 1700 );
397     w=prt.fntGetTextWidth( string, strlen(string) );
398     prt.drawText( 1000, 1200, string, strlen(string) );  /* Right */
399     prt.drawText( 1000-w, 1300, string, strlen(string) );  /* Left */
400     prt.drawText( 1000-(w/2), 1400, string, strlen(string) );  /* Centre */
401 
402     /* Now, I want to draw using 100th of inch as drawing unit */
403     prt.setHorzUnitsInch( 100 );
404     prt.setVertUnitsInch( 100 );
405 
406     /* Draw a box with sides 1 inch long */
407     prt.setForeground( FXRGB(255,0,0) ); /* Red box */
408     prt.drawRectangle( 300, 300, 100, 100 );  /* 3 inches from the margin */
409 
410     prt.endPage();
411     prt.endPrint();
412     return 1;
413 }
414 
415 // end of printing sample code.
416 
417