# HG changeset patch
# Parent 0024215f6bceeb635fee5b2f55b8c9396c262559

diff -r 0024215f6bce src/Make_mvc.mak
--- a/src/Make_mvc.mak	Sun Feb 05 01:18:48 2012 +0100
+++ b/src/Make_mvc.mak	Sun Feb 05 10:15:38 2012 +0700
@@ -882,7 +882,7 @@
 !ENDIF
 
 LINKARGS1 = $(linkdebug) $(conflags)
-LINKARGS2 = $(CON_LIB) $(GUI_LIB) $(LIBC) $(OLE_LIB)  user32.lib $(SNIFF_LIB) \
+LINKARGS2 = $(CON_LIB) $(GUI_LIB) $(LIBC) $(OLE_LIB)  user32.lib uxtheme.lib comctl32.lib pnglib.lib $(SNIFF_LIB) \
 		$(LUA_LIB) $(MZSCHEME_LIB) $(PERL_LIB) $(PYTHON_LIB) $(PYTHON3_LIB) $(RUBY_LIB) \
 		$(TCL_LIB) $(NETBEANS_LIB) $(XPM_LIB) $(LINK_PDB)
 
diff -r 0024215f6bce src/gui.h
--- a/src/gui.h	Sun Feb 05 01:18:48 2012 +0100
+++ b/src/gui.h	Sun Feb 05 10:15:38 2012 +0700
@@ -161,8 +161,8 @@
 # define TOOLBAR_BUTTON_HEIGHT	15
 # define TOOLBAR_BUTTON_WIDTH	16
 #else
-# define TOOLBAR_BUTTON_HEIGHT	18
-# define TOOLBAR_BUTTON_WIDTH	18
+# define TOOLBAR_BUTTON_HEIGHT	16
+# define TOOLBAR_BUTTON_WIDTH	16
 #endif
 #define TOOLBAR_BORDER_HEIGHT	12  /* room above+below buttons for MSWindows */
 
diff -r 0024215f6bce src/gui_w32.c
--- a/src/gui_w32.c	Sun Feb 05 01:18:48 2012 +0100
+++ b/src/gui_w32.c	Sun Feb 05 10:15:38 2012 +0700
@@ -24,6 +24,9 @@
  */
 
 #include "vim.h"
