View Javadoc

1   /**
2    * 
3    */
4   package org.sirius.server.win32.classes.controls;
5   
6   import java.rmi.RemoteException;
7   import java.util.Arrays;
8   import java.util.List;
9   
10  import javax.jws.WebService;
11  
12  import org.sirius.server.win32.classes.Common;
13  import org.sirius.server.win32.constants.IMEMConsts;
14  import org.sirius.server.win32.constants.ITabControlConsts;
15  import org.sirius.server.win32.constants.IThreadConsts;
16  import org.sirius.server.win32.constants.IWMConsts;
17  import org.sirius.server.win32lib.controls.Win32LibControlsClient;
18  
19  import com.sun.jna.Pointer;
20  import com.sun.jna.Structure;
21  import com.sun.jna.platform.win32.WinNT.HANDLE;
22  
23  /**
24   * @author Myk Kolisnyk
25   * 
26   */
27  @WebService
28  public class TabControl extends Common implements ITabControlConsts,
29          IThreadConsts, IMEMConsts, IWMConsts {
30  
31      public class NMHDR extends Structure {
32          public HWND hwndFrom;
33          public int  idFrom;
34          public int  code;
35  
36          /*
37           * (non-Javadoc)
38           * 
39           * @see com.sun.jna.Structure#getFieldOrder()
40           */
41          @Override
42          protected List getFieldOrder() {
43              // TODO Auto-generated method stub
44              return Arrays.asList(new String[] { "hwndFrom", "idFrom", "code" });
45          }
46      }
47  
48      public static class TC_HITTESTINFO extends Structure {
49          public POINT pt;
50          public int   flags;
51  
52          /*
53           * (non-Javadoc)
54           * 
55           * @see com.sun.jna.Structure#getFieldOrder()
56           */
57          @Override
58          protected List getFieldOrder() {
59              return Arrays.asList(new String[] { "pt", "flags" });
60          }
61      }
62  
63      public static class TC_ITEM extends Structure {
64          public int    mask;
65  
66          public int    dwState;
67          public int    dwStateMask;
68  
69          public String pszText;
70          public int    cchTextMax;
71          public int    iImage;
72          public LPARAM lParam;
73  
74          public TC_ITEM() {
75              // cchTextMax=255;
76              pszText = null;
77          }
78  
79          /*
80           * (non-Javadoc)
81           * 
82           * @see com.sun.jna.Structure#getFieldOrder()
83           */
84          @Override
85          protected List getFieldOrder() {
86              return Arrays
87                      .asList(new String[] { "mask", "dwState", "dwStateMask",
88                              "pszText", "cchTextMax", "iImage", "lParam" });
89          }
90      }
91  
92      Win32LibControlsClient win32lib;
93  
94      /**
95  	 * 
96  	 */
97      public TabControl() {
98          win32lib = new Win32LibControlsClient();
99      }
100 
101     public int GetCurFocus(final long hwndCtl) {
102         return sendMessage(hwndCtl, TCM_GETCURFOCUS, 0, 0);
103     }
104 
105     public int GetCurSel(final long hwndCtl) {
106         HWND hWnd = longToHwnd(hwndCtl);
107         WPARAM wParam = new WPARAM(0);
108         LPARAM lParam = new LPARAM(0);
109         return getUser32().SendMessage(hWnd, TCM_GETCURSEL, wParam, lParam);
110     }
111 
112     public TC_ITEM GetItem(final long hwndCtl, final int index) {
113         HWND hWnd = longToHwnd(hwndCtl);
114         TC_ITEM item = new TC_ITEM();
115         //WPARAM wParam = new WPARAM(index);
116 
117         this.initCommonControls();
118 
119         int dwThreadId = getUser32().GetWindowThreadProcessId(hWnd, null);
120         HANDLE threadHandle = getKernel32().OpenThread(TH_READ_CONTROL, true,
121                 dwThreadId);
122 
123         item.mask = TCIF_TEXT;
124         item.cchTextMax = 255;
125         item.pszText = new String();
126 
127         getKernel32().VirtualAllocEx(threadHandle, item.getPointer(), item.size(),
128                 new DWORD(MEM_COMMIT), new DWORD(MEM_LARGE_PAGES));
129 
130         LPARAM lParam = new LPARAM(Pointer.nativeValue(item.getPointer()));
131 
132         //int ret = getUser32().SendMessage(hWnd, TCM_GETITEMW, wParam, lParam);
133 
134         // item.dwState = pt.getInt(0);
135         Pointer pt = new Pointer(lParam.longValue());
136         for (int i = 0; i < item.size(); i++) {
137             System.out.println("Position " + i + ": " + pt.getInt(i));
138         }
139 
140         return item;
141     }
142 
143     public int GetItemCount(final long hwndCtl) throws RemoteException {
144         //return sendMessage(hwndCtl, TCM_GETITEMCOUNT, 0, 0);
145         return win32lib.tab().getItemsCount((int) hwndCtl);
146     }
147 
148     public String[] GetItemNames(final long hwndCtl) {
149         try {
150             return win32lib.tab().getItemNames((int) hwndCtl);
151         } catch (RemoteException e) {
152             // TODO Auto-generated catch block
153             e.printStackTrace();
154         }
155         return null;
156     }
157 
158     public RECT GetItemRect(final long hwndCtl, final int index) {
159         HWND hWnd = longToHwnd(hwndCtl);
160         RECT item = new RECT();
161         WPARAM wParam = new WPARAM(index);
162 
163         Pointer pt = item.getPointer();
164         LPARAM lParam = new LPARAM(Pointer.nativeValue(pt));
165         getUser32().SendMessage(hWnd, TCM_GETITEMRECT, wParam, lParam);
166         return item;
167     }
168 
169     /*
170      * public HWND GetToolTips(w){ (HWND)SNDMSG((w),TCM_GETTOOLTIPS,0,0) }
171      */
172 
173     public int GetRowCount(final long hwndCtl) {
174         return sendMessage(hwndCtl, TCM_GETROWCOUNT, 0, 0);
175     }
176 
177     public String GetSelectedItem(final long hwndCtl) {
178         try {
179             return win32lib.tab().getSelectedItem((int) hwndCtl);
180         } catch (RemoteException e) {
181             // TODO Auto-generated catch block
182             e.printStackTrace();
183         }
184         return null;
185     }
186 
187     public TC_HITTESTINFO HitTest(final long hwndCtl) {
188         HWND hWnd = longToHwnd(hwndCtl);
189         TC_HITTESTINFO item = new TC_HITTESTINFO();
190         WPARAM wParam = new WPARAM(0);
191 
192         Pointer pt = item.getPointer();
193         LPARAM lParam = new LPARAM(Pointer.nativeValue(pt));
194         getUser32().SendMessage(hWnd, TCM_HITTEST, wParam, lParam);
195         return item;
196     }
197 
198     public void Select(final long hwndCtl, final String tabName) {
199         try {
200             win32lib.tab().selectByName((int) hwndCtl, tabName);
201         } catch (RemoteException e) {
202             // TODO Auto-generated catch block
203             e.printStackTrace();
204         }
205     }
206 
207     public void SelectByIndex(final long hwndCtl, final int index) {
208         try {
209             win32lib.tab().selectByIndex((int) hwndCtl, index);
210         } catch (RemoteException e) {
211             // TODO Auto-generated catch block
212             e.printStackTrace();
213         }
214     }
215 
216     public void SetCurFocus(final long hwndCtl, final int index) {
217         sendMessage(hwndCtl, TCM_SETCURFOCUS, index, 0);
218     }
219 
220     public void SetCurSel(final long hwndCtl, final int index) {
221         try {
222             win32lib.tab().selectByIndex((int) hwndCtl, index);
223         } catch (RemoteException e) {
224             // TODO Auto-generated catch block
225             e.printStackTrace();
226         }
227     }
228 }