GtkCreate.cpp

Zbigniew Rebacz, 02/13/2014 05:11 PM

Download (3.64 KB)

 
1
#include <CtrlCore/CtrlCore.h>
2

    
3
#ifdef GUI_GTK
4

    
5
NAMESPACE_UPP
6

    
7
#define LLOG(x)    //  DLOG(x)
8

    
9
void Ctrl::Create(Ctrl *owner, bool popup)
10
{
11
        GuiLock __;
12
        ASSERT_(IsMainThread(), "Only the main thread can open a window");
13
        LLOG("Create " << Name() << " " << GetRect());
14
        ASSERT(!IsChild() && !IsOpen());
15
        LLOG("Ungrab1");
16

    
17
        top = new Top;
18
        top->window = gtk_window_new(popup && owner ? GTK_WINDOW_POPUP : GTK_WINDOW_TOPLEVEL);
19
        top->owner = owner;
20
        
21
        static int id;
22
        top->id = ++id;
23

    
24
        Win& w = wins.Add();
25
        w.id = top->id;
26
        w.ctrl = this;
27
        w.gtk = top->window;
28
        w.gdk = top->window->window;
29

    
30
        TopWindow *tw = dynamic_cast<TopWindow *>(this);
31
        if(popup && !owner) {
32
                gtk_window_set_decorated(gtk(), FALSE);
33
                gtk_window_set_has_frame(gtk(), FALSE);
34
                gtk_window_set_type_hint(gtk(), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
35
        }
36
        else
37
                gtk_window_set_type_hint(gtk(), popup ? GDK_WINDOW_TYPE_HINT_COMBO
38
                                                : tw && tw->tool ? GDK_WINDOW_TYPE_HINT_UTILITY
39
                                                : owner ? GDK_WINDOW_TYPE_HINT_DIALOG
40
                                                : GDK_WINDOW_TYPE_HINT_NORMAL);
41

    
42
        top->cursor_id = -1;
43

    
44
        gtk_widget_set_events(top->window, 0xffffffff);
45
        g_signal_connect(top->window, "event", G_CALLBACK(GtkEvent), (gpointer)(uintptr_t)top->id);
46
        
47
        gtk_widget_realize(top->window);
48

    
49
        Rect r = GetRect();
50
        gtk_window_move(gtk(), r.left, r.top);
51
        gtk_window_resize(gtk(), r.GetWidth(), r.GetHeight());
52

    
53
        if(owner && owner->top)
54
                gtk_window_set_transient_for(gtk(), owner->gtk());
55
        gtk_widget_set_app_paintable(top->window, TRUE);
56
        gtk_widget_set_can_focus(top->window, TRUE);
57
        isopen = true;
58

    
59
        top->im_context = gtk_im_multicontext_new();
60
        gtk_im_context_set_client_window(top->im_context, gdk());
61
         gtk_im_context_set_use_preedit(top->im_context, false);
62
        g_signal_connect(top->im_context, "commit", G_CALLBACK(IMCommit), (gpointer)(uintptr_t)top->id);
63

    
64
        WndShow(IsShown());
65

    
66
        FocusSync();
67
        if(!popup)
68
                SetWndFocus();
69
        
70
        SweepConfigure(true);
71
        
72
        DndInit();
73
        
74
        StateH(OPEN);
75

    
76
        activeCtrl = this;
77

    
78
        int mousex, mousey;
79
        gdk_window_get_pointer(gdk(), &mousex, &mousey, NULL);
80
        Point m(mousex, mousey);
81
        r = GetWndScreenRect().GetSize();
82
        if(r.Contains(m))
83
                DispatchMouse(MOUSEMOVE, m);
84
        
85
        RefreshLayoutDeep();
86
}
87

    
88
void Ctrl::WndDestroy()
89
{
90
        GuiLock __;
91
        LLOG("WndDestroy " << Name());
92
        DndExit();
93
        Ctrl *owner = GetOwner();
94
        if(owner && owner->top) {
95
                for(int i = 0; i < wins.GetCount(); i++)
96
                        if(wins[i].ctrl && wins[i].ctrl->GetOwner() == this && wins[i].gtk) {
97
                                LLOG("Changing owner");
98
                                gtk_window_set_transient_for((GtkWindow *)wins[i].gtk, owner->gtk());
99
                        }
100
                if(HasFocusDeep())
101
                        activeCtrl = owner;
102
        }
103
        gtk_widget_destroy(top->window);
104
        g_object_unref(top->im_context);
105
        isopen = false;
106
        popup = false;
107
        delete top;
108
        top = NULL;
109
        int q = FindCtrl(this);
110
        if(q >= 0)
111
                wins.Remove(q);
112
        if(owner)
113
                owner->WndUpdate();
114
}
115

    
116
Vector< Ptr<Ctrl> > Ctrl::activePopup;
117

    
118
void Ctrl::GuiPlatformRemove()
119
{
120
        for(int i = 0; i < activePopup.GetCount();)
121
                if(activePopup[i] == this) {
122
                        if(this == grabpopup && gdk_pointer_is_grabbed())
123
                                StopGrabPopup();
124
                        activePopup.Remove(i);
125
                        StartGrabPopup();
126
                }
127
                else
128
                        i++;
129
}
130

    
131
void Ctrl::PopUp(Ctrl *owner, bool savebits, bool activate, bool, bool)
132
{
133
        GuiLock __;
134
        LLOG("POPUP " << Name() << ", " << GetRect() << ", activate " << activate);
135
        Create(owner ? owner->GetTopCtrl() : GetActiveCtrl(), true);
136
        popup = true;
137
        if(activate) {
138
                Ptr<Ctrl> _this = this;
139
                SetFocus();
140
                if(_this) {
141
                        activePopup.Add(this);
142
                        StartGrabPopup();
143
                        CheckMouseCtrl();
144
                }
145
        }
146
}
147

    
148
END_UPP_NAMESPACE
149

    
150
#endif