-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.go
More file actions
3127 lines (2835 loc) · 112 KB
/
widget.go
File metadata and controls
3127 lines (2835 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctk
import (
"github.com/gofrs/uuid"
"github.com/go-curses/cdk"
cenums "github.com/go-curses/cdk/lib/enums"
"github.com/go-curses/cdk/lib/paint"
"github.com/go-curses/cdk/lib/ptypes"
"github.com/go-curses/cdk/lib/sync"
"github.com/go-curses/cdk/memphis"
"github.com/go-curses/ctk/lib/enums"
)
const (
TypeWidget cdk.CTypeTag = "ctk-widget"
TooltipColorStyle paint.StyleName = "tooltip-color"
TooltipColorTheme paint.ThemeName = "tooltip-color"
)
func init() {
_ = cdk.TypesManager.AddType(TypeWidget, nil)
style := paint.StyleDefault.Normal().
Background(paint.ColorYellow).
Foreground(paint.ColorDarkSlateBlue).
Dim(false)
paint.RegisterStyle(TooltipColorStyle, style)
borders, _ := paint.GetDefaultBorderRunes(paint.StockBorder)
arrows, _ := paint.GetArrows(paint.StockArrow)
tooltipThemeAspect := paint.ThemeAspect{
Normal: style,
Selected: style,
Active: style,
Prelight: style,
Insensitive: style,
FillRune: paint.DefaultFillRune,
BorderRunes: borders,
ArrowRunes: arrows,
Overlay: false,
}
paint.RegisterTheme(TooltipColorTheme, paint.Theme{
Content: tooltipThemeAspect,
Border: tooltipThemeAspect,
})
}
// Widget Hierarchy:
// Object
// +- Widget
// +- Container
// +- Misc
// +- Calendar
// +- CellView
// +- DrawingArea
// +- Entry
// +- Ruler
// +- Range
// +- Separator
// +- HSV
// +- Invisible
// +- OldEditable
// +- Preview
// +- Progress
//
// Widget is the base class all widgets in CTK derive from. It manages the
// widget lifecycle, states and style.
type Widget interface {
Object
Unparent()
Map()
Unmap()
IsMapped() (mapped bool)
Show()
Hide()
GetRegion() (region ptypes.Region)
LockDraw()
UnlockDraw()
LockEvent()
UnlockEvent()
AddAccelerator(accelSignal string, accelGroup AccelGroup, accelKey int, accelMods enums.ModifierType, accelFlags enums.AccelFlags)
RemoveAccelerator(accelGroup AccelGroup, accelKey int, accelMods enums.ModifierType) (value bool)
SetAccelPath(accelPath string, accelGroup AccelGroup)
CanActivateAccel(signalId int) (value bool)
Activate() (value bool)
Reparent(parent Widget)
IsFocus() (value bool)
GrabFocus()
GrabDefault()
SetSensitive(sensitive bool)
CssFullPath() (selector string)
CssState() (state enums.StateType)
SetParent(parent Widget)
GetParentWindow() (value Window)
SetEvents(events cdk.EventMask)
AddEvents(events cdk.EventMask)
GetToplevel() (value Widget)
GetAncestor(widgetType cdk.CTypeTag) (value Widget)
GetEvents() (value cdk.EventMask)
GetPointer(x int, y int)
IsAncestor(ancestor Widget) (value bool)
TranslateCoordinates(destWidget Widget, srcX int, srcY int, destX int, destY int) (value bool)
HideOnDelete() (value bool)
SetDirection(dir enums.TextDirection)
GetDirection() (value enums.TextDirection)
SetDefaultDirection(dir enums.TextDirection)
GetDefaultDirection() (value enums.TextDirection)
Path() (path string)
ClassPath(pathLength int, path string, pathReversed string)
GetCompositeName() (value string)
SetAppPaintable(appPaintable bool)
SetDoubleBuffered(doubleBuffered bool)
SetRedrawOnAllocate(redrawOnAllocate bool)
SetCompositeName(name string)
SetScrollAdjustments(hadjustment Adjustment, vadjustment Adjustment) (value bool)
Draw() cenums.EventFlag
MnemonicActivate(groupCycling bool) (value bool)
SendExpose(event cdk.Event) (value int)
SendFocusChange(event cdk.Event) (value bool)
ChildFocus(direction enums.DirectionType) (value bool)
ChildNotify(childProperty string)
FreezeChildNotify()
GetChildVisible() (value bool)
GetParent() (value Widget)
GetAllParents() (parents []Widget)
GetDisplay() (value cdk.Display)
GetRootWindow() (value Window)
GetScreen() (value cdk.Display)
HasScreen() (value bool)
GetSizeRequest() (width, height int)
SizeRequest() ptypes.Rectangle
SetSizeRequest(width, height int)
SetNoShowAll(noShowAll bool)
GetNoShowAll() (value bool)
AddMnemonicLabel(label Widget)
RemoveMnemonicLabel(label Widget)
ErrorBell()
KeynavFailed(direction enums.DirectionType) (value bool)
GetTooltipMarkup() (value string)
SetTooltipMarkup(markup string)
GetTooltipText() (value string)
SetTooltipText(text string)
GetTooltipWindow() (value Window)
SetTooltipWindow(customWindow Window)
GetHasTooltip() (value bool)
SetHasTooltip(hasTooltip bool)
GetWindow() (window Window)
GetAppPaintable() (value bool)
GetCanDefault() (value bool)
SetCanDefault(canDefault bool)
GetCanFocus() (value bool)
SetCanFocus(canFocus bool)
GetHasWindow() (ok bool)
GetSensitive() (value bool)
IsSensitive() bool
GetVisible() (value bool)
SetVisible(visible bool)
HasDefault() (value bool)
HasFocus() (value bool)
HasGrab() (value bool)
IsDrawable() (value bool)
IsToplevel() (value bool)
SetWindow(window Window)
SetReceivesDefault(receivesDefault bool)
GetReceivesDefault() (value bool)
SetRealized(realized bool)
GetRealized() (value bool)
SetMapped(mapped bool)
GetMapped() (value bool)
GetThemeRequest() (theme paint.Theme)
GetState() (value enums.StateType)
SetState(state enums.StateType)
HasState(s enums.StateType) bool
UnsetState(state enums.StateType)
GetFlags() enums.WidgetFlags
HasFlags(f enums.WidgetFlags) bool
UnsetFlags(v enums.WidgetFlags)
SetFlags(v enums.WidgetFlags)
IsParentFocused() bool
IsFocused() bool
CanFocus() bool
IsDefault() bool
CanDefault() bool
IsVisible() bool
HasEventFocus() bool
GrabEventFocus()
ReleaseEventFocus()
GetTopParent() (parent Widget)
GetWidgetAt(p *ptypes.Point2I) Widget
PushCompositeChild(child Widget)
PopCompositeChild(child Widget)
GetCompositeChildren() []Widget
RenderFrozen() bool
RenderFreeze()
RenderThaw()
RequestDrawAndShow()
RequestDrawAndSync()
}
var _ Widget = (*CWidget)(nil)
// The CWidget structure implements the Widget interface and is exported
// to facilitate type embedding with custom implementations. No member variables
// are exported as the interface methods are the only intended means of
// interacting with Widget objects.
type CWidget struct {
CObject
renderFrozen int
composites []Widget
parent Widget
state enums.StateType
flags enums.WidgetFlags
flagsLock *sync.RWMutex
drawLock *sync.Mutex
eventLock *sync.Mutex
tooltipWindow Window
tooltipTimer uuid.UUID
tooltipBrowseTimer uuid.UUID
}
// Init initializes a Widget object. This must be called at least once to
// set up the necessary defaults and allocate any memory structures. Calling
// this more than once is safe though unnecessary. Only the first call will
// result in any effect upon the Widget instance. Init is used in the
// NewWidget constructor and only necessary when implementing a derivative
// Widget type.
func (w *CWidget) Init() (already bool) {
if w.InitTypeItem(TypeWidget, w) {
return true
}
w.CObject.Init()
_ = w.InstallProperty(PropertyAppPaintable, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyCanDefault, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyCanFocus, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyCompositeChild, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyDoubleBuffered, cdk.BoolProperty, true, true)
_ = w.InstallProperty(PropertyEvents, cdk.StructProperty, true, cdk.EVENT_MASK_NONE)
_ = w.InstallProperty(PropertyHasDefault, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyHasFocus, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyHasTooltip, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyHeightRequest, cdk.IntProperty, true, -1)
_ = w.InstallProperty(PropertyIsFocus, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyNoShowAll, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyParent, cdk.StructProperty, true, nil)
_ = w.InstallProperty(PropertyReceivesDefault, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertySensitive, cdk.BoolProperty, true, true)
_ = w.InstallProperty(PropertyTooltipMarkup, cdk.StringProperty, true, "")
_ = w.InstallProperty(PropertyTooltipText, cdk.StringProperty, true, "")
_ = w.InstallProperty(PropertyVisible, cdk.BoolProperty, true, false)
_ = w.InstallProperty(PropertyWidthRequest, cdk.IntProperty, true, -1)
_ = w.InstallProperty(PropertyWindow, cdk.StructProperty, true, nil)
theme := paint.GetDefaultColorTheme()
getDefContentColors := func(state enums.StateType) (fg, bg paint.Color) {
switch state {
case enums.StateNormal:
fg, bg, _ = theme.Content.Normal.Decompose()
case enums.StateActive:
fg, bg, _ = theme.Content.Active.Decompose()
case enums.StatePrelight:
fg, bg, _ = theme.Content.Prelight.Decompose()
case enums.StateSelected:
fg, bg, _ = theme.Content.Selected.Decompose()
case enums.StateInsensitive:
fg, bg, _ = theme.Content.Insensitive.Decompose()
}
return
}
getDefBorderColors := func(state enums.StateType) (fg, bg paint.Color) {
switch state {
case enums.StateNormal:
fg, bg, _ = theme.Border.Normal.Decompose()
case enums.StateActive:
fg, bg, _ = theme.Border.Active.Decompose()
case enums.StatePrelight:
fg, bg, _ = theme.Border.Prelight.Decompose()
case enums.StateSelected:
fg, bg, _ = theme.Border.Selected.Decompose()
case enums.StateInsensitive:
fg, bg, _ = theme.Border.Insensitive.Decompose()
}
return
}
for _, state := range []enums.StateType{enums.StateNormal, enums.StateActive, enums.StatePrelight, enums.StateSelected, enums.StateInsensitive} {
_ = w.InstallCssProperty(CssPropertyClass, state, cdk.StringProperty, true, "")
_ = w.InstallCssProperty(CssPropertyWidth, state, cdk.IntProperty, true, -1)
_ = w.InstallCssProperty(CssPropertyHeight, state, cdk.IntProperty, true, -1)
fg, bg := getDefContentColors(state)
_ = w.InstallCssProperty(CssPropertyColor, state, cdk.ColorProperty, true, fg)
_ = w.InstallCssProperty(CssPropertyBackgroundColor, state, cdk.ColorProperty, true, bg)
fg, bg = getDefBorderColors(state)
_ = w.InstallCssProperty(CssPropertyBorderColor, state, cdk.ColorProperty, true, fg)
_ = w.InstallCssProperty(CssPropertyBorderBackgroundColor, state, cdk.ColorProperty, true, bg)
_ = w.InstallCssProperty(CssPropertyBold, state, cdk.BoolProperty, true, false)
_ = w.InstallCssProperty(CssPropertyBlink, state, cdk.BoolProperty, true, false)
_ = w.InstallCssProperty(CssPropertyReverse, state, cdk.BoolProperty, true, false)
_ = w.InstallCssProperty(CssPropertyUnderline, state, cdk.BoolProperty, true, false)
_ = w.InstallCssProperty(CssPropertyDim, state, cdk.BoolProperty, true, false)
_ = w.InstallCssProperty(CssPropertyItalic, state, cdk.BoolProperty, true, false)
_ = w.InstallCssProperty(CssPropertyStrike, state, cdk.BoolProperty, true, false)
}
w.renderFrozen = 0
w.composites = make([]Widget, 0)
w.flagsLock = &sync.RWMutex{}
w.drawLock = &sync.Mutex{}
w.eventLock = &sync.Mutex{}
w.state = enums.StateNormal
w.flags = enums.NULL_WIDGET_FLAG
w.tooltipWindow = nil
w.tooltipTimer = uuid.Nil
w.tooltipBrowseTimer = uuid.Nil
w.SetTheme(theme)
w.Connect(SignalLostFocus, WidgetLostFocusHandle, w.lostFocus)
w.Connect(SignalGainedFocus, WidgetGainedFocusHandle, w.gainedFocus)
w.Connect(SignalEnter, WidgetEnterHandle, w.enter)
w.Connect(SignalLeave, WidgetLeaveHandle, w.leave)
return false
}
func (w *CWidget) openTooltip() {
settings := GetDefaultSettings()
if settings.GetEnableTooltips() {
if w.GetHasTooltip() {
if w.tooltipBrowseTimer != uuid.Nil {
cdk.StopTimeout(w.tooltipBrowseTimer)
w.tooltipBrowseTimer = uuid.Nil
}
timeout := settings.GetTooltipTimeout()
w.Lock()
w.tooltipTimer = cdk.AddTimeout(
timeout,
w.openTooltipHandler,
)
w.LogDebug("openTooltip setting up timeout handler: %v (%v)", timeout, w.tooltipTimer)
w.Unlock()
}
}
}
func (w *CWidget) openTooltipHandler() cenums.EventFlag {
if display := w.GetDisplay(); display != nil && w.GetHasTooltip() {
tooltipWindow := w.GetTooltipWindow()
if screen := display.Screen(); screen != nil {
if w.tooltipBrowseTimer != uuid.Nil {
cdk.StopTimeout(w.tooltipBrowseTimer)
w.tooltipBrowseTimer = uuid.Nil
}
position, _ := display.CursorPosition()
tw, th := w.tooltipTextBufferInfo()
if w.HasPoint(&position) {
if tw > -1 && th > -1 {
sw, sh := screen.Size()
if position.X+2+tw < sw {
position.X += 2
}
if position.Y+1+th < sh {
position.Y += 1
}
if position.X+tw >= sw {
// too far to the right
position.X = sw - tw
}
if position.Y+th >= sh {
// too far down
position.Y = sh - th
}
tooltipWindow.Move(position.X, position.Y)
tooltipWindow.SetAllocation(ptypes.MakeRectangle(tw, th))
w.LogDebug("opening tooltip: %v", tooltipWindow.ObjectInfo())
settings := GetDefaultSettings()
browseModeTimeout := settings.GetTooltipBrowseModeTimeout()
if markup := w.GetTooltipMarkup(); markup != "" {
tooltipWindow.Resize()
tooltipWindow.Show()
w.tooltipBrowseTimer = cdk.AddTimeout(browseModeTimeout, func() cenums.EventFlag {
w.closeTooltip()
return cenums.EVENT_STOP
})
if window := w.GetWindow(); window != nil {
window.Connect(SignalCdkEvent, WidgetTooltipWindowEventHandle, w.processTooltipWindowEvent)
}
return cenums.EVENT_STOP
}
if text := w.GetTooltipText(); text != "" {
tooltipWindow.Resize()
tooltipWindow.Show()
w.tooltipBrowseTimer = cdk.AddTimeout(browseModeTimeout, func() cenums.EventFlag {
w.closeTooltip()
return cenums.EVENT_STOP
})
if window := w.GetWindow(); window != nil {
window.Connect(SignalCdkEvent, WidgetTooltipWindowEventHandle, w.processTooltipWindowEvent)
}
return cenums.EVENT_STOP
}
}
}
}
tooltipWindow.Hide()
}
return cenums.EVENT_STOP
}
func (w *CWidget) processTooltipWindowEvent(data []interface{}, argv ...interface{}) cenums.EventFlag {
if len(argv) >= 2 {
if evt, ok := argv[1].(cdk.Event); ok {
switch evt.(type) {
case *cdk.EventMouse, *cdk.EventKey:
w.closeTooltip()
}
}
}
return cenums.EVENT_PASS
}
func (w *CWidget) closeTooltip() {
if window := w.GetWindow(); window != nil {
_ = window.Disconnect(SignalCdkEvent, WidgetTooltipWindowEventHandle)
}
if w.tooltipBrowseTimer != uuid.Nil {
cdk.StopTimeout(w.tooltipBrowseTimer)
w.tooltipBrowseTimer = uuid.Nil
}
if w.tooltipTimer != uuid.Nil {
w.LogDebug("closing tooltip")
cdk.StopTimeout(w.tooltipTimer)
w.tooltipTimer = uuid.Nil
}
if w.tooltipWindow != nil {
w.tooltipWindow.Hide()
}
}
func (w *CWidget) newTooltipWindow() (tooltipWindow Window) {
tooltipWindow = NewWindow()
tooltipWindow.SetWindowType(cenums.WINDOW_POPUP)
tooltipWindow.SetFlags(enums.TOPLEVEL)
tooltipWindow.SetDecorated(false)
theme, _ := paint.GetTheme(TooltipColorTheme)
tooltipWindow.SetTheme(theme)
tooltipWindow.Connect(SignalCdkEvent, "widget-tooltip-window-resize-handler", w.tooltipEvent)
tooltipWindow.Connect(SignalResize, "widget-tooltip-window-resize-handler", w.tooltipResize)
tooltipWindow.Connect(SignalDraw, "widget-tooltip-window-draw-handler", w.tooltipDraw)
return
}
func (w *CWidget) tooltipTextBufferInfo() (longestLine, lineCount int) {
longestLine, lineCount = -1, -1
style := w.GetThemeRequest().Content.Normal
var tb memphis.TextBuffer
if markup := w.GetTooltipMarkup(); markup != "" {
if m, err := memphis.NewMarkup(markup, style); err != nil {
w.LogErr(err)
return
} else {
tb = m.TextBuffer(false)
}
} else if text := w.GetTooltipText(); text != "" {
tb = memphis.NewTextBuffer(text, style, false)
} else {
return
}
longestLine, lineCount = tb.PlainTextInfo(cenums.WRAP_WORD, false, cenums.JUSTIFY_LEFT, -1)
return
}
func (w *CWidget) tooltipEvent(data []interface{}, argv ...interface{}) cenums.EventFlag {
if _, event, ok := ArgvSignalEvent(argv...); ok {
if tooltipWindow := w.GetTooltipWindow(); tooltipWindow != nil {
switch e := event.(type) {
case *cdk.EventMouse:
point := e.Point2I().NewClone()
if !w.HasPoint(point) {
w.closeTooltip()
return cenums.EVENT_STOP
} else {
x, y := e.Position()
mw, mh := w.tooltipTextBufferInfo()
if x > mw {
x -= mw
} else {
x = mw - x
}
if y > mh {
y -= mh
} else {
y = mh - y
}
tooltipWindow.Move(x, y)
}
}
}
}
return cenums.EVENT_PASS
}
func (w *CWidget) tooltipResize(data []interface{}, argv ...interface{}) cenums.EventFlag {
alloc := ptypes.MakeRectangle(w.tooltipTextBufferInfo())
if tooltipWindow := w.GetTooltipWindow(); tooltipWindow != nil {
tooltipWindow.SetAllocation(alloc)
}
return cenums.EVENT_STOP
}
func (w *CWidget) tooltipDraw(data []interface{}, argv ...interface{}) cenums.EventFlag {
if tooltipWindow := w.GetTooltipWindow(); tooltipWindow != nil {
if surface, ok := argv[1].(*memphis.CSurface); ok {
size := surface.GetSize()
if !w.IsVisible() || size.W == 0 || size.H == 0 {
w.LogDebug("not visible, zero width or zero height")
return cenums.EVENT_STOP
}
theme := tooltipWindow.GetThemeRequest()
style := theme.Content.Normal
var tb memphis.TextBuffer
if markup := w.GetTooltipMarkup(); markup != "" {
if m, err := memphis.NewMarkup(markup, style); err != nil {
w.LogErr(err)
return cenums.EVENT_STOP
} else {
tb = m.TextBuffer(false)
}
} else if text := w.GetTooltipText(); text != "" {
tb = memphis.NewTextBuffer(text, style, false)
} else {
w.LogError("tooltip window is drawing without text?!")
return cenums.EVENT_STOP
}
surface.Fill(theme)
if tb != nil {
tb.Draw(surface, false, cenums.WRAP_WORD, false, cenums.JUSTIFY_LEFT, cenums.ALIGN_TOP)
}
if debug, _ := w.GetBoolProperty(cdk.PropertyDebug); debug {
surface.DebugBox(paint.ColorNavy, tooltipWindow.ObjectInfo())
}
}
}
return cenums.EVENT_STOP
}
// Destroy a widget. Equivalent to DestroyObject. When a widget is destroyed, it
// will break any references it holds to other objects. If the widget is
// inside a container, the widget will be removed from the container. If the
// widget is a toplevel (derived from Window), it will be removed from the
// list of toplevels, and the reference CTK holds to it will be removed.
// Removing a widget from its container or the list of toplevels results in
// the widget being finalized, unless you've added additional references to
// the widget with g_object_ref. In most cases, only toplevel widgets
// (windows) require explicit destruction, because when you destroy a
// toplevel its children will be destroyed as well.
func (w *CWidget) Destroy() {
w.closeTooltip()
w.Emit(SignalDestroyEvent, w)
w.DisconnectAll()
w.Hide()
if w.tooltipWindow != nil {
w.tooltipWindow.Destroy()
}
if err := w.DestroyObject(); err != nil {
w.LogErr(err)
}
}
// Unparent is only for use in widget implementations. Should be called by
// implementations of the remove method on Container, to dissociate a child from
// the container.
func (w *CWidget) Unparent() {
if w.parent != nil {
if parent, ok := w.parent.Self().(Container); ok {
if f := w.Emit(SignalUnparent, w, w.parent); f == cenums.EVENT_PASS {
parent.Remove(w)
}
}
}
}
func (w *CWidget) Map() {
if !w.IsMapped() {
region := w.GetRegion()
// w.LockDraw()
_ = memphis.MakeConfigureSurface(
w.ObjectID(),
region.Origin(),
region.Size(),
w.GetThemeRequest().Content.Normal,
)
w.SetFlags(enums.MAPPED)
// w.UnlockDraw()
w.Emit(SignalMap, w, region)
}
}
func (w *CWidget) Unmap() {
if w.IsMapped() {
memphis.RemoveSurface(w.ObjectID())
w.UnsetFlags(enums.MAPPED)
w.Emit(SignalUnmap, w)
}
}
func (w *CWidget) IsMapped() (mapped bool) {
_, err := memphis.GetSurface(w.ObjectID())
mapped = err == nil && w.HasFlags(enums.MAPPED)
return
}
// Show flags a widget to be displayed. Any widget that isn't shown will not
// appear on the screen. If you want to show all the widgets in a container,
// it's easier to call ShowAll on the container, instead of
// individually showing the widgets. Remember that you have to show the
// containers containing a widget, in addition to the widget itself, before
// it will appear onscreen. When a toplevel container is shown, it is
// immediately realized and mapped; other shown widgets are realized and
// mapped when their toplevel container is realized and mapped.
func (w *CWidget) Show() {
if w.HasFlags(enums.APP_PAINTABLE) {
if !w.HasFlags(enums.VISIBLE) {
w.SetFlags(enums.VISIBLE)
w.Emit(SignalShow, w)
w.Invalidate()
}
}
}
// Hide reverses the effects of Show, causing the widget to be hidden (invisible
// to the user).
func (w *CWidget) Hide() {
if w.HasFlags(enums.APP_PAINTABLE) {
if w.HasFlags(enums.VISIBLE) {
w.closeTooltip()
w.Unmap()
w.UnsetFlags(enums.VISIBLE)
w.Emit(SignalHide, w)
w.Invalidate()
}
}
}
// GetRegion returns the current origin and allocation in a Region type, taking
// any positive SizeRequest set.
func (w *CWidget) GetRegion() (region ptypes.Region) {
region = w.CObject.GetRegion()
req := w.SizeRequest()
if req.W > 0 && region.W > req.W {
region.W = req.W
}
if req.H > 0 && region.H > req.H {
region.H = req.H
}
return
}
func (w *CWidget) LockDraw() {
w.drawLock.Lock()
}
func (w *CWidget) UnlockDraw() {
w.drawLock.Unlock()
}
func (w *CWidget) LockEvent() {
w.eventLock.Lock()
}
func (w *CWidget) UnlockEvent() {
w.eventLock.Unlock()
}
func (w *CWidget) RenderFrozen() bool {
w.RLock()
defer w.RUnlock()
return w.renderFrozen > 0
}
func (w *CWidget) RenderFreeze() {
if !w.RenderFrozen() {
w.PassSignal(SignalInvalidate, SignalDraw)
}
w.Lock()
w.renderFrozen += 1
w.Unlock()
}
func (w *CWidget) RenderThaw() {
if w.RenderFrozen() {
w.Lock()
w.renderFrozen -= 1
w.Unlock()
}
if !w.RenderFrozen() {
w.ResumeSignal(SignalInvalidate)
w.Resize()
w.ResumeSignal(SignalDraw)
}
}
// Installs an accelerator for this widget in accel_group that causes
// accel_signal to be emitted if the accelerator is activated. The
// accel_group needs to be added to the widget's toplevel via
// WindowAddAccelGroup, and the signal must be of type G_RUN_ACTION.
// Accelerators added through this function are not user changeable during
// runtime. If you want to support accelerators that can be changed by the
// user, use AccelMapAddEntry and SetAccelPath or
// MenuItemSetAccelPath instead.
// Parameters:
// widget widget to install an accelerator on
// accelSignal widget signal to emit on accelerator activation
// accelGroup accel group for this widget, added to its toplevel
// accelKey GDK keyval of the accelerator
// accelMods modifier key combination of the accelerator
// accelFlags flag accelerators, e.g. GTK_ACCEL_VISIBLE
//
// Method stub, unimplemented
func (w *CWidget) AddAccelerator(accelSignal string, accelGroup AccelGroup, accelKey int, accelMods enums.ModifierType, accelFlags enums.AccelFlags) {
}
// Removes an accelerator from widget , previously installed with
// AddAccelerator.
// Parameters:
// widget widget to install an accelerator on
// accelGroup accel group for this widget
// accelKey GDK keyval of the accelerator
// accelMods modifier key combination of the accelerator
// returns whether an accelerator was installed and could be removed
//
// Method stub, unimplemented
func (w *CWidget) RemoveAccelerator(accelGroup AccelGroup, accelKey int, accelMods enums.ModifierType) (value bool) {
return false
}
// Given an accelerator group, accel_group , and an accelerator path,
// accel_path , sets up an accelerator in accel_group so whenever the key
// binding that is defined for accel_path is pressed, widget will be
// activated. This removes any accelerators (for any accelerator group)
// installed by previous calls to SetAccelPath. Associating
// accelerators with paths allows them to be modified by the user and the
// modifications to be saved for future use. (See AccelMapSave.) This
// function is a low level function that would most likely be used by a menu
// creation system like UIManager. If you use UIManager, setting up
// accelerator paths will be done automatically. Even when you you aren't
// using UIManager, if you only want to set up accelerators on menu items
// MenuItemSetAccelPath provides a somewhat more convenient
// interface. Note that accel_path string will be stored in a GQuark.
// Therefore, if you pass a static string, you can save some memory by
// interning it first with g_intern_static_string.
// Parameters:
// accelPath path used to look up the accelerator.
// accelGroup a AccelGroup.
//
// Method stub, unimplemented
func (w *CWidget) SetAccelPath(accelPath string, accelGroup AccelGroup) {}
// Determines whether an accelerator that activates the signal identified by
// signal_id can currently be activated. This is done by emitting the
// can-activate-accel signal on widget ; if the signal isn't overridden
// by a handler or in a derived widget, then the default check is that the
// widget must be sensitive, and the widget and all its ancestors mapped.
// Parameters:
// signalId the ID of a signal installed on widget
//
// Returns:
// TRUE if the accelerator can be activated.
//
// Method stub, unimplemented
func (w *CWidget) CanActivateAccel(signalId int) (value bool) {
return false
}
// For widgets that can be "activated" (buttons, menu items, etc.) this
// function activates them. Activation is what happens when you press Enter
// on a widget during key navigation. If widget isn't activatable, the
// function returns FALSE.
// Returns:
// TRUE if the widget was activatable
func (w *CWidget) Activate() (value bool) {
w.closeTooltip()
if w.IsVisible() && w.IsSensitive() {
if f := w.Emit(SignalActivate, w); f == cenums.EVENT_STOP {
value = true
}
}
return
}
// Move the Widget to the given container, removing itself first from any other
// container that was currently holding it. This method emits a reparent signal
// initially and if the listeners return EVENT_PAS, the change is applied
// Parameters:
// newParent a Container to move the widget into
//
// Emits: SignalReparent, Argv=[Widget instance, new parent]
func (w *CWidget) Reparent(parent Widget) {
if r := w.Emit(SignalReparent, w, parent); r == cenums.EVENT_PASS {
if pc, ok := parent.Self().(Container); ok {
if w.parent != nil {
if pc, ok := w.parent.Self().(Container); ok {
pc.Remove(w)
}
}
pc.Add(w)
}
}
}
// Determines if the widget is the focus widget within its toplevel. (This
// does not mean that the HAS_FOCUS flag is necessarily set; HAS_FOCUS will
// only be set if the toplevel widget additionally has the global input
// focus.)
// Returns:
// TRUE if the widget is the focus widget.
// Returns TRUE if the Widget instance is currently the focus of it's parent
// Window, FALSE otherwise
func (w *CWidget) IsFocus() (value bool) {
if window := w.GetWindow(); window != nil {
if w.CanFocus() {
if focused := window.GetFocus(); focused != nil {
if focused.ObjectID() == focused.ObjectID() {
return true
}
}
}
}
return false
}
// Causes widget to have the keyboard focus for the Window it's inside.
// widget must be a focusable widget, such as a Entry; something like
// Frame won't work. More precisely, it must have the GTK_CAN_FOCUS flag
// set. Use SetCanFocus to modify that flag. The widget also
// needs to be realized and mapped. This is indicated by the related signals.
// Grabbing the focus immediately after creating the widget will likely fail
// and cause critical warnings.
// If the Widget instance CanFocus() then take the focus of the associated
// Window. Any previously focused Widget will emit a lost-focus signal and the
// newly focused Widget will emit a gained-focus signal. This method emits a
// grab-focus signal initially and if the listeners return EVENT_PASS, the
// changes are applied
//
// Emits: SignalGrabFocus, Argv=[Widget instance]
// Emits: SignalLostFocus, Argv=[Previous focus Widget instance], From=Previous focus Widget instance
// Emits: SignalGainedFocus, Argv=[Widget instance, previous focus Widget instance]
func (w *CWidget) GrabFocus() {
if w.CanFocus() && w.IsVisible() && w.IsSensitive() {
if r := w.Emit(SignalGrabFocus, w); r == cenums.EVENT_PASS {
if tl := w.GetWindow(); tl != nil {
if focused := tl.GetFocus(); focused != nil {
if focused.ObjectID() != w.ObjectID() {
if err := focused.SetProperty(PropertyHasFocus, false); err != nil {
focused.LogErr(err)
}
focused.UnsetState(enums.StateSelected)
focused.Emit(SignalLostFocus)
focused.Invalidate()
}
}
tl.SetFocus(w)
if err := w.SetProperty(PropertyHasFocus, true); err != nil {
w.LogErr(err)
}
w.SetState(enums.StateSelected)
w.Emit(SignalGainedFocus)
w.Invalidate()
}
}
} else {
w.LogError("cannot grab focus: can't focus, invisible or insensitive")
}
}
// Causes widget to become the default widget. widget must have the
// GTK_CAN_DEFAULT flag set; typically you have to set this flag yourself by
// calling SetCanDefault (widget , TRUE). The default widget is
// activated when the user presses Enter in a window. Default widgets must be
// activatable, that is, Activate should affect them.
func (w *CWidget) GrabDefault() {}
// Sets the sensitivity of a widget. A widget is sensitive if the user can
// interact with it. Insensitive widgets are "grayed out" and the user can't
// interact with them. Insensitive widgets are known as "inactive",
// "disabled", or "ghosted" in some other toolkits.
// Parameters:
// sensitive TRUE to make the widget sensitive
// Emits: SignalSetSensitive, Argv=[Widget instance, given sensitive bool]
func (w *CWidget) SetSensitive(sensitive bool) {
if f := w.Emit(SignalSetSensitive, w, sensitive); f == cenums.EVENT_PASS {
w.closeTooltip()
if !sensitive {
w.SetState(enums.StateInsensitive)
} else {
w.UnsetState(enums.StateInsensitive)
}
if err := w.SetBoolProperty(PropertySensitive, sensitive); err != nil {
w.LogErr(err)
}
}
}
// CssFullPath returns a CSS selector rule for the Widget which includes the
// parent hierarchy in type form.
func (w *CWidget) CssFullPath() (selector string) {
selector = w.CObject.CssSelector()
p := w.GetParent()
for {
if p != nil && p.ObjectID() != w.ObjectID() {
selector = p.CssSelector() + " > " + selector
np := p.GetParent()
if np != nil && np.ObjectID() == p.ObjectID() {
break
}
p = np
} else {
break
}
}
return
}
func (w *CWidget) CssState() (state enums.StateType) {
if w.IsDrawable() && w.IsVisible() {
if w.IsSensitive() {
if w.IsFocus() {
w.RLock()
state = w.state.Set(enums.StateSelected)
w.RUnlock()
} else {
w.RLock()
state = state.Set(enums.StateNormal)
w.RUnlock()
}
} else {
w.RLock()
state = w.state.Set(enums.StateInsensitive)
w.RUnlock()
}
}
return
}
// This function is useful only when implementing subclasses of Container.
// Sets the container as the parent of widget , and takes care of some
// details such as updating the state and style of the child to reflect its
// new location. The opposite function is Unparent.
// Parameters:
// parent parent container
func (w *CWidget) SetParent(parent Widget) {
if f := w.Emit(SignalSetParent, w, w.parent, parent); f == cenums.EVENT_PASS {
w.closeTooltip()
if w.HasFlags(enums.PARENT_SENSITIVE) && w.parent != nil {
_ = parent.Disconnect(SignalLostFocus, WidgetLostFocusHandle)
_ = parent.Disconnect(SignalGainedFocus, WidgetGainedFocusHandle)
}
if err := w.SetStructProperty(PropertyParent, parent); err != nil {
w.LogErr(err)
} else if parent != nil && w.ObjectID() != parent.ObjectID() {
if w.HasFlags(enums.PARENT_SENSITIVE) {
parent.Connect(SignalLostFocus, WidgetLostFocusHandle, w.lostFocus)
parent.Connect(SignalGainedFocus, WidgetGainedFocusHandle, w.gainedFocus)
}
}
}
}
// Gets widget's parent window.
// Returns:
// the parent window of widget.
func (w *CWidget) GetParentWindow() (value Window) {
var ok bool