Skip to content

Commit dc272a0

Browse files
authored
Merge pull request #483 from mcorino/develop
add Help sample and test
2 parents 310416e + 28cc79b commit dc272a0

33 files changed

+935
-10
lines changed

lib/wx/core/context_help.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
2+
#
3+
# This software is released under the MIT license.
4+
5+
module Wx
6+
7+
class << self
8+
9+
define_method :ContextHelp do |window = nil|
10+
11+
context_help(window)
12+
13+
end
14+
15+
end
16+
17+
end

lib/wx/core/helpcontrollerhelpprovider.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@
1212
require_relative './simplehelpprovider'
1313

1414
class Wx::HelpControllerHelpProvider < Wx::SimpleHelpProvider
15-
def initialize(help_controller)
15+
def initialize(help_controller = nil)
1616
super()
1717
@hc = help_controller
1818
end
1919

20+
def get_help_controller
21+
@hc
22+
end
23+
24+
def set_help_controller(help_controller)
25+
@hc = help_controller
26+
end
27+
2028
# Show help for +win+; if the help text for +win+ is a string with a
2129
# single integer only, treats that as a section id for help and shows
2230
# that, otherwise shows a popup (native-style on Windows) of the text.
2331
def show_help(win)
2432
help_text = get_help(win)
25-
return false if help_text.empty?
33+
return false if help_text.empty? || @hc.nil?
2634
if help_text =~ /\A\d+\z/
2735
@hc.display_context_popup(help_text.to_i)
2836
else

lib/wx/doc/context_help.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# :stopdoc:
2+
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
3+
#
4+
# This software is released under the MIT license.
5+
# :startdoc:
6+
7+
8+
module Wx
9+
10+
# This method changes the cursor to a query and puts the application into a 'context-sensitive help mode'.
11+
#
12+
# When the user left-clicks on a window within the specified window, a Wx::EVT_HELP event is sent to that control,
13+
# and the application may respond to it by popping up some help.
14+
# The method returns after the left-click.
15+
#
16+
# For example:
17+
#
18+
# ```ruby
19+
# Wx.context_help(myWindow)
20+
# ```
21+
#
22+
# There are a couple of ways to invoke this behaviour implicitly:
23+
#
24+
# - Use the Wx::DIALOG_EX_CONTEXTHELP style for a dialog (WXMSW only). This will put a question mark in the titlebar,
25+
# and Windows will put the application into context-sensitive help mode automatically, without further programming.
26+
# - Create a Wx::ContextHelpButton, whose predefined behaviour is to create a context help object. Normally you will
27+
# write your application so that this button is only added to a dialog for non-Windows platforms (use
28+
# Wx::DIALOG_EX_CONTEXTHELP on WXMSW).
29+
#
30+
# Note that on macOS, the cursor does not change when in context-sensitive help mode.
31+
#
32+
# @param [Wx::Window] window the window which will be used to catch events; if nullptr, the top window will be used.
33+
# @return [void]
34+
def self.context_help(window = nil); end
35+
36+
# Convenience alias for {Wx.context_help} similarly named as the wxWidgets wxContextHelp class.
37+
#
38+
# @param [Wx::Window] window the window which will be used to catch events; if nullptr, the top window will be used.
39+
# @return [void]
40+
def self.ContextHelp(window = nil); end
41+
42+
end

lib/wx/doc/html/html_help_controller.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,36 @@ module Wx
99

1010
module HTML
1111

12-
class HelpController
12+
class HtmlHelpController
1313

1414
# Returns the latest frame size and position settings and whether a new frame is drawn with each invocation.
1515
# @return [Array(Wx::Frame,Wx::Size,Wx::Point,Boolean)] latest frame settings
1616
def get_frame_parameters; end
1717

1818
end
1919

