comparison android/dw.cpp @ 2727:bf585f375286

Android: Initial print implementation for Android.... Currently crashes shortly after displaying printer selection dialog. Fixes coming but I wanted to get the bulk of it committed.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 11 Dec 2021 14:14:26 +0000
parents cacde852e2db
children 850da6b24830
comparison
equal deleted inserted replaced
2726:7a15401e73f4 2727:bf585f375286
664 #endif 664 #endif
665 void *params[_DW_EVENT_PARAM_SIZE] = { nullptr, DW_POINTER(uri), nullptr, DW_INT_TO_POINTER(status), 665 void *params[_DW_EVENT_PARAM_SIZE] = { nullptr, DW_POINTER(uri), nullptr, DW_INT_TO_POINTER(status),
666 nullptr, nullptr, nullptr, nullptr, DW_INT_TO_POINTER(message), nullptr }; 666 nullptr, nullptr, nullptr, nullptr, DW_INT_TO_POINTER(message), nullptr };
667 667
668 _dw_event_handler(obj1, params); 668 _dw_event_handler(obj1, params);
669 }
670
671 typedef struct _dwprint
672 {
673 int (*drawfunc)(HPRINT, HPIXMAP, int, void *);
674 void *drawdata;
675 unsigned long flags;
676 char *jobname;
677 jint pages;
678 jobject printjob;
679 } DWPrint;
680
681 /* Handlers for Print events */
682 JNIEXPORT void JNICALL
683 Java_org_dbsoft_dwindows_DWPrintDocumentAdapter_eventHandlerPrintDraw(JNIEnv* env, jobject obj, jlong pr,
684 jobject bitmap, jint page, jint width, jint height)
685 {
686 DWPrint *print = (DWPrint *)pr;
687
688 if(print && print->drawfunc)
689 {
690 HPIXMAP pixmap = (HPIXMAP)alloca(sizeof(HPIXMAP));
691
692 pixmap->width = width;
693 pixmap->height = height;
694 pixmap->bitmap = bitmap;
695 pixmap->handle = nullptr;
696
697 print->drawfunc(print, pixmap, (int)page, print->drawdata);
698 }
699 }
700
701 JNIEXPORT void JNICALL
702 Java_org_dbsoft_dwindows_DWPrintDocumentAdapter_eventHandlerPrintFinish(JNIEnv* env, jobject obj, jlong pr)
703 {
704 DWPrint *print = (DWPrint *) pr;
705
706 if(print)
707 {
708 if(print->jobname)
709 free(print->jobname);
710 free(print);
711 }
669 } 712 }
670 713
671 JNIEXPORT void JNICALL 714 JNIEXPORT void JNICALL
672 Java_org_dbsoft_dwindows_DWindows_eventHandlerInt(JNIEnv* env, jobject obj, jobject obj1, jint message, 715 Java_org_dbsoft_dwindows_DWindows_eventHandlerInt(JNIEnv* env, jobject obj, jobject obj1, jint message,
673 jint inta, jint intb, jint intc, jint intd) { 716 jint inta, jint intb, jint intc, jint intd) {
7507 * Returns: 7550 * Returns:
7508 * A handle to the print object or nullptr on failure. 7551 * A handle to the print object or nullptr on failure.
7509 */ 7552 */
7510 HPRINT API dw_print_new(const char *jobname, unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata) 7553 HPRINT API dw_print_new(const char *jobname, unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata)
7511 { 7554 {
7512 /* TODO: Implement printing */ 7555 DWPrint *print;
7513 return nullptr; 7556
7557 if(!drawfunc || !(print = (DWPrint *)calloc(1, sizeof(DWPrint))))
7558 return nullptr;
7559
7560 if(!jobname)
7561 jobname = "Dynamic Windows Print Job";
7562
7563 print->drawfunc = (int (*)(HPRINT, HPIXMAP, int, void *))drawfunc;
7564 print->drawdata = drawdata;
7565 print->flags = flags;
7566 print->jobname = strdup(jobname);
7567 print->pages = (jint)pages;
7568
7569 return print;
7514 } 7570 }
7515 7571
7516 /* 7572 /*
7517 * Runs the print job, causing the draw page callbacks to fire. 7573 * Runs the print job, causing the draw page callbacks to fire.
7518 * Parameters: 7574 * Parameters:
7519 * print: Handle to the print object returned by dw_print_new(). 7575 * print: Handle to the print object returned by dw_print_new().
7520 * flags: Flags to run the print job. 7576 * flags: Flags to run the print job.
7521 * Returns: 7577 * Returns:
7522 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success. 7578 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success.
7523 */ 7579 */
7524 int API dw_print_run(HPRINT print, unsigned long flags) 7580 int API dw_print_run(HPRINT pr, unsigned long flags)
7525 { 7581 {
7526 /* TODO: Implement printing */ 7582 JNIEnv *env;
7527 return DW_ERROR_UNKNOWN; 7583 DWPrint *print = (DWPrint *)pr;
7584 int retval = DW_ERROR_UNKNOWN;
7585
7586 if(print && print->drawfunc && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
7587 {
7588 // Construct a String
7589 jstring jstr = env->NewStringUTF(print->jobname);
7590 // First get the class that contains the method you need to call
7591 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
7592 // Get the method that you want to call
7593 jmethodID printRun = env->GetMethodID(clazz, "printRun",
7594 "(JILjava/lang/String;II)Landroid/print/PrintJob;");
7595 // Call the method on the object
7596 print->printjob = _dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, printRun, (jlong)pr, (jint)print->flags, jstr, (jint)print->pages, (jint)flags), _DW_REFERENCE_WEAK);
7597 if(print->printjob)
7598 retval = DW_ERROR_NONE;
7599 }
7600 return retval;
7528 } 7601 }
7529 7602
7530 /* 7603 /*
7531 * Cancels the print job, typically called from a draw page callback. 7604 * Cancels the print job, typically called from a draw page callback.
7532 * Parameters: 7605 * Parameters:
7533 * print: Handle to the print object returned by dw_print_new(). 7606 * print: Handle to the print object returned by dw_print_new().
7534 */ 7607 */
7535 void API dw_print_cancel(HPRINT print) 7608 void API dw_print_cancel(HPRINT pr)
7536 { 7609 {
7537 /* TODO: Implement printing */ 7610 JNIEnv *env;
7611 DWPrint *print = (DWPrint *)pr;
7612
7613 if(print)
7614 {
7615 if(print->printjob && (env = (JNIEnv *) pthread_getspecific(_dw_env_key)))
7616 {
7617 // First get the class that contains the method you need to call
7618 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
7619 // Get the method that you want to call
7620 jmethodID printCancel = env->GetMethodID(clazz, "printCancel",
7621 "(Landroid/print/PrintJob;)V");
7622 // Call the method on the object
7623 env->CallVoidMethod(_dw_obj, printCancel, print->printjob);
7624 _dw_jni_check_exception(env);
7625 }
7626 if(print->jobname)
7627 free(print->jobname);
7628 free(print);
7629 }
7538 } 7630 }
7539 7631
7540 /* 7632 /*
7541 * Creates a new system notification if possible. 7633 * Creates a new system notification if possible.
7542 * Parameters: 7634 * Parameters: