File: message_pane.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (280 lines) | stat: -rw-r--r-- 8,377 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : message_pane.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "cl_config.h"
#include "editor_config.h"
#include "message_pane.h"
#include "pluginmanager.h"
#include <wx/dcbuffer.h>
#include <wx/msgdlg.h>

MessagePane::MessagePane(wxWindow* parent)
    : MessagePaneBase(parent)
{
    BitmapLoader* bmpLoader = PluginManager::Get()->GetStdIcons();
    m_bitmap1->SetBitmap(bmpLoader->LoadBitmap(wxT("messages/48/info")));
    m_buttonAction->Hide();
    Hide();
}

void MessagePane::OnKeyDown(wxKeyEvent& event)
{
    if(event.GetKeyCode() == WXK_ESCAPE) {
        DoHide();

    } else
        event.Skip();
}

void MessagePane::OnButtonClose(wxCommandEvent& event)
{
    wxUnusedVar(event);
    MessageDetails msg = m_messages.CurrentMessage();
    SavePreferenceIfNeeded(msg, wxNOT_FOUND); // -1 for Hide
    DoShowNextMessage();
}

void MessagePane::DoHide()
{
    if(IsShown()) {
        // Hide all default controls
        m_buttonAction->Hide();
        m_buttonAction1->Hide();
        m_buttonAction2->Hide();
        m_DontAnnoyMeCheck->Hide();

        m_messages.Clear();
        Hide();
        GetParent()->GetSizer()->Layout();
    }
}

void MessagePane::DoShowCurrentMessage()
{
    MessageDetails msg = m_messages.CurrentMessage();
    wxString txt = msg.message;

    m_buttonAction->Hide();
    m_buttonAction1->Hide();
    m_buttonAction2->Hide();
    m_DontAnnoyMeCheck->Hide();

    bool hasDefaultButton(false);
    if(msg.bmp.IsOk() == false) {
        BitmapLoader* bmpLoader = PluginManager::Get()->GetStdIcons();
        m_bitmap1->SetBitmap(bmpLoader->LoadBitmap(wxT("messages/48/info")));

    } else {
        m_bitmap1->SetBitmap(msg.bmp);
    }

    // display the buttons
    if(msg.btn1.buttonLabel.IsEmpty() == false) {
        m_buttonAction->SetLabel(msg.btn1.buttonLabel);
        m_buttonAction->Show();
        if(msg.btn1.isDefault) {
            m_buttonAction->SetDefault();
            hasDefaultButton = true;
        }
    }

    if(msg.btn2.buttonLabel.IsEmpty() == false) {
        m_buttonAction1->SetLabel(msg.btn2.buttonLabel);
        m_buttonAction1->Show();
        if(msg.btn2.isDefault) {
            m_buttonAction1->SetDefault();
            hasDefaultButton = true;
        }
    }

    if(msg.btn3.buttonLabel.IsEmpty() == false) {
        m_buttonAction2->SetLabel(msg.btn3.buttonLabel);
        m_buttonAction2->Show();
        if(msg.btn3.isDefault) {
            m_buttonAction2->SetDefault();
            hasDefaultButton = true;
        }
    }

    // Show hide button if needed and make the default if there is
    // no default button
    if(msg.showHideButton) {
        m_buttonClose->Show();
        if(!hasDefaultButton) {
            m_buttonClose->SetDefault();
        }
    } else {
        m_buttonClose->Hide();
    }

    m_staticTextMessage->SetLabel(txt);
    if(IsShown() == false) {
        Show();
    }

    // Show the NoSpam checkbox if needed
    m_DontAnnoyMeCheck->Show(msg.check.GetShowCheckbox());
    // Uncheck it, otherwise it will remember a stale 'tick' that the user subsequently reverted
    m_DontAnnoyMeCheck->SetValue(0);

    m_staticTextMessage->Fit();
    GetSizer()->Fit(this);
    GetParent()->GetSizer()->Layout();
    GetParent()->Refresh();
}

