• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Source Code --- TreeView example with tooltips

Started by Chris Chancellor, September 16, 2018, 05:59:03 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello all

this is an example which i have modified Roland's Treeview codes and added in tooltips


' TVTooltipsDlg.o2bas
' https://www.oxygenbasic.org/forum/index.php?PHPSESSID=7bqtee8khjqtk12kca4jokulvd&topic=1525.msg19000;topicseen#msg19000
'====================================================================
' Treeview example, modal dialog as main.  Thanxx to Roland

' Added tooltips
'====================================================================

$ filename "TVToolTipsDlg.exe"

uses rtl64

'% review
uses dialogs

uses TooltipsO2

string crlf=chr(13,10)
macro TreeView_GetItem(hwnd,pitem) SendMessage(hwnd, TVM_GETITEM,0, pitem)
macro TreeView_GetNextItem(hwnd,hitem,code) SendMessage(hwnd, TVM_GETNEXTITEM, code, hitem)
macro TreeView_GetSelection(hwnd) TreeView_GetNextItem(hwnd,null,TVGN_CARET)
macro TreeView_InsertItem(hwnd,lpis) SendMessage(hwnd, TVM_INSERTITEM,0, lpis)
macro TreeView_SetItem(hwnd,pitem) SendMessage(hwnd, TVM_SETITEM,0, pitem)

% TVGN_CARET=9
% TVIF_TEXT=1
% TVIF_HANDLE=16
% TVIF_CHILDREN=64
% TVM_INSERTITEM=4352
% TVM_GETNEXTITEM=4362
% TVM_GETITEM=4364
% TVM_SETITEM=4365
% TVN_SELCHANGED= -402
% TVS_HASBUTTONS=1
% TVS_HASLINES=2
% TVS_LINESATROOT=4

type NMHDR  'WinData.inc
  sys   hwndFrom
  sys   idFrom     'UINT_PTR
  uint  code
end type

type TVITEM 
  uint  mask
  sys   hItem 'HTREEITEM
  uint  state
  uint  stateMask
  char* pszText
  int   cchTextMax
  int   iImage
  int   iSelectedImage
  int   cChildren
  sys   lParam
end type
typedef TVITEM TV_ITEM

type TVINSERTSTRUCT 
  sys     hParent      'HTREEITEM
  sys     hInsertAfter 'HTREEITEM
  TV_ITEM item
end type
typedef TVINSERTSTRUCT TV_INSERTSTRUCT


'Identifiers
% IDC_TREEVIEW = 1001
% IDC_MLE      = 1002
% IDC_BUTTON   = 1003

declare sub InitTreeView(sys hWnd, lID, int lCount)

=============================================
'MAIN CODE
=============================================

init_common_controls()

char* cmdline
@cmdline=GetCommandLine()
sys hInstance = GetModuleHandle(null)


function DialogProc( sys hDlg, uint uMsg, sys wParam, lParam ) as int callback
   NMHDR ptnmhdr at lParam
   
   sys hMle=GetDlgItem(hDlg, IDC_MLE)
   sys hButton=GetDlgItem(hDlg, IDC_BUTTON)

   static string buffer, txt


   select case uMsg
 
      case WM_INITDIALOG
         InitTreeView(hDlg,IDC_TREEVIEW, 1)

         SetToolTip( hMle," Help Information",hDlg)
         SetToolTip( hButton," Exit the system",hDlg)


       
      case WM_COMMAND
         select case loword(wParam)
            case IDCANCEL, IDC_BUTTON
               EndDialog( hDlg, null )
        end select

      case WM_NOTIFY
         select case ptnmhdr.idFrom
            case IDC_TREEVIEW
               select case ptnmhdr.code
                 case TVN_SELCHANGED
                   TV_ITEM tTVItem
                               
                   'Get handle of item
                   sys hTV=GetDlgItem(hDlg, IDC_TREEVIEW)                 
                   sys hTVItem = TreeView_GetSelection(hTV)                                             
                   buffer = nuls 128
                   'Get text
                   tTVItem.hItem = hTVItem                 
                   tTVItem.mask = TVIF_TEXT                 
                   tTVItem.cchTextMax = 128                 
                   tTVItem.pszText = buffer                 
                   if TreeView_GetItem(hTV, &tTVItem) then                 
                   if len(tTVItem.pszText) then
                      txt=tTVItem.pszText
                      if txt="Title 1" then
                         txt="Main title" & crlf & crlf &
                         "All this is just a sample TreeView" & crlf &
                         "Just click on each chapter" & crlf &
                         "to see each item"                   
                         SendMessage(hMle, WM_SETTEXT, 0, txt)
                      else   
                         SendMessage(hMle, WM_SETTEXT, 0, txt & " is selected")
                      end if                 
                   end if                     
                   end if             
                 
            end select
         end select
     
      case WM_CLOSE
         EndDialog( hDlg, null )
               
   end select

   return 0
