Skip to content

Commit

Permalink
Merge branch 'macos-cursor' of https://github.com/MaartenBent/wxWidgets
Browse files Browse the repository at this point in the history
Implement loading cursor from macOS resources.

See wxWidgets#24374.
  • Loading branch information
vadz committed Apr 8, 2024
2 parents 8f379f2 + 9f5e5e6 commit 6f4f244
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ jobs:
export ASAN_OPTIONS='fast_unwind_on_malloc=0 detect_container_overflow=0'
# Exclude tests that are currently known to fail
wx_tests_selection='~[.] ~[valnum] ~wxPersistTLW ~wxPersistDVC ~wxTopLevel::Show ~wxFont::Weight ~wxFont::NativeFontInfo ~WebView ~RadioButton::Single ~RadioButton::Focus ~wxHtmlPrintout::Pagination ~wxTextCtrl::GetBestSize ~TextCtrlTestCase ~wxExecute::RedirectUTF8 ~Ellipsization::NormalCase ~SpinCtrl::* ~SpinCtrlDouble::* ~NotebookTestCase ~SettingsTestCase ~Window::Show ~ModalDialogsTestCase ~Button::Click ~Button::Disabled ~wxDVC::GetItemRect ~wxDVC::AppendTextColumn ~Grid::KeyboardSelection ~Grid::CellClick ~Grid::ReorderedColumnsCellClick ~Grid::CellSelect ~wxStyledTextCtrl::AutoComp ~EvtLoopTestCase ~EventPropagationTestCase'
./test_gui $wx_tests_selection
./test_gui.app/Contents/MacOS/test_gui $wx_tests_selection
- name: Building samples
if: matrix.skip_testing != true && matrix.skip_samples != true
Expand Down
19 changes: 13 additions & 6 deletions build/cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ endfunction()
# Add sample, test, demo or benchmark
# wx_add(<name> <group> [CONSOLE|CONSOLE_GUI|DLL] [IMPORTANT] [SRC_FILES...]
# [LIBRARIES ...] [NAME target_name] [FOLDER folder]
# [DATA ...] [DEFINITIONS ...] [RES ...] [PLIST ...)
# [DATA ...] [DEFINITIONS ...] [RES ...] [RES_BUNDLE ...] [PLIST ...)
# name default target name
# group can be Samples, Tests, Demos or Benchmarks
# first parameter may be CONSOLE to indicate a console application or DLL to indicate a shared library
Expand All @@ -702,6 +702,7 @@ endfunction()
# DATA followed by required data files. Use a colon to separate different source and dest paths
# DEFINITIONS list of definitions for the target
# RES followed by WIN32 .rc files
# RES_BUNDLE followed by macOS bundle resource files
# PLIST followed by macOS Info.plist.in file
#
# Additionally the following variables may be set before calling wx_add_sample:
Expand All @@ -728,7 +729,7 @@ function(wx_add name group)
cmake_parse_arguments(APP
"CONSOLE;CONSOLE_GUI;DLL;IMPORTANT"
"NAME;FOLDER"
"DATA;DEFINITIONS;DEPENDS;LIBRARIES;RES;PLIST"
"DATA;DEFINITIONS;DEPENDS;LIBRARIES;RES;RES_BUNDLE;PLIST"
${ARGN}
)

Expand All @@ -749,7 +750,7 @@ function(wx_add name group)
return()
endif()
set(SUB_DIR "tests")
set(DEFAULT_RC_FILE "samples/sample.rc")
set(DEFAULT_RC_FILE "tests/test.rc")
elseif(group STREQUAL Demos)
set(SUB_DIR "demos/${name}")
set(DEFAULT_RC_FILE "demos/${name}/${target_name}.rc")
Expand Down Expand Up @@ -791,7 +792,13 @@ function(wx_add name group)
list(APPEND src_files ${wxSOURCE_DIR}/${DEFAULT_RC_FILE})
endif()
elseif(APPLE AND NOT IPHONE)
list(APPEND src_files ${wxSOURCE_DIR}/src/osx/carbon/wxmac.icns)
set(bundle_files "${wxSOURCE_DIR}/src/osx/carbon/wxmac.icns")
if(APP_RES_BUNDLE)
foreach(res ${APP_RES_BUNDLE})
list(APPEND bundle_files "${wxSOURCE_DIR}/${SUB_DIR}/${res}")
endforeach()
endif()
list(APPEND src_files ${bundle_files})
endif()

if(APP_DLL)
Expand Down Expand Up @@ -841,12 +848,12 @@ function(wx_add name group)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/samples)
elseif(group STREQUAL Tests)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/tests)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/samples)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/3rdparty/catch/single_include)
target_include_directories(${target_name} PRIVATE ${wxTOOLKIT_INCLUDE_DIRS})
endif()

