1 namespace gui::controls;
9 #define ROW_HEIGHT listBox.rowHeight /*16*/
12 extern int __ecereVMethodID_class_OnDisplay;
13 extern int __ecereVMethodID_class_OnGetString;
17 class DropBoxBits { bool noHighlight:1, noStipple:1, editText:1, activeColor:1, showNone:1, changeContents:1; };
19 public class DropBox : CommonControl
23 style.activeColor = true;
25 class_property(icon) = "<:ecere>controls/dropBox.png";
29 listBox.foreground = foreground;
33 listBox.background = background;
37 property bool activeStipple
39 property_category "Appearance"
42 style.noStipple = !value;
45 get { return !style.noStipple; }
47 property bool showButton
49 property_category "Appearance"
52 button.visible = value;
53 listBox.borderStyle = value ? contour : none;
55 get { return button.visible; }
57 property Alignment alignment
59 property_category "Appearance"
64 field.alignment = value;
66 get { return alignment; }
68 property bool noHighlight
70 property_category "Appearance"
75 style.noHighlight = value;
76 listBox.fullRowSelect = !value;
80 get { return style.noHighlight; }
82 property bool activeColor
84 property_category "Appearance"
89 style.activeColor = value;
93 get { return style.activeColor; }
95 property DataRow currentRow
97 property_category "Private"
103 listBox.currentRow = value ? value : (style.showNone ? noneRow : null);
104 if(style.editText && style.changeContents)
106 char tempString[4096];
108 editBox.contents = (char *)dataType._vTbl[__ecereVMethodID_class_OnGetString](dataType, currentRow.GetData(null), tempString, null, null);
110 editBox.contents = "";
115 if(editBox.modifiedDocument)
117 NotifyTextEntry(master, this, editBox.contents, false);
118 editBox.modifiedDocument = false;
120 // editBox.Deactivate();
124 get { return currentRow; }
126 property DataRow firstRow { get { return this ? listBox.firstRow : null; } };
127 property char * contents { property_category "Data" set { if(editBox) editBox.contents = value; } get { return editBox ? editBox.contents : null; } };
128 property bool editText
130 property_category "Behavior"
141 this, textHorzScroll = true, borderStyle = 0;
142 anchor = Anchor { left = 0, top = 0, right = BTN_WIDTH, bottom = 0 };
143 // visible = false, modifyVirtualArea = false;
147 button.inactive = false;
149 style.editText = true;
158 button.inactive = true;
160 style.editText = false;
164 get { return style.editText; }
166 property EditBox editBox { get { return editBox; } }
167 property Seconds typingTimeout { property_category "Behavior" set { listBox.typingTimeout = value; } get { return listBox.typingTimeout; } };
168 property int rowHeight { property_category "Appearance" set { listBox.rowHeight = value; } get { return listBox.rowHeight; } };
169 property int maxShown
171 property_category "Behavior"
174 listBoxMaxShown = value;
175 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
177 get { return listBoxMaxShown; }
179 property Window pullDown
181 get { return pulledWindow; }
183 property bool showNone
187 if(value != style.showNone)
191 listBox.DeleteRow(noneRow);
194 style.showNone = value;
197 noneRow = listBox.AddRowNone();
201 get { return style.showNone; }
203 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
204 property bool changeContents
206 set { style.changeContents = value; }
207 get { return style.changeContents; }
210 property ListBox listBox { get { return listBox; } }
213 virtual bool Window::NotifySelect(DropBox dropBox, DataRow row, Modifiers mods);
214 virtual bool Window::NotifyClose(DropBox dropBox);
215 virtual bool Window::NotifyHighlight(DropBox dropBox, DataRow row, Modifiers mods);
216 virtual bool Window::NotifyTextEntry(DropBox dropBox, char * string, bool confirmed);
218 virtual Window OnDropDown()
220 // NASTY BUG ON WINDOWS IN PROPERTIES SHEET IF WE DON'T RECREATE THE LISTBOX HERE
221 // To reproduce, comment out, select borderStyle property, alt-tab to another program, come back, drop down
222 // The listBox goes annoyingly to 0, 0
227 // listBox.visible = true;
231 virtual void OnCloseDropDown(Window pullDown)
233 listBox.visible = false;
236 editBox.ActivateEx(true, false, false, false, null, null);
242 DataRow AddStringf(char * format, ...)
248 char string[MAX_F_STRING];
251 va_start(args, format);
252 vsprintf(string, format, args);
256 row.SetData(null, string);
262 DataRow AddString(char * string)
268 row.SetData(null, string);
274 void DeleteRow(DataRow row)
276 if(!row) row = currentRow;
277 listBox.DeleteRow(row);
279 if(row == currentRow)
284 // Convenience function using current rows
287 return currentRow.tag;
290 void * SetData(DataField field, any_object data)
295 return currentRow.SetData(field, data);
300 any_object GetData(DataField field)
304 // TODO: Fix this shouldn't be required (memguard?)
305 return (void *)currentRow.GetData(field);
310 void Sort(DataField field, int order)
312 listBox.Sort(field, order);
315 void AddField(DataField field)
320 field = DataField { alignment = alignment };
322 listBox.AddField(field);
323 this.field = listBox.firstField;
324 dataType = this.field.dataType;
333 row = listBox.AddRow();
334 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
339 DataRow AddRowAfter(DataRow after)
344 row = listBox.AddRowAfter(after);
345 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
352 return listBox.rowCount;
359 Window master = this.master;
362 if(currentRow && master)
365 NotifySelect(master, this, null, 0);
368 if(style.editText && style.changeContents)
369 editBox.contents = "";
372 noneRow = listBox.AddRowNone();
376 DataRow FindRow(int tag)
380 return listBox.FindRow(tag);
385 DataRow FindSubRow(int tag)
389 return listBox.FindSubRow(tag);
395 void SelectRow(DataRow row)
400 if(style.editText && style.changeContents)
402 char tempString[4096];
404 editBox.contents = (char *)dataType._vTbl[__ecereVMethodID_class_OnGetString](dataType, currentRow.GetData(null), tempString, null, null);
406 editBox.contents = "";
408 listBox.SelectRow(currentRow ? currentRow : (style.showNone ? noneRow : null));
416 if(editBox && editBox.modifiedDocument)
418 NotifyTextEntry(master, this, editBox.contents, true);
420 editBox.modifiedDocument = false;
429 listBoxMaxShown = 1000;
430 field = listBox.firstField;
431 dataType = field.dataType;
432 style.changeContents = true;
443 int rowCount = Min(listBox.rowCount, listBoxMaxShown);
444 int height = rowCount * ROW_HEIGHT;
445 listBox.clientSize = { clientSize.w, height };
448 void OnPosition(int x, int y, int width, int height)
452 int lx = absPosition.x - guiApp.desktop.absPosition.x;
453 int ly = absPosition.y - guiApp.desktop.absPosition.y;
454 int availHeight = pulledWindow.parent.clientSize.h;
455 int height = pulledWindow.clientSize.h;
457 // If it won't fit below but fits above, place it above...
458 if(ly + size.h + height > availHeight && height < ly)
463 if(ly + height > availHeight)
464 height = ((availHeight - ly) / ROW_HEIGHT) * ROW_HEIGHT;
467 if(!pulledWindow.initSize.w)
468 pulledWindow.size.w = size.w;
470 // Align it to the right (pull down button)
471 lx -= pulledWindow.size.w - size.w;
472 pulledWindow.position = { lx, ly };
473 pulledWindow.clientSize = { pulledWindow.clientSize.w, height };
477 bool OnKeyDown(Key key, unichar ch)
485 // ADDED THIS CHECK HERE TO NAVIGATE THROUGH GRID LISTBOX
488 if(eClass_IsDerived(master._class, class(DataBox)))
490 if(eClass_IsDerived(master.master._class, class(ListBox)))
493 else if(eClass_IsDerived(master._class, class(ListBox)))
496 if(key == escape && !pulledWindow)
498 //if(editBox) button.Deactivate();
499 //if(editBox) button.Activate();
502 if(editBox.modifiedDocument)
504 NotifyTextEntry(master, this, editBox.contents, false);
505 editBox.modifiedDocument = false;
507 //editBox.Deactivate();
511 if(style.editText && (key == space || key == right))
516 OnCloseDropDown(pulledWindow);
518 button.checked = false;
519 NotifyClose(master, this);
523 if(editBox.modifiedDocument)
525 NotifyTextEntry(master, this, editBox.contents, false);
526 editBox.modifiedDocument = false;
533 listBox.currentRow = currentRow ? currentRow : (style.showNone ? noneRow : null);
534 pulledWindow = OnDropDown();
535 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
536 if(pulledWindow) pulledWindow.visible = true;
537 button.checked = true;
547 if(editBox.modifiedDocument)
549 NotifyTextEntry(master, this, editBox.contents, true);
550 editBox.modifiedDocument = false;
551 //editBox.Deactivate();
554 // Add code to look through listbox and set current row if listbox is used
562 OnCloseDropDown(pulledWindow);
564 button.checked = false;
567 if(!NotifyClose(master, this))
573 // Moved this from below NotifySelect
574 currentRow = listBox.currentRow;
575 if(currentRow && currentRow.noneRow) currentRow = null;
576 if(style.editText && style.changeContents)
578 char tempString[4096];
580 editBox.contents = (char *)dataType._vTbl[__ecereVMethodID_class_OnGetString](dataType, currentRow.GetData(null), tempString, null, null);
582 editBox.contents = "";
585 NotifySelect(master, this, currentRow, key.modifiers | { closingDropDown = true });
592 // return listBox.OnKeyDown(key, ch);
597 void OnApplyGraphics()
599 button.anchor = { right = 0, top = 0, bottom = 0 };
600 button.size = { guiApp.textMode ? 8 : BTN_WIDTH, 0 };
605 window = this, delay = 0.01;
609 background = active ? red : lime;
610 parent.parent.background = parent.parent.active ? red : lime;
611 guiApp.desktop.Update(null);
616 void OnRedraw(Surface surface)
618 //timer.started = true;
621 if(active && !pulledWindow/*listBox.visible*/)
623 if(style.activeColor)
625 surface.SetBackground(selectionColor ? selectionColor : SELECTION_COLOR);
626 if(!style.noHighlight || !currentRow)
628 surface.Area(0,0, clientSize.w-1,clientSize.h-1);
633 surface.SetForeground(Color { 85,85,85 } );
635 surface.SetForeground((active && style.activeColor && !pulledWindow /*listBox.visible*/) ? SELECTION_TEXT : foreground);
636 surface.TextOpacity(true);
640 DataDisplayFlags displayFlags { active = active, current = true, dropBox = true, selected = true, fullRow = true };
641 dataType._vTbl[__ecereVMethodID_class_OnDisplay](dataType, currentRow.GetData(null), surface, 3,
642 1+(clientSize.h - listBox.rowHeight) / 2, clientSize.w - (button.visible ? button.size.w : 0) - 3,
643 field.userData, alignment, displayFlags);
646 surface.WriteText(2,2, "(none)", 6);
648 if(!style.noStipple && (!style.noHighlight || !currentRow))
650 surface.SetForeground(0xFFFFFF80);
651 if(active && !pulledWindow /*listBox.visible*/)
653 surface.Rectangle(0,0,clientSize.w-1-BTN_WIDTH,clientSize.h-1);
654 surface.SetForeground(black);
655 surface.LineStipple(0xAAAA);
656 surface.Rectangle(0,0,clientSize.w-1-BTN_WIDTH,clientSize.h-1);
657 surface.LineStipple(0);
663 bool OnResizing(int *width, int *height)
667 display.FontExtent(fontObject, "W", 1, null, &rowHeight);
668 rowHeight = Max(rowHeight, 16);
670 if(!*width) *width = Max(*width, rowHeight * 100 / 16);
672 //if(!*width) *width = Max(*width, 100);
673 //if(!*height) *height = Max(*height, 20);
674 if(!*height) *height = Max(*height, rowHeight * 20 / 16);
682 SetInitSize(initSize);
686 bool OnLeftButtonDown(int x, int y, Modifiers mods)
691 OnCloseDropDown(pulledWindow);
692 button.checked = false;
695 if(!NotifyClose(master, this))
700 listBox.currentRow = currentRow ? currentRow : (style.showNone ? noneRow : null);
701 pulledWindow = OnDropDown();
702 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
703 if(pulledWindow) pulledWindow.visible = true;
704 if(listBox.freeSelect)
705 pulledWindow.OnLeftButtonDown(-2,-2, 0);
706 button.checked = true;
711 bool OnKeyHit(Key key, unichar ch)
713 DataRow currentRow = this.currentRow;
715 if(!pulledWindow /*listBox.visible*/)
718 if(style.showNone && !currentRow) currentRow = noneRow;
723 case down: currentRow = currentRow ? currentRow.next : null; break;
725 case up: currentRow = currentRow ? currentRow.prev : null; break;
727 for(c = 0; c<listBoxMaxShown && currentRow && currentRow.next; c++, currentRow = currentRow.next);
730 for(c = 0; c<listBoxMaxShown && currentRow && currentRow.prev; c++, currentRow = currentRow.prev);
733 for(; currentRow && currentRow.next; currentRow = currentRow.next);
736 for(; currentRow && currentRow.prev; currentRow = currentRow.prev);
739 if(!editBox || button.active || !editBox.active || editBox.OnKeyHit(key, ch))
741 return listBox.OnKeyHit(key, ch);
748 if(currentRow.noneRow) currentRow = null;
749 property::currentRow = currentRow;
751 NotifySelect(master, this, currentRow, key.modifiers);
758 if(listBox.vertScroll)
761 listBox.vertScroll.Action(up, 0, key);
762 else if(key == wheelDown)
763 listBox.vertScroll.Action(down, 0, key);
765 return listBox.OnKeyHit(key, ch);
770 bool OnActivate(bool active, Window swap, bool * goOnWithActivation, bool direct)
772 // WAS !active JUST MISSING HERE ?
773 if(!active && style.editText)
775 if(editBox.modifiedDocument)
777 if(editBox) editBox.modifiedDocument = false;
778 NotifyTextEntry(master, this, editBox.contents, true); //false);
779 // MOVED THIS ABOVE TO AVOID INFINITE RECURSION
780 // if(editBox) editBox.modifiedDocument = false;
782 //editBox.Deactivate();
790 this, toggle = true, bevel = true, inactive = true,
791 anchor = Anchor { right = 0, top = 0, bottom = 0 },
792 size = Size { BTN_WIDTH, 0 },
794 bitmap = { "<:ecere>elements/arrowDown.png" };
796 bool NotifyPushed(Button control, int x, int y, Modifiers mods)
800 OnCloseDropDown(pulledWindow);
802 button.checked = false;
803 NotifyClose(master, this);
808 if(editBox && editBox.modifiedDocument)
810 NotifyTextEntry(master, this, editBox.contents, true);
811 editBox.modifiedDocument = false;
813 pulledWindow = OnDropDown();
816 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
817 if(listBox.freeSelect)
818 pulledWindow.OnLeftButtonDown(-2,-2, 0);
819 button.checked = true;
820 pulledWindow.visible = true;
831 master = this, fullRowSelect = true, freeSelect = true, /* noHighlight = style.noHighlight , */
832 interim = true, hasVertScroll = true, borderStyle = contour, position = { absPosition.x, absPosition.y + size.h },
833 size.w = size.w, visible = false;
835 bool NotifyActivate(Window control, bool active, Window swap)
840 if(swap != button && swap != this)
842 if(editBox && editBox.modifiedDocument)
844 NotifyTextEntry(master, this, editBox.contents, true);
845 editBox.modifiedDocument = false;
848 OnCloseDropDown(pulledWindow);
850 button.checked = false;
851 NotifyClose(master, this);
857 bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
861 // Add code to set text to list box contents if it has an editbox
865 OnCloseDropDown(pulledWindow);
867 button.checked = false;
868 mods.closingDropDown = true;
869 if(!NotifyClose(master, this))
872 currentRow = (row && !row.noneRow) ? row : null;
873 if(style.editText && style.changeContents)
875 char tempString[4096];
877 editBox.contents = (char *)dataType._vTbl[__ecereVMethodID_class_OnGetString](dataType, currentRow.GetData(null), tempString, null, null);
879 editBox.contents = "";
884 return NotifySelect(master, this, currentRow, mods);
887 bool NotifyHighlight(ListBox control, DataRow row, Modifiers mods)
889 return NotifyHighlight(master, this, row, mods);
892 bool NotifyKeyDown(ListBox listBox, DataRow selection, Key key, unichar ch)
894 return OnKeyDown(key, ch);
902 listBox.font = { font.faceName, font.size };
915 Color selectionColor;