Mercurial > dwindows
comparison dw.hpp @ 2898:d53fee198085
C++: Add KeyPress, ButtonPress, ButtonRelease and MotionNotify signals to Render.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 24 Dec 2022 00:52:45 +0000 |
parents | fed0c2112985 |
children | 425dc0126818 |
comparison
equal
deleted
inserted
replaced
2897:fed0c2112985 | 2898:d53fee198085 |
---|---|
514 }; | 514 }; |
515 | 515 |
516 class Render : public Drawable, public Widget | 516 class Render : public Drawable, public Widget |
517 { | 517 { |
518 private: | 518 private: |
519 bool ExposeConnected, ConfigureConnected; | 519 bool ExposeConnected, ConfigureConnected, KeyPressConnected, ButtonPressConnected, ButtonReleaseConnected, MotionNotifyConnected; |
520 #ifdef DW_LAMBDA | 520 #ifdef DW_LAMBDA |
521 std::function<int(DWExpose *)> _ConnectExpose; | 521 std::function<int(DWExpose *)> _ConnectExpose; |
522 std::function<int(int, int)> _ConnectConfigure; | 522 std::function<int(int, int)> _ConnectConfigure; |
523 std::function<int(char c, int, int, char *)> _ConnectKeyPress; | |
524 std::function<int(int, int, int)> _ConnectButtonPress; | |
525 std::function<int(int, int, int)> _ConnectButtonRelease; | |
526 std::function<int(int, int, int)> _ConnectMotionNotify; | |
523 #else | 527 #else |
524 int (*_ConnectExpose)(DWExpose *); | 528 int (*_ConnectExpose)(DWExpose *); |
525 int (*_ConnectConfigure)(int width, int height); | 529 int (*_ConnectConfigure)(int width, int height); |
530 int (*_ConnectKeyPress)(char c, int vk, int state, char *utf8); | |
531 int (*_ConnectButtonPress)(int x, int y, int buttonmask); | |
532 int (*_ConnectButtonRelease)(int x, int y, int buttonmask); | |
533 int (*_ConnectMotionNotify)(int x, int y, int buttonmask); | |
526 #endif | 534 #endif |
527 void Setup() { | 535 void Setup() { |
528 if(IsOverridden(Render::OnExpose, this)) { | 536 if(IsOverridden(Render::OnExpose, this)) { |
529 dw_signal_connect(hwnd, DW_SIGNAL_EXPOSE, DW_SIGNAL_FUNC(_OnExpose), this); | 537 dw_signal_connect(hwnd, DW_SIGNAL_EXPOSE, DW_SIGNAL_FUNC(_OnExpose), this); |
530 ExposeConnected = true; | 538 ExposeConnected = true; |
531 } | 539 } |
532 if(IsOverridden(Render::OnConfigure, this)) { | 540 if(IsOverridden(Render::OnConfigure, this)) { |
533 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); | 541 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); |
534 ConfigureConnected = true; | 542 ConfigureConnected = true; |
543 } | |
544 if(IsOverridden(Render::OnKeyPress, this)) { | |
545 dw_signal_connect(hwnd, DW_SIGNAL_KEY_PRESS, DW_SIGNAL_FUNC(_OnKeyPress), this); | |
546 KeyPressConnected = true; | |
547 } | |
548 if(IsOverridden(Render::OnButtonPress, this)) { | |
549 dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_PRESS, DW_SIGNAL_FUNC(_OnButtonPress), this); | |
550 ButtonPressConnected = true; | |
551 } | |
552 if(IsOverridden(Render::OnButtonRelease, this)) { | |
553 dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_RELEASE, DW_SIGNAL_FUNC(_OnButtonRelease), this); | |
554 ButtonReleaseConnected = true; | |
555 } | |
556 if(IsOverridden(Render::OnMotionNotify, this)) { | |
557 dw_signal_connect(hwnd, DW_SIGNAL_MOTION_NOTIFY, DW_SIGNAL_FUNC(_OnMotionNotify), this); | |
558 MotionNotifyConnected = true; | |
535 } | 559 } |
536 } | 560 } |
537 static int _OnExpose(HWND window, DWExpose *exp, void *data) { | 561 static int _OnExpose(HWND window, DWExpose *exp, void *data) { |
538 if(reinterpret_cast<Render *>(data)->_ConnectExpose) | 562 if(reinterpret_cast<Render *>(data)->_ConnectExpose) |
539 return reinterpret_cast<Render *>(data)->_ConnectExpose(exp); | 563 return reinterpret_cast<Render *>(data)->_ConnectExpose(exp); |
540 return reinterpret_cast<Render *>(data)->OnExpose(exp); } | 564 return reinterpret_cast<Render *>(data)->OnExpose(exp); } |
541 static int _OnConfigure(HWND window, int width, int height, void *data) { | 565 static int _OnConfigure(HWND window, int width, int height, void *data) { |
542 if(reinterpret_cast<Render *>(data)->_ConnectConfigure) | 566 if(reinterpret_cast<Render *>(data)->_ConnectConfigure) |
543 return reinterpret_cast<Render *>(data)->_ConnectConfigure(width, height); | 567 return reinterpret_cast<Render *>(data)->_ConnectConfigure(width, height); |
544 return reinterpret_cast<Render *>(data)->OnConfigure(width, height); } | 568 return reinterpret_cast<Render *>(data)->OnConfigure(width, height); } |
569 static int _OnKeyPress(HWND window, char c, int vk, int state, void *data, char *utf8) { | |
570 if(reinterpret_cast<Render *>(data)->_ConnectKeyPress) | |
571 return reinterpret_cast<Render *>(data)->_ConnectKeyPress(c, vk, state, utf8); | |
572 return reinterpret_cast<Render *>(data)->OnKeyPress(c, vk, state, utf8); } | |
573 static int _OnButtonPress(HWND window, int x, int y, int buttonmask, void *data) { | |
574 if(reinterpret_cast<Render *>(data)->_ConnectButtonPress) | |
575 return reinterpret_cast<Render *>(data)->_ConnectButtonPress(x, y, buttonmask); | |
576 return reinterpret_cast<Render *>(data)->OnButtonPress(x, y, buttonmask); } | |
577 static int _OnButtonRelease(HWND window, int x, int y, int buttonmask, void *data) { | |
578 if(reinterpret_cast<Render *>(data)->_ConnectButtonRelease) | |
579 return reinterpret_cast<Render *>(data)->_ConnectButtonRelease(x, y, buttonmask); | |
580 return reinterpret_cast<Render *>(data)->OnButtonRelease(x, y, buttonmask); } | |
581 static int _OnMotionNotify(HWND window, int x, int y, int buttonmask, void *data) { | |
582 if(reinterpret_cast<Render *>(data)->_ConnectMotionNotify) | |
583 return reinterpret_cast<Render *>(data)->_ConnectMotionNotify(x, y, buttonmask); | |
584 return reinterpret_cast<Render *>(data)->OnMotionNotify(x, y, buttonmask); } | |
545 public: | 585 public: |
546 // Constructors | 586 // Constructors |
547 Render(unsigned long id) { SetHWND(dw_render_new(id)); Setup(); } | 587 Render(unsigned long id) { SetHWND(dw_render_new(id)); Setup(); } |
548 Render() { SetHWND(dw_render_new(0)); Setup(); } | 588 Render() { SetHWND(dw_render_new(0)); Setup(); } |
549 | 589 |
587 _ConnectConfigure = userfunc; | 627 _ConnectConfigure = userfunc; |
588 if(!ConfigureConnected) { | 628 if(!ConfigureConnected) { |
589 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); | 629 dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); |
590 ConfigureConnected = true; | 630 ConfigureConnected = true; |
591 } | 631 } |
592 } | 632 } |
633 #ifdef DW_LAMBDA | |
634 void ConnectKeyPress(std::function<int(char, int, int, char *)> userfunc) | |
635 #else | |
636 void ConnectKeyPress(int (*userfunc)(char, int, int, char *)) | |
637 #endif | |
638 { | |
639 _ConnectKeyPress = userfunc; | |
640 if(!KeyPressConnected) { | |
641 dw_signal_connect(hwnd, DW_SIGNAL_KEY_PRESS, DW_SIGNAL_FUNC(_OnKeyPress), this); | |
642 KeyPressConnected = true; | |
643 } | |
644 } | |
645 #ifdef DW_LAMBDA | |
646 void ConnectButtonPress(std::function<int(int, int, int)> userfunc) | |
647 #else | |
648 void ConnectButtonPress(int (*userfunc)(int, int, int)) | |
649 #endif | |
650 { | |
651 _ConnectButtonPress = userfunc; | |
652 if(!KeyPressConnected) { | |
653 dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_PRESS, DW_SIGNAL_FUNC(_OnButtonPress), this); | |
654 ButtonPressConnected = true; | |
655 } | |
656 } | |
657 #ifdef DW_LAMBDA | |
658 void ConnectButtonRelease(std::function<int(int, int, int)> userfunc) | |
659 #else | |
660 void ConnectButtonRelease(int (*userfunc)(int, int, int)) | |
661 #endif | |
662 { | |
663 _ConnectButtonRelease = userfunc; | |
664 if(!KeyPressConnected) { | |
665 dw_signal_connect(hwnd, DW_SIGNAL_BUTTON_RELEASE, DW_SIGNAL_FUNC(_OnButtonRelease), this); | |
666 ButtonReleaseConnected = true; | |
667 } | |
668 } | |
669 #ifdef DW_LAMBDA | |
670 void ConnectMotionNotify(std::function<int(int, int, int)> userfunc) | |
671 #else | |
672 void ConnectMotionNotify(int (*userfunc)(int, int, int)) | |
673 #endif | |
674 { | |
675 _ConnectMotionNotify = userfunc; | |
676 if(!MotionNotifyConnected) { | |
677 dw_signal_connect(hwnd, DW_SIGNAL_MOTION_NOTIFY, DW_SIGNAL_FUNC(_OnMotionNotify), this); | |
678 MotionNotifyConnected = true; | |
679 } | |
680 } | |
593 protected: | 681 protected: |
594 // Our signal handler functions to be overriden... | 682 // Our signal handler functions to be overriden... |
595 // If they are not overridden and an event is generated, remove the unused handler | 683 // If they are not overridden and an event is generated, remove the unused handler |
596 virtual int OnExpose(DWExpose *exp) { | 684 virtual int OnExpose(DWExpose *exp) { |
597 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_EXPOSE); | 685 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_EXPOSE); |
599 return FALSE; | 687 return FALSE; |
600 } | 688 } |
601 virtual int OnConfigure(int width, int height) { | 689 virtual int OnConfigure(int width, int height) { |
602 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); | 690 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_CONFIGURE); |
603 ConfigureConnected = false; | 691 ConfigureConnected = false; |
692 return FALSE; | |
693 }; | |
694 virtual int OnKeyPress(char c, int vk, int state, char *utf8) { | |
695 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_KEY_PRESS); | |
696 KeyPressConnected = false; | |
697 return FALSE; | |
698 }; | |
699 virtual int OnButtonPress(char x, int y, int buttonmask) { | |
700 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_BUTTON_PRESS); | |
701 ButtonPressConnected = false; | |
702 return FALSE; | |
703 }; | |
704 virtual int OnButtonRelease(char x, int y, int buttonmask) { | |
705 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_BUTTON_RELEASE); | |
706 ButtonReleaseConnected = false; | |
707 return FALSE; | |
708 }; | |
709 virtual int OnMotionNotify(char x, int y, int buttonmask) { | |
710 dw_signal_disconnect_by_name(hwnd, DW_SIGNAL_MOTION_NOTIFY); | |
711 MotionNotifyConnected = false; | |
604 return FALSE; | 712 return FALSE; |
605 }; | 713 }; |
606 }; | 714 }; |
607 | 715 |
608 class Pixmap : public Drawable, public Handle | 716 class Pixmap : public Drawable, public Handle |