[Cpri-develop] cpri/examples 1_pri.c,NONE,1.1 ex.c,1.10,NONE
Brought to you by:
chrisan,
rasmusmyklebust
|
From: <ch...@us...> - 2004-02-08 11:38:58
|
Update of /cvsroot/cpri/cpri/examples In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18127/examples Added Files: 1_pri.c Removed Files: ex.c Log Message: Some more makefile cleanup/improvements --- NEW FILE: 1_pri.c --- /* Simple example program that prints a rectangle and some text. */ #include<allegro.h> #include <cgui.h> #include "cpri.h" #define STRMAX 50 #define NROWS 38 #define MAXCHARS 50 /* Just to have something to print: */ static char *names[NROWS]={"Jan","Habib","Ingela","Cecilia","Anders","Anna","Kenneth","Irina","Christer","Gert","Eva","Inezia","Gottfrid","Carin","Erik","Lennart","Louise","Katarina","John","Bettan","Kristina","Gunilla","Ernst","Madara","Jonas","Henrik","Johan","Bengt","Ingvar","Carl-Oskar","Karin","Fridjon","Gertrud","Jing","Helena","Joop","Ashafir","Birgitta"}; static int points[NROWS]={2638922,2565390,932183,740937,531658,463894,181237,135953,124439,124294,123616,112878,111848,107920,91055,51227,46144,40815,40356,37179,36336,34813,33270,25256,25153,20121,17096,17000,14950,4647,4296,3357,1353,755,725,601,182,18}; typedef struct t_target { int dest; char name[MAXCHARS+1]; } t_target; typedef struct t_freecfg { int fontsize; int font; int stroke; int style; int boxx; int boxy; int boxw; int boxh; int barcx; int barcy; t_target *t; } t_freecfg; typedef struct t_diagramcfg { int fontsize; int font; int stroke; int style; int h; int rh; int w; int pattern; int grey; int embedded; t_target *t; } t_diagramcfg; typedef struct t_header { char text[STRMAX]; int fontsize; int font; int stroke; int style; int outline; int repeat; } t_header; typedef struct t_table_header { int fontsize; int font; int stroke; int style; int outline; int repeat; } t_table_header; typedef struct t_table { int fontsize; int font; int stroke; int style; int outline; } t_table; typedef struct t_label { char text[STRMAX]; int join; } t_label; typedef struct t_column { t_label label; int w; int stroke; int style; int i; } t_column; typedef struct t_tabcfg { int x; int y; int bottom_margin; int lineheight; t_table table; t_header header; t_table_header table_header; int nrcols; t_column column[5]; int idcont; t_target *t; } t_tabcfg; static void quit(void *data nouse) { if (CpriIsSpooling()) if (Req("Warning!", "CPRI is still processing a print out. Exitting now may give an incomplete printout.|E~xit|Keep processing")==1) return; StopProcessEvents(); } static void show_queue(void *data nouse) { CpriPrinterQueueMonitor(); } static void show_settings(void *data nouse) { CpriPrinterPortDialogue(); } static void print_freepos_sample(void *data) { t_freecfg *pr = data; t_handle hnd; if (pr->t->dest) hnd = CpriOpenEntry(0, "Print sample", 0, pr->t->name); else hnd = CpriOpenEntry(0, "Print sample", 0, ""); if (hnd==NULL) { Req("Error!", "Print error| ~OK "); return; } CpriCommand(hnd, CPRI_PORTRAIT); CpriCommand(hnd, CPRI_CASETTE_FEED); CpriCommand(hnd, CPRI_END_START_SEQUENCE); /* Prepare font */ CpriCommand(hnd, CPRI_FONT_SIZE, pr->fontsize); CpriCommand(hnd, CPRI_FONT_TYPEFACE, pr->font, pr->stroke, pr->style); /* We print a simple box with a text in. The upper left corner of the box will be printed at current position. After the box has been printed the current position has advanced to the upper right corner. When printing text, the text will be printed at current position but using the base line of characters as reference point of the printing. So if the current y-position is e.g. 100 the major part of the text will be drawn ABOVE 100 and a minor part below. You have to take that in accout whe printing i free position mode. Therefore: if you need to print text in cells in a tabular way, it will be a lot easier to use the tabular mode, in which case the driver will carry such platform dependent details. */ CpriCommand(hnd, CPRI_ABS_MOVEX, pr->boxx); CpriCommand(hnd, CPRI_ABS_MOVEY, pr->boxy); CpriCommand(hnd, CPRI_RECTANGLE, pr->boxw, pr->boxh); CpriCommand(hnd, CPRI_ABS_MOVEX, pr->boxx + 2); CpriCommand(hnd, CPRI_REL_MOVEY, pr->fontsize*2/9 + 2); CpriText(hnd, "Hello folks"); /* We also like to print a barcode at some other location. If you like to really make the code acceptable you have to either change this example so that it ends with a proper check sum digit or re-program the barcode reader for "no checksum when reading interleaved 2/5 codes", which normally can be done with all barcode readers - see the manual. */ CpriCommand(hnd, CPRI_ABS_MOVEX, pr->barcx); CpriCommand(hnd, CPRI_ABS_MOVEY, pr->barcy); CpriCommand(hnd, CPRI_BAR_CODE_W, 15); CpriCommand(hnd, CPRI_BAR_CODE, "0123456789"); CpriCloseEntry(hnd); } static void show_freepos_sample(void *data) { static t_freecfg prcfg={14, UNIVERS, 1, 1, 40, 40, 60, 7, 100, 100, NULL}; t_freecfg *pr; pr = &prcfg; pr->t = data; CpriGetTypefaceNames(); MkDialogue(ADAPTIVE, "Specify free position print out", 0); StartContainer(RIGHT, ADAPTIVE, "Font properties", CT_BORDER); AddEditBox(DOWNLEFT, 50, "Size", FINT, 300, &pr->fontsize); AddDropDownS(DOWNLEFT, 0, "Typeface", &pr->font, CpriGetTypefaceNames(), NOTYPEFACES); AddCheck(DOWNLEFT, "Bold", &pr->stroke); AddCheck(DOWNLEFT, "Italic", &pr->style); EndContainer(); StartContainer(RIGHT, ADAPTIVE, "Text box", CT_BORDER); AddEditBox(DOWNLEFT, 50, "X-position", FINT, 10, &pr->boxx); AddEditBox(DOWNLEFT, 50, "Y-position", FINT, 10, &pr->boxy); AddEditBox(DOWNLEFT, 50, "Width", FINT, 10, &pr->boxw); AddEditBox(DOWNLEFT, 50, "Height", FINT, 10, &pr->boxh); EndContainer(); StartContainer(RIGHT, ADAPTIVE, "Bar code", CT_BORDER); AddEditBox(DOWNLEFT, 50, "X-position", FINT, 10, &pr->barcx); AddEditBox(DOWNLEFT, 50, "Y-position", FINT, 10, &pr->barcy); EndContainer(); AddButton(DOWNLEFT, "\33Close", CloseWin, NULL); AddButton(RIGHT|ALIGNRIGHT, "Print", print_freepos_sample, pr); DisplayWin(); } static void print_diagram_sample(void *data) { t_diagramcfg *pr = data; int row; t_handle hnd; char s[1000]; if (pr->t->dest) hnd = CpriOpenEntry(0, "Print diagram sample", 0, pr->t->name); else hnd = CpriOpenEntry(0, "Print diagram sample", 0, ""); if (hnd==NULL) { Req("Error!", "Print error| ~OK "); return; } CpriCommand(hnd, CPRI_PORTRAIT); CpriCommand(hnd, CPRI_CASETTE_FEED); CpriCommand(hnd, CPRI_END_START_SEQUENCE); /* Set up table */ CpriCommand(hnd, CPRI_TABLE, 10, 10, 10); CpriCommand(hnd, CPRI_FIX_ROW_HEIGHT, pr->rh); CpriCommand(hnd, CPRI_CREATE_COLUMN, 200); /* Set up font */ CpriCommand(hnd, CPRI_FONT_SIZE, pr->fontsize); CpriCommand(hnd, CPRI_FONT_TYPEFACE, pr->font, pr->stroke, pr->style); /* Draw rows */ for (row = 0; row < 10; row++) { CpriCommand(hnd, CPRI_MOVE_TO_NEXT_COLUMN); CpriCommand(hnd, CPRI_HORIZONTAL_BAR, pr->w * points[row] / points[0], pr->h, pr->pattern, pr->grey, pr->embedded); sprintf(s, "%s (%d)", names[row], points[row]); CpriText(hnd, s); CpriCommand(hnd, CPRI_END_BAR_LABEL); CpriCommand(hnd, CPRI_NEW_LINE); } CpriCommand(hnd, CPRI_END_TABLE); CpriCloseEntry(hnd); } static void show_diagram_sample(void *data) { static t_diagramcfg prcfg={14, UNIVERS, 0, 0, 10, 12, 180, 6, 30, 1, NULL}; t_diagramcfg *pr; cgui_drop_down_list_row_spacing = 4; pr = &prcfg; pr->t = data; MkDialogue(ADAPTIVE, "Specify diagram print out", 0); StartContainer(RIGHT, ADAPTIVE, "Font properties", CT_BORDER); AddEditBox(DOWNLEFT, 50, "Size", FINT, 300, &pr->fontsize); AddDropDownS(DOWNLEFT, 0, "Typeface", &pr->font, CpriGetTypefaceNames(), NOTYPEFACES); AddCheck(DOWNLEFT, "Bold", &pr->stroke); AddCheck(DOWNLEFT, "Italic", &pr->style); EndContainer(); StartContainer(RIGHT, ADAPTIVE, "Diagram bars", CT_BORDER); AddEditBox(DOWNLEFT, 50, "Height", FINT, 10, &pr->h); AddEditBox(DOWNLEFT, 50, "Row height", FINT, 10, &pr->rh); AddEditBox(DOWNLEFT, 50, "Max width", FINT, 10, &pr->w); AddDropDownS(DOWNLEFT, 0, "Pattern type", &pr->pattern, CpriGetBarIcons(), BAR_PATTERN_NR); AddEditBox(DOWNLEFT, 50, "Grey tone", FINT, 10, &pr->grey); AddTag(RIGHT, "%"); AddCheck(DOWNLEFT, "Embedded labels", &pr->embedded); EndContainer(); AddButton(DOWNLEFT, "\33Close", CloseWin, NULL); AddButton(RIGHT|ALIGNRIGHT, "Print", print_diagram_sample, pr); DisplayWin(); } static void print_tabular_sample(void *data) { t_tabcfg *pr = data; t_header *hdr; t_table_header *thdr; t_handle hnd; int i, row; char s[1000]; hdr = &pr->header; thdr = &pr->table_header; if (pr->t->dest) hnd = CpriOpenEntry(QUEUE_OPTIMALPORT, "Print table sample", 0, pr->t->name); else hnd = CpriOpenEntry(QUEUE_OPTIMALPORT, "Print table sample", 0, ""); if (hnd==NULL) { Req("Error!", "Print error| ~OK "); return; } CpriCommand(hnd, CPRI_PORTRAIT); CpriCommand(hnd, CPRI_CASETTE_FEED); CpriCommand(hnd, CPRI_END_START_SEQUENCE); CpriCommand(hnd, CPRI_TABLE, pr->x, pr->y, pr->bottom_margin); CpriCommand(hnd, CPRI_LINE_HEIGHT, pr->lineheight); for (i=0; i<pr->nrcols+2; i++) CpriCommand(hnd, CPRI_CREATE_COLUMN, pr->column[i].w); if (hdr->outline) CpriCommand(hnd, CPRI_START_GRID); if (hdr->repeat) CpriCommand(hnd, CPRI_BEGIN_TO_BE_REPEATED); CpriCommand(hnd, CPRI_FONT_SIZE, hdr->fontsize); CpriCommand(hnd, CPRI_FONT_TYPEFACE, hdr->font, hdr->stroke, hdr->style); CpriCommand(hnd, CPRI_MERGE_ALL_COLUMNS); CpriCommand(hnd, CPRI_MOVE_TO_NEXT_COLUMN); CpriCommand(hnd, CPRI_ALIGN,ALIGN_CENTER,hdr->text); CpriCommand(hnd, CPRI_NEW_LINE); CpriCommand(hnd, CPRI_ACTIVATE_COLUMNS); if (hdr->repeat && !thdr->repeat) CpriCommand(hnd, CPRI_END_TO_BE_REPEATED); if (thdr->outline) CpriCommand(hnd, CPRI_START_GRID); if (thdr->repeat && !hdr->repeat) CpriCommand(hnd, CPRI_BEGIN_TO_BE_REPEATED); CpriCommand(hnd, CPRI_FONT_SIZE, thdr->fontsize); CpriCommand(hnd, CPRI_FONT_TYPEFACE, thdr->font, thdr->stroke, thdr->style); for (i=1; i<pr->nrcols+2; i++) if (pr->column[i].label.join) CpriCommand(hnd, CPRI_DEACTIVATE_COLUMN, i); for (i=0; i<pr->nrcols+2; i++) { if (!pr->column[i].label.join) CpriCommand(hnd, CPRI_MOVE_TO_NEXT_COLUMN); CpriCommand(hnd,CPRI_ALIGN,ALIGN_CENTER,pr->column[i].label.text); } CpriCommand(hnd, CPRI_NEW_LINE); CpriCommand(hnd, CPRI_ACTIVATE_COLUMNS); if (thdr->repeat) CpriCommand(hnd, CPRI_END_TO_BE_REPEATED); if (pr->table.outline) CpriCommand(hnd, CPRI_START_GRID); CpriCommand(hnd, CPRI_FONT_SIZE, pr->table.fontsize); CpriCommand(hnd, CPRI_FONT_TYPEFACE, pr->table.font, pr->table.stroke, pr->table.style); for (row = 0; row < NROWS; row++) { for (i=0; i<pr->nrcols+2; i++) { CpriCommand(hnd, CPRI_MOVE_TO_NEXT_COLUMN); CpriCommand(hnd, CPRI_FONT_TYPEFACE, pr->table.font, pr->column[i].stroke, pr->column[i].style); switch (i) { case 0: sprintf(s, "%d", row+1); CpriCommand(hnd, CPRI_ALIGN, ALIGN_RIGHT, s); break; case 1: sprintf(s, "%s", names[row]); CpriText(hnd, s); break; case 2: sprintf(s, "%d", points[row]); CpriCommand(hnd, CPRI_ALIGN, ALIGN_RIGHT, s); break; default: sprintf(s, "asdf"); CpriText(hnd, s); } } CpriCommand(hnd, CPRI_NEW_LINE); } CpriCommand(hnd, CPRI_END_TABLE); CpriCloseEntry(hnd); } static void make_column_tab(void *data, int id nouse) { t_column *col = data; t_label *lab; lab = &col->label; StartContainer(TOPLEFT, ADAPTIVE, "Label", CT_BORDER); AddEditBox(DOWNLEFT, 200, "Text", FSTRING, STRMAX, lab->text); if (col->i > 0) AddCheck(DOWNLEFT,"Join left cell", &lab->join); EndContainer(); StartContainer(RIGHT, ADAPTIVE, "Column", CT_BORDER); AddEditBox(TOPLEFT, 50, "Width", FINT, 10, &col->w); AddCheck(DOWNLEFT,"Bold column", &col->stroke); AddCheck(DOWNLEFT,"Italic column", &col->style); EndContainer(); } static void create_tabs(t_tabcfg *pr) { int i, id; static int tabsel; id = CreateTabWindow(DOWNLEFT, ADAPTIVE, &tabsel); for (i=0; i<pr->nrcols+2; i++) { char s[100]; sprintf(s, "Column %d", i+1); pr->column[i].i = i; AddTab(id, make_column_tab, pr->column + i, s); } } static void updated_nr(void *data) { t_tabcfg *pr = data; EmptyContainer(pr->idcont); SelectContainer(pr->idcont); create_tabs(pr); ReBuildContainer(pr->idcont); } static void show_tabular_sample(void *data) { static t_tabcfg prcfg={ /* General table info */ 20, 30, 50, 130, /* Table body */ {14, TIMES, 0, 0, 1}, /* Page header: */ {"High score list", 24, TIMES, 0, 0, 1, 0}, /* Table header: */ {16, TIMES, 1, 0, 1, 1}, /* Column specification: */ 1,{ {{"", 0}, 8, 0, 0, 0}, /* Col 1 (place) */ {{"The players", 1}, 100, 0, 0, 0}, /* Col 2 (name) */ {{"Points", 0}, 20, 0, 0, 0}, /* Col 3 (points) */ {{"", 0}, 10, 0, 0, 0}, /* Col 4 (n.u.) */ {{"", 0}, 10, 0, 0, 0} /* Col 5 (n.u.) */ }, 0, NULL}; static const char *const cols[]={"2", "3", "4", "5"}; int idn; t_tabcfg *pr; t_header *hdr; t_table_header *thdr; t_table *tab; pr = &prcfg; pr->t = data; hdr = &pr->header; thdr = &pr->table_header; tab = &pr->table; MkDialogue(ADAPTIVE, "Specify table print out", 0); AutoHotKeys(0); StartContainer(TOPLEFT, ADAPTIVE, "", 0); StartContainer(TOPLEFT, ADAPTIVE, "General", CT_BORDER); AddEditBox(DOWNLEFT, 50, "Start x", FINT, 10, &pr->x); AddEditBox(RIGHT, 50, "Start y", FINT, 10, &pr->y); AddEditBox(RIGHT, 50, "Bottom margin", FINT, 10, &pr->bottom_margin); AddEditBox(DOWNLEFT, 50, "Relative row height", FINT, 10, &pr->lineheight); AddTag(RIGHT, "%"); idn = AddDropDownS(DOWNLEFT, 0, "Number of columns", &pr->nrcols, cols, 4); AddHandler(idn, updated_nr, pr); EndContainer(); pr->idcont = StartContainer(DOWNLEFT, ADAPTIVE, "", 0); create_tabs(pr); EndContainer(); EndContainer(); StartContainer(RIGHT, ADAPTIVE, "", 0); StartContainer(TOPLEFT, ADAPTIVE, "Page header", CT_BORDER); AddEditBox(DOWNLEFT, 200, "Text", FSTRING, STRMAX, hdr->text); AddEditBox(DOWNLEFT, 50, "Size", FINT, 300, &hdr->fontsize); AddDropDownS(DOWNLEFT, 0, "Typeface", &hdr->font, CpriGetTypefaceNames(), NOTYPEFACES); AddCheck(DOWNLEFT, "Outline", &hdr->outline); AddCheck(DOWNLEFT, "Bold", &hdr->stroke); AddCheck(RIGHT, "Italic", &hdr->style); AddCheck(DOWNLEFT, "Repeat", &hdr->repeat); EndContainer(); StartContainer(DOWNLEFT, ADAPTIVE, "Table header", CT_BORDER); AddEditBox(DOWNLEFT, 50, "Size", FINT, 300, &thdr->fontsize); AddDropDownS(DOWNLEFT, 0, "Typeface", &thdr->font, CpriGetTypefaceNames(), NOTYPEFACES); AddCheck(DOWNLEFT,"Bold", &thdr->stroke); AddCheck(RIGHT,"Italic", &thdr->style); AddCheck(DOWNLEFT,"Outlined", &thdr->outline); AddCheck(DOWNLEFT,"Repeat", &thdr->repeat); EndContainer(); StartContainer(DOWNLEFT, ADAPTIVE, "Table body", CT_BORDER); AddEditBox(DOWNLEFT, 50, "Size", FINT, 300, &tab->fontsize); AddDropDownS(DOWNLEFT, 0, "Typeface", &tab->font, CpriGetTypefaceNames(), NOTYPEFACES); AddCheck(DOWNLEFT, "Outline", &tab->outline); AddCheck(DOWNLEFT, "Bold", &tab->stroke); AddCheck(RIGHT, "Italic", &tab->style); EndContainer(); EndContainer(); AddButton(DOWNLEFT, "\33Close", CloseWin, NULL); AddButton(RIGHT|ALIGNRIGHT, "~Print", print_tabular_sample, pr); AutoHotKeys(1); SetWindowPosition(5, 5); DisplayWin(); } static void choose_filename(void *data) { t_target *t = data; MkDialogue(FILLSCREEN, "Enter a file name", 0); AddEditBox(DOWNLEFT, 200, "File name:", FSTRING, MAXCHARS, t->name); AddButton(DOWNLEFT, "~OK", CloseWin, NULL); DisplayWin(); } static void choose_output(void *data) { t_target *t = data; MkMenuRadio(&t->dest, 2, "Default printer", "Html file"); MkMenuItem(0, "Choose filename", "", choose_filename, t); } static void show_menu(void *data) { (void)data; #if defined ALLEGRO_LINUX static t_target target = {1, "default.html"}; #else static t_target target = {0, ""}; #endif MkMenuItem(0, "Display printer queues", "", show_queue, NULL); MkMenuItem(0, "Printer settings", "", show_settings, NULL); MkMenuItem(1, "Choose output", "", choose_output, &target); MkMenuItem(0, "Print sample (table)", "", show_tabular_sample, &target); MkMenuItem(0, "Print sample (free pos)", "", show_freepos_sample, &target); MkMenuItem(0, "Print sample (diagram)", "", show_diagram_sample, &target); MkMenuItem(0, "Exit", "Ctrl-Q", quit, NULL); } int main(void) { allegro_init(); #ifdef ALLEGRO_WINDOWS InitCguiWindowedMode(); #endif InitCgui(800, 600, 15); CpriInitPrinter(); MkDialogue(FILLSCREEN, "Print out testing", 0); MakeMenuBar(); MkMenuBarItem("File", show_menu, NULL); EndMenuBar(); AddTextBox(DOWNLEFT, "This example shows just a few of the features of " "the CPRI function, please read the docs for full understanding._ _" "Only the table printing is meaningful for html and text files. Also " "the table printing will have some necessary limitation, e.g. since " "the tables are implemented as table in html, the table cells will be " "either outlined with a border or not (i.e. the option to exclude table " "rows from outlining when printing to paper is not present in html)._" "As this exemple program is implemented you have to check the outlining " "option for the page header to make the entire table outlined._" "All integer values should be entered in mm._ _" "The values not possible to modify for the user in this example " "can of course be varied (like printing orienteation etc). Specially " "arbitrary cells may be joined, and also the font properties may be " "varied at arbitrary points in the table._ _" "If you like to inspect the code how to print you should look at the " "functions that actyally prints, the print__* funtions, the other stuff " "are just code to make the dialogs which is not really related to the " "printing._ _" "Also note: none of the data structures defined in this examples are " "needed for the actual printing. They are only part of the example, I " "made them just for convenience.", 500, 0, TB_FRAMESINK|TB_LINEFEED_); SetHotKey(0, quit, NULL, 0, KEY_Q); DisplayWin(); ProcessEvents(); DeInitCgui(); return 0; } END_OF_MAIN(); --- ex.c DELETED --- |