+#include <commctrl.h>
+#include <uxtheme.h>
+#include "PNGLib.h"
 
 /*
  * These are new in Windows ME/XP, only defined in recent compilers.
@@ -4095,6 +4098,58 @@
 #  define TBSTYLE_FLAT		0x0800
 # endif
 
+static FARPROC s_oldtoolbarproc;
+static HTHEME s_toolbartheme;
+
+static LRESULT CALLBACK ToolbarWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
+{
+    switch (msg) {
+	case WM_ERASEBKGND: {
+	    RECT rect; // The toolbar rect
+	    RECT rc; // The separator rect
+	    RECT src; // The drawn line rect
+	    HDC hdc; // The toolbar hdc
+	    HDC shdc; // The button hdc
+	    int i, nbuttons, x;
+	    TBBUTTON tbn;
+
+	    hdc = (HDC)wParam;
+	    GetClientRect(hwnd, &rect);
+	    // Draw rebar background gradient
+	    DrawThemeBackground(s_toolbartheme, hdc, 0, 0, &rect, NULL); // 3 = RP_BAND, 6 = RP_BACKGROUND
+
+	    // Draw separators
+	    nbuttons = SendMessage(s_toolbarhwnd, TB_BUTTONCOUNT, (WPARAM)0, (LPARAM)0);
+	    //MessageBox(NULL, itoa(nbuttons, buf, 10), "NBUTTONS", MB_OK);
+	    for (i = 0; i < nbuttons; ++i) {
+		SendMessage(s_toolbarhwnd, TB_GETBUTTON, (WPARAM)i, (LPARAM)&tbn);
+		if (tbn.fsStyle == BTNS_SEP) {
+		    SendMessage(s_toolbarhwnd, TB_GETITEMRECT, (WPARAM)i, (LPARAM)&rc);
+		    shdc = GetDC(s_toolbarhwnd);
+		    x = (rc.right - rc.left)/2;
+		    src.left = rc.left + (x-1);
+		    src.right = rc.right - (x+1);
+		    src.top = rc.top + 1;
+		    src.bottom = rc.bottom - 1;
+		    DrawEdge(shdc, &src, EDGE_ETCHED, BF_LEFT);
+		    ReleaseDC(s_toolbarhwnd, shdc);
+		}
+	    }
+
+	    return TRUE;
+	}
+	case WM_DESTROY:
+	    // MessageBox(NULL, "Closing", "-", MB_OK);
+	    CloseThemeData(s_toolbartheme);
+	    break;
+	case WM_NCDESTROY: // Sent after WM_DESTROY
+	    RemoveWindowSubclass(hwnd, ToolbarWndProc, uIdSubclass);
+	    break;
+    }
+
+    return DefSubclassProc(hwnd, msg, wParam, lParam);
+}
+
 /*
  * Create the toolbar, initially unpopulated.
  *  (just like the menu, there are no defaults, it's all
@@ -4103,26 +4158,62 @@
     static void
 initialise_toolbar(void)
 {
-    InitCommonControls();
-    s_toolbarhwnd = CreateToolbarEx(
-		    s_hwnd,
-		    WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
-		    4000,		//any old big number
-		    31,			//number of images in initial bitmap
-		    s_hinst,
-		    IDR_TOOLBAR1,	// id of initial bitmap
-		    NULL,
-		    0,			// initial number of buttons
-		    TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
-		    TOOLBAR_BUTTON_HEIGHT,
-		    TOOLBAR_BUTTON_WIDTH,
-		    TOOLBAR_BUTTON_HEIGHT,
-		    sizeof(TBBUTTON)
-		    );
-
+    INITCOMMONCONTROLSEX icex;
+    HIMAGELIST imgList;    
+
+    // Common controls v6 does nothing on InitCommonControls()
+    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
+    icex.dwICC  = ICC_BAR_CLASSES;
+    InitCommonControlsEx(&icex);
+
+    s_toolbarhwnd = CreateWindowEx(
+	0,
+	TOOLBARCLASSNAME,
+	(LPSTR)NULL, 
+	WS_CHILD | TBSTYLE_TOOLTIPS | CCS_TOP, // If TBSTYLE_FLAT the the background is not drawn = no gradient
+	0,
+	0, 
+	0, 
+	0, 
+	s_hwnd,
+	(HMENU)4000,
+	GetModuleHandle(NULL), 
+	NULL
+    );
+
+    // Open theme once
+    s_toolbartheme = OpenThemeData(s_toolbartheme, L"REBAR");
+
+    // Window subclassing
+    SetWindowSubclass(s_toolbarhwnd, ToolbarWndProc, 0, 0);
+
+    SendMessage(s_toolbarhwnd, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
+
+    // Create new ImageList, use ILC_MASK so the toolbar button is transparent
+    imgList = ImageList_Create(TOOLBAR_BUTTON_WIDTH, TOOLBAR_BUTTON_HEIGHT, ILC_COLOR32, 0, 64);
+    SendMessage(s_toolbarhwnd, TB_SETIMAGELIST, 0, (LPARAM)imgList);
+    ImageList_SetBkColor(imgList, GetSysColor(COLOR_BTNFACE));
+    
     gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
 }
 
+    static HANDLE
+load_png(char_u *fname)
+{
+    HANDLE hbitmap = NULL;
+    PNGINFO pngInfo;
+    PNG_Init(&pngInfo);
+    if (PNG_LoadFile(&pngInfo, fname)) 
+    {
+	if (PNG_Decode(&pngInfo)) 
+	{
+	    hbitmap = PNG_CreateBitmap(&pngInfo, s_hwnd, PNG_OUTF_AUTO, 0);
+	}
+    }
+    PNG_Cleanup(&pngInfo);
+    return hbitmap;
+}
+
     static int
 get_toolbar_bitmap(vimmenu_T *menu)
 {
@@ -4138,7 +4229,30 @@
 
 	if (menu->iconfile != NULL)
 	{
-	    gui_find_iconfile(menu->iconfile, fname, "bmp");
+	    gui_find_iconfile(menu->iconfile, fname, "png");
+	    hbitmap = load_png(fname);
+	    if (hbitmap == NULL)
+	    {
+		gui_find_iconfile(menu->iconfile, fname, "bmp");
+		hbitmap = LoadImage(
+			    NULL,
+			    fname,
+			    IMAGE_BITMAP,
+			    TOOLBAR_BUTTON_WIDTH,
+			    TOOLBAR_BUTTON_HEIGHT,
+			    LR_LOADFROMFILE |
+			    LR_LOADMAP3DCOLORS
+			    );
+	    }
+	}
+
+	/*
+	 * If the LoadImage call failed, or the "icon=" file
+	 * didn't exist or wasn't specified, try the menu name
+	 */
+	if (hbitmap == NULL && (gui_find_bitmap(menu->name, fname, "png") == OK))
+	    hbitmap = load_png(fname);	
+	if (hbitmap == NULL && (gui_find_bitmap(menu->name, fname, "bmp") == OK))
 	    hbitmap = LoadImage(
 			NULL,
 			fname,
@@ -4148,34 +4262,17 @@
 			LR_LOADFROMFILE |
 			LR_LOADMAP3DCOLORS
 			);
