1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  ITEMWIN.H                                */
4 /*                                                                           */
5 /* (C) 1993-96  Ullrich von Bassewitz                                        */
6 /*              Wacholderweg 14                                              */
7 /*              D-70597 Stuttgart                                            */
8 /* EMail:       uz@ibb.schwaben.com                                          */
9 /*                                                                           */
10 /*****************************************************************************/
11 
12 
13 
14 // $Id$
15 //
16 // $Log$
17 //
18 //
19 
20 
21 
22 #ifndef _ITEMWIN_H
23 #define _ITEMWIN_H
24 
25 
26 
27 #include "keydef.h"
28 #include "event.h"
29 #include "window.h"
30 
31 
32 
33 /*****************************************************************************/
34 /*                                   Data                                    */
35 /*****************************************************************************/
36 
37 
38 
39 // Highest value for any item ID used by the application
40 // Legal values are 1 <= ID <= MaxUserID
41 const i16 MaxUserID             = 0x7FFD;
42 
43 
44 // Flags
45 const u16 ifInactive            = 0x0001;       // Item is inactive
46 const u16 ifGrayed              = 0x0002;       // Item is grayed
47 const u16 ifSelected            = 0x0004;       // Item is selected
48 const u16 ifVertical            = 0x0008;       // Reserved
49 const u16 ifNoSub               = 0x0010;       // Don't show submenue
50 
51 
52 
53 /*****************************************************************************/
54 /*                             class WindowItem                              */
55 /*****************************************************************************/
56 
57 class ItemWindow;
58 
59 class WindowItem : public Streamable {
60 
61     friend class ResEditApp;                    // Resource editor is a friend
62     friend class ItemWindow;
63 
64     friend inline WindowItem* SetAccelKey (WindowItem* Item, Key AccelKey);
65     // This is a friend of class WindowItem and a short and convienient way to
66     // set the accel key when constructing a WindowItem. Just pass the created
67     // item through this function, the accel key is set and the item is returned.
68 
69 
70 protected:
71     ListNode<WindowItem>        INode;          // double linked list of items
72     ItemWindow*                 Owner;          // Owner window
73     u16                         ItemX, ItemY;   // Window position
74     u16                         ItemWidth;      // Entry width
75 
76     i16                         ID;             // item ID
77     u16                         Flags;          // Flag word
78 
79     Key                         HotKey;         //
80     i16                         HotPos;         // Position of the hotkey
81     Key                         AccelKey;       // Accelerator key
82 
83     String                      ItemText;       //
84     String                      HelpKey;        // Key for the online help
85 
86 
87     virtual void SelectNew (WindowItem* NewItem);
88     // Deselect the selected item and set NewItem as newly selected item.
89     // Checks if this == NewItem and ignores a request in this case
90 
91     virtual WindowItem* SelectNext ();
92     // Select the next item in the list. Return the item that is selected
93     // after the operation has been performed.
94 
95     virtual WindowItem* SelectPrev ();
96     // Select the previous item in the list. Return the item that is selected
97     // after the operation has been performed.
98 
99     void RegisterKey ();
100     // If the item is active and has an accel key: Register the key at the
101     // current thread.
102 
103     void UnregisterKey ();
104     // If the item is active and has an accel key: Unregister the key at the
105     // current thread.
106 
107     WindowItem (StreamableInit);
108     // Build constructor
109 
110 
111 public:
112     WindowItem (const String& aItemText, i16 aID, WindowItem* NextItem);
113 
114     // Derived from class Streamable
115     virtual void Store (Stream&) const;
116     virtual void Load (Stream&);
117     virtual u16 StreamableID () const;
118     static Streamable* Build ();
119 
120     // New functions
121     void SetOwner (ItemWindow* aOwner);
122     virtual void SetPos (u16 X, u16 Y);
123     void SetPos (Point P);
124     virtual void SetWidth (u16 NewWidth);
125     void SetHelpKey (const String& NewKey);
126     const String& GetHelpKey () const;
127     void SetAccelKey (Key NewNewKey);
128     void SetItemText (const String& aItemText);
129 
130     // Access class data
131     ItemWindow* GetOwner () const;
132     virtual u16 GetWidth () const;
133     virtual u16 MinWidth ();
134     u16 XPos () const;
135     u16 YPos () const;
136     Point Pos () const;
137     Key GetHotKey () const;
138     Key GetAccelKey () const;
139     const String& GetItemText () const;
140     virtual i16 GetID ();
141 
142     // Draw and clear the item and the item text
143     virtual void Draw ();
144     virtual void Clear ();
145     virtual void DrawItemText ();
146     virtual void ClearItemText ();
147 
148     // Change the status
149     virtual void Activate ();
150     virtual void Deactivate ();
151     virtual void Gray ();
152     virtual void Select ();
153     virtual void Deselect ();
154 
155     // Check the status
156     int IsSelected ();
157     int IsActive ();
158     int IsGrayed ();
159     int HasHelp ();
160 
161     // Call the help function with the help key of this item
162     virtual void CallHelp ();
163 
164     // Choose an entry
165     virtual i16 Choose ();
166 
167     // Locate items
168     virtual WindowItem* ItemWithID (i16 aID);
169     virtual WindowItem* ItemWithAccelKey (Key aAccelKey);
170     virtual WindowItem* ItemWithHotKey (Key aHotKey);
171 
172     ItemWindow* GetRootWindow ();
173     // Get the root of all windows in the current chain
174 
175 };
176 
177 
178 
179 
WindowItem(StreamableInit)180 inline WindowItem::WindowItem (StreamableInit) :
181     INode (this),
182     ItemText (Empty),
183     HelpKey (Empty)
184 {
185 }
186 
187 
188 
SetOwner(ItemWindow * aOwner)189 inline void WindowItem::SetOwner (ItemWindow* aOwner)
190 {
191     Owner = aOwner;
192 }
193 
194 
195 
SetPos(Point P)196 inline void WindowItem::SetPos (Point P)
197 {
198     SetPos (P.X, P.Y);
199 }
200 
201 
202 
SetHelpKey(const String & NewKey)203 inline void WindowItem::SetHelpKey (const String& NewKey)
204 {
205     HelpKey = NewKey;
206 }
207 
208 
209 
GetHelpKey()210 inline const String& WindowItem::GetHelpKey () const
211 {
212     return HelpKey;
213 }
214 
215 
216 
SetAccelKey(Key NewAccelKey)217 inline void WindowItem::SetAccelKey (Key NewAccelKey)
218 {
219     AccelKey = NewAccelKey;
220 }
221 
222 
223 
GetOwner()224 inline ItemWindow* WindowItem::GetOwner () const
225 {
226     return Owner;
227 }
228 
229 
230 
XPos()231 inline u16 WindowItem::XPos () const
232 {
233     return ItemX;
234 }
235 
236 
237 
YPos()238 inline u16 WindowItem::YPos () const
239 {
240     return ItemY;
241 }
242 
243 
244 
Pos()245 inline Point WindowItem::Pos () const
246 {
247     return Point (ItemX, ItemY);
248 }
249 
250 
251 
GetHotKey()252 inline Key WindowItem::GetHotKey () const
253 {
254     return HotKey;
255 }
256 
257 
258 
GetAccelKey()259 inline Key WindowItem::GetAccelKey () const
260 {
261     return AccelKey;
262 }
263 
264 
265 
GetItemText()266 inline const String& WindowItem::GetItemText () const
267 {
268     return ItemText;
269 }
270 
271 
272 
IsSelected()273 inline int WindowItem::IsSelected ()
274 {
275     return (Flags & ifSelected) != 0;
276 }
277 
278 
279 
IsActive()280 inline int WindowItem::IsActive ()
281 {
282     return (Flags & ifInactive) == 0;
283 }
284 
285 
286 
IsGrayed()287 inline int WindowItem::IsGrayed ()
288 {
289     return (Flags & ifGrayed) != 0;
290 }
291 
292 
293 
HasHelp()294 inline int WindowItem::HasHelp ()
295 {
296     return HelpKey.Len () > 0;
297 }
298 
299 
300 
SetAccelKey(WindowItem * Item,Key AccelKey)301 inline WindowItem* SetAccelKey (WindowItem* Item, Key AccelKey)
302 // This is a friend of class WindowItem and a short and convienient way to
303 // set the accel key when constructing a WindowItem. Just pass the created
304 // item through this function, the accel key is set and the item is returned.
305 {
306     Item->AccelKey = AccelKey;
307     return Item;
308 }
309 
310 
311 
312 /*****************************************************************************/
313 /*                             class ItemWindow                              */
314 /*****************************************************************************/
315 
316 
317 
318 class ItemWindow : public Window, public EventHandler {
319 
320     friend class ResEditApp;                    // Resource editor is a friend
321 
322 private:
323     // Data structures for use in Traverse
324     struct FindStruc1 {
325         i16 ID;
326         WindowItem* Item;
327     };
328     struct FindStruc2 {
329         Key K;
330         WindowItem* Item;
331     };
332 
333 
334     static int StoreOneItem (ListNode<WindowItem>*, void*);
335     // Helper function to store a WindowItem into the stream S
336 
337     static int SetOneItemWidth (ListNode<WindowItem>*, void*);
338     // Reset the width of one item
339 
340     static int SetOneOwner (ListNode<WindowItem>*, void*);
341     // Helper function for ItemWindow::SetItemListOwner. Sets the owner window
342     // for all items in the list
343 
344     static int SetOnePos (ListNode<WindowItem>*, void*);
345     // Helper function for ItemWindow::SetPos, sets the position of one item
346 
347     static int DrawOneItem (ListNode<WindowItem>*, void*);
348     // Helper function for DrawItems, draw one item
349 
350     static int CheckSelectedItem (ListNode<WindowItem>*, void*);
351     // Helper function for ValidateSelectedItem, find active item
352 
353     static int FindAccelKey (ListNode<WindowItem>*, void*);
354     // Helper function for ItemWithAccelKey
355 
356     static int FindHotKey (ListNode<WindowItem>*, void*);
357     // Helper function for ItemWithHotKey
358 
359     static int FindID (ListNode<WindowItem>*, void*);
360     // Helper function for ItemWithID
361 
362     static int RegisterOneItemKey (ListNode<WindowItem>*, void*);
363     // Register the accel key of one item
364 
365     static int UnregisterOneItemKey (ListNode<WindowItem>*, void*);
366     // Unregister the accel key of one item
367 
368 
369 protected:
370     u16                         ItemCount;      // Number of items in the list
371     WindowItem*                 Owner;          // Owner of the window
372     WindowItem*                 SelectedItem;   // Pointer to selected item
373     WindowItem*                 FirstItem;      // Pointer to item (list)
374 
375 
376     void SelectNextItem ();
377     // Choose the next item in the item list as selected
378 
379     void SelectPrevItem ();
380     // Choose the previous item in the item list as selected
381 
382     virtual void SetPos ();
383     // Sets the positions of all window items. The items are lined up
384     // vertically at the left window border. To change this behaviour,
385     // overload this function.
386 
387     void SetItemListOwner ();
388     // Traverse through all items and tell them their owner window
389 
390     void DrawItems ();
391     // Draw all items
392 
393     void ValidateSelectedItem ();
394     // Check if the item pointed to by SelectedItem is still active. If
395     // not, a new item is choosen.
396 
397     WindowItem* Traverse (int (*F) (ListNode<WindowItem>*, void*),
398                                      void* DataPtr = NULL) const;
399     // Use ListNode::Traverse to traverse through all window items starting
400     // at FirstItem. Return value corresponds to ListNode::Traverse
401 
402     virtual u32 GetStatusFlags ();
403     // Returns the flags that are used to build the status line in Browse
404 
405     virtual void HandleKey (Key& K);
406     // Key dispatcher used in Browse
407 
408     ItemWindow (const Rect &Bounds, u16 aState, WindowItem* ItemList);
409     // Internal used constructor, leaves with LockCount == 1
410 
411 
412 public:
413     ItemWindow (StreamableInit);
414     ItemWindow (const Rect &Bounds, u16 aState = wfFramed,
415                 unsigned aPalette = paGray,
416                 unsigned Number = 0, WindowItem* ItemList = NULL);
417     virtual ~ItemWindow ();
418 
419     // -- Derived from class Streamable
420 
421     virtual void Store (Stream &) const;
422     virtual void Load (Stream &);
423     virtual u16 StreamableID () const;
424     static Streamable* Build ();
425 
426     // -- Derived from class Window
427 
428     virtual void DrawInterior ();
429     // Draw the window interior. This overrides Window::DrawInterior and
430     // additionally draws the items
431 
432     virtual void Activate ();
433     // Activate the window. This overrides Window::Activate and highlights
434     // the selected item
435 
436     virtual void Deactivate ();
437     // Activate the window. This overrides Window::Activate and deselects
438     // the selected item
439 
440     // -- New functions
441 
442     virtual Key Browse ();
443     // Make the window active and display the window contents in a suitable
444     // manner. This function should be overridden for special derived
445     // windows. It returns the key that ended the browse state.
446 
447     WindowItem* GetOwner ();
448     // Retrieve the pointer to the owner window
449 
450     void SetOwner (WindowItem* aOwner);
451     // Set the pointer to the owner window
452 
453     void SelectNewItem (WindowItem* NewItem);
454     // Select the new item, update SelectedItem
455 
456     void SelectNewItem (i16 NewID);
457     // Select the new item, update SelectedItem
458 
459     void PlaceNear (const Point& Pos);
460     // Place the window near the given absolute position. If there is not enough
461     // room to place the window below the given item, the window is placed above.
462 
463     void PlaceNear (WindowItem* Item);
464     // Place the window below the given Item. Item must not be an item
465     // owned by this window. If there is not enough room to place the
466     // window below the given item, the window is placed above.
467 
468     virtual WindowItem* ItemWithAccelKey (Key aAccelKey);
469     // Try to find an item with the given accelerator key in the tree
470     // below this window. If one is found, a pointer to the item is
471     // returned, otherwise the function returns NULL.
472 
473     virtual WindowItem* ItemWithHotKey (Key aHotKey);
474     // Try to find an item with the given hot key in the tree
475     // below this window. If one is found, a pointer to the item is
476     // returned, otherwise the function returns NULL.
477 
478     virtual WindowItem* ItemWithID (i16 aID);
479     // Try to find an item with the given ID in the tree below this
480     // window. If one is found, a pointer to the item is returned,
481     // otherwise the function returns NULL.
482 
483     virtual WindowItem* ForcedItemWithID (i16 aID);
484     // Acts like ItemWithID but treats the case different, when no
485     // matching ID is found: This is considered as a fatal error.
486 
487     void SetAccelKey (i16 aID, Key aAccelKey);
488     // Sets the accelerator key for the item with the given ID. If no
489     // item with this ID is found, this is considered as a fatal error.
490 
491     Key GetHotKey (i16 aID);
492     // Return the hot key of the item with the given ID. If no such item
493     // is found, this is considered as a fatal error.
494 
495     Key GetAccelKey (i16 aID);
496     // Return the accelerator key of the item with the given ID. If no
497     // such item is found, this is considered as a fatal error.
498 
499     void SetHelpKey (i16 aID, const String& aHelpKey);
500     // Set the key for the help function for the item with the given ID.
501     // If no item with that ID is found, this is handled as a fatal error.
502 
503     void AddItem (WindowItem* Item);
504     // Add the given item to the item list of the window. After that, the
505     // item is owned by the window and destroyed if the window destructor
506     // is called. The given WindowItem is unlinked from the list it is
507     // linked in before it is added to the windows item list.
508 
509     void DeleteItem (WindowItem* Item);
510     // Take the item from the item list and delete it. If Item is the
511     // selected item, a new selected item is choosen. After deleting
512     // Item, the window is redrawn.
513 
514     void DrawItem (i16 aID);
515     // Redraw the item with the given ID.
516 
517     void ActivateItem (i16 aID);
518     // Activate the item with the given ID
519 
520     void DeactivateItem (i16 aID);
521     // Deactivate the item with the given ID
522 
523     void GrayItem (i16 aID);
524     // Gray the item with the given ID
525 
526     void RegisterItemKeys ();
527     // Register the accel keys of all active items
528 
529     void UnregisterItemKeys ();
530     // Unregister the accel keys of all active items
531 
532     virtual int CanClose ();
533     // Return true if the window is allowed to close. This function is a hook
534     // for derived windows, it returns always true.
535 
536     virtual void Zoom ();
537     // Interface function for derived classes. This function is a no op and
538     // must be overloaded
539 
540 };
541 
542 
543 
ItemWindow(StreamableInit X)544 inline ItemWindow::ItemWindow (StreamableInit X) :
545         Window (X)
546 {
547 }
548 
549 
550 
GetOwner()551 inline WindowItem* ItemWindow::GetOwner ()
552 {
553     return Owner;
554 }
555 
556 
557 
SetOwner(WindowItem * aOwner)558 inline void ItemWindow::SetOwner (WindowItem* aOwner)
559 {
560     Owner = aOwner;
561 }
562 
563 
564 
565 // End of ITEMWIN.H
566 
567 #endif
568 
569 
570 
571