20+
# This module method uses Wx::HTML::HtmlHelpController to display help in a modal dialog and returns after
21+
# the dialog has been closed.
22+
#
23+
# This is useful on platforms such as WXOSX where if you display help from a modal dialog, the help window must
24+
# itself be a modal dialog.
25+
#
26+
# @param [Wx::Window] parent parent of the dialog.
27+
# @param [String] help_file the HTML help file to show.
28+
# @param [String] topic an optional topic. If this is empty, the help contents will be shown.
29+
# @param [Integer] style is a combination of the flags described in the {Wx::HTML::HtmlHelpController} documentation.
30+
# @return [void]
31+
def self.html_modal_help(parent, help_file, topic = '', style = Wx::HTML::HF_DEFAULT_STYLE); end
32+
33+
# Convenience alias for {Wx::HTML.html_modal_help} similarly named as the wxWidgets wxHtmlModalHelp class.
34+
#
35+
# @param [Wx::Window] parent parent of the dialog.
36+
# @param [String] help_file the HTML help file to show.
37+
# @param [String] topic an optional topic. If this is empty, the help contents will be shown.
38+
# @param [Integer] style is a combination of the flags described in the {Wx::HTML::HtmlHelpController} documentation.
39+
# @return [void]
40+
def self.HtmlModalHelp(parent, help_file, topic, style = Wx::HTML::HF_DEFAULT_STYLE); end
41+
2042
end
2143

2244
end

lib/wx/html/htmlhelpcontroller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ def self.instance(*args)
2727

2828
end
2929

30+
class << self
31+
32+
define_method :HtmlModalHelp do |parent, help_file, topic = '', style = Wx::HTML::HF_DEFAULT_STYLE|
33+
34+
html_modal_help(parent, help_file, topic, style)
35+
36+
end
37+
38+
end
39+
3040
end
3141

3242
end

rakelib/lib/director/context_help_button.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,41 @@ class ContextHelpButton < Button
1616

1717
def setup
1818
super
19-
spec.items << 'wxContextHelp'
20-
spec.no_proxy 'wxContextHelp'
19+
# add custom implementation of ContextHelp as module function (not a class)
20+
spec.add_header_code <<~__CODE
21+
SWIGINTERN int SWIG_AsVal_bool (VALUE obj, bool *val); // forward decl
22+
23+
static VALUE wxruby_ContextHelp(int argc, VALUE *argv, VALUE self)
24+
{
25+
if (argc > 1)
26+
{
27+
rb_raise(rb_eArgError, "wrong # of arguments %d for 1", argc);
28+
return Qnil;
29+
}
30+
31+
void *ptr = nullptr;
32+
wxWindow *window = nullptr;
33+
int res = 0;
34+
35+
if (argc > 0)
36+
{
37+
res = SWIG_ConvertPtr(argv[0], &ptr, SWIGTYPE_p_wxWindow, 0);
38+
if (!SWIG_IsOK(res))
39+
{
40+
VALUE msg = rb_inspect(argv[0]);
41+
rb_raise(rb_eTypeError, "expected wxWindow* for 1 but got %s", StringValuePtr(msg));
42+
return Qnil;
43+
}
44+
window = reinterpret_cast< wxWindow * >(ptr);
45+
}
46+
47+
wxContextHelp(window, true);
48+
return Qnil;
49+
}
50+
__CODE
51+
spec.add_init_code <<~__CODE__
52+
rb_define_module_function(mWxCore, "ContextHelp", VALUEFUNC(wxruby_ContextHelp), -1);
53+
__CODE__
2154
end
2255
end # class ContextHelpButton
2356

