/*
======================================================================
toolbar.c

Ernie Wright  15 Apr 05
MSVC 4.0

Functions that manage the toolbar on the main window.

 * link with comctl32.dll
 * include commctrl.h
 * call InitCommonControls()
 * for n-bit images, BMP pal must contain 2^n entries
====================================================================== */

#include <windows.h>
#include <commctrl.h>
#include "qv.h"

#ifndef TBSTYLE_FLAT
#define TBSTYLE_FLAT 0x0800
#endif


/*
======================================================================
toolbarCreate()

Create the QV toolbar.
====================================================================== */

HWND toolbarCreate( HWND hwnd )
{
   HINSTANCE hinst;
   HWND htool;
   int i, index;

   TBBUTTON tbb[] = {
      { TOOLBAR_L,    0,          TBSTATE_ENABLED, TBSTYLE_SEP    },
      { STD_FILESAVE, IDC_SAVE,   TBSTATE_ENABLED, TBSTYLE_BUTTON },
      { STD_COPY,     IDC_COPY,   TBSTATE_ENABLED, TBSTYLE_BUTTON },
      { STD_PRINT,    IDC_PRINT,  TBSTATE_ENABLED, TBSTYLE_BUTTON },
      { STD_DELETE,   IDC_DELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON },
      { 8,            0,          TBSTATE_ENABLED, TBSTYLE_SEP    },
      { 0,            IDC_IMAGE,  TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_ALPHA,  TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_RED,    TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_GREEN,  TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_BLUE,   TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_HOT,    TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_SAFE,   TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 8,            0,          TBSTATE_ENABLED, TBSTYLE_SEP    },
      { 0,            IDC_FIT,    TBSTATE_ENABLED, TBSTYLE_BUTTON },
      { 0,            IDC_NOTES,  TBSTATE_ENABLED, TBSTYLE_BUTTON },
      { 0,            IDC_TEXT,   TBSTATE_ENABLED, TBSTYLE_CHECK  },
      { 0,            IDC_UNDO,   TBSTATE_ENABLED, TBSTYLE_BUTTON }
   };

   TBADDBITMAP tbadd[] = {
      HINST_COMMCTRL, IDB_STD_SMALL_COLOR,
      NULL,           IDB_TOOLBUTTONS
   };


   hinst = ( HINSTANCE ) GetWindowLongPtr( hwnd, GWLP_HINSTANCE );
   tbadd[ 1 ].hInst = hinst;

   /* create the toolbar */

   htool = CreateWindowEx( 0, TOOLBARCLASSNAME, NULL,
        WS_CHILD | WS_VISIBLE |
        CCS_TOP | CCS_NODIVIDER | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
        0, 0, 0, 0, hwnd, ( HMENU ) IDC_TOOLBAR, hinst, NULL );

   /* add bitmaps and buttons to the toolbar */

   SendMessage( htool, TB_BUTTONSTRUCTSIZE, ( WPARAM ) sizeof( TBBUTTON ), 0 );
   SendMessage( htool, TB_ADDBITMAP, 0, ( LPARAM ) &tbadd[ 0 ] );
   index = SendMessage( htool, TB_ADDBITMAP, 11, ( LPARAM ) &tbadd[ 1 ] );

   for ( i = 0; i < 7; i++ )
      tbb[ i + 6 ].iBitmap = index + i;
   for ( i = 7; i < 11; i++ )
      tbb[ i + 7 ].iBitmap = index + i;

   SendMessage( htool, TB_ADDBUTTONS, 18, ( LPARAM ) tbb );

   return htool;
}


/*
======================================================================
toolbarResize()

Resize the toolbar in response to a WM_SIZE received by the parent
window.  Returns the height of the toolbar.
====================================================================== */

int toolbarResize( HWND hwnd )
{
   RECT r;
   HWND htool = GetDlgItem( hwnd, IDC_TOOLBAR );
   SendMessage( htool, TB_AUTOSIZE, 0, 0 );
   GetWindowRect( htool, &r );
   return r.bottom - r.top;
}


