Skip to content

Commit

Permalink
[examples] update wxwidgets example
Browse files Browse the repository at this point in the history
  • Loading branch information
barendgehrels committed Aug 20, 2024
1 parent 81cc9d6 commit a3f9574
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 66 deletions.
34 changes: 0 additions & 34 deletions example/with_external_libs/CMakeLists.txt

This file was deleted.

29 changes: 29 additions & 0 deletions example/with_external_libs/wxwidgets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Boost.Geometry
# Example CMakeLists.txt building the Boost.Geometry with wxWidget example
#
# Copyright (c) 2021-2024 Barend Gehrels, Amsterdam, the Netherlands.

# Use, modification and distribution is subject to the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

cmake_minimum_required(VERSION 3.8...3.20)

project(wx_widgets_world_mapper)

add_executable(${PROJECT_NAME} x04_wxwidgets_world_mapper.cpp)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)

# Link the wxWidgets libraries to our executable
# Assuming it can be found by CMake in /usr/local
find_package(wxWidgets 3.3 COMPONENTS core base REQUIRED CONFIG)
target_link_libraries(${PROJECT_NAME} wxWidgets::wxWidgets)

# Link the Boost.Geometry library to our executable
# By default, it is assumed to be relative to this directory.
target_include_directories(${PROJECT_NAME} PRIVATE ../../../../..)

# If this does not work, or you build from elsewhere
# First set BOOST_ROOT
# Then use find_package(Boost)
# Then use target_link_libraries(${PROJECT_NAME} Boost::geometry)
47 changes: 47 additions & 0 deletions example/with_external_libs/wxwidgets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ![Boost.Geometry](../../../doc/other/logo/logo_bkg.png)

# wxWidgets

## Introduction