end function



'==========================================
sub winmain()
   uint Style, StyleEx
   
   Dialog( 0, 0, 520*0.6, 320*0.5, "Treeview example with Tooltips in OxygenBasic",
           WS_OVERLAPPEDWINDOW or DS_CENTER or DS_SETFONT,
           8, "MS Sans Serif", WS_EX_CLIENTEDGE)

   Style   = WS_CHILD OR WS_CLIPSIBLINGS OR WS_TABSTOP OR _
             WS_VISIBLE OR TVS_HASBUTTONS OR TVS_HASLINES OR _
             TVS_LINESATROOT
   StyleEx = WS_EX_CLIENTEDGE
   Control("Treeview",  IDC_TREEVIEW, "SysTreeView32", Style,
           10*0.6,10*0.5,190*0.6,270*0.5 , StyleEx )       

   Style   = WS_CHILD OR WS_CLIPSIBLINGS OR WS_TABSTOP OR _
             WS_VISIBLE OR ES_AUTOHSCROLL OR ES_AUTOVSCROLL OR _
             ES_MULTILINE OR ES_WANTRETURN

   Control("",  IDC_MLE, "EDIT", Style,
           216*0.6,10*0.5,290*0.6,180*0.5 , StyleEx )       

   PushButton( "Close" , IDC_BUTTON, 320*0.6, 230*0.5, 100*0.6, 30*0.5 )

   CreateModalDialog( null, @DialogProc, 0)
end sub




'=====================================================
sub TreeViewInsertItem(sys hTree, hParent, string sItem)
   TV_INSERTSTRUCT tTVInsert
   TV_ITEM tTVItem

   if hParent then
     tTVItem.mask        = TVIF_CHILDREN or TVIF_HANDLE
     tTVItem.hItem       = hParent
     tTVItem.cchildren   = 1
     TreeView_SetItem (hTree, tTVItem)
   end if

   tTVInsert.hParent              = hParent
   tTVInsert.Item.mask            = TVIF_TEXT
   tTVInsert.Item.pszText         = strptr(sItem)
   tTVInsert.Item.cchTextMax      = len(sItem)

   TreeView_InsertItem (hTree, &tTVInsert)
end sub



'==================================
sub InitTreeView(sys hWnd, lID, int lCount)
    sys hRoot, hParent
    int i,j,k
    sys hCtl

    hCtl = GetDlgItem(hWnd,lID)
   SetToolTip( hCtl,"Click on the plus sign to open more chapters "  + _
                "to review their help information",hWnd)

    for i = 1 to lCount
       hRoot = TreeViewInsertItem(hCtl, null, "Title " & str(i))
       for j = 1 to 3
          hParent = TreeViewInsertItem(hCtl, hRoot, "Chapter " & str(j))
         
          for k = 1 to 4
             TreeViewInsertItem(hCtl, hParent, "Paragraph " &  str(j) & "." & str(k))
          next k
        next j
    next i
end sub



winmain()






ToolTipsO2.inc  file


'ToolTipsO2.inc

uses corewin
  uses user

#lookahead


' tooltips constants
% TTF_IDISHWND=1
% TTF_SUBCLASS=16
% TTM_ADDTOOL=1028
% TTM_SETTIPBKCOLOR=1043
% TTM_SETTIPTEXTCOLOR=1044
% TTS_ALWAYSTIP=1
% TTS_BALLOON=64

type TOOLINFO
  UINT      cbSize
  UINT      uFlags
  sys       hwnd
  sys       uId    'UINT_PTR
  RECT      rect
  sys       hinst
  char*     lpszText
  sys       lParam
  sys      *lpReserved
end type



'============================
        ' RGB function for O2
         function RGB(int rcc, gcc, bcc) as int
                    return (rcc + gcc*256 + bcc*65536)
        end Function



'=======================================
sub SetToolTip(sys hControl, string TipText, sys hMDlg)

    TOOLINFO TI

    sys hToolTip = CreateWindowEx(0, "tooltips_class32", "", TTS_ALWAYSTIP | %TTS_BALLOON,
                                  0, 0, 0, 0, hMDlg, null, GetModuleHandle(null), null)

    ' set the text color
    SendMessage (hToolTip, TTM_SETTIPTEXTCOLOR, MAGENTA,0)
   ' for the background color
    int AzureColor = RGB(240,255,255)
    SendMessage (hToolTip, TTM_SETTIPBKCOLOR, AzureColor,0)

   ' do NOT use LEN(TI) as use by PB
     TI.cbSize    = sizeof(TI)

     TI.uFlags     = TTF_SUBCLASS | TTF_IDISHWND
     TI.hWnd      = GetParent(hToolTip)
     TI.uId          = hControl
     TI.lpszText  = strptr TipText
     SendMessage (hToolTip, TTM_ADDTOOL, 0, &ti)
end sub