summaryrefslogtreecommitdiffstats
path: root/languages/cpp/debugger/gdbbreakpointwidget.cpp
blob: 8626a3e1a73e7f869dd772c031ddc030b807e5f2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
/***************************************************************************
    begin                : Tue May 13 2003
    copyright            : (C) 2003 by John Birch
    email                : jbb@tdevelop.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "gdbbreakpointwidget.h"
#include "gdbtable.h"
#include "debuggertracingdialog.h"
#include "gdbcommand.h"
#include "gdbcontroller.h"

#include "breakpoint.h"
#include "domutil.h"

#include <kdebug.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kurl.h>
#include <kmessagebox.h>

#include <tqvbuttongroup.h>
#include <tqfileinfo.h>
#include <tqheader.h>
#include <tqtable.h>
#include <tqtoolbutton.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>
#include <tqvbox.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqcheckbox.h>

#include <stdlib.h>
#include <ctype.h>

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

namespace GDBDebugger
{

enum Column {
    Control     = 0,
    Enable      = 1,
    Type        = 2,
    tqStatus      = 3,
    Location    = 4,
    Condition   = 5,
    IgnoreCount = 6,
    Hits        = 7,
    Tracing     = 8
};


#define numCols 9

enum BW_ITEMS { BW_ITEM_Show, BW_ITEM_Edit, BW_ITEM_Disable, BW_ITEM_Delete,
                BW_ITEM_DisableAll, BW_ITEM_EnableAll, BW_ITEM_DeleteAll};

static int m_activeFlag = 0;

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

class BreakpointTableRow : public TQTableItem
{
public:

    BreakpointTableRow(TQTable* table, EditType editType, Breakpoint* bp);
    ~BreakpointTableRow();

    bool match (Breakpoint* bp) const;
    void reset ();
    void setRow();

    Breakpoint* breakpoint()        { return m_breakpoint; }

private:
    void appendEmptyRow();

private:
    Breakpoint* m_breakpoint;
};

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

BreakpointTableRow::BreakpointTableRow(TQTable* parent, EditType editType,
                                       Breakpoint* bp) :
        TQTableItem(parent, editType, ""),
        m_breakpoint(bp)
{
    appendEmptyRow();
    setRow();
}

/***************************************************************************/

BreakpointTableRow::~BreakpointTableRow()
{
    m_breakpoint->deleteLater();
}

/***************************************************************************/

bool BreakpointTableRow::match(Breakpoint* breakpoint) const
{
    return m_breakpoint->match(breakpoint);
}

/***************************************************************************/

void BreakpointTableRow::reset()
{
    m_breakpoint->reset();
    setRow();
}

/***************************************************************************/

void BreakpointTableRow::appendEmptyRow()
{
    int row = table()->numRows();
    table()->setNumRows(row+1);

    table()->setItem(row, Control, this);

    TQCheckTableItem* cti = new TQCheckTableItem( table(), "");
    table()->setItem(row, Enable, cti);

    ComplexEditCell* act = new ComplexEditCell(table());
    table()->setItem(row, Tracing, act);
    TQObject::connect(act, TQT_SIGNAL(edit(TQTableItem*)),
                     table()->parent(), TQT_SLOT(editTracing(TQTableItem*)));
}

/***************************************************************************/

void BreakpointTableRow::setRow()
{
    if ( m_breakpoint )
    {
        TQTableItem *item =  table()->item ( row(), Enable );
        Q_ASSERT(item->rtti() == 2);
        ((TQCheckTableItem*)item)->setChecked(m_breakpoint->isEnabled());

        TQString status=m_breakpoint->statusDisplay(m_activeFlag);

        table()->setText(row(), tqStatus, status);
        table()->setText(row(), Condition, m_breakpoint->conditional());
        table()->setText(row(), IgnoreCount, TQString::number(m_breakpoint->ignoreCount() ));
        table()->setText(row(), Hits, TQString::number(m_breakpoint->hits() ));

        TQString displayType = m_breakpoint->displayType();
        table()->setText(row(), Location, m_breakpoint->location());


        TQTableItem* ce = table()->item( row(), Tracing );
        ce->setText(breakpoint()->tracingEnabled() ? "Enabled" : "Disabled");
        // In case there's editor open in this cell, update it too.
        static_cast<ComplexEditCell*>(ce)->updateValue();


        if (m_breakpoint->isTemporary())
            displayType = i18n(" temporary");
        if (m_breakpoint->isHardwareBP())
            displayType += i18n(" hw");

        table()->setText(row(), Type, displayType);
        table()->adjustColumn(Type);
        table()->adjustColumn(tqStatus);
        table()->adjustColumn(Location);
        table()->adjustColumn(Hits);
        table()->adjustColumn(IgnoreCount);
        table()->adjustColumn(Condition);
    }
}

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

