github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/dialog.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
     6   */
     7  
     8  package winc
     9  
    10  import "github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    11  
    12  // Dialog displayed as z-order top window until closed.
    13  // It also disables parent window so it can not be clicked.
    14  type Dialog struct {
    15  	Form
    16  	isModal bool
    17  
    18  	btnOk     *PushButton
    19  	btnCancel *PushButton
    20  
    21  	onLoad   EventManager
    22  	onOk     EventManager
    23  	onCancel EventManager
    24  }
    25  
    26  func NewDialog(parent Controller) *Dialog {
    27  	dlg := new(Dialog)
    28  
    29  	dlg.isForm = true
    30  	dlg.isModal = true
    31  	RegClassOnlyOnce("winc_Dialog")
    32  
    33  	dlg.hwnd = CreateWindow("winc_Dialog", parent, w32.WS_EX_CONTROLPARENT, /* IMPORTANT */
    34  		w32.WS_SYSMENU|w32.WS_CAPTION|w32.WS_THICKFRAME /*|w32.WS_BORDER|w32.WS_POPUP*/)
    35  	dlg.parent = parent
    36  
    37  	// dlg might fail if icon resource is not embedded in the binary
    38  	if ico, err := NewIconFromResource(GetAppInstance(), uint16(AppIconID)); err == nil {
    39  		dlg.SetIcon(0, ico)
    40  	}
    41  
    42  	// Dlg forces display of focus rectangles, as soon as the user starts to type.
    43  	w32.SendMessage(dlg.hwnd, w32.WM_CHANGEUISTATE, w32.UIS_INITIALIZE, 0)
    44  	RegMsgHandler(dlg)
    45  
    46  	dlg.SetFont(DefaultFont)
    47  	dlg.SetText("Form")
    48  	dlg.SetSize(200, 100)
    49  	return dlg
    50  }
    51  
    52  func (dlg *Dialog) SetModal(modal bool) {
    53  	dlg.isModal = modal
    54  }
    55  
    56  // SetButtons wires up dialog events to buttons. btnCancel can be nil.
    57  func (dlg *Dialog) SetButtons(btnOk *PushButton, btnCancel *PushButton) {
    58  	dlg.btnOk = btnOk
    59  	dlg.btnOk.SetDefault()
    60  	dlg.btnCancel = btnCancel
    61  }
    62  
    63  // Events
    64  func (dlg *Dialog) OnLoad() *EventManager {
    65  	return &dlg.onLoad
    66  }
    67  
    68  func (dlg *Dialog) OnOk() *EventManager {
    69  	return &dlg.onOk
    70  }
    71  
    72  func (dlg *Dialog) OnCancel() *EventManager {
    73  	return &dlg.onCancel
    74  }
    75  
    76  // PreTranslateMessage handles dialog specific messages. IMPORTANT.
    77  func (dlg *Dialog) PreTranslateMessage(msg *w32.MSG) bool {
    78  	if msg.Message >= w32.WM_KEYFIRST && msg.Message <= w32.WM_KEYLAST {
    79  		if w32.IsDialogMessage(dlg.hwnd, msg) {
    80  			return true
    81  		}
    82  	}
    83  	return false
    84  }
    85  
    86  // Show dialog performs special setup for dialog windows.
    87  func (dlg *Dialog) Show() {
    88  	if dlg.isModal {
    89  		dlg.Parent().SetEnabled(false)
    90  	}
    91  	dlg.onLoad.Fire(NewEvent(dlg, nil))
    92  	dlg.Form.Show()
    93  }
    94  
    95  // Close dialog when you done with it.
    96  func (dlg *Dialog) Close() {
    97  	if dlg.isModal {
    98  		dlg.Parent().SetEnabled(true)
    99  	}
   100  	dlg.ControlBase.Close()
   101  }
   102  
   103  func (dlg *Dialog) cancel() {
   104  	if dlg.btnCancel != nil {
   105  		dlg.btnCancel.onClick.Fire(NewEvent(dlg.btnCancel, nil))
   106  	}
   107  	dlg.onCancel.Fire(NewEvent(dlg, nil))
   108  }
   109  
   110  func (dlg *Dialog) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
   111  	switch msg {
   112  	case w32.WM_COMMAND:
   113  		switch w32.LOWORD(uint32(wparam)) {
   114  		case w32.IDOK:
   115  			if dlg.btnOk != nil {
   116  				dlg.btnOk.onClick.Fire(NewEvent(dlg.btnOk, nil))
   117  			}
   118  			dlg.onOk.Fire(NewEvent(dlg, nil))
   119  			return w32.TRUE
   120  
   121  		case w32.IDCANCEL:
   122  			dlg.cancel()
   123  			return w32.TRUE
   124  		}
   125  
   126  	case w32.WM_CLOSE:
   127  		dlg.cancel() // use onCancel or dlg.btnCancel.OnClick to close
   128  		return 0
   129  
   130  	case w32.WM_DESTROY:
   131  		if dlg.isModal {
   132  			dlg.Parent().SetEnabled(true)
   133  		}
   134  	}
   135  	return w32.DefWindowProc(dlg.hwnd, msg, wparam, lparam)
   136  }