Skip to content
Snippets Groups Projects
Commit 3db42c8f authored by sqweek's avatar sqweek
Browse files

dlgs: implement error message dialog

parent 79cfa3b4
Branches
No related tags found
No related merge requests found
......@@ -11,20 +11,43 @@ import (
"unsafe"
)
type AlertParams struct {
p C.AlertDlgParams
}
func mkAlertParams(msg, title string, style C.AlertStyle) *AlertParams {
a := AlertParams{C.AlertDlgParams{msg: C.CString(msg), style: style}}
if title != "" {
a.p.title = C.CString(title)
}
return &a
}
func (a *AlertParams) run() C.DlgResult {
return C.alertDlg(&a.p)
}
func (a *AlertParams) free() {
C.free(unsafe.Pointer(a.p.msg))
if a.p.title != nil {
C.free(unsafe.Pointer(a.p.title))
}
}
func nsStr(s string) unsafe.Pointer {
return C.NSStr(unsafe.Pointer(&[]byte(s)[0]), C.int(len(s)))
}
func YesNoDlg(msg string, title string) bool {
p := C.AlertDlgParams{
msg: C.CString(msg),
}
defer C.free(unsafe.Pointer(p.msg))
if title != "" {
p.title = C.CString(title)
defer C.free(unsafe.Pointer(p.title))
func YesNoDlg(msg, title string) bool {
a := mkAlertParams(msg, title, C.MSG_YESNO)
defer a.free()
return a.run() == C.DLG_OK
}
return C.alertDlg(&p) == C.DLG_OK
func ErrorDlg(msg, title string) {
a := mkAlertParams(msg, title, C.MSG_ERROR)
defer a.free()
a.run()
}
func FileDlg(save int, title string, exts []string, relaxExt bool) (string, error) {
......
#include <objc/NSObjcRuntime.h>
typedef enum {
MSG_YESNO,
MSG_ERROR,
} AlertStyle;
typedef struct {
char* msg;
char* title;
AlertStyle style;
} AlertDlgParams;
typedef struct {
......
......@@ -38,8 +38,16 @@ DlgResult alertDlg(AlertDlgParams* params) {
[[alert window] setTitle:[[NSString alloc] initWithUTF8String:self->params->title]];
}
[alert setMessageText:[[NSString alloc] initWithUTF8String:self->params->msg]];
switch (self->params->style) {
case MSG_YESNO:
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
break;
case MSG_ERROR:
[alert setIcon:[NSImage imageNamed:NSImageNameCaution]];
[alert addButtonWithTitle:@"OK"];
break;
}
self->result = [alert runModal] == NSAlertFirstButtonReturn ? DLG_OK : DLG_CANCEL;
return self->result;
}
......
......@@ -46,6 +46,11 @@ func (b *MsgBuilder) YesNo() bool {
return b.yesNo()
}
/* Error spawns the message dialog with an error icon and single button, "Ok". */
func (b *MsgBuilder) Error() {
b.error()
}
/* FileFilter represents a category of files (eg. audio files, spreadsheets). */
type FileFilter struct {
Desc string
......
......@@ -8,6 +8,10 @@ func (b *MsgBuilder) yesNo() bool {
return cocoa.YesNoDlg(b.Msg, b.Dlg.Title)
}
func (b *MsgBuilder) error() {
cocoa.ErrorDlg(b.Msg, b.Dlg.Title)
}
func (b *FileBuilder) load() (string, error) {
return b.run(0)
}
......
......@@ -29,6 +29,13 @@ func (b *MsgBuilder) yesNo() bool {
return dlg.Run() == gtk.RESPONSE_YES
}
func (b *MsgBuilder) error() {
dlg := gtk.NewMessageDialog(nil, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "%s", b.Msg)
dlg.SetTitle(firstOf(b.Dlg.Title, "Error"))
defer closeDialog(&dlg.Dialog)
dlg.Run()
}
func (b *FileBuilder) load() (string, error) {
return chooseFile("Load", gtk.FILE_CHOOSER_ACTION_OPEN, b)
}
......
......@@ -28,6 +28,10 @@ func (b *MsgBuilder) yesNo() bool {
return r == w32.IDYES
}
func (b *MsgBuilder) error() {
w32.MessageBox(w32.HWND(0), firstOf(b.Dlg.Title, "Error"), b.Msg, w32.MB_OK | w32.MB_ICONERROR)
}
type filedlg struct {
buf []uint16
filters []uint16
......
......@@ -48,6 +48,8 @@ func events(events <-chan interface{}) {
fmt.Println(dialog.Message("Is this sentence false?").YesNo())
case e.Glyph == "b":
fmt.Println(dialog.Message("R U OK?").Title("Just checking").YesNo())
case e.Glyph == "c":
dialog.Message("Operation failed").Error()
}
case wde.CloseEvent:
wde.Stop()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment