Mercurial > dwindows
comparison os2/dw.c @ 1181:5d3c1b0686ce
Initial attempt at printing on OS/2.
Need to fix issue with dw_pixmap_set_font() but wanted to
get the rest of the code committed.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 20 Sep 2011 21:02:54 +0000 |
parents | 924c8087a755 |
children | cef7400dcaba |
comparison
equal
deleted
inserted
replaced
1180:4c20df806370 | 1181:5d3c1b0686ce |
---|---|
11 */ | 11 */ |
12 #define INCL_DOS | 12 #define INCL_DOS |
13 #define INCL_DOSERRORS | 13 #define INCL_DOSERRORS |
14 #define INCL_WIN | 14 #define INCL_WIN |
15 #define INCL_GPI | 15 #define INCL_GPI |
16 #define INCL_DEV | |
16 | 17 |
17 #include <os2.h> | 18 #include <os2.h> |
18 #include <stdlib.h> | 19 #include <stdlib.h> |
19 #include <string.h> | 20 #include <string.h> |
20 #include <stdio.h> | 21 #include <stdio.h> |
8794 */ | 8795 */ |
8795 int API dw_pixmap_set_font(HPIXMAP pixmap, char *fontname) | 8796 int API dw_pixmap_set_font(HPIXMAP pixmap, char *fontname) |
8796 { | 8797 { |
8797 if(pixmap && fontname && *fontname) | 8798 if(pixmap && fontname && *fontname) |
8798 { | 8799 { |
8799 char *name = strchr(fontname, '.'); | 8800 char *name = strchr(fontname, '.'); |
8800 | 8801 |
8801 if(name) | 8802 if(name) |
8802 { | 8803 { |
8803 FATTRS fat; | 8804 FATTRS fat; |
8804 | 8805 |
8805 memset(&fat, 0, sizeof(fat)); | 8806 memset(&fat, 0, sizeof(fat)); |
8806 | 8807 |
8807 fat.usRecordLength = sizeof(FATTRS); | 8808 fat.usRecordLength = sizeof(FATTRS); |
8808 strcpy(fat.szFacename, name); | 8809 strcpy(fat.szFacename, name); |
8809 | 8810 |
8810 GpiCreateLogFont(pixmap->hps, 0, 1L, &fat); | 8811 GpiCreateLogFont(pixmap->hps, 0, 1L, &fat); |
8811 GpiSetCharSet(pixmap->hps, 1L); | 8812 GpiSetCharSet(pixmap->hps, 1L); |
8812 return DW_ERROR_NONE; | 8813 return DW_ERROR_NONE; |
8813 } | 8814 } |
8814 } | 8815 } |
8815 return DW_ERROR_GENERAL; | 8816 return DW_ERROR_GENERAL; |
8816 } | 8817 } |
8817 | 8818 |
8818 /* | 8819 /* |
8819 * Destroys an allocated pixmap. | 8820 * Destroys an allocated pixmap. |
10078 { | 10079 { |
10079 id = id; | 10080 id = id; |
10080 return dw_box_new(DW_HORZ, 0); | 10081 return dw_box_new(DW_HORZ, 0); |
10081 } | 10082 } |
10082 | 10083 |
10084 typedef struct _dwprint | |
10085 { | |
10086 HDC hdc; | |
10087 char *printername; | |
10088 int (*drawfunc)(HPRINT, HPIXMAP, int, void *); | |
10089 void *drawdata; | |
10090 unsigned long flags; | |
10091 unsigned int startpage, endpage; | |
10092 char *jobname; | |
10093 } DWPrint; | |
10094 | |
10095 /* Internal functions to handle the print dialog */ | |
10096 int DWSIGNAL _dw_printer_cancel_func(HWND window, void *data) | |
10097 { | |
10098 DWPrint *print = (DWPrint *)data; | |
10099 DWDialog *dwwait = (DWDialog *)print->printername; | |
10100 | |
10101 window = (HWND)dwwait->data; | |
10102 | |
10103 dw_window_destroy(window); | |
10104 dw_dialog_dismiss(dwwait, NULL); | |
10105 return FALSE; | |
10106 } | |
10107 | |
10108 int DWSIGNAL _dw_printer_ok_func(HWND window, void *data) | |
10109 { | |
10110 DWPrint *print = (DWPrint *)data; | |
10111 DWDialog *dwwait = (DWDialog *)print->printername; | |
10112 HWND printerlist, startspin, endspin; | |
10113 char *result = NULL; | |
10114 | |
10115 window = (HWND)dwwait->data; | |
10116 printerlist = (HWND)dw_window_get_data(window, "_dw_list"); | |
10117 startspin = (HWND)dw_window_get_data(window, "_dw_start_spin"); | |
10118 endspin = (HWND)dw_window_get_data(window, "_dw_end_spin"); | |
10119 if(printerlist) | |
10120 { | |
10121 char printername[32] = ""; | |
10122 int selected = dw_listbox_selected(printerlist); | |
10123 | |
10124 /* Get the name of the selected printer */ | |
10125 if(selected != DW_ERROR_UNKNOWN) | |
10126 { | |
10127 dw_listbox_get_text(printerlist, selected, printername, 32); | |
10128 if(printername[0]) | |
10129 print->printername = result = strdup(printername); | |
10130 } | |
10131 /* Get the start and end positions */ | |
10132 print->startpage = (unsigned int)dw_spinbutton_get_pos(startspin); | |
10133 print->endpage = (unsigned int)dw_spinbutton_get_pos(endspin); | |
10134 | |
10135 /* If the start is bigger than end... swap them */ | |
10136 if(print->startpage > print->endpage) | |
10137 { | |
10138 print->endpage = print->startpage; | |
10139 print->startpage = (unsigned int)dw_spinbutton_get_pos(endspin); | |
10140 } | |
10141 } | |
10142 | |
10143 dw_window_destroy(window); | |
10144 dw_dialog_dismiss(dwwait, (void *)result); | |
10145 return FALSE; | |
10146 } | |
10147 | |
10148 /* Borrowed functions which should probably be rewritten */ | |
10149 BOOL _ExtractLogAddress(char * LogAddress, char * DetailStr) | |
10150 { | |
10151 char *p; | |
10152 | |
10153 p = DetailStr; | |
10154 while(*p++ != ';'); /* Gets to first ';' and one char beyond */ | |
10155 while(*p++ != ';'); /* Gets to second ';' and one char beyond */ | |
10156 while(*p != ';') *LogAddress++ = *p++; | |
10157 *LogAddress = '\0'; | |
10158 return TRUE; | |
10159 } | |
10160 | |
10161 BOOL _ExtractDriverName(char * DriverName, char * DetailStr) | |
10162 { | |
10163 char *p; | |
10164 | |
10165 p = DetailStr; | |
10166 while(*p++ != ';'); /* Gets to first ';' and one char beyond */ | |
10167 while(*p != '.' && *p != ';' && *p != ',') | |
10168 *DriverName++ = *p++; | |
10169 *DriverName = '\0'; | |
10170 return TRUE; | |
10171 } | |
10172 | |
10083 /* | 10173 /* |
10084 * Creates a new print object. | 10174 * Creates a new print object. |
10085 * Parameters: | 10175 * Parameters: |
10086 * jobname: Name of the print job to show in the queue. | 10176 * jobname: Name of the print job to show in the queue. |
10087 * flags: Flags to initially configure the print object. | 10177 * flags: Flags to initially configure the print object. |
10091 * Returns: | 10181 * Returns: |
10092 * A handle to the print object or NULL on failure. | 10182 * A handle to the print object or NULL on failure. |
10093 */ | 10183 */ |
10094 HPRINT API dw_print_new(char *jobname, unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata) | 10184 HPRINT API dw_print_new(char *jobname, unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata) |
10095 { | 10185 { |
10096 return NULL; | 10186 char printername[32], tmpbuf[20]; |
10187 HWND window, hbox, vbox, printerlist, button, text; | |
10188 DWDialog *dwwait; | |
10189 DWPrint *print; | |
10190 /* Check the default printer for now... want a printer list in the future */ | |
10191 int cb = PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER", "PRINTER", "", printername, 32); | |
10192 | |
10193 if(!drawfunc || !(print = calloc(1, sizeof(DWPrint)))) | |
10194 return NULL; | |
10195 | |
10196 print->drawfunc = drawfunc; | |
10197 print->drawdata = drawdata; | |
10198 print->jobname = jobname ? jobname : "Dynamic Windows Print Job"; | |
10199 print->startpage = 1; | |
10200 print->endpage = pages; | |
10201 print->flags = flags; | |
10202 | |
10203 /* Make sure we got a valid result */ | |
10204 if(cb > 2) | |
10205 printername[cb-2] = '\0'; | |
10206 else | |
10207 { | |
10208 /* Show an error and return failure */ | |
10209 dw_messagebox("Printing", DW_MB_ERROR | DW_MB_OK, "No printers detected."); | |
10210 free(print); | |
10211 return NULL; | |
10212 } | |
10213 | |
10214 /* Create the print dialog */ | |
10215 window = dw_window_new(HWND_DESKTOP, "Choose Printer", FCF_SHELLPOSITION | FCF_TITLEBAR | FCF_DLGBORDER | FCF_CLOSEBUTTON | FCF_SYSMENU); | |
10216 | |
10217 vbox = dw_box_new(DW_VERT, 5); | |
10218 | |
10219 dw_box_pack_start(window, vbox, 0, 0, TRUE, TRUE, 0); | |
10220 | |
10221 printerlist = dw_listbox_new(0, FALSE); | |
10222 dw_box_pack_start(vbox, printerlist, 1, 1, TRUE, TRUE, 0); | |
10223 dw_listbox_append(printerlist, printername); | |
10224 dw_listbox_select(printerlist, 0, TRUE); | |
10225 | |
10226 dw_window_set_data(window, "_dw_list", (void *)printerlist); | |
10227 | |
10228 /* Start spinbutton */ | |
10229 hbox = dw_box_new(DW_HORZ, 0); | |
10230 | |
10231 dw_box_pack_start(vbox, hbox, 0, 0, TRUE, FALSE, 0); | |
10232 | |
10233 text = dw_text_new("Start page:", 0); | |
10234 dw_window_set_style(text, DW_DT_VCENTER, DW_DT_VCENTER); | |
10235 dw_box_pack_start(hbox, text, 70, 20, FALSE, FALSE, 3); | |
10236 | |
10237 button = dw_spinbutton_new("1", 0); | |
10238 dw_spinbutton_set_limits(button, 1, pages); | |
10239 dw_box_pack_start(hbox, button, 20, 20, TRUE, FALSE, 3); | |
10240 dw_window_set_data(window, "_dw_start_spin", (void *)button); | |
10241 | |
10242 /* End spinbutton */ | |
10243 hbox = dw_box_new(DW_HORZ, 0); | |
10244 | |
10245 dw_box_pack_start(vbox, hbox, 0, 0, TRUE, FALSE, 0); | |
10246 | |
10247 text = dw_text_new("End page:", 0); | |
10248 dw_window_set_style(text, DW_DT_VCENTER, DW_DT_VCENTER); | |
10249 dw_box_pack_start(hbox, text, 70, 20, FALSE, FALSE, 3); | |
10250 | |
10251 sprintf(tmpbuf, "%d", pages); | |
10252 button = dw_spinbutton_new(tmpbuf, 0); | |
10253 dw_spinbutton_set_limits(button, 1, pages); | |
10254 dw_box_pack_start(hbox, button, 20, 20, TRUE, FALSE, 3); | |
10255 dw_window_set_data(window, "_dw_end_spin", (void *)button); | |
10256 | |
10257 /* Ok and Cancel buttons */ | |
10258 hbox = dw_box_new(DW_HORZ, 0); | |
10259 | |
10260 dw_box_pack_start(vbox, hbox, 0, 0, TRUE, FALSE, 0); | |
10261 dw_box_pack_start(hbox, 0, 100, 1, TRUE, FALSE, 0); | |
10262 | |
10263 button = dw_button_new("Ok", 0); | |
10264 dw_box_pack_start(hbox, button, 50, 30, TRUE, FALSE, 3); | |
10265 | |
10266 dwwait = dw_dialog_new((void *)window); | |
10267 /* Save it temporarily there until we need it */ | |
10268 print->printername = (char *)dwwait; | |
10269 | |
10270 dw_signal_connect(button, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_printer_ok_func), (void *)print); | |
10271 | |
10272 button = dw_button_new("Cancel", 0); | |
10273 dw_box_pack_start(hbox, button, 50, 30, TRUE, FALSE, 3); | |
10274 | |
10275 dw_signal_connect(button, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_printer_cancel_func), (void *)print); | |
10276 dw_signal_connect(window, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_dw_printer_cancel_func), (void *)print); | |
10277 | |
10278 dw_window_set_size(window, 300, 400); | |
10279 | |
10280 dw_window_show(window); | |
10281 | |
10282 print->printername = dw_dialog_wait(dwwait); | |
10283 | |
10284 /* The user picked a printer */ | |
10285 if(print->printername) | |
10286 { | |
10287 char PrintDetails[256]; | |
10288 char DriverName[32]; | |
10289 char LogAddress[32]; | |
10290 DEVOPENSTRUC dop; | |
10291 | |
10292 /* Get the printer information string */ | |
10293 cb = PrfQueryProfileString(HINI_PROFILE, "PM_SPOOLER_PRINTER", print->printername, "", PrintDetails, 256); | |
10294 _ExtractLogAddress(LogAddress, PrintDetails); | |
10295 _ExtractDriverName(DriverName, PrintDetails); | |
10296 dop.pszDriverName = DriverName; | |
10297 dop.pszLogAddress = LogAddress; | |
10298 dop.pdriv = NULL; | |
10299 dop.pszDataType = "PM_Q_STD"; | |
10300 /* Attempt to open a device context and return a handle to it */ | |
10301 print->hdc = DevOpenDC(dwhab, OD_QUEUED, "*", 4L, (PDEVOPENDATA) &dop, (HDC)NULL); | |
10302 if(print->hdc) | |
10303 return print; | |
10304 } | |
10305 /* The user canceled */ | |
10306 if(print->printername) | |
10307 free(print->printername); | |
10308 free(print); | |
10309 return NULL; | |
10097 } | 10310 } |
10098 | 10311 |
10099 /* | 10312 /* |
10100 * Runs the print job, causing the draw page callbacks to fire. | 10313 * Runs the print job, causing the draw page callbacks to fire. |
10101 * Parameters: | 10314 * Parameters: |
10104 * Returns: | 10317 * Returns: |
10105 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success. | 10318 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success. |
10106 */ | 10319 */ |
10107 int API dw_print_run(HPRINT print, unsigned long flags) | 10320 int API dw_print_run(HPRINT print, unsigned long flags) |
10108 { | 10321 { |
10109 return DW_ERROR_UNKNOWN; | 10322 DWPrint *p = print; |
10323 HPIXMAP pixmap; | |
10324 int x, result = DW_ERROR_UNKNOWN; | |
10325 SIZEL sizl = { 0, 0 }; | |
10326 | |
10327 if(!p) | |
10328 return result; | |
10329 | |
10330 if (!(pixmap = calloc(1,sizeof(struct _hpixmap)))) | |
10331 return result; | |
10332 | |
10333 /* Start the job */ | |
10334 DevEscape(p->hdc, DEVESC_STARTDOC, strlen(p->jobname), p->jobname, NULL, NULL); | |
10335 | |
10336 /*pixmap->handle = handle;*/ | |
10337 pixmap->hdc = p->hdc; | |
10338 pixmap->hps = GpiCreatePS(dwhab, p->hdc, &sizl, PU_PELS | GPIF_DEFAULT | GPIT_NORMAL | GPIA_ASSOC); | |
10339 pixmap->transcolor = DW_RGB_TRANSPARENT; | |
10340 pixmap->width = sizl.cx; | |
10341 pixmap->height = sizl.cy; | |
10342 | |
10343 /* Cycle through each page */ | |
10344 for(x=p->startpage-1; x<p->endpage && p->drawfunc; x++) | |
10345 { | |
10346 p->drawfunc(print, pixmap, x, p->drawdata); | |
10347 /* Next page */ | |
10348 DevEscape(p->hdc, DEVESC_NEWFRAME, 0, NULL, NULL, NULL); | |
10349 } | |
10350 if(p->drawfunc) | |
10351 result = DW_ERROR_NONE; | |
10352 /* Free memory */ | |
10353 dw_pixmap_destroy(pixmap); | |
10354 if(p->printername) | |
10355 free(p->printername); | |
10356 free(p); | |
10357 return result; | |
10110 } | 10358 } |
10111 | 10359 |
10112 /* | 10360 /* |
10113 * Cancels the print job, typically called from a draw page callback. | 10361 * Cancels the print job, typically called from a draw page callback. |
10114 * Parameters: | 10362 * Parameters: |
10115 * print: Handle to the print object returned by dw_print_new(). | 10363 * print: Handle to the print object returned by dw_print_new(). |
10116 */ | 10364 */ |
10117 void API dw_print_cancel(HPRINT print) | 10365 void API dw_print_cancel(HPRINT print) |
10118 { | 10366 { |
10367 DWPrint *p = print; | |
10368 | |
10369 if(p) | |
10370 p->drawfunc = NULL; | |
10119 } | 10371 } |
10120 | 10372 |
10121 /* | 10373 /* |
10122 * Returns a pointer to a static buffer which containes the | 10374 * Returns a pointer to a static buffer which containes the |
10123 * current user directory. Or the root directory (C:\ on | 10375 * current user directory. Or the root directory (C:\ on |