diff options
| author | trustable-code <krauter.simon@arcor.de> | 2018-09-24 19:57:05 +0200 |
|---|---|---|
| committer | trustable-code <krauter.simon@arcor.de> | 2018-09-24 19:57:05 +0200 |
| commit | 89a4ecc77c7833671913dbc7923db5329f22c447 (patch) | |
| tree | 95ef31e46a4aba32bf7ada9b06d835dd3a5a6d42 | |
| parent | 40ee93c6fb162ed5c20853060099088e0b568e31 (diff) | |
| download | NiGui-89a4ecc77c7833671913dbc7923db5329f22c447.tar.gz NiGui-89a4ecc77c7833671913dbc7923db5329f22c447.zip | |
Improve messages boxes and buttons
- Message box: button1 will be focused
- Buttons: Key_Return and Key_Space are now handled in
platform-independent code
| -rwxr-xr-x | examples/example_04_msgboxes.nim | 2 | ||||
| -rwxr-xr-x | src/nigui.nim | 7 | ||||
| -rwxr-xr-x | src/nigui/msgbox.nim | 12 | ||||
| -rwxr-xr-x | src/nigui/private/gtk3/platform_impl.nim | 11 | ||||
| -rwxr-xr-x | src/nigui/private/windows/platform_impl.nim | 15 |
5 files changed, 25 insertions, 22 deletions
diff --git a/examples/example_04_msgboxes.nim b/examples/example_04_msgboxes.nim index aef4bcc..c5a9b66 100755 --- a/examples/example_04_msgboxes.nim +++ b/examples/example_04_msgboxes.nim @@ -30,7 +30,7 @@ button2.onClick = proc(event: ClickEvent) = var button3 = newButton("Example 3") buttons.add(button3) button3.onClick = proc(event: ClickEvent) = - let res = window.msgBox("Hello.\n\nThis message box is created with \"msgBox()\" and has three buttons.", "Title of message box", "Button 1", " Button 2", "Button 3") + let res = window.msgBox("Hello.\n\nThis message box is created with \"msgBox()\" and has three buttons.", "Title of message box", "Button 1", "Button 2", "Button 3") textArea.addLine("Message box closed, result = " & $res) window.show() diff --git a/src/nigui.nim b/src/nigui.nim index 7f64f95..55bb1ae 100755 --- a/src/nigui.nim +++ b/src/nigui.nim @@ -2244,6 +2244,13 @@ method enabled(button: Button): bool = button.fEnabled method `enabled=`(button: Button, enabled: bool) = discard # has to be implemented by NativeTextBox +method handleKeyDownEvent*(button: Button, event: KeyboardEvent) = + if event.key == Key_Return or event.key == Key_Space: + var clickEvent = new ClickEvent + clickEvent.control = button + button.handleClickEvent(clickEvent) + + method `onDraw=`(container: NativeButton, callback: DrawProc) = raiseError("NativeButton does not allow onDraw.") diff --git a/src/nigui/msgbox.nim b/src/nigui/msgbox.nim index c955c03..eefb57a 100755 --- a/src/nigui/msgbox.nim +++ b/src/nigui/msgbox.nim @@ -3,10 +3,11 @@ # This module provides an extended message box.
# The message box is shown as modal window.
# The message box can have up to 3 buttons with customizable titles.
+# button1 will be focused.
# Call the proc msgBox() to open the message box.
# It will wait until the message box is closed.
# Meaning of the result value:
-# 0 - message box was closed over the window close button
+# 0 - message box was closed over the window close button or escape key
# 1..3 - button 1..3 clicked
# For an example see "example_04_msgboxes.nim".
@@ -26,6 +27,10 @@ proc msgBox*(parent: Window, message: string, title = "Message", button1 = "OK", window.init()
window.title = title
+ window.onKeyDown = proc(event: KeyboardEvent) =
+ if event.key == Key_Escape:
+ window.dispose()
+
var container = newLayoutContainer(Layout_Vertical)
container.padding = 10
window.control = container
@@ -49,6 +54,7 @@ proc msgBox*(parent: Window, message: string, title = "Message", button1 = "OK", b1.minWidth = buttonMinWidth
b1.onClick = buttonClick
buttonContainer.add(b1)
+ b1.focus()
if button2 != "":
b2 = newButton(button2)
@@ -76,7 +82,7 @@ proc msgBox*(parent: Window, message: string, title = "Message", button1 = "OK", if window.clickedButton == b1:
result = 1
- elif window.clickedButton == b2:
+ elif window.clickedButton == b2 and b2 != nil:
result = 2
- elif window.clickedButton == b3:
+ elif window.clickedButton == b3 and b3 != nil:
result = 3
diff --git a/src/nigui/private/gtk3/platform_impl.nim b/src/nigui/private/gtk3/platform_impl.nim index c66dfba..152f7b6 100755 --- a/src/nigui/private/gtk3/platform_impl.nim +++ b/src/nigui/private/gtk3/platform_impl.nim @@ -832,9 +832,9 @@ method `visible=`(control: ControlImpl, visible: bool) = else: gtk_widget_hide(control.fHandle) -proc dummy(widget: pointer, event: var GdkEventButton, data: pointer): bool {.cdecl.} = - echo "dummy" - result = true # Stop propagation +# proc dummy(widget: pointer, event: var GdkEventButton, data: pointer): bool {.cdecl.} = + # echo "dummy" + # result = true # Stop propagation method pUpdateScrollBar(control: ControlImpl) = if control.fScrollableWidth == -1 and control.fScrollableHeight == -1: @@ -1093,7 +1093,6 @@ method getPadding(frame: NativeFrame): Spacing = proc init(button: NativeButton) = button.fHandle = gtk_button_new() button.Button.init() - # discard g_signal_connect_data(button.fHandle, "clicked", pWidgetClickSignal, cast[pointer](button)) method `text=`(button: NativeButton, text: string) = procCall button.Button.`text=`(text) @@ -1111,10 +1110,6 @@ method naturalWidth(button: NativeButton): int = gtk_style_context_get_padding(context, GTK_STATE_FLAG_NORMAL, padding) result = button.getTextLineWidth(button.text) + padding.left + padding.right + 5 -method pAddButtonPressEvent(control: NativeButton) = - gtk_widget_add_events(control.fHandle, GDK_BUTTON_PRESS_MASK) - discard g_signal_connect_data(control.fHandle, "button-press-event", pDefaultControlButtonPressSignal, cast[pointer](control)) - method `enabled=`(button: NativeButton, enabled: bool) = button.fEnabled = enabled gtk_widget_set_sensitive(button.fHandle, enabled) diff --git a/src/nigui/private/windows/platform_impl.nim b/src/nigui/private/windows/platform_impl.nim index fbc833a..1b36d10 100755 --- a/src/nigui/private/windows/platform_impl.nim +++ b/src/nigui/private/windows/platform_impl.nim @@ -1280,16 +1280,11 @@ method getPadding(frame: NativeFrame): Spacing = var pButtonOrigWndProc: pointer proc pButtonWndProc(hWnd: pointer, uMsg: int32, wParam, lParam: pointer): pointer {.cdecl.} = - case uMsg - of WM_KEYDOWN: - let button = cast[Button](pGetWindowLongPtr(hWnd, GWLP_USERDATA)) - # if button != nil and (cast[int](wParam) == 13 or cast[int](wParam) == 32): - if button != nil and cast[int](wParam) == 13: - var event = new ClickEvent - event.control = button - button.handleClickEvent(event) - else: - discard + # case uMsg + # of WM_KEYDOWN: + # let button = cast[Button](pGetWindowLongPtr(hWnd, GWLP_USERDATA)) + # else: + # discard let comProcRes = pCommonControlWndProc(hWnd, uMsg, wParam, lParam) if comProcRes == PWndProcResult_False: return cast[pointer](false) |
