File: clPropertiesPage.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (551 lines) | stat: -rw-r--r-- 17,157 bytes parent folder | download | duplicates (2)
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
#include "clPropertiesPage.hpp"

#include "EditDlg.h"
#include "clDataViewListCtrl.h"
#include "clSystemSettings.h"
#include "clThemedListCtrl.h"
#include "codelite_events.h"
#include "event_notifier.h"
#include "globals.h"
#include "macros.h"

#include <wx/colordlg.h>
#include <wx/dirdlg.h>
#include <wx/filedlg.h>
#include <wx/numdlg.h>

wxDEFINE_EVENT(wxEVT_PROPERTIES_PAGE_MODIFIED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_PROPERTIES_PAGE_SAVED, clCommandEvent);

clPropertiesPage::clPropertiesPage(wxWindow* parent, wxWindowID id)
    : wxPanel(parent, id)
{
    SetSizer(new wxBoxSizer(wxVERTICAL));
    m_view = new clThemedListCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                                  wxDV_NO_HEADER | wxDV_COLUMN_WIDTH_NEVER_SHRINKS);

    // remove the wxTR_ROW_LINES style (this is the underlying style in clTreeCtrl
    // the controls the zebra line colouring
    m_view->SetTreeStyle(m_view->GetTreeStyle() & ~wxTR_ROW_LINES);

    GetSizer()->Add(m_view, wxSizerFlags(1).Expand());
    m_view->AppendTextColumn(_("Property Name"));
    m_view->AppendTextColumn(_("Property Value"));

    m_view->Bind(wxEVT_DATAVIEW_ACTION_BUTTON, &clPropertiesPage::OnActionButton, this);
    m_view->Bind(wxEVT_DATAVIEW_CHOICE, &clPropertiesPage::OnChoice, this);
    m_view->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &clPropertiesPage::OnValueChanged, this);
    EventNotifier::Get()->Bind(wxEVT_INIT_DONE, &clPropertiesPage::OnInitDone, this);
    GetSizer()->Layout();
}

clPropertiesPage::~clPropertiesPage()
{
    EventNotifier::Get()->Unbind(wxEVT_INIT_DONE, &clPropertiesPage::OnInitDone, this);
    m_view->Unbind(wxEVT_DATAVIEW_CHOICE, &clPropertiesPage::OnChoice, this);
    m_view->Unbind(wxEVT_DATAVIEW_ACTION_BUTTON, &clPropertiesPage::OnActionButton, this);
    m_view->Unbind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &clPropertiesPage::OnValueChanged, this);

    if(m_theme_event_connected) {
        EventNotifier::Get()->Unbind(wxEVT_SYS_COLOURS_CHANGED, &clPropertiesPage::OnThemeChanged, this);
    }
    m_view->DeleteAllItems();
}