GDBBreakpointWidget::GDBBreakpointWidget(GDBController* controller,
                                         TQWidget *parent, const char *name) :
TQHBox(parent, name),
controller_(controller)
{
    m_table = new GDBTable(0, numCols, this, name);
    m_table->setSelectionMode(TQTable::SingleRow);
    m_table->setShowGrid (false);
    m_table->setLeftMargin(0);
    m_table->setFocusStyle(TQTable::FollowStyle);

    m_table->hideColumn(Control);
    m_table->setColumnReadOnly(Type, true);
    m_table->setColumnReadOnly(tqStatus, true);
    m_table->setColumnReadOnly(Hits, true);
    m_table->setColumnWidth( Enable, 20);

    TQHeader *header = m_table->horizontalHeader();

    header->setLabel( Enable,       "" );
    header->setLabel( Type,         i18n("Type") );
    header->setLabel( tqStatus,       i18n("Status") );
    header->setLabel( Location,     i18n("Location") );
    header->setLabel( Condition,    i18n("Condition") );
    header->setLabel( IgnoreCount,  i18n("Ignore Count") );
    header->setLabel( Hits,         i18n("Hits") );
    header->setLabel( Tracing,      i18n("Tracing") );

    TQPopupMenu* newBreakpoint = new TQPopupMenu(this);
    newBreakpoint->insertItem(i18n("Code breakpoint", "Code"),
                              BP_TYPE_FilePos);
    newBreakpoint->insertItem(i18n("Data breakpoint", "Data write"),
                              BP_TYPE_Watchpoint);
    newBreakpoint->insertItem(i18n("Data read breakpoint", "Data read"),
                              BP_TYPE_ReadWatchpoint);


    m_ctxMenu = new TQPopupMenu( this );
    m_ctxMenu->insertItem( i18n("New breakpoint", "New"),
                                newBreakpoint);
    m_ctxMenu->insertItem( i18n( "Show text" ),    BW_ITEM_Show );
    int edit_id =
        m_ctxMenu->insertItem( i18n( "Edit" ),    BW_ITEM_Edit );
    m_ctxMenu->setAccel(TQt::Key_Enter, edit_id);
    m_ctxMenu->insertItem( i18n( "Disable" ), BW_ITEM_Disable );
    int del_id =
        m_ctxMenu->insertItem( SmallIcon("breakpoint_delete"),
                               i18n( "Delete" ),  BW_ITEM_Delete );
    m_ctxMenu->setAccel(TQt::Key_Delete, del_id);
    m_ctxMenu->insertSeparator();
    m_ctxMenu->insertItem( i18n( "Disable all"), BW_ITEM_DisableAll );
    m_ctxMenu->insertItem( i18n( "Enable all"), BW_ITEM_EnableAll );
    m_ctxMenu->insertItem( i18n( "Delete all"), BW_ITEM_DeleteAll );

    m_table->show();

    connect( newBreakpoint,       TQT_SIGNAL(activated(int)),
             this,          TQT_SLOT(slotAddBlankBreakpoint(int)) );

    connect( m_table,       TQT_SIGNAL(contextMenuRequested(int, int, const TQPoint &)),
             this,          TQT_SLOT(slotContextMenuShow(int, int, const TQPoint & )) );
    connect( m_ctxMenu,     TQT_SIGNAL(activated(int)),
             this,          TQT_SLOT(slotContextMenuSelect(int)) );

    connect( m_table,       TQT_SIGNAL(doubleClicked(int, int, int, const TQPoint &)),
             this,          TQT_SLOT(slotRowDoubleClicked(int, int, int, const TQPoint &)));

    connect( m_table,       TQT_SIGNAL(valueChanged(int, int)),
             this,          TQT_SLOT(slotNewValue(int, int)));

    connect( m_table,       TQT_SIGNAL(returnPressed()),
             this,          TQT_SLOT(slotEditBreakpoint()));
//    connect( m_table,       TQT_SIGNAL(f2Pressed()),
//             this,          TQT_SLOT(slotEditBreakpoint()));
    connect( m_table,       TQT_SIGNAL(deletePressed()),
             this,          TQT_SLOT(slotRemoveBreakpoint()));
// This slot doesn't exist anymore
//     connect( m_table,       TQT_SIGNAL(insertPressed()),
//              this,          TQT_SLOT(slotAddBlankBreakpoint()));

    // FIXME: maybe, all debugger components should derive from
    // a base class that does this connect.
    connect(controller, TQT_SIGNAL(event(GDBController::event_t)),
            this,       TQT_SLOT(slotEvent(GDBController::event_t)));

    connect(controller,
            TQT_SIGNAL(watchpointHit(int, const TQString&, const TQString&)),
            this,
            TQT_SLOT(slotWatchpointHit(int, const TQString&, const TQString&)));
}

