1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2004 Forgotten and the VBA development team
4 
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 // GBPaletteView.cpp : implementation file
20 //
21 
22 #include "stdafx.h"
23 #include "vba.h"
24 #include "FileDlg.h"
25 #include "GBPaletteView.h"
26 #include "WinResUtil.h"
27 
28 #include "../System.h"
29 #include "../gb/gbGlobals.h"
30 
31 #ifdef _DEBUG
32 #define new DEBUG_NEW
33 #undef THIS_FILE
34 static char THIS_FILE[] = __FILE__;
35 #endif
36 
updatePalette()37 void GBPaletteViewControl::updatePalette()
38 {
39   if(gbRom) {
40     memcpy(palette, &gbPalette[paletteAddress], 64);
41   }
42 }
43 
44 /////////////////////////////////////////////////////////////////////////////
45 // GBPaletteView dialog
46 
47 
GBPaletteView(CWnd * pParent)48 GBPaletteView::GBPaletteView(CWnd* pParent /*=NULL*/)
49   : ResizeDlg(GBPaletteView::IDD, pParent)
50 {
51   //{{AFX_DATA_INIT(GBPaletteView)
52   // NOTE: the ClassWizard will add member initialization here
53   //}}AFX_DATA_INIT
54   autoUpdate = false;
55 }
56 
~GBPaletteView()57 GBPaletteView::~GBPaletteView()
58 {
59 }
60 
DoDataExchange(CDataExchange * pDX)61 void GBPaletteView::DoDataExchange(CDataExchange* pDX)
62 {
63   CDialog::DoDataExchange(pDX);
64   //{{AFX_DATA_MAP(GBPaletteView)
65   // NOTE: the ClassWizard will add DDX and DDV calls here
66   //}}AFX_DATA_MAP
67   DDX_Control(pDX, IDC_PALETTE_VIEW, paletteView);
68   DDX_Control(pDX, IDC_PALETTE_VIEW_OBJ, paletteViewOBJ);
69   DDX_Control(pDX, IDC_COLOR, colorControl);
70 }
71 
72 
BEGIN_MESSAGE_MAP(GBPaletteView,CDialog)73 BEGIN_MESSAGE_MAP(GBPaletteView, CDialog)
74   //{{AFX_MSG_MAP(GBPaletteView)
75   ON_BN_CLICKED(IDC_SAVE_BG, OnSaveBg)
76   ON_BN_CLICKED(IDC_SAVE_OBJ, OnSaveObj)
77   ON_BN_CLICKED(IDC_REFRESH2, OnRefresh2)
78   ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate)
79   ON_BN_CLICKED(IDC_CLOSE, OnClose)
80   //}}AFX_MSG_MAP
81   ON_MESSAGE(WM_PALINFO, OnPalInfo)
82   END_MESSAGE_MAP()
83 
84   /////////////////////////////////////////////////////////////////////////////
85 // GBPaletteView message handlers
86 
87 BOOL GBPaletteView::OnInitDialog()
88 {
89   CDialog::OnInitDialog();
90 
91   DIALOG_SIZER_START( sz )
92     DIALOG_SIZER_END()
93     SetData(sz,
94             FALSE,
95             HKEY_CURRENT_USER,
96             "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBPaletteView",
97             NULL);
98 
99   paletteView.init(32, 64, 128);
100   paletteViewOBJ.init(32, 64, 128);
101 
102   paletteView.setPaletteAddress(0);
103   paletteView.refresh();
104 
105   paletteViewOBJ.setPaletteAddress(32);
106   paletteViewOBJ.refresh();
107 
108   return TRUE;  // return TRUE unless you set the focus to a control
109                 // EXCEPTION: OCX Property Pages should return FALSE
110 }
111 
save(int which)112 void GBPaletteView::save(int which)
113 {
114   CString captureBuffer;
115 
116   if(which == 0)
117     captureBuffer = "bg.pal";
118   else
119     captureBuffer = "obj.pal";
120 
121   LPCTSTR exts[] = {".pal", ".pal", ".act" };
122 
123   CString filter = theApp.winLoadFilter(IDS_FILTER_PAL);
124   CString title = winResLoadString(IDS_SELECT_PALETTE_NAME);
125   FileDlg dlg(this,
126               captureBuffer,
127               filter,
128               1,
129               "PAL",
130               exts,
131               "",
132               title,
133               true);
134 
135   if(dlg.DoModal() == IDCANCEL) {
136     return;
137   }
138 
139   PaletteViewControl *p = NULL;
140 
141   if(which == 0)
142     p = &paletteView;
143   else
144     p = &paletteViewOBJ;
145 
146   switch(dlg.getFilterIndex()) {
147   case 0:
148   case 1:
149     p->saveMSPAL(captureBuffer);
150     break;
151   case 2:
152     p->saveJASCPAL(captureBuffer);
153     break;
154   case 3:
155     p->saveAdobe(captureBuffer);
156     break;
157   }
158 }
159 
160 
OnSaveBg()161 void GBPaletteView::OnSaveBg()
162 {
163   save(0);
164 }
165 
OnSaveObj()166 void GBPaletteView::OnSaveObj()
167 {
168   save(1);
169 }
170 
OnRefresh2()171 void GBPaletteView::OnRefresh2()
172 {
173   paletteView.refresh();
174   paletteViewOBJ.refresh();
175 }
176 
update()177 void GBPaletteView::update()
178 {
179   OnRefresh2();
180 }
181 
182 
OnAutoUpdate()183 void GBPaletteView::OnAutoUpdate()
184 {
185   autoUpdate = !autoUpdate;
186   if(autoUpdate) {
187     theApp.winAddUpdateListener(this);
188   } else {
189     theApp.winRemoveUpdateListener(this);
190   }
191 }
192 
193 
OnClose()194 void GBPaletteView::OnClose()
195 {
196   theApp.winRemoveUpdateListener(this);
197 
198   DestroyWindow();
199 }
200 
OnPalInfo(WPARAM wParam,LPARAM lParam)201 LRESULT GBPaletteView::OnPalInfo(WPARAM wParam, LPARAM lParam)
202 {
203   u16 color = (u16)wParam;
204   u32 address = (u32)lParam;
205   CString buffer;
206 
207   bool isOBJ = address >= 32;
208   address &= 31;
209 
210   buffer.Format("%d", address);
211   GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer);
212 
213   int r = (color & 0x1f);
214   int g = (color & 0x3e0) >> 5;
215   int b = (color & 0x7c00) >> 10;
216 
217   buffer.Format("%d", r);
218   GetDlgItem(IDC_R)->SetWindowText(buffer);
219 
220   buffer.Format("%d", g);
221   GetDlgItem(IDC_G)->SetWindowText(buffer);
222 
223   buffer.Format("%d", b);
224   GetDlgItem(IDC_B)->SetWindowText(buffer);
225 
226 
227   buffer.Format("0x%04X", color);
228   GetDlgItem(IDC_VALUE)->SetWindowText(buffer);
229 
230   colorControl.setColor(color);
231 
232   if(isOBJ)
233     paletteView.setSelected(-1);
234   else
235     paletteViewOBJ.setSelected(-1);
236 
237   return TRUE;
238 }
239 
PostNcDestroy()240 void GBPaletteView::PostNcDestroy()
241 {
242   delete this;
243 }
244