00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #include "camtypes.h"
00112
00113 #include "bmpalctrl.h"
00114
00115 #include "dlgmgr.h"
00116 #include "filedlgs.h"
00117 #include "dlgcol.h"
00118
00119 #include "bmpalint.h"
00120 #include "bmpprefs.h"
00121 #include "bmapprev.h"
00122 #include "grndrgn.h"
00123 #include "palmenu.h"
00124
00125 CC_IMPLEMENT_MEMDUMP(BitmapExportPaletteControl, CC_CLASS_MEMDUMP)
00126
00127 #define new CAM_DEBUG_NEW
00128
00129 BitmapExportPaletteControl::BitmapExportPaletteControl()
00130 : m_MouseOverCell(INVALID_COLOUR_VALUE )
00131 , m_SelectedCell(INVALID_COLOUR_VALUE )
00132 , m_WindowID(0)
00133 , m_nPixelSize(72000 / GRenderRegion::GetDefaultDPI())
00134 , m_nCellWidthPixels(11)
00135 , m_nCellHeightPixels(11)
00136 , m_nCellWidth(m_nPixelSize * m_nCellHeightPixels)
00137 , m_nCellHeight(m_nPixelSize * m_nCellHeightPixels)
00138 , m_nCellsPerLine(30)
00139 , m_NumberOfColoursAtLastRedraw(0)
00140 {
00141 }
00142
00143 BitmapExportPaletteControl::~BitmapExportPaletteControl()
00144 {
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 void BitmapExportPaletteControl::SetCurrentSortType(BitmapExportPaletteInterface::PaletteSortType newSortType)
00155 {
00156 m_Palette.SetPaletteSortType(newSortType);
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 BitmapExportPaletteInterface::PaletteSortType BitmapExportPaletteControl::GetCurrentSortType()
00166 {
00167 return m_Palette.GetPaletteSortType();
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 INT32 BitmapExportPaletteControl::GetCellFromPos(INT32 x, INT32 y)
00178 {
00179 INT32 cellX = x / m_nCellWidthPixels;
00180
00181 INT32 cellY = y / m_nCellHeightPixels;
00182
00183 INT32 cell = (m_nCellsPerLine * cellY) + cellX;
00184
00185 if (cell >= m_Palette.GetNumberOfColours() || cellX >= m_nCellsPerLine)
00186 return INVALID_COLOUR_VALUE;
00187 else
00188 return cell;
00189 }
00190
00191 void BitmapExportPaletteControl::MsgMouseMove(ReDrawInfoType *pInfo)
00192 {
00193 INT32 cell = GetCellFromPos(pInfo->pMousePos->x / m_nPixelSize, (pInfo->dy - pInfo->pMousePos->y) / m_nPixelSize);
00194
00195 SetHighlightedCellFromSortedIndex(pInfo, cell);
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 bool BitmapExportPaletteControl::MsgMouseLeftButtonDown(ReDrawInfoType *pInfo)
00207 {
00208 ERROR3IF(!m_WindowID, "Window ID not set");
00209
00210 INT32 cell = GetCellFromPos(pInfo->pMousePos->x / m_nPixelSize, (pInfo->dy - pInfo->pMousePos->y) / m_nPixelSize);
00211
00212 return SetSelectedCellFromSortedIndex(pInfo, cell);
00213 }
00214
00215
00216
00217
00218
00219
00220
00221 void BitmapExportPaletteControl::MsgMouseRightButtonUp(ReDrawInfoType *pInfo, BmapPrevDlg *pBmapPrevDlg)
00222 {
00223 MsgMouseLeftButtonDown(pInfo);
00224
00225 if (m_SelectedCell == INVALID_COLOUR_VALUE)
00226 return;
00227
00228
00229
00230 OpPalettePopupCommand::Init(this, pBmapPrevDlg);
00231
00232 PaletteContextMenu* Menu = new PaletteContextMenu;
00233
00234
00235 if( Menu != NULL )
00236 Menu->Show();
00237 else
00238 ERROR3( "Can't create PaletteContextMenu" );
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 void BitmapExportPaletteControl::RedrawSelectedCell(ReDrawInfoType *pInfo)
00250 {
00251 InvalidateCell(pInfo, m_SelectedCell);
00252 }
00253
00254 void BitmapExportPaletteControl::InvalidateCell(ReDrawInfoType *pInfo, INT32 paletteIndex)
00255 {
00256 DocRect cellRect;
00257 ERROR3IF(!m_WindowID, "Window ID not set");
00258
00259 GetRectForCell(paletteIndex, &cellRect, pInfo->dy);
00260
00261
00262 cellRect.lo.y -= m_nPixelSize;
00263 cellRect.hi.y += m_nPixelSize;
00264 cellRect.hi.x += m_nPixelSize * 2;
00265
00266 DialogManager::InvalidateGadget(m_WindowID, _R(IDC_T2_PALETTE_CONTROL), pInfo, &cellRect);
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 bool BitmapExportPaletteControl::SetSelectedCell(ReDrawInfoType *pInfo, INT32 paletteIndex)
00278 {
00279 return SetSelectedCellFromSortedIndex(pInfo, m_Palette.RealValueToSortedValue(paletteIndex));
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 bool BitmapExportPaletteControl::SetSelectedCellFromSortedIndex(ReDrawInfoType *pInfo, INT32 paletteIndex)
00293 {
00294 if (paletteIndex == m_SelectedCell)
00295 return false;
00296
00297
00298 if (m_SelectedCell != INVALID_COLOUR_VALUE)
00299 InvalidateCell(pInfo, m_SelectedCell);
00300
00301 m_SelectedCell = paletteIndex;
00302
00303
00304 if (m_SelectedCell == INVALID_COLOUR_VALUE)
00305 return true;
00306
00307
00308 InvalidateCell(pInfo, m_SelectedCell);
00309
00310 return true;
00311 }
00312
00313 bool BitmapExportPaletteControl::SetHighlightedCell(ReDrawInfoType *pInfo, INT32 paletteIndex)
00314 {
00315 return SetHighlightedCellFromSortedIndex(pInfo, m_Palette.RealValueToSortedValue(paletteIndex));
00316 }
00317
00318 bool BitmapExportPaletteControl::SetHighlightedCellFromSortedIndex(ReDrawInfoType *pInfo, INT32 paletteIndex)
00319 {
00320 if (paletteIndex == m_MouseOverCell)
00321 return false;
00322
00323
00324 if (m_MouseOverCell != INVALID_COLOUR_VALUE)
00325 InvalidateCell(pInfo, m_MouseOverCell);
00326
00327 m_MouseOverCell = paletteIndex;
00328
00329
00330 if (m_MouseOverCell == INVALID_COLOUR_VALUE)
00331 return true;
00332
00333
00334 InvalidateCell(pInfo, m_MouseOverCell);
00335
00336 return true;
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 void BitmapExportPaletteControl::RenderSoon()
00348 {
00349
00350 if (m_SelectedCell > m_Palette.GetNumberOfColours())
00351 m_SelectedCell = INVALID_COLOUR_VALUE;
00352 if (m_MouseOverCell > m_Palette.GetNumberOfColours())
00353 m_MouseOverCell = INVALID_COLOUR_VALUE;
00354
00355 DialogManager::InvalidateGadget(m_WindowID, _R(IDC_T2_PALETTE_CONTROL));
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365 void BitmapExportPaletteControl::Render(ReDrawInfoType *pInfo)
00366 {
00367 DocRect PaletteSize(0, 0, pInfo->dx, pInfo->dy);
00368
00369
00370 RenderRegion *pRender = CreateOSRenderRegion(&PaletteSize, pInfo);
00371 if (pRender == 0)
00372 return;
00373
00374
00375 if (!m_Palette.GetNumberOfColours())
00376 {
00377
00378 RenderGrey(&PaletteSize, pRender);
00379 }
00380 else
00381 {
00382
00383 if (m_SelectedCell > m_Palette.GetNumberOfColours())
00384 m_SelectedCell = INVALID_COLOUR_VALUE;
00385 if (m_MouseOverCell > m_Palette.GetNumberOfColours())
00386 m_MouseOverCell = INVALID_COLOUR_VALUE;
00387
00388
00389 RenderPalette(&PaletteSize, pRender, pInfo->dy, pInfo->pClipRect);
00390 }
00391
00392 DestroyOSRenderRegion(pRender);
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 void BitmapExportPaletteControl::RenderGrey(DocRect *pPaletteSize, RenderRegion *pRender)
00402 {
00403 DialogColourInfo DialogColours;
00404 pRender->SetFillColour(DialogColours.DialogBack());
00405 pRender->SetLineColour(COLOUR_TRANS);
00406 pRender->DrawRect(pPaletteSize);
00407 }
00408
00409 void BitmapExportPaletteControl::GetRectForCell(INT32 cell, DocRect *pRect, INT32 controlHeight)
00410 {
00411 pRect->lo.x = (cell % m_nCellsPerLine) * m_nCellWidth;
00412 pRect->lo.y = controlHeight - (cell / m_nCellsPerLine) * m_nCellHeight - m_nCellHeight;
00413 pRect->hi.x = (cell % m_nCellsPerLine + 1) * m_nCellWidth - m_nPixelSize;
00414 pRect->hi.y = controlHeight - (cell / m_nCellsPerLine) * m_nCellHeight - m_nPixelSize;
00415 }
00416
00417 void BitmapExportPaletteControl::RenderPalette(DocRect *pPaletteSize, RenderRegion *pRender, INT32 controlHeight,
00418 DocRect* pClipRect)
00419 {
00420 INT32 nColours = m_Palette.GetNumberOfColours();
00421
00422
00423 if (nColours < m_NumberOfColoursAtLastRedraw)
00424 RenderGrey(pPaletteSize, pRender);
00425
00426 m_NumberOfColoursAtLastRedraw = nColours;
00427
00428 pRender->SetLineColour(COLOUR_BLACK);
00429
00430 DocRect cell;
00431
00432
00433 for (INT32 i = 0; i < nColours; ++i)
00434 {
00435 GetRectForCell(i, &cell, controlHeight);
00436 if (cell.IsIntersectedWith(*pClipRect))
00437 {
00438 BYTE r, g, b;
00439 r = m_Palette.GetRed(i);
00440 g = m_Palette.GetGreen(i);
00441 b = m_Palette.GetBlue(i);
00442 DrawCell(&cell, DocColour(r, g, b), m_Palette.GetFlags(i), pRender,
00443 IsColourWebSafe(r, g, b), i == m_SelectedCell);
00444 }
00445 }
00446
00447
00448 if (m_MouseOverCell != INVALID_COLOUR_VALUE)
00449 {
00450 GetRectForCell(m_MouseOverCell, &cell, controlHeight);
00451 pRender->SetFillColour(COLOUR_TRANS);
00452 pRender->SetLineColour(COLOUR_WHITE);
00453 pRender->DrawRect(&cell);
00454 }
00455
00456 }
00457
00458 void BitmapExportPaletteControl::SetSelectedColourToggleLocked()
00459 {
00460 m_Palette.SetFlags(m_SelectedCell, m_Palette.GetFlags(m_SelectedCell) ^ LOCKED_COLOUR);
00461 if (!GetSelectedColourLocked())
00462 BmapPrevDlg::m_pExportOptions->InvalidatePalette();
00463 }
00464
00465
00466
00467
00468
00469
00470
00471
00472 void BitmapExportPaletteControl::MakeColourWebSafe(BYTE *pColour)
00473 {
00474 if (*pColour % 51 != 0)
00475 {
00476 if (*pColour % 51 > 20)
00477
00478 *pColour += (51 - *pColour % 51);
00479 else
00480
00481 *pColour -= *pColour % 51;
00482 }
00483 }
00484
00485
00486
00487
00488
00489
00490
00491 void BitmapExportPaletteControl::SetSelectedColourWebSafe()
00492 {
00493 INT32 realIndex = m_Palette.SortedValueToRealValue(m_SelectedCell);
00494
00495 ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00496 ERROR3IF(!palette, "There is no palette - This should never happen");
00497
00498 if (!GetSelectedColourLocked())
00499 SetSelectedColourToggleLocked();
00500
00501 palette->Data[realIndex].PreEditedRed = palette->Data[realIndex].Red;
00502 palette->Data[realIndex].PreEditedGreen = palette->Data[realIndex].Green;
00503 palette->Data[realIndex].PreEditedBlue = palette->Data[realIndex].Blue;
00504
00505 MakeColourWebSafe(&palette->Data[realIndex].Red);
00506 MakeColourWebSafe(&palette->Data[realIndex].Green);
00507 MakeColourWebSafe(&palette->Data[realIndex].Blue);
00508 }
00509
00510 bool BitmapExportPaletteControl::SetSelectedColourToggleTransparent()
00511 {
00512 m_Palette.SetFlags(m_SelectedCell, m_Palette.GetFlags(m_SelectedCell) ^ TRANSPARENT_COLOUR);
00513
00514 if (m_SelectedCell == 0 && ((m_Palette.GetFlags(m_SelectedCell) & TRANSPARENT_COLOUR) == 0))
00515 return true;
00516 else
00517 return false;
00518 }
00519 void BitmapExportPaletteControl::SetSelectedColourToggleDeleted()
00520 {
00521 m_Palette.SetFlags(m_SelectedCell, m_Palette.GetFlags(m_SelectedCell) ^ DELETED_COLOUR);
00522 if (GetSelectedColourDeleted())
00523 BmapPrevDlg::m_pExportOptions->MakePaletteEntryUnreadable(m_Palette.SortedValueToRealValue(m_SelectedCell));
00524 else
00525 BmapPrevDlg::m_pExportOptions->InvalidatePalette();
00526 }
00527
00528 INT32 BitmapExportPaletteControl::GetSelectedColour()
00529 {
00530 return m_Palette.SortedValueToRealValue(m_SelectedCell);
00531 }
00532
00533 INT32 BitmapExportPaletteControl::GetMouseOverColour()
00534 {
00535 return m_Palette.SortedValueToRealValue(m_MouseOverCell);
00536 }
00537
00538 bool BitmapExportPaletteControl::GetSelectedColourLocked()
00539 {
00540 return ((m_Palette.GetFlags(m_SelectedCell) & LOCKED_COLOUR) != 0);
00541 }
00542
00543 bool BitmapExportPaletteControl::IsColourWebSafe(BYTE r, BYTE g, BYTE b)
00544 {
00545 return (r % 51 == 0 && g % 51 == 0 && b % 51 == 0);
00546 }
00547
00548 bool BitmapExportPaletteControl::GetSelectedColourWebSafe()
00549 {
00550 return IsColourWebSafe( m_Palette.GetRed(m_SelectedCell),
00551 m_Palette.GetGreen(m_SelectedCell),
00552 m_Palette.GetBlue(m_SelectedCell));
00553 }
00554
00555 bool BitmapExportPaletteControl::GetSelectedColourTransparent()
00556 {
00557 return ((m_Palette.GetFlags(m_SelectedCell) & TRANSPARENT_COLOUR) != 0);
00558 }
00559
00560 bool BitmapExportPaletteControl::GetSelectedColourDeleted()
00561 {
00562 return ((m_Palette.GetFlags(m_SelectedCell) & DELETED_COLOUR) != 0);
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578 void BitmapExportPaletteControl::DrawCell(DocRect *pCellRect, DocColour colour, INT32 flags, RenderRegion *pRender,
00579 bool webSafe, bool selected)
00580 {
00581 pRender->SetLineColour(COLOUR_BLACK);
00582 pRender->SetFillColour(colour);
00583 pRender->DrawRect(pCellRect);
00584
00585 if (selected)
00586 {
00587 pRender->SetFillColour(COLOUR_TRANS);
00588
00589 DocRect cell = *pCellRect;
00590
00591
00592 pRender->SetLineColour(COLOUR_WHITE);
00593 cell.Inflate(-1 * m_nPixelSize);
00594 pRender->DrawRect(&cell);
00595
00596
00597 pRender->SetLineColour(COLOUR_BLACK);
00598 cell.Inflate(-1 * m_nPixelSize);
00599 pRender->DrawRect(&cell);
00600 }
00601
00602 if (flags & DELETED_COLOUR)
00603 {
00604
00605
00606
00607 pRender->SetLineColour(COLOUR_BLACK);
00608 pRender->DrawLine( DocCoord(pCellRect->lo.x, pCellRect->hi.y + m_nPixelSize),
00609 DocCoord(pCellRect->hi.x + m_nPixelSize, pCellRect->lo.y));
00610
00611
00612 pRender->SetLineColour(COLOUR_WHITE);
00613 pRender->DrawLine( DocCoord(pCellRect->hi.x + m_nPixelSize, pCellRect->hi.y + m_nPixelSize),
00614 DocCoord(pCellRect->lo.x + m_nPixelSize, pCellRect->lo.y + m_nPixelSize));
00615
00616 return;
00617 }
00618
00619 if (flags & LOCKED_COLOUR)
00620 {
00621
00622
00623
00624 pRender->SetLineColour(COLOUR_BLACK);
00625 pRender->SetFillColour(COLOUR_WHITE);
00626
00627
00628 DocRect markRect;
00629 markRect.hi.x = pCellRect->lo.x + m_nPixelSize * 2;
00630 markRect.lo.x = pCellRect->lo.x;
00631 markRect.hi.y = pCellRect->lo.y + m_nPixelSize * 2;
00632 markRect.lo.y = pCellRect->lo.y;
00633 pRender->DrawRect(&markRect);
00634 }
00635
00636 if (flags & TRANSPARENT_COLOUR)
00637 {
00638
00639
00640 pRender->SetLineColour(COLOUR_BLACK);
00641 pRender->SetFillColour(COLOUR_WHITE);
00642
00643
00644 DocRect markRect;
00645 markRect.hi.x = pCellRect->lo.x + m_nPixelSize * 2;
00646 markRect.lo.x = pCellRect->lo.x;
00647 markRect.hi.y = pCellRect->hi.y;
00648 markRect.lo.y = pCellRect->hi.y - m_nPixelSize * 2;
00649 pRender->DrawRect(&markRect);
00650 }
00651
00652 if (webSafe)
00653 {
00654
00655
00656 pRender->SetLineColour(COLOUR_BLACK);
00657 pRender->SetFillColour(COLOUR_WHITE);
00658
00659 DocRect markRect = *pCellRect;
00660 markRect.Inflate(-4 * m_nPixelSize);
00661 pRender->DrawRect(&markRect);
00662 }
00663 }