/*
======================================================================
set_check()

Send a TB_CHECKBUTTON message to the toolbar for the control.
====================================================================== */

static void set_check( HWND hwnd, int id, int state )
{
   HWND htool;

   htool = GetDlgItem( hwnd, IDC_TOOLBAR );
   SendMessage( htool, TB_CHECKBUTTON, ( WPARAM ) id,
      state ? MAKELPARAM( 1, 0 ) : MAKELPARAM( 0, 0 ));
}


/*
======================================================================
toolbarSetChannel()

Show the channel as selected.
====================================================================== */

void toolbarSetChannel( HWND hwnd, int channel )
{
   static struct { int chan, id; } map[] = {
      { CH_IMAGE, IDC_IMAGE },
      { CH_ALPHA, IDC_ALPHA },
      { CH_RED,   IDC_RED   },
      { CH_GREEN, IDC_GREEN },
      { CH_BLUE,  IDC_BLUE  }
   };
   int i;

   for ( i = 0; i < 5; i++ )
      set_check( hwnd, map[ i ].id, map[ i ].chan == channel );
}


/*
======================================================================
toolbarSetMask()

Set the state of the Hot and Safe buttons.
====================================================================== */

void toolbarSetMask( HWND hwnd, int mask )
{
   set_check( hwnd, IDC_HOT, mask & MASK_HOT );
   set_check( hwnd, IDC_SAFE, mask & MASK_SAFE );
}


/*
======================================================================
toolbarSet()

Set the state of the channel and mask buttons.
====================================================================== */

void toolbarSet( HWND hwnd, int channel, int mask )
{
   toolbarSetChannel( hwnd, channel );
   toolbarSetMask( hwnd, mask );
}


/*
======================================================================
toolbarSetText()

Set the state of the text button.
====================================================================== */

void toolbarSetText( HWND hwnd, int state )
{
   set_check( hwnd, IDC_TEXT, state );
}


/*
======================================================================
toolbarTipText()

Respond to WM_NOTIFY messages sent to the parent when Windows needs
tooltip text for the toolbar buttons.
====================================================================== */

int toolbarTipText( LPARAM lparam )
{
   static struct { UINT id; char *text; } map[] = {
      { IDC_SAVE,   "Save As"            },
      { IDC_COPY,   "Copy to Clipboard"  },
      { IDC_PRINT,  "Print"              },
      { IDC_DELETE, "Delete"             },
      { IDC_IMAGE,  "RGB Channels"       },
      { IDC_ALPHA,  "Alpha Channel"      },
      { IDC_RED,    "Red Channel"        },
      { IDC_GREEN,  "Green Channel"      },
      { IDC_BLUE,   "Blue Channel"       },
      { IDC_HOT,    "Show Hot Pixels"    },
      { IDC_SAFE,   "Show Safe Outlines" },
      { IDC_FIT,    "Fit to Screen"      },
      { IDC_NOTES,  "Notes and Settings" },
      { IDC_TEXT,   "Add Text"           },
      { IDC_UNDO,   "Undo Text"          }
   };

   LPNMHDR nmh = ( LPNMHDR ) lparam;
   int i;

   if ( nmh->code == TTN_NEEDTEXT ) {
      LPTOOLTIPTEXT tt = ( LPTOOLTIPTEXT ) lparam;

      for ( i = 0; i < 15; i++ )
         if ( tt->hdr.idFrom == map[ i ].id ) {
            strcpy( tt->szText, map[ i ].text );
            return TRUE;
         }
   }

   return FALSE;
}


/*
======================================================================
toolbarHeight()

Return the pixel height of the toolbar.
====================================================================== */

int toolbarHeight( HWND hwnd )
{
   RECT r;
   HWND htool = GetDlgItem( hwnd, IDC_TOOLBAR );
   GetWindowRect( htool, &r );
   return r.bottom - r.top;
}
