1// Same copyright and license as the rest of the files in this project
2// This file contains accelerator related functions and structures
3
4package gtk
5
6// #include <gtk/gtk.h>
7// #include "gtk.go.h"
8import "C"
9import (
10	"errors"
11	"unsafe"
12
13	"github.com/gotk3/gotk3/gdk"
14	"github.com/gotk3/gotk3/glib"
15)
16
17/*
18 * GtkWindow
19 */
20
21// Window is a representation of GTK's GtkWindow.
22type Window struct {
23	Bin
24}
25
26// IWindow is an interface type implemented by all structs embedding a
27// Window.  It is meant to be used as an argument type for wrapper
28// functions that wrap around a C GTK function taking a GtkWindow.
29type IWindow interface {
30	toWindow() *C.GtkWindow
31}
32
33// native returns a pointer to the underlying GtkWindow.
34func (v *Window) native() *C.GtkWindow {
35	if v == nil || v.GObject == nil {
36		return nil
37	}
38	p := unsafe.Pointer(v.GObject)
39	return C.toGtkWindow(p)
40}
41
42func (v *Window) toWindow() *C.GtkWindow {
43	if v == nil {
44		return nil
45	}
46	return v.native()
47}
48
49func marshalWindow(p uintptr) (interface{}, error) {
50	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
51	obj := glib.Take(unsafe.Pointer(c))
52	return wrapWindow(obj), nil
53}
54
55func wrapWindow(obj *glib.Object) *Window {
56	return &Window{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}
57}
58
59// WindowNew is a wrapper around gtk_window_new().
60func WindowNew(t WindowType) (*Window, error) {
61	c := C.gtk_window_new(C.GtkWindowType(t))
62	if c == nil {
63		return nil, nilPtrErr
64	}
65	return wrapWindow(glib.Take(unsafe.Pointer(c))), nil
66}
67
68// SetTitle is a wrapper around gtk_window_set_title().
69func (v *Window) SetTitle(title string) {
70	cstr := C.CString(title)
71	defer C.free(unsafe.Pointer(cstr))
72	C.gtk_window_set_title(v.native(), (*C.gchar)(cstr))
73}
74
75// SetResizable is a wrapper around gtk_window_set_resizable().
76func (v *Window) SetResizable(resizable bool) {
77	C.gtk_window_set_resizable(v.native(), gbool(resizable))
78}
79
80// GetResizable is a wrapper around gtk_window_get_resizable().
81func (v *Window) GetResizable() bool {
82	c := C.gtk_window_get_resizable(v.native())
83	return gobool(c)
84}
85
86// ActivateFocus is a wrapper around gtk_window_activate_focus().
87func (v *Window) ActivateFocus() bool {
88	c := C.gtk_window_activate_focus(v.native())
89	return gobool(c)
90}
91
92// ActivateDefault is a wrapper around gtk_window_activate_default().
93func (v *Window) ActivateDefault() bool {
94	c := C.gtk_window_activate_default(v.native())
95	return gobool(c)
96}
97
98// SetModal is a wrapper around gtk_window_set_modal().
99func (v *Window) SetModal(modal bool) {
100	C.gtk_window_set_modal(v.native(), gbool(modal))
101}
102
103// SetDefaultSize is a wrapper around gtk_window_set_default_size().
104func (v *Window) SetDefaultSize(width, height int) {
105	C.gtk_window_set_default_size(v.native(), C.gint(width), C.gint(height))
106}
107
108// SetIcon is a wrapper around gtk_window_set_icon().
109func (v *Window) SetIcon(icon *gdk.Pixbuf) {
110	iconPtr := (*C.GdkPixbuf)(unsafe.Pointer(icon.Native()))
111	C.gtk_window_set_icon(v.native(), iconPtr)
112}
113
114// WindowSetDefaultIcon is a wrapper around gtk_window_set_default_icon().
115func WindowSetDefaultIcon(icon *gdk.Pixbuf) {
116	iconPtr := (*C.GdkPixbuf)(unsafe.Pointer(icon.Native()))
117	C.gtk_window_set_default_icon(iconPtr)
118}
119
120// TODO(jrick) GdkGeometry GdkWindowHints.
121/*
122func (v *Window) SetGeometryHints() {
123}
124*/
125
126// SetGravity is a wrapper around gtk_window_set_gravity().
127func (v *Window) SetGravity(gravity gdk.GdkGravity) {
128	C.gtk_window_set_gravity(v.native(), C.GdkGravity(gravity))
129}
130
131// TODO(jrick) GdkGravity.
132/*
133func (v *Window) GetGravity() {
134}
135*/
136
137// SetPosition is a wrapper around gtk_window_set_position().
138func (v *Window) SetPosition(position WindowPosition) {
139	C.gtk_window_set_position(v.native(), C.GtkWindowPosition(position))
140}
141
142// SetTransientFor is a wrapper around gtk_window_set_transient_for().
143func (v *Window) SetTransientFor(parent IWindow) {
144	var pw *C.GtkWindow = nil
145	if parent != nil {
146		pw = parent.toWindow()
147	}
148	C.gtk_window_set_transient_for(v.native(), pw)
149}
150
151// SetDestroyWithParent is a wrapper around
152// gtk_window_set_destroy_with_parent().
153func (v *Window) SetDestroyWithParent(setting bool) {
154	C.gtk_window_set_destroy_with_parent(v.native(), gbool(setting))
155}
156
157// SetHideTitlebarWhenMaximized is a wrapper around
158// gtk_window_set_hide_titlebar_when_maximized().
159func (v *Window) SetHideTitlebarWhenMaximized(setting bool) {
160	C.gtk_window_set_hide_titlebar_when_maximized(v.native(),
161		gbool(setting))
162}
163
164// IsActive is a wrapper around gtk_window_is_active().
165func (v *Window) IsActive() bool {
166	c := C.gtk_window_is_active(v.native())
167	return gobool(c)
168}
169
170// HasToplevelFocus is a wrapper around gtk_window_has_toplevel_focus().
171func (v *Window) HasToplevelFocus() bool {
172	c := C.gtk_window_has_toplevel_focus(v.native())
173	return gobool(c)
174}
175
176// GetFocus is a wrapper around gtk_window_get_focus().
177func (v *Window) GetFocus() (*Widget, error) {
178	c := C.gtk_window_get_focus(v.native())
179	if c == nil {
180		return nil, nilPtrErr
181	}
182	return wrapWidget(glib.Take(unsafe.Pointer(c))), nil
183}
184
185// SetFocus is a wrapper around gtk_window_set_focus().
186func (v *Window) SetFocus(w *Widget) {
187	C.gtk_window_set_focus(v.native(), w.native())
188}
189
190// GetDefaultWidget is a wrapper around gtk_window_get_default_widget().
191func (v *Window) GetDefaultWidget() *Widget {
192	c := C.gtk_window_get_default_widget(v.native())
193	if c == nil {
194		return nil
195	}
196	obj := glib.Take(unsafe.Pointer(c))
197	return wrapWidget(obj)
198}
199
200// SetDefault is a wrapper around gtk_window_set_default().
201func (v *Window) SetDefault(widget IWidget) {
202	C.gtk_window_set_default(v.native(), widget.toWidget())
203}
204
205// Present is a wrapper around gtk_window_present().
206func (v *Window) Present() {
207	C.gtk_window_present(v.native())
208}
209
210// PresentWithTime is a wrapper around gtk_window_present_with_time().
211func (v *Window) PresentWithTime(ts uint32) {
212	C.gtk_window_present_with_time(v.native(), C.guint32(ts))
213}
214
215// Iconify is a wrapper around gtk_window_iconify().
216func (v *Window) Iconify() {
217	C.gtk_window_iconify(v.native())
218}
219
220// Deiconify is a wrapper around gtk_window_deiconify().
221func (v *Window) Deiconify() {
222	C.gtk_window_deiconify(v.native())
223}
224
225// Stick is a wrapper around gtk_window_stick().
226func (v *Window) Stick() {
227	C.gtk_window_stick(v.native())
228}
229
230// Unstick is a wrapper around gtk_window_unstick().
231func (v *Window) Unstick() {
232	C.gtk_window_unstick(v.native())
233}
234
235// Maximize is a wrapper around gtk_window_maximize().
236func (v *Window) Maximize() {
237	C.gtk_window_maximize(v.native())
238}
239
240// Unmaximize is a wrapper around gtk_window_unmaximize().
241func (v *Window) Unmaximize() {
242	C.gtk_window_unmaximize(v.native())
243}
244
245// Fullscreen is a wrapper around gtk_window_fullscreen().
246func (v *Window) Fullscreen() {
247	C.gtk_window_fullscreen(v.native())
248}
249
250// Unfullscreen is a wrapper around gtk_window_unfullscreen().
251func (v *Window) Unfullscreen() {
252	C.gtk_window_unfullscreen(v.native())
253}
254
255// SetKeepAbove is a wrapper around gtk_window_set_keep_above().
256func (v *Window) SetKeepAbove(setting bool) {
257	C.gtk_window_set_keep_above(v.native(), gbool(setting))
258}
259
260// SetKeepBelow is a wrapper around gtk_window_set_keep_below().
261func (v *Window) SetKeepBelow(setting bool) {
262	C.gtk_window_set_keep_below(v.native(), gbool(setting))
263}
264
265// SetDecorated is a wrapper around gtk_window_set_decorated().
266func (v *Window) SetDecorated(setting bool) {
267	C.gtk_window_set_decorated(v.native(), gbool(setting))
268}
269
270// SetDeletable is a wrapper around gtk_window_set_deletable().
271func (v *Window) SetDeletable(setting bool) {
272	C.gtk_window_set_deletable(v.native(), gbool(setting))
273}
274
275// SetTypeHint is a wrapper around gtk_window_set_type_hint().
276func (v *Window) SetTypeHint(typeHint gdk.WindowTypeHint) {
277	C.gtk_window_set_type_hint(v.native(), C.GdkWindowTypeHint(typeHint))
278}
279
280// SetSkipTaskbarHint is a wrapper around gtk_window_set_skip_taskbar_hint().
281func (v *Window) SetSkipTaskbarHint(setting bool) {
282	C.gtk_window_set_skip_taskbar_hint(v.native(), gbool(setting))
283}
284
285// SetSkipPagerHint is a wrapper around gtk_window_set_skip_pager_hint().
286func (v *Window) SetSkipPagerHint(setting bool) {
287	C.gtk_window_set_skip_pager_hint(v.native(), gbool(setting))
288}
289
290// SetUrgencyHint is a wrapper around gtk_window_set_urgency_hint().
291func (v *Window) SetUrgencyHint(setting bool) {
292	C.gtk_window_set_urgency_hint(v.native(), gbool(setting))
293}
294
295// SetAcceptFocus is a wrapper around gtk_window_set_accept_focus().
296func (v *Window) SetAcceptFocus(setting bool) {
297	C.gtk_window_set_accept_focus(v.native(), gbool(setting))
298}
299
300// SetFocusOnMap is a wrapper around gtk_window_set_focus_on_map().
301func (v *Window) SetFocusOnMap(setting bool) {
302	C.gtk_window_set_focus_on_map(v.native(), gbool(setting))
303}
304
305// SetStartupID is a wrapper around gtk_window_set_startup_id().
306func (v *Window) SetStartupID(sid string) {
307	cstr := (*C.gchar)(C.CString(sid))
308	defer C.free(unsafe.Pointer(cstr))
309	C.gtk_window_set_startup_id(v.native(), cstr)
310}
311
312// SetRole is a wrapper around gtk_window_set_role().
313func (v *Window) SetRole(s string) {
314	cstr := (*C.gchar)(C.CString(s))
315	defer C.free(unsafe.Pointer(cstr))
316	C.gtk_window_set_role(v.native(), cstr)
317}
318
319// GetDecorated is a wrapper around gtk_window_get_decorated().
320func (v *Window) GetDecorated() bool {
321	c := C.gtk_window_get_decorated(v.native())
322	return gobool(c)
323}
324
325// GetDeletable is a wrapper around gtk_window_get_deletable().
326func (v *Window) GetDeletable() bool {
327	c := C.gtk_window_get_deletable(v.native())
328	return gobool(c)
329}
330
331// WindowGetDefaultIconName is a wrapper around gtk_window_get_default_icon_name().
332func WindowGetDefaultIconName() (string, error) {
333	return stringReturn(C.gtk_window_get_default_icon_name())
334}
335
336// GetDefaultSize is a wrapper around gtk_window_get_default_size().
337func (v *Window) GetDefaultSize() (width, height int) {
338	var w, h C.gint
339	C.gtk_window_get_default_size(v.native(), &w, &h)
340	return int(w), int(h)
341}
342
343// GetDestroyWithParent is a wrapper around
344// gtk_window_get_destroy_with_parent().
345func (v *Window) GetDestroyWithParent() bool {
346	c := C.gtk_window_get_destroy_with_parent(v.native())
347	return gobool(c)
348}
349
350// GetHideTitlebarWhenMaximized is a wrapper around
351// gtk_window_get_hide_titlebar_when_maximized().
352func (v *Window) GetHideTitlebarWhenMaximized() bool {
353	c := C.gtk_window_get_hide_titlebar_when_maximized(v.native())
354	return gobool(c)
355}
356
357// GetIcon is a wrapper around gtk_window_get_icon().
358func (v *Window) GetIcon() (*gdk.Pixbuf, error) {
359	c := C.gtk_window_get_icon(v.native())
360	if c == nil {
361		return nil, nilPtrErr
362	}
363
364	p := &gdk.Pixbuf{glib.Take(unsafe.Pointer(c))}
365	return p, nil
366}
367
368// GetIconName is a wrapper around gtk_window_get_icon_name().
369func (v *Window) GetIconName() (string, error) {
370	return stringReturn(C.gtk_window_get_icon_name(v.native()))
371}
372
373// GetModal is a wrapper around gtk_window_get_modal().
374func (v *Window) GetModal() bool {
375	c := C.gtk_window_get_modal(v.native())
376	return gobool(c)
377}
378
379// GetPosition is a wrapper around gtk_window_get_position().
380func (v *Window) GetPosition() (root_x, root_y int) {
381	var x, y C.gint
382	C.gtk_window_get_position(v.native(), &x, &y)
383	return int(x), int(y)
384}
385
386func stringReturn(c *C.gchar) (string, error) {
387	if c == nil {
388		return "", nilPtrErr
389	}
390	return C.GoString((*C.char)(c)), nil
391}
392
393// GetRole is a wrapper around gtk_window_get_role().
394func (v *Window) GetRole() (string, error) {
395	return stringReturn(C.gtk_window_get_role(v.native()))
396}
397
398// GetSize is a wrapper around gtk_window_get_size().
399func (v *Window) GetSize() (width, height int) {
400	var w, h C.gint
401	C.gtk_window_get_size(v.native(), &w, &h)
402	return int(w), int(h)
403}
404
405// GetTitle is a wrapper around gtk_window_get_title().
406func (v *Window) GetTitle() (string, error) {
407	return stringReturn(C.gtk_window_get_title(v.native()))
408}
409
410// GetTransientFor is a wrapper around gtk_window_get_transient_for().
411func (v *Window) GetTransientFor() (*Window, error) {
412	c := C.gtk_window_get_transient_for(v.native())
413	if c == nil {
414		return nil, nilPtrErr
415	}
416	return wrapWindow(glib.Take(unsafe.Pointer(c))), nil
417}
418
419// GetAttachedTo is a wrapper around gtk_window_get_attached_to().
420func (v *Window) GetAttachedTo() (*Widget, error) {
421	c := C.gtk_window_get_attached_to(v.native())
422	if c == nil {
423		return nil, nilPtrErr
424	}
425	return wrapWidget(glib.Take(unsafe.Pointer(c))), nil
426}
427
428// GetTypeHint is a wrapper around gtk_window_get_type_hint().
429func (v *Window) GetTypeHint() gdk.WindowTypeHint {
430	c := C.gtk_window_get_type_hint(v.native())
431	return gdk.WindowTypeHint(c)
432}
433
434// GetSkipTaskbarHint is a wrapper around gtk_window_get_skip_taskbar_hint().
435func (v *Window) GetSkipTaskbarHint() bool {
436	c := C.gtk_window_get_skip_taskbar_hint(v.native())
437	return gobool(c)
438}
439
440// GetSkipPagerHint is a wrapper around gtk_window_get_skip_pager_hint().
441func (v *Window) GetSkipPagerHint() bool {
442	c := C.gtk_window_get_skip_taskbar_hint(v.native())
443	return gobool(c)
444}
445
446// GetUrgencyHint is a wrapper around gtk_window_get_urgency_hint().
447func (v *Window) GetUrgencyHint() bool {
448	c := C.gtk_window_get_urgency_hint(v.native())
449	return gobool(c)
450}
451
452// GetAcceptFocus is a wrapper around gtk_window_get_accept_focus().
453func (v *Window) GetAcceptFocus() bool {
454	c := C.gtk_window_get_accept_focus(v.native())
455	return gobool(c)
456}
457
458// GetFocusOnMap is a wrapper around gtk_window_get_focus_on_map().
459func (v *Window) GetFocusOnMap() bool {
460	c := C.gtk_window_get_focus_on_map(v.native())
461	return gobool(c)
462}
463
464// HasGroup is a wrapper around gtk_window_has_group().
465func (v *Window) HasGroup() bool {
466	c := C.gtk_window_has_group(v.native())
467	return gobool(c)
468}
469
470// Move is a wrapper around gtk_window_move().
471func (v *Window) Move(x, y int) {
472	C.gtk_window_move(v.native(), C.gint(x), C.gint(y))
473}
474
475// Resize is a wrapper around gtk_window_resize().
476func (v *Window) Resize(width, height int) {
477	C.gtk_window_resize(v.native(), C.gint(width), C.gint(height))
478}
479
480// WindowSetDefaultIconFromFile is a wrapper around gtk_window_set_default_icon_from_file().
481func WindowSetDefaultIconFromFile(file string) error {
482	cstr := C.CString(file)
483	defer C.free(unsafe.Pointer(cstr))
484	var err *C.GError = nil
485	res := C.gtk_window_set_default_icon_from_file((*C.gchar)(cstr), &err)
486	if res == 0 {
487		defer C.g_error_free(err)
488		return errors.New(C.GoString((*C.char)(err.message)))
489	}
490	return nil
491}
492
493// WindowSetDefaultIconName is a wrapper around gtk_window_set_default_icon_name().
494func WindowSetDefaultIconName(s string) {
495	cstr := (*C.gchar)(C.CString(s))
496	defer C.free(unsafe.Pointer(cstr))
497	C.gtk_window_set_default_icon_name(cstr)
498}
499
500// SetIconFromFile is a wrapper around gtk_window_set_icon_from_file().
501func (v *Window) SetIconFromFile(file string) error {
502	cstr := C.CString(file)
503	defer C.free(unsafe.Pointer(cstr))
504	var err *C.GError = nil
505	res := C.gtk_window_set_icon_from_file(v.native(), (*C.gchar)(cstr), &err)
506	if res == 0 {
507		defer C.g_error_free(err)
508		return errors.New(C.GoString((*C.char)(err.message)))
509	}
510	return nil
511}
512
513// SetIconName is a wrapper around gtk_window_set_icon_name().
514func (v *Window) SetIconName(name string) {
515	cstr := C.CString(name)
516	defer C.free(unsafe.Pointer(cstr))
517	C.gtk_window_set_icon_name(v.native(), (*C.gchar)(cstr))
518}
519
520// SetAutoStartupNotification is a wrapper around
521// gtk_window_set_auto_startup_notification().
522// This doesn't seem write.  Might need to rethink?
523/*
524func (v *Window) SetAutoStartupNotification(setting bool) {
525	C.gtk_window_set_auto_startup_notification(gbool(setting))
526}
527*/
528
529// GetMnemonicsVisible is a wrapper around
530// gtk_window_get_mnemonics_visible().
531func (v *Window) GetMnemonicsVisible() bool {
532	c := C.gtk_window_get_mnemonics_visible(v.native())
533	return gobool(c)
534}
535
536// SetMnemonicsVisible is a wrapper around
537// gtk_window_get_mnemonics_visible().
538func (v *Window) SetMnemonicsVisible(setting bool) {
539	C.gtk_window_set_mnemonics_visible(v.native(), gbool(setting))
540}
541
542// GetFocusVisible is a wrapper around gtk_window_get_focus_visible().
543func (v *Window) GetFocusVisible() bool {
544	c := C.gtk_window_get_focus_visible(v.native())
545	return gobool(c)
546}
547
548// SetFocusVisible is a wrapper around gtk_window_set_focus_visible().
549func (v *Window) SetFocusVisible(setting bool) {
550	C.gtk_window_set_focus_visible(v.native(), gbool(setting))
551}
552
553// GetApplication is a wrapper around gtk_window_get_application().
554func (v *Window) GetApplication() (*Application, error) {
555	c := C.gtk_window_get_application(v.native())
556	if c == nil {
557		return nil, nilPtrErr
558	}
559
560	return wrapApplication(glib.Take(unsafe.Pointer(c))), nil
561}
562
563// SetApplication is a wrapper around gtk_window_set_application().
564func (v *Window) SetApplication(a *Application) {
565	C.gtk_window_set_application(v.native(), a.native())
566}
567
568// ActivateKey is a wrapper around gtk_window_activate_key().
569func (v *Window) ActivateKey(event *gdk.EventKey) bool {
570	c := C.gtk_window_activate_key(v.native(), (*C.GdkEventKey)(unsafe.Pointer(event.Native())))
571	return gobool(c)
572}
573
574// AddMnemonic is a wrapper around gtk_window_add_mnemonic().
575func (v *Window) AddMnemonic(keyval uint, target *Widget) {
576	C.gtk_window_add_mnemonic(v.native(), C.guint(keyval), target.native())
577}
578
579// RemoveMnemonic is a wrapper around gtk_window_remove_mnemonic().
580func (v *Window) RemoveMnemonic(keyval uint, target *Widget) {
581	C.gtk_window_remove_mnemonic(v.native(), C.guint(keyval), target.native())
582}
583
584// ActivateMnemonic is a wrapper around gtk_window_mnemonic_activate().
585func (v *Window) ActivateMnemonic(keyval uint, mods gdk.ModifierType) bool {
586	c := C.gtk_window_mnemonic_activate(v.native(), C.guint(keyval), C.GdkModifierType(mods))
587	return gobool(c)
588}
589
590// GetMnemonicModifier is a wrapper around gtk_window_get_mnemonic_modifier().
591func (v *Window) GetMnemonicModifier() gdk.ModifierType {
592	c := C.gtk_window_get_mnemonic_modifier(v.native())
593	return gdk.ModifierType(c)
594}
595
596// SetMnemonicModifier is a wrapper around gtk_window_set_mnemonic_modifier().
597func (v *Window) SetMnemonicModifier(mods gdk.ModifierType) {
598	C.gtk_window_set_mnemonic_modifier(v.native(), C.GdkModifierType(mods))
599}
600
601// TODO gtk_window_begin_move_drag().
602// TODO gtk_window_begin_resize_drag().
603// TODO gtk_window_get_default_icon_list().
604// TODO gtk_window_get_group().
605// TODO gtk_window_get_icon_list().
606// TODO gtk_window_get_window_type().
607// TODO gtk_window_list_toplevels().
608// TODO gtk_window_parse_geometry().
609// TODO gtk_window_propagate_key_event().
610// TODO gtk_window_set_attached_to().
611// TODO gtk_window_set_default_icon_list().
612// TODO gtk_window_set_icon_list().
613// TODO gtk_window_set_screen().
614// TODO gtk_window_get_resize_grip_area().
615