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";
27 watch(foreground) { listBox.foreground = foreground; if(editBox) editBox.foreground = foreground; };
28 watch(background) { listBox.background = background; if(editBox) editBox.background = background; };
29 watch(selectionColor) { listBox.selectionColor = selectionColor; if(editBox) editBox.selectionColor = selectionColor; };
30 watch(selectionText) { listBox.selectionText = selectionText; if(editBox) editBox.selectionText = selectionText; };
31 watch(opacity) { listBox.opacity = opacity; if(editBox) editBox.opacity = opacity; };
34 property bool activeStipple
36 property_category $"Appearance"
39 style.noStipple = !value;
42 get { return !style.noStipple; }
44 property bool showButton
46 property_category $"Appearance"
49 button.visible = value;
50 listBox.borderStyle = value ? contour : none;
52 get { return button.visible; }
54 property Alignment alignment
56 property_category $"Appearance"
61 field.alignment = value;
63 get { return alignment; }
65 property bool noHighlight
67 property_category $"Appearance"
72 style.noHighlight = value;
73 listBox.fullRowSelect = !value;
77 get { return style.noHighlight; }
79 property bool activeColor
81 property_category $"Appearance"
86 style.activeColor = value;
90 get { return style.activeColor; }
92 property DataRow currentRow
94 property_category $"Private"
100 listBox.currentRow = value ? value : (style.showNone ? noneRow : null);
101 if(style.editText && style.changeContents)
103 char tempString[4096];
105 editBox.contents = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, currentRow.GetData(null), tempString, null, null);
107 editBox.contents = "";
112 if(editBox.modifiedDocument)
114 NotifyTextEntry(master, this, editBox.contents, false);
115 editBox.modifiedDocument = false;
117 // editBox.Deactivate();
121 get { return currentRow; }
123 property DataRow firstRow { get { return this ? listBox.firstRow : null; } };
124 property DataRow lastRow { get { return this ? listBox.lastRow : null; } };
125 property const char * contents { property_category $"Data" set { if(editBox) editBox.contents = value; } get { return editBox ? editBox.contents : null; } };
126 property bool editText
128 property_category $"Behavior"
139 this, textHorzScroll = true, borderStyle = 0;
140 anchor = Anchor { left = 0, top = 0, right = BTN_WIDTH, bottom = 0 };
141 // visible = false, modifyVirtualArea = false;
142 foreground = foreground;
143 background = background;
144 selectionColor = selectionColor;
145 selectionText = selectionText;
149 button.inactive = false;
151 style.editText = true;
160 button.inactive = true;
162 style.editText = false;
166 get { return style.editText; }
168 property EditBox editBox { get { return editBox; } }
169 property Seconds typingTimeout { property_category $"Behavior" set { listBox.typingTimeout = value; } get { return listBox.typingTimeout; } };
170 property int rowHeight { property_category $"Appearance" set { listBox.rowHeight = value; } get { return listBox.rowHeight; } };
171 property int maxShown
173 property_category $"Behavior"
176 listBoxMaxShown = value;
177 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
179 get { return listBoxMaxShown; }
181 property Window pullDown
183 get { return pulledWindow; }
185 property bool showNone
189 if(value != style.showNone)
193 listBox.DeleteRow(noneRow);
196 style.showNone = value;
199 noneRow = listBox.AddRowNone();
203 get { return style.showNone; }
205 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
206 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
207 property bool changeContents
209 set { style.changeContents = value; }
210 get { return style.changeContents; }
213 property ListBox listBox { get { return listBox; } }
215 property int rowCount { get { return listBox.rowCount; } }
218 virtual bool Window::NotifySelect(DropBox dropBox, DataRow row, Modifiers mods);
219 virtual bool Window::NotifyClose(DropBox dropBox);
220 virtual bool Window::NotifyHighlight(DropBox dropBox, DataRow row, Modifiers mods);
221 virtual bool Window::NotifyTextEntry(DropBox dropBox, const char * string, bool confirmed);
223 virtual Window OnDropDown()
225 // NASTY BUG ON WINDOWS IN PROPERTIES SHEET IF WE DON'T RECREATE THE LISTBOX HERE
226 // To reproduce, comment out, select borderStyle property, alt-tab to another program, come back, drop down
227 // The listBox goes annoyingly to 0, 0
232 // listBox.visible = true;
236 virtual void OnCloseDropDown(Window pullDown)
239 listBox.visible = false;
242 editBox.ActivateEx(true, false, false, false, null, null);
249 DataRow AddStringf(const char * format, ...)
255 char string[MAX_F_STRING];
258 va_start(args, format);
259 vsnprintf(string, sizeof(string), format, args);
260 string[sizeof(string)-1] = 0;
264 row.SetData(null, string);
270 DataRow AddString(const char * string)
276 row.SetData(null, string);
282 void DeleteRow(DataRow row)
284 if(!row) row = currentRow;
285 listBox.DeleteRow(row);
287 if(row == currentRow)
292 // Convenience function using current rows
295 return currentRow.tag;
298 void * SetData(DataField field, any_object data)
303 return currentRow.SetData(field, data);
308 any_object GetData(DataField field)
310 return this ? currentRow.GetData(field) : null;
313 void Sort(DataField field, int order)
315 listBox.Sort(field, order);
318 void AddField(DataField field)
323 field = DataField { alignment = alignment };
325 listBox.AddField(field);
326 this.field = listBox.firstField;
327 dataType = this.field.dataType;
336 row = listBox.AddRow();
337 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
342 DataRow AddRowAfter(DataRow after)
347 row = listBox.AddRowAfter(after);
348 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
355 return listBox.rowCount;
362 Window master = this.master;
365 if(currentRow && master)
368 NotifySelect(master, this, null, 0);
371 if(style.editText && style.changeContents)
372 editBox.contents = "";
375 noneRow = listBox.AddRowNone();
379 DataRow FindRow(int64 tag)
383 return listBox.FindRow(tag);
388 DataRow FindSubRow(int64 tag)
392 return listBox.FindSubRow(tag);
398 void SelectRow(DataRow row)
403 if(style.editText && style.changeContents)
405 char tempString[4096];
407 editBox.contents = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, currentRow.GetData(null), tempString, null, null);
409 editBox.contents = "";
411 listBox.SelectRow(currentRow ? currentRow : (style.showNone ? noneRow : null));
419 if(editBox && editBox.modifiedDocument)
421 NotifyTextEntry(master, this, editBox.contents, true);
423 editBox.modifiedDocument = false;
432 listBoxMaxShown = 1000;
433 field = listBox.firstField;
434 dataType = field.dataType;
435 style.changeContents = true;
446 int rowCount = Min(listBox.rowCount, listBoxMaxShown);
447 int height = rowCount * ROW_HEIGHT;
448 listBox.clientSize = { clientSize.w, height };
451 void OnPosition(int x, int y, int width, int height)
455 int lx = absPosition.x - guiApp.desktop.absPosition.x;
456 int ly = absPosition.y - guiApp.desktop.absPosition.y;
457 int availHeight = pulledWindow.parent.clientSize.h;
458 int height = pulledWindow.clientSize.h;
460 // If it won't fit below but fits above, place it above...
461 if(ly + size.h + height > availHeight && height < ly)
466 if(ly + height > availHeight)
467 height = ((availHeight - ly) / ROW_HEIGHT) * ROW_HEIGHT;
470 if(!pulledWindow.initSize.w)
471 pulledWindow.size.w = size.w;
473 // Align it to the right (pull down button)
474 lx -= pulledWindow.size.w - size.w;
475 pulledWindow.position = { lx, ly };
476 pulledWindow.clientSize = { pulledWindow.clientSize.w, height };
480 bool OnKeyDown(Key key, unichar ch)
488 // ADDED THIS CHECK HERE TO NAVIGATE THROUGH GRID LISTBOX
491 if(eClass_IsDerived(master._class, class(DataBox)))
493 if(eClass_IsDerived(master.master._class, class(ListBox)))
496 else if(eClass_IsDerived(master._class, class(ListBox)))
499 if(key == escape && !pulledWindow)
501 //if(editBox) button.Deactivate();
502 //if(editBox) button.Activate();
505 if(editBox.modifiedDocument)
507 NotifyTextEntry(master, this, editBox.contents, false);
508 editBox.modifiedDocument = false;
510 //editBox.Deactivate();
514 if(style.editText && (key == space || key == right))
519 OnCloseDropDown(pulledWindow);
521 button.checked = false;
522 NotifyClose(master, this);
526 if(editBox.modifiedDocument)
528 NotifyTextEntry(master, this, editBox.contents, false);
529 editBox.modifiedDocument = false;
536 listBox.currentRow = currentRow ? currentRow : (style.showNone ? noneRow : null);
537 pulledWindow = OnDropDown();
538 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
539 if(pulledWindow) pulledWindow.visible = true;
540 button.checked = true;
550 if(editBox.modifiedDocument)
552 NotifyTextEntry(master, this, editBox.contents, true);
553 editBox.modifiedDocument = false;
554 //editBox.Deactivate();
557 // Add code to look through listbox and set current row if listbox is used
565 OnCloseDropDown(pulledWindow);
567 button.checked = false;
570 if(!NotifyClose(master, this))
576 // Moved this from below NotifySelect
577 currentRow = listBox.currentRow;
578 if(currentRow && currentRow.noneRow) currentRow = null;
579 if(style.editText && style.changeContents)
581 char tempString[4096];
583 editBox.contents = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, currentRow.GetData(null), tempString, null, null);
585 editBox.contents = "";
588 NotifySelect(master, this, currentRow, key.modifiers | { closingDropDown = true });
595 // return listBox.OnKeyDown(key, ch);
600 void OnApplyGraphics()
602 button.anchor = { right = 0, top = 0, bottom = 0 };
603 button.size = { guiApp.textMode ? 8 : BTN_WIDTH, 0 };
608 window = this, delay = 0.01;
612 background = active ? red : lime;
613 parent.parent.background = parent.parent.active ? red : lime;
614 guiApp.desktop.Update(null);
619 void OnRedraw(Surface surface)
621 //timer.started = true;
624 if(active && !pulledWindow/*listBox.visible*/)
626 if(style.activeColor)
628 surface.SetBackground(selectionColor ? selectionColor : SELECTION_COLOR);
629 if(!style.noHighlight || !currentRow)
631 surface.Area(0,0, clientSize.w-1,clientSize.h-1);
636 surface.SetForeground(Color { 85,85,85 } );
638 surface.SetForeground((active && style.activeColor && !pulledWindow /*listBox.visible*/) ? selectionText : foreground);
639 surface.TextOpacity(true);
643 DataDisplayFlags displayFlags { active = active, current = true, dropBox = true, selected = true, fullRow = true };
644 ((void (*)(void *, void *, void *, int, int, int, void *, uint, uint))(void *)dataType._vTbl[__ecereVMethodID_class_OnDisplay])(dataType, currentRow.GetData(null), surface, 3,
645 1+(clientSize.h - listBox.rowHeight) / 2, clientSize.w - (button.visible ? button.size.w : 0) - 3,
646 field.userData, alignment, displayFlags);
649 surface.WriteText(2,2, "(none)", 6);
651 if(!style.noStipple && (!style.noHighlight || !currentRow))
653 surface.SetForeground(0xFFFFFF80);
654 if(active && !pulledWindow /*listBox.visible*/)
656 surface.Rectangle(0,0,clientSize.w-1-BTN_WIDTH,clientSize.h-1);
657 surface.SetForeground(black);
658 surface.LineStipple(0xAAAA);
659 surface.Rectangle(0,0,clientSize.w-1-BTN_WIDTH,clientSize.h-1);
660 surface.LineStipple(0);
666 bool OnResizing(int *width, int *height)
670 display.FontExtent(fontObject, "W", 1, null, &rowHeight);
671 rowHeight = Max(rowHeight, 16);
673 if(!*width) *width = Max(*width, rowHeight * 100 / 16);
675 //if(!*width) *width = Max(*width, 100);
676 //if(!*height) *height = Max(*height, 20);
677 if(!*height) *height = Max(*height, rowHeight * 20 / 16);
685 SetInitSize(initSize);
689 bool OnLeftButtonDown(int x, int y, Modifiers mods)
694 OnCloseDropDown(pulledWindow);
695 button.checked = false;
698 if(!NotifyClose(master, this))
703 listBox.currentRow = currentRow ? currentRow : (style.showNone ? noneRow : null);
704 pulledWindow = OnDropDown();
705 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
706 if(pulledWindow) pulledWindow.visible = true;
707 if(listBox.freeSelect)
708 pulledWindow.OnLeftButtonDown(-2,-2, 0);
709 button.checked = true;
714 bool OnKeyHit(Key key, unichar ch)
716 DataRow currentRow = this.currentRow;
718 if(!pulledWindow /*listBox.visible*/)
721 if(style.showNone && !currentRow) currentRow = noneRow;
726 case down: currentRow = currentRow ? currentRow.next : null; break;
728 case up: currentRow = currentRow ? currentRow.prev : null; break;
730 for(c = 0; c<listBoxMaxShown && currentRow && currentRow.next; c++, currentRow = currentRow.next);
733 for(c = 0; c<listBoxMaxShown && currentRow && currentRow.prev; c++, currentRow = currentRow.prev);
736 for(; currentRow && currentRow.next; currentRow = currentRow.next);
739 for(; currentRow && currentRow.prev; currentRow = currentRow.prev);
742 if(!editBox || button.active || !editBox.active || editBox.OnKeyHit(key, ch))
744 return listBox.OnKeyHit(key, ch);
751 if(currentRow.noneRow) currentRow = null;
752 property::currentRow = currentRow;
754 NotifySelect(master, this, currentRow, key.modifiers);
761 if(listBox.vertScroll)
764 listBox.vertScroll.Action(up, 0, key);
765 else if(key == wheelDown)
766 listBox.vertScroll.Action(down, 0, key);
768 return listBox.OnKeyHit(key, ch);
773 bool OnActivate(bool active, Window swap, bool * goOnWithActivation, bool direct)
775 // WAS !active JUST MISSING HERE ?
776 if(!active && style.editText)
778 if(editBox.modifiedDocument)
780 if(editBox) editBox.modifiedDocument = false;
781 NotifyTextEntry(master, this, editBox.contents, true); //false);
782 // MOVED THIS ABOVE TO AVOID INFINITE RECURSION
783 // if(editBox) editBox.modifiedDocument = false;
785 //editBox.Deactivate();
793 this, toggle = true, bevel = true, inactive = true,
794 anchor = Anchor { right = 0, top = 0, bottom = 0 },
795 size = Size { BTN_WIDTH, 0 },
797 bitmap = { "<:ecere>elements/arrowDown.png" };
799 bool NotifyPushed(Button control, int x, int y, Modifiers mods)
803 OnCloseDropDown(pulledWindow);
805 button.checked = false;
806 NotifyClose(master, this);
811 if(editBox && editBox.modifiedDocument)
813 NotifyTextEntry(master, this, editBox.contents, true);
814 editBox.modifiedDocument = false;
816 pulledWindow = OnDropDown();
819 OnPosition(position.x, position.y, clientSize.w, clientSize.h);
820 if(listBox.freeSelect)
821 pulledWindow.OnLeftButtonDown(-2,-2, 0);
822 button.checked = true;
823 pulledWindow.visible = true;
834 master = this, fullRowSelect = true, freeSelect = true, /* noHighlight = style.noHighlight , */
835 interim = true, hasVertScroll = true, borderStyle = contour, position = { absPosition.x, absPosition.y + size.h },
836 size.w = size.w, visible = false;
838 bool NotifyActivate(Window control, bool active, Window swap)
843 if(swap != button && swap != this)
845 if(editBox && editBox.modifiedDocument)
847 NotifyTextEntry(master, this, editBox.contents, true);
848 editBox.modifiedDocument = false;
851 OnCloseDropDown(pulledWindow);
853 button.checked = false;
854 NotifyClose(master, this);
860 bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
866 // Add code to set text to list box contents if it has an editbox
870 OnCloseDropDown(pulledWindow);
872 button.checked = false;
873 mods.closingDropDown = true;
874 if(!NotifyClose(master, this))
880 currentRow = (row && !row.noneRow) ? row : null;
881 if(style.editText && style.changeContents)
883 char tempString[4096];
885 editBox.contents = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)dataType._vTbl[__ecereVMethodID_class_OnGetString])(dataType, currentRow.GetData(null), tempString, null, null);
887 editBox.contents = "";
892 result = NotifySelect(master, this, currentRow, mods);
897 bool NotifyHighlight(ListBox control, DataRow row, Modifiers mods)
899 return NotifyHighlight(master, this, row, mods);
902 bool NotifyKeyDown(ListBox listBox, DataRow selection, Key key, unichar ch)
904 return OnKeyDown(key, ch);
912 listBox.font = { font.faceName, font.size };
925 ColorAlpha selectionColor, selectionText;
927 selectionColor = SELECTION_COLOR;
928 selectionText = SELECTION_TEXT;