/***************************************************************************/

GDBBreakpointWidget::~GDBBreakpointWidget()
{
    delete m_table;
}

/***************************************************************************/

void GDBBreakpointWidget::reset()
{
    for ( int row = 0; row < m_table->numRows(); row++ )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr)
        {
            btr->reset();
            sendToGdb(*(btr->breakpoint()));
        }
    }
}

/***************************************************************************/

// When a file is loaded then we need to tell the editor (display window)
// which lines contain a breakpoint.
void GDBBreakpointWidget::slotRefreshBP(const KURL &filename)
{
    for ( int row = 0; row < m_table->numRows(); row++ )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr)
        {
            FilePosBreakpoint* bp = dynamic_cast<FilePosBreakpoint*>(btr->breakpoint());
            if (bp && bp->hasFileAndLine()
                && (bp->fileName() == filename.path()))
                emit refreshBPState(*bp);
        }
    }
}

void GDBBreakpointWidget::slotBreakpointHit(int id)
{
    BreakpointTableRow* br = findId(id);

    // FIXME: should produce an message, this is most likely
    // an error.
    if (!br)
        return;

    Breakpoint* b = br->breakpoint();

    if (b->tracingEnabled())
    {
        controller_->addCommand(
            new CliCommand(("printf "
                            + b->traceRealFormatString()).latin1(),
                           this,
                           &GDBBreakpointWidget::handleTracingPrintf));

        controller_->addCommand(new
                            GDBCommand("-exec-continue"));

    }
    else
    {
        controller_->demandAttention();
    }
}

void GDBBreakpointWidget::slotWatchpointHit(int id,
                                            const TQString& oldValue,
                                            const TQString& newValue)
{
    BreakpointTableRow* br = findId(id);

    // FIXME: should produce an message, this is most likely
    // an error.
    if (!br)
        return;

    Watchpoint* b = dynamic_cast<Watchpoint*>(br->breakpoint());


    KMessageBox::information(
        0,
        i18n("<b>Data write breakpoint</b><br>"
             "Expression: %1<br>"
             "Address: 0x%2<br>"
             "Old value: %3<br>"
             "New value: %4")
        .tqarg(b->varName())
        .tqarg(b->address(), 0, 16)
        .tqarg(oldValue)
        .tqarg(newValue));
}

/***************************************************************************/

BreakpointTableRow* GDBBreakpointWidget::find(Breakpoint *breakpoint)
{
    // NOTE:- The match doesn't have to be equal. Each type of bp
    // must decide on the match criteria.
    Q_ASSERT (breakpoint);

    for ( int row = 0; row < m_table->numRows(); row++ )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr && btr->match(breakpoint))
            return btr;
    }

    return 0;
}

/***************************************************************************/

// The Id is supplied by the debugger
BreakpointTableRow* GDBBreakpointWidget::findId(int dbgId)
{
    for ( int row = 0; row < m_table->numRows(); row++ )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr && btr->breakpoint()->dbgId() == dbgId)
            return btr;
    }

    return 0;
}

/***************************************************************************/

// The key is a unique number supplied by us
BreakpointTableRow* GDBBreakpointWidget::findKey(int BPKey)
{
    for ( int row = 0; row < m_table->numRows(); row++ )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr && btr->breakpoint()->key() == BPKey)
            return btr;
    }

    return 0;
}

bool GDBBreakpointWidget::hasWatchpointForAddress(
    unsigned long long address) const
{
    for(int i = 0; i < m_table->numRows(); ++i)
    {
        BreakpointTableRow* br = (BreakpointTableRow*)
            m_table->item(i, Control);

        Watchpoint* w = dynamic_cast<Watchpoint*>(br->breakpoint());
        if (w && w->address() == address)
            return true;
    }
    return false;
}

/***************************************************************************/

BreakpointTableRow* GDBBreakpointWidget::addBreakpoint(Breakpoint *bp)
{
    BreakpointTableRow* btr =
        new BreakpointTableRow( m_table, TQTableItem::WhenCurrent, bp );

    connect(bp, TQT_SIGNAL(modified(Breakpoint*)),
            this, TQT_SLOT(slotBreakpointModified(Breakpoint*)));

    sendToGdb(*bp);

    return btr;
}