if(APP_DATA)
# TODO: handle data files differently for OS X bundles
# Copy data files to output directory
foreach(data_src ${APP_DATA})
string(FIND ${data_src} ":" HAS_COLON)
Expand Down Expand Up @@ -875,7 +882,7 @@ function(wx_add name group)
endif()
set_target_properties(${target_name} PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "${PLIST_FILE}"
RESOURCE "${wxSOURCE_DIR}/src/osx/carbon/wxmac.icns")
RESOURCE "${bundle_files}")
endif()
set_target_properties(${target_name} PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER "org.wxwidgets.${target_name}"
Expand Down
15 changes: 14 additions & 1 deletion build/cmake/tests/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,21 @@ set(TEST_GUI_DATA
intl/ja/internat.po
)

wx_add_test(test_gui CONSOLE_GUI ${TEST_GUI_SRC}
set(TEST_GUI_RES_BUNDLE
horse.png
../include/wx/msw/bullseye.cur
)

# On macOS create an .app bundle so the resources can be added.
# On other platforms, just use a console app with gui libraries.
set(APP_TYPE)
if(NOT APPLE)
set(APP_TYPE CONSOLE_GUI)
endif()

wx_add_test(test_gui ${APP_TYPE} ${TEST_GUI_SRC}
DATA ${TEST_GUI_DATA}
RES_BUNDLE ${TEST_GUI_RES_BUNDLE}
)
if(wxUSE_AUI)
wx_exe_link_libraries(test_gui wxaui)
Expand Down
14 changes: 8 additions & 6 deletions interface/wx/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class wxCursor : public wxGDIObject
Hotspot y coordinate (relative to the top left of the image).
@param maskBits
Bits for a mask bitmap.
@param fg
Foreground colour.
@param bg
Background colour.
@onlyfor{wxgtk}
Expand All @@ -114,7 +118,8 @@ class wxCursor : public wxGDIObject
*/
wxCursor(const char bits[], int width, int height,
int hotSpotX = -1, int hotSpotY = -1,
const char maskBits[] = nullptr);
const char maskBits[] = nullptr,
const wxColour* fg = nullptr, const wxColour* bg = nullptr);

/**
Constructs a cursor by passing a string resource name or filename.
Expand All @@ -135,11 +140,8 @@ class wxCursor : public wxGDIObject
(to load a cursor from a .ico icon file) and @c wxBITMAP_TYPE_ANI
(to load a cursor from a .ani icon file).
- under MacOS, it defaults to @c wxBITMAP_TYPE_MACCURSOR_RESOURCE;
when specifying a string resource name, first the color cursors 'crsr'
and then the black/white cursors 'CURS' in the resource chain are scanned
through. Note that resource forks are deprecated on macOS so this
is only available for legacy reasons and should not be used in
new code.
when specifying a string resource name, first a PNG and then a CUR
image is searched in resources.
- under GTK, it defaults to @c wxBITMAP_TYPE_XPM.
See the wxCursor(const wxImage& image) ctor for more info.
- under X11, it defaults to @c wxBITMAP_TYPE_XPM.
Expand Down
34 changes: 30 additions & 4 deletions src/osx/carbon/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "wx/image.h"
#endif // WX_PRECOMP

#include "wx/filename.h"
#include "wx/stdpaths.h"
#include "wx/xpmdecod.h"

#include "wx/osx/private.h"
Expand Down Expand Up @@ -191,7 +193,7 @@ ClassicCursor gMacCursors[kwxCursorLast+1] =
0x1FF8, 0x1FF8, 0x1FF8, 0x0FF0, 0x07E0, 0x07E0, 0x07E0, 0x07E0},
{0x0008, 0x0008}
},

};

#endif
Expand Down Expand Up @@ -274,15 +276,34 @@ void wxCursor::InitFromImage(const wxImage & image)
wxCursor::wxCursor(const wxString& cursor_file, wxBitmapType flags, int hotSpotX, int hotSpotY)
{
m_refData = new wxCursorRefData;
#if wxUSE_IMAGE
if ( flags == wxBITMAP_TYPE_MACCURSOR_RESOURCE )
{
#if wxOSX_USE_COCOA
wxFAIL_MSG( wxT("Not implemented") );
wxImage image;
wxFileName fileName( wxStandardPaths::Get().GetResourcesDir(), cursor_file, "png" );
if ( image.LoadFile( fileName.GetFullPath(), wxBITMAP_TYPE_PNG ) )
{
image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX );
image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY );
}
else
{
fileName.SetExt( "cur" );
image.LoadFile( fileName.GetFullPath(), wxBITMAP_TYPE_CUR );
}
if ( image.IsOk() )
{
m_refData->DecRef();
m_refData = nullptr;
InitFromImage( image );
}
else
wxLogDebug( "No PNG or CUR cursor image found in Resources" );
#endif
}
else
{
#if wxUSE_IMAGE
wxImage image ;
image.LoadFile( cursor_file, flags ) ;
if ( image.IsOk() )
Expand All @@ -293,8 +314,13 @@ wxCursor::wxCursor(const wxString& cursor_file, wxBitmapType flags, int hotSpotX
m_refData = nullptr ;
InitFromImage( image ) ;
}
#endif
}
#else
wxUnusedVar(cursor_file);
wxUnusedVar(flags);
wxUnusedVar(hotSpotX);
wxUnusedVar(hotSpotY);
#endif
}

// Cursors by stock number
Expand Down
Loading

0 comments on commit 6f4f244

Please sign in to comment.