Repository navigation
Expand file tree
/
Copy pathLayoutDialog.cpp
More file actions
2210 lines (2017 loc) · 64.9 KB
/
Copy pathLayoutDialog.cpp
File metadata and controls
2210 lines (2017 loc) · 64.9 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
// LayoutDialog.cpp : implementation file
//
#include "stdafx.h"
#include <set>
#include "iPodWizard.h"
#include "LayoutDialog.h"
#include ".\LayoutDialog.h"
#include <algorithm>
// CLayoutDialog dialog
// The hex digits of a text, in capitals, and no more than the eight of an address
CString HexDigitsOf(const CString &s)
{
CString d;
for (int i = 0; i < s.GetLength() && d.GetLength() < 8; i++)
{
TCHAR c = s[i];
if (c >= 'a' && c <= 'f')
c = (TCHAR)(c - 'a' + 'A');
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))
d += c;
}
return d;
}
// Digits in pairs: "0123ABCD" -> "01 23 AB CD" (a last single digit stays alone)
CString SpacedHex(const CString &digits)
{
CString r;
for (int i = 0; i < digits.GetLength(); i++)
{
if (i > 0 && (i % 2) == 0)
r += ' ';
r += digits[i];
}
return r;
}
CString OffsetCue(DWORD offset)
{
CString s;
s.Format(TEXT("%08X"), offset);
return SpacedHex(s);
}
// The box is empty until something is typed (it shows the current offset of the hex view in grey, see CCueEdit), so there is
// nothing to put into it from the value.
void DDX_MyTextAddrHex2(CDataExchange *pDX, int nIDC, DWORD &value)
{
CString s;
if (pDX->m_bSaveAndValidate)
{
DWORD temp = 0;
pDX->m_pDlgWnd->GetDlgItem(nIDC)->GetWindowText(s);
s.MakeLower();
for (int i = 0; i < s.GetLength(); i++)
{
TCHAR c = ((LPCTSTR)s)[i];
if (c == ' ')
continue; // the digits are shown in pairs
if (c >= '0' && c <= '9')
{
temp = (temp << 4) + (c - '0');
}
else if (c >= 'a' && c <= 'f')
{
temp = (temp << 4) + (c - 'a' + 10);
}
else
{
pDX->m_pDlgWnd->GetDlgItem(nIDC)->SetWindowText(TEXT(""));
return;
}
}
value=temp;
}
}
BEGIN_MESSAGE_MAP(CCueEdit, CEdit)
ON_WM_PAINT()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
void CCueEdit::SetCue(const CString &s)
{
if (s == m_Cue)
return;
m_Cue = s;
if (m_hWnd != NULL)
Invalidate();
}
void CCueEdit::OnPaint()
{
Default(); // the box as it is
if (m_Cue.IsEmpty() || GetWindowTextLength() > 0 || ::GetFocus() == m_hWnd)
return;
CClientDC dc(this);
CRect rc;
GetRect(&rc);
CFont *pOld = dc.SelectObject(GetFont());
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(DarkMode::Enabled() ? DarkMode::DisabledTextColor() : ::GetSysColor(COLOR_GRAYTEXT));
dc.IntersectClipRect(rc);
dc.DrawText(m_Cue, &rc, DT_SINGLELINE | DT_LEFT | DT_TOP | DT_NOPREFIX);
dc.SelectObject(pOld);
}
void CCueEdit::OnSetFocus(CWnd *pOld)
{
CEdit::OnSetFocus(pOld);
Invalidate();
}
void CCueEdit::OnKillFocus(CWnd *pNew)
{
CEdit::OnKillFocus(pNew);
Invalidate();
}
IMPLEMENT_DYNAMIC(CLayoutDialog, CDarkDialog)
CLayoutDialog::CLayoutDialog(CWnd* pParent /*=NULL*/)
: CDarkDialog(CLayoutDialog::IDD, pParent)
, m_pFirmware(NULL)
, m_ResourceType(0)
, m_CmdFunction(0)
, m_ClickedCol(-1), m_bLinkClick(false), m_bValBigEndian(false)
, m_RequestedMode(0)
, m_UsedShown(false)
, m_listTop0(0)
, m_curTree(0)
, m_LastCmdMode(3)
, m_bNoRestore(false)
, m_bListsBuilt(false)
, m_bModesReady(false)
, m_FindString(_T(""))
, m_ChangeString(_T(""))
, m_HexAddr(0)
, m_bSFilter(FALSE)
, m_hexBottom(0)
, m_selectorColor1(0)
, m_selectorColor2(0)
, m_selectorCtxValid(FALSE)
, m_paneTop(0)
, m_paneHeight(0)
, m_paneHeightMin(0)
, m_splitterGap(0)
, m_dragging(false)
, m_dragStartY(0)
, m_dragStartPaneHeight(0)
, m_treeLeft(0)
, m_treeWidth(0)
, m_colGap(0)
, m_listRightMargin(0)
, m_draggingCol(false)
, m_dragStartX(0)
, m_dragStartTreeWidth(0)
{
memset(m_selectorCtxPre, 0, sizeof(m_selectorCtxPre));
memset(m_selectorCtxPost, 0, sizeof(m_selectorCtxPost));
memset(m_hTree, 0, sizeof(m_hTree));
memset(m_treeBuilt, 0, sizeof(m_treeBuilt));
}
CLayoutDialog::~CLayoutDialog()
{
}
void CLayoutDialog::DoDataExchange(CDataExchange* pDX)
{
CDarkDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_RESOURCE_LIST, m_ResourceList);
DDX_Control(pDX, IDC_RESOURCE_TREE, m_ResourceTree);
DDX_Control(pDX, IDC_HEX, m_HexWnd);
DDX_Control(pDX, IDC_LANGIDX_COMBO, m_ResourceTypeIdx);
DDX_Text(pDX, IDC_FIND_EDIT, m_FindString);
DDX_Text(pDX, IDC_CHANGE_EDIT, m_ChangeString);
DDX_MyTextAddrHex2(pDX, IDC_ADDR_EDIT, m_HexAddr);
}
BEGIN_MESSAGE_MAP(CLayoutDialog, CDarkDialog)
ON_BN_CLICKED(IDC_FIND_BUTTON, OnBnClickedFindButton)
ON_EN_CHANGE(IDC_FIND_EDIT, OnEnChangeFindEdit)
ON_BN_CLICKED(IDC_FINDNEXT_BUTTON, OnBnClickedFindnextButton)
ON_BN_CLICKED(IDC_CHANGETEXT_BUTTON, OnBnClickedChangeTextButton)
ON_EN_CHANGE(IDC_CHANGE_EDIT, OnEnChangeChangeEdit)
ON_BN_CLICKED(IDC_SAVECHANGES_BUTTON, OnBnClickedSaveResourceButton)
ON_BN_CLICKED(IDC_LOADCHANGES_BUTTON, OnBnClickedLoadChangesButton)
ON_BN_CLICKED(IDC_JUMP_BUTTON, OnBnClickedJumpToButton)
ON_WM_TIMER()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_RADIO_STRINGVAL, OnBnClickedValueKind)
ON_BN_CLICKED(IDC_RADIO_HEXVAL, OnBnClickedValueKind)
ON_BN_CLICKED(IDC_RADIO_DECVAL, OnBnClickedValueKind)
ON_EN_CHANGE(IDC_ADDR_EDIT, OnEnChangeAddrEdit)
ON_CBN_SELCHANGE(IDC_LANGIDX_COMBO, OnCbnSelchangeResourceTypeidxCombo)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_RESOURCE_LIST, OnLvnItemchangedResourceList)
ON_NOTIFY(NM_DBLCLK, IDC_RESOURCE_LIST, OnLvnItemDblClickedResourceList)
ON_NOTIFY(NM_CLICK, IDC_RESOURCE_LIST, OnLvnItemClickedResourceList)
//ON_NOTIFY(LVN_ITEMACTIVATE, IDC_RESOURCE_LIST, OnLvnItemchangedResourceList)
ON_NOTIFY(TVN_SELCHANGED, IDC_RESOURCE_TREE, &CLayoutDialog::OnTvnSelchangedResourceTree)
ON_NOTIFY(TVN_ITEMEXPANDING, IDC_RESOURCE_TREE, &CLayoutDialog::OnTvnItemexpandingResourceTree)
ON_NOTIFY(TCN_SELCHANGE, IDC_CMD_SUBTABS, &CLayoutDialog::OnTcnSelchangeCmdTabs)
ON_NOTIFY(NM_RCLICK, IDC_RESOURCE_LIST, &CLayoutDialog::OnNMRClickResourceList)
ON_NOTIFY(NM_RCLICK, IDC_RESOURCE_TREE, &CLayoutDialog::OnNMRClickResourceTree)
ON_BN_CLICKED(IDC_SAVEALLRESOURCES_BUTTON, &CLayoutDialog::OnBnClickedSaveallresourcesButton)
ON_BN_CLICKED(IDC_LOADALLRESOURCES_BUTTON, &CLayoutDialog::OnBnClickedLoadallresourcesButton)
ON_BN_CLICKED(IDC_SELECTORBAR_BUTTON, &CLayoutDialog::OnBnClickedSelectorBarButton)
#ifdef IPW_TESTHOOK
ON_MESSAGE(WM_USER + 777, OnTestHook)
#endif
ON_WM_SIZE()
ON_WM_SETCURSOR()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_CBN_SELENDOK(IDC_USEDBY_COMBO, OnCbnSelendokUsedCombo)
ON_MESSAGE(WM_USER + 778, OnListPainted)
END_MESSAGE_MAP()
// CLayoutDialog message handlers
static bool s_bTreeBuilding = false; // the tree is being filled or emptied: its selection changes are not acted on
BOOL CLayoutDialog::OnInitDialog()
{
CDarkDialog::OnInitDialog();
// the tip of the two Load buttons; a button that is off gets no mouse messages, so PreTranslateMessage hands them to the tip
m_LoadTip.Create(this, TTS_ALWAYSTIP);
m_LoadTip.SetMaxTipWidth(300);
m_LoadTip.AddTool(GetDlgItem(IDC_LOADCHANGES_BUTTON), _T("Old resource saving is enabled."));
m_LoadTip.AddTool(GetDlgItem(IDC_LOADALLRESOURCES_BUTTON), _T("Old resource saving is enabled."));
m_LoadTip.Activate(FALSE);
// the scroll bars of the tree and the list look like the ones of the font viewers (arrow buttons), where they are and used the same
m_ChangeEdit.SubclassDlgItem(IDC_CHANGE_EDIT, this);
m_AddrEdit.SubclassDlgItem(IDC_ADDR_EDIT, this);
m_AddrCueShown = 0xFFFFFFFF;
SetTimer(IDT_ADDRCUE, 150, NULL); // the hex view moves its cursor by itself: the hint of Hex GoTo follows it
DarkMode::UseClassicScrollBars(m_ResourceTree.m_hWnd);
DarkMode::UseClassicScrollBars(m_ResourceList.m_hWnd);
// TODO: Add extra initialization here
reinterpret_cast<CEdit*>(GetDlgItem(IDC_ADDR_EDIT))->LimitText(11); // eight digits and the three spaces between the pairs
m_ResourceList.SetExtendedStyle(m_ResourceList.GetExtendedStyle()|LVS_EX_FULLROWSELECT);
this->CheckDlgButton(IDC_RADIO_DECVAL, 1);
CRect clientRect, treeRect, hexRect;
GetClientRect(&clientRect);
m_ResourceTree.GetWindowRect(&treeRect);
ScreenToClient(&treeRect);
m_HexWnd.GetWindowRect(&hexRect);
ScreenToClient(&hexRect);
m_paneTop = treeRect.top;
m_paneHeight = treeRect.Height();
m_paneHeightMin = treeRect.Height();
m_splitterGap = hexRect.top - treeRect.bottom;
m_hexBottom = clientRect.Height() - hexRect.bottom;
CRect listRect0;
m_ResourceList.GetWindowRect(&listRect0);
ScreenToClient(&listRect0);
m_treeLeft = treeRect.left;
m_treeWidth = treeRect.Width();
m_colGap = listRect0.left - treeRect.right;
m_listRightMargin = clientRect.Width() - listRect0.right;
m_Stretch.Init(this);
m_listTop0 = listRect0.top;
// one tree for each mode, made once; m_ResourceTree stands for the one that is shown (ActivateTree)
for (int i = 0; i < 4; i++)
{
m_hTree[i] = NULL;
m_treeBuilt[i] = false;
}
m_hTree[0] = m_ResourceTree.UnsubclassWindow();
for (int i = 1; i < 4; i++)
{
m_hTree[i] = ::CreateWindowEx(::GetWindowLong(m_hTree[0], GWL_EXSTYLE), WC_TREEVIEW, _T(""),
(::GetWindowLong(m_hTree[0], GWL_STYLE) & ~WS_VISIBLE) | WS_CHILD,
treeRect.left, treeRect.top, treeRect.Width(), treeRect.Height(),
m_hWnd, (HMENU)(UINT_PTR)IDC_RESOURCE_TREE, AfxGetInstanceHandle(), NULL);
::SendMessage(m_hTree[i], WM_SETFONT, ::SendMessage(m_hTree[0], WM_GETFONT, 0, 0), TRUE);
DarkMode::ApplyToWindow(m_hTree[i]);
DarkMode::UseClassicScrollBars(m_hTree[i]); // (the first tree is done above; these are made later and nothing styles them otherwise)
}
m_ResourceTree.SubclassWindow(m_hTree[0]);
m_curTree = 0;
// "Table" and "Instructions": the two ways the commands are shown, above the list at the right
m_CmdTabs.Create(WS_CHILD | WS_TABSTOP, CRect(0, 0, 100, 22), this, IDC_CMD_SUBTABS);
m_CmdTabs.SetFont(GetFont());
m_CmdTabs.InsertItem(0, _T("Table"));
m_CmdTabs.InsertItem(1, _T("Instructions"));
DarkMode::ApplyToWindow(m_CmdTabs.m_hWnd);
// the drop-down of the places a font type is used in; it sits over the cell and is only shown for font types
m_UsedCombo.Create(CBS_DROPDOWNLIST | WS_CHILD | WS_VSCROLL | WS_TABSTOP, CRect(0, 0, 100, 200), &m_ResourceList, IDC_USEDBY_COMBO);
// the drop-down is a child of the list itself: the list does not draw over its own children, and the drop-down needs
// no pushing back on top after every draw
m_ResourceList.ModifyStyle(0, WS_CLIPCHILDREN);
m_UsedCombo.SetFont(GetFont());
m_UsedCombo.SetItemHeight(-1, 12);
DarkMode::ApplyToWindow(m_UsedCombo.m_hWnd);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CLayoutDialog::SetFirmware(CFirmware *pFirmware)
{
m_pFirmware = pFirmware;
m_bListsBuilt = false;
m_bModesReady = false;
for (int i = 0; i < 5; i++)
m_State[i] = ModeState();
m_LastCmdMode = 3;
s_bTreeBuilding = true; // what is selected while the trees are emptied is of no interest
for (int i = 0; i < 4; i++)
{
m_treeBuilt[i] = false;
if (m_hTree[i] != NULL)
TreeView_DeleteAllItems(m_hTree[i]);
}
s_bTreeBuilding = false;
m_Names.Build(m_pFirmware);
m_Commands.Build(m_pFirmware, &m_Names);
m_Attrs.Build(m_pFirmware, &m_Names, &m_Commands);
m_CmdFunction = 0;
m_CmdLines.clear();
// update strings
m_ResourceTypeIdx.ResetContent();
CString s;
ViewList=m_pFirmware->GetViewList();
MenuList=m_pFirmware->GetMenuList();
TypeList=m_pFirmware->GetTypeList();
TCMDList=m_pFirmware->GetTCMDList();
m_ResourceTypeIdx.InsertString(0, _T("Layout"));
m_ResourceTypeIdx.InsertString(1, _T("Menus"));
m_ResourceTypeIdx.InsertString(2, _T("Font Types"));
m_ResourceTypeIdx.InsertString(3, TEXT("Command Table"));
m_ResourceTypeIdx.InsertString(4, TEXT("Command Instructions"));
m_ResourceTypeIdx.SetCurSel(m_RequestedMode);
//MessageBox(_T("S"));
if (ViewList==NULL || MenuList==NULL || TypeList==NULL)
goto disable;
if (ViewList->size()<1 || MenuList->size()<1 || TypeList->size()<1)
goto disable;
m_bModesReady = true;
if (theApp.m_LayoutRender==FALSE)
OnCbnSelchangeResourceTypeidxCombo();
else
{
m_ResourceTree.DeleteAllItems();
UpdateModeText();
}
m_HexWnd.SetFirmware(m_pFirmware);
GetDlgItem(IDC_FIND_EDIT)->EnableWindow(TRUE);
GetDlgItem(IDC_CHANGE_EDIT)->EnableWindow(TRUE);
//GetDlgItem(IDC_CHKWILD)->EnableWindow(TRUE);
GetDlgItem(IDC_LOADCHANGES_BUTTON)->EnableWindow(TRUE);
GetDlgItem(IDC_JUMP_BUTTON)->EnableWindow(TRUE);
GetDlgItem(IDC_ADDR_EDIT)->EnableWindow(TRUE);
GetDlgItem(IDC_RADIO_STRINGVAL)->EnableWindow(TRUE);
GetDlgItem(IDC_RADIO_HEXVAL)->EnableWindow(TRUE);
GetDlgItem(IDC_LANGIDX_COMBO)->EnableWindow(TRUE);
GetDlgItem(IDC_SAVEALLRESOURCES_BUTTON)->EnableWindow(TRUE);
GetDlgItem(IDC_LOADALLRESOURCES_BUTTON)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECTORBAR_BUTTON)->EnableWindow(TRUE);
UpdateLoadButtons();
UpdateData(FALSE);
return;
disable:
GetDlgItem(IDC_FIND_EDIT)->EnableWindow(FALSE);
GetDlgItem(IDC_CHANGE_EDIT)->EnableWindow(FALSE);
//GetDlgItem(IDC_CHKWILD)->EnableWindow(FALSE);
GetDlgItem(IDC_LOADCHANGES_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_JUMP_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_ADDR_EDIT)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO_STRINGVAL)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO_HEXVAL)->EnableWindow(FALSE);
GetDlgItem(IDC_LANGIDX_COMBO)->EnableWindow(FALSE);
GetDlgItem(IDC_SAVEALLRESOURCES_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_LOADALLRESOURCES_BUTTON)->EnableWindow(FALSE);
GetDlgItem(IDC_SELECTORBAR_BUTTON)->EnableWindow(FALSE);
UpdateData(FALSE);
}
// ---------------------------------------------------------------------------------------------------------
// "Change Selector Bar Color": the two colors of the gradient of the selection bar
//
// 5.5G and the later 5G builds (13.6.2.1 and up): every element of the layouts carries the two colors as its first two
// extra values (about 1650 places, found from the layouts: the places of one build are not those of another).
// 5G, the older builds (13.6.0 to 13.6.1.2): the colors are in the program code: one small function stores them as the
// immediates of six MOV instructions (mov r1,#R / strb r1,[r0] / mov r1,#G / strb r1,[r0,#1] / mov r1,#B /
// strb r1,[r0,#2], then the same with r2 for the second color). The pattern below does not depend on the colors.
// ---------------------------------------------------------------------------------------------------------
// The function of the 5G that stores the two colors, from its first MOV on (the 17 instruction words; the immediate of
// the six MOVs that hold the colors is not compared). Words 0, 2 and 4 hold the red, green and blue of the light color,
// words 9, 11 and 13 those of the dark one.
static const DWORD kSelectorCode5G[17][2] = {
{ 0xE3A01000, 0xFFFFFF00 }, { 0xE5C01000, 0xFFFFFFFF }, // mov r1,#R strb r1,[r0]
{ 0xE3A01000, 0xFFFFFF00 }, { 0xE5C01001, 0xFFFFFFFF }, // mov r1,#G strb r1,[r0,#1]
{ 0xE3A01000, 0xFFFFFF00 }, { 0xE5C01002, 0xFFFFFFFF }, // mov r1,#B strb r1,[r0,#2]
{ 0xE3A010FF, 0xFFFFFFFF }, { 0xE5C01003, 0xFFFFFFFF }, // mov r1,#255 strb r1,[r0,#3] (opacity)
{ 0xE2800004, 0xFFFFFFFF }, // add r0,r0,#4
{ 0xE3A02000, 0xFFFFFF00 }, { 0xE5C02000, 0xFFFFFFFF }, // mov r2,#R strb r2,[r0]
{ 0xE3A02000, 0xFFFFFF00 }, { 0xE5C02001, 0xFFFFFFFF }, // mov r2,#G strb r2,[r0,#1]
{ 0xE3A02000, 0xFFFFFF00 }, { 0xE5C02002, 0xFFFFFFFF }, // mov r2,#B strb r2,[r0,#2]
{ 0xE5C01003, 0xFFFFFFFF }, // strb r1,[r0,#3]
{ 0xE12FFF1E, 0xFFFFFFFF }, // bx lr
};
static void FindSelectorCode5G(const BYTE *buf, DWORD sz, vector<DWORD> &hits)
{
for (DWORD i = 0; i + 17 * 4 <= sz; i += 4)
{
const DWORD *w = (const DWORD *)&buf[i];
if ((w[0] & 0xFFFFFF00) != 0xE3A01000)
continue;
bool ok = true;
for (int k = 1; k < 17 && ok; k++)
ok = (w[k] & kSelectorCode5G[k][1]) == kSelectorCode5G[k][0];
if (ok)
hits.push_back(i);
}
}
// The places of the two colors in a 5.5G firmware: the first two extra values of every layout element that has them
void CLayoutDialog::FindSelectorSlots(vector<DWORD> &slots)
{
if (ViewList == NULL || m_pFirmware == NULL)
return;
const BYTE *buf = m_pFirmware->GetFirmwareBuffer();
for (size_t v = 0; v < ViewList->size(); v++)
{
ViewRecord *pv = ViewList->at(v);
for (size_t k = 0; k < pv->ViewElemList.size(); k++)
{
const resource_type_view_elem *e = pv->ViewElemList[k]->element;
CLayoutAttrs::Extra x;
CLayoutAttrs::ExtraOf(e, &x);
if (x.gradient && x.p[0] != x.p[1])
slots.push_back((DWORD)((const BYTE *)e - buf) + 76);
}
}
}
// The notice of the tweak (with its credit) has the choice of the iPod on it: 5G, 5.5G or Cancel
static INT_PTR CALLBACK SelectorBarDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_SELBAR_5G:
case IDC_SELBAR_55G:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
void CLayoutDialog::OnBnClickedSelectorBarButton()
{
INT_PTR which = DialogBox(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD_SELECTOR_BAR), m_hWnd, SelectorBarDlgProc);
if (which == IDC_SELBAR_5G)
ChangeSelectorBar5G();
else if (which == IDC_SELBAR_55G)
ChangeSelectorBar55G();
}
void CLayoutDialog::ChangeSelectorBar5G()
{
LPBYTE buf = m_pFirmware->GetFirmwareBuffer();
DWORD sz = m_pFirmware->GetFirmwareSize();
// the 5G and the 5.5G are the family 0xB of the images; the photo (0x6) and the nano (0xC) are not supported yet
if ((m_pFirmware->GetFirmwareVersion() >> 12) != 0xB)
{
MessageBox(_T("This is not a 5G firmware. Only the 5G and the 5.5G are supported."), _T("Selector Bar Color Changer"));
return;
}
// the later 5G builds (13.6.2.1, 13.6.3) keep the colors in the layouts, like the 5.5G
vector<DWORD> slots;
FindSelectorSlots(slots);
if (!slots.empty())
{
ChangeSelectorBar55G();
return;
}
vector<DWORD> hits;
FindSelectorCode5G(buf, sz, hits);
if (hits.empty())
{
MessageBox(_T("Selector bar gradient not found. Is this a 5G firmware?"), _T("Selector Bar Color Changer"));
return;
}
// the colors the first function holds (COLORREF: red in the lowest byte)
static const int kLight[3] = { 0, 2, 4 };
static const int kDark[3] = { 9, 11, 13 };
const DWORD *w = (const DWORD *)&buf[hits[0]];
unsigned int light = RGB(w[kLight[0]] & 0xFF, w[kLight[1]] & 0xFF, w[kLight[2]] & 0xFF);
unsigned int dark = RGB(w[kDark[0]] & 0xFF, w[kDark[1]] & 0xFF, w[kDark[2]] & 0xFF);
// the dark color is asked first, as for the 5.5G, where the gradient starts with it
bool changed = false;
unsigned int ret_color;
if (GetUserColor(dark, &ret_color, GetParent()->GetParent()->m_hWnd) == true)
{
dark = ret_color & 0x00FFFFFF;
changed = true;
}
if (GetUserColor(light, &ret_color, GetParent()->GetParent()->m_hWnd) == true)
{
light = ret_color & 0x00FFFFFF;
changed = true;
}
if (!changed)
return;
// only the immediates change; the opacity stays
for (size_t h = 0; h < hits.size(); h++)
for (int k = 0; k < 6; k++)
{
int idx = k < 3 ? kLight[k] : kDark[k - 3];
unsigned int color = k < 3 ? light : dark;
DWORD at = hits[h] + 4 * idx;
DWORD word = (*(const DWORD *)&buf[at] & 0xFFFFFF00) | ((color >> (8 * (k % 3))) & 0xFF);
m_pFirmware->SetData(at, (LPBYTE)&word, 4);
}
m_selectorColor1 = dark | 0xFF000000;
m_selectorColor2 = light | 0xFF000000;
m_HexWnd.Invalidate();
CString msg;
msg.Format(_T("Selector bar color updated at %d location(s).\n\nWrite the firmware to the iPod to apply the change."), (int)hits.size());
MessageBox(msg, _T("Selector Bar Color Changer"));
}
void CLayoutDialog::ChangeSelectorBar55G()
{
LPBYTE buf = m_pFirmware->GetFirmwareBuffer();
DWORD sz = m_pFirmware->GetFirmwareSize();
vector<DWORD> slots;
FindSelectorSlots(slots);
if (slots.empty())
{
vector<DWORD> code;
FindSelectorCode5G(buf, sz, code);
if ((m_pFirmware->GetFirmwareVersion() >> 12) == 0xB && !code.empty())
MessageBox(_T("This firmware keeps the selector bar colors in its program code. Choose 5G."), _T("Selector Bar Color Changer"));
else
MessageBox(_T("Selector bar gradient not found. Is this a 5.5G firmware?"), _T("Selector Bar Color Changer"));
return;
}
// Read the current gradient colours from the first element that has them.
unsigned int color1 = *(unsigned int *)&buf[slots[0]];
unsigned int color2 = *(unsigned int *)&buf[slots[0] + 4];
const unsigned int old1 = color1, old2 = color2;
bool changed = false;
unsigned int ret_color;
// Mask out alpha byte before passing to ChooseColor — COLORREF must be <= 0x00FFFFFF
if (GetUserColor(color1 & 0x00FFFFFF, &ret_color, GetParent()->GetParent()->m_hWnd) == true)
{
color1 = ret_color | 0xFF000000;
changed = true;
}
if (GetUserColor(color2 & 0x00FFFFFF, &ret_color, GetParent()->GetParent()->m_hWnd) == true)
{
color2 = ret_color | 0xFF000000;
changed = true;
}
if (!changed)
return;
BYTE newPair[8];
*(DWORD *)&newPair[0] = color1;
*(DWORD *)&newPair[4] = color2;
// Write the new colours to every place that holds the colours that were read (an element with a gradient of
// its own keeps it).
int count = 0;
for (size_t k = 0; k < slots.size(); k++)
{
DWORD off = slots[k];
if (off + 7 < sz && *(unsigned int *)&buf[off] == old1 && *(unsigned int *)&buf[off + 4] == old2)
{
memcpy(&buf[off], newPair, 8);
count++;
}
}
m_selectorColor1 = color1;
m_selectorColor2 = color2;
CString msg;
msg.Format(_T("Selector bar color updated at %d location(s).\n\nWrite the firmware to the iPod to apply the change."), count);
MessageBox(msg, _T("Selector Bar Color Changer"));
}
void CLayoutDialog::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == IDT_ADDRCUE)
{
if (m_pFirmware != NULL && m_HexWnd.m_hWnd != NULL && m_HexWnd.m_CursorPos != m_AddrCueShown)
{
m_AddrCueShown = m_HexWnd.m_CursorPos;
m_AddrEdit.SetCue(OffsetCue(m_AddrCueShown));
}
return;
}
CDarkDialog::OnTimer(nIDEvent);
}
void CLayoutDialog::OnDestroy()
{
KillTimer(IDT_ADDRCUE);
CDarkDialog::OnDestroy();
}
void CLayoutDialog::OnBnClickedJumpToButton()
{
if (GetDlgItem(IDC_ADDR_EDIT)->GetWindowTextLength() == 0)
return; // nothing typed: the box only shows the current offset
if (m_HexAddr<=m_pFirmware->GetFirmwareSize())
m_HexWnd.SetOffset(m_HexAddr);
}
static const int LAY_SPLITTER_HIT = 5;
static const int LAY_PANE_MIN = 20;
int CLayoutDialog::EffectivePaneHeight()
{
CRect clientRect;
GetClientRect(&clientRect);
int maxPaneHeight = clientRect.Height() - m_paneTop - m_splitterGap - LAY_PANE_MIN - m_hexBottom;
int paneH = m_paneHeight;
if (paneH < m_paneHeightMin) paneH = m_paneHeightMin;
if (paneH > maxPaneHeight) paneH = maxPaneHeight;
if (paneH < m_paneHeightMin) paneH = m_paneHeightMin;
return paneH;
}
void CLayoutDialog::LayoutSplitter()
{
if (m_paneHeight == 0) return;
if (!IsWindow(m_ResourceTree.GetSafeHwnd())) return;
if (!IsWindow(m_ResourceList.GetSafeHwnd())) return;
if (!IsWindow(m_HexWnd.GetSafeHwnd())) return;
CRect clientRect, treeRect, listRect, hexRect;
GetClientRect(&clientRect);
// the hex view and the two boxes Find and Change Value take the extra width, and the buttons beside the boxes go with their
// right end; the buttons of the top row stay at the left; the tree and the list are placed below
static const int stretchIds[] = { IDC_HEX, IDC_FIND_EDIT, IDC_CHANGE_EDIT };
static const int skipIds[] = { IDC_RESOURCE_TREE, IDC_RESOURCE_LIST };
static const int moveIds[] = { IDC_FIND_BUTTON, IDC_FINDNEXT_BUTTON, IDC_CHKWILD, IDC_CHANGETEXT_BUTTON,
IDC_RADIO_STRINGVAL, IDC_RADIO_HEXVAL, IDC_RADIO_DECVAL };
m_Stretch.Apply(this, StretchExtraWidth(), stretchIds, 3, skipIds, 2, false, moveIds, 7);
m_ResourceTree.GetWindowRect(&treeRect); ScreenToClient(&treeRect);
m_ResourceList.GetWindowRect(&listRect); ScreenToClient(&listRect);
m_HexWnd.GetWindowRect(&hexRect); ScreenToClient(&hexRect);
int paneH = EffectivePaneHeight();
int treeW = EffectiveTreeWidth();
int listLeft = m_treeLeft + treeW + m_colGap;
int listWidth = PageWidth() - m_listRightMargin - listLeft;
if (listWidth < 20) listWidth = 20;
for (int i = 0; i < 4; i++)
if (m_hTree[i] != NULL)
::SetWindowPos(m_hTree[i], NULL, m_treeLeft, treeRect.top, treeW, paneH, SWP_NOZORDER | SWP_NOACTIVATE);
const int tabH = IsCommandMode() ? 22 : 0;
m_ResourceList.SetWindowPos(NULL, listLeft, m_listTop0 + tabH,
listWidth, paneH - tabH,
SWP_NOZORDER | SWP_NOACTIVATE);
if (m_CmdTabs.m_hWnd != NULL)
m_CmdTabs.SetWindowPos(NULL, listLeft, m_listTop0, listWidth, 22, SWP_NOZORDER | SWP_NOACTIVATE);
int hexTop = m_paneTop + paneH + m_splitterGap;
int hexHeight = clientRect.Height() - hexTop - m_hexBottom;
if (hexHeight < LAY_PANE_MIN) hexHeight = LAY_PANE_MIN;
m_HexWnd.SetWindowPos(NULL, hexRect.left, hexTop,
hexRect.Width(), hexHeight,
SWP_NOZORDER | SWP_NOACTIVATE);
}
void CLayoutDialog::OnSize(UINT nType, int cx, int cy)
{
CDarkDialog::OnSize(nType, cx, cy);
if (nType == SIZE_MINIMIZED) return;
LayoutSplitter();
}
static const int LAY_COLUMN_MIN = 80;
// The width the page is laid out for: its template width plus how much wider the main window is than its minimum
int CLayoutDialog::PageWidth()
{
return m_Stretch.BaseWidth() + StretchExtraWidth();
}
int CLayoutDialog::EffectiveTreeWidth()
{
int maxW = PageWidth() - m_listRightMargin - m_treeLeft - m_colGap - LAY_COLUMN_MIN;
int w = m_treeWidth;
if (w > maxW) w = maxW;
if (w < LAY_COLUMN_MIN) w = LAY_COLUMN_MIN;
return w;
}
// Is the point on the narrow gap between the tree and the list?
bool CLayoutDialog::OnColumnSplitter(CPoint pt)
{
if (m_paneHeight <= 0)
return false;
int mid = m_treeLeft + EffectiveTreeWidth() + m_colGap / 2;
return abs(pt.x - mid) <= LAY_SPLITTER_HIT && pt.y >= m_paneTop && pt.y <= m_paneTop + EffectivePaneHeight();
}
BOOL CLayoutDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_paneHeight > 0)
{
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
if (OnColumnSplitter(pt))
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEWE));
return TRUE;
}
int splitterMid = m_paneTop + EffectivePaneHeight() + m_splitterGap / 2;
if (abs(pt.y - splitterMid) <= LAY_SPLITTER_HIT)
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENS));
return TRUE;
}
}
return CDarkDialog::OnSetCursor(pWnd, nHitTest, message);
}
void CLayoutDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
if (OnColumnSplitter(point))
{
m_draggingCol = true;
m_dragStartX = point.x;
m_dragStartTreeWidth = EffectiveTreeWidth();
SetCapture();
return;
}
if (m_paneHeight > 0)
{
int effH = EffectivePaneHeight();
int splitterMid = m_paneTop + effH + m_splitterGap / 2;
if (abs(point.y - splitterMid) <= LAY_SPLITTER_HIT)
{
m_dragging = true;
m_dragStartY = point.y;
m_dragStartPaneHeight = effH;
SetCapture();
return;
}
}
CDarkDialog::OnLButtonDown(nFlags, point);
}
void CLayoutDialog::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_draggingCol)
{
m_treeWidth = m_dragStartTreeWidth + (point.x - m_dragStartX);
m_treeWidth = EffectiveTreeWidth(); // within the limits
LayoutSplitter();
return;
}
if (m_dragging)
{
CRect clientRect;
GetClientRect(&clientRect);
int delta = point.y - m_dragStartY;
int newHeight = m_dragStartPaneHeight + delta;
int maxHeight = clientRect.Height() - m_paneTop - m_splitterGap - LAY_PANE_MIN - m_hexBottom;
if (newHeight < m_paneHeightMin) newHeight = m_paneHeightMin;
if (newHeight > maxHeight) newHeight = maxHeight;
m_paneHeight = newHeight;
LayoutSplitter();
return;
}
CDarkDialog::OnMouseMove(nFlags, point);
}
void CLayoutDialog::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_draggingCol)
{
m_draggingCol = false;
ReleaseCapture();
return;
}
if (m_dragging)
{
m_dragging = false;
ReleaseCapture();
return;
}
CDarkDialog::OnLButtonUp(nFlags, point);
}
void CLayoutDialog::OnBnClickedFindButton()
{
if (IsCommandMode())
{
CmdFind(false);
return;
}
//FindString(m_ResourceTree.GetNextItem(m_ResourceTree.GetRootItem(), TVGN_NEXT));
bool startsearch=true;
ExpandAllViews();
FindResource(m_ResourceTree.GetRootItem(), false, NULL, &startsearch);
}
void CLayoutDialog::OnBnClickedLoadChangesButton()
{
if (m_ResourceTree.GetSelectedItem()==NULL)
return;
CString filename;
CString sID=m_ResourceTree.GetItemText(m_ResourceTree.GetSelectedItem());
CString temp;
DWORD j;
int k;
int sel_id;
bool found=false;
sel_id=_ttoi(sID.GetBuffer());
LPBYTE lpBuf;
UINT nCount;
switch (m_ResourceType)
{
case 0: //view
filename.Format(TEXT("view%d.rsrc"), sel_id);
for (j = 0; j < ViewList->size(); j++)
{
if (ViewList->at(j)->id==sel_id)
{
nCount=4;
for (k = 0; (DWORD)k < (DWORD)ViewList->at(j)->num_elems; k++)
{
nCount+=ViewList->at(j)->ViewElemList[k]->element->size+4;
}
lpBuf=(LPBYTE)ViewList->at(j)->ViewElemList[0]->element-4;
found=true;
break;
}
}
break;
case 1: //menu
filename.Format(TEXT("menu%d.rsrc"), sel_id);
for (j = 0; j < MenuList->size(); j++)
{
if (MenuList->at(j)->id==sel_id)
{
nCount=4;
for (k = 0; (DWORD)k < (DWORD)MenuList->at(j)->num_elems; k++)
{
nCount+=MenuList->at(j)->MenuElemList[k]->element->size+4;
}
lpBuf=(LPBYTE)MenuList->at(j)->MenuElemList[0]->element-4;
found=true;
break;
}
}
break;
case 2: //type
filename.Format(TEXT("type%d.rsrc"), sel_id);
if (theApp.m_OldResourceSaving)
found=ResourceBytes(2, sel_id, &lpBuf, &nCount); // the first font type of the ID, as in 1.3
else
{
TypeRecord *pType=SelectedType();
if (pType!=NULL)
{
nCount=sizeof(*pType->element);
lpBuf=(LPBYTE)pType->element;
found=true;
}
}
break;
case 3: //commands: the function as it is stored
case 4:
filename.Format(TEXT("tcmd%d.rsrc"), sel_id);
if (theApp.m_OldResourceSaving)
found=OldTcmdBytes(sel_id, &lpBuf, &nCount);
else
found=CmdRecord(sel_id, &lpBuf, &nCount);
break;
}
if (found==false)
{
MessageBox(_T("This is not a valid resource for saving!"));
return;
}
CFile file;
CFileDialog dlgb(TRUE, TEXT("rsrc"), 0, OFN_HIDEREADONLY, TEXT("iPod Resource Files (*.rsrc)|*.rsrc||"), this);
MO_LOAD_RESOURCES_PATH(dlgb)
if (dlgb.DoModal() != IDOK)
return;
MO_SAVE_RESOURCES_PATH(dlgb)
if (!file.Open(dlgb.GetPathName(), CFile::modeRead))
{
MessageBox(TEXT("Unable to load file!"));
return;
}
DWORD filelen=(DWORD)file.GetLength();
if (filelen!=nCount)
{
MessageBox(_T("The resource file you chose doesn't fit this resource!"));
return;
}
else
{
if (IsCommandMode())
{
if (theApp.m_OldResourceSaving)
{
// the old record of a command (see OldTcmdBytes) does not say what the function is: nothing of it can be put into the firmware
file.Close();
MessageBox(TEXT("This is an old command resource. It fits, but it is not applied: the firmware is not changed."));
return;
}
if (!CmdReadRecord(file, sel_id, lpBuf, nCount))
return;
}
else
{
file.Read(lpBuf, nCount);
file.Close();
}
MessageBox(TEXT("Loaded resource file successfully!"));
LRESULT res;
NMTREEVIEW nm;
nm.itemNew.hItem=m_ResourceTree.GetSelectedItem();
OnTvnSelchangedResourceTree((NMHDR *)&nm, &res);
m_HexWnd.Invalidate();
}
}
// The bytes of one resource, as they are in the firmware (what Save Resource writes and Load Resource reads back)
bool CLayoutDialog::ResourceBytes(int mode, int id, LPBYTE *pBuf, UINT *pLen)
{
DWORD j;
int k;
switch (mode)
{
case 0: //view
for (j = 0; j < ViewList->size(); j++)