-	}
-
-	/*
-	 * If the LoadImage call failed, or the "icon=" file
-	 * didn't exist or wasn't specified, try the menu name
-	 */
-	if (hbitmap == NULL
-		&& (gui_find_bitmap(menu->name, fname, "bmp") == OK))
-	    hbitmap = LoadImage(
-		    NULL,
-		    fname,
-		    IMAGE_BITMAP,
-		    TOOLBAR_BUTTON_WIDTH,
-		    TOOLBAR_BUTTON_HEIGHT,
-		    LR_LOADFROMFILE |
-		    LR_LOADMAP3DCOLORS
-		);
 
 	if (hbitmap != NULL)
 	{
-	    TBADDBITMAP tbAddBitmap;
-
-	    tbAddBitmap.hInst = NULL;
-	    tbAddBitmap.nID = (long_u)hbitmap;
-
-	    i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
-			    (WPARAM)1, (LPARAM)&tbAddBitmap);
-	    /* i will be set to -1 if it fails */
+	    HIMAGELIST imgList;
+
+	    imgList = (HIMAGELIST)SendMessage(s_toolbarhwnd, TB_GETIMAGELIST, 0, 0);
+	    // Add bitmap to the list
+	    // See http://www.codeguru.com/cpp/controls/toolbar/article.php/c5487/
+	    // to generate hotlist and disabled image list automatically
+	    i = ImageList_AddMasked(imgList, hbitmap, RGB(0, 0, 0));
+	    DeleteObject(hbitmap);
 	}
     }
     if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
@@ -4189,13 +4286,21 @@
     static void
 initialise_tabline(void)
 {
-    InitCommonControls();
+    INITCOMMONCONTROLSEX iccs;
+    
+    iccs.dwSize = sizeof(INITCOMMONCONTROLSEX);
+    iccs.dwICC = ICC_TAB_CLASSES;
+
+    InitCommonControlsEx(&iccs);
 
     s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
-	    WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
+	    WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS|TCS_HOTTRACK|TCS_FLATBUTTONS|TCS_BUTTONS,
 	    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
 	    CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
 
+    // this code removes the separator between the tab elements (if TCS_FLATBUTTONS is specified)
+    // SendMessage(s_tabhwnd, (UINT) TCM_SETEXTENDEDSTYLE, TCS_EX_FLATSEPARATORS, 0);
+
     gui.tabline_height = TABLINE_HEIGHT;
 
 # ifdef USE_SYSMENU_FONT