void MessagePane::ShowMessage(const wxString& message, bool showHideButton, const wxBitmap& bmp,
                              const ButtonDetails& btn1, const ButtonDetails& btn2, const ButtonDetails& btn3,
                              const CheckboxDetails& chkbox)
{
    // Dont duplicate messages...
    if(m_messages.IsExists(message)) return;

    MessageDetails msg;
    msg.message = message;
    msg.btn1 = btn1;
    msg.btn2 = btn2;
    msg.btn3 = btn3;
    msg.check = chkbox;
    msg.bmp = bmp;
    msg.showHideButton = showHideButton;
    m_messages.PushMessage(msg);
    DoShowCurrentMessage();
}

void MessagePane::OnActionButton(wxCommandEvent& event)
{
    MessageDetails msg = m_messages.CurrentMessage();
    SavePreferenceIfNeeded(msg, 0);
    DoPostEvent(msg.btn1);
    DoShowNextMessage();
}

void MessagePane::DoShowNextMessage()
{
    m_messages.PopMessage();
    if(m_messages.IsEmpty()) {
        DoHide();
        return;
    }

    DoShowCurrentMessage();
}

/////////////////////////////////////////////////////////////////////////////
//

bool MessagePaneData::IsExists(const wxString& msg)
{
    for(size_t i = 0; i < m_queue.size(); i++) {
        if(msg == m_queue.at(i).message) return true;
    }
    return false;
}

void MessagePaneData::Clear() { m_queue.clear(); }

MessageDetails MessagePaneData::CurrentMessage()
{
    if(m_queue.empty()) return MessageDetails();

    return m_queue.back();
}

bool MessagePaneData::IsEmpty() { return m_queue.empty(); }

void MessagePaneData::PopMessage()
{
    if(m_queue.empty()) return;

    m_queue.pop_back();
}

void MessagePaneData::PushMessage(const MessageDetails& msg) { m_queue.push_back(msg); }

void MessagePane::OnEraseBG(wxEraseEvent& event) { wxUnusedVar(event); }

void MessagePane::OnPaint(wxPaintEvent& event)
{
    wxBufferedPaintDC dc(this);
    //	wxRect rr = GetClientRect();
    //
    //	dc.SetPen(  wxPen  (wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
    //	dc.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
    //	dc.DrawRectangle(rr);
    //
    //	rr.Deflate(1);
    //	dc.SetPen(wxPen( *wxRED_PEN ));
    //	dc.SetBrush( *wxTRANSPARENT_BRUSH );
    //
    //	dc.DrawRectangle(rr);
}

void MessagePane::OnActionButton1(wxCommandEvent& event)
{
    MessageDetails msg = m_messages.CurrentMessage();
    SavePreferenceIfNeeded(msg, 1);
    DoPostEvent(msg.btn2);
    DoShowNextMessage();
}

void MessagePane::OnActionButton2(wxCommandEvent& event)
{
    MessageDetails msg = m_messages.CurrentMessage();
    SavePreferenceIfNeeded(msg, 2);
    DoPostEvent(msg.btn3);
    DoShowNextMessage();
}

void MessagePane::DoPostEvent(ButtonDetails btn)
{
    if(btn.commandId != wxNOT_FOUND && btn.window) {
        if(btn.menuCommand) {
            wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, btn.commandId);
            btn.window->AddPendingEvent(evt);
        } else {
            wxCommandEvent evt(btn.commandId);
            btn.window->AddPendingEvent(evt);
        }
    }
}

void MessagePane::SavePreferenceIfNeeded(const MessageDetails msg, int choice)
{
    // If the checkbox is both shown (to cater for random ticks) and checked, save the preference
    if(choice != wxNOT_FOUND && m_DontAnnoyMeCheck->IsShown() && m_DontAnnoyMeCheck->IsChecked()) {
        wxString label = msg.check.GetLabel();
        if(!label.IsEmpty()) {
            clConfig::Get().SetAnnoyingDlgAnswer(label, choice + 1); // +1 to skip the Hide button
        }
    }

    if(choice == wxNOT_FOUND && m_DontAnnoyMeCheck->IsShown()) {
        wxString label = msg.check.GetLabel();
        if(!label.IsEmpty()) {
            clConfig::Get().SetAnnoyingDlgAnswer(label, m_DontAnnoyMeCheck->IsChecked() ? 1 : 0);
        }
    }
}