[wxWidgets](https://www.wxwidgets.org/) is a stable and powerful open source framework for developing native cross-platform GUI applications in C++.

## Building wxWidgets

There are several possibilities. This documentation uses the CMake approach.

* Retrieve wxWidgets from github
* Be sure to also retrieve the git submodules
* Build and install with cmake, such that it can be found from anywhere.

```
cd ~git
git clone --recurse-submodules [email protected]:wxWidgets/wxWidgets.git
cd wxWidgets
mkdir my_build_folder
cd my_build_folder
cmake ..
cmake --build .
sudo cmake --build . --target install
```

It is (on macOs) now installed in `/usr/local/lib/`

## Building this example

Assuming you want to build it with CMake

```
cd example/with_external_libs/wxwidgets
mkdir my_build_folder
cd my_build_folder
cmake ..
cmake --build .
```

## Running this example

You can pass an Ascii file with WKT polygons as the first command line argument. There are several
packed with Boost.Geometry as examples and as test data.

For example: `./wx_widgets_world_mapper ../../../data/world.wkt`
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Boost.Geometry
//
// Copyright (c) 2010-2021 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2010-2024 Barend Gehrels, Amsterdam, the Netherlands.
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -11,19 +11,7 @@
// highlighting the country under the mouse, and indicating position
// of the mouse in latitude/longitude and in pixels.

// To compile this program:
// Install wxWidgets (if not done before)
// export BOOST_ROOT=.....
// export WX_ROOT=.... (for example /home/myname/mylib/wxWidgets/Linux/x86_64)
// mkdir build
// cd build
// cmake .. -G Ninja
// ninja
// If necessary, CMakeLists.txt should be adapted, the options for wx
// are provided by "wx-config --cxxflags" and "... --libs"
// and might need a change in CMakeLists.txt

//#define EXAMPLE_WX_USE_GRAPHICS_CONTEXT 1
// #define EXAMPLE_WX_USE_GRAPHICS_CONTEXT 1

#include <fstream>
#include <sstream>
Expand All @@ -36,16 +24,16 @@
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/ring.hpp>

#include "wx/wx.h"
#include "wx/math.h"
#include "wx/stockitem.h"
#include <wx/wx.h>
#include <wx/math.h>
#include <wx/stockitem.h>
#include <wx/cmdline.h>

#ifdef EXAMPLE_WX_USE_GRAPHICS_CONTEXT
#include "wx/graphics.h"
#include "wx/dcgraph.h"
#endif


using point_2d = boost::geometry::model::d2::point_xy<double>;
using country_type = boost::geometry::model::multi_polygon
<
Expand All @@ -58,7 +46,7 @@ BOOST_GEOMETRY_REGISTER_POINT_2D(wxPoint, int, cs::cartesian, x, y)
BOOST_GEOMETRY_REGISTER_POINT_2D(wxRealPoint, double, cs::cartesian, x, y)

// ----------------------------------------------------------------------------
// Read an ASCII file containing WKT's
// Read an ASCII file containing WKT's of either POLYGON or MULTIPOLYGON
// ----------------------------------------------------------------------------
template <typename Geometry, typename Box>
inline void read_wkt(std::string const& filename, std::vector<Geometry>& geometries, Box& box)
Expand All @@ -70,13 +58,24 @@ inline void read_wkt(std::string const& filename, std::vector<Geometry>& geometr
{
std::string line;
std::getline(cpp_file, line);
if (! line.empty())
if (line.empty())
{
continue;
}
Geometry geometry;
if (line.substr(0, 4) == "POLY")
{
boost::geometry::model::polygon<point_2d> polygon;
boost::geometry::read_wkt(line, polygon);
geometry.push_back(polygon);
}
else
{
Geometry geometry;
boost::geometry::read_wkt(line, geometry);
geometries.push_back(geometry);
boost::geometry::expand(box, boost::geometry::return_envelope<Box>(geometry));
}

geometries.push_back(geometry);
boost::geometry::expand(box, boost::geometry::return_envelope<Box>(geometry));
}
}
}
Expand All @@ -99,7 +98,7 @@ class HelloWorldFrame: public wxFrame
class HelloWorldCanvas: public wxWindow
{
public:
HelloWorldCanvas(wxFrame *frame);
HelloWorldCanvas(wxFrame *frame, const std::string& filename);

private:
void DrawCountries(wxDC& dc);
Expand All @@ -122,6 +121,7 @@ class HelloWorldCanvas: public wxWindow
std::shared_ptr<map_transformer_type> m_map_transformer;
std::shared_ptr<inverse_transformer_type> m_inverse_transformer;

std::string m_filename;
boost::geometry::model::box<point_2d> m_box;
std::vector<country_type> m_countries;
int m_focus = -1;
Expand All @@ -132,7 +132,7 @@ class HelloWorldCanvas: public wxWindow

wxFrame* m_owner = nullptr;

DECLARE_EVENT_TABLE()
DECLARE_EVENT_TABLE()
};


Expand All @@ -143,19 +143,18 @@ class HelloWorldApp: public wxApp
public:
bool OnInit()
{
const auto filename = wxApp::argc >= 2 ? wxApp::argv[1].ToStdString() : "example/data/world.wkt";

// Create the main frame window
HelloWorldFrame *frame = new HelloWorldFrame(NULL, _T("Boost.Geometry for wxWidgets - Hello World!"), wxDefaultPosition, wxSize(640, 480));
HelloWorldFrame *frame = new HelloWorldFrame(NULL, _T("Boost.Geometry for wxWidgets - Hello World!"), wxDefaultPosition, wxSize(1024, 768));

wxMenu *file_menu = new wxMenu;
file_menu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT));
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(file_menu, _T("&File"));
frame->SetMenuBar(menuBar);

int width, height;
frame->GetClientSize(&width, &height);

(void) new HelloWorldCanvas(frame);
(void) new HelloWorldCanvas(frame, filename);

// Show the frame
frame->Show(true);
Expand Down Expand Up @@ -191,12 +190,13 @@ void HelloWorldFrame::OnCloseWindow(wxCloseEvent& )


// ----------------------------------------------------------------------------
HelloWorldCanvas::HelloWorldCanvas(wxFrame *frame)
HelloWorldCanvas::HelloWorldCanvas(wxFrame *frame, const std::string& filename)
: wxWindow(frame, wxID_ANY)
, m_owner(frame)
, m_filename(filename)
{
boost::geometry::assign_inverse(m_box);
read_wkt("../data/world.wkt", m_countries, m_box);
read_wkt(m_filename, m_countries, m_box);
}


Expand Down

0 comments on commit a3f9574

Please sign in to comment.