1/*
2 * Copyright 2011 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Copyright 1999 M.Kawamura
6 */
7
8
9#include "CannaCommon.h"
10#include "PaletteWindow.h"
11#include "canna/mfdef.h"
12#include "PaletteIconImages.h"
13#include <Bitmap.h>
14#include <Screen.h>
15
16#include "WindowPrivate.h"
17
18PaletteWindow::PaletteWindow( BRect rect, BLooper *looper )
19	:BWindow( rect, B_EMPTY_STRING, kLeftTitledWindowLook,
20			B_FLOATING_ALL_WINDOW_FEEL,
21			B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_NOT_CLOSABLE |
22			B_AVOID_FOCUS | B_WILL_ACCEPT_FIRST_CLICK )
23{
24	cannaLooper = looper;
25
26	ResizeTo( 122, 46 );
27	BRect frame = Frame();
28	frame.OffsetTo( -1, -1 );
29	frame.bottom += 3;
30	frame.right += 3;
31	back = new BBox( frame );
32	AddChild( back );
33
34	BRect largerect( 0, 0, HexOnwidth - 1, HexOnheight - 1 );
35	BRect smallrect( 0, 0, HiraOnwidth - 1, HiraOnheight - 1);
36	int32 largebytes = HexOnbytesperpixel * HexOnwidth * HexOnheight;
37	int32 smallbytes = HiraOnbytesperpixel * HiraOnwidth * HiraOnheight;
38	color_space cspace = HexOncspace;
39	BPicture *onpict, *offpict;
40	BBitmap *smallimage, *largeimage;
41	BMessage *msg;
42//printf( "smallbytes = %d\n", smallbytes );
43	smallimage = new BBitmap( smallrect, cspace );
44	largeimage = new BBitmap( largerect, cspace );
45
46	back->MovePenTo( 0, 0 );
47
48	smallimage->SetBits( HiraOnbits, smallbytes, 0, cspace );
49	back->BeginPicture( new BPicture );
50	back->DrawBitmap( smallimage );
51	onpict = back->EndPicture();
52	smallimage->SetBits( HiraOffbits, smallbytes, 0, cspace );
53	back->BeginPicture( new BPicture );
54	back->DrawBitmap( smallimage );
55	offpict = back->EndPicture();
56	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
57	msg->AddInt32( "mode", CANNA_MODE_HenkanMode );
58	HiraButton = new BPictureButton( BRect( 4, 4, 4 + HiraOnwidth - 1, 4 + HiraOnheight - 1), "hira",
59						offpict, onpict, msg, B_TWO_STATE_BUTTON );
60	back->AddChild( HiraButton );
61
62	smallimage->SetBits( KataOnbits, smallbytes, 0, cspace );
63	back->BeginPicture( new BPicture );
64	back->DrawBitmap( smallimage );
65	onpict = back->EndPicture();
66	smallimage->SetBits( KataOffbits, smallbytes, 0, cspace );
67	back->BeginPicture( new BPicture );
68	back->DrawBitmap( smallimage );
69	offpict = back->EndPicture();
70	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
71	msg->AddInt32( "mode", CANNA_MODE_ZenKataHenkanMode );
72	KataButton = new BPictureButton( BRect( 26, 4, 26 + HiraOnwidth - 1, 4 + HiraOnheight - 1 ), "kata",
73						offpict, onpict, msg, B_TWO_STATE_BUTTON );
74	back->AddChild( KataButton );
75
76	smallimage->SetBits( ZenAlphaOnbits, smallbytes, 0, cspace );
77	back->BeginPicture( new BPicture );
78	back->DrawBitmap( smallimage );
79	onpict = back->EndPicture();
80	smallimage->SetBits( ZenAlphaOffbits, smallbytes, 0, cspace );
81	back->BeginPicture( new BPicture );
82	back->DrawBitmap( smallimage );
83	offpict = back->EndPicture();
84	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
85	msg->AddInt32( "mode", CANNA_MODE_ZenAlphaHenkanMode );
86	ZenAlphaButton = new BPictureButton( BRect( 48, 4, 48 + HiraOnwidth - 1, 4 + HiraOnheight - 1 ), "zenalpha",
87						offpict, onpict, msg, B_TWO_STATE_BUTTON );
88	back->AddChild( ZenAlphaButton );
89
90	smallimage->SetBits( HanAlphaOnbits, smallbytes, 0, cspace );
91	back->BeginPicture( new BPicture );
92	back->DrawBitmap( smallimage );
93	onpict = back->EndPicture();
94	smallimage->SetBits( HanAlphaOffbits, smallbytes, 0, cspace );
95	back->BeginPicture( new BPicture );
96	back->DrawBitmap( smallimage );
97	offpict = back->EndPicture();
98	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
99	msg->AddInt32( "mode", CANNA_MODE_HanAlphaHenkanMode );
100	HanAlphaButton = new BPictureButton( BRect( 70, 4, 70 + HiraOnwidth - 1, 4 + HiraOnheight - 1 ), "hanalpha",
101						offpict, onpict, msg, B_TWO_STATE_BUTTON );
102	back->AddChild( HanAlphaButton );
103
104	largeimage->SetBits( ExtendOnbits, largebytes, 0, cspace );
105	back->BeginPicture( new BPicture );
106	back->DrawBitmap( largeimage );
107	onpict = back->EndPicture();
108	largeimage->SetBits( ExtendOffbits, largebytes, 0, cspace );
109	back->BeginPicture( new BPicture );
110	back->DrawBitmap( largeimage );
111	offpict = back->EndPicture();
112	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
113	msg->AddInt32( "mode", CANNA_MODE_TourokuMode );
114	ExtendButton = new BPictureButton( BRect( 94, 4, 94 + HexOnwidth -1 , 4 + HexOnheight - 1 ), "extend",
115						offpict, onpict, msg, B_TWO_STATE_BUTTON );
116	back->AddChild( ExtendButton );
117
118	largeimage->SetBits( KigoOnbits, largebytes, 0, cspace );
119	back->BeginPicture( new BPicture );
120	back->DrawBitmap( largeimage );
121	onpict = back->EndPicture();
122	largeimage->SetBits( KigoOffbits, largebytes, 0, cspace );
123	back->BeginPicture( new BPicture );
124	back->DrawBitmap( largeimage );
125	offpict = back->EndPicture();
126	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
127	msg->AddInt32( "mode", CANNA_MODE_KigoMode );
128	KigoButton = new BPictureButton( BRect( 4, 26, 4 + HexOnwidth -1, 26 + HexOnheight - 1 ), "kigo",
129						offpict, onpict, msg, B_TWO_STATE_BUTTON );
130	back->AddChild( KigoButton );
131
132	largeimage->SetBits( HexOnbits, largebytes, 0, cspace );
133	back->BeginPicture( new BPicture );
134	back->DrawBitmap( largeimage );
135	onpict = back->EndPicture();
136	largeimage->SetBits( HexOffbits, largebytes, 0, cspace );
137	back->BeginPicture( new BPicture );
138	back->DrawBitmap( largeimage );
139	offpict = back->EndPicture();
140	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
141	msg->AddInt32( "mode", CANNA_MODE_HexMode );
142	HexButton = new BPictureButton( BRect( 34, 26, 34 + HexOnwidth -1, 26 + HexOnheight - 1 ), "hex",
143						offpict, onpict, msg, B_TWO_STATE_BUTTON );
144	back->AddChild( HexButton );
145
146	largeimage->SetBits( BushuOnbits, largebytes, 0, cspace );
147	back->BeginPicture( new BPicture );
148	back->DrawBitmap( largeimage );
149	onpict = back->EndPicture();
150	largeimage->SetBits( BushuOffbits, largebytes, 0, cspace );
151	back->BeginPicture( new BPicture );
152	back->DrawBitmap( largeimage );
153	offpict = back->EndPicture();
154	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
155	msg->AddInt32( "mode", CANNA_MODE_BushuMode );
156	BushuButton = new BPictureButton( BRect( 64, 26, 64 + HexOnwidth -1, 26 + HexOnheight - 1 ), "bushu",
157						offpict, onpict, msg, B_TWO_STATE_BUTTON );
158	back->AddChild( BushuButton );
159
160/*
161	largeimage->SetBits( TorokuOnbits, largebytes, 0, cspace );
162	back->BeginPicture( new BPicture );
163	back->DrawBitmap( largeimage );
164	onpict = back->EndPicture();
165	largeimage->SetBits( TorokuOffbits, largebytes, 0, cspace );
166	back->BeginPicture( new BPicture );
167	back->DrawBitmap( largeimage );
168	offpict = back->EndPicture();
169	msg = new BMessage( MODE_CHANGED_FROM_PALETTE );
170	msg->AddInt32( "mode", CANNA_MODE_ExtendMode );
171	TorokuButton = new BPictureButton( BRect( 87, 26, 110, 41 ), "toroku",
172						offpict, onpict, msg, B_TWO_STATE_BUTTON );
173	back->AddChild( TorokuButton );
174*/
175	HiraButton->SetValue( B_CONTROL_ON );
176	delete smallimage;
177	delete largeimage;
178	delete offpict;
179	delete onpict;
180
181}
182
183
184void PaletteWindow::MessageReceived( BMessage *msg )
185{
186	int32 mode;
187	switch( msg->what )
188	{
189		case PALETTE_WINDOW_HIDE:
190			if ( !IsHidden() )
191				Hide();
192				break;
193
194		case PALETTE_WINDOW_SHOW:
195			if ( IsHidden() )
196			{
197				BRect frame = Frame();
198				BRect screenrect = BScreen().Frame();
199				float x, y;
200
201				x = screenrect.right - frame.right;
202				y = screenrect.bottom - frame.bottom;
203
204				if ( x < 0 )
205					frame.OffsetBy( x, 0 );
206
207				if ( y < 0 )
208					frame.OffsetBy( 0, y );
209
210				MoveTo( frame.left, frame.top );
211				SetWorkspaces( B_CURRENT_WORKSPACE );
212				Show();
213			}
214			break;
215
216		case MODE_CHANGED_FROM_PALETTE:
217			cannaLooper->PostMessage( msg );
218			break;
219
220		case PALETTE_WINDOW_BUTTON_UPDATE:
221			msg->FindInt32( "mode", &mode );
222			AllButtonOff();
223//printf( "Palette:NewMode = %d\n", mode );
224			switch( mode )
225			{
226				case CANNA_MODE_ZenKataKakuteiMode:
227				case CANNA_MODE_ZenKataHenkanMode:
228					KataButton->SetValue( B_CONTROL_ON );
229					break;
230				case CANNA_MODE_ZenAlphaKakuteiMode:
231				case CANNA_MODE_ZenAlphaHenkanMode:
232					ZenAlphaButton->SetValue( B_CONTROL_ON );
233					break;
234				case CANNA_MODE_HanAlphaKakuteiMode:
235				case CANNA_MODE_HanAlphaHenkanMode:
236					HanAlphaButton->SetValue( B_CONTROL_ON );
237					break;
238				case CANNA_MODE_ExtendMode:
239				case CANNA_MODE_TourokuMode:
240				case CANNA_MODE_TourokuHinshiMode:
241				case CANNA_MODE_TourokuDicMode:
242				case CANNA_MODE_DeleteDicMode:
243				case CANNA_MODE_MountDicMode:
244					ExtendButton->SetValue( B_CONTROL_ON );
245					break;
246				case CANNA_MODE_KigoMode:
247				case CANNA_MODE_RussianMode:
248				case CANNA_MODE_GreekMode:
249				case CANNA_MODE_LineMode:
250					KigoButton->SetValue( B_CONTROL_ON );
251					break;
252				case CANNA_MODE_HexMode:
253					HexButton->SetValue( B_CONTROL_ON );
254					break;
255				case CANNA_MODE_BushuMode:
256					BushuButton->SetValue( B_CONTROL_ON );
257					break;
258/*
259				case CANNA_MODE_TourokuMode:
260				case CANNA_MODE_TourokuHinshiMode:
261				case CANNA_MODE_TourokuDicMode:
262				case CANNA_MODE_DeleteDicMode:
263				case CANNA_MODE_MountDicMode:
264					TorokuButton->SetValue( B_CONTROL_ON );
265					break;
266*/
267				default:
268					HiraButton->SetValue( B_CONTROL_ON );
269					break;
270			}
271			break;
272
273
274		default:
275			BWindow::MessageReceived( msg );
276	}
277}
278
279void PaletteWindow::AllButtonOff()
280{
281	HiraButton->SetValue( B_CONTROL_OFF );
282	KataButton->SetValue( B_CONTROL_OFF );
283	ZenAlphaButton->SetValue( B_CONTROL_OFF );
284	HanAlphaButton->SetValue( B_CONTROL_OFF );
285	ExtendButton->SetValue( B_CONTROL_OFF );
286	KigoButton->SetValue( B_CONTROL_OFF );
287	HexButton->SetValue( B_CONTROL_OFF );
288	BushuButton->SetValue( B_CONTROL_OFF );
289//	TorokuButton->SetValue( B_CONTROL_OFF );
290}
291
292void
293PaletteWindow::FrameMoved( BPoint screenPoint )
294{
295	gSettings.palette_loc = screenPoint;
296}
297
298