rakelib/lib/director/help_controller.rb

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ def setup
2121
spec.rename_for_ruby('Init' => "#{spec.module_name}::Initialize")
2222
# ignore these (pure virtual) decls
2323
spec.ignore %w[
24+
wxHelpControllerBase::DisplayContents
2425
wxHelpControllerBase::DisplayBlock
2526
wxHelpControllerBase::DisplaySection
27+
wxHelpControllerBase::KeywordSearch
2628
wxHelpControllerBase::LoadFile
2729
wxHelpControllerBase::Quit
28-
]
30+
], ignore_doc: false
2931
# and add them as the implemented overrides they are
3032
spec.extend_interface spec.module_name,
33+
'virtual bool DisplayContents()',
3134
'virtual bool DisplayBlock(long blockNo)',
3235
'virtual bool DisplaySection(int sectionNo)',
36+
'virtual bool DisplaySection(const wxString &section)',
37+
'virtual bool KeywordSearch(const wxString &keyWord, wxHelpSearchMode mode=wxHELP_SEARCH_ALL)',
3338
'virtual bool LoadFile(const wxString &file=wxEmptyString)',
3439
'virtual bool Quit()'
3540
# ignore this problematic method
@@ -62,11 +67,74 @@ def setup
6267
spec.ignore 'wxHtmlHelpController::CreateHelpFrame',
6368
'wxHtmlHelpController::CreateHelpDialog',
6469
'wxHtmlHelpController::GetFrame',
65-
'wxHtmlHelpController::GetDialog'
70+
'wxHtmlHelpController::GetDialog',
71+
'wxHtmlHelpController::DisplayContents',
72+
'wxHtmlHelpController::KeywordSearch'
73+
# add custom implementation of HtmlModalHelp as module function (not a class)
74+
spec.add_header_code <<~__CODE
75+
static VALUE wxruby_HtmlModalHelp(int argc, VALUE *argv, VALUE self)
76+
{
77+
if (argc < 2 || argc > 4)
78+
{
79+
rb_raise(rb_eArgError, "wrong # of arguments %d for 2 (max 4)", argc);
80+
return Qnil;
81+
}
82+
83+
void *ptr = nullptr;
84+
wxWindow *parent = nullptr;
85+
wxString help_file;
86+
wxString topic = wxEmptyString;
87+
int style = wxHF_DEFAULT_STYLE;
88+
int res = 0;
89+
90+
res = SWIG_ConvertPtr(argv[0], &ptr, SWIGTYPE_p_wxWindow, 0);
91+
if (!SWIG_IsOK(res))
92+
{
93+
VALUE msg = rb_inspect(argv[0]);
94+
rb_raise(rb_eTypeError, "expected wxWindow* for 1 but got %s", StringValuePtr(msg));
95+
return Qnil;
96+
}
97+
parent = reinterpret_cast< wxWindow * >(ptr);
98+
if (TYPE(argv[1]) != T_STRING)
99+
{
100+
VALUE msg = rb_inspect(argv[1]);
101+
rb_raise(rb_eTypeError, "expected String for 2 but got %s", StringValuePtr(msg));
102+
return Qnil;
103+
}
104+
help_file = RSTR_TO_WXSTR(argv[1]);
105+
if (argc > 2)
106+
{
107+
if (TYPE(argv[2]) != T_STRING)
108+
{
109+
VALUE msg = rb_inspect(argv[2]);
110+
rb_raise(rb_eTypeError, "expected String for 3 but got %s", StringValuePtr(msg));
111+
return Qnil;
112+
}
113+
topic = RSTR_TO_WXSTR(argv[2]);
114+
}
115+
if (argc > 3)
116+
{
117+
if (TYPE(argv[3]) != T_FIXNUM)
118+
{
119+
VALUE msg = rb_inspect(argv[3]);
120+
rb_raise(rb_eTypeError, "expected Integer for 4 but got %s", StringValuePtr(msg));
121+
return Qnil;
122+
}
123+
style = NUM2INT(argv[3]);
124+
}
125+
wxHtmlModalHelp(parent, help_file, topic, style);
126+
return Qnil;
127+
}
128+
__CODE
129+
spec.add_init_code <<~__CODE__
130+
rb_define_module_function(mWxHtmlHelpController, "HtmlModalHelp", VALUEFUNC(wxruby_HtmlModalHelp), -1);
131+
__CODE__
66132
elsif spec.module_name == 'wxExtHelpController'
67133
spec.ignore %w[
134+
wxExtHelpController::DisplayContents
68135
wxExtHelpController::DisplayBlock
69136
wxExtHelpController::DisplaySection
137+
wxExtHelpController::KeywordSearch
70138
wxExtHelpController::LoadFile
71139
wxExtHelpController::Quit
72140
wxExtHelpController::GetFrameParameters

samples/help/doc.chm

13.2 KB
Binary file not shown.

samples/help/doc.chw

10.7 KB
Binary file not shown.

samples/help/doc.zip

4.88 KB
Binary file not shown.

0 commit comments

Comments
 (0)