# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1671768831 0 # Node ID 5a6bf6bd30010cbc571560927e2cafcbbb5216ea # Parent 5bbd275369cd8727b5c716bc5401bccadeb871e6 C++: Divide up C++11 and Lambda support since some compilers can support lambdas without full C++11 support. For instance MSVC 2010 supports lambdas but only MSVC 2017 supports C++11 or higher. GCC 4.5 supports lambdas but GCC 4.8.1 supports C++11. diff -r 5bbd275369cd -r 5a6bf6bd3001 dw.hpp --- a/dw.hpp Fri Dec 23 02:56:29 2022 +0000 +++ b/dw.hpp Fri Dec 23 04:13:51 2022 +0000 @@ -8,14 +8,20 @@ #include // Attempt to support compilers without nullptr type literal -#if __cplusplus >= 201103L +#if __cplusplus >= 201103L #define DW_CPP11 #define DW_NULL nullptr -#include #else #define DW_NULL NULL #endif +// Support Lambdas on C++11, Visual C 2010 or GCC 4.5 +#ifdef DW_CPP11 || (defined(_MSC_VER) && _MSC_VER >= 1600) || \ + (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))) +#define DW_LAMBDA +#include +#endif + // Attempt to allow compilation on GCC older than 4.7 #if defined(__GNUC__) && (__GNuC__ < 5 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)) #define override @@ -118,7 +124,7 @@ { private: bool ClickedConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectClicked; #else int (*_ConnectClicked)(); @@ -142,7 +148,7 @@ return FALSE; } public: -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectClicked(std::function userfunc) #else void ConnectClicked(int (*userfunc)()) @@ -227,7 +233,7 @@ { private: bool DeleteConnected, ConfigureConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectDelete; std::function _ConnectConfigure; #else @@ -295,7 +301,7 @@ delete menu; } } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectDelete(std::function userfunc) #else void ConnectDelete(int (*userfunc)()) @@ -307,7 +313,7 @@ DeleteConnected = true; } } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectConfigure(std::function userfunc) #else void ConnectConfigure(int (*userfunc)(int, int)) @@ -511,7 +517,7 @@ { private: bool ExposeConnected, ConfigureConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectExpose; std::function _ConnectConfigure; #else @@ -560,7 +566,7 @@ void GetTextExtents(const char *text, int *width, int *height) { dw_font_text_extents_get(hwnd, DW_NULL, text, width, height); } char *GetFont() { return dw_window_get_font(hwnd); } void Redraw() { dw_render_redraw(hwnd); } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectExpose(std::function userfunc) #else void ConnectExpose(int (*userfunc)(DWExpose *)) @@ -572,7 +578,7 @@ ExposeConnected = true; } } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectConfigure(std::function userfunc) #else void ConnectConfigure(int (*userfunc)(int, int)) @@ -652,7 +658,7 @@ { private: bool ChangedConnected, ResultConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectChanged; std::function _ConnectResult; #else @@ -687,7 +693,7 @@ int JavascriptRun(const char *script, void *scriptdata) { return dw_html_javascript_run(hwnd, script, scriptdata); } int Raw(const char *buffer) { return dw_html_raw(hwnd, buffer); } int URL(const char *url) { return dw_html_url(hwnd, url); } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectChanged(std::function userfunc) #else void ConnectChanged(int (*userfunc)(int, char *)) @@ -699,7 +705,7 @@ ChangedConnected = true; } } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectResult(std::function userfunc) #else void ConnectResult(int (*userfunc)(int, char *, void *)) @@ -759,7 +765,7 @@ { private: bool ListSelectConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectListSelect; #else int (*_ConnectListSelect)(int index); @@ -789,7 +795,7 @@ int Selected() { return dw_listbox_selected(hwnd); } int Selected(int where) { return dw_listbox_selected_multi(hwnd, where); } void SetTop(int top) { dw_listbox_set_top(hwnd, top); } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectListSelect(std::function userfunc) #else void ConnectListSelect(int (*userfunc)(int)) @@ -836,7 +842,7 @@ { private: bool ValueChangedConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectValueChanged; #else int (*_ConnectValueChanged)(int value); @@ -861,7 +867,7 @@ return FALSE; } public: -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectValueChanged(std::function userfunc) #else void ConnectValueChanged(int (*userfunc)(int)) @@ -943,7 +949,7 @@ { private: bool ItemSelectConnected, ItemContextConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectItemSelect; std::function _ConnectItemContext; #else @@ -984,7 +990,7 @@ return FALSE; } public: -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectItemSelect(std::function userfunc) #else void ConnectItemSelect(int (*userfunc)(HTREEITEM, char *, void *)) @@ -996,7 +1002,7 @@ ItemSelectConnected = true; } } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectItemContext(std::function userfunc) #else void ConnectItemContext(int (*userfunc)(char *, int, int, void *)) @@ -1014,7 +1020,7 @@ { private: bool ItemEnterConnected, ColumnClickConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectItemEnter; std::function _ConnectColumnClick; #else @@ -1076,7 +1082,7 @@ void SetRowData(int row, void *data) { dw_container_set_row_data(allocpointer, row, data); } void SetRowTitle(int row, const char *title) { dw_container_set_row_title(allocpointer, row, title); } void SetStripe(unsigned long oddcolor, unsigned long evencolor) { dw_container_set_stripe(hwnd, oddcolor, evencolor); } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectItemEnter(std::function userfunc) #else void ConnectItemEnter(int (*userfunc)(char *)) @@ -1088,7 +1094,7 @@ ItemEnterConnected = true; } } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnecColumnClick(std::function userfunc) #else void ConnectColumnClick(int (*userfunc)(int)) @@ -1139,7 +1145,7 @@ { private: bool TreeExpandConnected; -#ifdef DW_CPP11 +#ifdef DW_LAMBDA std::function _ConnectTreeExpand; #else int (*_ConnectTreeExpand)(HTREEITEM); @@ -1181,7 +1187,7 @@ void *GetData(HTREEITEM item) { dw_tree_item_get_data(hwnd, item); } void Select(HTREEITEM item) { dw_tree_item_select(hwnd, item); } void SetData(HTREEITEM item, void *itemdata) { dw_tree_item_set_data(hwnd, item, itemdata); } -#ifdef DW_CPP11 +#ifdef DW_LAMBDA void ConnectTreeExpand(std::function userfunc) #else void ConnectTreeExpand(int (*userfunc)(HTREEITEM)) diff -r 5bbd275369cd -r 5a6bf6bd3001 dwtestoo.cpp --- a/dwtestoo.cpp Fri Dec 23 02:56:29 2022 +0000 +++ b/dwtestoo.cpp Fri Dec 23 04:13:51 2022 +0000 @@ -14,7 +14,7 @@ int OnConfigure(int width, int height) override { return FALSE; } }; -#ifndef DW_CPP11 +#ifndef DW_LAMBDA int button_clicked() { DW::App *app = DW::App::Init(); @@ -39,7 +39,7 @@ DW::Button *button = new DW::Button("Test window"); window->PackStart(button, DW_SIZE_AUTO, DW_SIZE_AUTO, TRUE, TRUE, 0); -#ifdef DW_CPP11 +#ifdef DW_LAMBDA button->ConnectClicked([app] () -> int { app->MessageBox("Button", DW_MB_OK | DW_MB_WARNING, "Clicked!"); @@ -54,7 +54,7 @@ // add menus to the menubar DW::Menu *menu = new DW::Menu(); DW::MenuItem *menuitem = menu->AppendItem("~Quit"); -#ifdef DW_CPP11 +#ifdef DW_LAMBDA menuitem->ConnectClicked([app] () -> int { if(app->MessageBox("dwtest", DW_MB_YESNO | DW_MB_QUESTION, "Are you sure you want to exit?") != 0) {