void clPropertiesPage::AddProperty(const wxString& label, const wxArrayString& choices,
                                   const wxString& string_selection, clPropertiesPage::Callback_t update_cb)
{
    size_t index = choices.Index(string_selection);
    if(index == wxString::npos) {
        index = 0;
    }
    AddProperty(label, choices, static_cast<int>(index), std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, const std::vector<wxString>& choices, size_t sel,
                                   clPropertiesPage::Callback_t update_cb)
{
    wxArrayString arr;
    arr.reserve(choices.size());
    for(const auto& s : choices) {
        arr.Add(s);
    }
    AddProperty(label, arr, sel, std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, const std::vector<wxString>& choices,
                                   const wxString& selection, clPropertiesPage::Callback_t update_cb)
{
    wxArrayString arr;
    arr.reserve(choices.size());
    for(const auto& s : choices) {
        arr.Add(s);
    }
    AddProperty(label, arr, selection, std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, const wxArrayString& choices, size_t sel,
                                   clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    clDataViewTextWithButton choice(sel < choices.size() ? choices[sel] : wxString(),
                                    eCellButtonType::BT_DROPDOWN_ARROW, wxNOT_FOUND);
    wxVariant v;
    v << choice;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::CHOICE, choices, std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, bool checked, clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    clDataViewCheckbox c(checked);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::CHECKBOX, checked, std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, long value, clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    clDataViewTextWithButton c(wxString() << value, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::INTEGER, value, std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, const wxString& value, clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    // horizontal 3 dots symbol
    clDataViewTextWithButton c(value, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::TEXT_EDIT, value, std::move(update_cb));
}

void clPropertiesPage::AddProperty(const wxString& label, const wxColour& value, clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    clDataViewColour c(value);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::COLOUR, value, std::move(update_cb));
}

void clPropertiesPage::AddPropertyFilePicker(const wxString& label, const wxString& path,
                                             clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    // horizontal 3 dots symbol
    clDataViewTextWithButton c(path, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::FILE_PICKER, path, std::move(update_cb));
}

void clPropertiesPage::AddPropertyDirPicker(const wxString& label, const wxString& path,
                                            clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    // horizontal 3 dots symbol
    clDataViewTextWithButton c(path, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::DIR_PICKER, path, std::move(update_cb));
}

void clPropertiesPage::AddPropertyLanguagePicker(const wxString& label, const wxArrayString& langs,
                                                 clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    // horizontal 3 dots symbol
    wxString languages_str = wxJoin(langs, ';');
    clDataViewTextWithButton c(languages_str, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::LANGUAGE_PICKER, languages_str, std::move(update_cb));
}

void clPropertiesPage::AddPropertyButton(const wxString& label, const wxString& button_label,
                                         clPropertiesPage::Callback_t update_cb)
{
    wxVector<wxVariant> cols;
    cols.push_back(label);

    // horizontal 3 dots symbol
    clDataViewButton button(button_label, wxNOT_FOUND);
    wxVariant v;
    v << button;
    cols.push_back(v);
    m_view->AppendItem(cols);
    UpdateLastLineData(LineKind::BUTTON, nullptr, std::move(update_cb));
}

void clPropertiesPage::AddHeader(const wxString& label)
{
    // keep the header row number
    m_header_rows.push_back(m_view->GetItemCount());

    auto item = m_view->AppendItem(label);
    SetHeaderColours(item);
}

void clPropertiesPage::OnActionButton(wxDataViewEvent& e)
{
    auto item = e.GetItem();
    int row = m_view->ItemToRow(item);

    const LineData* data = nullptr;
    if(!GetLineData(row, &data))
        return;

    switch(data->line_kind) {
    case LineKind::TEXT_EDIT: {
        wxString value_str;
        if(!data->value.GetAs(&value_str))
            return;
        ShowTextEditor(row, value_str);
        break;
    }
    case LineKind::COLOUR: {
        wxColour colour_value;
        if(!data->value.GetAs(&colour_value))
            return;
        ShowColourPicker(row, colour_value);
        break;
    }
    case LineKind::CHOICE: {
        wxArrayString arr_value;
        if(!data->value.GetAs(&arr_value))
            return;
        ShowStringSelectionMenu(row, arr_value);
        break;
    }
    case LineKind::FILE_PICKER: {
        wxString path;
        if(!data->value.GetAs(&path))
            return;
        ShowFilePicker(row, path);
        break;
    }
    case LineKind::LANGUAGE_PICKER: {
        wxString curlangs;
        if(!data->value.GetAs(&curlangs))
            return;
        ShowLanguagePicker(row, curlangs);
        break;
    }
    case LineKind::DIR_PICKER: {
        wxString path;
        if(!data->value.GetAs(&path))
            return;
        ShowDirPicker(row, path);
        break;
    }
    case LineKind::INTEGER: {
        long value;
        if(!data->value.GetAs(&value))
            return;
        ShowNumberPicker(row, value);
        break;
    }
    case LineKind::BUTTON: {
        DoButtonClicked(row);
        break;
    }
    case LineKind::CHECKBOX:
    case LineKind::UNKNOWN:
    default:
        break;
    }
}

void clPropertiesPage::ShowColourPicker(size_t line, const wxColour& colour)
{
    wxColour c = ::wxGetColourFromUser(wxGetTopLevelParent(this), colour);
    if(!c.IsOk()) {
        return;
    }

    clDataViewColour v(c);
    wxVariant var;
    var << v;
    m_view->SetValue(var, line, 1);
    UpdateLineData(line, LineKind::COLOUR, c, nullptr);
    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::ShowTextEditor(size_t line, const wxString& text)
{
    // use EditDlg directly and not clGetTextFromUser() method
    // since empty string is a valid value
    EditDlg dlg(wxGetTopLevelParent(this), text);
    if(dlg.ShowModal() != wxID_OK) {
        return;
    }

    wxString new_text = dlg.GetText();

    // update the cell value
    clDataViewTextWithButton c(new_text, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    m_view->SetValue(v, line, 1);
    UpdateLineData(line, LineKind::TEXT_EDIT, new_text, nullptr);
    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::ShowStringSelectionMenu(size_t line, const wxArrayString& options)
{
    m_view->ShowStringSelectionMenu(m_view->RowToItem(line), options, 1);
    // any change is handled inside the `OnChoice` method
}

void clPropertiesPage::ShowFilePicker(size_t line, const wxString& path)
{
    wxString default_path = wxEmptyString;
    wxString default_name = wxEmptyString;
    wxString default_ext = wxEmptyString;
    if(!path.empty() && wxFileName(path).IsOk()) {
        default_path = wxFileName(path).GetPath();
        default_name = wxFileName(path).GetFullName();
        default_ext = wxFileName(path).GetExt();
    }

    wxString new_path = wxFileSelector(_("Choose a file"), default_path, default_name, default_ext, wxEmptyString, 0,
                                       wxGetTopLevelParent(this));
    if(new_path.empty()) {
        return;
    }

    // update the view
    clDataViewTextWithButton c(new_path, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    m_view->SetValue(v, line, 1);
    UpdateLineData(line, LineKind::FILE_PICKER, new_path, nullptr);
    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::ShowDirPicker(size_t line, const wxString& path)
{
    wxString new_path = wxDirSelector(_("Choose a directory"), path, 0, wxDefaultPosition, wxGetTopLevelParent(this));
    if(new_path.empty()) {
        return;
    }

    // update the view
    clDataViewTextWithButton c(new_path, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    m_view->SetValue(v, line, 1);
    UpdateLineData(line, LineKind::DIR_PICKER, new_path, nullptr);
    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::ShowLanguagePicker(size_t line, const wxString& langs)
{
    wxArrayString selected;
    if(!::clShowFileTypeSelectionDialog(wxGetTopLevelParent(this), wxStringTokenize(langs, ";", wxTOKEN_STRTOK),
                                        &selected)) {
        return;
    }

    // update the view
    wxString new_label = wxJoin(selected, ';');
    clDataViewTextWithButton c(new_label, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    m_view->SetValue(v, line, 1);
    UpdateLineData(line, LineKind::LANGUAGE_PICKER, new_label, nullptr);
    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::DoButtonClicked(size_t line)
{
    // call the user provided callback
    NotifyChange(line);
}

void clPropertiesPage::ShowNumberPicker(size_t line, long number)
{
    wxString label = m_view->GetItemText(m_view->RowToItem(line));
    wxNumberEntryDialog dlg(wxGetTopLevelParent(this), label, wxEmptyString, _("Choose a number"), number, -10000,
                            10000);
    if(dlg.ShowModal() != wxID_OK)
        return;

    int new_number = dlg.GetValue();
    clDataViewTextWithButton c(wxString() << new_number, eCellButtonType::BT_ELLIPSIS, wxNOT_FOUND);
    wxVariant v;
    v << c;
    m_view->SetValue(v, line, 1);
    UpdateLineData(line, LineKind::INTEGER, new_number, nullptr);
    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::SetHeaderColours(const wxDataViewItem& item)
{
    // Notice that we can't use here m_view->GetColours()
    // since we can't really tell if we tree got the event `wxEVT_SYS_COLOURS_CHANGED` before
    // we did
    wxColour baseColour = clSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
    wxColour header_bg_colour;
    wxColour header_text_colour;

    header_bg_colour = baseColour.ChangeLightness(80);
    if(DrawingUtils::IsDark(header_bg_colour)) {
        header_text_colour = "WHITE";
    } else {
        header_text_colour = "BLACK";
    }

    m_view->SetItemBold(item, true);
    m_view->SetItemBackgroundColour(item, header_bg_colour, 0);
    m_view->SetItemBackgroundColour(item, header_bg_colour, 1);

    m_view->SetItemTextColour(item, header_text_colour, 0);
    m_view->SetItemTextColour(item, header_text_colour, 1);
}

void clPropertiesPage::OnThemeChanged(clCommandEvent& event)
{
    event.Skip();
    for(size_t row : m_header_rows) {
        wxDataViewItem item = m_view->RowToItem(row);
        SetHeaderColours(item);
    }
    m_view->Refresh();
}

void clPropertiesPage::OnInitDone(wxCommandEvent& event)
{
    event.Skip();
    EventNotifier::Get()->Bind(wxEVT_SYS_COLOURS_CHANGED, &clPropertiesPage::OnThemeChanged, this);
    m_theme_event_connected = true;
}

bool clPropertiesPage::GetLineData(size_t line, const LineData** data) const
{
    if(!data) {
        return false;
    }
    auto iter = m_lines.find(line);
    if(iter == m_lines.end()) {
        return false;
    }

    *data = &(iter->second);
    return true;
}

void clPropertiesPage::OnChoice(wxDataViewEvent& event)
{
    event.Skip();
    size_t line = m_view->ItemToRow(event.GetItem());

    // Get the user callback and call it with the new value
    const LineData* line_data = nullptr;
    if(!GetLineData(line, &line_data))
        return;

    if(line_data->callback) {
        line_data->callback(m_view->GetItemText(m_view->RowToItem(line)), event.GetString());
    }
    SetModified();
}

void clPropertiesPage::OnValueChanged(wxDataViewEvent& event)
{
    event.Skip();
    size_t line = m_view->ItemToRow(event.GetItem());
    UpdateLineData(line, LineKind::CHECKBOX, m_view->IsItemChecked(event.GetItem(), 1), nullptr);

    // call the user callback for this change
    NotifyChange(line);
    SetModified();
}

void clPropertiesPage::SetModified()
{
    m_isModified = true;
    clCommandEvent modified_event(wxEVT_PROPERTIES_PAGE_MODIFIED);
    modified_event.SetEventObject(this);
    GetEventHandler()->AddPendingEvent(modified_event);
}

void clPropertiesPage::ClearModified()
{
    m_isModified = false;
    clCommandEvent modified_event(wxEVT_PROPERTIES_PAGE_SAVED);
    modified_event.SetEventObject(this);
    GetEventHandler()->AddPendingEvent(modified_event);
}

void clPropertiesPage::NotifyChange(size_t line)
{
    const LineData* line_data = nullptr;
    if(!GetLineData(line, &line_data))
        return;

    if(line_data->callback) {
        line_data->callback(m_view->GetItemText(m_view->RowToItem(line)), line_data->value);
    }
}

void clPropertiesPage::Finalize() { m_view->Commit(); }

void clPropertiesPage::Begin() { m_view->Begin(); }

void clPropertiesPage::Clear()
{
    CHECK_PTR_RET(m_view);
    m_view->DeleteAllItems();
    m_isModified = false;
    m_header_rows.clear();
    m_lines.clear();
}