comparison dwtestoo.cpp @ 2932:3f660f47a45f

C++: Add HTML and ScrollBox pages to dwtestoo.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 30 Dec 2022 22:53:19 +0000
parents 30c1f37713b6
children 3cdb02171b01
comparison
equal deleted inserted replaced
2931:30c1f37713b6 2932:3f660f47a45f
28 #define SHAPES_DIRECT 1 28 #define SHAPES_DIRECT 1
29 #define DRAW_FILE 2 29 #define DRAW_FILE 2
30 30
31 #define APP_TITLE "Dynamic Windows C++" 31 #define APP_TITLE "Dynamic Windows C++"
32 #define APP_EXIT "Are you sure you want to exit?" 32 #define APP_EXIT "Are you sure you want to exit?"
33
34 #define MAX_WIDGETS 20
33 35
34 // Handle the case of very old compilers by using 36 // Handle the case of very old compilers by using
35 // A simple non-lambda example instead. 37 // A simple non-lambda example instead.
36 #ifndef DW_LAMBDA 38 #ifndef DW_LAMBDA
37 39
1539 { 1541 {
1540 percent->SetPos(value * 10); 1542 percent->SetPos(value * 10);
1541 return TRUE; 1543 return TRUE;
1542 }); 1544 });
1543 } 1545 }
1546
1547 // Page 6 - HTML
1548 void CreateHTML(DW::Box *notebookbox)
1549 {
1550 DW::HTML *rawhtml = new DW::HTML();
1551 if(rawhtml && rawhtml->GetHWND())
1552 {
1553 DW::Box *hbox = new DW::Box(DW_HORZ, 0);
1554 DW::ComboBox *javascript = new DW::ComboBox();
1555
1556 javascript->Append("window.scrollTo(0,500);");
1557 javascript->Append("window.document.title;");
1558 javascript->Append("window.navigator.userAgent;");
1559
1560 notebookbox->PackStart(rawhtml, 0, 100, TRUE, FALSE, 0);
1561 rawhtml->Raw("<html><body><center><h1>dwtest</h1></center></body></html>");
1562 DW::HTML *html = new DW::HTML();
1563
1564 notebookbox->PackStart(hbox, 0, 0, TRUE, FALSE, 0);
1565
1566 // Add navigation buttons
1567 DW::Button *button = new DW::Button("Back");
1568 hbox->PackStart(button, FALSE, FALSE, 0);
1569 button->ConnectClicked([html]() -> int
1570 {
1571 html->Action(DW_HTML_GOBACK);
1572 return TRUE;
1573 });
1574
1575 button = new DW::Button("Forward");
1576 hbox->PackStart(button, FALSE, FALSE, 0);
1577 button->ConnectClicked([html]() -> int
1578 {
1579 html->Action(DW_HTML_GOFORWARD);
1580 return TRUE;
1581 });
1582
1583 // Put in some extra space
1584 hbox->PackStart(0, 5, 1, FALSE, FALSE, 0);
1585
1586 button = new DW::Button("Reload");
1587 hbox->PackStart(button, FALSE, FALSE, 0);
1588 button->ConnectClicked([html]() -> int
1589 {
1590 html->Action(DW_HTML_RELOAD);
1591 return TRUE;
1592 });
1593
1594 // Put in some extra space
1595 hbox->PackStart(0, 5, 1, FALSE, FALSE, 0);
1596 hbox->PackStart(javascript, TRUE, FALSE, 0);
1597
1598 button = new DW::Button("Run");
1599 hbox->PackStart(button, FALSE, FALSE, 0);
1600 button->ConnectClicked([this, javascript, html]() -> int
1601 {
1602 char *script = javascript->GetText();
1603
1604 html->JavascriptRun(script);
1605 this->app->Free(script);
1606 return FALSE;
1607 });
1608 javascript->ClickDefault(button);
1609
1610 notebookbox->PackStart(html, 0, 100, TRUE, TRUE, 0);
1611 html->URL("https://dbsoft.org/dw_help.php");
1612 DW::StatusText *htmlstatus = new DW::StatusText("HTML status loading...");
1613 notebookbox->PackStart(htmlstatus, 100, DW_SIZE_AUTO, TRUE, FALSE, 1);
1614
1615 // Connect the signal handlers
1616 html->ConnectChanged([htmlstatus](int status, char *url) -> int
1617 {
1618 const char *statusnames[] = { "none", "started", "redirect", "loading", "complete", NULL };
1619
1620 if(htmlstatus && url && status < 5)
1621 {
1622 int length = (int)strlen(url) + (int)strlen(statusnames[status]) + 10;
1623 char *text = (char *)calloc(1, length+1);
1624
1625 snprintf(text, length, "Status %s: %s", statusnames[status], url);
1626 htmlstatus->SetText(text);
1627 free(text);
1628 }
1629 return FALSE;
1630 });
1631
1632 html->ConnectResult([this](int status, char *result, void *script_data)
1633 {
1634 this->app->MessageBox("Javascript Result", DW_MB_OK | (status ? DW_MB_ERROR : DW_MB_INFORMATION),
1635 result ? result : "Javascript result is not a string value");
1636 return TRUE;
1637 });
1638 }
1639 else
1640 {
1641 DW::Text *htmltext = new DW::Text("HTML widget not available.");
1642 notebookbox->PackStart(htmltext, 0, 100, TRUE, TRUE, 0);
1643 }
1644 }
1645
1646 // Page 7 - ScrollBox
1647 void CreateScrollBox(DW::Box *notebookbox)
1648 {
1649 char buf[101] = {0};
1650
1651 /* create a box to pack into the notebook page */
1652 DW::ScrollBox *scrollbox = new DW::ScrollBox(DW_VERT, 0);
1653 notebookbox->PackStart(scrollbox, 0, 0, TRUE, TRUE, 1);
1654
1655 DW::Button *adjbutton = new DW::Button("Show Adjustments", 0);
1656 scrollbox->PackStart(adjbutton, FALSE, FALSE, 0);
1657 adjbutton->ConnectClicked([this, scrollbox]() -> int
1658 {
1659 int pos = scrollbox->GetPos(DW_VERT);
1660 int range = scrollbox->GetRange(DW_VERT);
1661 this->app->Debug("Pos %d Range %d\n", pos, range);
1662 return FALSE;
1663 });
1664
1665 for(int i = 0; i < MAX_WIDGETS; i++)
1666 {
1667 DW::Box *tmpbox = new DW::Box(DW_HORZ, 0);
1668 scrollbox->PackStart(tmpbox, 0, 0, TRUE, FALSE, 2);
1669 snprintf(buf, 100, "Label %d", i);
1670 DW::Text *label = new DW::Text(buf );
1671 tmpbox->PackStart(label, 0, DW_SIZE_AUTO, TRUE, FALSE, 0);
1672 snprintf(buf, 100, "Entry %d", i);
1673 DW::Entryfield *entry = new DW::Entryfield(buf , i);
1674 tmpbox->PackStart(entry, 0, DW_SIZE_AUTO, TRUE, FALSE, 0);
1675 }
1676 }
1544 public: 1677 public:
1545 // Constructor creates the application 1678 // Constructor creates the application
1546 DWTest(const char *title): DW::Window(title) { 1679 DWTest(const char *title): DW::Window(title) {
1547 // Get our application singleton 1680 // Get our application singleton
1548 app = DW::App::Init(); 1681 app = DW::App::Init();
1634 notebookbox = new DW::Box(DW_VERT, 5); 1767 notebookbox = new DW::Box(DW_VERT, 5);
1635 CreateButtons(notebookbox); 1768 CreateButtons(notebookbox);
1636 notebookpage = notebook->PageNew(); 1769 notebookpage = notebook->PageNew();
1637 notebook->Pack(notebookpage, notebookbox); 1770 notebook->Pack(notebookpage, notebookbox);
1638 notebook->PageSetText(notebookpage, "buttons"); 1771 notebook->PageSetText(notebookpage, "buttons");
1772
1773 // Create Notebook Page 6 - HTML
1774 notebookbox = new DW::Box(DW_VERT, 5);
1775 CreateHTML(notebookbox);
1776 notebookpage = notebook->PageNew();
1777 notebook->Pack(notebookpage, notebookbox);
1778 notebook->PageSetText(notebookpage, "html");
1779
1780 // Create Notebook Page 7 - ScrollBox
1781 notebookbox = new DW::Box(DW_VERT, 5);
1782 CreateScrollBox(notebookbox);
1783 notebookpage = notebook->PageNew();
1784 notebook->Pack(notebookpage, notebookbox);
1785 notebook->PageSetText(notebookpage, "scrollbox");
1639 1786
1640 // Finalize the window 1787 // Finalize the window
1641 this->SetSize(640, 550); 1788 this->SetSize(640, 550);
1642 1789
1643 timer = new DW::Timer(2000, [this]() -> int 1790 timer = new DW::Timer(2000, [this]() -> int