/***************************************************************************/

void GDBBreakpointWidget::removeBreakpoint(BreakpointTableRow* btr)
{
    if (!btr)
        return;

    // Pending but the debugger hasn't started processing this bp so
    // we can just remove it.
    Breakpoint* bp = btr->breakpoint();
    // No gdb breakpoint, and no breakpoint addition command in the
    // queue. Just remove.
    if (bp->dbgId() == -1 && !bp->isDbgProcessing())
    {
        bp->setActionDie();
        sendToGdb(*bp);
        m_table->removeRow(btr->row());
    }
    else
    {
        bp->setActionClear(true);
        sendToGdb(*bp);
        btr->setRow();
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotToggleBreakpoint(const TQString &fileName, int lineNum)
{
    FilePosBreakpoint *fpBP = new FilePosBreakpoint(fileName, lineNum+1);

    BreakpointTableRow* btr = find(fpBP);
    if (btr)
    {
        removeBreakpoint(btr);
    }
    else
        addBreakpoint(fpBP);
}

/***************************************************************************/

void GDBBreakpointWidget::slotToggleBreakpointEnabled(const TQString &fileName, int lineNum)
{
    FilePosBreakpoint *fpBP = new FilePosBreakpoint(fileName, lineNum+1);

    BreakpointTableRow* btr = find(fpBP);
    delete fpBP;
    if (btr)
    {
        Breakpoint* bp=btr->breakpoint();
        bp->setEnabled(!bp->isEnabled());
        sendToGdb(*bp);
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotToggleWatchpoint(const TQString &varName)
{
    Watchpoint *watchpoint = new Watchpoint(varName, false, true);
    BreakpointTableRow* btr = find(watchpoint);
    if (btr)
    {
        removeBreakpoint(btr);
        delete watchpoint;
    }
    else
        addBreakpoint(watchpoint);
}

void GDBBreakpointWidget::handleBreakpointList(const GDBMI::ResultRecord& r)
{
    m_activeFlag++;

    const GDBMI::Value& blist = r["BreakpointTable"]["body"];

    for(unsigned i = 0, e = blist.size(); i != e; ++i)
    {
        const GDBMI::Value& b = blist[i];

        int id = b["number"].literal().toInt();
        BreakpointTableRow* btr = findId(id);
        if (btr)
        {
            Breakpoint *bp = btr->breakpoint();
            bp->setActive(m_activeFlag, id);
            bp->setHits(b["times"].toInt());
            if (b.hasField("ignore"))
                bp->setIgnoreCount(b["ignore"].toInt());
            else
                bp->setIgnoreCount(0);
            if (b.hasField("cond"))
                bp->setConditional(b["cond"].literal());
            else
                bp->setConditional(TQString());
            btr->setRow();
            emit publishBPState(*bp);
        }
        else
        {
            // It's a breakpoint added outside, most probably
            // via gdb console. Add it now.
            TQString type = b["type"].literal();

            if (type == "breakpoint" || type == "hw breakpoint")
            {
                if (b.hasField("fullname") && b.hasField("line"))
                {
                    Breakpoint* bp = new FilePosBreakpoint(
                        b["fullname"].literal(),
                        b["line"].literal().toInt());

                    bp->setActive(m_activeFlag, id);
                    bp->setActionAdd(false);
                    bp->setPending(false);

                    new BreakpointTableRow(m_table,
                                           TQTableItem::WhenCurrent,
                                           bp);

                    emit publishBPState(*bp);
                }
            }

        }
    }

    // Remove any inactive breakpoints.
    for ( int row = m_table->numRows()-1; row >= 0 ; row-- )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr)
        {
            Breakpoint* bp = btr->breakpoint();
            if (!(bp->isActive(m_activeFlag)))
            {
                // FIXME: need to review is this happens for
                // as-yet unset breakpoint.
                bp->removedInGdb();
            }
        }
    }
}

void GDBBreakpointWidget::handleTracingPrintf(const TQValueVector<TQString>& s)
{
    // The first line of output is the command itself, which we don't need.
    for(unsigned i = 1; i < s.size(); ++i)
        emit tracingOutput(s[i].local8Bit());
}

/***************************************************************************/

void GDBBreakpointWidget::slotBreakpointSet(Breakpoint* bp)
{
    // FIXME: why 'key' is used here?
    BreakpointTableRow* btr = findKey(bp->key());
    if (!btr)
    {
        kdDebug(9012) << "Early return\n";
        return;
    }

    btr->setRow();
}

/***************************************************************************/

void GDBBreakpointWidget::slotAddBlankBreakpoint(int idx)
{
    BreakpointTableRow* btr = 0;
    switch (idx)
    {
      case BP_TYPE_FilePos:
          btr = addBreakpoint(new FilePosBreakpoint());
          break;

      case BP_TYPE_Watchpoint:
          btr = addBreakpoint(new Watchpoint(""));
          break;

      case BP_TYPE_ReadWatchpoint:
          btr = addBreakpoint(new ReadWatchpoint(""));
          break;

      default:
          break;
    }

    if (btr)
    {
        m_table->selectRow(btr->row());
        m_table->editCell(btr->row(), Location, false);
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotRemoveBreakpoint()
{
    int row = m_table->currentRow();
    if ( row != -1)
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        removeBreakpoint(btr);
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotRemoveAllBreakpoints()
{
    for ( int row = m_table->numRows()-1; row>=0; row-- )
    {
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        removeBreakpoint(btr);
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotRowDoubleClicked(int row, int col, int btn, const TQPoint &)
{
    if ( btn == Qt::LeftButton )
    {
//    kdDebug(9012) << "in slotRowSelected row=" << row << endl;
        BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
        if (btr)
        {
            FilePosBreakpoint* bp = dynamic_cast<FilePosBreakpoint*>(btr->breakpoint());
            if (bp && bp->hasFileAndLine())
                emit gotoSourcePosition(bp->fileName(), bp->lineNum()-1);

            // put the focus back on the clicked item if appropriate
            if (col == Location || col ==  Condition || col == IgnoreCount)
                m_table->editCell(row, col, false);
        }
    }
}

void GDBBreakpointWidget::slotContextMenuShow( int row, int /*col*/, const TQPoint &mousePos )
{
    BreakpointTableRow *btr = (BreakpointTableRow *)m_table->item(row, Control );

    if (btr == NULL)
    {
        btr = (BreakpointTableRow *)m_table->item(m_table->currentRow(),
                                                  Control );
    }

    if (btr != NULL)
    {
        m_ctxMenu->setItemEnabled(
            BW_ITEM_Show,
            btr->breakpoint()->hasFileAndLine());

        if (btr->breakpoint( )->isEnabled( ))
        {
            m_ctxMenu->changeItem( BW_ITEM_Disable, i18n("Disable") );
        }
        else
        {
            m_ctxMenu->changeItem( BW_ITEM_Disable, i18n("Enable") );
        }

        m_ctxMenu->setItemEnabled(BW_ITEM_Disable, true);
        m_ctxMenu->setItemEnabled(BW_ITEM_Delete, true);
        m_ctxMenu->setItemEnabled(BW_ITEM_Edit, true);
    }
    else
    {
        m_ctxMenu->setItemEnabled(BW_ITEM_Show, false);
        m_ctxMenu->setItemEnabled(BW_ITEM_Disable, false);
        m_ctxMenu->setItemEnabled(BW_ITEM_Delete, false);
        m_ctxMenu->setItemEnabled(BW_ITEM_Edit, false);
    }

    bool has_bps = (m_table->numRows() != 0);
    m_ctxMenu->setItemEnabled(BW_ITEM_DisableAll, has_bps);
    m_ctxMenu->setItemEnabled(BW_ITEM_EnableAll, has_bps);
    m_ctxMenu->setItemEnabled(BW_ITEM_Delete, has_bps);

    m_ctxMenu->popup( mousePos );
}

void GDBBreakpointWidget::slotContextMenuSelect( int item )
{
    int                  row, col;
    BreakpointTableRow  *btr;
    Breakpoint          *bp;
    FilePosBreakpoint   *fbp;

    row= m_table->currentRow( );
    if (row == -1)
        return;
    btr = (BreakpointTableRow *)m_table->item( row, Control );
    if (btr == NULL)
        return;
    bp = btr->breakpoint( );
    if (bp == NULL)
        return;
    fbp = dynamic_cast<FilePosBreakpoint*>(bp);

    switch( item )
    {
        case BW_ITEM_Show:
            if (fbp)
                emit gotoSourcePosition(fbp->fileName(), fbp->lineNum()-1);
            break;
        case BW_ITEM_Edit:
            col = m_table->currentColumn( );
            if (col == Location || col ==  Condition || col == IgnoreCount)
                m_table->editCell(row, col, false);
            break;
        case BW_ITEM_Disable:

            bp->setEnabled( !bp->isEnabled( ) );
            btr->setRow( );
            sendToGdb( *bp );
            break;
        case BW_ITEM_Delete:
            slotRemoveBreakpoint( );
            break;
        case BW_ITEM_DeleteAll:
            slotRemoveAllBreakpoints();
            break;
        case BW_ITEM_DisableAll:
        case BW_ITEM_EnableAll:
            for ( int row = 0; row < m_table->numRows(); row++ )
            {
                BreakpointTableRow* btr = (BreakpointTableRow *)
                    m_table->item(row, Control);

                if (btr)
                {
                    btr->breakpoint()->setEnabled(item == BW_ITEM_EnableAll);
                    btr->setRow();
                    sendToGdb(*btr->breakpoint());
                }
            }
            break;
        default:
            // oops, check it out! this case is not in sync with the
            // m_ctxMenu.  Check the enum in the header file.
            return;
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotEditRow(int row, int col, const TQPoint &)
{
//    kdDebug(9012) << "in slotEditRow row=" << row << endl;
    BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);
    if (btr)
    {
        if (col == Location || col ==  Condition || col == IgnoreCount)
            m_table->editCell(row, col, false);
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotNewValue(int row, int col)
{
    BreakpointTableRow* btr = (BreakpointTableRow *) m_table->item(row, Control);

    TQString new_value = m_table->text(row, col);

    if (btr)
    {
        Breakpoint* bp = btr->breakpoint();
        switch (col)
        {
        case Enable:
        {
            TQCheckTableItem *item =
                (TQCheckTableItem*)m_table->item ( row, Enable );
            bp->setEnabled(item->isChecked());
        }
        break;

        case Location:
        {
            if (bp->location() != new_value)
            {
                // GDB does not allow to change location of
                // an existing breakpoint. So, need to remove old
                // breakpoint and add another.

                // Announce to editor that breakpoit at its
                // current location is dying.
                bp->setActionDie();
                emit publishBPState(*bp);

                // However, we don't want the line in breakpoint
                // widget to disappear and appear again.

                // Emit delete command. This won't resync breakpoint
                // table (unlike clearBreakpoint), so we won't have
                // nasty effect where line in the table first disappears
                // and then appears again, and won't have internal issues
                // as well.
                if (!controller_->stateIsOn(s_dbgNotStarted))
                    controller_->addCommand(bp->dbgRemoveCommand().latin1());

                // Now add new breakpoint in gdb. It will correspond to
                // the same 'Breakpoint' and 'BreakpointRow' objects in
                // KDevelop is the previous, deleted, breakpoint.

                // Note: clears 'actionDie' implicitly.
                bp->setActionAdd(true);
                bp->setLocation(new_value);
            }
            break;
        }

        case Condition:
        {
            bp->setConditional(new_value);
            break;
        }

        case IgnoreCount:
        {
            bp->setIgnoreCount(new_value.toInt());
            break;
        }
        default:
            break;
        }

        bp->setActionModify(true);


        // This is not needed for most changes, since we've
        // just read a value from table cell to breakpoint, and
        // setRow will write back the same value to the cell.
        // It's only really needed for tracing column changes,
        // where tracing config dialog directly changes breakpoint,
        // so we need to send those changes to the table.
        btr->setRow();


        sendToGdb(*bp);
    }
}

/***************************************************************************/

void GDBBreakpointWidget::slotEditBreakpoint(const TQString &fileName, int lineNum)
{
    FilePosBreakpoint *fpBP = new FilePosBreakpoint(fileName, lineNum+1);

    BreakpointTableRow* btr = find(fpBP);
    delete fpBP;

    if (btr)
    {
        TQTableSelection ts;
        ts.init(btr->row(), 0);
        ts.expandTo(btr->row(), numCols);
        m_table->addSelection(ts);
        m_table->editCell(btr->row(), Location, false);
    }

}

void GDBBreakpointWidget::sendToGdb(Breakpoint& BP)
{
    // Announce the change in state. We need to do this before
    // everything. For example, if debugger is not yet running, we'll
    // immediate exit after setting pending flag, but we still want changes
    // in "enabled" flag to be shown on the left border of the editor.
    emit publishBPState(BP);

    BP.sendToGdb(controller_);
}

void GDBBreakpointWidget::slotBreakpointModified(Breakpoint* b)
{
    emit publishBPState(*b);

    if (BreakpointTableRow* btr = find(b))
    {
        if (b->isActionDie())
        {
            // Breakpoint was deleted, kill the table row.
            m_table->removeRow(btr->row());
        }
        else
        {
            btr->setRow();
        }
    }
}

void GDBBreakpointWidget::slotEvent(GDBController::event_t e)
{
    switch(e)
    {
    case GDBController::program_state_changed:
        {
            controller_->addCommand(
                new GDBCommand("-break-list",
                               this,
                               &GDBBreakpointWidget::handleBreakpointList));
            break;
        }

    case GDBController::shared_library_loaded:
    case GDBController::connected_to_program:
        {
            for ( int row = 0; row < m_table->numRows(); row++ )
            {
                BreakpointTableRow* btr = (BreakpointTableRow *)
                    m_table->item(row, Control);

                if (btr)
                {
                    Breakpoint* bp = btr->breakpoint();
                    if ( (bp->dbgId() == -1 ||  bp->isPending())
                         && !bp->isDbgProcessing()
                         && bp->isValid())
                    {
                        sendToGdb(*bp);
                    }
                }
            }
            break;
        }
    case GDBController::program_exited:
        {
            for(int row = 0; row < m_table->numRows(); ++row)
            {
                Breakpoint* b = static_cast<BreakpointTableRow*>(
                    m_table->item(row, Control))->breakpoint();

                b->applicationExited(controller_);
            }
        }

    default:
        ;
    }
}


/***************************************************************************/

void GDBBreakpointWidget::slotEditBreakpoint()
{
    m_table->editCell(m_table->currentRow(), Location, false);
}


void GDBBreakpointWidget::editTracing(TQTableItem* item)
{
    BreakpointTableRow* btr = (BreakpointTableRow *)
        m_table->item(item->row(), Control);

    DebuggerTracingDialog* d = new DebuggerTracingDialog(
        btr->breakpoint(), m_table, "");

    int r = d->exec();

    // Note: change cell text here and explicitly call slotNewValue here.
    // We want this signal to be emitted when we close the tracing dialog
    // and not when we select some other cell, as happens in TQt by default.
    if (r == TQDialog::Accepted)
    {
        // The dialog has modified "btr->breakpoint()" already.
        // Calling 'slotNewValue' will flush the changes back
        // to the table.
        slotNewValue(item->row(), item->col());
    }

    delete d;
}


/***************************************************************************/

void GDBBreakpointWidget::savePartialProjectSession(TQDomElement* el)
{
    TQDomDocument domDoc = el->ownerDocument();
    if (domDoc.isNull())
        return;

    TQDomElement breakpointListEl = domDoc.createElement("breakpointList");
    for ( int row = 0; row < m_table->numRows(); row++ )
    {
        BreakpointTableRow* btr =
            (BreakpointTableRow *) m_table->item(row, Control);
        Breakpoint* bp = btr->breakpoint();

        TQDomElement breakpointEl =
            domDoc.createElement("breakpoint"+TQString::number(row));

        breakpointEl.setAttribute("type", bp->type());
        breakpointEl.setAttribute("location", bp->location(false));
        breakpointEl.setAttribute("enabled", bp->isEnabled());
        breakpointEl.setAttribute("condition", bp->conditional());
        breakpointEl.setAttribute("tracingEnabled",
                                  TQString::number(bp->tracingEnabled()));
        breakpointEl.setAttribute("traceFormatStringEnabled",
                                  TQString::number(bp->traceFormatStringEnabled()));
        breakpointEl.setAttribute("tracingFormatString",
                                  bp->traceFormatString());

        TQDomElement tracedExpressions =
            domDoc.createElement("tracedExpressions");

        TQStringList::const_iterator i, e;
        for(i = bp->tracedExpressions().begin(),
                e = bp->tracedExpressions().end();
            i != e; ++i)
        {
            TQDomElement expr = domDoc.createElement("expression");
            expr.setAttribute("value", *i);
            tracedExpressions.appendChild(expr);
        }

        breakpointEl.appendChild(tracedExpressions);

        breakpointListEl.appendChild(breakpointEl);
    }

    if (!breakpointListEl.isNull())
        el->appendChild(breakpointListEl);
}

/***************************************************************************/

void GDBBreakpointWidget::restorePartialProjectSession(const TQDomElement* el)
{
    /** Eventually, would be best to make each breakpoint type handle loading/
        saving it's data. The only problem is that on load, we need to allocate
        with new different objects, depending on type, and that requires some
        kind of global registry. Gotta find out if a solution for that exists in
        KDE (Boost.Serialization is too much dependency, and rolling my own is
        boring).
    */
    TQDomElement breakpointListEl = el->namedItem("breakpointList").toElement();
    if (!breakpointListEl.isNull())
    {
        TQDomElement breakpointEl;
        for (breakpointEl = breakpointListEl.firstChild().toElement();
                !breakpointEl.isNull();
                breakpointEl = breakpointEl.nextSibling().toElement())
        {
            Breakpoint* bp=0;
            BP_TYPES type = (BP_TYPES) breakpointEl.attribute( "type", "0").toInt();
            switch (type)
            {
            case BP_TYPE_FilePos:
            {
                bp = new FilePosBreakpoint();
                break;
            }
            case BP_TYPE_Watchpoint:
            {
                bp = new Watchpoint("");
                break;
            }
            default:
                break;
            }

            // Common settings for any type of breakpoint
            if (bp)
            {
                bp->setLocation(breakpointEl.attribute( "location", ""));
                if (type == BP_TYPE_Watchpoint)
                {
                    bp->setEnabled(false);
                }
                else
                {
                    bp->setEnabled(
                        breakpointEl.attribute( "enabled", "1").toInt());
                }
                bp->setConditional(breakpointEl.attribute( "condition", ""));

                bp->setTracingEnabled(
                    breakpointEl.attribute("tracingEnabled", "0").toInt());
                bp->setTraceFormatString(
                    breakpointEl.attribute("tracingFormatString", ""));
                bp->setTraceFormatStringEnabled(
                    breakpointEl.attribute("traceFormatStringEnabled", "0")
                    .toInt());

                TQDomNode tracedExpr =
                    breakpointEl.namedItem("tracedExpressions");

                if (!tracedExpr.isNull())
                {
                    TQStringList l;

                    for(TQDomNode c = tracedExpr.firstChild(); !c.isNull();
                        c = c.nextSibling())
                    {
                        TQDomElement el = c.toElement();
                        l.push_back(el.attribute("value", ""));
                    }
                    bp->setTracedExpressions(l);
                }

                // Now add the breakpoint. Don't try to check if
                // breakpoint already exists.
                // It's easy to check that breakpoint on the same
                // line already exists, but it might have different condition,
                // and checking conditions for equality is too complex thing.
                // And anyway, it's will be suprising of realoading a project
                // changes the set of breakpoints.
                addBreakpoint(bp);
            }
        }
    }
}

/***************************************************************************/

void GDBBreakpointWidget::focusInEvent( TQFocusEvent */* e*/ )
{
    // Without the following 'if', when we first open the breakpoints
    // widget, the background is all black. This happens only with
    //    m_table->setFocusStyle(TQTable::FollowStyle);
    // in constructor, so I suspect TQt bug. But anyway, without
    // current cell keyboard actions like Enter for edit won't work,
    // so keyboard focus does not makes much sense.
    if (m_table->currentRow() == -1 ||
        m_table->currentColumn() == -1)
    {
        m_table->setCurrentCell(0, 0);
    }
    m_table->setFocus();
}

ComplexEditCell::
ComplexEditCell(TQTable* table)
: TQTableItem(table, TQTableItem::WhenCurrent)
{
}


TQWidget* ComplexEditCell::createEditor() const
{
    TQHBox* box = new TQHBox( table()->viewport() );
    box->setPaletteBackgroundColor(
               table()->tqpalette().active().highlight());

    label_ = new TQLabel(text(), box, "label");
    label_->setBackgroundMode(TQt::PaletteHighlight);
    // Sorry for hardcode, but '2' is already hardcoded in
    // TQt source, in TQTableItem::paint. Since I don't want the
    // text to jump 2 pixels to the right when editor is activated,
    // need to set the same indent for label.
    label_->setIndent(2);
    TQPalette p = label_->palette();

    p.setColor(TQPalette::Active, TQColorGroup::Foreground,
               table()->tqpalette().active().highlightedText());
    p.setColor(TQPalette::Inactive, TQColorGroup::Foreground,
               table()->tqpalette().active().highlightedText());

    label_->setPalette(p);

    TQPushButton* b = new TQPushButton("...", box);
    // This is exactly what is done in TQDesigner source in the
    // similar context. Haven't had any success making the good look
    // with tqlayout, I suppose that tqsizeHint for button is always larger
    // than 20.
    b->setFixedWidth( 20 );

    connect(b, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotEdit()));

    return box;
}

void ComplexEditCell::updateValue()
{
    if (!label_.isNull())
    {
        label_->setText(table()->text(row(), col()));
    }
}

void ComplexEditCell::slotEdit()
{
    emit edit(this);
}

}


#include "gdbbreakpointwidget.moc"