Missing parentheses added.
[wine] / dlls / comctl32 / treeview.c
1 /* Treeview control
2  *
3  * Copyright 1998 Eric Kohl <ekohl@abo.rhein-zeitung.de>
4  * Copyright 1998 Alex Priem <alexp@sci.kun.nl>
5  *
6  *
7  * TODO:
8  *   - Nearly all notifications.
9  * 
10  *   list-handling stuff: sort, sorted insertitem.
11  *
12  *   refreshtreeview:   
13                 -small array containing info about positions.
14                 -better implementation of DrawItem (connecting lines).
15                 -implement partial drawing?
16  *   Expand:            -ctlmacro expands twice ->toggle.
17  *  -drag&drop.
18  *  -scrollbars.
19  *  -Unicode messages
20  *  -TVITEMEX 
21  *
22  * FIXMEs:  
23    -GetNextItem: add flag for traversing visible items 
24    -DblClick:   ctlmacro.exe's NM_DBLCLK seems to go wrong (returns FALSE).
25              
26  */
27
28 #include <string.h>
29 #include "windows.h"
30 #include "commctrl.h"
31 #include "treeview.h"
32 #include "heap.h"
33 #include "win.h"
34 #include "debug.h"
35
36 /* ffs should be in <string.h>. */
37
38 /* Defines, since they do not need to return previous state, and nr
39  * has no side effects in this file.
40  */
41 #define tv_test_bit(nr,bf)      (((LPBYTE)bf)[nr>>3]&(1<<(nr&7)))
42 #define tv_set_bit(nr,bf)       ((LPBYTE)bf)[nr>>3]|=(1<<(nr&7))
43 #define tv_clear_bit(nr,bf)     ((LPBYTE)bf)[nr>>3]&=~(1<<(nr&7))
44
45
46 #define TREEVIEW_GetInfoPtr(wndPtr) ((TREEVIEW_INFO *)wndPtr->wExtra[0])
47
48 static BOOL32
49 TREEVIEW_SendSimpleNotify (WND *wndPtr, UINT32 code);
50 static BOOL32
51 TREEVIEW_SendTreeviewNotify (WND *wndPtr, UINT32 code, UINT32 action, 
52                         INT32 oldItem, INT32 newItem, POINT32 pt);
53 static LRESULT
54 TREEVIEW_SelectItem (WND *wndPtr, WPARAM32 wParam, LPARAM lParam);
55 static void
56 TREEVIEW_Refresh (WND *wndPtr, HDC32 hdc);
57
58
59
60
61
62 /* helper functions. Work with the assumption that validity of operands 
63    is checked beforehand */
64
65
66 static TREEVIEW_ITEM *
67 TREEVIEW_ValidItem (TREEVIEW_INFO *infoPtr,int  handle)
68 {
69  
70  if ((!handle) || (handle>infoPtr->uMaxHandle)) return NULL;
71  if (tv_test_bit (handle, infoPtr->freeList)) return NULL;
72
73  return & infoPtr->items[handle];
74 }
75
76
77
78 static TREEVIEW_ITEM *TREEVIEW_GetPrevListItem (TREEVIEW_INFO *infoPtr, 
79                                         TREEVIEW_ITEM *tvItem)
80
81 {
82  TREEVIEW_ITEM *wineItem;
83
84  if (tvItem->upsibling) 
85                 return (& infoPtr->items[tvItem->upsibling]);
86
87  wineItem=tvItem;
88  while (wineItem->parent) {
89         wineItem=& infoPtr->items[wineItem->parent];
90         if (wineItem->upsibling) 
91                 return (& infoPtr->items[wineItem->upsibling]);
92  } 
93
94  return NULL;
95 }
96
97 static TREEVIEW_ITEM *TREEVIEW_GetNextListItem (TREEVIEW_INFO *infoPtr, 
98                                         TREEVIEW_ITEM *tvItem)
99
100 {
101  TREEVIEW_ITEM *wineItem;
102
103  if (tvItem->sibling) 
104                 return (& infoPtr->items[tvItem->sibling]);
105
106  wineItem=tvItem;
107  while (wineItem->parent) {
108         wineItem=& infoPtr->items [wineItem->parent];
109         if (wineItem->sibling) 
110                 return (& infoPtr->items [wineItem->sibling]);
111  } 
112
113  return NULL;
114 }
115
116 static TREEVIEW_ITEM *TREEVIEW_GetLastListItem (TREEVIEW_INFO *infoPtr)
117
118 {
119   TREEVIEW_ITEM *wineItem;
120   
121  wineItem=NULL;
122  if (infoPtr->TopRootItem) 
123         wineItem=& infoPtr->items [infoPtr->TopRootItem];
124  while (wineItem->sibling) 
125         wineItem=& infoPtr->items [wineItem->sibling];
126
127  return wineItem;
128 }
129         
130  
131
132
133 static void
134 TREEVIEW_RemoveItem (TREEVIEW_INFO *infoPtr, TREEVIEW_ITEM *wineItem)
135
136 {
137  TREEVIEW_ITEM *parentItem, *upsiblingItem, *siblingItem;
138  INT32 iItem;
139
140  iItem=wineItem->hItem;
141  tv_set_bit(iItem,infoPtr->freeList);
142  infoPtr->uNumItems--;
143  parentItem=NULL;
144  if (wineItem->pszText!=LPSTR_TEXTCALLBACK32A) 
145         HeapFree (GetProcessHeap (), 0, wineItem->pszText);
146
147  if (wineItem->parent) {
148         parentItem=& infoPtr->items[ wineItem->parent];
149         if (parentItem->cChildren==1) {
150                 parentItem->cChildren=0;
151                 parentItem->firstChild=0;    
152                 return;
153         } else {
154                 parentItem->cChildren--;
155                 if (parentItem->firstChild==iItem) 
156                         parentItem->firstChild=wineItem->sibling;
157                 }
158  }
159
160  if (iItem==infoPtr->TopRootItem) 
161         infoPtr->TopRootItem=wineItem->sibling;
162  if (wineItem->upsibling) {
163         upsiblingItem=& infoPtr->items [wineItem->upsibling];
164         upsiblingItem->sibling=wineItem->sibling;
165  }
166  if (wineItem->sibling) {
167         siblingItem=& infoPtr->items [wineItem->sibling];
168         siblingItem->upsibling=wineItem->upsibling;
169  }
170 }
171
172
173
174 static void TREEVIEW_RemoveAllChildren (TREEVIEW_INFO *infoPtr, 
175                                            TREEVIEW_ITEM *parentItem)
176
177 {
178  TREEVIEW_ITEM *killItem;
179  INT32  kill;
180  
181  kill=parentItem->firstChild;
182  while (kill) {
183         tv_set_bit ( kill, infoPtr->freeList);
184         killItem=& infoPtr->items[kill];
185         if (killItem->pszText!=LPSTR_TEXTCALLBACK32A) 
186                 HeapFree (GetProcessHeap (), 0, killItem->pszText);
187         kill=killItem->sibling;
188  }
189  infoPtr->uNumItems -= parentItem->cChildren;
190  parentItem->firstChild = 0;
191  parentItem->cChildren = 0;
192 }
193
194
195 /* Note:TREEVIEW_RemoveTree doesn't remove infoPtr itself */
196
197 static void TREEVIEW_RemoveTree (TREEVIEW_INFO *infoPtr)
198                                            
199
200 {
201         TREEVIEW_ITEM *killItem;
202         int i;
203
204     for (i=1; i<=infoPtr->uMaxHandle; i++) 
205                 if (!tv_test_bit (i, infoPtr->freeList)) {
206                         killItem=& infoPtr->items [i];  
207                         if (killItem->pszText!=LPSTR_TEXTCALLBACK32A)
208                                 HeapFree (GetProcessHeap (), 0, killItem->pszText);
209                 } 
210
211     if (infoPtr->uNumPtrsAlloced) {
212         HeapFree (GetProcessHeap (), 0, infoPtr->items);
213         HeapFree (GetProcessHeap (), 0, infoPtr->freeList);
214         infoPtr->uNumItems=0;
215         infoPtr->uNumPtrsAlloced=0;
216         infoPtr->uMaxHandle=0;
217     }   
218 }
219
220
221
222
223
224
225
226
227
228
229 static LRESULT
230 TREEVIEW_GetImageList (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
231 {
232   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
233
234   TRACE (treeview,"\n");
235
236   if (infoPtr==NULL) return 0;
237
238   if ((INT32)wParam == TVSIL_NORMAL) 
239         return (LRESULT) infoPtr->himlNormal;
240   if ((INT32)wParam == TVSIL_STATE) 
241         return (LRESULT) infoPtr->himlState;
242
243   return 0;
244 }
245
246
247
248
249 static LRESULT
250 TREEVIEW_SetImageList (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
251 {
252     TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
253     HIMAGELIST himlTemp;
254
255     switch ((INT32)wParam) {
256         case TVSIL_NORMAL:
257             himlTemp = infoPtr->himlNormal;
258             infoPtr->himlNormal = (HIMAGELIST)lParam;
259             return (LRESULT)himlTemp;
260
261         case TVSIL_STATE:
262             himlTemp = infoPtr->himlState;
263             infoPtr->himlState = (HIMAGELIST)lParam;
264             return (LRESULT)himlTemp;
265     }
266
267     return (LRESULT)NULL;
268 }
269
270
271
272 static LRESULT
273 TREEVIEW_SetItemHeight (WND *wndPtr, WPARAM32 wParam)
274 {
275   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
276   INT32 prevHeight=infoPtr->uItemHeight;
277   HDC32 hdc;
278   TEXTMETRIC32A tm;
279
280
281   if (wParam==-1) {
282         hdc=GetDC32 (wndPtr->hwndSelf);
283         infoPtr->uItemHeight=-1;
284         GetTextMetrics32A (hdc, &tm);
285     infoPtr->uRealItemHeight= tm.tmHeight + tm.tmExternalLeading;
286         ReleaseDC32 (wndPtr->hwndSelf, hdc);
287         return prevHeight;
288   }
289
290         /* FIXME: check wParam > imagelist height */
291
292   if (!(wndPtr->dwStyle & TVS_NONEVENHEIGHT))
293         infoPtr->uItemHeight = (INT32) wParam & 0xfffffffe;
294   return prevHeight;
295 }
296
297 static LRESULT
298 TREEVIEW_GetItemHeight (WND *wndPtr)
299 {
300   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
301   
302   return infoPtr->uItemHeight;
303 }
304   
305 static LRESULT
306 TREEVIEW_SetTextColor (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
307 {
308   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
309   COLORREF prevColor=infoPtr->clrText;
310
311   infoPtr->clrText=(COLORREF) lParam;
312   return (LRESULT) prevColor;
313 }
314
315 static LRESULT
316 TREEVIEW_GetTextColor (WND *wndPtr)
317 {
318   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
319         
320   return (LRESULT) infoPtr->clrText;
321 }
322
323
324 static INT32
325 TREEVIEW_DrawItem (WND *wndPtr, HDC32 hdc, TREEVIEW_ITEM *wineItem, 
326                    TREEVIEW_ITEM *upperItem, int indent)
327 {
328   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
329   INT32  oldBkMode,center,xpos;
330   COLORREF oldBkColor;
331   UINT32 uTextJustify = DT_LEFT;
332   HPEN32 hOldPen, hnewPen,hRootPen;
333   RECT32 r,upper;
334   
335   hnewPen = CreatePen32(PS_DOT, 0, GetSysColor32(COLOR_WINDOWTEXT) );
336   hOldPen = SelectObject32( hdc, hnewPen );
337  
338   r=wineItem->rect;
339   if (upperItem) 
340         upper=upperItem->rect;
341   else {
342         upper.top=0;
343         upper.left=8;
344   }
345   center=(r.top+r.bottom)/2;
346   xpos=r.left+8;
347
348   if (wndPtr->dwStyle & TVS_HASLINES) {
349         POINT32 points[3];
350         if ((wndPtr->dwStyle & TVS_LINESATROOT) && (indent==0)) {
351                 points[0].y=points[1].y=center;
352                 points[2].y=upper.top;
353                 points[1].x=points[2].x=upper.left;
354                 points[0].x=upper.left+12;
355                 points[2].y+=5;
356
357                 Polyline32 (hdc,points,3);
358         }
359         else {
360                 points[0].y=points[1].y=center;
361                 points[2].y=upper.top;
362                 points[1].x=points[2].x=upper.left+13;
363                 points[0].x=upper.left+25;
364                 points[2].y+=5;
365                 Polyline32 (hdc,points,3);
366         }
367  }
368
369   DeleteObject32(hnewPen);
370   SelectObject32(hdc, hOldPen);
371
372   if ((wndPtr->dwStyle & TVS_HASBUTTONS) && (wineItem->cChildren)) {
373 /*
374         hRootPen = CreatePen32(PS_SOLID, 0, GetSysColor32(COLOR_WINDOW) );
375         SelectObject32( hdc, hRootPen );
376 */
377
378         Rectangle32 (hdc, xpos-4, center-4, xpos+5, center+5);
379         MoveToEx32 (hdc, xpos-2, center, NULL);
380         LineTo32   (hdc, xpos+3, center);
381         if (!(wineItem->state & TVIS_EXPANDED)) {
382                 MoveToEx32 (hdc, xpos,   center-2, NULL);
383                 LineTo32   (hdc, xpos,   center+3);
384         }
385  /*     DeleteObject32(hRootPen); */
386         }
387
388
389   xpos+=13;
390
391   if (wineItem->mask & TVIF_IMAGE) {
392         if (wineItem->iImage!=I_IMAGECALLBACK) {
393                 if (infoPtr->himlNormal) {
394                         ImageList_Draw (infoPtr->himlNormal,wineItem->iImage, hdc,
395                                         xpos-2, r.top+1, ILD_NORMAL);
396                         xpos+=15;
397                 }
398         }
399   }
400
401   r.left=xpos;
402   if ((wineItem->mask & TVIF_TEXT) && (wineItem->pszText)) {
403             if (wineItem->state & TVIS_SELECTED) {
404                 oldBkMode = SetBkMode32(hdc, OPAQUE);
405                 oldBkColor= SetBkColor32 (hdc, GetSysColor32( COLOR_HIGHLIGHT));
406                 SetTextColor32 (hdc, GetSysColor32(COLOR_HIGHLIGHTTEXT));
407             }
408             else {
409                 oldBkMode = SetBkMode32(hdc, TRANSPARENT);
410             }
411             r.left += 3;
412             r.right -= 3;
413                         if (infoPtr->clrText==-1)
414                 SetTextColor32 (hdc, COLOR_BTNTEXT);
415                         else 
416                                 SetTextColor32 (hdc, infoPtr->clrText);  /* FIXME: retval */
417             DrawText32A(hdc, wineItem->pszText, lstrlen32A(wineItem->pszText),
418                   &r, uTextJustify|DT_VCENTER|DT_SINGLELINE);
419             if (oldBkMode != TRANSPARENT)
420                 SetBkMode32(hdc, oldBkMode);
421             if (wineItem->state & TVIS_SELECTED)
422                 SetBkColor32 (hdc, oldBkColor);
423         }
424
425  return wineItem->rect.right;
426 }
427
428
429
430
431
432
433
434 static LRESULT
435 TREEVIEW_GetItemRect (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
436 {
437   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
438   TREEVIEW_ITEM *wineItem;
439   INT32 iItem;
440   LPRECT32 lpRect;
441
442   TRACE (treeview,"\n");
443   if (infoPtr==NULL) return FALSE;
444   
445   iItem = (INT32)lParam;
446   wineItem = TREEVIEW_ValidItem (infoPtr, iItem);
447   if (!wineItem) return FALSE;
448
449   wineItem=& infoPtr->items[ iItem ];
450   if (!wineItem->visible) return FALSE;
451
452   lpRect = (LPRECT32)lParam;
453   if (lpRect == NULL) return FALSE;
454         
455   if ((INT32) wParam) {
456         lpRect->left    = wineItem->text.left;
457         lpRect->right   = wineItem->text.right;
458         lpRect->bottom  = wineItem->text.bottom;
459         lpRect->top     = wineItem->text.top;
460   } else {
461         lpRect->left    = wineItem->rect.left;
462         lpRect->right   = wineItem->rect.right;
463         lpRect->bottom  = wineItem->rect.bottom;
464         lpRect->top     = wineItem->rect.top;
465   }
466
467   return TRUE;
468 }
469
470
471
472 static LRESULT
473 TREEVIEW_GetVisibleCount (WND *wndPtr,  WPARAM32 wParam, LPARAM lParam)
474
475 {
476   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
477
478   TRACE (treeview,"\n");
479
480   return (LRESULT) infoPtr->uVisibleHeight / infoPtr->uRealItemHeight;
481 }
482
483
484
485 static LRESULT
486 TREEVIEW_SetItem (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
487 {
488   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
489   TREEVIEW_ITEM *wineItem;
490   TV_ITEM *tvItem;
491   INT32 iItem,len;
492
493   TRACE (treeview,"\n");
494   tvItem=(LPTVITEM) lParam;
495   iItem=tvItem->hItem;
496
497   wineItem = TREEVIEW_ValidItem (infoPtr, iItem);
498   if (!wineItem) return FALSE;
499
500   if (tvItem->mask & TVIF_CHILDREN) {
501         wineItem->cChildren=tvItem->cChildren;
502   }
503
504   if (tvItem->mask & TVIF_IMAGE) {
505        wineItem->iImage=tvItem->iImage;
506   }
507
508   if (tvItem->mask & TVIF_INTEGRAL) {
509 /*        wineItem->iIntegral=tvItem->iIntegral; */
510   }
511
512   if (tvItem->mask & TVIF_PARAM) {
513         wineItem->lParam=tvItem->lParam;
514   }
515
516   if (tvItem->mask & TVIF_SELECTEDIMAGE) {
517         wineItem->iSelectedImage=tvItem->iSelectedImage;
518   }
519
520   if (tvItem->mask & TVIF_STATE) {
521         wineItem->state=tvItem->state & tvItem->stateMask;
522   }
523
524   if (tvItem->mask & TVIF_TEXT) {
525         len=tvItem->cchTextMax;
526         if (len>wineItem->cchTextMax) {
527                 HeapFree (GetProcessHeap (), 0, wineItem->pszText);
528                 wineItem->pszText= HeapAlloc (GetProcessHeap (), 
529                                 HEAP_ZERO_MEMORY, len+1);
530         }
531         lstrcpyn32A (wineItem->pszText, tvItem->pszText,len);
532    }
533
534   return TRUE;
535 }
536
537
538
539
540
541 static void
542 TREEVIEW_Refresh (WND *wndPtr, HDC32 hdc)
543
544 {
545     TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
546     HFONT32 hFont, hOldFont;
547     RECT32 rect;
548     HBRUSH32 hbrBk;
549     INT32 iItem, indent, x, y, height;
550     INT32 viewtop,viewbottom,viewleft,viewright;
551     TREEVIEW_ITEM *wineItem, *prevItem;
552
553     TRACE (treeview,"\n");
554
555     if (infoPtr->Timer & TV_REFRESH_TIMER_SET) {
556                 KillTimer32 (wndPtr->hwndSelf, TV_REFRESH_TIMER);
557                 infoPtr->Timer &= ~TV_REFRESH_TIMER_SET;
558     }
559
560     
561     GetClientRect32 (wndPtr->hwndSelf, &rect);
562     if ((rect.left-rect.right ==0) || (rect.top-rect.bottom==0)) return;
563     viewtop=infoPtr->cy;
564     viewbottom=infoPtr->cy + rect.bottom-rect.top;
565     viewleft=infoPtr->cx;
566     viewright=infoPtr->cx + rect.right-rect.left;
567
568         infoPtr->uVisibleHeight=viewbottom - viewtop;
569
570     hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject32 (DEFAULT_GUI_FONT);
571     hOldFont = SelectObject32 (hdc, hFont);
572
573     /* draw background */
574     hbrBk = GetSysColorBrush32(COLOR_WINDOW);
575     FillRect32(hdc, &rect, hbrBk);
576
577
578     iItem=infoPtr->TopRootItem;
579     infoPtr->firstVisible=0;
580     wineItem=NULL;
581     indent=0;
582     x=y=0;
583     TRACE (treeview, "[%d %d %d %d]\n",viewtop,viewbottom,viewleft,viewright);
584
585     while (iItem) {
586                 prevItem=wineItem;
587         wineItem= & infoPtr->items[iItem];
588
589                 TRACE (treeview, "%d %d [%d %d %d %d] (%s)\n",y,x,
590                         wineItem->rect.top, wineItem->rect.bottom,
591                         wineItem->rect.left, wineItem->rect.right,
592                         wineItem->pszText);
593
594                 height=infoPtr->uRealItemHeight * wineItem->iIntegral;
595                 if ((y >= viewtop) && (y <= viewbottom) &&
596                 (x >= viewleft  ) && (x <= viewright)) {
597                         wineItem->rect.top = y - infoPtr->cy + rect.top;
598                         wineItem->rect.bottom = wineItem->rect.top + height ;
599                         wineItem->rect.left = x - infoPtr->cx + rect.left;
600                         wineItem->rect.right = rect.right;
601                         if (!infoPtr->firstVisible)
602                                 infoPtr->firstVisible=wineItem->hItem;
603                         TREEVIEW_DrawItem (wndPtr, hdc, wineItem, prevItem, indent);
604                 }
605                 else {
606                         wineItem->rect.top  = wineItem->rect.bottom = -1;
607                         wineItem->rect.left = wineItem->rect.right = -1;
608                 }
609
610                 /* look up next item */
611         
612                 if ((wineItem->firstChild) && (wineItem->state & TVIS_EXPANDED)) {
613                         iItem=wineItem->firstChild;
614                         indent++;
615                         x+=infoPtr->uIndent;
616                 }
617                 else {
618                         iItem=wineItem->sibling;
619                         while ((!iItem) && (indent>0)) {
620                                 indent--;
621                                 x-=infoPtr->uIndent;
622                                 prevItem=wineItem;
623                                 wineItem=&infoPtr->items[wineItem->parent];
624                                 iItem=wineItem->sibling;
625                         }
626                 }
627         y +=height;
628     }                           /* while */
629
630     infoPtr->uTotalHeight=y;
631     if (y >= (viewbottom-viewtop)) {
632                 if (!(infoPtr->uInternalStatus & TV_VSCROLL))
633                         ShowScrollBar32 (wndPtr->hwndSelf, SB_VERT, TRUE);
634                 infoPtr->uInternalStatus |=TV_VSCROLL;
635                 SetScrollRange32 (wndPtr->hwndSelf, SB_VERT, 0, 
636                                         y - infoPtr->uVisibleHeight, FALSE);
637                 SetScrollPos32 (wndPtr->hwndSelf, SB_VERT, infoPtr->cy, TRUE);
638         }
639     else {
640                 if (infoPtr->uInternalStatus & TV_VSCROLL) 
641                         ShowScrollBar32 (wndPtr->hwndSelf, SB_VERT, FALSE);
642                 infoPtr->uInternalStatus &= ~TV_VSCROLL;
643         }
644
645
646     SelectObject32 (hdc, hOldFont);
647 }
648
649
650 static LRESULT 
651 TREEVIEW_HandleTimer ( WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
652 {
653   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
654   HDC32 hdc;
655
656   if (!infoPtr) return FALSE;
657  
658   TRACE (treeview, "timer\n");
659
660   switch (wParam) {
661         case TV_REFRESH_TIMER:
662                 KillTimer32 (wndPtr->hwndSelf, TV_REFRESH_TIMER);
663                 infoPtr->Timer &= ~TV_REFRESH_TIMER_SET;
664                 hdc=GetDC32 (wndPtr->hwndSelf);
665                 TREEVIEW_Refresh (wndPtr, hdc);
666                 ReleaseDC32 (wndPtr->hwndSelf, hdc);
667                 return 0;
668         case TV_EDIT_TIMER:
669                 KillTimer32 (wndPtr->hwndSelf, TV_EDIT_TIMER);
670                 infoPtr->Timer &= ~TV_EDIT_TIMER_SET;
671                 return 0;
672  }
673                 
674  return 1;
675 }
676
677
678 static void
679 TREEVIEW_QueueRefresh (WND *wndPtr)
680
681 {
682  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
683
684  TRACE (treeview,"queued\n");
685  if (infoPtr->Timer & TV_REFRESH_TIMER_SET) {
686         KillTimer32 (wndPtr->hwndSelf, TV_REFRESH_TIMER);
687  }
688
689  SetTimer32 (wndPtr->hwndSelf, TV_REFRESH_TIMER, TV_REFRESH_DELAY, 0);
690  infoPtr->Timer|=TV_REFRESH_TIMER_SET;
691 }
692
693
694
695 static LRESULT
696 TREEVIEW_GetItem (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
697 {
698   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
699   LPTVITEM      tvItem;
700   TREEVIEW_ITEM *wineItem;
701   INT32         iItem,len;
702
703   TRACE (treeview,"\n");
704   tvItem=(LPTVITEM) lParam;
705   iItem=tvItem->hItem;
706
707   wineItem = TREEVIEW_ValidItem (infoPtr, iItem);
708   if (!wineItem) return FALSE;
709
710
711    if (tvItem->mask & TVIF_CHILDREN) {
712         tvItem->cChildren=wineItem->cChildren;
713    }
714
715    if (tvItem->mask & TVIF_HANDLE) {
716         tvItem->hItem=wineItem->hItem;
717    }
718
719    if (tvItem->mask & TVIF_IMAGE) {
720         tvItem->iImage=wineItem->iImage;
721    }
722
723    if (tvItem->mask & TVIF_INTEGRAL) {
724 /*        tvItem->iIntegral=wineItem->iIntegral; */
725    }
726
727    if (tvItem->mask & TVIF_PARAM) {
728         tvItem->lParam=wineItem->lParam;
729    }
730
731    if (tvItem->mask & TVIF_SELECTEDIMAGE) {
732         tvItem->iSelectedImage=wineItem->iSelectedImage;
733    }
734
735    if (tvItem->mask & TVIF_STATE) {
736         tvItem->state=wineItem->state & tvItem->stateMask;
737    }
738
739    if (tvItem->mask & TVIF_TEXT) {
740         len=wineItem->cchTextMax;
741         if (wineItem->cchTextMax>tvItem->cchTextMax) 
742                 len=tvItem->cchTextMax-1;
743         lstrcpyn32A (tvItem->pszText, tvItem->pszText,len);
744    }
745
746   return TRUE;
747 }
748
749
750
751 static LRESULT
752 TREEVIEW_GetNextItem32 (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
753
754 {
755   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
756   TREEVIEW_ITEM *wineItem;
757   INT32 iItem, flag;
758
759
760   TRACE (treeview,"item:%lu, flags:%x\n", lParam, wParam);
761   if (!infoPtr) return FALSE;
762
763   flag= (INT32) wParam;
764   switch (flag) {
765         case TVGN_ROOT:         return (LRESULT) infoPtr->TopRootItem;
766         case TVGN_CARET:        return (LRESULT) infoPtr->selectedItem;
767         case TVGN_FIRSTVISIBLE: return (LRESULT) infoPtr->firstVisible;
768         case TVGN_DROPHILITE:   return (LRESULT) infoPtr->dropItem;
769         }
770         
771   iItem= (INT32) lParam;
772   wineItem = TREEVIEW_ValidItem (infoPtr, iItem);
773   if (!wineItem) return FALSE;
774
775   switch (flag) {
776         case TVGN_NEXT:         return (LRESULT) wineItem->sibling;
777         case TVGN_PREVIOUS:     return (LRESULT) wineItem->upsibling;
778         case TVGN_PARENT:       return (LRESULT) wineItem->parent;
779         case TVGN_CHILD:        return (LRESULT) wineItem->firstChild;
780         case TVGN_LASTVISIBLE:  FIXME (treeview,"TVGN_LASTVISIBLE not implemented\n");
781                                 return 0;
782         case TVGN_NEXTVISIBLE:  wineItem=TREEVIEW_GetNextListItem 
783                                                 (infoPtr,wineItem);
784                                 if (wineItem) 
785                                         return (LRESULT) wineItem->hItem;
786                                 else
787                                         return (LRESULT) 0;
788         case TVGN_PREVIOUSVISIBLE: wineItem=TREEVIEW_GetPrevListItem
789                                                 (infoPtr, wineItem);
790                                 if (wineItem) 
791                                         return (LRESULT) wineItem->hItem;
792                                 else
793                                         return (LRESULT) 0;
794         default:        FIXME (treeview,"Unknown msg %x,item %x\n", flag,iItem);
795         }
796
797  return 0;
798 }
799
800
801 static LRESULT
802 TREEVIEW_GetCount (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
803 {
804  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
805
806  return (LRESULT) infoPtr->uNumItems;
807 }
808
809
810
811
812 /* the method used below isn't the most memory-friendly, but it avoids 
813    a lot of memory reallocations */ 
814
815 /* BTW: we waste handle 0; 0 is not an allowed handle. Fix this by
816         decreasing infoptr->items with 1, and increasing it by 1 if 
817         it is referenced in mm-handling stuff? */
818
819 static LRESULT
820 TREEVIEW_InsertItem32A (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
821
822 {
823   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
824   TVINSERTSTRUCT  *ptdi;
825   TV_ITEM       *tvItem;
826   TREEVIEW_ITEM *wineItem, *parentItem, *prevsib, *sibItem;
827   INT32         iItem,listItems,i,len;
828   
829   TRACE (treeview,"\n");
830   ptdi = (TVINSERTSTRUCT *) lParam;
831
832         /* check if memory is available */
833
834   if (infoPtr->uNumPtrsAlloced==0) {
835         infoPtr->items = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
836                                     TVITEM_ALLOC*sizeof (TREEVIEW_ITEM));
837         infoPtr->freeList= HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
838                                     (1+(TVITEM_ALLOC>>5)) *sizeof (INT32));
839         infoPtr->uNumPtrsAlloced=TVITEM_ALLOC;
840         infoPtr->TopRootItem=1;
841    }
842
843   if (infoPtr->uNumItems == (infoPtr->uNumPtrsAlloced-1) ) {
844         TREEVIEW_ITEM *oldItems = infoPtr->items;
845         INT32 *oldfreeList = infoPtr->freeList;
846
847         infoPtr->uNumPtrsAlloced*=2;
848         infoPtr->items = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
849                               infoPtr->uNumPtrsAlloced*sizeof (TREEVIEW_ITEM));
850         infoPtr->freeList= HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
851                               (1+(infoPtr->uNumPtrsAlloced>>5))*sizeof (INT32));
852
853         memcpy (&infoPtr->items[0], &oldItems[0],
854                     infoPtr->uNumPtrsAlloced/2 * sizeof(TREEVIEW_ITEM));
855         memcpy (&infoPtr->freeList[0], &oldfreeList[0],
856                     (infoPtr->uNumPtrsAlloced>>6) * sizeof(INT32));
857
858          HeapFree (GetProcessHeap (), 0, oldItems);  
859          HeapFree (GetProcessHeap (), 0, oldfreeList);  
860     }
861
862   iItem=0;
863   infoPtr->uNumItems++;
864
865   if (infoPtr->uMaxHandle==(infoPtr->uNumItems-1))  { 
866         iItem=infoPtr->uNumItems;
867         infoPtr->uMaxHandle++;
868   } 
869   else {                                         /* check freelist */
870         for (i=0; i<infoPtr->uNumPtrsAlloced>>5; i++) {
871                 if (infoPtr->freeList[i]) {
872                         iItem=ffs (infoPtr->freeList[i]);
873                         tv_clear_bit(iItem,&infoPtr->freeList[i]);
874                         break;
875                 }
876          } 
877   }
878   if (!iItem) ERR (treeview, "Argh -- can't find free item.\n");
879   
880   tvItem= & ptdi->item;
881   wineItem=& infoPtr->items[iItem];
882
883
884
885   if ((ptdi->hParent==TVI_ROOT) || (ptdi->hParent==0)) {
886         parentItem=NULL;
887         wineItem->parent=0; 
888         sibItem=&infoPtr->items [infoPtr->TopRootItem];
889         listItems=infoPtr->uNumItems;
890   }
891   else  {
892         parentItem= &infoPtr->items[ptdi->hParent];
893         if (!parentItem->firstChild) 
894                 parentItem->firstChild=iItem;
895         wineItem->parent=ptdi->hParent;
896         sibItem=&infoPtr->items [parentItem->firstChild];
897         parentItem->cChildren++;
898         listItems=parentItem->cChildren;
899   }
900
901   wineItem->upsibling=0;  /* needed in case we're the first item in a list */ 
902   wineItem->sibling=0;     
903   wineItem->firstChild=0;
904
905   if (listItems>1) {
906      prevsib=NULL;
907      switch (ptdi->hInsertAfter) {
908                 case TVI_FIRST: wineItem->sibling=infoPtr->TopRootItem;
909                         infoPtr->TopRootItem=iItem;
910                         break;
911                 case TVI_LAST:  
912                         while (sibItem->sibling) {
913                                 prevsib=sibItem;
914                                 sibItem=&infoPtr->items [sibItem->sibling];
915                         }
916                         sibItem->sibling=iItem;
917                         if (prevsib!=NULL) 
918                                 wineItem->upsibling=prevsib->hItem;
919                         else
920                                 wineItem->sibling=0;    /* terminate list */
921                         break;
922                 case TVI_SORT:  
923                         FIXME (treeview, "Sorted insert not implemented yet\n");
924                         break;
925                 default:        
926                         while ((sibItem->sibling) && (sibItem->sibling!=iItem)) {
927                                 prevsib=sibItem;
928                 sibItem=&infoPtr->items [sibItem->sibling];
929             }
930                         if (sibItem->sibling) 
931                                 WARN (treeview, "Buggy program tried to insert item after nonexisting handle.");
932                         sibItem->upsibling=iItem;
933                         wineItem->sibling=sibItem->hItem;
934                         if (prevsib!=NULL) 
935                                 wineItem->upsibling=prevsib->hItem;
936                         break;
937         }
938    }    
939
940
941 /* Fill in info structure */
942
943    wineItem->mask=tvItem->mask;
944    wineItem->hItem=iItem;
945    wineItem->iIntegral=1; 
946
947    if (tvItem->mask & TVIF_CHILDREN)
948          wineItem->cChildren=tvItem->cChildren;
949
950    if (tvItem->mask & TVIF_IMAGE) 
951         wineItem->iImage=tvItem->iImage;
952
953 /*   if (tvItem->mask & TVIF_INTEGRAL) 
954         wineItem->iIntegral=tvItem->iIntegral;  */
955    
956
957    if (tvItem->mask & TVIF_PARAM) 
958         wineItem->lParam=tvItem->lParam;
959
960    if (tvItem->mask & TVIF_SELECTEDIMAGE) 
961         wineItem->iSelectedImage=tvItem->iSelectedImage;
962
963    if (tvItem->mask & TVIF_STATE) {
964         wineItem->state=tvItem->state;
965         wineItem->stateMask=tvItem->stateMask;
966    }
967
968    if (tvItem->mask & TVIF_TEXT) {
969         TRACE (treeview,"(%s)\n", tvItem->pszText); 
970         if (tvItem->pszText!=LPSTR_TEXTCALLBACK32A) {
971                 len = lstrlen32A (tvItem->pszText)+1;
972                 wineItem->pszText=
973                         HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, len+1);
974                 lstrcpy32A (wineItem->pszText, tvItem->pszText);
975                 wineItem->cchTextMax=len;
976         }
977    }
978
979    TREEVIEW_QueueRefresh (wndPtr);
980
981    return (LRESULT) iItem;
982 }
983
984
985
986 static LRESULT
987 TREEVIEW_DeleteItem (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
988 {
989   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
990   INT32 iItem;
991   POINT32 pt;
992   TREEVIEW_ITEM *wineItem;
993
994   TRACE (treeview,"\n");
995   if (!infoPtr) return FALSE;
996
997   if ((INT32) lParam == TVI_ROOT) {
998         TREEVIEW_RemoveTree (infoPtr);
999   } else {
1000         iItem= (INT32) lParam;
1001         wineItem = TREEVIEW_ValidItem (infoPtr, iItem);
1002         if (!wineItem) return FALSE;
1003         TREEVIEW_SendTreeviewNotify (wndPtr, TVN_DELETEITEM, 0, iItem, 0, pt);
1004         TREEVIEW_RemoveItem (infoPtr, wineItem);
1005   }
1006
1007   TREEVIEW_QueueRefresh (wndPtr);
1008
1009   return TRUE;
1010 }
1011
1012
1013 static LRESULT
1014 TREEVIEW_GetIndent (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1015 {
1016  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1017
1018  return infoPtr->uIndent;
1019 }
1020
1021 static LRESULT
1022 TREEVIEW_SetIndent (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1023 {
1024   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1025   INT32 newIndent;
1026    
1027   newIndent=(INT32) wParam;
1028   if (newIndent < MINIMUM_INDENT) newIndent=MINIMUM_INDENT;
1029   infoPtr->uIndent=newIndent;
1030   
1031   return 0;
1032 }
1033
1034
1035
1036
1037
1038 static LRESULT
1039 TREEVIEW_Create (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1040 {
1041     TREEVIEW_INFO *infoPtr;
1042         HDC32 hdc;
1043     TEXTMETRIC32A tm;
1044   
1045     TRACE (treeview,"\n");
1046       /* allocate memory for info structure */
1047       infoPtr = (TREEVIEW_INFO *)HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
1048                                      sizeof(TREEVIEW_INFO));
1049
1050     wndPtr->wExtra[0] = (DWORD)infoPtr;
1051
1052     if (infoPtr == NULL) {
1053                 ERR (treeview, "could not allocate info memory!\n");
1054                 return 0;
1055     }
1056
1057     if ((TREEVIEW_INFO*)wndPtr->wExtra[0] != infoPtr) {
1058                 ERR (treeview, "pointer assignment error!\n");
1059                 return 0;
1060     }
1061
1062         hdc=GetDC32 (wndPtr->hwndSelf);
1063
1064     /* set default settings */
1065     infoPtr->uInternalStatus=0;
1066     infoPtr->uNumItems=0;
1067     infoPtr->clrBk = GetSysColor32 (COLOR_WINDOW);
1068     infoPtr->clrText = GetSysColor32 (COLOR_BTNTEXT);
1069     infoPtr->cy = 0;
1070     infoPtr->cx = 0;
1071     infoPtr->uIndent = 15;
1072     infoPtr->himlNormal = NULL;
1073     infoPtr->himlState = NULL;
1074         infoPtr->uItemHeight = -1;
1075     GetTextMetrics32A (hdc, &tm);
1076     infoPtr->uRealItemHeight= tm.tmHeight + tm.tmExternalLeading;
1077
1078     infoPtr->items = NULL;
1079     infoPtr->selectedItem=0;
1080     infoPtr->clrText=-1;        /* use system color */
1081     infoPtr->dropItem=0;
1082
1083 /*
1084     infoPtr->hwndNotify = GetParent32 (wndPtr->hwndSelf);
1085     infoPtr->bTransparent = (wndPtr->dwStyle & TBSTYLE_FLAT);
1086 */
1087
1088     if (wndPtr->dwStyle & TBSTYLE_TOOLTIPS) {
1089         /* Create tooltip control */
1090 //      infoPtr->hwndToolTip = CreateWindowEx32A (....);
1091
1092         /* Send TV_TOOLTIPSCREATED notification */
1093
1094     }
1095     ReleaseDC32 (wndPtr->hwndSelf, hdc);
1096
1097     return 0;
1098 }
1099
1100
1101
1102 static LRESULT 
1103 TREEVIEW_Destroy (WND *wndPtr) 
1104 {
1105    TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1106      
1107    TREEVIEW_RemoveTree (infoPtr);
1108    if (infoPtr->Timer & TV_REFRESH_TIMER_SET) 
1109         KillTimer32 (wndPtr->hwndSelf, TV_REFRESH_TIMER);
1110
1111    HeapFree (GetProcessHeap (), 0, infoPtr);
1112
1113    return 0;
1114 }
1115
1116
1117 static LRESULT
1118 TREEVIEW_Paint (WND *wndPtr, WPARAM32 wParam)
1119 {
1120     HDC32 hdc;
1121     PAINTSTRUCT32 ps;
1122
1123     hdc = wParam==0 ? BeginPaint32 (wndPtr->hwndSelf, &ps) : (HDC32)wParam;
1124     TREEVIEW_QueueRefresh (wndPtr);
1125     if(!wParam)
1126         EndPaint32 (wndPtr->hwndSelf, &ps);
1127     return 0;
1128 }
1129
1130
1131
1132 static LRESULT
1133 TREEVIEW_EraseBackground (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1134 {
1135     TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1136     HBRUSH32 hBrush = CreateSolidBrush32 (infoPtr->clrBk);
1137     RECT32 rect;
1138
1139     TRACE (treeview,"\n");
1140     GetClientRect32 (wndPtr->hwndSelf, &rect);
1141     FillRect32 ((HDC32)wParam, &rect, hBrush);
1142     DeleteObject32 (hBrush);
1143     return TRUE;
1144 }
1145
1146
1147
1148
1149
1150   
1151
1152
1153
1154 static BOOL32
1155 TREEVIEW_SendSimpleNotify (WND *wndPtr, UINT32 code)
1156 {
1157     NMHDR nmhdr;
1158
1159     TRACE (treeview, "%x\n",code);
1160     nmhdr.hwndFrom = wndPtr->hwndSelf;
1161     nmhdr.idFrom   = wndPtr->wIDmenu;
1162     nmhdr.code     = code;
1163
1164     return (BOOL32) SendMessage32A (GetParent32 (wndPtr->hwndSelf), WM_NOTIFY,
1165                                    (WPARAM32)nmhdr.idFrom, (LPARAM)&nmhdr);
1166 }
1167
1168
1169
1170
1171 static BOOL32
1172 TREEVIEW_SendTreeviewNotify (WND *wndPtr, UINT32 code, UINT32 action, 
1173                         INT32 oldItem, INT32 newItem, POINT32 pt)
1174 {
1175   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1176   NMTREEVIEW nmhdr;
1177   TREEVIEW_ITEM  *wineItem;
1178
1179   TRACE (treeview,"code:%x action:%x olditem:%x newitem:%x\n",
1180                   code,action,oldItem,newItem);
1181   nmhdr.hdr.hwndFrom = wndPtr->hwndSelf;
1182   nmhdr.hdr.idFrom = wndPtr->wIDmenu;
1183   nmhdr.hdr.code = code;
1184   nmhdr.action = action;
1185   if (oldItem) {
1186         wineItem=& infoPtr->items[oldItem];
1187         nmhdr.itemOld.mask              = wineItem->mask;
1188         nmhdr.itemOld.hItem             = wineItem->hItem;
1189         nmhdr.itemOld.state             = wineItem->state;
1190         nmhdr.itemOld.stateMask = wineItem->stateMask;
1191         nmhdr.itemOld.iImage            = wineItem->iImage;
1192         nmhdr.itemOld.pszText   = wineItem->pszText;
1193         nmhdr.itemOld.cchTextMax        = wineItem->cchTextMax;
1194         nmhdr.itemOld.iImage            = wineItem->iImage;
1195         nmhdr.itemOld.iSelectedImage    = wineItem->iSelectedImage;
1196         nmhdr.itemOld.cChildren         = wineItem->cChildren;
1197         nmhdr.itemOld.lParam            = wineItem->lParam;
1198   }
1199
1200   if (newItem) {
1201         wineItem=& infoPtr->items[newItem];
1202         nmhdr.itemNew.mask              = wineItem->mask;
1203         nmhdr.itemNew.hItem             = wineItem->hItem;
1204         nmhdr.itemNew.state             = wineItem->state;
1205         nmhdr.itemNew.stateMask = wineItem->stateMask;
1206         nmhdr.itemNew.iImage            = wineItem->iImage;
1207         nmhdr.itemNew.pszText   = wineItem->pszText;
1208         nmhdr.itemNew.cchTextMax        = wineItem->cchTextMax;
1209         nmhdr.itemNew.iImage            = wineItem->iImage;
1210         nmhdr.itemNew.iSelectedImage    = wineItem->iSelectedImage;
1211         nmhdr.itemNew.cChildren         = wineItem->cChildren;
1212         nmhdr.itemNew.lParam            = wineItem->lParam;
1213   }
1214
1215   nmhdr.ptDrag.x = pt.x;
1216   nmhdr.ptDrag.y = pt.y;
1217
1218   return (BOOL32)SendMessage32A (GetParent32 (wndPtr->hwndSelf), WM_NOTIFY,
1219                                    (WPARAM32)wndPtr->wIDmenu, (LPARAM)&nmhdr);
1220
1221 }
1222
1223
1224
1225
1226 static LRESULT
1227 TREEVIEW_Expand (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1228 {
1229  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1230  TREEVIEW_ITEM *wineItem;
1231  UINT32 flag;
1232  INT32 expandItem;
1233  POINT32 pt;
1234  
1235  flag= (UINT32) wParam;
1236  expandItem= (INT32) lParam;
1237  TRACE (treeview,"flags:%x item:%x\n", expandItem, wParam);
1238  wineItem = TREEVIEW_ValidItem (infoPtr, expandItem);
1239  if (!wineItem) return 0;
1240  if (!wineItem->cChildren) return 0;
1241
1242  if (flag & TVE_TOGGLE) {               /* FIXME: check exact behaviour here */
1243         flag &= ~TVE_TOGGLE;            /* ie: bitwise ops or 'case' ops */
1244         if (wineItem->state & TVIS_EXPANDED) 
1245                 flag |= TVE_COLLAPSE;
1246         else
1247                 flag |= TVE_EXPAND;
1248  }
1249
1250  switch (flag) {
1251     case TVE_COLLAPSERESET: 
1252                 if (!wineItem->state & TVIS_EXPANDED) return 0;
1253                 wineItem->state &= ~(TVIS_EXPANDEDONCE | TVIS_EXPANDED);
1254                 TREEVIEW_RemoveAllChildren (infoPtr, wineItem);
1255                 break;
1256
1257     case TVE_COLLAPSE: 
1258                 if (!wineItem->state & TVIS_EXPANDED) return 0;
1259                 wineItem->state &= ~TVIS_EXPANDED;
1260                 break;
1261
1262     case TVE_EXPAND: 
1263                 if (wineItem->state & TVIS_EXPANDED) return 0;
1264                 if (!(wineItem->state & TVIS_EXPANDEDONCE)) {
1265                 if (TREEVIEW_SendTreeviewNotify (wndPtr, TVN_ITEMEXPANDING, 
1266                                                                                         0, 0, expandItem, pt))
1267                                         return FALSE;   /* FIXME: OK? */
1268                 wineItem->state |= TVIS_EXPANDED | TVIS_EXPANDEDONCE;
1269         TREEVIEW_SendTreeviewNotify (wndPtr, TVN_ITEMEXPANDED, 
1270                                                                                         0, 0, expandItem, pt);
1271         }
1272         wineItem->state |= TVIS_EXPANDED;
1273         break;
1274    case TVE_EXPANDPARTIAL:
1275                 FIXME (treeview, "TVE_EXPANDPARTIAL not implemented\n");
1276                 wineItem->state ^=TVIS_EXPANDED;
1277                 wineItem->state |=TVIS_EXPANDEDONCE;
1278                 break;
1279   }
1280  
1281  TREEVIEW_QueueRefresh (wndPtr);
1282
1283  return TRUE;
1284 }
1285
1286
1287
1288 static HTREEITEM
1289 TREEVIEW_HitTest (WND *wndPtr, LPTVHITTESTINFO lpht)
1290 {
1291  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1292  TREEVIEW_ITEM *wineItem;
1293  RECT32 rect;
1294  UINT32 status,x,y;
1295  
1296
1297
1298  GetClientRect32 (wndPtr->hwndSelf, &rect);
1299  TRACE (treeview,"(%d,%d)\n",lpht->pt.x, lpht->pt.y);
1300
1301  status=0;
1302  x=lpht->pt.x;
1303  y=lpht->pt.y;
1304  if (x < rect.left)  status|=TVHT_TOLEFT;
1305  if (x > rect.right) status|=TVHT_TORIGHT;
1306  if (y < rect.top )  status|=TVHT_ABOVE;
1307  if (y > rect.bottom) status|=TVHT_BELOW;
1308  if (status) {
1309         lpht->flags=status;
1310         return 0;
1311  }
1312
1313  if (!infoPtr->firstVisible) WARN (treeview,"Can't fetch first visible item");
1314  wineItem=&infoPtr->items [infoPtr->firstVisible];
1315
1316  while ((wineItem!=NULL) && (y > wineItem->rect.bottom))
1317        wineItem=TREEVIEW_GetNextListItem (infoPtr,wineItem);
1318         
1319  if (wineItem==NULL) {
1320         lpht->flags=TVHT_NOWHERE;
1321         return 0;
1322  }
1323
1324  if (x>wineItem->rect.right) {
1325         lpht->flags|=TVHT_ONITEMRIGHT;
1326         return wineItem->hItem;
1327  }
1328  
1329         
1330  if (x<wineItem->rect.left+10) lpht->flags|=TVHT_ONITEMBUTTON;
1331
1332  lpht->flags=TVHT_ONITEMLABEL;    /* FIXME: implement other flags */
1333         
1334
1335  lpht->hItem=wineItem->hItem;
1336  return wineItem->hItem;
1337 }
1338
1339
1340 static LRESULT
1341 TREEVIEW_HitTest32 (WND *wndPtr, LPARAM lParam)
1342 {
1343  
1344   return (LRESULT) TREEVIEW_HitTest (wndPtr, (LPTVHITTESTINFO) lParam);
1345 }
1346
1347
1348
1349
1350 LRESULT
1351 TREEVIEW_LButtonDoubleClick (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1352 {
1353   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1354   TREEVIEW_ITEM *wineItem;
1355   INT32 iItem;
1356   TVHITTESTINFO ht;
1357
1358   TRACE (treeview,"\n");
1359   ht.pt.x = (INT32)LOWORD(lParam);
1360   ht.pt.y = (INT32)HIWORD(lParam);
1361   SetFocus32 (wndPtr->hwndSelf);
1362
1363   iItem=TREEVIEW_HitTest (wndPtr, &ht);
1364   TRACE (treeview,"item %d \n",iItem);
1365   wineItem=TREEVIEW_ValidItem (infoPtr, iItem);
1366   if (!wineItem) return 0;
1367  
1368   if (TREEVIEW_SendSimpleNotify (wndPtr, NM_DBLCLK)!=TRUE) {     /* FIXME!*/
1369         wineItem->state &= ~TVIS_EXPANDEDONCE;
1370         TREEVIEW_Expand (wndPtr, (WPARAM32) TVE_TOGGLE, (LPARAM) iItem);
1371  }
1372  return TRUE;
1373 }
1374
1375
1376
1377 static LRESULT
1378 TREEVIEW_LButtonDown (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1379 {
1380   INT32 iItem;
1381   TVHITTESTINFO ht;
1382
1383   TRACE (treeview,"\n");
1384   ht.pt.x = (INT32)LOWORD(lParam);
1385   ht.pt.y = (INT32)HIWORD(lParam);
1386
1387   SetFocus32 (wndPtr->hwndSelf);
1388   iItem=TREEVIEW_HitTest (wndPtr, &ht);
1389   TRACE (treeview,"item %d \n",iItem);
1390   if (ht.flags & TVHT_ONITEMBUTTON) {
1391         TREEVIEW_Expand (wndPtr, (WPARAM32) TVE_TOGGLE, (LPARAM) iItem);
1392   }
1393         
1394   if (TREEVIEW_SelectItem (wndPtr, (WPARAM32) TVGN_CARET, (LPARAM) iItem))
1395          return 0;
1396
1397   
1398  return 0;
1399 }
1400
1401
1402 static LRESULT
1403 TREEVIEW_RButtonDown (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1404 {
1405
1406  return 0;
1407 }
1408
1409
1410
1411
1412 /* FIXME: If the specified item is the child of a collapsed parent item,
1413 expand parent's list of child items to reveal the specified item.
1414 */
1415
1416 static LRESULT
1417 TREEVIEW_SelectItem (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1418 {
1419  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1420  TREEVIEW_ITEM *prevItem,*wineItem;
1421  INT32 action,prevSelect, newSelect;
1422  POINT32 dummy;
1423
1424   TRACE (treeview,"item %lx, flag %x\n", lParam, wParam);
1425   newSelect= (INT32) lParam;
1426   wineItem = TREEVIEW_ValidItem (infoPtr, newSelect);
1427   if (!wineItem) return FALSE;
1428   prevSelect=infoPtr->selectedItem;
1429   prevItem= TREEVIEW_ValidItem (infoPtr, prevSelect);
1430   dummy.x=0;
1431   dummy.y=0;
1432
1433   action= (INT32) wParam;
1434
1435   switch (action) {
1436         case TVGN_CARET: 
1437             if (TREEVIEW_SendTreeviewNotify (wndPtr, TVN_SELCHANGING, TVC_BYMOUSE, 
1438                                                                                 prevSelect, newSelect,dummy)) 
1439                         return FALSE;       /* FIXME: OK? */
1440                 
1441             if (prevItem) prevItem->state &= ~TVIS_SELECTED;
1442                 infoPtr->selectedItem=newSelect;
1443                 wineItem->state |=TVIS_SELECTED;
1444                 TREEVIEW_SendTreeviewNotify (wndPtr, TVN_SELCHANGED, 
1445                                 TVC_BYMOUSE, prevSelect, newSelect, dummy);
1446                 break;
1447         case TVGN_DROPHILITE: 
1448                 FIXME (treeview, "DROPHILITE not implemented");
1449                 break;
1450         case TVGN_FIRSTVISIBLE:
1451                 FIXME (treeview, "FIRSTVISIBLE not implemented");
1452                 break;
1453  }
1454  
1455  TREEVIEW_QueueRefresh (wndPtr);
1456   
1457  return TRUE;
1458 }
1459
1460
1461
1462 /* FIXME: does KEYDOWN also send notifications?? If so, use 
1463    TREEVIEW_SelectItem.
1464 */
1465    
1466
1467 static LRESULT
1468 TREEVIEW_KeyDown (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1469 {
1470  TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1471  TREEVIEW_ITEM *prevItem,*newItem;
1472  int prevSelect;
1473
1474
1475  TRACE (treeview,"%x %lx\n",wParam, lParam);
1476  prevSelect=infoPtr->selectedItem;
1477  if (!prevSelect) return FALSE;
1478
1479  prevItem= TREEVIEW_ValidItem (infoPtr, prevSelect);
1480  
1481  newItem=NULL;
1482  switch (wParam) {
1483         case VK_UP: 
1484                 newItem=TREEVIEW_GetPrevListItem (infoPtr, prevItem);
1485                 if (!newItem) 
1486                         newItem=& infoPtr->items[infoPtr->TopRootItem];
1487                 break;
1488         case VK_DOWN: 
1489                 newItem=TREEVIEW_GetNextListItem (infoPtr, prevItem);
1490                 if (!newItem) newItem=prevItem;
1491                 break;
1492         case VK_HOME:
1493                 newItem=& infoPtr->items[infoPtr->TopRootItem];
1494                 break;
1495         case VK_END:
1496                 newItem=TREEVIEW_GetLastListItem (infoPtr);
1497                 break;
1498         case VK_PRIOR:
1499         case VK_NEXT:
1500         case VK_BACK:
1501         case VK_RETURN:
1502                 FIXME (treeview, "%x not implemented\n", wParam);
1503                 break;
1504  }
1505
1506  if (!newItem) return FALSE;
1507
1508  if (prevItem!=newItem) {
1509         prevItem->state &= ~TVIS_SELECTED;
1510         newItem->state |= TVIS_SELECTED;
1511         infoPtr->selectedItem=newItem->hItem;
1512         TREEVIEW_QueueRefresh (wndPtr);
1513         return TRUE;
1514  }
1515
1516  return FALSE;
1517 }
1518
1519
1520
1521 static LRESULT
1522 TREEVIEW_VScroll (WND *wndPtr, WPARAM32 wParam, LPARAM lParam)
1523
1524 {
1525   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1526   int maxHeight;
1527
1528   TRACE (treeview,"wp %x, lp %lx\n", wParam, lParam);
1529   if (!infoPtr->uInternalStatus & TV_VSCROLL) return FALSE;
1530
1531   switch (LOWORD (wParam)) {
1532         case SB_LINEUP: 
1533                         if (!infoPtr->cy) return FALSE;
1534                         infoPtr->cy -= infoPtr->uRealItemHeight;
1535                         if (infoPtr->cy < 0) infoPtr->cy=0;
1536                         break;
1537         case SB_LINEDOWN: 
1538                         maxHeight=infoPtr->uTotalHeight-infoPtr->uVisibleHeight;
1539                         if (infoPtr->cy == maxHeight) return FALSE;
1540                         infoPtr->cy += infoPtr->uRealItemHeight;
1541                         if (infoPtr->cy > maxHeight) 
1542                                 infoPtr->cy = maxHeight;
1543                         break;
1544         case SB_PAGEUP: 
1545                         if (!infoPtr->cy) return FALSE;
1546                         infoPtr->cy -= infoPtr->uVisibleHeight;
1547                         if (infoPtr->cy < 0) infoPtr->cy=0;
1548                         break;
1549         case SB_PAGEDOWN:
1550                         maxHeight=infoPtr->uTotalHeight-infoPtr->uVisibleHeight;
1551                         if (infoPtr->cy == maxHeight) return FALSE;
1552                         infoPtr->cy += infoPtr->uVisibleHeight;
1553             if (infoPtr->cy > maxHeight)
1554                 infoPtr->cy = maxHeight;
1555                         break;
1556         case SB_THUMBTRACK: 
1557                         infoPtr->cy = HIWORD (wParam);
1558                         break;
1559                         
1560   }
1561   
1562   TREEVIEW_QueueRefresh (wndPtr);
1563   return TRUE;
1564 }
1565
1566 static LRESULT
1567 TREEVIEW_HScroll (WND *wndPtr, WPARAM32 wParam, LPARAM lParam) 
1568 {
1569   TREEVIEW_INFO *infoPtr = TREEVIEW_GetInfoPtr(wndPtr);
1570
1571   TRACE (treeview,"wp %lx, lp %x\n", lParam, wParam);
1572         
1573   if (!infoPtr->uInternalStatus & TV_HSCROLL) return FALSE;
1574   return TRUE;
1575 }
1576
1577
1578
1579
1580   LRESULT WINAPI
1581   TREEVIEW_WindowProc (HWND32 hwnd, UINT32 uMsg, WPARAM32 wParam, LPARAM lParam)
1582   {
1583       WND *wndPtr = WIN_FindWndPtr(hwnd);
1584   
1585   
1586     switch (uMsg) {
1587         case TVM_INSERTITEM32A:
1588           return TREEVIEW_InsertItem32A (wndPtr, wParam, lParam);
1589
1590         case TVM_INSERTITEM32W:
1591                 FIXME (treeview, "Unimplemented msg TVM_INSERTITEM32W\n");
1592                 return 0;
1593
1594         case TVM_DELETEITEM:
1595                 return TREEVIEW_DeleteItem (wndPtr, wParam, lParam);
1596
1597         case TVM_EXPAND:
1598                 return TREEVIEW_Expand (wndPtr, wParam, lParam);
1599
1600         case TVM_GETITEMRECT:
1601                 return TREEVIEW_GetItemRect (wndPtr, wParam, lParam);
1602
1603         case TVM_GETCOUNT:
1604                 return TREEVIEW_GetCount (wndPtr, wParam, lParam);
1605
1606         case TVM_GETINDENT:
1607                 return TREEVIEW_GetIndent (wndPtr, wParam, lParam);
1608
1609         case TVM_SETINDENT:
1610                 return TREEVIEW_SetIndent (wndPtr, wParam, lParam);
1611
1612         case TVM_GETIMAGELIST:
1613                 return TREEVIEW_GetImageList (wndPtr, wParam, lParam);
1614
1615                 case TVM_SETIMAGELIST:
1616                 return TREEVIEW_SetImageList (wndPtr, wParam, lParam);
1617
1618         case TVM_GETNEXTITEM:
1619                 return TREEVIEW_GetNextItem32 (wndPtr, wParam, lParam);
1620
1621         case TVM_SELECTITEM:
1622                 return TREEVIEW_SelectItem (wndPtr, wParam, lParam);
1623
1624         case TVM_GETITEM32A:
1625                 return TREEVIEW_GetItem (wndPtr, wParam, lParam);
1626
1627         case TVM_GETITEM32W:
1628                 FIXME (treeview, "Unimplemented msg TVM_GETITEM32W\n");
1629                 return 0;
1630
1631         case TVM_SETITEM32A:
1632                 return TREEVIEW_SetItem (wndPtr, wParam, lParam);
1633
1634         case TVM_SETITEM32W:
1635                 FIXME (treeview, "Unimplemented msg TVM_SETITEMW\n");
1636                 return 0;
1637
1638         case TVM_EDITLABEL32A:
1639                 FIXME (treeview, "Unimplemented msg TVM_EDITLABEL32A \n");
1640                 return 0;
1641
1642         case TVM_EDITLABEL32W:
1643                 FIXME (treeview, "Unimplemented msg TVM_EDITLABEL32W \n");
1644                 return 0;
1645
1646         case TVM_GETEDITCONTROL:
1647                 FIXME (treeview, "Unimplemented msg TVM_GETEDITCONTROL\n");
1648                 return 0;
1649
1650         case TVM_GETVISIBLECOUNT:
1651                 return TREEVIEW_GetVisibleCount (wndPtr, wParam, lParam);
1652
1653         case TVM_HITTEST:
1654                 return TREEVIEW_HitTest32 (wndPtr, lParam);
1655
1656         case TVM_CREATEDRAGIMAGE:
1657                 FIXME (treeview, "Unimplemented msg TVM_CREATEDRAGIMAGE\n");
1658                 return 0;
1659   
1660         case TVM_SORTCHILDREN:
1661                 FIXME (treeview, "Unimplemented msg TVM_SORTCHILDREN\n");
1662                 return 0;
1663   
1664         case TVM_ENSUREVISIBLE:
1665                 FIXME (treeview, "Unimplemented msg TVM_ENSUREVISIBLE\n");
1666                 return 0;
1667   
1668         case TVM_SORTCHILDRENCB:
1669                 FIXME (treeview, "Unimplemented msg TVM_SORTCHILDRENCB\n");
1670                 return 0;
1671   
1672         case TVM_ENDEDITLABELNOW:
1673                 FIXME (treeview, "Unimplemented msg TVM_ENDEDITLABELNOW\n");
1674                 return 0;
1675   
1676         case TVM_GETISEARCHSTRING32A:
1677                 FIXME (treeview, "Unimplemented msg TVM_GETISEARCHSTRING32A\n");
1678                 return 0;
1679   
1680         case TVM_GETISEARCHSTRING32W:
1681                 FIXME (treeview, "Unimplemented msg TVM_GETISEARCHSTRING32W\n");
1682                 return 0;
1683   
1684         case TVM_SETTOOLTIPS:
1685                 FIXME (treeview, "Unimplemented msg TVM_SETTOOLTIPS\n");
1686                 return 0;
1687   
1688         case TVM_GETTOOLTIPS:
1689                 FIXME (treeview, "Unimplemented msg TVM_GETTOOLTIPS\n");
1690                 return 0;
1691   
1692         case TVM_SETINSERTMARK:
1693                 FIXME (treeview, "Unimplemented msg TVM_SETINSERTMARK\n");
1694                 return 0;
1695   
1696         case TVM_SETITEMHEIGHT:
1697                 return TREEVIEW_SetItemHeight (wndPtr, wParam);
1698   
1699         case TVM_GETITEMHEIGHT:
1700                 return TREEVIEW_GetItemHeight (wndPtr);
1701   
1702         case TVM_SETBKCOLOR:
1703                 FIXME (treeview, "Unimplemented msg TVM_SETBKCOLOR\n");
1704                 return 0;
1705         
1706         case TVM_SETTEXTCOLOR:
1707                 return TREEVIEW_SetTextColor (wndPtr, wParam, lParam);
1708   
1709         case TVM_GETBKCOLOR:
1710                 FIXME (treeview, "Unimplemented msg TVM_GETBKCOLOR\n");
1711                 return 0;
1712   
1713         case TVM_GETTEXTCOLOR:
1714                 return TREEVIEW_GetTextColor (wndPtr);
1715   
1716         case TVM_SETSCROLLTIME:
1717                 FIXME (treeview, "Unimplemented msg TVM_SETSCROLLTIME\n");
1718                 return 0;
1719   
1720         case TVM_GETSCROLLTIME:
1721                 FIXME (treeview, "Unimplemented msg TVM_GETSCROLLTIME\n");
1722                 return 0;
1723   
1724         case TVM_SETINSERTMARKCOLOR:
1725                 FIXME (treeview, "Unimplemented msg TVM_SETINSERTMARKCOLOR\n");
1726                 return 0;
1727   
1728         case TVM_SETUNICODEFORMAT:
1729                 FIXME (treeview, "Unimplemented msg TVM_SETUNICODEFORMAT\n");
1730                 return 0;
1731   
1732         case TVM_GETUNICODEFORMAT:
1733                 FIXME (treeview, "Unimplemented msg TVM_GETUNICODEFORMAT\n");
1734                 return 0;
1735   
1736 //              case WM_COMMAND:
1737   
1738                 case WM_CREATE:
1739                         return TREEVIEW_Create (wndPtr, wParam, lParam);
1740   
1741                 case WM_DESTROY:
1742                         return TREEVIEW_Destroy (wndPtr);
1743   
1744 //              case WM_ENABLE:
1745   
1746                 case WM_ERASEBKGND:
1747                 return TREEVIEW_EraseBackground (wndPtr, wParam, lParam);
1748   
1749                 case WM_GETDLGCODE:
1750                 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1751   
1752                 case WM_PAINT:
1753                 return TREEVIEW_Paint (wndPtr, wParam);
1754   
1755 //              case WM_GETFONT:
1756 //              case WM_SETFONT:
1757   
1758                 case WM_KEYDOWN:
1759                         return TREEVIEW_KeyDown (wndPtr, wParam, lParam);
1760   
1761   
1762 //              case WM_KILLFOCUS:
1763 //              case WM_SETFOCUS:
1764   
1765   
1766                 case WM_LBUTTONDOWN:
1767                         return TREEVIEW_LButtonDown (wndPtr, wParam, lParam);
1768   
1769                 case WM_LBUTTONDBLCLK:
1770                         return TREEVIEW_LButtonDoubleClick (wndPtr, wParam, lParam);
1771   
1772                 case WM_RBUTTONDOWN:
1773                         return TREEVIEW_RButtonDown (wndPtr, wParam, lParam);
1774   
1775   
1776 //              case WM_SYSCOLORCHANGE:
1777 //              case WM_STYLECHANGED:
1778 //              case WM_SETREDRAW:
1779   
1780                 case WM_TIMER:
1781                         return TREEVIEW_HandleTimer (wndPtr, wParam, lParam);
1782   
1783 //              case WM_SIZE:
1784                 case WM_HSCROLL: 
1785                         return TREEVIEW_HScroll (wndPtr, wParam, lParam);
1786                 case WM_VSCROLL: 
1787                         return TREEVIEW_VScroll (wndPtr, wParam, lParam);
1788   
1789                 default:
1790                 if (uMsg >= WM_USER)
1791                 FIXME (treeview, "Unknown msg %04x wp=%08x lp=%08lx\n",
1792                      uMsg, wParam, lParam);
1793             return DefWindowProc32A (hwnd, uMsg, wParam, lParam);
1794       }
1795     return 0;
1796 }
1797
1798
1799 VOID
1800 TREEVIEW_Register (VOID)
1801 {
1802     WNDCLASS32A wndClass;
1803
1804     TRACE (treeview,"\n");
1805
1806     if (GlobalFindAtom32A (WC_TREEVIEW32A)) return;
1807
1808     ZeroMemory (&wndClass, sizeof(WNDCLASS32A));
1809     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
1810     wndClass.lpfnWndProc   = (WNDPROC32)TREEVIEW_WindowProc;
1811     wndClass.cbClsExtra    = 0;
1812     wndClass.cbWndExtra    = sizeof(TREEVIEW_INFO *);
1813     wndClass.hCursor       = LoadCursor32A (0, IDC_ARROW32A);
1814     wndClass.hbrBackground = 0;
1815     wndClass.lpszClassName = WC_TREEVIEW32A;
1816  
1817     RegisterClass32A (&wndClass);
1818 }
1819
1820
1821 VOID
1822 TREEVIEW_Unregister (VOID)
1823 {
1824     if (GlobalFindAtom32A (WC_TREEVIEW32A))
1825         UnregisterClass32A (WC_TREEVIEW32A, (HINSTANCE32)NULL);
1826 }
1827